web--browse: look at the BROWSER env var
[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_SPEC="\
8 git am [options] [(<mbox>|<Maildir>)...]
9 git am [options] (--resolved | --skip | --abort)
10 --
11 i,interactive   run interactively
12 b,binary*       (historical option -- no-op)
13 3,3way          allow fall back on 3way merging if needed
14 q,quiet         be quiet
15 s,signoff       add a Signed-off-by line to the commit message
16 u,utf8          recode into utf8 (default)
17 k,keep          pass -k flag to git-mailinfo
18 keep-cr         pass --keep-cr flag to git-mailsplit for mbox format
19 no-keep-cr      do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
20 c,scissors      strip everything before a scissors line
21 whitespace=     pass it through git-apply
22 ignore-space-change pass it through git-apply
23 ignore-whitespace pass it through git-apply
24 directory=      pass it through git-apply
25 C=              pass it through git-apply
26 p=              pass it through git-apply
27 patch-format=   format the patch(es) are in
28 reject          pass it through git-apply
29 resolvemsg=     override error message when patch failure occurs
30 continue        continue applying patches after resolving a conflict
31 r,resolved      synonyms for --continue
32 skip            skip the current patch
33 abort           restore the original branch and abort the patching operation.
34 committer-date-is-author-date    lie about committer date
35 ignore-date     use current timestamp for author date
36 rerere-autoupdate update the index with reused conflict resolution if possible
37 rebasing*       (internal use for git-rebase)"
38
39 . git-sh-setup
40 prefix=$(git rev-parse --show-prefix)
41 set_reflog_action am
42 require_work_tree
43 cd_to_toplevel
44
45 git var GIT_COMMITTER_IDENT >/dev/null ||
46         die "You need to set your committer info first"
47
48 if git rev-parse --verify -q HEAD >/dev/null
49 then
50         HAS_HEAD=yes
51 else
52         HAS_HEAD=
53 fi
54
55 cmdline="git am"
56 if test '' != "$interactive"
57 then
58         cmdline="$cmdline -i"
59 fi
60 if test '' != "$threeway"
61 then
62         cmdline="$cmdline -3"
63 fi
64
65 sq () {
66         git rev-parse --sq-quote "$@"
67 }
68
69 stop_here () {
70     echo "$1" >"$dotest/next"
71     git rev-parse --verify -q HEAD >"$dotest/abort-safety"
72     exit 1
73 }
74
75 safe_to_abort () {
76         if test -f "$dotest/dirtyindex"
77         then
78                 return 1
79         fi
80
81         if ! test -s "$dotest/abort-safety"
82         then
83                 return 0
84         fi
85
86         abort_safety=$(cat "$dotest/abort-safety")
87         if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
88         then
89                 return 0
90         fi
91         echo >&2 "You seem to have moved HEAD since the last 'am' failure."
92         echo >&2 "Not rewinding to ORIG_HEAD"
93         return 1
94 }
95
96 stop_here_user_resolve () {
97     if [ -n "$resolvemsg" ]; then
98             printf '%s\n' "$resolvemsg"
99             stop_here $1
100     fi
101     echo "When you have resolved this problem run \"$cmdline --resolved\"."
102     echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
103     echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
104
105     stop_here $1
106 }
107
108 go_next () {
109         rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
110                 "$dotest/patch" "$dotest/info"
111         echo "$next" >"$dotest/next"
112         this=$next
113 }
114
115 cannot_fallback () {
116         echo "$1"
117         echo "Cannot fall back to three-way merge."
118         exit 1
119 }
120
121 fall_back_3way () {
122     O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
123
124     rm -fr "$dotest"/patch-merge-*
125     mkdir "$dotest/patch-merge-tmp-dir"
126
127     # First see if the patch records the index info that we can use.
128     git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
129         "$dotest/patch" &&
130     GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
131     git write-tree >"$dotest/patch-merge-base+" ||
132     cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
133
134     say Using index info to reconstruct a base tree...
135     if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
136         git apply --cached <"$dotest/patch"
137     then
138         mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
139         mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
140     else
141         cannot_fallback "Did you hand edit your patch?
142 It does not apply to blobs recorded in its index."
143     fi
144
145     test -f "$dotest/patch-merge-index" &&
146     his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
147     orig_tree=$(cat "$dotest/patch-merge-base") &&
148     rm -fr "$dotest"/patch-merge-* || exit 1
149
150     say Falling back to patching base and 3-way merge...
151
152     # This is not so wrong.  Depending on which base we picked,
153     # orig_tree may be wildly different from ours, but his_tree
154     # has the same set of wildly different changes in parts the
155     # patch did not touch, so recursive ends up canceling them,
156     # saying that we reverted all those changes.
157
158     eval GITHEAD_$his_tree='"$FIRSTLINE"'
159     export GITHEAD_$his_tree
160     if test -n "$GIT_QUIET"
161     then
162             GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
163     fi
164     git-merge-recursive $orig_tree -- HEAD $his_tree || {
165             git rerere $allow_rerere_autoupdate
166             echo Failed to merge in the changes.
167             exit 1
168     }
169     unset GITHEAD_$his_tree
170 }
171
172 clean_abort () {
173         test $# = 0 || echo >&2 "$@"
174         rm -fr "$dotest"
175         exit 1
176 }
177
178 patch_format=
179
180 check_patch_format () {
181         # early return if patch_format was set from the command line
182         if test -n "$patch_format"
183         then
184                 return 0
185         fi
186
187         # we default to mbox format if input is from stdin and for
188         # directories
189         if test $# = 0 || test "x$1" = "x-" || test -d "$1"
190         then
191                 patch_format=mbox
192                 return 0
193         fi
194
195         # otherwise, check the first few lines of the first patch to try
196         # to detect its format
197         {
198                 read l1
199                 read l2
200                 read l3
201                 case "$l1" in
202                 "From "* | "From: "*)
203                         patch_format=mbox
204                         ;;
205                 '# This series applies on GIT commit'*)
206                         patch_format=stgit-series
207                         ;;
208                 "# HG changeset patch")
209                         patch_format=hg
210                         ;;
211                 *)
212                         # if the second line is empty and the third is
213                         # a From, Author or Date entry, this is very
214                         # likely an StGIT patch
215                         case "$l2,$l3" in
216                         ,"From: "* | ,"Author: "* | ,"Date: "*)
217                                 patch_format=stgit
218                                 ;;
219                         *)
220                                 ;;
221                         esac
222                         ;;
223                 esac
224                 if test -z "$patch_format" &&
225                         test -n "$l1" &&
226                         test -n "$l2" &&
227                         test -n "$l3"
228                 then
229                         # This begins with three non-empty lines.  Is this a
230                         # piece of e-mail a-la RFC2822?  Grab all the headers,
231                         # discarding the indented remainder of folded lines,
232                         # and see if it looks like that they all begin with the
233                         # header field names...
234                         tr -d '\015' <"$1" |
235                         sed -n -e '/^$/q' -e '/^[       ]/d' -e p |
236                         sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
237                         patch_format=mbox
238                 fi
239         } < "$1" || clean_abort
240 }
241
242 split_patches () {
243         case "$patch_format" in
244         mbox)
245                 if test -n "$rebasing" || test t = "$keepcr"
246                 then
247                     keep_cr=--keep-cr
248                 else
249                     keep_cr=
250                 fi
251                 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
252                 clean_abort
253                 ;;
254         stgit-series)
255                 if test $# -ne 1
256                 then
257                         clean_abort "Only one StGIT patch series can be applied at once"
258                 fi
259                 series_dir=`dirname "$1"`
260                 series_file="$1"
261                 shift
262                 {
263                         set x
264                         while read filename
265                         do
266                                 set "$@" "$series_dir/$filename"
267                         done
268                         # remove the safety x
269                         shift
270                         # remove the arg coming from the first-line comment
271                         shift
272                 } < "$series_file" || clean_abort
273                 # set the patch format appropriately
274                 patch_format=stgit
275                 # now handle the actual StGIT patches
276                 split_patches "$@"
277                 ;;
278         stgit)
279                 this=0
280                 for stgit in "$@"
281                 do
282                         this=`expr "$this" + 1`
283                         msgnum=`printf "%0${prec}d" $this`
284                         # Perl version of StGIT parse_patch. The first nonemptyline
285                         # not starting with Author, From or Date is the
286                         # subject, and the body starts with the next nonempty
287                         # line not starting with Author, From or Date
288                         perl -ne 'BEGIN { $subject = 0 }
289                                 if ($subject > 1) { print ; }
290                                 elsif (/^\s+$/) { next ; }
291                                 elsif (/^Author:/) { print s/Author/From/ ; }
292                                 elsif (/^(From|Date)/) { print ; }
293                                 elsif ($subject) {
294                                         $subject = 2 ;
295                                         print "\n" ;
296                                         print ;
297                                 } else {
298                                         print "Subject: ", $_ ;
299                                         $subject = 1;
300                                 }
301                         ' < "$stgit" > "$dotest/$msgnum" || clean_abort
302                 done
303                 echo "$this" > "$dotest/last"
304                 this=
305                 msgnum=
306                 ;;
307         *)
308                 if test -n "$parse_patch" ; then
309                         clean_abort "Patch format $patch_format is not supported."
310                 else
311                         clean_abort "Patch format detection failed."
312                 fi
313                 ;;
314         esac
315 }
316
317 prec=4
318 dotest="$GIT_DIR/rebase-apply"
319 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
320 resolvemsg= resume= scissors= no_inbody_headers=
321 git_apply_opt=
322 committer_date_is_author_date=
323 ignore_date=
324 allow_rerere_autoupdate=
325
326 if test "$(git config --bool --get am.keepcr)" = true
327 then
328     keepcr=t
329 fi
330
331 while test $# != 0
332 do
333         case "$1" in
334         -i|--interactive)
335                 interactive=t ;;
336         -b|--binary)
337                 : ;;
338         -3|--3way)
339                 threeway=t ;;
340         -s|--signoff)
341                 sign=t ;;
342         -u|--utf8)
343                 utf8=t ;; # this is now default
344         --no-utf8)
345                 utf8= ;;
346         -k|--keep)
347                 keep=t ;;
348         -c|--scissors)
349                 scissors=t ;;
350         --no-scissors)
351                 scissors=f ;;
352         -r|--resolved|--continue)
353                 resolved=t ;;
354         --skip)
355                 skip=t ;;
356         --abort)
357                 abort=t ;;
358         --rebasing)
359                 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
360         -d|--dotest)
361                 die "-d option is no longer supported.  Do not use."
362                 ;;
363         --resolvemsg)
364                 shift; resolvemsg=$1 ;;
365         --whitespace|--directory)
366                 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
367         -C|-p)
368                 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
369         --patch-format)
370                 shift ; patch_format="$1" ;;
371         --reject|--ignore-whitespace|--ignore-space-change)
372                 git_apply_opt="$git_apply_opt $1" ;;
373         --committer-date-is-author-date)
374                 committer_date_is_author_date=t ;;
375         --ignore-date)
376                 ignore_date=t ;;
377         --rerere-autoupdate|--no-rerere-autoupdate)
378                 allow_rerere_autoupdate="$1" ;;
379         -q|--quiet)
380                 GIT_QUIET=t ;;
381         --keep-cr)
382                 keepcr=t ;;
383         --no-keep-cr)
384                 keepcr=f ;;
385         --)
386                 shift; break ;;
387         *)
388                 usage ;;
389         esac
390         shift
391 done
392
393 # If the dotest directory exists, but we have finished applying all the
394 # patches in them, clear it out.
395 if test -d "$dotest" &&
396    last=$(cat "$dotest/last") &&
397    next=$(cat "$dotest/next") &&
398    test $# != 0 &&
399    test "$next" -gt "$last"
400 then
401    rm -fr "$dotest"
402 fi
403
404 if test -d "$dotest"
405 then
406         case "$#,$skip$resolved$abort" in
407         0,*t*)
408                 # Explicit resume command and we do not have file, so
409                 # we are happy.
410                 : ;;
411         0,)
412                 # No file input but without resume parameters; catch
413                 # user error to feed us a patch from standard input
414                 # when there is already $dotest.  This is somewhat
415                 # unreliable -- stdin could be /dev/null for example
416                 # and the caller did not intend to feed us a patch but
417                 # wanted to continue unattended.
418                 test -t 0
419                 ;;
420         *)
421                 false
422                 ;;
423         esac ||
424         die "previous rebase directory $dotest still exists but mbox given."
425         resume=yes
426
427         case "$skip,$abort" in
428         t,t)
429                 die "Please make up your mind. --skip or --abort?"
430                 ;;
431         t,)
432                 git rerere clear
433                 git read-tree --reset -u HEAD HEAD
434                 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
435                 git reset HEAD
436                 git update-ref ORIG_HEAD $orig_head
437                 ;;
438         ,t)
439                 if test -f "$dotest/rebasing"
440                 then
441                         exec git rebase --abort
442                 fi
443                 git rerere clear
444                 if safe_to_abort
445                 then
446                         git read-tree --reset -u HEAD ORIG_HEAD
447                         git reset ORIG_HEAD
448                 fi
449                 rm -fr "$dotest"
450                 exit ;;
451         esac
452         rm -f "$dotest/dirtyindex"
453 else
454         # Make sure we are not given --skip, --resolved, nor --abort
455         test "$skip$resolved$abort" = "" ||
456                 die "Resolve operation not in progress, we are not resuming."
457
458         # Start afresh.
459         mkdir -p "$dotest" || exit
460
461         if test -n "$prefix" && test $# != 0
462         then
463                 first=t
464                 for arg
465                 do
466                         test -n "$first" && {
467                                 set x
468                                 first=
469                         }
470                         if is_absolute_path "$arg"
471                         then
472                                 set "$@" "$arg"
473                         else
474                                 set "$@" "$prefix$arg"
475                         fi
476                 done
477                 shift
478         fi
479
480         check_patch_format "$@"
481
482         split_patches "$@"
483
484         # -i can and must be given when resuming; everything
485         # else is kept
486         echo " $git_apply_opt" >"$dotest/apply-opt"
487         echo "$threeway" >"$dotest/threeway"
488         echo "$sign" >"$dotest/sign"
489         echo "$utf8" >"$dotest/utf8"
490         echo "$keep" >"$dotest/keep"
491         echo "$keepcr" >"$dotest/keepcr"
492         echo "$scissors" >"$dotest/scissors"
493         echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
494         echo "$GIT_QUIET" >"$dotest/quiet"
495         echo 1 >"$dotest/next"
496         if test -n "$rebasing"
497         then
498                 : >"$dotest/rebasing"
499         else
500                 : >"$dotest/applying"
501                 if test -n "$HAS_HEAD"
502                 then
503                         git update-ref ORIG_HEAD HEAD
504                 else
505                         git update-ref -d ORIG_HEAD >/dev/null 2>&1
506                 fi
507         fi
508 fi
509
510 case "$resolved" in
511 '')
512         case "$HAS_HEAD" in
513         '')
514                 files=$(git ls-files) ;;
515         ?*)
516                 files=$(git diff-index --cached --name-only HEAD --) ;;
517         esac || exit
518         if test "$files"
519         then
520                 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
521                 die "Dirty index: cannot apply patches (dirty: $files)"
522         fi
523 esac
524
525 if test "$(cat "$dotest/utf8")" = t
526 then
527         utf8=-u
528 else
529         utf8=-n
530 fi
531 if test "$(cat "$dotest/keep")" = t
532 then
533         keep=-k
534 fi
535 case "$(cat "$dotest/keepcr")" in
536 t)
537         keepcr=--keep-cr ;;
538 f)
539         keepcr=--no-keep-cr ;;
540 esac
541 case "$(cat "$dotest/scissors")" in
542 t)
543         scissors=--scissors ;;
544 f)
545         scissors=--no-scissors ;;
546 esac
547 if test "$(cat "$dotest/no_inbody_headers")" = t
548 then
549         no_inbody_headers=--no-inbody-headers
550 else
551         no_inbody_headers=
552 fi
553 if test "$(cat "$dotest/quiet")" = t
554 then
555         GIT_QUIET=t
556 fi
557 if test "$(cat "$dotest/threeway")" = t
558 then
559         threeway=t
560 fi
561 git_apply_opt=$(cat "$dotest/apply-opt")
562 if test "$(cat "$dotest/sign")" = t
563 then
564         SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
565                         s/>.*/>/
566                         s/^/Signed-off-by: /'
567                 `
568 else
569         SIGNOFF=
570 fi
571
572 last=`cat "$dotest/last"`
573 this=`cat "$dotest/next"`
574 if test "$skip" = t
575 then
576         this=`expr "$this" + 1`
577         resume=
578 fi
579
580 if test "$this" -gt "$last"
581 then
582         say Nothing to do.
583         rm -fr "$dotest"
584         exit
585 fi
586
587 while test "$this" -le "$last"
588 do
589         msgnum=`printf "%0${prec}d" $this`
590         next=`expr "$this" + 1`
591         test -f "$dotest/$msgnum" || {
592                 resume=
593                 go_next
594                 continue
595         }
596
597         # If we are not resuming, parse and extract the patch information
598         # into separate files:
599         #  - info records the authorship and title
600         #  - msg is the rest of commit log message
601         #  - patch is the patch body.
602         #
603         # When we are resuming, these files are either already prepared
604         # by the user, or the user can tell us to do so by --resolved flag.
605         case "$resume" in
606         '')
607                 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
608                         <"$dotest/$msgnum" >"$dotest/info" ||
609                         stop_here $this
610
611                 # skip pine's internal folder data
612                 sane_grep '^Author: Mail System Internal Data$' \
613                         <"$dotest"/info >/dev/null &&
614                         go_next && continue
615
616                 test -s "$dotest/patch" || {
617                         echo "Patch is empty.  Was it split wrong?"
618                         echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
619                         echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
620                         stop_here $this
621                 }
622                 rm -f "$dotest/original-commit" "$dotest/author-script"
623                 if test -f "$dotest/rebasing" &&
624                         commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
625                                 -e q "$dotest/$msgnum") &&
626                         test "$(git cat-file -t "$commit")" = commit
627                 then
628                         git cat-file commit "$commit" |
629                         sed -e '1,/^$/d' >"$dotest/msg-clean"
630                         echo "$commit" > "$dotest/original-commit"
631                         get_author_ident_from_commit "$commit" > "$dotest/author-script"
632                 else
633                         {
634                                 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
635                                 echo
636                                 cat "$dotest/msg"
637                         } |
638                         git stripspace > "$dotest/msg-clean"
639                 fi
640                 ;;
641         esac
642
643         if test -f "$dotest/author-script"
644         then
645                 eval $(cat "$dotest/author-script")
646         else
647                 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
648                 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
649                 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
650         fi
651
652         if test -z "$GIT_AUTHOR_EMAIL"
653         then
654                 echo "Patch does not have a valid e-mail address."
655                 stop_here $this
656         fi
657
658         export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
659
660         case "$resume" in
661         '')
662             if test '' != "$SIGNOFF"
663             then
664                 LAST_SIGNED_OFF_BY=`
665                     sed -ne '/^Signed-off-by: /p' \
666                     "$dotest/msg-clean" |
667                     sed -ne '$p'
668                 `
669                 ADD_SIGNOFF=`
670                     test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
671                     test '' = "$LAST_SIGNED_OFF_BY" && echo
672                     echo "$SIGNOFF"
673                 }`
674             else
675                 ADD_SIGNOFF=
676             fi
677             {
678                 if test -s "$dotest/msg-clean"
679                 then
680                         cat "$dotest/msg-clean"
681                 fi
682                 if test '' != "$ADD_SIGNOFF"
683                 then
684                         echo "$ADD_SIGNOFF"
685                 fi
686             } >"$dotest/final-commit"
687             ;;
688         *)
689                 case "$resolved$interactive" in
690                 tt)
691                         # This is used only for interactive view option.
692                         git diff-index -p --cached HEAD -- >"$dotest/patch"
693                         ;;
694                 esac
695         esac
696
697         resume=
698         if test "$interactive" = t
699         then
700             test -t 0 ||
701             die "cannot be interactive without stdin connected to a terminal."
702             action=again
703             while test "$action" = again
704             do
705                 echo "Commit Body is:"
706                 echo "--------------------------"
707                 cat "$dotest/final-commit"
708                 echo "--------------------------"
709                 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
710                 read reply
711                 case "$reply" in
712                 [yY]*) action=yes ;;
713                 [aA]*) action=yes interactive= ;;
714                 [nN]*) action=skip ;;
715                 [eE]*) git_editor "$dotest/final-commit"
716                        action=again ;;
717                 [vV]*) action=again
718                        git_pager "$dotest/patch" ;;
719                 *)     action=again ;;
720                 esac
721             done
722         else
723             action=yes
724         fi
725
726         if test -f "$dotest/final-commit"
727         then
728                 FIRSTLINE=$(sed 1q "$dotest/final-commit")
729         else
730                 FIRSTLINE=""
731         fi
732
733         if test $action = skip
734         then
735                 go_next
736                 continue
737         fi
738
739         if test -x "$GIT_DIR"/hooks/applypatch-msg
740         then
741                 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
742                 stop_here $this
743         fi
744
745         say "Applying: $FIRSTLINE"
746
747         case "$resolved" in
748         '')
749                 # When we are allowed to fall back to 3-way later, don't give
750                 # false errors during the initial attempt.
751                 squelch=
752                 if test "$threeway" = t
753                 then
754                         squelch='>/dev/null 2>&1 '
755                 fi
756                 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
757                 apply_status=$?
758                 ;;
759         t)
760                 # Resolved means the user did all the hard work, and
761                 # we do not have to do any patch application.  Just
762                 # trust what the user has in the index file and the
763                 # working tree.
764                 resolved=
765                 git diff-index --quiet --cached HEAD -- && {
766                         echo "No changes - did you forget to use 'git add'?"
767                         echo "If there is nothing left to stage, chances are that something else"
768                         echo "already introduced the same changes; you might want to skip this patch."
769                         stop_here_user_resolve $this
770                 }
771                 unmerged=$(git ls-files -u)
772                 if test -n "$unmerged"
773                 then
774                         echo "You still have unmerged paths in your index"
775                         echo "did you forget to use 'git add'?"
776                         stop_here_user_resolve $this
777                 fi
778                 apply_status=0
779                 git rerere
780                 ;;
781         esac
782
783         if test $apply_status != 0 && test "$threeway" = t
784         then
785                 if (fall_back_3way)
786                 then
787                     # Applying the patch to an earlier tree and merging the
788                     # result may have produced the same tree as ours.
789                     git diff-index --quiet --cached HEAD -- && {
790                         say No changes -- Patch already applied.
791                         go_next
792                         continue
793                     }
794                     # clear apply_status -- we have successfully merged.
795                     apply_status=0
796                 fi
797         fi
798         if test $apply_status != 0
799         then
800                 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
801                 stop_here_user_resolve $this
802         fi
803
804         if test -x "$GIT_DIR"/hooks/pre-applypatch
805         then
806                 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
807         fi
808
809         tree=$(git write-tree) &&
810         commit=$(
811                 if test -n "$ignore_date"
812                 then
813                         GIT_AUTHOR_DATE=
814                 fi
815                 parent=$(git rev-parse --verify -q HEAD) ||
816                 say >&2 "applying to an empty history"
817
818                 if test -n "$committer_date_is_author_date"
819                 then
820                         GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
821                         export GIT_COMMITTER_DATE
822                 fi &&
823                 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
824         ) &&
825         git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
826         stop_here $this
827
828         if test -f "$dotest/original-commit"; then
829                 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
830         fi
831
832         if test -x "$GIT_DIR"/hooks/post-applypatch
833         then
834                 "$GIT_DIR"/hooks/post-applypatch
835         fi
836
837         go_next
838 done
839
840 if test -s "$dotest"/rewritten; then
841     git notes copy --for-rewrite=rebase < "$dotest"/rewritten
842     if test -x "$GIT_DIR"/hooks/post-rewrite; then
843         "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
844     fi
845 fi
846
847 rm -fr "$dotest"
848 git gc --auto