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"
73 git rev-parse
--sq-quote "$@"
77 echo "$1" >"$dotest/next"
78 git rev-parse
--verify -q HEAD
>"$dotest/abort-safety"
83 if test -f "$dotest/dirtyindex"
88 if ! test -s "$dotest/abort-safety"
93 abort_safety
=$
(cat "$dotest/abort-safety")
94 if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
98 gettextln
"You seem to have moved HEAD since the last 'am' failure.
99 Not rewinding to ORIG_HEAD" >&2
103 stop_here_user_resolve
() {
104 if [ -n "$resolvemsg" ]; then
105 printf '%s\n' "$resolvemsg"
108 eval_gettextln
"When you have resolved this problem, run \"\$cmdline --continue\".
109 If you prefer to skip this patch, run \"\$cmdline --skip\" instead.
110 To restore the original branch and stop patching, run \"\$cmdline --abort\"."
116 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
117 "$dotest/patch" "$dotest/info"
118 echo "$next" >"$dotest/next"
124 gettextln
"Cannot fall back to three-way merge."
129 O_OBJECT
=$
(cd "$GIT_OBJECT_DIRECTORY" && pwd)
131 rm -fr "$dotest"/patch-merge-
*
132 mkdir
"$dotest/patch-merge-tmp-dir"
134 # First see if the patch records the index info that we can use.
135 cmd
="git apply $git_apply_opt --build-fake-ancestor" &&
136 cmd
="$cmd "'"$dotest/patch-merge-tmp-index" "$dotest/patch"' &&
138 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
139 git write-tree
>"$dotest/patch-merge-base+" ||
140 cannot_fallback
"$(gettext "Repository lacks necessary blobs to fall back on
3-way merge.
")"
142 say
"$(gettext "Using index info to reconstruct a base tree...
")"
144 cmd
='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
146 if test -z "$GIT_QUIET"
148 eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
151 cmd
="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
154 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
155 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
157 cannot_fallback
"$(gettext "Did you hand edit your
patch?
158 It does not apply to blobs recorded
in its index.
")"
161 test -f "$dotest/patch-merge-index" &&
162 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
163 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
164 rm -fr "$dotest"/patch-merge-
* ||
exit 1
166 say
"$(gettext "Falling back to patching base and
3-way merge...
")"
168 # This is not so wrong. Depending on which base we picked,
169 # orig_tree may be wildly different from ours, but his_tree
170 # has the same set of wildly different changes in parts the
171 # patch did not touch, so recursive ends up canceling them,
172 # saying that we reverted all those changes.
174 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
175 export GITHEAD_
$his_tree
176 if test -n "$GIT_QUIET"
178 GIT_MERGE_VERBOSITY
=0 && export GIT_MERGE_VERBOSITY
180 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
181 git rerere
$allow_rerere_autoupdate
182 die
"$(gettext "Failed to merge
in the changes.
")"
184 unset GITHEAD_
$his_tree
188 test $# = 0 ||
echo >&2 "$@"
195 check_patch_format
() {
196 # early return if patch_format was set from the command line
197 if test -n "$patch_format"
202 # we default to mbox format if input is from stdin and for
204 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
210 # otherwise, check the first few non-blank lines of the first
211 # patch to try to detect its format
213 # Start from first line containing non-whitespace
222 "From "* |
"From: "*)
225 '# This series applies on GIT commit'*)
226 patch_format
=stgit-series
228 "# HG changeset patch")
232 # if the second line is empty and the third is
233 # a From, Author or Date entry, this is very
234 # likely an StGIT patch
236 ,"From: "* |
,"Author: "* |
,"Date: "*)
244 if test -z "$patch_format" &&
249 # This begins with three non-empty lines. Is this a
250 # piece of e-mail a-la RFC2822? Grab all the headers,
251 # discarding the indented remainder of folded lines,
252 # and see if it looks like that they all begin with the
253 # header field names...
255 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
256 sane_egrep
-v '^[!-9;-~]+:' >/dev
/null ||
259 } < "$1" || clean_abort
263 case "$patch_format" in
265 if test t
= "$keepcr"
271 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
277 clean_abort
"$(gettext "Only one StGIT
patch series can be applied
at once
")"
279 series_dir
=$
(dirname "$1")
286 set "$@" "$series_dir/$filename"
288 # remove the safety x
290 # remove the arg coming from the first-line comment
292 } < "$series_file" || clean_abort
293 # set the patch format appropriately
295 # now handle the actual StGIT patches
300 test 0 -eq "$#" && set -- -
303 this
=$
(expr "$this" + 1)
304 msgnum
=$
(printf "%0${prec}d" $this)
305 # Perl version of StGIT parse_patch. The first nonemptyline
306 # not starting with Author, From or Date is the
307 # subject, and the body starts with the next nonempty
308 # line not starting with Author, From or Date
309 @@PERL@@
-ne 'BEGIN { $subject = 0 }
310 if ($subject > 1) { print ; }
311 elsif (/^\s+$/) { next ; }
312 elsif (/^Author:/) { s/Author/From/ ; print ;}
313 elsif (/^(From|Date)/) { print ; }
319 print "Subject: ", $_ ;
322 ' -- "$stgit" >"$dotest/$msgnum" || clean_abort
324 echo "$this" > "$dotest/last"
330 test 0 -eq "$#" && set -- -
333 this
=$
(( $this + 1 ))
334 msgnum
=$
(printf "%0${prec}d" $this)
335 # hg stores changeset metadata in #-commented lines preceding
336 # the commit message and diff(s). The only metadata we care about
337 # are the User and Date (Node ID and Parent are hashes which are
338 # only relevant to the hg repository and thus not useful to us)
339 # Since we cannot guarantee that the commit message is in
340 # git-friendly format, we put no Subject: line and just consume
341 # all of the message as the body
342 LANG
=C LC_ALL
=C @@PERL@@
-M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
343 if ($subject) { print ; }
344 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
345 elsif (/^\# Date /) {
346 my ($hashsign, $str, $time, $tz) = split ;
347 $tz_str = sprintf "%+05d", (0-$tz)/36;
349 strftime("%a, %d %b %Y %H:%M:%S ",
352 } elsif (/^\# /) { next ; }
357 ' -- "$hg" >"$dotest/$msgnum" || clean_abort
359 echo "$this" >"$dotest/last"
364 if test -n "$patch_format"
366 clean_abort
"$(eval_gettext "Patch format \
$patch_format is not supported.
")"
368 clean_abort
"$(gettext "Patch format detection failed.
")"
375 dotest
="$GIT_DIR/rebase-apply"
376 sign
= utf8
=t keep
= keepcr
= skip
= interactive
= resolved
= rebasing
= abort
=
377 messageid
= resolvemsg
= resume
= scissors
= no_inbody_headers
=
379 committer_date_is_author_date
=
381 allow_rerere_autoupdate
=
384 if test "$(git config --bool --get am.messageid)" = true
389 if test "$(git config --bool --get am.keepcr)" = true
400 gettextln
>&2 "The -b/--binary option has been a no-op for long time, and
401 it will be removed. Please do not use it anymore."
408 utf8
=t
;; # this is now default
423 -r|
--resolved|
--continue)
430 rebasing
=t threeway
=t
;;
432 resolvemsg
="${1#--resolvemsg=}" ;;
433 --whitespace=*|
--directory=*|
--exclude=*|
--include=*)
434 git_apply_opt
="$git_apply_opt $(sq "$1")" ;;
436 git_apply_opt
="$git_apply_opt $(sq "$1")" ;;
438 patch_format
="${1#--patch-format=}" ;;
439 --reject|
--ignore-whitespace|
--ignore-space-change)
440 git_apply_opt
="$git_apply_opt $1" ;;
441 --committer-date-is-author-date)
442 committer_date_is_author_date
=t
;;
445 --rerere-autoupdate|
--no-rerere-autoupdate)
446 allow_rerere_autoupdate
="$1" ;;
456 gpg_sign_opt
="-S${1#--gpg-sign=}" ;;
465 # If the dotest directory exists, but we have finished applying all the
466 # patches in them, clear it out.
467 if test -d "$dotest" &&
468 test -f "$dotest/last" &&
469 test -f "$dotest/next" &&
470 last
=$
(cat "$dotest/last") &&
471 next
=$
(cat "$dotest/next") &&
473 test "$next" -gt "$last"
478 if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
480 case "$#,$skip$resolved$abort" in
482 # Explicit resume command and we do not have file, so
486 # No file input but without resume parameters; catch
487 # user error to feed us a patch from standard input
488 # when there is already $dotest. This is somewhat
489 # unreliable -- stdin could be /dev/null for example
490 # and the caller did not intend to feed us a patch but
491 # wanted to continue unattended.
498 die
"$(eval_gettext "previous rebase directory \
$dotest still exists but mbox given.
")"
501 case "$skip,$abort" in
503 die
"$(gettext "Please
make up your mind.
--skip or
--abort?
")"
507 git read-tree
--reset -u HEAD HEAD
508 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
510 git update-ref ORIG_HEAD
$orig_head
513 if test -f "$dotest/rebasing"
515 exec git rebase
--abort
520 git read-tree
--reset -u HEAD ORIG_HEAD
526 rm -f "$dotest/dirtyindex"
528 # Possible stray $dotest directory in the independent-run
529 # case; in the --rebasing case, it is upto the caller
530 # (git-rebase--am) to take care of stray directories.
531 if test -d "$dotest" && test -z "$rebasing"
533 case "$skip,$resolved,$abort" in
539 die
"$(eval_gettext "Stray \
$dotest directory found.
540 Use
\"git am
--abort\" to remove it.
")"
545 # Make sure we are not given --skip, --continue, or --abort
546 test "$skip$resolved$abort" = "" ||
547 die
"$(gettext "Resolve operation not
in progress
, we are not resuming.
")"
550 mkdir
-p "$dotest" ||
exit
552 if test -n "$prefix" && test $# != 0
557 test -n "$first" && {
561 if is_absolute_path
"$arg"
565 set "$@" "$prefix$arg"
571 check_patch_format
"$@"
575 # -i can and must be given when resuming; everything
577 echo " $git_apply_opt" >"$dotest/apply-opt"
578 echo "$threeway" >"$dotest/threeway"
579 echo "$sign" >"$dotest/sign"
580 echo "$utf8" >"$dotest/utf8"
581 echo "$keep" >"$dotest/keep"
582 echo "$messageid" >"$dotest/messageid"
583 echo "$scissors" >"$dotest/scissors"
584 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
585 echo "$GIT_QUIET" >"$dotest/quiet"
586 echo 1 >"$dotest/next"
587 if test -n "$rebasing"
589 : >"$dotest/rebasing"
591 : >"$dotest/applying"
592 if test -n "$HAS_HEAD"
594 git update-ref ORIG_HEAD HEAD
596 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
601 git update-index
-q --refresh
607 files
=$
(git ls-files
) ;;
609 files
=$
(git diff-index
--cached --name-only HEAD
--) ;;
613 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
614 die
"$(eval_gettext "Dirty index
: cannot apply patches
(dirty
: \
$files)")"
618 # Now, decide what command line options we will give to the git
619 # commands we invoke, based on the result of parsing command line
620 # options and previous invocation state stored in $dotest/ files.
622 if test "$(cat "$dotest/utf8
")" = t
628 keep
=$
(cat "$dotest/keep")
637 case "$(cat "$dotest/messageid
")" in
643 case "$(cat "$dotest/scissors
")" in
645 scissors
=--scissors ;;
647 scissors
=--no-scissors ;;
649 if test "$(cat "$dotest/no_inbody_headers
")" = t
651 no_inbody_headers
=--no-inbody-headers
655 if test "$(cat "$dotest/quiet
")" = t
659 if test "$(cat "$dotest/threeway
")" = t
663 git_apply_opt
=$
(cat "$dotest/apply-opt")
664 if test "$(cat "$dotest/sign
")" = t
666 SIGNOFF
=$
(git var GIT_COMMITTER_IDENT |
sed -e '
668 s/^/Signed-off-by: /'
674 last
=$
(cat "$dotest/last")
675 this
=$
(cat "$dotest/next")
678 this
=$
(expr "$this" + 1)
682 while test "$this" -le "$last"
684 msgnum
=$
(printf "%0${prec}d" $this)
685 next
=$
(expr "$this" + 1)
686 test -f "$dotest/$msgnum" ||
{
692 # If we are not resuming, parse and extract the patch information
693 # into separate files:
694 # - info records the authorship and title
695 # - msg is the rest of commit log message
696 # - patch is the patch body.
698 # When we are resuming, these files are either already prepared
699 # by the user, or the user can tell us to do so by --continue flag.
702 if test -f "$dotest/rebasing"
704 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
705 -e q
"$dotest/$msgnum") &&
706 test "$(git cat-file -t "$commit")" = commit ||
708 git cat-file commit
"$commit" |
709 sed -e '1,/^$/d' >"$dotest/msg-clean"
710 echo "$commit" >"$dotest/original-commit"
711 get_author_ident_from_commit
"$commit" >"$dotest/author-script"
712 git diff-tree
--root --binary --full-index "$commit" >"$dotest/patch"
714 git mailinfo
$keep $no_inbody_headers $messageid $scissors $utf8 "$dotest/msg" "$dotest/patch" \
715 <"$dotest/$msgnum" >"$dotest/info" ||
718 # skip pine's internal folder data
719 sane_grep
'^Author: Mail System Internal Data$' \
720 <"$dotest"/info
>/dev
/null
&&
723 test -s "$dotest/patch" ||
{
724 eval_gettextln
"Patch is empty. Was it split wrong?
725 If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
726 To restore the original branch and stop patching run \"\$cmdline --abort\"."
729 rm -f "$dotest/original-commit" "$dotest/author-script"
731 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
735 git stripspace
> "$dotest/msg-clean"
740 if test -f "$dotest/author-script"
742 eval $
(cat "$dotest/author-script")
744 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
745 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
746 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
749 if test -z "$GIT_AUTHOR_EMAIL"
751 gettextln
"Patch does not have a valid e-mail address."
755 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
759 if test '' != "$SIGNOFF"
761 LAST_SIGNED_OFF_BY
=$
(
762 sed -ne '/^Signed-off-by: /p' \
763 "$dotest/msg-clean" |
767 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" ||
{
768 test '' = "$LAST_SIGNED_OFF_BY" && echo
775 if test -s "$dotest/msg-clean"
777 cat "$dotest/msg-clean"
779 if test '' != "$ADD_SIGNOFF"
783 } >"$dotest/final-commit"
786 case "$resolved$interactive" in
788 # This is used only for interactive view option.
789 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
795 if test "$interactive" = t
798 die
"$(gettext "cannot be interactive without stdin connected to a terminal.
")"
800 while test "$action" = again
802 gettextln
"Commit Body is:"
803 echo "--------------------------"
804 cat "$dotest/final-commit"
805 echo "--------------------------"
806 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
807 # in your translation. The program will only accept English
808 # input at this point.
809 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
813 [aA
]*) action
=yes interactive
= ;;
814 [nN
]*) action
=skip
;;
815 [eE
]*) git_editor
"$dotest/final-commit"
818 git_pager
"$dotest/patch" ;;
826 if test $action = skip
832 if test -x "$GIT_DIR"/hooks
/applypatch-msg
834 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
838 if test -f "$dotest/final-commit"
840 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
845 say
"$(eval_gettext "Applying
: \
$FIRSTLINE")"
849 # When we are allowed to fall back to 3-way later, don't give
850 # false errors during the initial attempt.
852 if test "$threeway" = t
854 squelch
='>/dev/null 2>&1 '
856 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
860 # Resolved means the user did all the hard work, and
861 # we do not have to do any patch application. Just
862 # trust what the user has in the index file and the
865 git diff-index
--quiet --cached HEAD
-- && {
866 gettextln
"No changes - did you forget to use 'git add'?
867 If there is nothing left to stage, chances are that something else
868 already introduced the same changes; you might want to skip this patch."
869 stop_here_user_resolve
$this
871 unmerged
=$
(git ls-files
-u)
872 if test -n "$unmerged"
874 gettextln
"You still have unmerged paths in your index
875 did you forget to use 'git add'?"
876 stop_here_user_resolve
$this
883 if test $apply_status != 0 && test "$threeway" = t
887 # Applying the patch to an earlier tree and merging the
888 # result may have produced the same tree as ours.
889 git diff-index
--quiet --cached HEAD
-- && {
890 say
"$(gettext "No changes
-- Patch already applied.
")"
894 # clear apply_status -- we have successfully merged.
898 if test $apply_status != 0
900 eval_gettextln
'Patch failed at $msgnum $FIRSTLINE'
901 if test "$(git config --bool advice.amworkdir)" != false
903 eval_gettextln
'The copy of the patch that failed is found in:
906 stop_here_user_resolve
$this
909 if test -x "$GIT_DIR"/hooks
/pre-applypatch
911 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
914 tree
=$
(git write-tree
) &&
916 if test -n "$ignore_date"
920 parent
=$
(git rev-parse
--verify -q HEAD
) ||
921 say
>&2 "$(gettext "applying to an empty
history")"
923 if test -n "$committer_date_is_author_date"
925 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
926 export GIT_COMMITTER_DATE
928 git commit-tree
${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree \
929 <"$dotest/final-commit"
931 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
934 if test -f "$dotest/original-commit"; then
935 echo "$(cat "$dotest/original-commit
") $commit" >> "$dotest/rewritten"
938 if test -x "$GIT_DIR"/hooks
/post-applypatch
940 "$GIT_DIR"/hooks
/post-applypatch
946 if test -s "$dotest"/rewritten
; then
947 git notes copy
--for-rewrite=rebase
< "$dotest"/rewritten
948 if test -x "$GIT_DIR"/hooks
/post-rewrite
; then
949 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$dotest"/rewritten
953 # If am was called with --rebasing (from git-rebase--am), it's up to
954 # the caller to take care of housekeeping.
955 if ! test -f "$dotest/rebasing"