-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit-ext-exec
113 lines (97 loc) · 2.42 KB
/
git-ext-exec
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
#!/bin/bash
#|
#| Executes a command in all given directories that are either read from a file
#| or from STDIN.
#|
#| Usage:
#|
#| git-ext-exec [OPTION... --] COMMAND
#|
#| Options:
#| -b, --break break execution on an error immediately
#| -f, --file FILE read the directory list from the FILE instead of STDIN
#| -g, --generate dump the command script to STDIN instead of running it
#| -h, --help show this help and exit
#| -v, --verbose print the directory to execute COMMAND in to STDERR
#|
#| If any options are provided, use -- to separate the option list from the
#| COMMAND to execute. When the COMMAND is more complex, rather employ -s option
#| and pass the command as a single argument, which is then executed as a script.
#| However, -s option can obscure the exit code of the command sometimes, so that
#| -b option might not be effective.
#|
#| This command is useful for combining with git-ext-list that can supply the list
#| of Git repositories and the command is then executed in their context, e.g.:
#|
#| git-ext-each | git-ext-exec git pull
BREAK=0
DIRS=-
EXEC=
VERBOSE=0
SCRIPT=
GENERATE=0
SCRIPT_FILE="${BASH_SOURCE[0]}"
while [[ -n "$1" ]]; do
case "$1" in
-b|--break)
BREAK=1
shift
;;
-f|--file)
DIRS="$2"
shift 2
;;
-g|--generate)
SCRIPT="/dev/stdout"
GENERATE=1
shift
;;
-h|--help)
sed '/#|.*/ s/^#|[[:space:]]\?//p;d' < "$SCRIPT_FILE"
exit 1
;;
-v|--verbose)
VERBOSE=1
shift
;;
--)
shift
break
;;
-*)
echo "Unknown option encountered: '$1'" >&2
exit 1
;;
*)
break
;;
esac
done
if [[ $GENERATE -eq 0 ]]; then
# Because tempfile is not available on MSYS2, let's use a rough approximation
SCRIPT="${TMPDIR:-/tmp}"/`date +'git-ext-exec-%s-%N.sh'`
fi
echo '#!'"$SHELL" > "$SCRIPT"
echo 'shopt -s expand_aliases' >> "$SCRIPT"
cat "$DIRS" | while read DIR; do
if [[ $VERBOSE -ne 0 ]]; then
echo 'echo '"$DIR"' >&2' >> "$SCRIPT"
fi
echo '# '"$DIR"'
(
cd '"$DIR"'
'"$@"'
)' >> "$SCRIPT"
if [[ $BREAK -ne 0 ]]; then
echo 'RESULT=$?
if [[ $RESULT -ne 0 ]]; then
exit $RESULT
fi
' >> "$SCRIPT"
fi
done
if [[ $GENERATE -eq 0 ]]; then
"$SHELL" -l -- "$SCRIPT"
# Should be trapped
rm "$SCRIPT"
fi