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