Merge branch 'es/utf8-stupid-compiler-workaround'
[git] / git-am.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005, 2006 Junio C Hamano
4
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)
11 --
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)"
44
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
51
52 git var GIT_COMMITTER_IDENT >/dev/null ||
53         die "$(gettext "You need to set your committer info first")"
54
55 if git rev-parse --verify -q HEAD >/dev/null
56 then
57         HAS_HEAD=yes
58 else
59         HAS_HEAD=
60 fi
61
62 cmdline="git am"
63 if test '' != "$interactive"
64 then
65         cmdline="$cmdline -i"
66 fi
67 if test '' != "$threeway"
68 then
69         cmdline="$cmdline -3"
70 fi
71
72 sq () {
73         git rev-parse --sq-quote "$@"
74 }
75
76 stop_here () {
77     echo "$1" >"$dotest/next"
78     git rev-parse --verify -q HEAD >"$dotest/abort-safety"
79     exit 1
80 }
81
82 safe_to_abort () {
83         if test -f "$dotest/dirtyindex"
84         then
85                 return 1
86         fi
87
88         if ! test -s "$dotest/abort-safety"
89         then
90                 return 0
91         fi
92
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
97         fi
98         gettextln "You seem to have moved HEAD since the last 'am' failure.
99 Not rewinding to ORIG_HEAD" >&2
100         return 1
101 }
102
103 stop_here_user_resolve () {
104     if [ -n "$resolvemsg" ]; then
105             printf '%s\n' "$resolvemsg"
106             stop_here $1
107     fi
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\"."
111
112     stop_here $1
113 }
114
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
120 }
121
122 cannot_fallback () {
123         echo "$1"
124         gettextln "Cannot fall back to three-way merge."
125         exit 1
126 }
127
128 fall_back_3way () {
129     O_OBJECT=$(cd "$GIT_OBJECT_DIRECTORY" && pwd)
130
131     rm -fr "$dotest"/patch-merge-*
132     mkdir "$dotest/patch-merge-tmp-dir"
133
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.")"
141
142     say "$(gettext "Using index info to reconstruct a base tree...")"
143
144     cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
145
146     if test -z "$GIT_QUIET"
147     then
148         eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
149     fi
150
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.")"
159     fi
160
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
165
166     say "$(gettext "Falling back to patching base and 3-way merge...")"
167
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.
173
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
179     fi
180     git-merge-recursive $orig_tree -- HEAD $his_tree || {
181             git rerere $allow_rerere_autoupdate
182             die "$(gettext "Failed to merge in the changes.")"
183     }
184     unset GITHEAD_$his_tree
185 }
186
187 clean_abort () {
188         test $# = 0 || echo >&2 "$@"
189         rm -fr "$dotest"
190         exit 1
191 }
192
193 patch_format=
194
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
200         fi
201
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
208         fi
209
210         # otherwise, check the first few non-blank lines of the first
211         # patch to try to detect its format
212         {
213                 # Start from first line containing non-whitespace
214                 l1=
215                 while test -z "$l1"
216                 do
217                         read l1 || break
218                 done
219                 read l2
220                 read l3
221                 case "$l1" in
222                 "From "* | "From: "*)
223                         patch_format=mbox
224                         ;;
225                 '# This series applies on GIT commit'*)
226                         patch_format=stgit-series
227                         ;;
228                 "# HG changeset patch")
229                         patch_format=hg
230                         ;;
231                 *)
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
238                                 ;;
239                         *)
240                                 ;;
241                         esac
242                         ;;
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
258                 fi
259         } < "$1" || clean_abort
260 }
261
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=
270                 fi
271                 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
272                 clean_abort
273                 ;;
274         stgit-series)
275                 if test $# -ne 1
276                 then
277                         clean_abort "$(gettext "Only one StGIT patch series can be applied at once")"
278                 fi
279                 series_dir=$(dirname "$1")
280                 series_file="$1"
281                 shift
282                 {
283                         set x
284                         while read filename
285                         do
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 "$@"
297                 ;;
298         stgit)
299                 this=0
300                 for stgit in "$@"
301                 do
302                         this=$(expr "$this" + 1)
303                         msgnum=$(printf "%0${prec}d" $this)
304                         # Perl version of StGIT parse_patch. The first nonemptyline
305                         # not starting with Author, From or Date is the
306                         # subject, and the body starts with the next nonempty
307                         # line not starting with Author, From or Date
308                         @@PERL@@ -ne 'BEGIN { $subject = 0 }
309                                 if ($subject > 1) { print ; }
310                                 elsif (/^\s+$/) { next ; }
311                                 elsif (/^Author:/) { s/Author/From/ ; print ;}
312                                 elsif (/^(From|Date)/) { print ; }
313                                 elsif ($subject) {
314                                         $subject = 2 ;
315                                         print "\n" ;
316                                         print ;
317                                 } else {
318                                         print "Subject: ", $_ ;
319                                         $subject = 1;
320                                 }
321                         ' < "$stgit" > "$dotest/$msgnum" || clean_abort
322                 done
323                 echo "$this" > "$dotest/last"
324                 this=
325                 msgnum=
326                 ;;
327         hg)
328                 this=0
329                 for hg in "$@"
330                 do
331                         this=$(( $this + 1 ))
332                         msgnum=$(printf "%0${prec}d" $this)
333                         # hg stores changeset metadata in #-commented lines preceding
334                         # the commit message and diff(s). The only metadata we care about
335                         # are the User and Date (Node ID and Parent are hashes which are
336                         # only relevant to the hg repository and thus not useful to us)
337                         # Since we cannot guarantee that the commit message is in
338                         # git-friendly format, we put no Subject: line and just consume
339                         # all of the message as the body
340                         LANG=C LC_ALL=C @@PERL@@ -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
341                                 if ($subject) { print ; }
342                                 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
343                                 elsif (/^\# Date /) {
344                                         my ($hashsign, $str, $time, $tz) = split ;
345                                         $tz = sprintf "%+05d", (0-$tz)/36;
346                                         print "Date: " .
347                                               strftime("%a, %d %b %Y %H:%M:%S ",
348                                                        localtime($time))
349                                               . "$tz\n";
350                                 } elsif (/^\# /) { next ; }
351                                 else {
352                                         print "\n", $_ ;
353                                         $subject = 1;
354                                 }
355                         ' <"$hg" >"$dotest/$msgnum" || clean_abort
356                 done
357                 echo "$this" >"$dotest/last"
358                 this=
359                 msgnum=
360                 ;;
361         *)
362                 if test -n "$patch_format"
363                 then
364                         clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
365                 else
366                         clean_abort "$(gettext "Patch format detection failed.")"
367                 fi
368                 ;;
369         esac
370 }
371
372 prec=4
373 dotest="$GIT_DIR/rebase-apply"
374 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
375 messageid= resolvemsg= resume= scissors= no_inbody_headers=
376 git_apply_opt=
377 committer_date_is_author_date=
378 ignore_date=
379 allow_rerere_autoupdate=
380 gpg_sign_opt=
381 threeway=
382
383 if test "$(git config --bool --get am.messageid)" = true
384 then
385     messageid=t
386 fi
387
388 if test "$(git config --bool --get am.keepcr)" = true
389 then
390     keepcr=t
391 fi
392
393 if test "$(git config --bool --get am.threeWay)" = true
394 then
395     threeway=t
396 fi
397
398 while test $# != 0
399 do
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."
406                 ;;
407         -3|--3way)
408                 threeway=t ;;
409         --no-3way)
410                 threeway=f ;;
411         -s|--signoff)
412                 sign=t ;;
413         -u|--utf8)
414                 utf8=t ;; # this is now default
415         --no-utf8)
416                 utf8= ;;
417         -m|--message-id)
418                 messageid=t ;;
419         --no-message-id)
420                 messageid=f ;;
421         -k|--keep)
422                 keep=t ;;
423         --keep-non-patch)
424                 keep=b ;;
425         -c|--scissors)
426                 scissors=t ;;
427         --no-scissors)
428                 scissors=f ;;
429         -r|--resolved|--continue)
430                 resolved=t ;;
431         --skip)
432                 skip=t ;;
433         --abort)
434                 abort=t ;;
435         --rebasing)
436                 rebasing=t threeway=t ;;
437         --resolvemsg=*)
438                 resolvemsg="${1#--resolvemsg=}" ;;
439         --whitespace=*|--directory=*|--exclude=*|--include=*)
440                 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
441         -C*|-p*)
442                 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
443         --patch-format=*)
444                 patch_format="${1#--patch-format=}" ;;
445         --reject|--ignore-whitespace|--ignore-space-change)
446                 git_apply_opt="$git_apply_opt $1" ;;
447         --committer-date-is-author-date)
448                 committer_date_is_author_date=t ;;
449         --ignore-date)
450                 ignore_date=t ;;
451         --rerere-autoupdate|--no-rerere-autoupdate)
452                 allow_rerere_autoupdate="$1" ;;
453         -q|--quiet)
454                 GIT_QUIET=t ;;
455         --keep-cr)
456                 keepcr=t ;;
457         --no-keep-cr)
458                 keepcr=f ;;
459         --gpg-sign)
460                 gpg_sign_opt=-S ;;
461         --gpg-sign=*)
462                 gpg_sign_opt="-S${1#--gpg-sign=}" ;;
463         --)
464                 shift; break ;;
465         *)
466                 usage ;;
467         esac
468         shift
469 done
470
471 # If the dotest directory exists, but we have finished applying all the
472 # patches in them, clear it out.
473 if test -d "$dotest" &&
474    test -f "$dotest/last" &&
475    test -f "$dotest/next" &&
476    last=$(cat "$dotest/last") &&
477    next=$(cat "$dotest/next") &&
478    test $# != 0 &&
479    test "$next" -gt "$last"
480 then
481    rm -fr "$dotest"
482 fi
483
484 if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
485 then
486         case "$#,$skip$resolved$abort" in
487         0,*t*)
488                 # Explicit resume command and we do not have file, so
489                 # we are happy.
490                 : ;;
491         0,)
492                 # No file input but without resume parameters; catch
493                 # user error to feed us a patch from standard input
494                 # when there is already $dotest.  This is somewhat
495                 # unreliable -- stdin could be /dev/null for example
496                 # and the caller did not intend to feed us a patch but
497                 # wanted to continue unattended.
498                 test -t 0
499                 ;;
500         *)
501                 false
502                 ;;
503         esac ||
504         die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
505         resume=yes
506
507         case "$skip,$abort" in
508         t,t)
509                 die "$(gettext "Please make up your mind. --skip or --abort?")"
510                 ;;
511         t,)
512                 git rerere clear
513                 git read-tree --reset -u HEAD HEAD
514                 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
515                 git reset HEAD
516                 git update-ref ORIG_HEAD $orig_head
517                 ;;
518         ,t)
519                 if test -f "$dotest/rebasing"
520                 then
521                         exec git rebase --abort
522                 fi
523                 git rerere clear
524                 if safe_to_abort
525                 then
526                         git read-tree --reset -u HEAD ORIG_HEAD
527                         git reset ORIG_HEAD
528                 fi
529                 rm -fr "$dotest"
530                 exit ;;
531         esac
532         rm -f "$dotest/dirtyindex"
533 else
534         # Possible stray $dotest directory in the independent-run
535         # case; in the --rebasing case, it is upto the caller
536         # (git-rebase--am) to take care of stray directories.
537         if test -d "$dotest" && test -z "$rebasing"
538         then
539                 case "$skip,$resolved,$abort" in
540                 ,,t)
541                         rm -fr "$dotest"
542                         exit 0
543                         ;;
544                 *)
545                         die "$(eval_gettext "Stray \$dotest directory found.
546 Use \"git am --abort\" to remove it.")"
547                         ;;
548                 esac
549         fi
550
551         # Make sure we are not given --skip, --continue, or --abort
552         test "$skip$resolved$abort" = "" ||
553                 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
554
555         # Start afresh.
556         mkdir -p "$dotest" || exit
557
558         if test -n "$prefix" && test $# != 0
559         then
560                 first=t
561                 for arg
562                 do
563                         test -n "$first" && {
564                                 set x
565                                 first=
566                         }
567                         if is_absolute_path "$arg"
568                         then
569                                 set "$@" "$arg"
570                         else
571                                 set "$@" "$prefix$arg"
572                         fi
573                 done
574                 shift
575         fi
576
577         check_patch_format "$@"
578
579         split_patches "$@"
580
581         # -i can and must be given when resuming; everything
582         # else is kept
583         echo " $git_apply_opt" >"$dotest/apply-opt"
584         echo "$threeway" >"$dotest/threeway"
585         echo "$sign" >"$dotest/sign"
586         echo "$utf8" >"$dotest/utf8"
587         echo "$keep" >"$dotest/keep"
588         echo "$messageid" >"$dotest/messageid"
589         echo "$scissors" >"$dotest/scissors"
590         echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
591         echo "$GIT_QUIET" >"$dotest/quiet"
592         echo 1 >"$dotest/next"
593         if test -n "$rebasing"
594         then
595                 : >"$dotest/rebasing"
596         else
597                 : >"$dotest/applying"
598                 if test -n "$HAS_HEAD"
599                 then
600                         git update-ref ORIG_HEAD HEAD
601                 else
602                         git update-ref -d ORIG_HEAD >/dev/null 2>&1
603                 fi
604         fi
605 fi
606
607 git update-index -q --refresh
608
609 case "$resolved" in
610 '')
611         case "$HAS_HEAD" in
612         '')
613                 files=$(git ls-files) ;;
614         ?*)
615                 files=$(git diff-index --cached --name-only HEAD --) ;;
616         esac || exit
617         if test "$files"
618         then
619                 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
620                 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
621         fi
622 esac
623
624 # Now, decide what command line options we will give to the git
625 # commands we invoke, based on the result of parsing command line
626 # options and previous invocation state stored in $dotest/ files.
627
628 if test "$(cat "$dotest/utf8")" = t
629 then
630         utf8=-u
631 else
632         utf8=-n
633 fi
634 keep=$(cat "$dotest/keep")
635 case "$keep" in
636 t)
637         keep=-k ;;
638 b)
639         keep=-b ;;
640 *)
641         keep= ;;
642 esac
643 case "$(cat "$dotest/messageid")" in
644 t)
645         messageid=-m ;;
646 f)
647         messageid= ;;
648 esac
649 case "$(cat "$dotest/scissors")" in
650 t)
651         scissors=--scissors ;;
652 f)
653         scissors=--no-scissors ;;
654 esac
655 if test "$(cat "$dotest/no_inbody_headers")" = t
656 then
657         no_inbody_headers=--no-inbody-headers
658 else
659         no_inbody_headers=
660 fi
661 if test "$(cat "$dotest/quiet")" = t
662 then
663         GIT_QUIET=t
664 fi
665 if test "$(cat "$dotest/threeway")" = t
666 then
667         threeway=t
668 else
669         threeway=f
670 fi
671 git_apply_opt=$(cat "$dotest/apply-opt")
672 if test "$(cat "$dotest/sign")" = t
673 then
674         SIGNOFF=$(git var GIT_COMMITTER_IDENT | sed -e '
675                         s/>.*/>/
676                         s/^/Signed-off-by: /'
677                 )
678 else
679         SIGNOFF=
680 fi
681
682 last=$(cat "$dotest/last")
683 this=$(cat "$dotest/next")
684 if test "$skip" = t
685 then
686         this=$(expr "$this" + 1)
687         resume=
688 fi
689
690 while test "$this" -le "$last"
691 do
692         msgnum=$(printf "%0${prec}d" $this)
693         next=$(expr "$this" + 1)
694         test -f "$dotest/$msgnum" || {
695                 resume=
696                 go_next
697                 continue
698         }
699
700         # If we are not resuming, parse and extract the patch information
701         # into separate files:
702         #  - info records the authorship and title
703         #  - msg is the rest of commit log message
704         #  - patch is the patch body.
705         #
706         # When we are resuming, these files are either already prepared
707         # by the user, or the user can tell us to do so by --continue flag.
708         case "$resume" in
709         '')
710                 if test -f "$dotest/rebasing"
711                 then
712                         commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
713                                 -e q "$dotest/$msgnum") &&
714                         test "$(git cat-file -t "$commit")" = commit ||
715                                 stop_here $this
716                         git cat-file commit "$commit" |
717                         sed -e '1,/^$/d' >"$dotest/msg-clean"
718                         echo "$commit" >"$dotest/original-commit"
719                         get_author_ident_from_commit "$commit" >"$dotest/author-script"
720                         git diff-tree --root --binary --full-index "$commit" >"$dotest/patch"
721                 else
722                         git mailinfo $keep $no_inbody_headers $messageid $scissors $utf8 "$dotest/msg" "$dotest/patch" \
723                                 <"$dotest/$msgnum" >"$dotest/info" ||
724                                 stop_here $this
725
726                         # skip pine's internal folder data
727                         sane_grep '^Author: Mail System Internal Data$' \
728                                 <"$dotest"/info >/dev/null &&
729                                 go_next && continue
730
731                         test -s "$dotest/patch" || {
732                                 eval_gettextln "Patch is empty.  Was it split wrong?
733 If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
734 To restore the original branch and stop patching run \"\$cmdline --abort\"."
735                                 stop_here $this
736                         }
737                         rm -f "$dotest/original-commit" "$dotest/author-script"
738                         {
739                                 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
740                                 echo
741                                 cat "$dotest/msg"
742                         } |
743                         git stripspace > "$dotest/msg-clean"
744                 fi
745                 ;;
746         esac
747
748         if test -f "$dotest/author-script"
749         then
750                 eval $(cat "$dotest/author-script")
751         else
752                 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
753                 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
754                 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
755         fi
756
757         if test -z "$GIT_AUTHOR_EMAIL"
758         then
759                 gettextln "Patch does not have a valid e-mail address."
760                 stop_here $this
761         fi
762
763         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
764
765         case "$resume" in
766         '')
767             if test '' != "$SIGNOFF"
768             then
769                 LAST_SIGNED_OFF_BY=$(
770                     sed -ne '/^Signed-off-by: /p' \
771                     "$dotest/msg-clean" |
772                     sed -ne '$p'
773                 )
774                 ADD_SIGNOFF=$(
775                     test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
776                     test '' = "$LAST_SIGNED_OFF_BY" && echo
777                     echo "$SIGNOFF"
778                 })
779             else
780                 ADD_SIGNOFF=
781             fi
782             {
783                 if test -s "$dotest/msg-clean"
784                 then
785                         cat "$dotest/msg-clean"
786                 fi
787                 if test '' != "$ADD_SIGNOFF"
788                 then
789                         echo "$ADD_SIGNOFF"
790                 fi
791             } >"$dotest/final-commit"
792             ;;
793         *)
794                 case "$resolved$interactive" in
795                 tt)
796                         # This is used only for interactive view option.
797                         git diff-index -p --cached HEAD -- >"$dotest/patch"
798                         ;;
799                 esac
800         esac
801
802         resume=
803         if test "$interactive" = t
804         then
805             test -t 0 ||
806             die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
807             action=again
808             while test "$action" = again
809             do
810                 gettextln "Commit Body is:"
811                 echo "--------------------------"
812                 cat "$dotest/final-commit"
813                 echo "--------------------------"
814                 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
815                 # in your translation. The program will only accept English
816                 # input at this point.
817                 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
818                 read reply
819                 case "$reply" in
820                 [yY]*) action=yes ;;
821                 [aA]*) action=yes interactive= ;;
822                 [nN]*) action=skip ;;
823                 [eE]*) git_editor "$dotest/final-commit"
824                        action=again ;;
825                 [vV]*) action=again
826                        git_pager "$dotest/patch" ;;
827                 *)     action=again ;;
828                 esac
829             done
830         else
831             action=yes
832         fi
833
834         if test $action = skip
835         then
836                 go_next
837                 continue
838         fi
839
840         hook="$(git rev-parse --git-path hooks/applypatch-msg)"
841         if test -x "$hook"
842         then
843                 "$hook" "$dotest/final-commit" || stop_here $this
844         fi
845
846         if test -f "$dotest/final-commit"
847         then
848                 FIRSTLINE=$(sed 1q "$dotest/final-commit")
849         else
850                 FIRSTLINE=""
851         fi
852
853         say "$(eval_gettext "Applying: \$FIRSTLINE")"
854
855         case "$resolved" in
856         '')
857                 # When we are allowed to fall back to 3-way later, don't give
858                 # false errors during the initial attempt.
859                 squelch=
860                 if test "$threeway" = t
861                 then
862                         squelch='>/dev/null 2>&1 '
863                 fi
864                 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
865                 apply_status=$?
866                 ;;
867         t)
868                 # Resolved means the user did all the hard work, and
869                 # we do not have to do any patch application.  Just
870                 # trust what the user has in the index file and the
871                 # working tree.
872                 resolved=
873                 git diff-index --quiet --cached HEAD -- && {
874                         gettextln "No changes - did you forget to use 'git add'?
875 If there is nothing left to stage, chances are that something else
876 already introduced the same changes; you might want to skip this patch."
877                         stop_here_user_resolve $this
878                 }
879                 unmerged=$(git ls-files -u)
880                 if test -n "$unmerged"
881                 then
882                         gettextln "You still have unmerged paths in your index
883 did you forget to use 'git add'?"
884                         stop_here_user_resolve $this
885                 fi
886                 apply_status=0
887                 git rerere
888                 ;;
889         esac
890
891         if test $apply_status != 0 && test "$threeway" = t
892         then
893                 if (fall_back_3way)
894                 then
895                     # Applying the patch to an earlier tree and merging the
896                     # result may have produced the same tree as ours.
897                     git diff-index --quiet --cached HEAD -- && {
898                         say "$(gettext "No changes -- Patch already applied.")"
899                         go_next
900                         continue
901                     }
902                     # clear apply_status -- we have successfully merged.
903                     apply_status=0
904                 fi
905         fi
906         if test $apply_status != 0
907         then
908                 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
909                 if test "$(git config --bool advice.amworkdir)" != false
910                 then
911                         eval_gettextln 'The copy of the patch that failed is found in:
912    $dotest/patch'
913                 fi
914                 stop_here_user_resolve $this
915         fi
916
917         hook="$(git rev-parse --git-path hooks/pre-applypatch)"
918         if test -x "$hook"
919         then
920                 "$hook" || stop_here $this
921         fi
922
923         tree=$(git write-tree) &&
924         commit=$(
925                 if test -n "$ignore_date"
926                 then
927                         GIT_AUTHOR_DATE=
928                 fi
929                 parent=$(git rev-parse --verify -q HEAD) ||
930                 say >&2 "$(gettext "applying to an empty history")"
931
932                 if test -n "$committer_date_is_author_date"
933                 then
934                         GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
935                         export GIT_COMMITTER_DATE
936                 fi &&
937                 git commit-tree ${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree  \
938                         <"$dotest/final-commit"
939         ) &&
940         git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
941         stop_here $this
942
943         if test -f "$dotest/original-commit"; then
944                 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
945         fi
946
947         hook="$(git rev-parse --git-path hooks/post-applypatch)"
948         test -x "$hook" && "$hook"
949
950         go_next
951 done
952
953 if test -s "$dotest"/rewritten; then
954     git notes copy --for-rewrite=rebase < "$dotest"/rewritten
955     hook="$(git rev-parse --git-path hooks/post-rewrite)"
956     if test -x "$hook"; then
957         "$hook" rebase < "$dotest"/rewritten
958     fi
959 fi
960
961 # If am was called with --rebasing (from git-rebase--am), it's up to
962 # the caller to take care of housekeeping.
963 if ! test -f "$dotest/rebasing"
964 then
965         rm -fr "$dotest"
966         git gc --auto
967 fi