Code

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