am: teach mercurial patch parser how to read from stdin
[git/mjg.git] / git-am.sh
blobf35710cb502543f35b642fd8a49f0d2cc8beba62
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 SUBDIRECTORY_OK=Yes
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_STUCKLONG=t
8 OPTIONS_SPEC="\
9 git am [options] [(<mbox>|<Maildir>)...]
10 git am [options] (--continue | --skip | --abort)
12 i,interactive run interactively
13 b,binary* (historical option -- no-op)
14 3,3way allow fall back on 3way merging if needed
15 q,quiet be quiet
16 s,signoff add a Signed-off-by line to the commit message
17 u,utf8 recode into utf8 (default)
18 k,keep pass -k flag to git-mailinfo
19 keep-non-patch pass -b flag to git-mailinfo
20 m,message-id pass -m flag to git-mailinfo
21 keep-cr pass --keep-cr flag to git-mailsplit for mbox format
22 no-keep-cr do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
23 c,scissors strip everything before a scissors line
24 whitespace= pass it through git-apply
25 ignore-space-change pass it through git-apply
26 ignore-whitespace pass it through git-apply
27 directory= pass it through git-apply
28 exclude= pass it through git-apply
29 include= pass it through git-apply
30 C= pass it through git-apply
31 p= pass it through git-apply
32 patch-format= format the patch(es) are in
33 reject pass it through git-apply
34 resolvemsg= override error message when patch failure occurs
35 continue continue applying patches after resolving a conflict
36 r,resolved synonyms for --continue
37 skip skip the current patch
38 abort restore the original branch and abort the patching operation.
39 committer-date-is-author-date lie about committer date
40 ignore-date use current timestamp for author date
41 rerere-autoupdate update the index with reused conflict resolution if possible
42 S,gpg-sign? GPG-sign commits
43 rebasing* (internal use for git-rebase)"
45 . git-sh-setup
46 . git-sh-i18n
47 prefix=$(git rev-parse --show-prefix)
48 set_reflog_action am
49 require_work_tree
50 cd_to_toplevel
52 git var GIT_COMMITTER_IDENT >/dev/null ||
53 die "$(gettext "You need to set your committer info first")"
55 if git rev-parse --verify -q HEAD >/dev/null
56 then
57 HAS_HEAD=yes
58 else
59 HAS_HEAD=
62 cmdline="git am"
63 if test '' != "$interactive"
64 then
65 cmdline="$cmdline -i"
67 if test '' != "$threeway"
68 then
69 cmdline="$cmdline -3"
72 sq () {
73 git rev-parse --sq-quote "$@"
76 stop_here () {
77 echo "$1" >"$dotest/next"
78 git rev-parse --verify -q HEAD >"$dotest/abort-safety"
79 exit 1
82 safe_to_abort () {
83 if test -f "$dotest/dirtyindex"
84 then
85 return 1
88 if ! test -s "$dotest/abort-safety"
89 then
90 return 0
93 abort_safety=$(cat "$dotest/abort-safety")
94 if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
95 then
96 return 0
98 gettextln "You seem to have moved HEAD since the last 'am' failure.
99 Not rewinding to ORIG_HEAD" >&2
100 return 1
103 stop_here_user_resolve () {
104 if [ -n "$resolvemsg" ]; then
105 printf '%s\n' "$resolvemsg"
106 stop_here $1
108 eval_gettextln "When you have resolved this problem, run \"\$cmdline --continue\".
109 If you prefer to skip this patch, run \"\$cmdline --skip\" instead.
110 To restore the original branch and stop patching, run \"\$cmdline --abort\"."
112 stop_here $1
115 go_next () {
116 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
117 "$dotest/patch" "$dotest/info"
118 echo "$next" >"$dotest/next"
119 this=$next
122 cannot_fallback () {
123 echo "$1"
124 gettextln "Cannot fall back to three-way merge."
125 exit 1
128 fall_back_3way () {
129 O_OBJECT=$(cd "$GIT_OBJECT_DIRECTORY" && pwd)
131 rm -fr "$dotest"/patch-merge-*
132 mkdir "$dotest/patch-merge-tmp-dir"
134 # First see if the patch records the index info that we can use.
135 cmd="git apply $git_apply_opt --build-fake-ancestor" &&
136 cmd="$cmd "'"$dotest/patch-merge-tmp-index" "$dotest/patch"' &&
137 eval "$cmd" &&
138 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
139 git write-tree >"$dotest/patch-merge-base+" ||
140 cannot_fallback "$(gettext "Repository lacks necessary blobs to fall back on 3-way merge.")"
142 say "$(gettext "Using index info to reconstruct a base tree...")"
144 cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
146 if test -z "$GIT_QUIET"
147 then
148 eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
151 cmd="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
152 if eval "$cmd"
153 then
154 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
155 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
156 else
157 cannot_fallback "$(gettext "Did you hand edit your patch?
158 It does not apply to blobs recorded in its index.")"
161 test -f "$dotest/patch-merge-index" &&
162 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
163 orig_tree=$(cat "$dotest/patch-merge-base") &&
164 rm -fr "$dotest"/patch-merge-* || exit 1
166 say "$(gettext "Falling back to patching base and 3-way merge...")"
168 # This is not so wrong. Depending on which base we picked,
169 # orig_tree may be wildly different from ours, but his_tree
170 # has the same set of wildly different changes in parts the
171 # patch did not touch, so recursive ends up canceling them,
172 # saying that we reverted all those changes.
174 eval GITHEAD_$his_tree='"$FIRSTLINE"'
175 export GITHEAD_$his_tree
176 if test -n "$GIT_QUIET"
177 then
178 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
180 git-merge-recursive $orig_tree -- HEAD $his_tree || {
181 git rerere $allow_rerere_autoupdate
182 die "$(gettext "Failed to merge in the changes.")"
184 unset GITHEAD_$his_tree
187 clean_abort () {
188 test $# = 0 || echo >&2 "$@"
189 rm -fr "$dotest"
190 exit 1
193 patch_format=
195 check_patch_format () {
196 # early return if patch_format was set from the command line
197 if test -n "$patch_format"
198 then
199 return 0
202 # we default to mbox format if input is from stdin and for
203 # directories
204 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
205 then
206 patch_format=mbox
207 return 0
210 # otherwise, check the first few non-blank lines of the first
211 # patch to try to detect its format
213 # Start from first line containing non-whitespace
215 while test -z "$l1"
217 read l1 || break
218 done
219 read l2
220 read l3
221 case "$l1" in
222 "From "* | "From: "*)
223 patch_format=mbox
225 '# This series applies on GIT commit'*)
226 patch_format=stgit-series
228 "# HG changeset patch")
229 patch_format=hg
232 # if the second line is empty and the third is
233 # a From, Author or Date entry, this is very
234 # likely an StGIT patch
235 case "$l2,$l3" in
236 ,"From: "* | ,"Author: "* | ,"Date: "*)
237 patch_format=stgit
241 esac
243 esac
244 if test -z "$patch_format" &&
245 test -n "$l1" &&
246 test -n "$l2" &&
247 test -n "$l3"
248 then
249 # This begins with three non-empty lines. Is this a
250 # piece of e-mail a-la RFC2822? Grab all the headers,
251 # discarding the indented remainder of folded lines,
252 # and see if it looks like that they all begin with the
253 # header field names...
254 tr -d '\015' <"$1" |
255 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
256 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
257 patch_format=mbox
259 } < "$1" || clean_abort
262 split_patches () {
263 case "$patch_format" in
264 mbox)
265 if test t = "$keepcr"
266 then
267 keep_cr=--keep-cr
268 else
269 keep_cr=
271 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
272 clean_abort
274 stgit-series)
275 if test $# -ne 1
276 then
277 clean_abort "$(gettext "Only one StGIT patch series can be applied at once")"
279 series_dir=$(dirname "$1")
280 series_file="$1"
281 shift
283 set x
284 while read filename
286 set "$@" "$series_dir/$filename"
287 done
288 # remove the safety x
289 shift
290 # remove the arg coming from the first-line comment
291 shift
292 } < "$series_file" || clean_abort
293 # set the patch format appropriately
294 patch_format=stgit
295 # now handle the actual StGIT patches
296 split_patches "$@"
298 stgit)
299 this=0
300 test 0 -eq "$#" && set -- -
301 for stgit in "$@"
303 this=$(expr "$this" + 1)
304 msgnum=$(printf "%0${prec}d" $this)
305 # Perl version of StGIT parse_patch. The first nonemptyline
306 # not starting with Author, From or Date is the
307 # subject, and the body starts with the next nonempty
308 # line not starting with Author, From or Date
309 @@PERL@@ -ne 'BEGIN { $subject = 0 }
310 if ($subject > 1) { print ; }
311 elsif (/^\s+$/) { next ; }
312 elsif (/^Author:/) { s/Author/From/ ; print ;}
313 elsif (/^(From|Date)/) { print ; }
314 elsif ($subject) {
315 $subject = 2 ;
316 print "\n" ;
317 print ;
318 } else {
319 print "Subject: ", $_ ;
320 $subject = 1;
322 ' -- "$stgit" >"$dotest/$msgnum" || clean_abort
323 done
324 echo "$this" > "$dotest/last"
325 this=
326 msgnum=
329 this=0
330 test 0 -eq "$#" && set -- -
331 for hg in "$@"
333 this=$(( $this + 1 ))
334 msgnum=$(printf "%0${prec}d" $this)
335 # hg stores changeset metadata in #-commented lines preceding
336 # the commit message and diff(s). The only metadata we care about
337 # are the User and Date (Node ID and Parent are hashes which are
338 # only relevant to the hg repository and thus not useful to us)
339 # Since we cannot guarantee that the commit message is in
340 # git-friendly format, we put no Subject: line and just consume
341 # all of the message as the body
342 LANG=C LC_ALL=C @@PERL@@ -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
343 if ($subject) { print ; }
344 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
345 elsif (/^\# Date /) {
346 my ($hashsign, $str, $time, $tz) = split ;
347 $tz_str = sprintf "%+05d", (0-$tz)/36;
348 print "Date: " .
349 strftime("%a, %d %b %Y %H:%M:%S ",
350 gmtime($time-$tz))
351 . "$tz_str\n";
352 } elsif (/^\# /) { next ; }
353 else {
354 print "\n", $_ ;
355 $subject = 1;
357 ' -- "$hg" >"$dotest/$msgnum" || clean_abort
358 done
359 echo "$this" >"$dotest/last"
360 this=
361 msgnum=
364 if test -n "$patch_format"
365 then
366 clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
367 else
368 clean_abort "$(gettext "Patch format detection failed.")"
371 esac
374 prec=4
375 dotest="$GIT_DIR/rebase-apply"
376 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
377 messageid= resolvemsg= resume= scissors= no_inbody_headers=
378 git_apply_opt=
379 committer_date_is_author_date=
380 ignore_date=
381 allow_rerere_autoupdate=
382 gpg_sign_opt=
384 if test "$(git config --bool --get am.messageid)" = true
385 then
386 messageid=t
389 if test "$(git config --bool --get am.keepcr)" = true
390 then
391 keepcr=t
394 while test $# != 0
396 case "$1" in
397 -i|--interactive)
398 interactive=t ;;
399 -b|--binary)
400 gettextln >&2 "The -b/--binary option has been a no-op for long time, and
401 it will be removed. Please do not use it anymore."
403 -3|--3way)
404 threeway=t ;;
405 -s|--signoff)
406 sign=t ;;
407 -u|--utf8)
408 utf8=t ;; # this is now default
409 --no-utf8)
410 utf8= ;;
411 -m|--message-id)
412 messageid=t ;;
413 --no-message-id)
414 messageid=f ;;
415 -k|--keep)
416 keep=t ;;
417 --keep-non-patch)
418 keep=b ;;
419 -c|--scissors)
420 scissors=t ;;
421 --no-scissors)
422 scissors=f ;;
423 -r|--resolved|--continue)
424 resolved=t ;;
425 --skip)
426 skip=t ;;
427 --abort)
428 abort=t ;;
429 --rebasing)
430 rebasing=t threeway=t ;;
431 --resolvemsg=*)
432 resolvemsg="${1#--resolvemsg=}" ;;
433 --whitespace=*|--directory=*|--exclude=*|--include=*)
434 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
435 -C*|-p*)
436 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
437 --patch-format=*)
438 patch_format="${1#--patch-format=}" ;;
439 --reject|--ignore-whitespace|--ignore-space-change)
440 git_apply_opt="$git_apply_opt $1" ;;
441 --committer-date-is-author-date)
442 committer_date_is_author_date=t ;;
443 --ignore-date)
444 ignore_date=t ;;
445 --rerere-autoupdate|--no-rerere-autoupdate)
446 allow_rerere_autoupdate="$1" ;;
447 -q|--quiet)
448 GIT_QUIET=t ;;
449 --keep-cr)
450 keepcr=t ;;
451 --no-keep-cr)
452 keepcr=f ;;
453 --gpg-sign)
454 gpg_sign_opt=-S ;;
455 --gpg-sign=*)
456 gpg_sign_opt="-S${1#--gpg-sign=}" ;;
458 shift; break ;;
460 usage ;;
461 esac
462 shift
463 done
465 # If the dotest directory exists, but we have finished applying all the
466 # patches in them, clear it out.
467 if test -d "$dotest" &&
468 test -f "$dotest/last" &&
469 test -f "$dotest/next" &&
470 last=$(cat "$dotest/last") &&
471 next=$(cat "$dotest/next") &&
472 test $# != 0 &&
473 test "$next" -gt "$last"
474 then
475 rm -fr "$dotest"
478 if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
479 then
480 case "$#,$skip$resolved$abort" in
481 0,*t*)
482 # Explicit resume command and we do not have file, so
483 # we are happy.
484 : ;;
486 # No file input but without resume parameters; catch
487 # user error to feed us a patch from standard input
488 # when there is already $dotest. This is somewhat
489 # unreliable -- stdin could be /dev/null for example
490 # and the caller did not intend to feed us a patch but
491 # wanted to continue unattended.
492 test -t 0
495 false
497 esac ||
498 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
499 resume=yes
501 case "$skip,$abort" in
502 t,t)
503 die "$(gettext "Please make up your mind. --skip or --abort?")"
506 git rerere clear
507 git read-tree --reset -u HEAD HEAD
508 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
509 git reset HEAD
510 git update-ref ORIG_HEAD $orig_head
513 if test -f "$dotest/rebasing"
514 then
515 exec git rebase --abort
517 git rerere clear
518 if safe_to_abort
519 then
520 git read-tree --reset -u HEAD ORIG_HEAD
521 git reset ORIG_HEAD
523 rm -fr "$dotest"
524 exit ;;
525 esac
526 rm -f "$dotest/dirtyindex"
527 else
528 # Possible stray $dotest directory in the independent-run
529 # case; in the --rebasing case, it is upto the caller
530 # (git-rebase--am) to take care of stray directories.
531 if test -d "$dotest" && test -z "$rebasing"
532 then
533 case "$skip,$resolved,$abort" in
534 ,,t)
535 rm -fr "$dotest"
536 exit 0
539 die "$(eval_gettext "Stray \$dotest directory found.
540 Use \"git am --abort\" to remove it.")"
542 esac
545 # Make sure we are not given --skip, --continue, or --abort
546 test "$skip$resolved$abort" = "" ||
547 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
549 # Start afresh.
550 mkdir -p "$dotest" || exit
552 if test -n "$prefix" && test $# != 0
553 then
554 first=t
555 for arg
557 test -n "$first" && {
558 set x
559 first=
561 if is_absolute_path "$arg"
562 then
563 set "$@" "$arg"
564 else
565 set "$@" "$prefix$arg"
567 done
568 shift
571 check_patch_format "$@"
573 split_patches "$@"
575 # -i can and must be given when resuming; everything
576 # else is kept
577 echo " $git_apply_opt" >"$dotest/apply-opt"
578 echo "$threeway" >"$dotest/threeway"
579 echo "$sign" >"$dotest/sign"
580 echo "$utf8" >"$dotest/utf8"
581 echo "$keep" >"$dotest/keep"
582 echo "$messageid" >"$dotest/messageid"
583 echo "$scissors" >"$dotest/scissors"
584 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
585 echo "$GIT_QUIET" >"$dotest/quiet"
586 echo 1 >"$dotest/next"
587 if test -n "$rebasing"
588 then
589 : >"$dotest/rebasing"
590 else
591 : >"$dotest/applying"
592 if test -n "$HAS_HEAD"
593 then
594 git update-ref ORIG_HEAD HEAD
595 else
596 git update-ref -d ORIG_HEAD >/dev/null 2>&1
601 git update-index -q --refresh
603 case "$resolved" in
605 case "$HAS_HEAD" in
607 files=$(git ls-files) ;;
609 files=$(git diff-index --cached --name-only HEAD --) ;;
610 esac || exit
611 if test "$files"
612 then
613 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
614 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
616 esac
618 # Now, decide what command line options we will give to the git
619 # commands we invoke, based on the result of parsing command line
620 # options and previous invocation state stored in $dotest/ files.
622 if test "$(cat "$dotest/utf8")" = t
623 then
624 utf8=-u
625 else
626 utf8=-n
628 keep=$(cat "$dotest/keep")
629 case "$keep" in
631 keep=-k ;;
633 keep=-b ;;
635 keep= ;;
636 esac
637 case "$(cat "$dotest/messageid")" in
639 messageid=-m ;;
641 messageid= ;;
642 esac
643 case "$(cat "$dotest/scissors")" in
645 scissors=--scissors ;;
647 scissors=--no-scissors ;;
648 esac
649 if test "$(cat "$dotest/no_inbody_headers")" = t
650 then
651 no_inbody_headers=--no-inbody-headers
652 else
653 no_inbody_headers=
655 if test "$(cat "$dotest/quiet")" = t
656 then
657 GIT_QUIET=t
659 if test "$(cat "$dotest/threeway")" = t
660 then
661 threeway=t
663 git_apply_opt=$(cat "$dotest/apply-opt")
664 if test "$(cat "$dotest/sign")" = t
665 then
666 SIGNOFF=$(git var GIT_COMMITTER_IDENT | sed -e '
667 s/>.*/>/
668 s/^/Signed-off-by: /'
670 else
671 SIGNOFF=
674 last=$(cat "$dotest/last")
675 this=$(cat "$dotest/next")
676 if test "$skip" = t
677 then
678 this=$(expr "$this" + 1)
679 resume=
682 while test "$this" -le "$last"
684 msgnum=$(printf "%0${prec}d" $this)
685 next=$(expr "$this" + 1)
686 test -f "$dotest/$msgnum" || {
687 resume=
688 go_next
689 continue
692 # If we are not resuming, parse and extract the patch information
693 # into separate files:
694 # - info records the authorship and title
695 # - msg is the rest of commit log message
696 # - patch is the patch body.
698 # When we are resuming, these files are either already prepared
699 # by the user, or the user can tell us to do so by --continue flag.
700 case "$resume" in
702 if test -f "$dotest/rebasing"
703 then
704 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
705 -e q "$dotest/$msgnum") &&
706 test "$(git cat-file -t "$commit")" = commit ||
707 stop_here $this
708 git cat-file commit "$commit" |
709 sed -e '1,/^$/d' >"$dotest/msg-clean"
710 echo "$commit" >"$dotest/original-commit"
711 get_author_ident_from_commit "$commit" >"$dotest/author-script"
712 git diff-tree --root --binary --full-index "$commit" >"$dotest/patch"
713 else
714 git mailinfo $keep $no_inbody_headers $messageid $scissors $utf8 "$dotest/msg" "$dotest/patch" \
715 <"$dotest/$msgnum" >"$dotest/info" ||
716 stop_here $this
718 # skip pine's internal folder data
719 sane_grep '^Author: Mail System Internal Data$' \
720 <"$dotest"/info >/dev/null &&
721 go_next && continue
723 test -s "$dotest/patch" || {
724 eval_gettextln "Patch is empty. Was it split wrong?
725 If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
726 To restore the original branch and stop patching run \"\$cmdline --abort\"."
727 stop_here $this
729 rm -f "$dotest/original-commit" "$dotest/author-script"
731 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
732 echo
733 cat "$dotest/msg"
735 git stripspace > "$dotest/msg-clean"
738 esac
740 if test -f "$dotest/author-script"
741 then
742 eval $(cat "$dotest/author-script")
743 else
744 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
745 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
746 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
749 if test -z "$GIT_AUTHOR_EMAIL"
750 then
751 gettextln "Patch does not have a valid e-mail address."
752 stop_here $this
755 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
757 case "$resume" in
759 if test '' != "$SIGNOFF"
760 then
761 LAST_SIGNED_OFF_BY=$(
762 sed -ne '/^Signed-off-by: /p' \
763 "$dotest/msg-clean" |
764 sed -ne '$p'
766 ADD_SIGNOFF=$(
767 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
768 test '' = "$LAST_SIGNED_OFF_BY" && echo
769 echo "$SIGNOFF"
771 else
772 ADD_SIGNOFF=
775 if test -s "$dotest/msg-clean"
776 then
777 cat "$dotest/msg-clean"
779 if test '' != "$ADD_SIGNOFF"
780 then
781 echo "$ADD_SIGNOFF"
783 } >"$dotest/final-commit"
786 case "$resolved$interactive" in
788 # This is used only for interactive view option.
789 git diff-index -p --cached HEAD -- >"$dotest/patch"
791 esac
792 esac
794 resume=
795 if test "$interactive" = t
796 then
797 test -t 0 ||
798 die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
799 action=again
800 while test "$action" = again
802 gettextln "Commit Body is:"
803 echo "--------------------------"
804 cat "$dotest/final-commit"
805 echo "--------------------------"
806 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
807 # in your translation. The program will only accept English
808 # input at this point.
809 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
810 read reply
811 case "$reply" in
812 [yY]*) action=yes ;;
813 [aA]*) action=yes interactive= ;;
814 [nN]*) action=skip ;;
815 [eE]*) git_editor "$dotest/final-commit"
816 action=again ;;
817 [vV]*) action=again
818 git_pager "$dotest/patch" ;;
819 *) action=again ;;
820 esac
821 done
822 else
823 action=yes
826 if test $action = skip
827 then
828 go_next
829 continue
832 if test -x "$GIT_DIR"/hooks/applypatch-msg
833 then
834 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
835 stop_here $this
838 if test -f "$dotest/final-commit"
839 then
840 FIRSTLINE=$(sed 1q "$dotest/final-commit")
841 else
842 FIRSTLINE=""
845 say "$(eval_gettext "Applying: \$FIRSTLINE")"
847 case "$resolved" in
849 # When we are allowed to fall back to 3-way later, don't give
850 # false errors during the initial attempt.
851 squelch=
852 if test "$threeway" = t
853 then
854 squelch='>/dev/null 2>&1 '
856 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
857 apply_status=$?
860 # Resolved means the user did all the hard work, and
861 # we do not have to do any patch application. Just
862 # trust what the user has in the index file and the
863 # working tree.
864 resolved=
865 git diff-index --quiet --cached HEAD -- && {
866 gettextln "No changes - did you forget to use 'git add'?
867 If there is nothing left to stage, chances are that something else
868 already introduced the same changes; you might want to skip this patch."
869 stop_here_user_resolve $this
871 unmerged=$(git ls-files -u)
872 if test -n "$unmerged"
873 then
874 gettextln "You still have unmerged paths in your index
875 did you forget to use 'git add'?"
876 stop_here_user_resolve $this
878 apply_status=0
879 git rerere
881 esac
883 if test $apply_status != 0 && test "$threeway" = t
884 then
885 if (fall_back_3way)
886 then
887 # Applying the patch to an earlier tree and merging the
888 # result may have produced the same tree as ours.
889 git diff-index --quiet --cached HEAD -- && {
890 say "$(gettext "No changes -- Patch already applied.")"
891 go_next
892 continue
894 # clear apply_status -- we have successfully merged.
895 apply_status=0
898 if test $apply_status != 0
899 then
900 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
901 if test "$(git config --bool advice.amworkdir)" != false
902 then
903 eval_gettextln 'The copy of the patch that failed is found in:
904 $dotest/patch'
906 stop_here_user_resolve $this
909 if test -x "$GIT_DIR"/hooks/pre-applypatch
910 then
911 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
914 tree=$(git write-tree) &&
915 commit=$(
916 if test -n "$ignore_date"
917 then
918 GIT_AUTHOR_DATE=
920 parent=$(git rev-parse --verify -q HEAD) ||
921 say >&2 "$(gettext "applying to an empty history")"
923 if test -n "$committer_date_is_author_date"
924 then
925 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
926 export GIT_COMMITTER_DATE
927 fi &&
928 git commit-tree ${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree \
929 <"$dotest/final-commit"
930 ) &&
931 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
932 stop_here $this
934 if test -f "$dotest/original-commit"; then
935 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
938 if test -x "$GIT_DIR"/hooks/post-applypatch
939 then
940 "$GIT_DIR"/hooks/post-applypatch
943 go_next
944 done
946 if test -s "$dotest"/rewritten; then
947 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
948 if test -x "$GIT_DIR"/hooks/post-rewrite; then
949 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
953 # If am was called with --rebasing (from git-rebase--am), it's up to
954 # the caller to take care of housekeeping.
955 if ! test -f "$dotest/rebasing"
956 then
957 rm -fr "$dotest"
958 git gc --auto