3 # Copyright (c) 2005, 2006 Junio C Hamano
8 git am [options] [<mbox>|<Maildir>...]
9 git am [options] (--resolved | --skip | --abort)
11 i,interactive run interactively
12 b,binary* (historical option -- no-op)
13 3,3way allow fall back on 3way merging if needed
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)"
40 prefix
=$
(git rev-parse
--show-prefix)
45 git var GIT_COMMITTER_IDENT
>/dev
/null ||
46 die
"You need to set your committer info first"
48 if git rev-parse
--verify -q HEAD
>/dev
/null
56 if test '' != "$interactive"
60 if test '' != "$threeway"
66 git rev-parse
--sq-quote "$@"
70 echo "$1" >"$dotest/next"
74 stop_here_user_resolve
() {
75 if [ -n "$resolvemsg" ]; then
76 printf '%s\n' "$resolvemsg"
79 echo "When you have resolved this problem run \"$cmdline --resolved\"."
80 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
81 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
87 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
88 "$dotest/patch" "$dotest/info"
89 echo "$next" >"$dotest/next"
95 echo "Cannot fall back to three-way merge."
100 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
102 rm -fr "$dotest"/patch-merge-
*
103 mkdir
"$dotest/patch-merge-tmp-dir"
105 # First see if the patch records the index info that we can use.
106 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
108 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
109 git write-tree
>"$dotest/patch-merge-base+" ||
110 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
112 say Using index info to reconstruct a base tree...
113 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
114 git apply
--cached <"$dotest/patch"
116 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
117 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
119 cannot_fallback
"Did you hand edit your patch?
120 It does not apply to blobs recorded in its index."
123 test -f "$dotest/patch-merge-index" &&
124 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
125 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
126 rm -fr "$dotest"/patch-merge-
* ||
exit 1
128 say Falling back to patching base and
3-way merge...
130 # This is not so wrong. Depending on which base we picked,
131 # orig_tree may be wildly different from ours, but his_tree
132 # has the same set of wildly different changes in parts the
133 # patch did not touch, so recursive ends up canceling them,
134 # saying that we reverted all those changes.
136 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
137 export GITHEAD_
$his_tree
138 if test -n "$GIT_QUIET"
140 export GIT_MERGE_VERBOSITY
=0
142 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
143 git rerere
$allow_rerere_autoupdate
144 echo Failed to merge
in the changes.
147 unset GITHEAD_
$his_tree
151 test $# = 0 ||
echo >&2 "$@"
158 check_patch_format
() {
159 # early return if patch_format was set from the command line
160 if test -n "$patch_format"
165 # we default to mbox format if input is from stdin and for
167 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
173 # otherwise, check the first few lines of the first patch to try
174 # to detect its format
180 "From "* |
"From: "*)
183 '# This series applies on GIT commit'*)
184 patch_format
=stgit-series
186 "# HG changeset patch")
190 # if the second line is empty and the third is
191 # a From, Author or Date entry, this is very
192 # likely an StGIT patch
194 ,"From: "* |
,"Author: "* |
,"Date: "*)
202 if test -z "$patch_format" &&
207 # This begins with three non-empty lines. Is this a
208 # piece of e-mail a-la RFC2822? Grab all the headers,
209 # discarding the indented remainder of folded lines,
210 # and see if it looks like that they all begin with the
211 # header field names...
213 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
214 sane_egrep
-v '^[!-9;-~]+:' >/dev
/null ||
217 } < "$1" || clean_abort
221 case "$patch_format" in
223 if test -n "$rebasing" ||
test t
= "$keepcr"
229 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
235 clean_abort
"Only one StGIT patch series can be applied at once"
237 series_dir
=`dirname "$1"`
244 set "$@" "$series_dir/$filename"
246 # remove the safety x
248 # remove the arg coming from the first-line comment
250 } < "$series_file" || clean_abort
251 # set the patch format appropriately
253 # now handle the actual StGIT patches
260 this
=`expr "$this" + 1`
261 msgnum
=`printf "%0${prec}d" $this`
262 # Perl version of StGIT parse_patch. The first nonemptyline
263 # not starting with Author, From or Date is the
264 # subject, and the body starts with the next nonempty
265 # line not starting with Author, From or Date
266 perl
-ne 'BEGIN { $subject = 0 }
267 if ($subject > 1) { print ; }
268 elsif (/^\s+$/) { next ; }
269 elsif (/^Author:/) { print s/Author/From/ ; }
270 elsif (/^(From|Date)/) { print ; }
276 print "Subject: ", $_ ;
279 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
281 echo "$this" > "$dotest/last"
286 if test -n "$parse_patch" ; then
287 clean_abort
"Patch format $patch_format is not supported."
289 clean_abort
"Patch format detection failed."
296 dotest
="$GIT_DIR/rebase-apply"
297 sign
= utf8
=t keep
= keepcr
= skip
= interactive
= resolved
= rebasing
= abort
=
298 resolvemsg
= resume
= scissors
= no_inbody_headers
=
300 committer_date_is_author_date
=
302 allow_rerere_autoupdate
=
304 if test "$(git config --bool --get am.keepcr)" = true
321 utf8
=t
;; # this is now default
330 -r|
--resolved|
--continue)
337 rebasing
=t threeway
=t keep
=t scissors
=f no_inbody_headers
=t
;;
339 die
"-d option is no longer supported. Do not use."
342 shift; resolvemsg
=$1 ;;
343 --whitespace|
--directory)
344 git_apply_opt
="$git_apply_opt $(sq "$1=$2")"; shift ;;
346 git_apply_opt
="$git_apply_opt $(sq "$1$2")"; shift ;;
348 shift ; patch_format
="$1" ;;
349 --reject|
--ignore-whitespace|
--ignore-space-change)
350 git_apply_opt
="$git_apply_opt $1" ;;
351 --committer-date-is-author-date)
352 committer_date_is_author_date
=t
;;
355 --rerere-autoupdate|
--no-rerere-autoupdate)
356 allow_rerere_autoupdate
="$1" ;;
371 # If the dotest directory exists, but we have finished applying all the
372 # patches in them, clear it out.
373 if test -d "$dotest" &&
374 last
=$
(cat "$dotest/last") &&
375 next
=$
(cat "$dotest/next") &&
377 test "$next" -gt "$last"
384 case "$#,$skip$resolved$abort" in
386 # Explicit resume command and we do not have file, so
390 # No file input but without resume parameters; catch
391 # user error to feed us a patch from standard input
392 # when there is already $dotest. This is somewhat
393 # unreliable -- stdin could be /dev/null for example
394 # and the caller did not intend to feed us a patch but
395 # wanted to continue unattended.
402 die
"previous rebase directory $dotest still exists but mbox given."
405 case "$skip,$abort" in
407 die
"Please make up your mind. --skip or --abort?"
411 git read-tree
--reset -u HEAD HEAD
412 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
414 git update-ref ORIG_HEAD
$orig_head
417 if test -f "$dotest/rebasing"
419 exec git rebase
--abort
422 test -f "$dotest/dirtyindex" ||
{
423 git read-tree
--reset -u HEAD ORIG_HEAD
429 rm -f "$dotest/dirtyindex"
431 # Make sure we are not given --skip, --resolved, nor --abort
432 test "$skip$resolved$abort" = "" ||
433 die
"Resolve operation not in progress, we are not resuming."
436 mkdir
-p "$dotest" ||
exit
438 if test -n "$prefix" && test $# != 0
443 test -n "$first" && {
451 set "$@" "$prefix$arg" ;;
457 check_patch_format
"$@"
461 # -i can and must be given when resuming; everything
463 echo " $git_apply_opt" >"$dotest/apply-opt"
464 echo "$threeway" >"$dotest/threeway"
465 echo "$sign" >"$dotest/sign"
466 echo "$utf8" >"$dotest/utf8"
467 echo "$keep" >"$dotest/keep"
468 echo "$keepcr" >"$dotest/keepcr"
469 echo "$scissors" >"$dotest/scissors"
470 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
471 echo "$GIT_QUIET" >"$dotest/quiet"
472 echo 1 >"$dotest/next"
473 if test -n "$rebasing"
475 : >"$dotest/rebasing"
477 : >"$dotest/applying"
478 if test -n "$HAS_HEAD"
480 git update-ref ORIG_HEAD HEAD
482 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
491 files
=$
(git ls-files
) ;;
493 files
=$
(git diff-index
--ignore-submodules --cached \
494 --name-only HEAD
--) ;;
498 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
499 die
"Dirty index: cannot apply patches (dirty: $files)"
503 if test "$(cat "$dotest/utf8
")" = t
509 if test "$(cat "$dotest/keep
")" = t
513 case "$(cat "$dotest/keepcr
")" in
517 keepcr
=--no-keep-cr ;;
519 case "$(cat "$dotest/scissors
")" in
521 scissors
=--scissors ;;
523 scissors
=--no-scissors ;;
525 if test "$(cat "$dotest/no_inbody_headers
")" = t
527 no_inbody_headers
=--no-inbody-headers
531 if test "$(cat "$dotest/quiet
")" = t
535 if test "$(cat "$dotest/threeway
")" = t
539 git_apply_opt
=$
(cat "$dotest/apply-opt")
540 if test "$(cat "$dotest/sign
")" = t
542 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
544 s/^/Signed-off-by: /'
550 last
=`cat "$dotest/last"`
551 this
=`cat "$dotest/next"`
554 this
=`expr "$this" + 1`
558 if test "$this" -gt "$last"
565 while test "$this" -le "$last"
567 msgnum
=`printf "%0${prec}d" $this`
568 next
=`expr "$this" + 1`
569 test -f "$dotest/$msgnum" ||
{
575 # If we are not resuming, parse and extract the patch information
576 # into separate files:
577 # - info records the authorship and title
578 # - msg is the rest of commit log message
579 # - patch is the patch body.
581 # When we are resuming, these files are either already prepared
582 # by the user, or the user can tell us to do so by --resolved flag.
585 git mailinfo
$keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
586 <"$dotest/$msgnum" >"$dotest/info" ||
589 # skip pine's internal folder data
590 sane_grep
'^Author: Mail System Internal Data$' \
591 <"$dotest"/info
>/dev
/null
&&
594 test -s "$dotest/patch" ||
{
595 echo "Patch is empty. Was it split wrong?"
596 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
597 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
600 rm -f "$dotest/original-commit" "$dotest/author-script"
601 if test -f "$dotest/rebasing" &&
602 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
603 -e q
"$dotest/$msgnum") &&
604 test "$(git cat-file -t "$commit")" = commit
606 git cat-file commit
"$commit" |
607 sed -e '1,/^$/d' >"$dotest/msg-clean"
608 echo "$commit" > "$dotest/original-commit"
609 get_author_ident_from_commit
"$commit" > "$dotest/author-script"
612 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
616 git stripspace
> "$dotest/msg-clean"
621 if test -f "$dotest/author-script"
623 eval $
(cat "$dotest/author-script")
625 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
626 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
627 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
630 if test -z "$GIT_AUTHOR_EMAIL"
632 echo "Patch does not have a valid e-mail address."
636 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
640 if test '' != "$SIGNOFF"
643 sed -ne '/^Signed-off-by: /p' \
644 "$dotest/msg-clean" |
648 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
649 test '' = "$LAST_SIGNED_OFF_BY" && echo
656 if test -s "$dotest/msg-clean"
658 cat "$dotest/msg-clean"
660 if test '' != "$ADD_SIGNOFF"
664 } >"$dotest/final-commit"
667 case "$resolved$interactive" in
669 # This is used only for interactive view option.
670 git diff-index
--ignore-submodules -p --cached \
671 HEAD
-- >"$dotest/patch"
677 if test "$interactive" = t
680 die
"cannot be interactive without stdin connected to a terminal."
682 while test "$action" = again
684 echo "Commit Body is:"
685 echo "--------------------------"
686 cat "$dotest/final-commit"
687 echo "--------------------------"
688 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
692 [aA
]*) action
=yes interactive
= ;;
693 [nN
]*) action
=skip
;;
694 [eE
]*) git_editor
"$dotest/final-commit"
697 git_pager
"$dotest/patch" ;;
705 if test -f "$dotest/final-commit"
707 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
712 if test $action = skip
718 if test -x "$GIT_DIR"/hooks
/applypatch-msg
720 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
724 say
"Applying: $FIRSTLINE"
728 # When we are allowed to fall back to 3-way later, don't give
729 # false errors during the initial attempt.
731 if test "$threeway" = t
733 squelch
='>/dev/null 2>&1 '
735 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
739 # Resolved means the user did all the hard work, and
740 # we do not have to do any patch application. Just
741 # trust what the user has in the index file and the
744 git diff-index
--ignore-submodules --quiet --cached HEAD
-- && {
745 echo "No changes - did you forget to use 'git add'?"
746 echo "If there is nothing left to stage, chances are that something else"
747 echo "already introduced the same changes; you might want to skip this patch."
748 stop_here_user_resolve
$this
750 unmerged
=$
(git ls-files
-u)
751 if test -n "$unmerged"
753 echo "You still have unmerged paths in your index"
754 echo "did you forget to use 'git add'?"
755 stop_here_user_resolve
$this
762 if test $apply_status != 0 && test "$threeway" = t
766 # Applying the patch to an earlier tree and merging the
767 # result may have produced the same tree as ours.
768 git diff-index
--ignore-submodules --quiet --cached \
770 say No changes
-- Patch already applied.
774 # clear apply_status -- we have successfully merged.
778 if test $apply_status != 0
780 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
781 stop_here_user_resolve
$this
784 if test -x "$GIT_DIR"/hooks
/pre-applypatch
786 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
789 tree
=$
(git write-tree
) &&
791 if test -n "$ignore_date"
795 parent
=$
(git rev-parse
--verify -q HEAD
) ||
796 say
>&2 "applying to an empty history"
798 if test -n "$committer_date_is_author_date"
800 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
801 export GIT_COMMITTER_DATE
803 git commit-tree
$tree ${parent:+-p} $parent <"$dotest/final-commit"
805 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
808 if test -f "$dotest/original-commit"; then
809 echo "$(cat "$dotest/original-commit
") $commit" >> "$dotest/rewritten"
812 if test -x "$GIT_DIR"/hooks
/post-applypatch
814 "$GIT_DIR"/hooks
/post-applypatch
820 if test -s "$dotest"/rewritten
; then
821 git notes copy
--for-rewrite=rebase
< "$dotest"/rewritten
822 if test -x "$GIT_DIR"/hooks
/post-rewrite
; then
823 "$GIT_DIR"/hooks
/post-rewrite rebase
< "$dotest"/rewritten