]> git.ipfire.org Git - thirdparty/git.git/blame - git-am.sh
Windows: Fix intermittent failures of t7701
[thirdparty/git.git] / git-am.sh
CommitLineData
d1c5f2a4
JH
1#!/bin/sh
2#
d64e6b04 3# Copyright (c) 2005, 2006 Junio C Hamano
d1c5f2a4 4
c1491841 5SUBDIRECTORY_OK=Yes
78443d90
PH
6OPTIONS_KEEPDASHDASH=
7OPTIONS_SPEC="\
588c038a 8git am [options] [<mbox>|<Maildir>...]
09f8d055 9git am [options] (--resolved | --skip | --abort)
78443d90 10--
e72c7406 11d,dotest= (removed -- do not use)
66006c63 12i,interactive run interactively
cb3a160d 13b,binary (historical option -- no-op)
78443d90
PH
143,3way allow fall back on 3way merging if needed
15s,signoff add a Signed-off-by line to the commit message
16u,utf8 recode into utf8 (default)
fe1fa946 17k,keep pass -k flag to git-mailinfo
78443d90 18whitespace= pass it through git-apply
b47dfe9e 19directory= pass it through git-apply
78443d90
PH
20C= pass it through git-apply
21p= pass it through git-apply
b80da424 22reject pass it through git-apply
78443d90
PH
23resolvemsg= override error message when patch failure occurs
24r,resolved to be used after a patch failure
3041c324 25skip skip the current patch
3e5057a8 26abort restore the original branch and abort the patching operation.
3041c324 27rebasing (internal use for git-rebase)"
78443d90 28
cf1fe88c 29. git-sh-setup
bb034f83 30prefix=$(git rev-parse --show-prefix)
f9474132 31set_reflog_action am
7eff28a9 32require_work_tree
c1491841 33cd_to_toplevel
d1c5f2a4 34
460abee5
SB
35git var GIT_COMMITTER_IDENT >/dev/null ||
36 die "You need to set your committer info first"
d64e6b04 37
b47dfe9e
JH
38sq () {
39 for sqarg
40 do
41 printf "%s" "$sqarg" |
17f26a9e 42 sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
b47dfe9e
JH
43 done
44}
45
d1c5f2a4
JH
46stop_here () {
47 echo "$1" >"$dotest/next"
48 exit 1
49}
50
ced9456a 51stop_here_user_resolve () {
cc120056 52 if [ -n "$resolvemsg" ]; then
a23bfaed 53 printf '%s\n' "$resolvemsg"
cc120056
SE
54 stop_here $1
55 fi
75e1645f 56 cmdline="git am"
ced9456a
RS
57 if test '' != "$interactive"
58 then
59 cmdline="$cmdline -i"
60 fi
61 if test '' != "$threeway"
62 then
63 cmdline="$cmdline -3"
64 fi
ced9456a
RS
65 echo "When you have resolved this problem run \"$cmdline --resolved\"."
66 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
3e5057a8 67 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
ced9456a
RS
68
69 stop_here $1
70}
71
d1c5f2a4
JH
72go_next () {
73 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
74 "$dotest/patch" "$dotest/info"
75 echo "$next" >"$dotest/next"
76 this=$next
77}
78
fd7bcfb5
JH
79cannot_fallback () {
80 echo "$1"
81 echo "Cannot fall back to three-way merge."
82 exit 1
83}
84
d1c5f2a4
JH
85fall_back_3way () {
86 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
87
88 rm -fr "$dotest"/patch-merge-*
89 mkdir "$dotest/patch-merge-tmp-dir"
90
91 # First see if the patch records the index info that we can use.
7a988699
JS
92 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
93 "$dotest/patch" &&
fd7bcfb5 94 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
5be60078 95 git write-tree >"$dotest/patch-merge-base+" ||
b1440cc8 96 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
fd7bcfb5
JH
97
98 echo Using index info to reconstruct a base tree...
99 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
cb3a160d 100 git apply --cached <"$dotest/patch"
d1c5f2a4 101 then
d1c5f2a4
JH
102 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
103 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
fd7bcfb5
JH
104 else
105 cannot_fallback "Did you hand edit your patch?
106It does not apply to blobs recorded in its index."
d1c5f2a4
JH
107 fi
108
109 test -f "$dotest/patch-merge-index" &&
5be60078 110 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
d1c5f2a4
JH
111 orig_tree=$(cat "$dotest/patch-merge-base") &&
112 rm -fr "$dotest"/patch-merge-* || exit 1
113
114 echo Falling back to patching base and 3-way merge...
115
116 # This is not so wrong. Depending on which base we picked,
117 # orig_tree may be wildly different from ours, but his_tree
118 # has the same set of wildly different changes in parts the
579c9bb1 119 # patch did not touch, so recursive ends up canceling them,
d1c5f2a4
JH
120 # saying that we reverted all those changes.
121
2e6e3e82 122 eval GITHEAD_$his_tree='"$FIRSTLINE"'
579c9bb1
SP
123 export GITHEAD_$his_tree
124 git-merge-recursive $orig_tree -- HEAD $his_tree || {
b4372ef1 125 git rerere
d1c5f2a4
JH
126 echo Failed to merge in the changes.
127 exit 1
128 }
579c9bb1 129 unset GITHEAD_$his_tree
d1c5f2a4
JH
130}
131
132prec=4
51ef1daa 133dotest="$GIT_DIR/rebase-apply"
a2003379 134sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
3f0a8f3c 135resolvemsg= resume=
67dad687 136git_apply_opt=
d1c5f2a4 137
822f7c73 138while test $# != 0
d1c5f2a4
JH
139do
140 case "$1" in
78443d90
PH
141 -i|--interactive)
142 interactive=t ;;
143 -b|--binary)
cb3a160d 144 : ;;
78443d90
PH
145 -3|--3way)
146 threeway=t ;;
dfdd7e66 147 -s|--signoff)
78443d90
PH
148 sign=t ;;
149 -u|--utf8)
150 utf8=t ;; # this is now default
151 --no-utf8)
152 utf8= ;;
153 -k|--keep)
154 keep=t ;;
155 -r|--resolved)
156 resolved=t ;;
157 --skip)
158 skip=t ;;
3e5057a8
NS
159 --abort)
160 abort=t ;;
3041c324 161 --rebasing)
cb3a160d 162 rebasing=t threeway=t keep=t ;;
78443d90 163 -d|--dotest)
e72c7406
JH
164 die "-d option is no longer supported. Do not use."
165 ;;
78443d90
PH
166 --resolvemsg)
167 shift; resolvemsg=$1 ;;
b47dfe9e
JH
168 --whitespace|--directory)
169 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
78443d90 170 -C|-p)
b47dfe9e 171 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
b80da424 172 --reject)
173 git_apply_opt="$git_apply_opt $1" ;;
d1c5f2a4 174 --)
78443d90 175 shift; break ;;
d1c5f2a4 176 *)
78443d90 177 usage ;;
d1c5f2a4 178 esac
78443d90 179 shift
d1c5f2a4
JH
180done
181
0c15cc92
JH
182# If the dotest directory exists, but we have finished applying all the
183# patches in them, clear it out.
d1c5f2a4
JH
184if test -d "$dotest" &&
185 last=$(cat "$dotest/last") &&
186 next=$(cat "$dotest/next") &&
187 test $# != 0 &&
188 test "$next" -gt "$last"
189then
190 rm -fr "$dotest"
191fi
192
193if test -d "$dotest"
194then
3e5057a8 195 case "$#,$skip$resolved$abort" in
c95b1389
JH
196 0,*t*)
197 # Explicit resume command and we do not have file, so
198 # we are happy.
199 : ;;
200 0,)
201 # No file input but without resume parameters; catch
202 # user error to feed us a patch from standard input
e72c7406 203 # when there is already $dotest. This is somewhat
c95b1389
JH
204 # unreliable -- stdin could be /dev/null for example
205 # and the caller did not intend to feed us a patch but
206 # wanted to continue unattended.
207 tty -s
208 ;;
209 *)
210 false
211 ;;
212 esac ||
28ed6e7b 213 die "previous rebase directory $dotest still exists but mbox given."
271440e3 214 resume=yes
3e5057a8 215
95f8ebbf
OM
216 case "$skip,$abort" in
217 t,)
218 git rerere clear
219 git read-tree --reset -u HEAD HEAD
220 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
221 git reset HEAD
222 git update-ref ORIG_HEAD $orig_head
223 ;;
224 ,t)
3e5057a8
NS
225 git rerere clear
226 git read-tree --reset -u HEAD ORIG_HEAD
227 git reset ORIG_HEAD
228 rm -fr "$dotest"
229 exit ;;
230 esac
d1c5f2a4 231else
3e5057a8
NS
232 # Make sure we are not given --skip, --resolved, nor --abort
233 test "$skip$resolved$abort" = "" ||
cc120056 234 die "Resolve operation not in progress, we are not resuming."
d1c5f2a4
JH
235
236 # Start afresh.
237 mkdir -p "$dotest" || exit
238
bb034f83
JH
239 if test -n "$prefix" && test $# != 0
240 then
241 first=t
242 for arg
243 do
244 test -n "$first" && {
245 set x
246 first=
247 }
248 case "$arg" in
249 /*)
250 set "$@" "$arg" ;;
251 *)
252 set "$@" "$prefix$arg" ;;
253 esac
254 done
255 shift
256 fi
5be60078 257 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
d1c5f2a4
JH
258 rm -fr "$dotest"
259 exit 1
260 }
261
22db240d 262 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
a2003379 263 # for the resuming session after a patch failure.
22db240d 264 # -i can and must be given when resuming.
9b9f5a22 265 echo " $git_apply_opt" >"$dotest/apply-opt"
22db240d 266 echo "$threeway" >"$dotest/threeway"
d1c5f2a4
JH
267 echo "$sign" >"$dotest/sign"
268 echo "$utf8" >"$dotest/utf8"
269 echo "$keep" >"$dotest/keep"
d1c5f2a4 270 echo 1 >"$dotest/next"
3041c324
JH
271 if test -n "$rebasing"
272 then
273 : >"$dotest/rebasing"
274 else
275 : >"$dotest/applying"
22e40795 276 git update-ref ORIG_HEAD HEAD
3041c324 277 fi
d1c5f2a4
JH
278fi
279
0c15cc92
JH
280case "$resolved" in
281'')
38762c47 282 files=$(git diff-index --cached --name-only HEAD --) || exit
0c15cc92
JH
283 if [ "$files" ]; then
284 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
285 exit 1
286 fi
287esac
288
d1c5f2a4
JH
289if test "$(cat "$dotest/utf8")" = t
290then
291 utf8=-u
bb1091a4
JH
292else
293 utf8=-n
d1c5f2a4
JH
294fi
295if test "$(cat "$dotest/keep")" = t
296then
297 keep=-k
298fi
22db240d
JH
299if test "$(cat "$dotest/threeway")" = t
300then
301 threeway=t
302fi
9b9f5a22 303git_apply_opt=$(cat "$dotest/apply-opt")
d1c5f2a4
JH
304if test "$(cat "$dotest/sign")" = t
305then
5354a56f 306 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
d1c5f2a4
JH
307 s/>.*/>/
308 s/^/Signed-off-by: /'
309 `
310else
311 SIGNOFF=
312fi
d1c5f2a4
JH
313
314last=`cat "$dotest/last"`
315this=`cat "$dotest/next"`
316if test "$skip" = t
317then
318 this=`expr "$this" + 1`
69224716 319 resume=
d1c5f2a4
JH
320fi
321
322if test "$this" -gt "$last"
323then
324 echo Nothing to do.
325 rm -fr "$dotest"
326 exit
327fi
328
329while test "$this" -le "$last"
330do
331 msgnum=`printf "%0${prec}d" $this`
332 next=`expr "$this" + 1`
333 test -f "$dotest/$msgnum" || {
69224716 334 resume=
d1c5f2a4
JH
335 go_next
336 continue
337 }
0c15cc92
JH
338
339 # If we are not resuming, parse and extract the patch information
340 # into separate files:
341 # - info records the authorship and title
342 # - msg is the rest of commit log message
343 # - patch is the patch body.
344 #
345 # When we are resuming, these files are either already prepared
346 # by the user, or the user can tell us to do so by --resolved flag.
271440e3
JH
347 case "$resume" in
348 '')
5be60078 349 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
271440e3
JH
350 <"$dotest/$msgnum" >"$dotest/info" ||
351 stop_here $this
ca193cf1
JS
352
353 # skip pine's internal folder data
354 grep '^Author: Mail System Internal Data$' \
355 <"$dotest"/info >/dev/null &&
356 go_next && continue
357
28ed6e7b 358 test -s "$dotest/patch" || {
6777c380 359 echo "Patch is empty. Was it split wrong?"
87ab7992
DZ
360 stop_here $this
361 }
5e835cac
JH
362 if test -f "$dotest/rebasing" &&
363 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
364 -e q "$dotest/$msgnum") &&
365 test "$(git cat-file -t "$commit")" = commit
366 then
367 git cat-file commit "$commit" |
368 sed -e '1,/^$/d' >"$dotest/msg-clean"
369 else
370 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
371 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
372
41a3e3aa 373 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
5e835cac
JH
374 git stripspace > "$dotest/msg-clean"
375 fi
271440e3
JH
376 ;;
377 esac
d1c5f2a4
JH
378
379 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
380 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
381 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
e0e3ba20 382
3b78959e 383 if test -z "$GIT_AUTHOR_EMAIL"
e0e3ba20 384 then
3b78959e 385 echo "Patch does not have a valid e-mail address."
e0e3ba20
JH
386 stop_here $this
387 fi
388
2c674191 389 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
d1c5f2a4 390
4bfb6b62
JH
391 case "$resume" in
392 '')
393 if test '' != "$SIGNOFF"
394 then
d1c5f2a4 395 LAST_SIGNED_OFF_BY=`
4bfb6b62
JH
396 sed -ne '/^Signed-off-by: /p' \
397 "$dotest/msg-clean" |
b4ce54fc 398 sed -ne '$p'
d1c5f2a4 399 `
4bfb6b62
JH
400 ADD_SIGNOFF=`
401 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
d1c5f2a4
JH
402 test '' = "$LAST_SIGNED_OFF_BY" && echo
403 echo "$SIGNOFF"
4bfb6b62
JH
404 }`
405 else
d1c5f2a4 406 ADD_SIGNOFF=
4bfb6b62
JH
407 fi
408 {
d1c5f2a4
JH
409 if test -s "$dotest/msg-clean"
410 then
d1c5f2a4
JH
411 cat "$dotest/msg-clean"
412 fi
413 if test '' != "$ADD_SIGNOFF"
414 then
415 echo "$ADD_SIGNOFF"
416 fi
4bfb6b62
JH
417 } >"$dotest/final-commit"
418 ;;
0c15cc92 419 *)
6d28644d 420 case "$resolved$interactive" in
0c15cc92
JH
421 tt)
422 # This is used only for interactive view option.
38762c47 423 git diff-index -p --cached HEAD -- >"$dotest/patch"
0c15cc92
JH
424 ;;
425 esac
4bfb6b62 426 esac
d1c5f2a4 427
4bfb6b62 428 resume=
d1c5f2a4
JH
429 if test "$interactive" = t
430 then
a1451104
JH
431 test -t 0 ||
432 die "cannot be interactive without stdin connected to a terminal."
d1c5f2a4
JH
433 action=again
434 while test "$action" = again
435 do
436 echo "Commit Body is:"
437 echo "--------------------------"
438 cat "$dotest/final-commit"
439 echo "--------------------------"
9754563c 440 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
d1c5f2a4
JH
441 read reply
442 case "$reply" in
f89ad67f
JH
443 [yY]*) action=yes ;;
444 [aA]*) action=yes interactive= ;;
445 [nN]*) action=skip ;;
ef0c2abf 446 [eE]*) git_editor "$dotest/final-commit"
d1c5f2a4 447 action=again ;;
f89ad67f
JH
448 [vV]*) action=again
449 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
450 *) action=again ;;
d1c5f2a4
JH
451 esac
452 done
453 else
454 action=yes
455 fi
1d9b2656 456 FIRSTLINE=$(sed 1q "$dotest/final-commit")
d1c5f2a4
JH
457
458 if test $action = skip
459 then
460 go_next
461 continue
462 fi
463
464 if test -x "$GIT_DIR"/hooks/applypatch-msg
465 then
466 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
467 stop_here $this
468 fi
469
c8fe1980 470 printf 'Applying: %s\n' "$FIRSTLINE"
d1c5f2a4 471
0c15cc92
JH
472 case "$resolved" in
473 '')
b47dfe9e 474 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
0c15cc92
JH
475 apply_status=$?
476 ;;
477 t)
478 # Resolved means the user did all the hard work, and
479 # we do not have to do any patch application. Just
480 # trust what the user has in the index file and the
481 # working tree.
482 resolved=
38762c47 483 git diff-index --quiet --cached HEAD -- && {
64646d11 484 echo "No changes - did you forget to use 'git add'?"
ced9456a 485 stop_here_user_resolve $this
06aff47b 486 }
5be60078 487 unmerged=$(git ls-files -u)
c1d1128b
JH
488 if test -n "$unmerged"
489 then
490 echo "You still have unmerged paths in your index"
64646d11 491 echo "did you forget to use 'git add'?"
ced9456a 492 stop_here_user_resolve $this
c1d1128b 493 fi
0c15cc92 494 apply_status=0
b4372ef1 495 git rerere
0c15cc92
JH
496 ;;
497 esac
498
d1c5f2a4
JH
499 if test $apply_status = 1 && test "$threeway" = t
500 then
73319032 501 if (fall_back_3way)
d1c5f2a4 502 then
73319032
JH
503 # Applying the patch to an earlier tree and merging the
504 # result may have produced the same tree as ours.
38762c47 505 git diff-index --quiet --cached HEAD -- && {
06aff47b
AR
506 echo No changes -- Patch already applied.
507 go_next
508 continue
509 }
73319032
JH
510 # clear apply_status -- we have successfully merged.
511 apply_status=0
d1c5f2a4
JH
512 fi
513 fi
514 if test $apply_status != 0
515 then
4d2e283a 516 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
ced9456a 517 stop_here_user_resolve $this
d1c5f2a4
JH
518 fi
519
520 if test -x "$GIT_DIR"/hooks/pre-applypatch
521 then
522 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
523 fi
524
5be60078 525 tree=$(git write-tree) &&
5be60078
JH
526 parent=$(git rev-parse --verify HEAD) &&
527 commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
2e6e3e82 528 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
d1c5f2a4
JH
529 stop_here $this
530
531 if test -x "$GIT_DIR"/hooks/post-applypatch
532 then
533 "$GIT_DIR"/hooks/post-applypatch
534 fi
535
536 go_next
537done
538
e0cd252e
MS
539git gc --auto
540
d1c5f2a4 541rm -fr "$dotest"