3 # Copyright (c) 2005, 2006 Junio C Hamano
9 git am [options] [(<mbox>|<Maildir>)...]
10 git am [options] (--continue | --skip | --abort)
12 i,interactive run interactively
13 b,binary* (historical option -- no-op)
14 3,3way allow fall back on 3way merging if needed
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)"
47 prefix
=$
(git rev-parse
--show-prefix)
52 git var GIT_COMMITTER_IDENT
>/dev
/null ||
53 die
"$(gettext "You need to
set your committer info first
")"
55 if git rev-parse
--verify -q HEAD
>/dev
/null
63 if test '' != "$interactive"
67 if test '' != "$threeway"
72 empty_tree
=4b825dc642cb6eb9a060e54bf8d69288fbee4904
75 git rev-parse
--sq-quote "$@"
79 echo "$1" >"$dotest/next"
80 git rev-parse
--verify -q HEAD
>"$dotest/abort-safety"
85 if test -f "$dotest/dirtyindex"
90 if ! test -f "$dotest/abort-safety"
95 abort_safety
=$
(cat "$dotest/abort-safety")
96 if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
100 gettextln
"You seem to have moved HEAD since the last 'am' failure.
101 Not rewinding to ORIG_HEAD" >&2
105 stop_here_user_resolve
() {
106 if [ -n "$resolvemsg" ]; then
107 printf '%s\n' "$resolvemsg"
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\"."
118 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
119 "$dotest/patch" "$dotest/info"
120 echo "$next" >"$dotest/next"
126 gettextln
"Cannot fall back to three-way merge."
131 O_OBJECT
=$
(cd "$GIT_OBJECT_DIRECTORY" && pwd)
133 rm -fr "$dotest"/patch-merge-
*
134 mkdir
"$dotest/patch-merge-tmp-dir"
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"' &&
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.
")"
144 say
"$(gettext "Using index info to reconstruct a base tree...
")"
146 cmd
='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
148 if test -z "$GIT_QUIET"
150 eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
153 cmd
="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
156 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
157 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
159 cannot_fallback
"$(gettext "Did you hand edit your
patch?
160 It does not apply to blobs recorded
in its index.
")"
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
168 say
"$(gettext "Falling back to patching base and
3-way merge...
")"
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.
176 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
177 export GITHEAD_
$his_tree
178 if test -n "$GIT_QUIET"
180 GIT_MERGE_VERBOSITY
=0 && export GIT_MERGE_VERBOSITY
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.
")"
187 unset GITHEAD_
$his_tree
191 test $# = 0 ||
echo >&2 "$@"
198 check_patch_format
() {
199 # early return if patch_format was set from the command line
200 if test -n "$patch_format"
205 # we default to mbox format if input is from stdin and for
207 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
213 # otherwise, check the first few non-blank lines of the first
214 # patch to try to detect its format
216 # Start from first line containing non-whitespace
225 "From "* |
"From: "*)
228 '# This series applies on GIT commit'*)
229 patch_format
=stgit-series
231 "# HG changeset patch")
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
239 ,"From: "* |
,"Author: "* |
,"Date: "*)
247 if test -z "$patch_format" &&
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...
258 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
259 sane_egrep
-v '^[!-9;-~]+:' >/dev
/null ||
262 } < "$1" || clean_abort
266 case "$patch_format" in
268 if test t
= "$keepcr"
274 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
280 clean_abort
"$(gettext "Only one StGIT
patch series can be applied
at once
")"
282 series_dir
=$
(dirname "$1")
289 set "$@" "$series_dir/$filename"
291 # remove the safety x
293 # remove the arg coming from the first-line comment
295 } < "$series_file" || clean_abort
296 # set the patch format appropriately
298 # now handle the actual StGIT patches
305 this
=$
(expr "$this" + 1)
306 msgnum
=$
(printf "%0${prec}d" $this)
307 # Perl version of StGIT parse_patch. The first nonemptyline
308 # not starting with Author, From or Date is the
309 # subject, and the body starts with the next nonempty
310 # line not starting with Author, From or Date
311 @@PERL@@
-ne 'BEGIN { $subject = 0 }
312 if ($subject > 1) { print ; }
313 elsif (/^\s+$/) { next ; }
314 elsif (/^Author:/) { s/Author/From/ ; print ;}
315 elsif (/^(From|Date)/) { print ; }
321 print "Subject: ", $_ ;
324 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
326 echo "$this" > "$dotest/last"
334 this
=$
(( $this + 1 ))
335 msgnum
=$
(printf "%0${prec}d" $this)
336 # hg stores changeset metadata in #-commented lines preceding
337 # the commit message and diff(s). The only metadata we care about
338 # are the User and Date (Node ID and Parent are hashes which are
339 # only relevant to the hg repository and thus not useful to us)
340 # Since we cannot guarantee that the commit message is in
341 # git-friendly format, we put no Subject: line and just consume
342 # all of the message as the body
343 LANG
=C LC_ALL
=C @@PERL@@
-M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
344 if ($subject) { print ; }
345 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
346 elsif (/^\# Date /) {
347 my ($hashsign, $str, $time, $tz) = split ;
348 $tz = sprintf "%+05d", (0-$tz)/36;
350 strftime("%a, %d %b %Y %H:%M:%S ",
353 } elsif (/^\# /) { next ; }
358 ' <"$hg" >"$dotest/$msgnum" || clean_abort
360 echo "$this" >"$dotest/last"
365 if test -n "$patch_format"
367 clean_abort
"$(eval_gettext "Patch format \
$patch_format is not supported.
")"
369 clean_abort
"$(gettext "Patch format detection failed.
")"
376 dotest
="$GIT_DIR/rebase-apply"
377 sign
= utf8
=t keep
= keepcr
= skip
= interactive
= resolved
= rebasing
= abort
=
378 messageid
= resolvemsg
= resume
= scissors
= no_inbody_headers
=
380 committer_date_is_author_date
=
382 allow_rerere_autoupdate
=
386 if test "$(git config --bool --get am.messageid)" = true
391 if test "$(git config --bool --get am.keepcr)" = true
402 gettextln
>&2 "The -b/--binary option has been a no-op for long time, and
403 it will be removed. Please do not use it anymore."
410 utf8
=t
;; # this is now default
425 -r|
--resolved|
--continue)
432 rebasing
=t threeway
=t
;;
434 resolvemsg
="${1#--resolvemsg=}" ;;
435 --whitespace=*|
--directory=*|
--exclude=*|
--include=*)
436 git_apply_opt
="$git_apply_opt $(sq "$1")" ;;
438 git_apply_opt
="$git_apply_opt $(sq "$1")" ;;
440 patch_format
="${1#--patch-format=}" ;;
441 --reject|
--ignore-whitespace|
--ignore-space-change)
442 git_apply_opt
="$git_apply_opt $1" ;;
443 --committer-date-is-author-date)
444 committer_date_is_author_date
=t
;;
447 --rerere-autoupdate|
--no-rerere-autoupdate)
448 allow_rerere_autoupdate
="$1" ;;
458 gpg_sign_opt
="-S${1#--gpg-sign=}" ;;
467 # If the dotest directory exists, but we have finished applying all the
468 # patches in them, clear it out.
469 if test -d "$dotest" &&
470 test -f "$dotest/last" &&
471 test -f "$dotest/next" &&
472 last
=$
(cat "$dotest/last") &&
473 next
=$
(cat "$dotest/next") &&
475 test "$next" -gt "$last"
480 if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
482 case "$#,$skip$resolved$abort" in
484 # Explicit resume command and we do not have file, so
488 # No file input but without resume parameters; catch
489 # user error to feed us a patch from standard input
490 # when there is already $dotest. This is somewhat
491 # unreliable -- stdin could be /dev/null for example
492 # and the caller did not intend to feed us a patch but
493 # wanted to continue unattended.
500 die
"$(eval_gettext "previous rebase directory \
$dotest still exists but mbox given.
")"
503 case "$skip,$abort" in
505 die
"$(gettext "Please
make up your mind.
--skip or
--abort?
")"
509 head_tree
=$
(git rev-parse
--verify -q HEAD ||
echo $empty_tree) &&
510 git read-tree
--reset -u $head_tree $head_tree &&
511 index_tree
=$
(git write-tree
) &&
512 git read-tree
-m -u $index_tree $head_tree
513 git read-tree
$head_tree
516 if test -f "$dotest/rebasing"
518 exec git rebase
--abort
523 head_tree
=$
(git rev-parse
--verify -q HEAD ||
echo $empty_tree) &&
524 git read-tree
--reset -u $head_tree $head_tree &&
525 index_tree
=$
(git write-tree
) &&
526 orig_head
=$
(git rev-parse
--verify -q ORIG_HEAD ||
echo $empty_tree) &&
527 git read-tree
-m -u $index_tree $orig_head
528 if git rev-parse
--verify -q ORIG_HEAD
>/dev
/null
2>&1
532 git read-tree
$empty_tree
533 curr_branch
=$
(git symbolic-ref HEAD
2>/dev
/null
) &&
534 git update-ref
-d $curr_branch
540 rm -f "$dotest/dirtyindex"
542 # Possible stray $dotest directory in the independent-run
543 # case; in the --rebasing case, it is upto the caller
544 # (git-rebase--am) to take care of stray directories.
545 if test -d "$dotest" && test -z "$rebasing"
547 case "$skip,$resolved,$abort" in
553 die
"$(eval_gettext "Stray \
$dotest directory found.
554 Use
\"git am
--abort\" to remove it.
")"
559 # Make sure we are not given --skip, --continue, or --abort
560 test "$skip$resolved$abort" = "" ||
561 die
"$(gettext "Resolve operation not
in progress
, we are not resuming.
")"
564 mkdir
-p "$dotest" ||
exit
566 if test -n "$prefix" && test $# != 0
571 test -n "$first" && {
575 if is_absolute_path
"$arg"
579 set "$@" "$prefix$arg"
585 check_patch_format
"$@"
589 # -i can and must be given when resuming; everything
591 echo " $git_apply_opt" >"$dotest/apply-opt"
592 echo "$threeway" >"$dotest/threeway"
593 echo "$sign" >"$dotest/sign"
594 echo "$utf8" >"$dotest/utf8"
595 echo "$keep" >"$dotest/keep"
596 echo "$messageid" >"$dotest/messageid"
597 echo "$scissors" >"$dotest/scissors"
598 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
599 echo "$GIT_QUIET" >"$dotest/quiet"
600 echo 1 >"$dotest/next"
601 if test -n "$rebasing"
603 : >"$dotest/rebasing"
605 : >"$dotest/applying"
606 if test -n "$HAS_HEAD"
608 git update-ref ORIG_HEAD HEAD
610 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
615 git update-index
-q --refresh
621 files
=$
(git ls-files
) ;;
623 files
=$
(git diff-index
--cached --name-only HEAD
--) ;;
627 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
628 die
"$(eval_gettext "Dirty index
: cannot apply patches
(dirty
: \
$files)")"
632 # Now, decide what command line options we will give to the git
633 # commands we invoke, based on the result of parsing command line
634 # options and previous invocation state stored in $dotest/ files.
636 if test "$(cat "$dotest/utf8
")" = t
642 keep
=$
(cat "$dotest/keep")
651 case "$(cat "$dotest/messageid
")" in
657 case "$(cat "$dotest/scissors
")" in
659 scissors
=--scissors ;;
661 scissors
=--no-scissors ;;
663 if test "$(cat "$dotest/no_inbody_headers
")" = t
665 no_inbody_headers
=--no-inbody-headers
669 if test "$(cat "$dotest/quiet
")" = t
673 if test "$(cat "$dotest/threeway
")" = t
677 git_apply_opt
=$
(cat "$dotest/apply-opt")
678 if test "$(cat "$dotest/sign
")" = t
680 SIGNOFF
=$
(git var GIT_COMMITTER_IDENT |
sed -e '
682 s/^/Signed-off-by: /'
688 last
=$
(cat "$dotest/last")
689 this
=$
(cat "$dotest/next")
692 this
=$
(expr "$this" + 1)
696 while test "$this" -le "$last"
698 msgnum
=$
(printf "%0${prec}d" $this)
699 next
=$
(expr "$this" + 1)
700 test -f "$dotest/$msgnum" ||
{
706 # If we are not resuming, parse and extract the patch information
707 # into separate files:
708 # - info records the authorship and title
709 # - msg is the rest of commit log message
710 # - patch is the patch body.
712 # When we are resuming, these files are either already prepared
713 # by the user, or the user can tell us to do so by --continue flag.
716 if test -f "$dotest/rebasing"
718 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
719 -e q
"$dotest/$msgnum") &&
720 test "$(git cat-file -t "$commit")" = commit ||
722 git cat-file commit
"$commit" |
723 sed -e '1,/^$/d' >"$dotest/msg-clean"
724 echo "$commit" >"$dotest/original-commit"
725 get_author_ident_from_commit
"$commit" >"$dotest/author-script"
726 git diff-tree
--root --binary --full-index "$commit" >"$dotest/patch"
728 git mailinfo
$keep $no_inbody_headers $messageid $scissors $utf8 "$dotest/msg" "$dotest/patch" \
729 <"$dotest/$msgnum" >"$dotest/info" ||
732 # skip pine's internal folder data
733 sane_grep
'^Author: Mail System Internal Data$' \
734 <"$dotest"/info
>/dev
/null
&&
737 test -s "$dotest/patch" ||
{
738 eval_gettextln
"Patch is empty. Was it split wrong?
739 If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
740 To restore the original branch and stop patching run \"\$cmdline --abort\"."
743 rm -f "$dotest/original-commit" "$dotest/author-script"
745 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
749 git stripspace
> "$dotest/msg-clean"
754 if test -f "$dotest/author-script"
756 eval $
(cat "$dotest/author-script")
758 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
759 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
760 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
763 if test -z "$GIT_AUTHOR_EMAIL"
765 gettextln
"Patch does not have a valid e-mail address."
769 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
773 if test '' != "$SIGNOFF"
775 LAST_SIGNED_OFF_BY
=$
(
776 sed -ne '/^Signed-off-by: /p' \
777 "$dotest/msg-clean" |
781 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" ||
{
782 test '' = "$LAST_SIGNED_OFF_BY" && echo
789 if test -s "$dotest/msg-clean"
791 cat "$dotest/msg-clean"
793 if test '' != "$ADD_SIGNOFF"
797 } >"$dotest/final-commit"
800 case "$resolved$interactive" in
802 # This is used only for interactive view option.
803 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
809 if test "$interactive" = t
812 die
"$(gettext "cannot be interactive without stdin connected to a terminal.
")"
814 while test "$action" = again
816 gettextln
"Commit Body is:"
817 echo "--------------------------"
818 cat "$dotest/final-commit"
819 echo "--------------------------"
820 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
821 # in your translation. The program will only accept English
822 # input at this point.
823 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
827 [aA
]*) action
=yes interactive
= ;;
828 [nN
]*) action
=skip
;;
829 [eE
]*) git_editor
"$dotest/final-commit"
832 git_pager
"$dotest/patch" ;;
840 if test $action = skip
846 hook
="$(git rev-parse --git-path hooks/applypatch-msg)"
849 "$hook" "$dotest/final-commit" || stop_here
$this
852 if test -f "$dotest/final-commit"
854 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
859 say
"$(eval_gettext "Applying
: \
$FIRSTLINE")"
863 # When we are allowed to fall back to 3-way later, don't give
864 # false errors during the initial attempt.
866 if test "$threeway" = t
868 squelch
='>/dev/null 2>&1 '
870 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
874 # Resolved means the user did all the hard work, and
875 # we do not have to do any patch application. Just
876 # trust what the user has in the index file and the
879 git diff-index
--quiet --cached HEAD
-- && {
880 gettextln
"No changes - did you forget to use 'git add'?
881 If there is nothing left to stage, chances are that something else
882 already introduced the same changes; you might want to skip this patch."
883 stop_here_user_resolve
$this
885 unmerged
=$
(git ls-files
-u)
886 if test -n "$unmerged"
888 gettextln
"You still have unmerged paths in your index
889 did you forget to use 'git add'?"
890 stop_here_user_resolve
$this
897 if test $apply_status != 0 && test "$threeway" = t
901 # Applying the patch to an earlier tree and merging the
902 # result may have produced the same tree as ours.
903 git diff-index
--quiet --cached HEAD
-- && {
904 say
"$(gettext "No changes
-- Patch already applied.
")"
908 # clear apply_status -- we have successfully merged.
912 if test $apply_status != 0
914 eval_gettextln
'Patch failed at $msgnum $FIRSTLINE'
915 if test "$(git config --bool advice.amworkdir)" != false
917 eval_gettextln
'The copy of the patch that failed is found in:
920 stop_here_user_resolve
$this
923 hook
="$(git rev-parse --git-path hooks/pre-applypatch)"
926 "$hook" || stop_here
$this
929 tree
=$
(git write-tree
) &&
931 if test -n "$ignore_date"
935 parent
=$
(git rev-parse
--verify -q HEAD
) ||
936 say
>&2 "$(gettext "applying to an empty
history")"
938 if test -n "$committer_date_is_author_date"
940 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
941 export GIT_COMMITTER_DATE
943 git commit-tree
${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree \
944 <"$dotest/final-commit"
946 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
949 if test -f "$dotest/original-commit"; then
950 echo "$(cat "$dotest/original-commit
") $commit" >> "$dotest/rewritten"
953 hook
="$(git rev-parse --git-path hooks/post-applypatch)"
954 test -x "$hook" && "$hook"
959 if test -s "$dotest"/rewritten
; then
960 git notes copy
--for-rewrite=rebase
< "$dotest"/rewritten
961 hook
="$(git rev-parse --git-path hooks/post-rewrite)"
962 if test -x "$hook"; then
963 "$hook" rebase
< "$dotest"/rewritten
967 # If am was called with --rebasing (from git-rebase--am), it's up to
968 # the caller to take care of housekeeping.
969 if ! test -f "$dotest/rebasing"