builtin-am: implement --rebasing mode
[git/mingw.git] / git-am.sh
blob3b770281232904132ac10620ded4a36d36a00be5
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 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
74 sq () {
75 git rev-parse --sq-quote "$@"
78 stop_here () {
79 echo "$1" >"$dotest/next"
80 git rev-parse --verify -q HEAD >"$dotest/abort-safety"
81 exit 1
84 safe_to_abort () {
85 if test -f "$dotest/dirtyindex"
86 then
87 return 1
90 if ! test -f "$dotest/abort-safety"
91 then
92 return 0
95 abort_safety=$(cat "$dotest/abort-safety")
96 if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
97 then
98 return 0
100 gettextln "You seem to have moved HEAD since the last 'am' failure.
101 Not rewinding to ORIG_HEAD" >&2
102 return 1
105 stop_here_user_resolve () {
106 if [ -n "$resolvemsg" ]; then
107 printf '%s\n' "$resolvemsg"
108 stop_here $1
110 eval_gettextln "When you have resolved this problem, run \"\$cmdline --continue\".
111 If you prefer to skip this patch, run \"\$cmdline --skip\" instead.
112 To restore the original branch and stop patching, run \"\$cmdline --abort\"."
114 stop_here $1
117 go_next () {
118 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
119 "$dotest/patch" "$dotest/info"
120 echo "$next" >"$dotest/next"
121 this=$next
124 cannot_fallback () {
125 echo "$1"
126 gettextln "Cannot fall back to three-way merge."
127 exit 1
130 fall_back_3way () {
131 O_OBJECT=$(cd "$GIT_OBJECT_DIRECTORY" && pwd)
133 rm -fr "$dotest"/patch-merge-*
134 mkdir "$dotest/patch-merge-tmp-dir"
136 # First see if the patch records the index info that we can use.
137 cmd="git apply $git_apply_opt --build-fake-ancestor" &&
138 cmd="$cmd "'"$dotest/patch-merge-tmp-index" "$dotest/patch"' &&
139 eval "$cmd" &&
140 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
141 git write-tree >"$dotest/patch-merge-base+" ||
142 cannot_fallback "$(gettext "Repository lacks necessary blobs to fall back on 3-way merge.")"
144 say "$(gettext "Using index info to reconstruct a base tree...")"
146 cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
148 if test -z "$GIT_QUIET"
149 then
150 eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
153 cmd="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
154 if eval "$cmd"
155 then
156 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
157 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
158 else
159 cannot_fallback "$(gettext "Did you hand edit your patch?
160 It does not apply to blobs recorded in its index.")"
163 test -f "$dotest/patch-merge-index" &&
164 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
165 orig_tree=$(cat "$dotest/patch-merge-base") &&
166 rm -fr "$dotest"/patch-merge-* || exit 1
168 say "$(gettext "Falling back to patching base and 3-way merge...")"
170 # This is not so wrong. Depending on which base we picked,
171 # orig_tree may be wildly different from ours, but his_tree
172 # has the same set of wildly different changes in parts the
173 # patch did not touch, so recursive ends up canceling them,
174 # saying that we reverted all those changes.
176 eval GITHEAD_$his_tree='"$FIRSTLINE"'
177 export GITHEAD_$his_tree
178 if test -n "$GIT_QUIET"
179 then
180 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
182 our_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree)
183 git-merge-recursive $orig_tree -- $our_tree $his_tree || {
184 git rerere $allow_rerere_autoupdate
185 die "$(gettext "Failed to merge in the changes.")"
187 unset GITHEAD_$his_tree
190 clean_abort () {
191 test $# = 0 || echo >&2 "$@"
192 rm -fr "$dotest"
193 exit 1
196 patch_format=
198 check_patch_format () {
199 # early return if patch_format was set from the command line
200 if test -n "$patch_format"
201 then
202 return 0
205 # we default to mbox format if input is from stdin and for
206 # directories
207 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
208 then
209 patch_format=mbox
210 return 0
213 # otherwise, check the first few non-blank lines of the first
214 # patch to try to detect its format
216 # Start from first line containing non-whitespace
218 while test -z "$l1"
220 read l1 || break
221 done
222 read l2
223 read l3
224 case "$l1" in
225 "From "* | "From: "*)
226 patch_format=mbox
228 '# This series applies on GIT commit'*)
229 patch_format=stgit-series
231 "# HG changeset patch")
232 patch_format=hg
235 # if the second line is empty and the third is
236 # a From, Author or Date entry, this is very
237 # likely an StGIT patch
238 case "$l2,$l3" in
239 ,"From: "* | ,"Author: "* | ,"Date: "*)
240 patch_format=stgit
244 esac
246 esac
247 if test -z "$patch_format" &&
248 test -n "$l1" &&
249 test -n "$l2" &&
250 test -n "$l3"
251 then
252 # This begins with three non-empty lines. Is this a
253 # piece of e-mail a-la RFC2822? Grab all the headers,
254 # discarding the indented remainder of folded lines,
255 # and see if it looks like that they all begin with the
256 # header field names...
257 tr -d '\015' <"$1" |
258 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
259 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
260 patch_format=mbox
262 } < "$1" || clean_abort
265 split_patches () {
266 case "$patch_format" in
267 mbox)
268 if test t = "$keepcr"
269 then
270 keep_cr=--keep-cr
271 else
272 keep_cr=
274 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
275 clean_abort
277 stgit-series)
278 if test $# -ne 1
279 then
280 clean_abort "$(gettext "Only one StGIT patch series can be applied at once")"
282 series_dir=$(dirname "$1")
283 series_file="$1"
284 shift
286 set x
287 while read filename
289 set "$@" "$series_dir/$filename"
290 done
291 # remove the safety x
292 shift
293 # remove the arg coming from the first-line comment
294 shift
295 } < "$series_file" || clean_abort
296 # set the patch format appropriately
297 patch_format=stgit
298 # now handle the actual StGIT patches
299 split_patches "$@"
301 stgit)
302 this=0
303 test 0 -eq "$#" && set -- -
304 for stgit in "$@"
306 this=$(expr "$this" + 1)
307 msgnum=$(printf "%0${prec}d" $this)
308 # Perl version of StGIT parse_patch. The first nonemptyline
309 # not starting with Author, From or Date is the
310 # subject, and the body starts with the next nonempty
311 # line not starting with Author, From or Date
312 @@PERL@@ -ne 'BEGIN { $subject = 0 }
313 if ($subject > 1) { print ; }
314 elsif (/^\s+$/) { next ; }
315 elsif (/^Author:/) { s/Author/From/ ; print ;}
316 elsif (/^(From|Date)/) { print ; }
317 elsif ($subject) {
318 $subject = 2 ;
319 print "\n" ;
320 print ;
321 } else {
322 print "Subject: ", $_ ;
323 $subject = 1;
325 ' -- "$stgit" >"$dotest/$msgnum" || clean_abort
326 done
327 echo "$this" > "$dotest/last"
328 this=
329 msgnum=
332 this=0
333 test 0 -eq "$#" && set -- -
334 for hg in "$@"
336 this=$(( $this + 1 ))
337 msgnum=$(printf "%0${prec}d" $this)
338 # hg stores changeset metadata in #-commented lines preceding
339 # the commit message and diff(s). The only metadata we care about
340 # are the User and Date (Node ID and Parent are hashes which are
341 # only relevant to the hg repository and thus not useful to us)
342 # Since we cannot guarantee that the commit message is in
343 # git-friendly format, we put no Subject: line and just consume
344 # all of the message as the body
345 LANG=C LC_ALL=C @@PERL@@ -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
346 if ($subject) { print ; }
347 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
348 elsif (/^\# Date /) {
349 my ($hashsign, $str, $time, $tz) = split ;
350 $tz_str = sprintf "%+05d", (0-$tz)/36;
351 print "Date: " .
352 strftime("%a, %d %b %Y %H:%M:%S ",
353 gmtime($time-$tz))
354 . "$tz_str\n";
355 } elsif (/^\# /) { next ; }
356 else {
357 print "\n", $_ ;
358 $subject = 1;
360 ' -- "$hg" >"$dotest/$msgnum" || clean_abort
361 done
362 echo "$this" >"$dotest/last"
363 this=
364 msgnum=
367 if test -n "$patch_format"
368 then
369 clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
370 else
371 clean_abort "$(gettext "Patch format detection failed.")"
374 esac
377 prec=4
378 dotest="$GIT_DIR/rebase-apply"
379 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
380 messageid= resolvemsg= resume= scissors= no_inbody_headers=
381 git_apply_opt=
382 committer_date_is_author_date=
383 ignore_date=
384 allow_rerere_autoupdate=
385 gpg_sign_opt=
386 threeway=
388 if test "$(git config --bool --get am.messageid)" = true
389 then
390 messageid=t
393 if test "$(git config --bool --get am.keepcr)" = true
394 then
395 keepcr=t
398 while test $# != 0
400 case "$1" in
401 -i|--interactive)
402 interactive=t ;;
403 -b|--binary)
404 gettextln >&2 "The -b/--binary option has been a no-op for long time, and
405 it will be removed. Please do not use it anymore."
407 -3|--3way)
408 threeway=t ;;
409 -s|--signoff)
410 sign=t ;;
411 -u|--utf8)
412 utf8=t ;; # this is now default
413 --no-utf8)
414 utf8= ;;
415 -m|--message-id)
416 messageid=t ;;
417 --no-message-id)
418 messageid=f ;;
419 -k|--keep)
420 keep=t ;;
421 --keep-non-patch)
422 keep=b ;;
423 -c|--scissors)
424 scissors=t ;;
425 --no-scissors)
426 scissors=f ;;
427 -r|--resolved|--continue)
428 resolved=t ;;
429 --skip)
430 skip=t ;;
431 --abort)
432 abort=t ;;
433 --rebasing)
434 rebasing=t threeway=t ;;
435 --resolvemsg=*)
436 resolvemsg="${1#--resolvemsg=}" ;;
437 --whitespace=*|--directory=*|--exclude=*|--include=*)
438 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
439 -C*|-p*)
440 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
441 --patch-format=*)
442 patch_format="${1#--patch-format=}" ;;
443 --reject|--ignore-whitespace|--ignore-space-change)
444 git_apply_opt="$git_apply_opt $1" ;;
445 --committer-date-is-author-date)
446 committer_date_is_author_date=t ;;
447 --ignore-date)
448 ignore_date=t ;;
449 --rerere-autoupdate|--no-rerere-autoupdate)
450 allow_rerere_autoupdate="$1" ;;
451 -q|--quiet)
452 GIT_QUIET=t ;;
453 --keep-cr)
454 keepcr=t ;;
455 --no-keep-cr)
456 keepcr=f ;;
457 --gpg-sign)
458 gpg_sign_opt=-S ;;
459 --gpg-sign=*)
460 gpg_sign_opt="-S${1#--gpg-sign=}" ;;
462 shift; break ;;
464 usage ;;
465 esac
466 shift
467 done
469 # If the dotest directory exists, but we have finished applying all the
470 # patches in them, clear it out.
471 if test -d "$dotest" &&
472 test -f "$dotest/last" &&
473 test -f "$dotest/next" &&
474 last=$(cat "$dotest/last") &&
475 next=$(cat "$dotest/next") &&
476 test $# != 0 &&
477 test "$next" -gt "$last"
478 then
479 rm -fr "$dotest"
482 if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
483 then
484 case "$#,$skip$resolved$abort" in
485 0,*t*)
486 # Explicit resume command and we do not have file, so
487 # we are happy.
488 : ;;
490 # No file input but without resume parameters; catch
491 # user error to feed us a patch from standard input
492 # when there is already $dotest. This is somewhat
493 # unreliable -- stdin could be /dev/null for example
494 # and the caller did not intend to feed us a patch but
495 # wanted to continue unattended.
496 test -t 0
499 false
501 esac ||
502 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
503 resume=yes
505 case "$skip,$abort" in
506 t,t)
507 die "$(gettext "Please make up your mind. --skip or --abort?")"
510 git rerere clear
511 head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
512 git read-tree --reset -u $head_tree $head_tree &&
513 index_tree=$(git write-tree) &&
514 git read-tree -m -u $index_tree $head_tree
515 git read-tree $head_tree
518 if test -f "$dotest/rebasing"
519 then
520 exec git rebase --abort
522 git rerere clear
523 if safe_to_abort
524 then
525 head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
526 git read-tree --reset -u $head_tree $head_tree &&
527 index_tree=$(git write-tree) &&
528 orig_head=$(git rev-parse --verify -q ORIG_HEAD || echo $empty_tree) &&
529 git read-tree -m -u $index_tree $orig_head
530 if git rev-parse --verify -q ORIG_HEAD >/dev/null 2>&1
531 then
532 git reset ORIG_HEAD
533 else
534 git read-tree $empty_tree
535 curr_branch=$(git symbolic-ref HEAD 2>/dev/null) &&
536 git update-ref -d $curr_branch
539 rm -fr "$dotest"
540 exit ;;
541 esac
542 rm -f "$dotest/dirtyindex"
543 else
544 # Possible stray $dotest directory in the independent-run
545 # case; in the --rebasing case, it is upto the caller
546 # (git-rebase--am) to take care of stray directories.
547 if test -d "$dotest" && test -z "$rebasing"
548 then
549 case "$skip,$resolved,$abort" in
550 ,,t)
551 rm -fr "$dotest"
552 exit 0
555 die "$(eval_gettext "Stray \$dotest directory found.
556 Use \"git am --abort\" to remove it.")"
558 esac
561 # Make sure we are not given --skip, --continue, or --abort
562 test "$skip$resolved$abort" = "" ||
563 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
565 # Start afresh.
566 mkdir -p "$dotest" || exit
568 if test -n "$prefix" && test $# != 0
569 then
570 first=t
571 for arg
573 test -n "$first" && {
574 set x
575 first=
577 if is_absolute_path "$arg"
578 then
579 set "$@" "$arg"
580 else
581 set "$@" "$prefix$arg"
583 done
584 shift
587 check_patch_format "$@"
589 split_patches "$@"
591 # -i can and must be given when resuming; everything
592 # else is kept
593 echo " $git_apply_opt" >"$dotest/apply-opt"
594 echo "$threeway" >"$dotest/threeway"
595 echo "$sign" >"$dotest/sign"
596 echo "$utf8" >"$dotest/utf8"
597 echo "$keep" >"$dotest/keep"
598 echo "$messageid" >"$dotest/messageid"
599 echo "$scissors" >"$dotest/scissors"
600 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
601 echo "$GIT_QUIET" >"$dotest/quiet"
602 echo 1 >"$dotest/next"
603 if test -n "$rebasing"
604 then
605 : >"$dotest/rebasing"
606 else
607 : >"$dotest/applying"
608 if test -n "$HAS_HEAD"
609 then
610 git update-ref ORIG_HEAD HEAD
611 else
612 git update-ref -d ORIG_HEAD >/dev/null 2>&1
617 git update-index -q --refresh
619 case "$resolved" in
621 case "$HAS_HEAD" in
623 files=$(git ls-files) ;;
625 files=$(git diff-index --cached --name-only HEAD --) ;;
626 esac || exit
627 if test "$files"
628 then
629 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
630 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
632 esac
634 # Now, decide what command line options we will give to the git
635 # commands we invoke, based on the result of parsing command line
636 # options and previous invocation state stored in $dotest/ files.
638 if test "$(cat "$dotest/utf8")" = t
639 then
640 utf8=-u
641 else
642 utf8=-n
644 keep=$(cat "$dotest/keep")
645 case "$keep" in
647 keep=-k ;;
649 keep=-b ;;
651 keep= ;;
652 esac
653 case "$(cat "$dotest/messageid")" in
655 messageid=-m ;;
657 messageid= ;;
658 esac
659 case "$(cat "$dotest/scissors")" in
661 scissors=--scissors ;;
663 scissors=--no-scissors ;;
664 esac
665 if test "$(cat "$dotest/no_inbody_headers")" = t
666 then
667 no_inbody_headers=--no-inbody-headers
668 else
669 no_inbody_headers=
671 if test "$(cat "$dotest/quiet")" = t
672 then
673 GIT_QUIET=t
675 if test "$(cat "$dotest/threeway")" = t
676 then
677 threeway=t
679 git_apply_opt=$(cat "$dotest/apply-opt")
680 if test "$(cat "$dotest/sign")" = t
681 then
682 SIGNOFF=$(git var GIT_COMMITTER_IDENT | sed -e '
683 s/>.*/>/
684 s/^/Signed-off-by: /'
686 else
687 SIGNOFF=
690 last=$(cat "$dotest/last")
691 this=$(cat "$dotest/next")
692 if test "$skip" = t
693 then
694 this=$(expr "$this" + 1)
695 resume=
698 while test "$this" -le "$last"
700 msgnum=$(printf "%0${prec}d" $this)
701 next=$(expr "$this" + 1)
702 test -f "$dotest/$msgnum" || {
703 resume=
704 go_next
705 continue
708 # If we are not resuming, parse and extract the patch information
709 # into separate files:
710 # - info records the authorship and title
711 # - msg is the rest of commit log message
712 # - patch is the patch body.
714 # When we are resuming, these files are either already prepared
715 # by the user, or the user can tell us to do so by --continue flag.
716 case "$resume" in
718 if test -f "$dotest/rebasing"
719 then
720 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
721 -e q "$dotest/$msgnum") &&
722 test "$(git cat-file -t "$commit")" = commit ||
723 stop_here $this
724 git cat-file commit "$commit" |
725 sed -e '1,/^$/d' >"$dotest/msg-clean"
726 echo "$commit" >"$dotest/original-commit"
727 get_author_ident_from_commit "$commit" >"$dotest/author-script"
728 git diff-tree --root --binary --full-index "$commit" >"$dotest/patch"
729 else
730 git mailinfo $keep $no_inbody_headers $messageid $scissors $utf8 "$dotest/msg" "$dotest/patch" \
731 <"$dotest/$msgnum" >"$dotest/info" ||
732 stop_here $this
734 # skip pine's internal folder data
735 sane_grep '^Author: Mail System Internal Data$' \
736 <"$dotest"/info >/dev/null &&
737 go_next && continue
739 test -s "$dotest/patch" || {
740 eval_gettextln "Patch is empty. Was it split wrong?
741 If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
742 To restore the original branch and stop patching run \"\$cmdline --abort\"."
743 stop_here $this
745 rm -f "$dotest/original-commit" "$dotest/author-script"
747 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
748 echo
749 cat "$dotest/msg"
751 git stripspace > "$dotest/msg-clean"
754 esac
756 if test -f "$dotest/author-script"
757 then
758 eval $(cat "$dotest/author-script")
759 else
760 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
761 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
762 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
765 if test -z "$GIT_AUTHOR_EMAIL"
766 then
767 gettextln "Patch does not have a valid e-mail address."
768 stop_here $this
771 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
773 case "$resume" in
775 if test '' != "$SIGNOFF"
776 then
777 LAST_SIGNED_OFF_BY=$(
778 sed -ne '/^Signed-off-by: /p' \
779 "$dotest/msg-clean" |
780 sed -ne '$p'
782 ADD_SIGNOFF=$(
783 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
784 test '' = "$LAST_SIGNED_OFF_BY" && echo
785 echo "$SIGNOFF"
787 else
788 ADD_SIGNOFF=
791 if test -s "$dotest/msg-clean"
792 then
793 cat "$dotest/msg-clean"
795 if test '' != "$ADD_SIGNOFF"
796 then
797 echo "$ADD_SIGNOFF"
799 } >"$dotest/final-commit"
802 case "$resolved$interactive" in
804 # This is used only for interactive view option.
805 git diff-index -p --cached HEAD -- >"$dotest/patch"
807 esac
808 esac
810 resume=
811 if test "$interactive" = t
812 then
813 test -t 0 ||
814 die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
815 action=again
816 while test "$action" = again
818 gettextln "Commit Body is:"
819 echo "--------------------------"
820 cat "$dotest/final-commit"
821 echo "--------------------------"
822 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
823 # in your translation. The program will only accept English
824 # input at this point.
825 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
826 read reply
827 case "$reply" in
828 [yY]*) action=yes ;;
829 [aA]*) action=yes interactive= ;;
830 [nN]*) action=skip ;;
831 [eE]*) git_editor "$dotest/final-commit"
832 action=again ;;
833 [vV]*) action=again
834 git_pager "$dotest/patch" ;;
835 *) action=again ;;
836 esac
837 done
838 else
839 action=yes
842 if test $action = skip
843 then
844 go_next
845 continue
848 hook="$(git rev-parse --git-path hooks/applypatch-msg)"
849 if test -x "$hook"
850 then
851 "$hook" "$dotest/final-commit" || stop_here $this
854 if test -f "$dotest/final-commit"
855 then
856 FIRSTLINE=$(sed 1q "$dotest/final-commit")
857 else
858 FIRSTLINE=""
861 say "$(eval_gettext "Applying: \$FIRSTLINE")"
863 case "$resolved" in
865 # When we are allowed to fall back to 3-way later, don't give
866 # false errors during the initial attempt.
867 squelch=
868 if test "$threeway" = t
869 then
870 squelch='>/dev/null 2>&1 '
872 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
873 apply_status=$?
876 # Resolved means the user did all the hard work, and
877 # we do not have to do any patch application. Just
878 # trust what the user has in the index file and the
879 # working tree.
880 resolved=
881 git diff-index --quiet --cached HEAD -- && {
882 gettextln "No changes - did you forget to use 'git add'?
883 If there is nothing left to stage, chances are that something else
884 already introduced the same changes; you might want to skip this patch."
885 stop_here_user_resolve $this
887 unmerged=$(git ls-files -u)
888 if test -n "$unmerged"
889 then
890 gettextln "You still have unmerged paths in your index
891 did you forget to use 'git add'?"
892 stop_here_user_resolve $this
894 apply_status=0
895 git rerere
897 esac
899 if test $apply_status != 0 && test "$threeway" = t
900 then
901 if (fall_back_3way)
902 then
903 # Applying the patch to an earlier tree and merging the
904 # result may have produced the same tree as ours.
905 git diff-index --quiet --cached HEAD -- && {
906 say "$(gettext "No changes -- Patch already applied.")"
907 go_next
908 continue
910 # clear apply_status -- we have successfully merged.
911 apply_status=0
914 if test $apply_status != 0
915 then
916 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
917 if test "$(git config --bool advice.amworkdir)" != false
918 then
919 eval_gettextln 'The copy of the patch that failed is found in:
920 $dotest/patch'
922 stop_here_user_resolve $this
925 hook="$(git rev-parse --git-path hooks/pre-applypatch)"
926 if test -x "$hook"
927 then
928 "$hook" || stop_here $this
931 tree=$(git write-tree) &&
932 commit=$(
933 if test -n "$ignore_date"
934 then
935 GIT_AUTHOR_DATE=
937 parent=$(git rev-parse --verify -q HEAD) ||
938 say >&2 "$(gettext "applying to an empty history")"
940 if test -n "$committer_date_is_author_date"
941 then
942 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
943 export GIT_COMMITTER_DATE
944 fi &&
945 git commit-tree ${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree \
946 <"$dotest/final-commit"
947 ) &&
948 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
949 stop_here $this
951 if test -f "$dotest/original-commit"; then
952 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
955 hook="$(git rev-parse --git-path hooks/post-applypatch)"
956 test -x "$hook" && "$hook"
958 go_next
959 done
961 if test -s "$dotest"/rewritten; then
962 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
963 hook="$(git rev-parse --git-path hooks/post-rewrite)"
964 if test -x "$hook"; then
965 "$hook" rebase < "$dotest"/rewritten
969 # If am was called with --rebasing (from git-rebase--am), it's up to
970 # the caller to take care of housekeeping.
971 if ! test -f "$dotest/rebasing"
972 then
973 rm -fr "$dotest"
974 git gc --auto