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