-
Notifications
You must be signed in to change notification settings - Fork 1
/
ggc-wikis
executable file
·100 lines (83 loc) · 1.79 KB
/
ggc-wikis
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
#!/bin/bash
set -euo pipefail
IFS=
# shellcheck disable=SC2088
wikis='~/*/*wiki/.git'
# ggc from chj-bin is better (reorders arguments, too)
#ggc () {
# git grep --cached -E "$@" | cat # avoid pager
#}
usage () {
echo "$0 searcharguments"
echo " runs ggc (git grep --cached -E) with the given arguments"
echo " on the wiki repositories at $wikis"
echo " Env vars:"
echo " ABS=1 print absolute paths, and print separators to stderr instead"
echo " Z=1 add -z to git grep, print absolute paths, and don't print separators"
exit 1
}
if [[ $# = 0 || $1 = -h || $1 = --help ]]; then
usage
fi
searcharguments=("$@")
perhaps_abs () {
if [[ ${Z-} = 1 ]]; then
W="$1" perl -0 -wne 'print "$ENV{W}/$_" or die'
elif [[ ${ABS-} = 1 ]]; then
W="$1" perl -wne 'print "$ENV{W}/$_" or die'
else
cat
fi
}
note () {
# When asked for absolute paths, don't spoil STDOUT with notes
if ! [[ ${Z-} = 1 ]]; then
if [[ ${ABS-} = 1 ]]; then
echo "$@" >&2
else
echo "$@"
fi
fi
}
ggcZ () {
if [[ ${Z-} = 1 ]]; then
ggc -z "$@"
else
ggc "$@"
fi
}
gw () {
w=`dirname "$1"`
(
set +e
note "== $w `perl -we 'print "="x($ARGV[1]-7-length($ARGV[0]))' "$w" "${COLUMNS-80}"`"
cd "$w"
ggcZ "${searcharguments[@]}" | perhaps_abs "$w"
note
) || true
}
setvar () {
varname="$1"
val="$2"
code=$varname=$(printf '%q'"$val")
eval "hash_$code"
}
seen_set () {
key="$1"
varname=$(echo "$key" | md5sum | awk '{print $1}')
setvar "$varname" 1
}
seen_ref () {
key="$1"
varname=$(echo "$key" | md5sum | awk '{print $1}')
eval "test \${hash_$varname-0} -eq 1"
}
eval '
for w in '"$wikis"'; do
p=$(readlink -f "$w")
if ! seen_ref "$p"; then
seen_set "$p"
gw "$w";
fi
done
'