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 rerere-autoupdate update the index with reused conflict resolution if possible
34 rebasing* (internal use for git-rebase)"
37 prefix
=$
(git rev-parse
--show-prefix)
42 git var GIT_COMMITTER_IDENT
>/dev
/null ||
43 die
"You need to set your committer info first"
45 if git rev-parse
--verify -q HEAD
>/dev
/null
53 git rev-parse
--sq-quote "$@"
57 echo "$1" >"$dotest/next"
61 stop_here_user_resolve
() {
62 if [ -n "$resolvemsg" ]; then
63 printf '%s\n' "$resolvemsg"
67 if test '' != "$interactive"
71 if test '' != "$threeway"
75 echo "When you have resolved this problem run \"$cmdline --resolved\"."
76 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
77 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
83 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
84 "$dotest/patch" "$dotest/info"
85 echo "$next" >"$dotest/next"
91 echo "Cannot fall back to three-way merge."
96 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
98 rm -fr "$dotest"/patch-merge-
*
99 mkdir
"$dotest/patch-merge-tmp-dir"
101 # First see if the patch records the index info that we can use.
102 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
104 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
105 git write-tree
>"$dotest/patch-merge-base+" ||
106 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
108 say Using index info to reconstruct a base tree...
109 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
110 git apply
--cached <"$dotest/patch"
112 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
113 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
115 cannot_fallback
"Did you hand edit your patch?
116 It does not apply to blobs recorded in its index."
119 test -f "$dotest/patch-merge-index" &&
120 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
121 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
122 rm -fr "$dotest"/patch-merge-
* ||
exit 1
124 say Falling back to patching base and
3-way merge...
126 # This is not so wrong. Depending on which base we picked,
127 # orig_tree may be wildly different from ours, but his_tree
128 # has the same set of wildly different changes in parts the
129 # patch did not touch, so recursive ends up canceling them,
130 # saying that we reverted all those changes.
132 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
133 export GITHEAD_
$his_tree
134 if test -n "$GIT_QUIET"
136 export GIT_MERGE_VERBOSITY
=0
138 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
139 git rerere
$allow_rerere_autoupdate
140 echo Failed to merge
in the changes.
143 unset GITHEAD_
$his_tree
147 test $# = 0 ||
echo >&2 "$@"
154 check_patch_format
() {
155 # early return if patch_format was set from the command line
156 if test -n "$patch_format"
161 # we default to mbox format if input is from stdin and for
163 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
169 # otherwise, check the first few lines of the first patch to try
170 # to detect its format
176 "From "* |
"From: "*)
179 '# This series applies on GIT commit'*)
180 patch_format
=stgit-series
182 "# HG changeset patch")
186 # if the second line is empty and the third is
187 # a From, Author or Date entry, this is very
188 # likely an StGIT patch
190 ,"From: "* |
,"Author: "* |
,"Date: "*)
198 if test -z "$patch_format" &&
203 # This begins with three non-empty lines. Is this a
204 # piece of e-mail a-la RFC2822? Grab all the headers,
205 # discarding the indented remainder of folded lines,
206 # and see if it looks like that they all begin with the
207 # header field names...
208 sed -n -e '/^$/q' -e '/^[ ]/d' -e p
"$1" |
209 sane_egrep
-v '^[!-9;-~]+:' >/dev
/null ||
212 } < "$1" || clean_abort
216 case "$patch_format" in
224 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
230 clean_abort
"Only one StGIT patch series can be applied at once"
232 series_dir
=`dirname "$1"`
239 set "$@" "$series_dir/$filename"
241 # remove the safety x
243 # remove the arg coming from the first-line comment
245 } < "$series_file" || clean_abort
246 # set the patch format appropriately
248 # now handle the actual StGIT patches
255 this
=`expr "$this" + 1`
256 msgnum
=`printf "%0${prec}d" $this`
257 # Perl version of StGIT parse_patch. The first nonemptyline
258 # not starting with Author, From or Date is the
259 # subject, and the body starts with the next nonempty
260 # line not starting with Author, From or Date
261 perl
-ne 'BEGIN { $subject = 0 }
262 if ($subject > 1) { print ; }
263 elsif (/^\s+$/) { next ; }
264 elsif (/^Author:/) { print s/Author/From/ ; }
265 elsif (/^(From|Date)/) { print ; }
271 print "Subject: ", $_ ;
274 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
276 echo "$this" > "$dotest/last"
281 if test -n "$parse_patch" ; then
282 clean_abort
"Patch format $patch_format is not supported."
284 clean_abort
"Patch format detection failed."
291 dotest
="$GIT_DIR/rebase-apply"
292 sign
= utf8
=t keep
= skip
= interactive
= resolved
= rebasing
= abort
=
293 resolvemsg
= resume
= scissors
= no_inbody_headers
=
295 committer_date_is_author_date
=
297 allow_rerere_autoupdate
=
311 utf8
=t
;; # this is now default
327 rebasing
=t threeway
=t keep
=t scissors
=f no_inbody_headers
=t
;;
329 die
"-d option is no longer supported. Do not use."
332 shift; resolvemsg
=$1 ;;
333 --whitespace|
--directory)
334 git_apply_opt
="$git_apply_opt $(sq "$1=$2")"; shift ;;
336 git_apply_opt
="$git_apply_opt $(sq "$1$2")"; shift ;;
338 shift ; patch_format
="$1" ;;
339 --reject|
--ignore-whitespace|
--ignore-space-change)
340 git_apply_opt
="$git_apply_opt $1" ;;
341 --committer-date-is-author-date)
342 committer_date_is_author_date
=t
;;
345 --rerere-autoupdate|
--no-rerere-autoupdate)
346 allow_rerere_autoupdate
="$1" ;;
357 # If the dotest directory exists, but we have finished applying all the
358 # patches in them, clear it out.
359 if test -d "$dotest" &&
360 last
=$
(cat "$dotest/last") &&
361 next
=$
(cat "$dotest/next") &&
363 test "$next" -gt "$last"
370 case "$#,$skip$resolved$abort" in
372 # Explicit resume command and we do not have file, so
376 # No file input but without resume parameters; catch
377 # user error to feed us a patch from standard input
378 # when there is already $dotest. This is somewhat
379 # unreliable -- stdin could be /dev/null for example
380 # and the caller did not intend to feed us a patch but
381 # wanted to continue unattended.
388 die
"previous rebase directory $dotest still exists but mbox given."
391 case "$skip,$abort" in
393 die
"Please make up your mind. --skip or --abort?"
397 git read-tree
--reset -u HEAD HEAD
398 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
400 git update-ref ORIG_HEAD
$orig_head
403 if test -f "$dotest/rebasing"
405 exec git rebase
--abort
408 test -f "$dotest/dirtyindex" ||
{
409 git read-tree
--reset -u HEAD ORIG_HEAD
415 rm -f "$dotest/dirtyindex"
417 # Make sure we are not given --skip, --resolved, nor --abort
418 test "$skip$resolved$abort" = "" ||
419 die
"Resolve operation not in progress, we are not resuming."
422 mkdir
-p "$dotest" ||
exit
424 if test -n "$prefix" && test $# != 0
429 test -n "$first" && {
437 set "$@" "$prefix$arg" ;;
443 check_patch_format
"$@"
447 # -i can and must be given when resuming; everything
449 echo " $git_apply_opt" >"$dotest/apply-opt"
450 echo "$threeway" >"$dotest/threeway"
451 echo "$sign" >"$dotest/sign"
452 echo "$utf8" >"$dotest/utf8"
453 echo "$keep" >"$dotest/keep"
454 echo "$scissors" >"$dotest/scissors"
455 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
456 echo "$GIT_QUIET" >"$dotest/quiet"
457 echo 1 >"$dotest/next"
458 if test -n "$rebasing"
460 : >"$dotest/rebasing"
462 : >"$dotest/applying"
463 if test -n "$HAS_HEAD"
465 git update-ref ORIG_HEAD HEAD
467 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
476 files
=$
(git ls-files
) ;;
478 files
=$
(git diff-index
--cached --name-only HEAD
--) ;;
482 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
483 die
"Dirty index: cannot apply patches (dirty: $files)"
487 if test "$(cat "$dotest/utf8
")" = t
493 if test "$(cat "$dotest/keep
")" = t
497 case "$(cat "$dotest/scissors
")" in
499 scissors
=--scissors ;;
501 scissors
=--no-scissors ;;
503 if test "$(cat "$dotest/no_inbody_headers
")" = t
505 no_inbody_headers
=--no-inbody-headers
509 if test "$(cat "$dotest/quiet
")" = t
513 if test "$(cat "$dotest/threeway
")" = t
517 git_apply_opt
=$
(cat "$dotest/apply-opt")
518 if test "$(cat "$dotest/sign
")" = t
520 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
522 s/^/Signed-off-by: /'
528 last
=`cat "$dotest/last"`
529 this
=`cat "$dotest/next"`
532 this
=`expr "$this" + 1`
536 if test "$this" -gt "$last"
543 while test "$this" -le "$last"
545 msgnum
=`printf "%0${prec}d" $this`
546 next
=`expr "$this" + 1`
547 test -f "$dotest/$msgnum" ||
{
553 # If we are not resuming, parse and extract the patch information
554 # into separate files:
555 # - info records the authorship and title
556 # - msg is the rest of commit log message
557 # - patch is the patch body.
559 # When we are resuming, these files are either already prepared
560 # by the user, or the user can tell us to do so by --resolved flag.
563 git mailinfo
$keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
564 <"$dotest/$msgnum" >"$dotest/info" ||
567 # skip pine's internal folder data
568 sane_grep
'^Author: Mail System Internal Data$' \
569 <"$dotest"/info
>/dev
/null
&&
572 test -s "$dotest/patch" ||
{
573 echo "Patch is empty. Was it split wrong?"
576 if test -f "$dotest/rebasing" &&
577 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
578 -e q
"$dotest/$msgnum") &&
579 test "$(git cat-file -t "$commit")" = commit
581 git cat-file commit
"$commit" |
582 sed -e '1,/^$/d' >"$dotest/msg-clean"
585 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
589 git stripspace
> "$dotest/msg-clean"
594 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
595 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
596 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
598 if test -z "$GIT_AUTHOR_EMAIL"
600 echo "Patch does not have a valid e-mail address."
604 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
608 if test '' != "$SIGNOFF"
611 sed -ne '/^Signed-off-by: /p' \
612 "$dotest/msg-clean" |
616 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
617 test '' = "$LAST_SIGNED_OFF_BY" && echo
624 if test -s "$dotest/msg-clean"
626 cat "$dotest/msg-clean"
628 if test '' != "$ADD_SIGNOFF"
632 } >"$dotest/final-commit"
635 case "$resolved$interactive" in
637 # This is used only for interactive view option.
638 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
644 if test "$interactive" = t
647 die
"cannot be interactive without stdin connected to a terminal."
649 while test "$action" = again
651 echo "Commit Body is:"
652 echo "--------------------------"
653 cat "$dotest/final-commit"
654 echo "--------------------------"
655 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
659 [aA
]*) action
=yes interactive
= ;;
660 [nN
]*) action
=skip
;;
661 [eE
]*) git_editor
"$dotest/final-commit"
664 : ${GIT_PAGER=$(git var GIT_PAGER)}
667 $GIT_PAGER "$dotest/patch" ;;
674 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
676 if test $action = skip
682 if test -x "$GIT_DIR"/hooks
/applypatch-msg
684 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
688 say
"Applying: $FIRSTLINE"
692 # When we are allowed to fall back to 3-way later, don't give
693 # false errors during the initial attempt.
695 if test "$threeway" = t
697 squelch
='>/dev/null 2>&1 '
699 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
703 # Resolved means the user did all the hard work, and
704 # we do not have to do any patch application. Just
705 # trust what the user has in the index file and the
708 git diff-index
--quiet --cached HEAD
-- && {
709 echo "No changes - did you forget to use 'git add'?"
710 stop_here_user_resolve
$this
712 unmerged
=$
(git ls-files
-u)
713 if test -n "$unmerged"
715 echo "You still have unmerged paths in your index"
716 echo "did you forget to use 'git add'?"
717 stop_here_user_resolve
$this
724 if test $apply_status = 1 && test "$threeway" = t
728 # Applying the patch to an earlier tree and merging the
729 # result may have produced the same tree as ours.
730 git diff-index
--quiet --cached HEAD
-- && {
731 say No changes
-- Patch already applied.
735 # clear apply_status -- we have successfully merged.
739 if test $apply_status != 0
741 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
742 stop_here_user_resolve
$this
745 if test -x "$GIT_DIR"/hooks
/pre-applypatch
747 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
750 tree
=$
(git write-tree
) &&
752 if test -n "$ignore_date"
756 parent
=$
(git rev-parse
--verify -q HEAD
) ||
757 say
>&2 "applying to an empty history"
759 if test -n "$committer_date_is_author_date"
761 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
762 export GIT_COMMITTER_DATE
764 git commit-tree
$tree ${parent:+-p} $parent <"$dotest/final-commit"
766 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
769 if test -x "$GIT_DIR"/hooks
/post-applypatch
771 "$GIT_DIR"/hooks
/post-applypatch