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 c,scissors strip everything before a scissors line
19 whitespace= pass it through git-apply
20 ignore-space-change pass it through git-apply
21 ignore-whitespace pass it through git-apply
22 directory= pass it through git-apply
23 C= pass it through git-apply
24 p= pass it through git-apply
25 patch-format= format the patch(es) are in
26 reject pass it through git-apply
27 resolvemsg= override error message when patch failure occurs
28 r,resolved to be used after a patch failure
29 skip skip the current patch
30 abort restore the original branch and abort the patching operation.
31 committer-date-is-author-date lie about committer date
32 ignore-date use current timestamp for author date
33 rebasing* (internal use for git-rebase)"
36 prefix=$(git rev-parse --show-prefix)
41 git var GIT_COMMITTER_IDENT >/dev/null ||
42 die "You need to set your committer info first"
44 if git rev-parse --verify -q HEAD >/dev/null
52 git rev-parse --sq-quote "$@"
56 echo "$1" >"$dotest/next"
60 stop_here_user_resolve () {
61 if [ -n "$resolvemsg" ]; then
62 printf '%s\n' "$resolvemsg"
66 if test '' != "$interactive"
70 if test '' != "$threeway"
74 echo "When you have resolved this problem run \"$cmdline --resolved\"."
75 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
76 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
82 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
83 "$dotest/patch" "$dotest/info"
84 echo "$next" >"$dotest/next"
90 echo "Cannot fall back to three-way merge."
95 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
97 rm -fr "$dotest"/patch-merge-*
98 mkdir "$dotest/patch-merge-tmp-dir"
100 # First see if the patch records the index info that we can use.
101 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
103 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
104 git write-tree >"$dotest/patch-merge-base+" ||
105 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
107 say Using index info to reconstruct a base tree...
108 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
109 git apply --cached <"$dotest/patch"
111 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
112 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
114 cannot_fallback "Did you hand edit your patch?
115 It does not apply to blobs recorded in its index."
118 test -f "$dotest/patch-merge-index" &&
119 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
120 orig_tree=$(cat "$dotest/patch-merge-base") &&
121 rm -fr "$dotest"/patch-merge-* || exit 1
123 say Falling back to patching base and 3-way merge...
125 # This is not so wrong. Depending on which base we picked,
126 # orig_tree may be wildly different from ours, but his_tree
127 # has the same set of wildly different changes in parts the
128 # patch did not touch, so recursive ends up canceling them,
129 # saying that we reverted all those changes.
131 eval GITHEAD_$his_tree='"$FIRSTLINE"'
132 export GITHEAD_$his_tree
133 if test -n "$GIT_QUIET"
135 export GIT_MERGE_VERBOSITY=0
137 git-merge-recursive $orig_tree -- HEAD $his_tree || {
139 echo Failed to merge in the changes.
142 unset GITHEAD_$his_tree
146 test $# = 0 || echo >&2 "$@"
153 check_patch_format () {
154 # early return if patch_format was set from the command line
155 if test -n "$patch_format"
160 # we default to mbox format if input is from stdin and for
162 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
168 # otherwise, check the first few lines of the first patch to try
169 # to detect its format
175 "From "* | "From: "*)
178 '# This series applies on GIT commit'*)
179 patch_format=stgit-series
181 "# HG changeset patch")
185 # if the second line is empty and the third is
186 # a From, Author or Date entry, this is very
187 # likely an StGIT patch
189 ,"From: "* | ,"Author: "* | ,"Date: "*)
197 if test -z "$patch_format" &&
202 # This begins with three non-empty lines. Is this a
203 # piece of e-mail a-la RFC2822? Grab all the headers,
204 # discarding the indented remainder of folded lines,
205 # and see if it looks like that they all begin with the
206 # header field names...
207 sed -n -e '/^$/q' -e '/^[ ]/d' -e p "$1" |
208 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
211 } < "$1" || clean_abort
215 case "$patch_format" in
223 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
229 clean_abort "Only one StGIT patch series can be applied at once"
231 series_dir=`dirname "$1"`
238 set "$@" "$series_dir/$filename"
240 # remove the safety x
242 # remove the arg coming from the first-line comment
244 } < "$series_file" || clean_abort
245 # set the patch format appropriately
247 # now handle the actual StGIT patches
254 this=`expr "$this" + 1`
255 msgnum=`printf "%0${prec}d" $this`
256 # Perl version of StGIT parse_patch. The first nonemptyline
257 # not starting with Author, From or Date is the
258 # subject, and the body starts with the next nonempty
259 # line not starting with Author, From or Date
260 perl -ne 'BEGIN { $subject = 0 }
261 if ($subject > 1) { print ; }
262 elsif (/^\s+$/) { next ; }
263 elsif (/^Author:/) { print s/Author/From/ ; }
264 elsif (/^(From|Date)/) { print ; }
270 print "Subject: ", $_ ;
273 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
275 echo "$this" > "$dotest/last"
280 if test -n "$parse_patch" ; then
281 clean_abort "Patch format $patch_format is not supported."
283 clean_abort "Patch format detection failed."
290 dotest="$GIT_DIR/rebase-apply"
291 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
292 resolvemsg= resume= scissors= no_inbody_headers=
294 committer_date_is_author_date=
309 utf8=t ;; # this is now default
325 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
327 die "-d option is no longer supported. Do not use."
330 shift; resolvemsg=$1 ;;
331 --whitespace|--directory)
332 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
334 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
336 shift ; patch_format="$1" ;;
337 --reject|--ignore-whitespace|--ignore-space-change)
338 git_apply_opt="$git_apply_opt $1" ;;
339 --committer-date-is-author-date)
340 committer_date_is_author_date=t ;;
353 # If the dotest directory exists, but we have finished applying all the
354 # patches in them, clear it out.
355 if test -d "$dotest" &&
356 last=$(cat "$dotest/last") &&
357 next=$(cat "$dotest/next") &&
359 test "$next" -gt "$last"
366 case "$#,$skip$resolved$abort" in
368 # Explicit resume command and we do not have file, so
372 # No file input but without resume parameters; catch
373 # user error to feed us a patch from standard input
374 # when there is already $dotest. This is somewhat
375 # unreliable -- stdin could be /dev/null for example
376 # and the caller did not intend to feed us a patch but
377 # wanted to continue unattended.
384 die "previous rebase directory $dotest still exists but mbox given."
387 case "$skip,$abort" in
389 die "Please make up your mind. --skip or --abort?"
393 git read-tree --reset -u HEAD HEAD
394 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
396 git update-ref ORIG_HEAD $orig_head
399 if test -f "$dotest/rebasing"
401 exec git rebase --abort
404 test -f "$dotest/dirtyindex" || {
405 git read-tree --reset -u HEAD ORIG_HEAD
411 rm -f "$dotest/dirtyindex"
413 # Make sure we are not given --skip, --resolved, nor --abort
414 test "$skip$resolved$abort" = "" ||
415 die "Resolve operation not in progress, we are not resuming."
418 mkdir -p "$dotest" || exit
420 if test -n "$prefix" && test $# != 0
425 test -n "$first" && {
433 set "$@" "$prefix$arg" ;;
439 check_patch_format "$@"
443 # -i can and must be given when resuming; everything
445 echo " $git_apply_opt" >"$dotest/apply-opt"
446 echo "$threeway" >"$dotest/threeway"
447 echo "$sign" >"$dotest/sign"
448 echo "$utf8" >"$dotest/utf8"
449 echo "$keep" >"$dotest/keep"
450 echo "$scissors" >"$dotest/scissors"
451 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
452 echo "$GIT_QUIET" >"$dotest/quiet"
453 echo 1 >"$dotest/next"
454 if test -n "$rebasing"
456 : >"$dotest/rebasing"
458 : >"$dotest/applying"
459 if test -n "$HAS_HEAD"
461 git update-ref ORIG_HEAD HEAD
463 git update-ref -d ORIG_HEAD >/dev/null 2>&1
472 files=$(git ls-files) ;;
474 files=$(git diff-index --cached --name-only HEAD --) ;;
478 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
479 die "Dirty index: cannot apply patches (dirty: $files)"
483 if test "$(cat "$dotest/utf8")" = t
489 if test "$(cat "$dotest/keep")" = t
493 case "$(cat "$dotest/scissors")" in
495 scissors=--scissors ;;
497 scissors=--no-scissors ;;
499 if test "$(cat "$dotest/no_inbody_headers")" = t
501 no_inbody_headers=--no-inbody-headers
505 if test "$(cat "$dotest/quiet")" = t
509 if test "$(cat "$dotest/threeway")" = t
513 git_apply_opt=$(cat "$dotest/apply-opt")
514 if test "$(cat "$dotest/sign")" = t
516 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
518 s/^/Signed-off-by: /'
524 last=`cat "$dotest/last"`
525 this=`cat "$dotest/next"`
528 this=`expr "$this" + 1`
532 if test "$this" -gt "$last"
539 while test "$this" -le "$last"
541 msgnum=`printf "%0${prec}d" $this`
542 next=`expr "$this" + 1`
543 test -f "$dotest/$msgnum" || {
549 # If we are not resuming, parse and extract the patch information
550 # into separate files:
551 # - info records the authorship and title
552 # - msg is the rest of commit log message
553 # - patch is the patch body.
555 # When we are resuming, these files are either already prepared
556 # by the user, or the user can tell us to do so by --resolved flag.
559 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
560 <"$dotest/$msgnum" >"$dotest/info" ||
563 # skip pine's internal folder data
564 sane_grep '^Author: Mail System Internal Data$' \
565 <"$dotest"/info >/dev/null &&
568 test -s "$dotest/patch" || {
569 echo "Patch is empty. Was it split wrong?"
572 if test -f "$dotest/rebasing" &&
573 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
574 -e q "$dotest/$msgnum") &&
575 test "$(git cat-file -t "$commit")" = commit
577 git cat-file commit "$commit" |
578 sed -e '1,/^$/d' >"$dotest/msg-clean"
581 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
585 git stripspace > "$dotest/msg-clean"
590 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
591 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
592 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
594 if test -z "$GIT_AUTHOR_EMAIL"
596 echo "Patch does not have a valid e-mail address."
600 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
604 if test '' != "$SIGNOFF"
607 sed -ne '/^Signed-off-by: /p' \
608 "$dotest/msg-clean" |
612 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
613 test '' = "$LAST_SIGNED_OFF_BY" && echo
620 if test -s "$dotest/msg-clean"
622 cat "$dotest/msg-clean"
624 if test '' != "$ADD_SIGNOFF"
628 } >"$dotest/final-commit"
631 case "$resolved$interactive" in
633 # This is used only for interactive view option.
634 git diff-index -p --cached HEAD -- >"$dotest/patch"
640 if test "$interactive" = t
643 die "cannot be interactive without stdin connected to a terminal."
645 while test "$action" = again
647 echo "Commit Body is:"
648 echo "--------------------------"
649 cat "$dotest/final-commit"
650 echo "--------------------------"
651 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
655 [aA]*) action=yes interactive= ;;
656 [nN]*) action=skip ;;
657 [eE]*) git_editor "$dotest/final-commit"
660 : ${GIT_PAGER=$(git var GIT_PAGER)}
663 $GIT_PAGER "$dotest/patch" ;;
670 FIRSTLINE=$(sed 1q "$dotest/final-commit")
672 if test $action = skip
678 if test -x "$GIT_DIR"/hooks/applypatch-msg
680 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
684 say "Applying: $FIRSTLINE"
688 # When we are allowed to fall back to 3-way later, don't give
689 # false errors during the initial attempt.
691 if test "$threeway" = t
693 squelch='>/dev/null 2>&1 '
695 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
699 # Resolved means the user did all the hard work, and
700 # we do not have to do any patch application. Just
701 # trust what the user has in the index file and the
704 git diff-index --quiet --cached HEAD -- && {
705 echo "No changes - did you forget to use 'git add'?"
706 stop_here_user_resolve $this
708 unmerged=$(git ls-files -u)
709 if test -n "$unmerged"
711 echo "You still have unmerged paths in your index"
712 echo "did you forget to use 'git add'?"
713 stop_here_user_resolve $this
720 if test $apply_status = 1 && test "$threeway" = t
724 # Applying the patch to an earlier tree and merging the
725 # result may have produced the same tree as ours.
726 git diff-index --quiet --cached HEAD -- && {
727 say No changes -- Patch already applied.
731 # clear apply_status -- we have successfully merged.
735 if test $apply_status != 0
737 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
738 stop_here_user_resolve $this
741 if test -x "$GIT_DIR"/hooks/pre-applypatch
743 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
746 tree=$(git write-tree) &&
748 if test -n "$ignore_date"
752 parent=$(git rev-parse --verify -q HEAD) ||
753 say >&2 "applying to an empty history"
755 if test -n "$committer_date_is_author_date"
757 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
758 export GIT_COMMITTER_DATE
760 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
762 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
765 if test -x "$GIT_DIR"/hooks/post-applypatch
767 "$GIT_DIR"/hooks/post-applypatch