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