blob: 2cb698770ba03e52313259cd92dc95cd5756a2e4 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
#!/bin/sh
set -eu
usage() {
cat <<-'EOF'
Usage:
repos [-e DIR...] [-v] [DIRECTORY]
repos -h
EOF
}
help() {
cat <<-'EOF'
Options:
-e DIR exclude the given directory from traversing
-v enable verbose mode
-h, --help show this message
DIRECTORY the folder to traverse
Traverse DIRECTORY looking for VCS repositores. As soon as
one is found, stop recursing, and emit its name alongside its
type.
On verbose mode, print the directories being visited.
Examples:
Show all repositories under ~/dev/, excluding a few directories:
$ repos -e ~/dev/go/ -e ~/dev/quicklisp/ ~/dev/
# ...
EOF
}
for flag in "$@"; do
case "$flag" in
(--)
break
;;
(--help)
usage
help
exit
;;
(*)
;;
esac
done
# Similar to urlencode but only for % and \n (and not \0).
array_encode() {
sed 's/%/%25/g' | sed -e :a -e '$!N; s/\n/%0A/; ta'
}
array_decode() {
sed -e 's/%0A/\
/g' -e 's/%25/%/g'
}
#
# CAVEAT:
# To avoid needing to keep decoding the array elements on every call to
# `arr_includes`, assume directory names don't contain newlines. This makes the
# current code scanning ~/dev/ from 8 seconds go to 1 second.
#
arr_push() {
ARR="$1"
ELT="$2"
if [ -n "$ARR" ]; then
printf '%s\n' "$ARR"
fi
printf '%s\n' "$ELT" # | array_encode (see CAVEAT)
}
arr_includes() {
ARR="$1"
ELT="$2"
printf '%s\n' "$ARR" | while read -r el; do
# if [ "$(printf '%s\n' "$el" | array_decode)" = "$ELT" ]; then (see CAVEAT)
if [ "$el" = "$ELT" ]; then
return 2
fi
done
if [ $? = 2 ]; then
return 0
else
return 1
fi
}
EXCLUDE=
VERBOSE=false
while getopts 'e:vh' flag; do
case "$flag" in
(e)
case "$OPTARG" in
*/)
ARG="$OPTARG"
;;
*)
ARG="$OPTARG/"
;;
esac
EXCLUDE="$(arr_push "$EXCLUDE" "$ARG")"
;;
(v)
VERBOSE=true
;;
(h)
usage
help
exit
;;
(*)
usage >&2
exit 2
;;
esac
done
shift $((OPTIND - 1))
is_repository() {
TYPE="$(vcs -C "$1" -t 2>/dev/null)"
if [ -n "$TYPE" ]; then
printf '%s\n' "$1"
else
return 1
fi
}
traverse_directory() {
if [ "$VERBOSE" = true ]; then
printf 'cur: %s\n' "$1" >&2
fi
if arr_includes "$EXCLUDE" "$1"; then
return
fi
if is_repository "$1"; then
return
fi
for d in "$1"/*; do
if [ "$VERBOSE" = true ]; then
printf 'cur: %s\n' "$d" >&2
fi
if [ ! -d "$d" ]; then
continue
fi
if arr_includes "$EXCLUDE" "$d/"; then
continue
fi
if ! is_repository "$d"; then
traverse_directory "$d"
fi
done
}
if [ -z "${1:-}" ]; then
set -- "$PWD"
fi
for dir in "$@"; do
traverse_directory "${dir%%/}"
done
|