{{ message }}
Repository navigation
Expand file tree
/
Copy pathgit-push-update
More file actions
executable file
·376 lines (339 loc) · 9.55 KB
/
Copy pathgit-push-update
File metadata and controls
executable file
·376 lines (339 loc) · 9.55 KB
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/bin/sh
set -e
if ! command -v mktemp >/dev/null
then
mktemp()
{
local res
if test "$#" -eq 1 -a "$1" = "-d"
then
res="$TMP/$RANDOM"
if mkdir "$res"
then
printf "%s\n" "$res"
return
else
echo "Fail to create temporary directory" >&2
exit 1
fi
else
echo "Unsupported mktemp invocation: $@" >&2
exit 1
fi
}
fi
checkout_from_quote()
{
sed -e 's/^"\(.*\)"$/\1/' \
-e 's/(\\.)\+/*/g' \
-e 's/ $/\\ /' \
-e 's/^/\//'
}
# remote specified ==> use it
# not specified ==> take remote specified for HEAD, if it is defined
# always error if the tracking branch does not contain branch to push
resolve_remote_branch()
{
local cmd remotename0 branchname branchname_full
remotename0="$1"
if test -n "$remotename0"
then
REMOTE_REF=$(git rev-parse --symbolic-full-name "$remotename0")
case "$REMOTE_REF" in
refs/remotes/*);;
*)
echo "Invalid remote: $remotename0"
echo 1
;;
esac
cmd=$(git for-each-ref --shell --format='
if test "$REMOTE_REF" -eq %(upstream)
then
if test -n "$branchname"
then
echo "Both \"$branchname\" and "%(refname:short)" track $REMOTE_REF"
exit 1
fi
branchname=%(refname:short)
fi
' /refs/heads)
eval "$cmd"
if test -z "$branchname"
then
echo "No local branch track $REMOTE_REF"
exit 1
fi
else
branchname_full=$(git rev-parse --symbolic-full-name HEAD)
if test -z "$branchname_full"
then
echo "Cannot find remote branch with detached head"
exit 1
fi
cmd=$(git for-each-ref --shell --format='
branchname=%(refname:short)
REMOTE_REF=%(upstream)
' "$branchname_full")
eval "$cmd"
fi
if ! git merge-base --is-ancestor "$UPDATE_HEAD" "$branchname"
then
echo "Commit to push $UPDATE_HEAD does not belong to branch tracking $REMOTE_REF"
echo "Suspect mistake"
exit 1
fi
REMOTE=$(git config branch."$branchname".pushRemote || git config branch."$branchname".remote)
REMOTE_BRANCH=$(git config branch."$branchname".merge)
if test -z "$REMOTE" -o -z "$REMOTE_BRANCH"
then
echo "Failed to find remote info for $branchname: $REMOTE,$REMOTE_BRANCH"
exit 1
fi
}
prepare_MERGE()
{
local src
if test -n "$1"
then src="$1"
else src=HEAD
fi
if test "$#" -gt 1
then
echo "Only 1 source branch is allowed for merge type"
exit 1
fi
MERGE_LOCAL_NAME=$(git rev-parse --symbolic-full-name "$src" | sed -e 's|^refs/heads/||')
if test -z "$MERGE_LOCAL_NAME"
then
echo "Cannot resolve local branch: $src"
exit 1
fi
MERGE_LOCAL_HASH=$(git rev-parse --revs-only "$src")
MERGE_PREVIOUS_HASH=
UPDATE_HEAD="$MERGE_LOCAL_HASH"
}
prepare2_MERGE()
{
MERGE_REMOTE_BRANCH="$REMOTE_BRANCH"
}
ff_possible_MERGE()
{
local actual_lp expect_lp
actual_lp=$(git log --first-parent --pretty=format:%P "$REMOTE_CURRENT".."$UPDATE_HEAD"|tail -1|cut -d' ' -f1)
expect_lp=$(git rev-parse --revs-only "$REMOTE_CURRENT")
test "$actual_lp" = "$expect_lp"
}
git_checkout_skipall()
{
local dst_ref_full dst_hash
dst_ref_full="$1"
dst_hash="$2"
if test -n "$dst_hash"
then git update-ref "$dst_ref_full" "$dst_hash"
fi
git symbolic-ref HEAD "$dst_ref_full"
git ls-tree -r -z HEAD | git update-index -z --index-info
git ls-files -z|git update-index --skip-worktree -z --stdin
}
update_MERGE()
{
local dst src
dst="$1"
src="$MERGE_LOCAL_HASH"
git update-ref "refs/remotes/$OPT_HOST/$MERGE_LOCAL_NAME" "$src"
git_checkout_skipall "$MERGE_REMOTE_BRANCH" "$dst"
if test -z "$PUSH_COMMIT"
then
git merge --no-ff --edit "$OPT_HOST/$MERGE_LOCAL_NAME"
else
git merge --no-ff --no-commit "$OPT_HOST/$MERGE_LOCAL_NAME"
git commit -q --no-edit -c "$PUSH_COMMIT"
fi
}
prepare_REBASE()
{
local tmp
if test "$#" -eq 0
then set HEAD
fi
REBASE_HEAD=$(git rev-parse --revs-only "$@" | sed -e '/^\^[0-9a-f]\+$/d')
REBASE_LIMIT=$(git rev-parse --revs-only "$@" | sed -e '/^[0-9a-f]\+$/d' -e 's/\^//')
if test -z "$REBASE_HEAD"
then
echo "Cannot resolve source spec: $src"
exit 1
else
tmp=$(git rev-parse --revs-only "$REBASE_HEAD")
if test -z "$tmp"
then
echo "Cannot resolve source spec: $src"
exit 1
fi
fi
if test -n "$REBASE_LIMIT"
then
tmp=$(git rev-parse --revs-only "$REBASE_LIMIT")
if test -z "$tmp"
then
echo "Cannot resolve source spec: $src"
exit 1
fi
fi
UPDATE_HEAD="$REBASE_HEAD"
}
prepare2_REBASE()
{
local limit
if test -z "$REBASE_LIMIT" -a "$OPT_NOFORKPOINT" != yes
then REBASE_LIMIT=$(git merge-base --fork-point "$REMOTE_REF" "$UPDATE_HEAD")
fi
limit="${REBASE_LIMIT:-$REMOTE_CURRENT}"
if git log --pretty=format:%p:%s "$limit".."$UPDATE_HEAD"|grep '^[^:]* [^:]*:'
then
echo "Cannot rebase merge commits" >&2
exit 1
fi
}
ff_possible_REBASE()
{
test -z "$REBASE_LIMIT" && git merge-base --is-ancestor "$REMOTE_CURRENT" "$UPDATE_HEAD"
}
update_REBASE()
{
local dst src changed
dst="$1"
src="$2"
git branch -f rebase "$REBASE_HEAD"
if test -n "$REBASE_LIMIT"
then
git rebase --keep-empty --onto "$dst" "$REBASE_LIMIT" rebase
changed=$(git cherry -v HEAD "$REBASE_HEAD" "$REBASE_LIMIT" | grep '^+' || true)
else
git rebase --keep-empty "$dst" rebase
changed=$(git cherry -v HEAD "$REBASE_HEAD" | grep '^+' || true)
fi
if test -n "$changed"
then
echo "Commits changed during rebase:"
echo "--------------------------"
echo "$changed" | sed 's/+ \([0-9a-f]\{7\}\)[0-9a-f]\+ /\1 /'
echo "--------------------------"
echo "They will cause conflicts at later rebases. Update your branch and try again"
exit 1
fi
}
try_pushupdate()
{
prepare2_$UPDATE_TYPE
if ff_possible_$UPDATE_TYPE
then
PUSH_COMMIT="$UPDATE_HEAD"
else
if test -d "$REPO_DIR/update-tmp-repo"
then rm -rf "$REPO_DIR/update-tmp-repo"
fi
REPO="$REPO_DIR/update-tmp-repo"
git clone -q --no-checkout --shared . "$REPO"
git push -q "$REPO" +"$UPDATE_HEAD":refs/heads/local +"$REMOTE_CURRENT":refs/heads/remote
(
cd "$REPO"
# cannot use it because of "error: Sparse checkout leaves no entry on working directory"
# git config core.sparseCheckout true
# echo "!/" >".git/info/sparse-checkout"
git_checkout_skipall refs/heads/remote
update_$UPDATE_TYPE remote local
)
test "$?" -eq 0
git fetch -q "$REPO" HEAD
PUSH_COMMIT=$(git rev-parse FETCH_HEAD)
fi
TRIES_LEFT=$(expr "$TRIES_LEFT" - 1 || true)
echo "Pushing..."
if git -c advice.pushFetchFirst=false push "$REMOTE" "$PUSH_COMMIT":"$REMOTE_BRANCH"
then exit 0
else
git fetch -q "$REMOTE" "$REMOTE_BRANCH"
REMOTE_OLD="$REMOTE_CURRENT"
REMOTE_CURRENT=$(git rev-parse FETCH_HEAD)
if test "$REMOTE_CURRENT" != "$PUSH_COMMIT"
then
if test "$REMOTE_OLD" != "$REMOTE_CURRENT"
then
echo "Remote branch was updated by other user during preparing new updates"
if test "$TRIES_LEFT" -gt 0
then
echo "Retrying"
return
fi
fi
exit 1
fi
fi
}
finalize()
{
rm -rf "$REPO_DIR/update-tmp-repo"
rmdir "$REPO_DIR" || true
}
OPTS_SPEC="\
git push-update [options] <src>
--
h,help Help
type!= Update type to use [merge(default),rebase]
dest!= Remote reference name to push to (default is tracked by current branch)
host!= Host to be mentioned in merge message (hostname by default)
tries!= How many times to retry if upstream updated (default 1 for rebase, 2 for merge)
no-fork-point! Don't look for forkpoint while rebasing without explicit limit"
# git push-update remoteName branch
eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- "$@")"
OPT_HOST=
OPT_TYPE=merge
OPT_DEST=
OPT_NOFORKPOINT=no
OPT_TRIES=
while test "$1" != "--"
do
case "$1" in
--help|-h) exit;;
--dest) OPT_DEST="$2"
shift;;
--host) OPT_HOST="$2"
shift;;
--type) OPT_TYPE="$2"
shift;;
--tries) OPT_TRIES="$2"
shift;;
--no-fork-point) OPT_NOFORKPOINT="yes";;
*) echo "Unexpected option: $1" >/dev/stderr
exit 1;;
esac
shift
done
if test $# -eq 1
then
eval "$(echo "$OPTS_SPEC" | git rev-parse --parseopt -- -h)"
exit
fi
case "$OPT_TYPE" in
merge) UPDATE_TYPE=MERGE
TRIES_LEFT="${OPT_TRIES:-2}";;
rebase) UPDATE_TYPE=REBASE
TRIES_LEFT="${OPT_TRIES:-1}";;
*) echo "Unknown update type: $type"
exit 1;;
esac
if test -z "$OPT_HOST"
then
OPT_HOST=$(hostname)
fi
shift
REPO_DIR=$(mktemp -d)
trap finalize 0
prepare_$UPDATE_TYPE "$@"
resolve_remote_branch "$OPT_DEST"
git fetch -q "$REMOTE" "$REMOTE_BRANCH"
REMOTE_CURRENT=$(git rev-parse FETCH_HEAD)
PUSH_COMMIT=
while true
do try_pushupdate
done
You can’t perform that action at this time.
