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 git rev-parse --sq-quote "$@"
60 echo "$1" >"$dotest/next"
64 stop_here_user_resolve () {
65 if [ -n "$resolvemsg" ]; then
66 printf '%s\n' "$resolvemsg"
70 if test '' != "$interactive"
74 if test '' != "$threeway"
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\"."
86 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
87 "$dotest/patch" "$dotest/info"
88 echo "$next" >"$dotest/next"
94 echo "Cannot fall back to three-way merge."
99 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
101 rm -fr "$dotest"/patch-merge-*
102 mkdir "$dotest/patch-merge-tmp-dir"
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" \
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."
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"
115 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
116 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
118 cannot_fallback "Did you hand edit your patch?
119 It does not apply to blobs recorded in its index."
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
127 say Falling back to patching base and 3-way merge...
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.
135 eval GITHEAD_$his_tree='"$FIRSTLINE"'
136 export GITHEAD_$his_tree
137 if test -n "$GIT_QUIET"
139 export GIT_MERGE_VERBOSITY=0
141 git-merge-recursive $orig_tree -- HEAD $his_tree || {
142 git rerere $allow_rerere_autoupdate
143 echo Failed to merge in the changes.
146 unset GITHEAD_$his_tree
150 test $# = 0 || echo >&2 "$@"
157 check_patch_format () {
158 # early return if patch_format was set from the command line
159 if test -n "$patch_format"
164 # we default to mbox format if input is from stdin and for
166 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
172 # otherwise, check the first few lines of the first patch to try
173 # to detect its format
179 "From "* | "From: "*)
182 '# This series applies on GIT commit'*)
183 patch_format=stgit-series
185 "# HG changeset patch")
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
193 ,"From: "* | ,"Author: "* | ,"Date: "*)
201 if test -z "$patch_format" &&
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...
212 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
213 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
216 } < "$1" || clean_abort
220 case "$patch_format" in
222 if test -n "$rebasing" || test t = "$keepcr"
228 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
234 clean_abort "Only one StGIT patch series can be applied at once"
236 series_dir=`dirname "$1"`
243 set "$@" "$series_dir/$filename"
245 # remove the safety x
247 # remove the arg coming from the first-line comment
249 } < "$series_file" || clean_abort
250 # set the patch format appropriately
252 # now handle the actual StGIT patches
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 ; }
275 print "Subject: ", $_ ;
278 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
280 echo "$this" > "$dotest/last"
285 if test -n "$parse_patch" ; then
286 clean_abort "Patch format $patch_format is not supported."
288 clean_abort "Patch format detection failed."
295 dotest="$GIT_DIR/rebase-apply"
296 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
297 resolvemsg= resume= scissors= no_inbody_headers=
299 committer_date_is_author_date=
301 allow_rerere_autoupdate=
303 if test "$(git config --bool --get am.keepcr)" = true
320 utf8=t ;; # this is now default
329 -r|--resolved|--continue)
336 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
338 die "-d option is no longer supported. Do not use."
341 shift; resolvemsg=$1 ;;
342 --whitespace|--directory)
343 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
345 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
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 ;;
354 --rerere-autoupdate|--no-rerere-autoupdate)
355 allow_rerere_autoupdate="$1" ;;
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") &&
376 test "$next" -gt "$last"
383 case "$#,$skip$resolved$abort" in
385 # Explicit resume command and we do not have file, so
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.
401 die "previous rebase directory $dotest still exists but mbox given."
404 case "$skip,$abort" in
406 die "Please make up your mind. --skip or --abort?"
410 git read-tree --reset -u HEAD HEAD
411 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
413 git update-ref ORIG_HEAD $orig_head
416 if test -f "$dotest/rebasing"
418 exec git rebase --abort
421 test -f "$dotest/dirtyindex" || {
422 git read-tree --reset -u HEAD ORIG_HEAD
428 rm -f "$dotest/dirtyindex"
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."
435 mkdir -p "$dotest" || exit
437 if test -n "$prefix" && test $# != 0
442 test -n "$first" && {
450 set "$@" "$prefix$arg" ;;
456 check_patch_format "$@"
460 # -i can and must be given when resuming; everything
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"
474 : >"$dotest/rebasing"
476 : >"$dotest/applying"
477 if test -n "$HAS_HEAD"
479 git update-ref ORIG_HEAD HEAD
481 git update-ref -d ORIG_HEAD >/dev/null 2>&1
490 files=$(git ls-files) ;;
492 files=$(git diff-index --cached --name-only HEAD --) ;;
496 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
497 die "Dirty index: cannot apply patches (dirty: $files)"
501 if test "$(cat "$dotest/utf8")" = t
507 if test "$(cat "$dotest/keep")" = t
511 case "$(cat "$dotest/keepcr")" in
515 keepcr=--no-keep-cr ;;
517 case "$(cat "$dotest/scissors")" in
519 scissors=--scissors ;;
521 scissors=--no-scissors ;;
523 if test "$(cat "$dotest/no_inbody_headers")" = t
525 no_inbody_headers=--no-inbody-headers
529 if test "$(cat "$dotest/quiet")" = t
533 if test "$(cat "$dotest/threeway")" = t
537 git_apply_opt=$(cat "$dotest/apply-opt")
538 if test "$(cat "$dotest/sign")" = t
540 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
542 s/^/Signed-off-by: /'
548 last=`cat "$dotest/last"`
549 this=`cat "$dotest/next"`
552 this=`expr "$this" + 1`
556 if test "$this" -gt "$last"
563 while test "$this" -le "$last"
565 msgnum=`printf "%0${prec}d" $this`
566 next=`expr "$this" + 1`
567 test -f "$dotest/$msgnum" || {
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.
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.
583 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
584 <"$dotest/$msgnum" >"$dotest/info" ||
587 # skip pine's internal folder data
588 sane_grep '^Author: Mail System Internal Data$' \
589 <"$dotest"/info >/dev/null &&
592 test -s "$dotest/patch" || {
593 echo "Patch is empty. Was it split wrong?"
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
602 git cat-file commit "$commit" |
603 sed -e '1,/^$/d' >"$dotest/msg-clean"
604 echo "$commit" > "$dotest/original-commit"
607 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
611 git stripspace > "$dotest/msg-clean"
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")"
620 if test -z "$GIT_AUTHOR_EMAIL"
622 echo "Patch does not have a valid e-mail address."
626 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
630 if test '' != "$SIGNOFF"
633 sed -ne '/^Signed-off-by: /p' \
634 "$dotest/msg-clean" |
638 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
639 test '' = "$LAST_SIGNED_OFF_BY" && echo
646 if test -s "$dotest/msg-clean"
648 cat "$dotest/msg-clean"
650 if test '' != "$ADD_SIGNOFF"
654 } >"$dotest/final-commit"
657 case "$resolved$interactive" in
659 # This is used only for interactive view option.
660 git diff-index -p --cached HEAD -- >"$dotest/patch"
666 if test "$interactive" = t
669 die "cannot be interactive without stdin connected to a terminal."
671 while test "$action" = again
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 "
681 [aA]*) action=yes interactive= ;;
682 [nN]*) action=skip ;;
683 [eE]*) git_editor "$dotest/final-commit"
686 git_pager "$dotest/patch" ;;
693 FIRSTLINE=$(sed 1q "$dotest/final-commit")
695 if test $action = skip
701 if test -x "$GIT_DIR"/hooks/applypatch-msg
703 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
707 say "Applying: $FIRSTLINE"
711 # When we are allowed to fall back to 3-way later, don't give
712 # false errors during the initial attempt.
714 if test "$threeway" = t
716 squelch='>/dev/null 2>&1 '
718 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
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
727 git diff-index --quiet --cached HEAD -- && {
728 echo "No changes - did you forget to use 'git add'?"
729 stop_here_user_resolve $this
731 unmerged=$(git ls-files -u)
732 if test -n "$unmerged"
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
743 if test $apply_status != 0 && test "$threeway" = t
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.
754 # clear apply_status -- we have successfully merged.
758 if test $apply_status != 0
760 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
761 stop_here_user_resolve $this
764 if test -x "$GIT_DIR"/hooks/pre-applypatch
766 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
769 tree=$(git write-tree) &&
771 if test -n "$ignore_date"
775 parent=$(git rev-parse --verify -q HEAD) ||
776 say >&2 "applying to an empty history"
778 if test -n "$committer_date_is_author_date"
780 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
781 export GIT_COMMITTER_DATE
783 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
785 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
788 if test -f "$dotest/original-commit"; then
789 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
792 if test -x "$GIT_DIR"/hooks/post-applypatch
794 "$GIT_DIR"/hooks/post-applypatch
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