xdiff/xprepare: fix a memory leak
[git.git] / git-am.sh
blob3103c0fc28910560d85f4c2cff2b6a0595848b16
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 for stgit in "$@"
305 this=$(expr "$this" + 1)
306 msgnum=$(printf "%0${prec}d" $this)
307 # Perl version of StGIT parse_patch. The first nonemptyline
308 # not starting with Author, From or Date is the
309 # subject, and the body starts with the next nonempty
310 # line not starting with Author, From or Date
311 @@PERL@@ -ne 'BEGIN { $subject = 0 }
312 if ($subject > 1) { print ; }
313 elsif (/^\s+$/) { next ; }
314 elsif (/^Author:/) { s/Author/From/ ; print ;}
315 elsif (/^(From|Date)/) { print ; }
316 elsif ($subject) {
317 $subject = 2 ;
318 print "\n" ;
319 print ;
320 } else {
321 print "Subject: ", $_ ;
322 $subject = 1;
324 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
325 done
326 echo "$this" > "$dotest/last"
327 this=
328 msgnum=
331 this=0
332 for hg in "$@"
334 this=$(( $this + 1 ))
335 msgnum=$(printf "%0${prec}d" $this)
336 # hg stores changeset metadata in #-commented lines preceding
337 # the commit message and diff(s). The only metadata we care about
338 # are the User and Date (Node ID and Parent are hashes which are
339 # only relevant to the hg repository and thus not useful to us)
340 # Since we cannot guarantee that the commit message is in
341 # git-friendly format, we put no Subject: line and just consume
342 # all of the message as the body
343 LANG=C LC_ALL=C @@PERL@@ -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
344 if ($subject) { print ; }
345 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
346 elsif (/^\# Date /) {
347 my ($hashsign, $str, $time, $tz) = split ;
348 $tz = sprintf "%+05d", (0-$tz)/36;
349 print "Date: " .
350 strftime("%a, %d %b %Y %H:%M:%S ",
351 localtime($time))
352 . "$tz\n";
353 } elsif (/^\# /) { next ; }
354 else {
355 print "\n", $_ ;
356 $subject = 1;
358 ' <"$hg" >"$dotest/$msgnum" || clean_abort
359 done
360 echo "$this" >"$dotest/last"
361 this=
362 msgnum=
365 if test -n "$patch_format"
366 then
367 clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
368 else
369 clean_abort "$(gettext "Patch format detection failed.")"
372 esac
375 prec=4
376 dotest="$GIT_DIR/rebase-apply"
377 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
378 messageid= resolvemsg= resume= scissors= no_inbody_headers=
379 git_apply_opt=
380 committer_date_is_author_date=
381 ignore_date=
382 allow_rerere_autoupdate=
383 gpg_sign_opt=
385 if test "$(git config --bool --get am.messageid)" = true
386 then
387 messageid=t
390 if test "$(git config --bool --get am.keepcr)" = true
391 then
392 keepcr=t
395 while test $# != 0
397 case "$1" in
398 -i|--interactive)
399 interactive=t ;;
400 -b|--binary)
401 gettextln >&2 "The -b/--binary option has been a no-op for long time, and
402 it will be removed. Please do not use it anymore."
404 -3|--3way)
405 threeway=t ;;
406 -s|--signoff)
407 sign=t ;;
408 -u|--utf8)
409 utf8=t ;; # this is now default
410 --no-utf8)
411 utf8= ;;
412 -m|--message-id)
413 messageid=t ;;
414 --no-message-id)
415 messageid=f ;;
416 -k|--keep)
417 keep=t ;;
418 --keep-non-patch)
419 keep=b ;;
420 -c|--scissors)
421 scissors=t ;;
422 --no-scissors)
423 scissors=f ;;
424 -r|--resolved|--continue)
425 resolved=t ;;
426 --skip)
427 skip=t ;;
428 --abort)
429 abort=t ;;
430 --rebasing)
431 rebasing=t threeway=t ;;
432 --resolvemsg=*)
433 resolvemsg="${1#--resolvemsg=}" ;;
434 --whitespace=*|--directory=*|--exclude=*|--include=*)
435 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
436 -C*|-p*)
437 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
438 --patch-format=*)
439 patch_format="${1#--patch-format=}" ;;
440 --reject|--ignore-whitespace|--ignore-space-change)
441 git_apply_opt="$git_apply_opt $1" ;;
442 --committer-date-is-author-date)
443 committer_date_is_author_date=t ;;
444 --ignore-date)
445 ignore_date=t ;;
446 --rerere-autoupdate|--no-rerere-autoupdate)
447 allow_rerere_autoupdate="$1" ;;
448 -q|--quiet)
449 GIT_QUIET=t ;;
450 --keep-cr)
451 keepcr=t ;;
452 --no-keep-cr)
453 keepcr=f ;;
454 --gpg-sign)
455 gpg_sign_opt=-S ;;
456 --gpg-sign=*)
457 gpg_sign_opt="-S${1#--gpg-sign=}" ;;
459 shift; break ;;
461 usage ;;
462 esac
463 shift
464 done
466 # If the dotest directory exists, but we have finished applying all the
467 # patches in them, clear it out.
468 if test -d "$dotest" &&
469 test -f "$dotest/last" &&
470 test -f "$dotest/next" &&
471 last=$(cat "$dotest/last") &&
472 next=$(cat "$dotest/next") &&
473 test $# != 0 &&
474 test "$next" -gt "$last"
475 then
476 rm -fr "$dotest"
479 if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
480 then
481 case "$#,$skip$resolved$abort" in
482 0,*t*)
483 # Explicit resume command and we do not have file, so
484 # we are happy.
485 : ;;
487 # No file input but without resume parameters; catch
488 # user error to feed us a patch from standard input
489 # when there is already $dotest. This is somewhat
490 # unreliable -- stdin could be /dev/null for example
491 # and the caller did not intend to feed us a patch but
492 # wanted to continue unattended.
493 test -t 0
496 false
498 esac ||
499 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
500 resume=yes
502 case "$skip,$abort" in
503 t,t)
504 die "$(gettext "Please make up your mind. --skip or --abort?")"
507 git rerere clear
508 head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
509 git read-tree --reset -u $head_tree $head_tree &&
510 index_tree=$(git write-tree) &&
511 git read-tree -m -u $index_tree $head_tree
512 git read-tree $head_tree
515 if test -f "$dotest/rebasing"
516 then
517 exec git rebase --abort
519 git rerere clear
520 if safe_to_abort
521 then
522 head_tree=$(git rev-parse --verify -q HEAD || echo $empty_tree) &&
523 git read-tree --reset -u $head_tree $head_tree &&
524 index_tree=$(git write-tree) &&
525 orig_head=$(git rev-parse --verify -q ORIG_HEAD || echo $empty_tree) &&
526 git read-tree -m -u $index_tree $orig_head
527 if git rev-parse --verify -q ORIG_HEAD >/dev/null 2>&1
528 then
529 git reset ORIG_HEAD
530 else
531 git read-tree $empty_tree
532 curr_branch=$(git symbolic-ref HEAD 2>/dev/null) &&
533 git update-ref -d $curr_branch
536 rm -fr "$dotest"
537 exit ;;
538 esac
539 rm -f "$dotest/dirtyindex"
540 else
541 # Possible stray $dotest directory in the independent-run
542 # case; in the --rebasing case, it is upto the caller
543 # (git-rebase--am) to take care of stray directories.
544 if test -d "$dotest" && test -z "$rebasing"
545 then
546 case "$skip,$resolved,$abort" in
547 ,,t)
548 rm -fr "$dotest"
549 exit 0
552 die "$(eval_gettext "Stray \$dotest directory found.
553 Use \"git am --abort\" to remove it.")"
555 esac
558 # Make sure we are not given --skip, --continue, or --abort
559 test "$skip$resolved$abort" = "" ||
560 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
562 # Start afresh.
563 mkdir -p "$dotest" || exit
565 if test -n "$prefix" && test $# != 0
566 then
567 first=t
568 for arg
570 test -n "$first" && {
571 set x
572 first=
574 if is_absolute_path "$arg"
575 then
576 set "$@" "$arg"
577 else
578 set "$@" "$prefix$arg"
580 done
581 shift
584 check_patch_format "$@"
586 split_patches "$@"
588 # -i can and must be given when resuming; everything
589 # else is kept
590 echo " $git_apply_opt" >"$dotest/apply-opt"
591 echo "$threeway" >"$dotest/threeway"
592 echo "$sign" >"$dotest/sign"
593 echo "$utf8" >"$dotest/utf8"
594 echo "$keep" >"$dotest/keep"
595 echo "$messageid" >"$dotest/messageid"
596 echo "$scissors" >"$dotest/scissors"
597 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
598 echo "$GIT_QUIET" >"$dotest/quiet"
599 echo 1 >"$dotest/next"
600 if test -n "$rebasing"
601 then
602 : >"$dotest/rebasing"
603 else
604 : >"$dotest/applying"
605 if test -n "$HAS_HEAD"
606 then
607 git update-ref ORIG_HEAD HEAD
608 else
609 git update-ref -d ORIG_HEAD >/dev/null 2>&1
614 git update-index -q --refresh
616 case "$resolved" in
618 case "$HAS_HEAD" in
620 files=$(git ls-files) ;;
622 files=$(git diff-index --cached --name-only HEAD --) ;;
623 esac || exit
624 if test "$files"
625 then
626 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
627 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
629 esac
631 # Now, decide what command line options we will give to the git
632 # commands we invoke, based on the result of parsing command line
633 # options and previous invocation state stored in $dotest/ files.
635 if test "$(cat "$dotest/utf8")" = t
636 then
637 utf8=-u
638 else
639 utf8=-n
641 keep=$(cat "$dotest/keep")
642 case "$keep" in
644 keep=-k ;;
646 keep=-b ;;
648 keep= ;;
649 esac
650 case "$(cat "$dotest/messageid")" in
652 messageid=-m ;;
654 messageid= ;;
655 esac
656 case "$(cat "$dotest/scissors")" in
658 scissors=--scissors ;;
660 scissors=--no-scissors ;;
661 esac
662 if test "$(cat "$dotest/no_inbody_headers")" = t
663 then
664 no_inbody_headers=--no-inbody-headers
665 else
666 no_inbody_headers=
668 if test "$(cat "$dotest/quiet")" = t
669 then
670 GIT_QUIET=t
672 if test "$(cat "$dotest/threeway")" = t
673 then
674 threeway=t
676 git_apply_opt=$(cat "$dotest/apply-opt")
677 if test "$(cat "$dotest/sign")" = t
678 then
679 SIGNOFF=$(git var GIT_COMMITTER_IDENT | sed -e '
680 s/>.*/>/
681 s/^/Signed-off-by: /'
683 else
684 SIGNOFF=
687 last=$(cat "$dotest/last")
688 this=$(cat "$dotest/next")
689 if test "$skip" = t
690 then
691 this=$(expr "$this" + 1)
692 resume=
695 while test "$this" -le "$last"
697 msgnum=$(printf "%0${prec}d" $this)
698 next=$(expr "$this" + 1)
699 test -f "$dotest/$msgnum" || {
700 resume=
701 go_next
702 continue
705 # If we are not resuming, parse and extract the patch information
706 # into separate files:
707 # - info records the authorship and title
708 # - msg is the rest of commit log message
709 # - patch is the patch body.
711 # When we are resuming, these files are either already prepared
712 # by the user, or the user can tell us to do so by --continue flag.
713 case "$resume" in
715 if test -f "$dotest/rebasing"
716 then
717 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
718 -e q "$dotest/$msgnum") &&
719 test "$(git cat-file -t "$commit")" = commit ||
720 stop_here $this
721 git cat-file commit "$commit" |
722 sed -e '1,/^$/d' >"$dotest/msg-clean"
723 echo "$commit" >"$dotest/original-commit"
724 get_author_ident_from_commit "$commit" >"$dotest/author-script"
725 git diff-tree --root --binary --full-index "$commit" >"$dotest/patch"
726 else
727 git mailinfo $keep $no_inbody_headers $messageid $scissors $utf8 "$dotest/msg" "$dotest/patch" \
728 <"$dotest/$msgnum" >"$dotest/info" ||
729 stop_here $this
731 # skip pine's internal folder data
732 sane_grep '^Author: Mail System Internal Data$' \
733 <"$dotest"/info >/dev/null &&
734 go_next && continue
736 test -s "$dotest/patch" || {
737 eval_gettextln "Patch is empty. Was it split wrong?
738 If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
739 To restore the original branch and stop patching run \"\$cmdline --abort\"."
740 stop_here $this
742 rm -f "$dotest/original-commit" "$dotest/author-script"
744 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
745 echo
746 cat "$dotest/msg"
748 git stripspace > "$dotest/msg-clean"
751 esac
753 if test -f "$dotest/author-script"
754 then
755 eval $(cat "$dotest/author-script")
756 else
757 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
758 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
759 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
762 if test -z "$GIT_AUTHOR_EMAIL"
763 then
764 gettextln "Patch does not have a valid e-mail address."
765 stop_here $this
768 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
770 case "$resume" in
772 if test '' != "$SIGNOFF"
773 then
774 LAST_SIGNED_OFF_BY=$(
775 sed -ne '/^Signed-off-by: /p' \
776 "$dotest/msg-clean" |
777 sed -ne '$p'
779 ADD_SIGNOFF=$(
780 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
781 test '' = "$LAST_SIGNED_OFF_BY" && echo
782 echo "$SIGNOFF"
784 else
785 ADD_SIGNOFF=
788 if test -s "$dotest/msg-clean"
789 then
790 cat "$dotest/msg-clean"
792 if test '' != "$ADD_SIGNOFF"
793 then
794 echo "$ADD_SIGNOFF"
796 } >"$dotest/final-commit"
799 case "$resolved$interactive" in
801 # This is used only for interactive view option.
802 git diff-index -p --cached HEAD -- >"$dotest/patch"
804 esac
805 esac
807 resume=
808 if test "$interactive" = t
809 then
810 test -t 0 ||
811 die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
812 action=again
813 while test "$action" = again
815 gettextln "Commit Body is:"
816 echo "--------------------------"
817 cat "$dotest/final-commit"
818 echo "--------------------------"
819 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
820 # in your translation. The program will only accept English
821 # input at this point.
822 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
823 read reply
824 case "$reply" in
825 [yY]*) action=yes ;;
826 [aA]*) action=yes interactive= ;;
827 [nN]*) action=skip ;;
828 [eE]*) git_editor "$dotest/final-commit"
829 action=again ;;
830 [vV]*) action=again
831 git_pager "$dotest/patch" ;;
832 *) action=again ;;
833 esac
834 done
835 else
836 action=yes
839 if test $action = skip
840 then
841 go_next
842 continue
845 if test -x "$GIT_DIR"/hooks/applypatch-msg
846 then
847 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
848 stop_here $this
851 if test -f "$dotest/final-commit"
852 then
853 FIRSTLINE=$(sed 1q "$dotest/final-commit")
854 else
855 FIRSTLINE=""
858 say "$(eval_gettext "Applying: \$FIRSTLINE")"
860 case "$resolved" in
862 # When we are allowed to fall back to 3-way later, don't give
863 # false errors during the initial attempt.
864 squelch=
865 if test "$threeway" = t
866 then
867 squelch='>/dev/null 2>&1 '
869 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
870 apply_status=$?
873 # Resolved means the user did all the hard work, and
874 # we do not have to do any patch application. Just
875 # trust what the user has in the index file and the
876 # working tree.
877 resolved=
878 git diff-index --quiet --cached HEAD -- && {
879 gettextln "No changes - did you forget to use 'git add'?
880 If there is nothing left to stage, chances are that something else
881 already introduced the same changes; you might want to skip this patch."
882 stop_here_user_resolve $this
884 unmerged=$(git ls-files -u)
885 if test -n "$unmerged"
886 then
887 gettextln "You still have unmerged paths in your index
888 did you forget to use 'git add'?"
889 stop_here_user_resolve $this
891 apply_status=0
892 git rerere
894 esac
896 if test $apply_status != 0 && test "$threeway" = t
897 then
898 if (fall_back_3way)
899 then
900 # Applying the patch to an earlier tree and merging the
901 # result may have produced the same tree as ours.
902 git diff-index --quiet --cached HEAD -- && {
903 say "$(gettext "No changes -- Patch already applied.")"
904 go_next
905 continue
907 # clear apply_status -- we have successfully merged.
908 apply_status=0
911 if test $apply_status != 0
912 then
913 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
914 if test "$(git config --bool advice.amworkdir)" != false
915 then
916 eval_gettextln 'The copy of the patch that failed is found in:
917 $dotest/patch'
919 stop_here_user_resolve $this
922 if test -x "$GIT_DIR"/hooks/pre-applypatch
923 then
924 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
927 tree=$(git write-tree) &&
928 commit=$(
929 if test -n "$ignore_date"
930 then
931 GIT_AUTHOR_DATE=
933 parent=$(git rev-parse --verify -q HEAD) ||
934 say >&2 "$(gettext "applying to an empty history")"
936 if test -n "$committer_date_is_author_date"
937 then
938 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
939 export GIT_COMMITTER_DATE
940 fi &&
941 git commit-tree ${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree \
942 <"$dotest/final-commit"
943 ) &&
944 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
945 stop_here $this
947 if test -f "$dotest/original-commit"; then
948 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
951 if test -x "$GIT_DIR"/hooks/post-applypatch
952 then
953 "$GIT_DIR"/hooks/post-applypatch
956 go_next
957 done
959 if test -s "$dotest"/rewritten; then
960 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
961 if test -x "$GIT_DIR"/hooks/post-rewrite; then
962 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
966 # If am was called with --rebasing (from git-rebase--am), it's up to
967 # the caller to take care of housekeeping.
968 if ! test -f "$dotest/rebasing"
969 then
970 rm -fr "$dotest"
971 git gc --auto