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 whitespace= pass it through git-apply
19 ignore-space-change pass it through git-apply
20 ignore-whitespace pass it through git-apply
21 directory= pass it through git-apply
22 C= pass it through git-apply
23 p= pass it through git-apply
24 patch-format= format the patch(es) are in
25 reject pass it through git-apply
26 resolvemsg= override error message when patch failure occurs
27 r,resolved to be used after a patch failure
28 skip skip the current patch
29 abort restore the original branch and abort the patching operation.
30 committer-date-is-author-date lie about committer date
31 ignore-date use current timestamp for author date
32 rebasing* (internal use for git-rebase)"
35 prefix
=$
(git rev-parse
--show-prefix)
40 git var GIT_COMMITTER_IDENT
>/dev
/null ||
41 die
"You need to set your committer info first"
43 if git rev-parse
--verify -q HEAD
>/dev
/null
51 git rev-parse
--sq-quote "$@"
55 echo "$1" >"$dotest/next"
59 stop_here_user_resolve
() {
60 if [ -n "$resolvemsg" ]; then
61 printf '%s\n' "$resolvemsg"
65 if test '' != "$interactive"
69 if test '' != "$threeway"
73 echo "When you have resolved this problem run \"$cmdline --resolved\"."
74 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
75 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
81 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
82 "$dotest/patch" "$dotest/info"
83 echo "$next" >"$dotest/next"
89 echo "Cannot fall back to three-way merge."
94 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
96 rm -fr "$dotest"/patch-merge-
*
97 mkdir
"$dotest/patch-merge-tmp-dir"
99 # First see if the patch records the index info that we can use.
100 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
102 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
103 git write-tree
>"$dotest/patch-merge-base+" ||
104 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
106 say Using index info to reconstruct a base tree...
107 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
108 git apply
--cached <"$dotest/patch"
110 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
111 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
113 cannot_fallback
"Did you hand edit your patch?
114 It does not apply to blobs recorded in its index."
117 test -f "$dotest/patch-merge-index" &&
118 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
119 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
120 rm -fr "$dotest"/patch-merge-
* ||
exit 1
122 say Falling back to patching base and
3-way merge...
124 # This is not so wrong. Depending on which base we picked,
125 # orig_tree may be wildly different from ours, but his_tree
126 # has the same set of wildly different changes in parts the
127 # patch did not touch, so recursive ends up canceling them,
128 # saying that we reverted all those changes.
130 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
131 export GITHEAD_
$his_tree
132 if test -n "$GIT_QUIET"
134 export GIT_MERGE_VERBOSITY
=0
136 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
138 echo Failed to merge
in the changes.
141 unset GITHEAD_
$his_tree
145 test $# = 0 ||
echo >&2 "$@"
152 check_patch_format
() {
153 # early return if patch_format was set from the command line
154 if test -n "$patch_format"
159 # we default to mbox format if input is from stdin and for
161 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
167 # otherwise, check the first few lines of the first patch to try
168 # to detect its format
174 "From "* |
"From: "*)
177 '# This series applies on GIT commit'*)
178 patch_format
=stgit-series
180 "# HG changeset patch")
184 # if the second line is empty and the third is
185 # a From, Author or Date entry, this is very
186 # likely an StGIT patch
188 ,"From: "* |
,"Author: "* |
,"Date: "*)
196 if test -z "$patch_format" &&
201 # This begins with three non-empty lines. Is this a
202 # piece of e-mail a-la RFC2822? Grab all the headers,
203 # discarding the indented remainder of folded lines,
204 # and see if it looks like that they all begin with the
205 # header field names...
206 sed -n -e '/^$/q' -e '/^[ ]/d' -e p
"$1" |
207 egrep -v '^[A-Za-z]+(-[A-Za-z]+)*:' >/dev
/null ||
210 } < "$1" || clean_abort
214 case "$patch_format" in
222 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
228 clean_abort
"Only one StGIT patch series can be applied at once"
230 series_dir
=`dirname "$1"`
237 set "$@" "$series_dir/$filename"
239 # remove the safety x
241 # remove the arg coming from the first-line comment
243 } < "$series_file" || clean_abort
244 # set the patch format appropriately
246 # now handle the actual StGIT patches
253 this
=`expr "$this" + 1`
254 msgnum
=`printf "%0${prec}d" $this`
255 # Perl version of StGIT parse_patch. The first nonemptyline
256 # not starting with Author, From or Date is the
257 # subject, and the body starts with the next nonempty
258 # line not starting with Author, From or Date
259 perl
-ne 'BEGIN { $subject = 0 }
260 if ($subject > 1) { print ; }
261 elsif (/^\s+$/) { next ; }
262 elsif (/^Author:/) { print s/Author/From/ ; }
263 elsif (/^(From|Date)/) { print ; }
269 print "Subject: ", $_ ;
272 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
274 echo "$this" > "$dotest/last"
279 if test -n "$parse_patch" ; then
280 clean_abort
"Patch format $patch_format is not supported."
282 clean_abort
"Patch format detection failed."
289 dotest
="$GIT_DIR/rebase-apply"
290 sign
= utf8
=t keep
= skip
= interactive
= resolved
= rebasing
= abort
=
293 committer_date_is_author_date
=
308 utf8
=t
;; # this is now default
320 rebasing
=t threeway
=t keep
=t
;;
322 die
"-d option is no longer supported. Do not use."
325 shift; resolvemsg
=$1 ;;
326 --whitespace|
--directory)
327 git_apply_opt
="$git_apply_opt $(sq "$1=$2")"; shift ;;
329 git_apply_opt
="$git_apply_opt $(sq "$1$2")"; shift ;;
331 shift ; patch_format
="$1" ;;
332 --reject|
--ignore-whitespace|
--ignore-space-change)
333 git_apply_opt
="$git_apply_opt $1" ;;
334 --committer-date-is-author-date)
335 committer_date_is_author_date
=t
;;
348 # If the dotest directory exists, but we have finished applying all the
349 # patches in them, clear it out.
350 if test -d "$dotest" &&
351 last
=$
(cat "$dotest/last") &&
352 next
=$
(cat "$dotest/next") &&
354 test "$next" -gt "$last"
361 case "$#,$skip$resolved$abort" in
363 # Explicit resume command and we do not have file, so
367 # No file input but without resume parameters; catch
368 # user error to feed us a patch from standard input
369 # when there is already $dotest. This is somewhat
370 # unreliable -- stdin could be /dev/null for example
371 # and the caller did not intend to feed us a patch but
372 # wanted to continue unattended.
379 die
"previous rebase directory $dotest still exists but mbox given."
382 case "$skip,$abort" in
384 die
"Please make up your mind. --skip or --abort?"
388 git read-tree
--reset -u HEAD HEAD
389 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
391 git update-ref ORIG_HEAD
$orig_head
394 if test -f "$dotest/rebasing"
396 exec git rebase
--abort
399 test -f "$dotest/dirtyindex" ||
{
400 git read-tree
--reset -u HEAD ORIG_HEAD
406 rm -f "$dotest/dirtyindex"
408 # Make sure we are not given --skip, --resolved, nor --abort
409 test "$skip$resolved$abort" = "" ||
410 die
"Resolve operation not in progress, we are not resuming."
413 mkdir
-p "$dotest" ||
exit
415 if test -n "$prefix" && test $# != 0
420 test -n "$first" && {
428 set "$@" "$prefix$arg" ;;
434 check_patch_format
"$@"
438 # -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
439 # for the resuming session after a patch failure.
440 # -i can and must be given when resuming.
441 echo " $git_apply_opt" >"$dotest/apply-opt"
442 echo "$threeway" >"$dotest/threeway"
443 echo "$sign" >"$dotest/sign"
444 echo "$utf8" >"$dotest/utf8"
445 echo "$keep" >"$dotest/keep"
446 echo "$GIT_QUIET" >"$dotest/quiet"
447 echo 1 >"$dotest/next"
448 if test -n "$rebasing"
450 : >"$dotest/rebasing"
452 : >"$dotest/applying"
453 if test -n "$HAS_HEAD"
455 git update-ref ORIG_HEAD HEAD
457 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
466 files
=$
(git ls-files
) ;;
468 files
=$
(git diff-index
--cached --name-only HEAD
--) ;;
472 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
473 die
"Dirty index: cannot apply patches (dirty: $files)"
477 if test "$(cat "$dotest/utf8
")" = t
483 if test "$(cat "$dotest/keep
")" = t
487 if test "$(cat "$dotest/quiet
")" = t
491 if test "$(cat "$dotest/threeway
")" = t
495 git_apply_opt
=$
(cat "$dotest/apply-opt")
496 if test "$(cat "$dotest/sign
")" = t
498 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
500 s/^/Signed-off-by: /'
506 last
=`cat "$dotest/last"`
507 this
=`cat "$dotest/next"`
510 this
=`expr "$this" + 1`
514 if test "$this" -gt "$last"
521 while test "$this" -le "$last"
523 msgnum
=`printf "%0${prec}d" $this`
524 next
=`expr "$this" + 1`
525 test -f "$dotest/$msgnum" ||
{
531 # If we are not resuming, parse and extract the patch information
532 # into separate files:
533 # - info records the authorship and title
534 # - msg is the rest of commit log message
535 # - patch is the patch body.
537 # When we are resuming, these files are either already prepared
538 # by the user, or the user can tell us to do so by --resolved flag.
541 git mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
542 <"$dotest/$msgnum" >"$dotest/info" ||
545 # skip pine's internal folder data
546 grep '^Author: Mail System Internal Data$' \
547 <"$dotest"/info
>/dev
/null
&&
550 test -s "$dotest/patch" ||
{
551 echo "Patch is empty. Was it split wrong?"
554 if test -f "$dotest/rebasing" &&
555 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
556 -e q
"$dotest/$msgnum") &&
557 test "$(git cat-file -t "$commit")" = commit
559 git cat-file commit
"$commit" |
560 sed -e '1,/^$/d' >"$dotest/msg-clean"
562 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
563 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
565 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
566 git stripspace
> "$dotest/msg-clean"
571 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
572 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
573 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
575 if test -z "$GIT_AUTHOR_EMAIL"
577 echo "Patch does not have a valid e-mail address."
581 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
585 if test '' != "$SIGNOFF"
588 sed -ne '/^Signed-off-by: /p' \
589 "$dotest/msg-clean" |
593 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
594 test '' = "$LAST_SIGNED_OFF_BY" && echo
601 if test -s "$dotest/msg-clean"
603 cat "$dotest/msg-clean"
605 if test '' != "$ADD_SIGNOFF"
609 } >"$dotest/final-commit"
612 case "$resolved$interactive" in
614 # This is used only for interactive view option.
615 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
621 if test "$interactive" = t
624 die
"cannot be interactive without stdin connected to a terminal."
626 while test "$action" = again
628 echo "Commit Body is:"
629 echo "--------------------------"
630 cat "$dotest/final-commit"
631 echo "--------------------------"
632 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
636 [aA
]*) action
=yes interactive
= ;;
637 [nN
]*) action
=skip
;;
638 [eE
]*) git_editor
"$dotest/final-commit"
641 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
648 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
650 if test $action = skip
656 if test -x "$GIT_DIR"/hooks
/applypatch-msg
658 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
662 say
"Applying: $FIRSTLINE"
666 # When we are allowed to fall back to 3-way later, don't give
667 # false errors during the initial attempt.
669 if test "$threeway" = t
671 squelch
='>/dev/null 2>&1 '
673 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
677 # Resolved means the user did all the hard work, and
678 # we do not have to do any patch application. Just
679 # trust what the user has in the index file and the
682 git diff-index
--quiet --cached HEAD
-- && {
683 echo "No changes - did you forget to use 'git add'?"
684 stop_here_user_resolve
$this
686 unmerged
=$
(git ls-files
-u)
687 if test -n "$unmerged"
689 echo "You still have unmerged paths in your index"
690 echo "did you forget to use 'git add'?"
691 stop_here_user_resolve
$this
698 if test $apply_status = 1 && test "$threeway" = t
702 # Applying the patch to an earlier tree and merging the
703 # result may have produced the same tree as ours.
704 git diff-index
--quiet --cached HEAD
-- && {
705 say No changes
-- Patch already applied.
709 # clear apply_status -- we have successfully merged.
713 if test $apply_status != 0
715 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
716 stop_here_user_resolve
$this
719 if test -x "$GIT_DIR"/hooks
/pre-applypatch
721 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
724 tree
=$
(git write-tree
) &&
726 if test -n "$ignore_date"
730 parent
=$
(git rev-parse
--verify -q HEAD
) ||
731 say
>&2 "applying to an empty history"
733 if test -n "$committer_date_is_author_date"
735 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
736 export GIT_COMMITTER_DATE
738 git commit-tree
$tree ${parent:+-p} $parent <"$dotest/final-commit"
740 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
743 if test -x "$GIT_DIR"/hooks
/post-applypatch
745 "$GIT_DIR"/hooks
/post-applypatch