-
Notifications
You must be signed in to change notification settings - Fork 1
/
git-multipush.sh
281 lines (259 loc) · 6.98 KB
/
git-multipush.sh
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/bin/sh
## git multipush Copyright 2010 Gavin Beatty <[email protected]>.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You can find the GNU General Public License at:
## http://www.gnu.org/licenses/
set -e
# @VERSION@
version_print() {
echo "git multipush version ${VERSION}"
}
SUBDIRECTORY_OK=Yes
OPTIONS_KEEPDASHDASH="yes"
OPTIONS_SPEC="\
git multipush [options] [<remote>..] [-- GIT_OPTIONS]
git multipush [options] -s <remote> [...]
git multipush [options] --unset
git multipush [options] -g
--
v,verbose print each command as it is run
e,error exit with error on the first git push error
n,dry-run don't run any commands, just print them
b,branch= directly passed on to git push as follows: git push <remote> <branch>
s,set the given remotes are put in a comma-separated list in the config in multipush.<branch>.remotes
unset unset multipush.<branch>.remotes
g,get print each entry in multipush.<branch>.remotes on its own line
system passed on to git config
global passed on to git config
file= passed on to git config
z,null with -g|--get, print with \\000 terminator instead of \\n
d,debug print debug info
version print version info in 'git multipush version \$version' format"
. "$(git --exec-path)/git-sh-setup"
debug_run() {
if test -n "$debug" ; then
printf "debug:" >&2
"$@" >&2
fi
}
verbose_print() {
if test -n "$verbose" ; then
echo "$@" >&2
fi
}
run() {
if test -z "$dryrun" ; then
"$@"
fi
}
doit() {
debug_run sq "$@"
verbose_print "$@"
run "$@"
}
sq() {
git rev-parse --sq-quote "$@"
}
evalit() {
e=0
eval "$1" || e=$?
if test "$e" -ne 0 ; then
if test -n "$fail" ; then
exit "$e"
else
exxit="$e"
fi
fi
return 0
}
ref() {
# doit can't be used because of bug... fix that
git symbolic-ref "$1"
}
comma_assert() {
for i in "$@" ; do
if echo "$i" | grep -F -q ',' ; then
die "Cannot add a default remote with a comma in its name"
fi
done
}
assert_HEAD() {
debug_run echo ' using HEAD as branch'
if ! git rev-parse --verify -q HEAD >/dev/null ; then
die "Cannot work with detached HEAD and no <branch> given."
fi
}
git_config() {
if test -n "${file=}" ; then
file=" $(sq --file "$file")"
fi
if test -n "${system=}" ; then
system=" $(sq "$system")"
fi
if test -n "${global=}" ; then
global=" $(sq "$global")"
fi
eval "doit git config${system}${global}${file} $(sq "$@")"
}
main() {
dryrun=""
verbose=""
fail=""
debug=""
branch=""
set_opt=""
unset_opt=""
get_opt=""
term="\n"
while test $# -ne 0 ; do
case "$1" in
-v|--verbose)
verbose="true"
;;
-e|--error)
fail="true"
;;
-n|--dry-run)
dryrun="true"
verbose="true"
;;
-b|--branch)
branch="$2"
shift
;;
-s|--set)
set_opt="true"
;;
--unset)
unset_opt="true"
;;
-g|--get)
get_opt="true"
;;
--system)
system="--system"
;;
--global)
global="--global"
;;
--file)
file="$2"
shift
;;
-z|--null)
term="\000"
;;
-d|--debug)
debug="true"
verbose=""
;;
--version)
version_print
exit 0
;;
--)
shift
break
;;
esac
shift
done
if test -n "$set_opt" ; then
if test $# -eq 0 ; then
die "Must give at least one <remote> with -s|--set"
fi
remote_commas="$1"
shift
comma_assert "$remote_commas"
for rem in "$@" ; do
comma_assert "$rem"
remote_commas="${remote_commas},${rem}"
done
if test -z "$branch" ; then
assert_HEAD
branch="$(ref "HEAD" | sed -e 's|^refs/heads/||')"
fi
git_config "multipush.${branch}.remotes" "$remote_commas"
exit 0
elif test -n "$unset_opt" ; then
if test $# -ne 0 ; then
die "Unexpected arguments given with --unset"
fi
if test -z "$branch" ; then
assert_HEAD
branch="$(ref "HEAD" | sed -e 's|^refs/heads/||')"
fi
git_config --unset "multipush.${branch}.remotes"
exit 0
elif test -n "$get_opt" ; then
if test -z "$branch" ; then
assert_HEAD
branch="$(ref "HEAD" | sed -e 's|^refs/heads/||')"
fi
remotes="$(git_config "multipush.${branch}.remotes" | sed -e 's/,/ /g')"
for rem in $remotes ; do
printf "${rem}${term}"
done
exit 0
fi
while test $# -gt 0 ; do
if test x"$1" = x"--" ; then
shift
break
fi
remotes="${remotes- }$(sq "$1")"
shift
done
while test $# -gt 0 ; do
git_opts="${git_opts- }$(sq "$1")"
shift
done
eval set -- "${remotes-}"
exxit=0
if test $# -gt 0 ; then
for rem in "$@" ; do
if test -z "${branch-}" ; then
evalit "doit git push${git_opts-} $(sq "$rem")"
else
evalit "doit git push${git_opts-} $(sq "$rem") $(sq "$branch")"
fi
done
else
e=0
if test -z "$branch" ; then
assert_HEAD
hbranch="$(ref "HEAD" | sed -e 's|^refs/heads/||')"
else
hbranch="$branch"
fi
remote_commas="$(git_config "multipush.${hbranch}.remotes" || e=$?)"
debug_run echo " multipush.${hbranch}.remotes =$(sq "$remote_commas")"
if test "$e" -eq 0 && test -n "${remote_commas-}" ; then
remotes="$(echo "$remote_commas" | sed -e 's/,/ /g')"
for rem in $remotes ; do
evalit "doit git push${git_opts-}$(sq "$rem")$(sq "$hbranch")"
done
else
if test -z "${branch-}" ; then
evalit "doit git push${git_opts-}"
else
evalit "doit git push${git_opts-} origin$(sq "$hbranch")"
fi
fi
fi
exit "$exxit"
}
trap "echo \"caught SIGINT\" ; exit 1 ;" INT
trap "echo \"caught SIGTERM\" ; exit 1 ;" TERM
trap "echo \"caught SIGHUP\" ; exit 1 ;" HUP
main "$@"