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 continue continue applying patches after resolving a conflict
29 r,resolved synonyms for --continue
30 skip skip the current patch
31 abort restore the original branch and abort the patching operation.
32 committer-date-is-author-date lie about committer date
33 ignore-date use current timestamp for author date
34 rerere-autoupdate update the index with reused conflict resolution if possible
35 rebasing* (internal use for git-rebase)"
38 prefix
=$
(git rev-parse
--show-prefix)
43 git var GIT_COMMITTER_IDENT
>/dev
/null ||
44 die
"You need to set your committer info first"
46 if git rev-parse
--verify -q HEAD
>/dev
/null
54 git rev-parse
--sq-quote "$@"
58 echo "$1" >"$dotest/next"
62 stop_here_user_resolve
() {
63 if [ -n "$resolvemsg" ]; then
64 printf '%s\n' "$resolvemsg"
68 if test '' != "$interactive"
72 if test '' != "$threeway"
76 echo "When you have resolved this problem run \"$cmdline --resolved\"."
77 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
78 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
84 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
85 "$dotest/patch" "$dotest/info"
86 echo "$next" >"$dotest/next"
92 echo "Cannot fall back to three-way merge."
97 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
99 rm -fr "$dotest"/patch-merge-
*
100 mkdir
"$dotest/patch-merge-tmp-dir"
102 # First see if the patch records the index info that we can use.
103 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
105 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
106 git write-tree
>"$dotest/patch-merge-base+" ||
107 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
109 say Using index info to reconstruct a base tree...
110 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
111 git apply
--cached <"$dotest/patch"
113 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
114 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
116 cannot_fallback
"Did you hand edit your patch?
117 It does not apply to blobs recorded in its index."
120 test -f "$dotest/patch-merge-index" &&
121 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
122 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
123 rm -fr "$dotest"/patch-merge-
* ||
exit 1
125 say Falling back to patching base and
3-way merge...
127 # This is not so wrong. Depending on which base we picked,
128 # orig_tree may be wildly different from ours, but his_tree
129 # has the same set of wildly different changes in parts the
130 # patch did not touch, so recursive ends up canceling them,
131 # saying that we reverted all those changes.
133 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
134 export GITHEAD_
$his_tree
135 if test -n "$GIT_QUIET"
137 export GIT_MERGE_VERBOSITY
=0
139 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
140 git rerere
$allow_rerere_autoupdate
141 echo Failed to merge
in the changes.
144 unset GITHEAD_
$his_tree
148 test $# = 0 ||
echo >&2 "$@"
155 check_patch_format
() {
156 # early return if patch_format was set from the command line
157 if test -n "$patch_format"
162 # we default to mbox format if input is from stdin and for
164 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
170 # otherwise, check the first few lines of the first patch to try
171 # to detect its format
177 "From "* |
"From: "*)
180 '# This series applies on GIT commit'*)
181 patch_format
=stgit-series
183 "# HG changeset patch")
187 # if the second line is empty and the third is
188 # a From, Author or Date entry, this is very
189 # likely an StGIT patch
191 ,"From: "* |
,"Author: "* |
,"Date: "*)
199 if test -z "$patch_format" &&
204 # This begins with three non-empty lines. Is this a
205 # piece of e-mail a-la RFC2822? Grab all the headers,
206 # discarding the indented remainder of folded lines,
207 # and see if it looks like that they all begin with the
208 # header field names...
210 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
211 sane_egrep
-v '^[!-9;-~]+:' >/dev
/null ||
214 } < "$1" || clean_abort
218 case "$patch_format" in
226 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
232 clean_abort
"Only one StGIT patch series can be applied at once"
234 series_dir
=`dirname "$1"`
241 set "$@" "$series_dir/$filename"
243 # remove the safety x
245 # remove the arg coming from the first-line comment
247 } < "$series_file" || clean_abort
248 # set the patch format appropriately
250 # now handle the actual StGIT patches
257 this
=`expr "$this" + 1`
258 msgnum
=`printf "%0${prec}d" $this`
259 # Perl version of StGIT parse_patch. The first nonemptyline
260 # not starting with Author, From or Date is the
261 # subject, and the body starts with the next nonempty
262 # line not starting with Author, From or Date
263 perl
-ne 'BEGIN { $subject = 0 }
264 if ($subject > 1) { print ; }
265 elsif (/^\s+$/) { next ; }
266 elsif (/^Author:/) { print s/Author/From/ ; }
267 elsif (/^(From|Date)/) { print ; }
273 print "Subject: ", $_ ;
276 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
278 echo "$this" > "$dotest/last"
283 if test -n "$parse_patch" ; then
284 clean_abort
"Patch format $patch_format is not supported."
286 clean_abort
"Patch format detection failed."
293 dotest
="$GIT_DIR/rebase-apply"
294 sign
= utf8
=t keep
= skip
= interactive
= resolved
= rebasing
= abort
=
295 resolvemsg
= resume
= scissors
= no_inbody_headers
=
297 committer_date_is_author_date
=
299 allow_rerere_autoupdate
=
313 utf8
=t
;; # this is now default
322 -r|
--resolved|
--continue)
329 rebasing
=t threeway
=t keep
=t scissors
=f no_inbody_headers
=t
;;
331 die
"-d option is no longer supported. Do not use."
334 shift; resolvemsg
=$1 ;;
335 --whitespace|
--directory)
336 git_apply_opt
="$git_apply_opt $(sq "$1=$2")"; shift ;;
338 git_apply_opt
="$git_apply_opt $(sq "$1$2")"; shift ;;
340 shift ; patch_format
="$1" ;;
341 --reject|
--ignore-whitespace|
--ignore-space-change)
342 git_apply_opt
="$git_apply_opt $1" ;;
343 --committer-date-is-author-date)
344 committer_date_is_author_date
=t
;;
347 --rerere-autoupdate|
--no-rerere-autoupdate)
348 allow_rerere_autoupdate
="$1" ;;
359 # If the dotest directory exists, but we have finished applying all the
360 # patches in them, clear it out.
361 if test -d "$dotest" &&
362 last
=$
(cat "$dotest/last") &&
363 next
=$
(cat "$dotest/next") &&
365 test "$next" -gt "$last"
372 case "$#,$skip$resolved$abort" in
374 # Explicit resume command and we do not have file, so
378 # No file input but without resume parameters; catch
379 # user error to feed us a patch from standard input
380 # when there is already $dotest. This is somewhat
381 # unreliable -- stdin could be /dev/null for example
382 # and the caller did not intend to feed us a patch but
383 # wanted to continue unattended.
390 die
"previous rebase directory $dotest still exists but mbox given."
393 case "$skip,$abort" in
395 die
"Please make up your mind. --skip or --abort?"
399 git read-tree
--reset -u HEAD HEAD
400 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
402 git update-ref ORIG_HEAD
$orig_head
405 if test -f "$dotest/rebasing"
407 exec git rebase
--abort
410 test -f "$dotest/dirtyindex" ||
{
411 git read-tree
--reset -u HEAD ORIG_HEAD
417 rm -f "$dotest/dirtyindex"
419 # Make sure we are not given --skip, --resolved, nor --abort
420 test "$skip$resolved$abort" = "" ||
421 die
"Resolve operation not in progress, we are not resuming."
424 mkdir
-p "$dotest" ||
exit
426 if test -n "$prefix" && test $# != 0
431 test -n "$first" && {
439 set "$@" "$prefix$arg" ;;
445 check_patch_format
"$@"
449 # -i can and must be given when resuming; everything
451 echo " $git_apply_opt" >"$dotest/apply-opt"
452 echo "$threeway" >"$dotest/threeway"
453 echo "$sign" >"$dotest/sign"
454 echo "$utf8" >"$dotest/utf8"
455 echo "$keep" >"$dotest/keep"
456 echo "$scissors" >"$dotest/scissors"
457 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
458 echo "$GIT_QUIET" >"$dotest/quiet"
459 echo 1 >"$dotest/next"
460 if test -n "$rebasing"
462 : >"$dotest/rebasing"
464 : >"$dotest/applying"
465 if test -n "$HAS_HEAD"
467 git update-ref ORIG_HEAD HEAD
469 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
478 files
=$
(git ls-files
) ;;
480 files
=$
(git diff-index
--cached --name-only HEAD
--) ;;
484 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
485 die
"Dirty index: cannot apply patches (dirty: $files)"
489 if test "$(cat "$dotest/utf8
")" = t
495 if test "$(cat "$dotest/keep
")" = t
499 case "$(cat "$dotest/scissors
")" in
501 scissors
=--scissors ;;
503 scissors
=--no-scissors ;;
505 if test "$(cat "$dotest/no_inbody_headers
")" = t
507 no_inbody_headers
=--no-inbody-headers
511 if test "$(cat "$dotest/quiet
")" = t
515 if test "$(cat "$dotest/threeway
")" = t
519 git_apply_opt
=$
(cat "$dotest/apply-opt")
520 if test "$(cat "$dotest/sign
")" = t
522 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
524 s/^/Signed-off-by: /'
530 last
=`cat "$dotest/last"`
531 this
=`cat "$dotest/next"`
534 this
=`expr "$this" + 1`
538 if test "$this" -gt "$last"
545 while test "$this" -le "$last"
547 msgnum
=`printf "%0${prec}d" $this`
548 next
=`expr "$this" + 1`
549 test -f "$dotest/$msgnum" ||
{
555 # If we are not resuming, parse and extract the patch information
556 # into separate files:
557 # - info records the authorship and title
558 # - msg is the rest of commit log message
559 # - patch is the patch body.
561 # When we are resuming, these files are either already prepared
562 # by the user, or the user can tell us to do so by --resolved flag.
565 git mailinfo
$keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
566 <"$dotest/$msgnum" >"$dotest/info" ||
569 # skip pine's internal folder data
570 sane_grep
'^Author: Mail System Internal Data$' \
571 <"$dotest"/info
>/dev
/null
&&
574 test -s "$dotest/patch" ||
{
575 echo "Patch is empty. Was it split wrong?"
578 if test -f "$dotest/rebasing" &&
579 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
580 -e q
"$dotest/$msgnum") &&
581 test "$(git cat-file -t "$commit")" = commit
583 git cat-file commit
"$commit" |
584 sed -e '1,/^$/d' >"$dotest/msg-clean"
587 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
591 git stripspace
> "$dotest/msg-clean"
596 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
597 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
598 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
600 if test -z "$GIT_AUTHOR_EMAIL"
602 echo "Patch does not have a valid e-mail address."
606 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
610 if test '' != "$SIGNOFF"
613 sed -ne '/^Signed-off-by: /p' \
614 "$dotest/msg-clean" |
618 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
619 test '' = "$LAST_SIGNED_OFF_BY" && echo
626 if test -s "$dotest/msg-clean"
628 cat "$dotest/msg-clean"
630 if test '' != "$ADD_SIGNOFF"
634 } >"$dotest/final-commit"
637 case "$resolved$interactive" in
639 # This is used only for interactive view option.
640 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
646 if test "$interactive" = t
649 die
"cannot be interactive without stdin connected to a terminal."
651 while test "$action" = again
653 echo "Commit Body is:"
654 echo "--------------------------"
655 cat "$dotest/final-commit"
656 echo "--------------------------"
657 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
661 [aA
]*) action
=yes interactive
= ;;
662 [nN
]*) action
=skip
;;
663 [eE
]*) git_editor
"$dotest/final-commit"
666 git_pager
"$dotest/patch" ;;
673 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
675 if test $action = skip
681 if test -x "$GIT_DIR"/hooks
/applypatch-msg
683 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
687 say
"Applying: $FIRSTLINE"
691 # When we are allowed to fall back to 3-way later, don't give
692 # false errors during the initial attempt.
694 if test "$threeway" = t
696 squelch
='>/dev/null 2>&1 '
698 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
702 # Resolved means the user did all the hard work, and
703 # we do not have to do any patch application. Just
704 # trust what the user has in the index file and the
707 git diff-index
--quiet --cached HEAD
-- && {
708 echo "No changes - did you forget to use 'git add'?"
709 stop_here_user_resolve
$this
711 unmerged
=$
(git ls-files
-u)
712 if test -n "$unmerged"
714 echo "You still have unmerged paths in your index"
715 echo "did you forget to use 'git add'?"
716 stop_here_user_resolve
$this
723 if test $apply_status = 1 && test "$threeway" = t
727 # Applying the patch to an earlier tree and merging the
728 # result may have produced the same tree as ours.
729 git diff-index
--quiet --cached HEAD
-- && {
730 say No changes
-- Patch already applied.
734 # clear apply_status -- we have successfully merged.
738 if test $apply_status != 0
740 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
741 stop_here_user_resolve
$this
744 if test -x "$GIT_DIR"/hooks
/pre-applypatch
746 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
749 tree
=$
(git write-tree
) &&
751 if test -n "$ignore_date"
755 parent
=$
(git rev-parse
--verify -q HEAD
) ||
756 say
>&2 "applying to an empty history"
758 if test -n "$committer_date_is_author_date"
760 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
761 export GIT_COMMITTER_DATE
763 git commit-tree
$tree ${parent:+-p} $parent <"$dotest/final-commit"
765 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
768 if test -x "$GIT_DIR"/hooks
/post-applypatch
770 "$GIT_DIR"/hooks
/post-applypatch