-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-ext-each
100 lines (87 loc) · 2 KB
/
git-ext-each
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
#|
#| Finds all Git repositories in the given directory tree and optionally executes
#| the given actions on them.
#|
#| Usage:
#|
#| git-ext-each [OPTION...] [-- FIND_MORE]
#|
#| Options:
#| -a, --after use the default action after FIND_MORE applied
#| -b, --before use the default action before FIND_MORE applied
#| -h, --help show this help and exit
#| -p, --parent remain in the parent directory to FIND_MORE
#| -r, --root DIR starts the search in DIR (default: .)
#|
#| Arguments:
#| FIND_MORE additional predicates for 'find'
#|
#| If FIND_MORE is missing, the default action prints the repository directory.
#| Otherwise FIND_MORE actions are applied, e.g., the following example prints
#| repositories that contain a pom.xml file only:
#|
#| git-ext-each -a -- -execdir test -f pom.xml \;
AFTER=0
BEFORE=0
PARENT=0
ROOT='.'
SCRIPT_FILE="${BASH_SOURCE[0]}"
while [[ -n "$1" ]]; do
case "$1" in
-a|--after)
AFTER=1
shift
;;
-b|--before)
BEFORE=1
shift
;;
-h|--help)
sed '/#|.*/ s/^#|[[:space:]]\?//p;d' < "$SCRIPT_FILE"
exit 1
;;
-p|--parent)
PARENT=1
shift
;;
-r|--root)
ROOT="$2"
shift 2
;;
--)
shift
break
;;
-*)
echo "Unknown option encountered: '$1'" >&2
exit 1
;;
*)
break
;;
esac
done
if [[ $PARENT -ne 0 ]]; then
DEFAULT_PRINT="-print"
else
DEFAULT_PRINT="-printf %h\n"
fi
AFTER_PRINT=
BEFORE_PRINT=
if [[ $AFTER -ne 0 ]]; then
AFTER_PRINT=$DEFAULT_PRINT
fi
if [[ $BEFORE -ne 0 ]]; then
BEFORE_PRINT=$DEFAULT_PRINT
fi
if [[ $# -ne 0 ]]; then
ACTION="$@"
else
ACTION=$DEFAULT_PRINT
fi
if [[ $PARENT -ne 0 ]]; then
find -L -- "$ROOT" -type d -execdir test -d {}/.git \; -prune $BEFORE_PRINT $ACTION $AFTER_PRINT
else
find -L -- "$ROOT" -type d -iname '.git' -prune $BEFORE_PRINT $ACTION $AFTER_PRINT
fi