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 c,scissors strip everything before a scissors line
20 whitespace= pass it through git-apply
21 ignore-space-change pass it through git-apply
22 ignore-whitespace pass it through git-apply
23 directory= pass it through git-apply
24 C= pass it through git-apply
25 p= pass it through git-apply
26 patch-format= format the patch(es) are in
27 reject pass it through git-apply
28 resolvemsg= override error message when patch failure occurs
29 continue continue applying patches after resolving a conflict
30 r,resolved synonyms for --continue
31 skip skip the current patch
32 abort restore the original branch and abort the patching operation.
33 committer-date-is-author-date lie about committer date
34 ignore-date use current timestamp for author date
35 rerere-autoupdate update the index with reused conflict resolution if possible
36 rebasing* (internal use for git-rebase)"
39 prefix
=$
(git rev-parse
--show-prefix)
44 git var GIT_COMMITTER_IDENT
>/dev
/null ||
45 die
"You need to set your committer info first"
47 if git rev-parse
--verify -q HEAD
>/dev
/null
55 git rev-parse
--sq-quote "$@"
59 echo "$1" >"$dotest/next"
63 stop_here_user_resolve
() {
64 if [ -n "$resolvemsg" ]; then
65 printf '%s\n' "$resolvemsg"
69 if test '' != "$interactive"
73 if test '' != "$threeway"
77 echo "When you have resolved this problem run \"$cmdline --resolved\"."
78 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
79 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
85 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
86 "$dotest/patch" "$dotest/info"
87 echo "$next" >"$dotest/next"
93 echo "Cannot fall back to three-way merge."
98 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
100 rm -fr "$dotest"/patch-merge-
*
101 mkdir
"$dotest/patch-merge-tmp-dir"
103 # First see if the patch records the index info that we can use.
104 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
106 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
107 git write-tree
>"$dotest/patch-merge-base+" ||
108 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
110 say Using index info to reconstruct a base tree...
111 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
112 git apply
--cached <"$dotest/patch"
114 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
115 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
117 cannot_fallback
"Did you hand edit your patch?
118 It does not apply to blobs recorded in its index."
121 test -f "$dotest/patch-merge-index" &&
122 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
123 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
124 rm -fr "$dotest"/patch-merge-
* ||
exit 1
126 say Falling back to patching base and
3-way merge...
128 # This is not so wrong. Depending on which base we picked,
129 # orig_tree may be wildly different from ours, but his_tree
130 # has the same set of wildly different changes in parts the
131 # patch did not touch, so recursive ends up canceling them,
132 # saying that we reverted all those changes.
134 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
135 export GITHEAD_
$his_tree
136 if test -n "$GIT_QUIET"
138 export GIT_MERGE_VERBOSITY
=0
140 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
141 git rerere
$allow_rerere_autoupdate
142 echo Failed to merge
in the changes.
145 unset GITHEAD_
$his_tree
149 test $# = 0 ||
echo >&2 "$@"
156 check_patch_format
() {
157 # early return if patch_format was set from the command line
158 if test -n "$patch_format"
163 # we default to mbox format if input is from stdin and for
165 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
171 # otherwise, check the first few lines of the first patch to try
172 # to detect its format
178 "From "* |
"From: "*)
181 '# This series applies on GIT commit'*)
182 patch_format
=stgit-series
184 "# HG changeset patch")
188 # if the second line is empty and the third is
189 # a From, Author or Date entry, this is very
190 # likely an StGIT patch
192 ,"From: "* |
,"Author: "* |
,"Date: "*)
200 if test -z "$patch_format" &&
205 # This begins with three non-empty lines. Is this a
206 # piece of e-mail a-la RFC2822? Grab all the headers,
207 # discarding the indented remainder of folded lines,
208 # and see if it looks like that they all begin with the
209 # header field names...
211 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
212 sane_egrep
-v '^[!-9;-~]+:' >/dev
/null ||
215 } < "$1" || clean_abort
219 case "$patch_format" in
221 if test -n "$rebasing$keepcr"
227 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
233 clean_abort
"Only one StGIT patch series can be applied at once"
235 series_dir
=`dirname "$1"`
242 set "$@" "$series_dir/$filename"
244 # remove the safety x
246 # remove the arg coming from the first-line comment
248 } < "$series_file" || clean_abort
249 # set the patch format appropriately
251 # now handle the actual StGIT patches
258 this
=`expr "$this" + 1`
259 msgnum
=`printf "%0${prec}d" $this`
260 # Perl version of StGIT parse_patch. The first nonemptyline
261 # not starting with Author, From or Date is the
262 # subject, and the body starts with the next nonempty
263 # line not starting with Author, From or Date
264 perl
-ne 'BEGIN { $subject = 0 }
265 if ($subject > 1) { print ; }
266 elsif (/^\s+$/) { next ; }
267 elsif (/^Author:/) { print s/Author/From/ ; }
268 elsif (/^(From|Date)/) { print ; }
274 print "Subject: ", $_ ;
277 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
279 echo "$this" > "$dotest/last"
284 if test -n "$parse_patch" ; then
285 clean_abort
"Patch format $patch_format is not supported."
287 clean_abort
"Patch format detection failed."
294 dotest
="$GIT_DIR/rebase-apply"
295 sign
= utf8
=t keep
= keepcr
= skip
= interactive
= resolved
= rebasing
= abort
=
296 resolvemsg
= resume
= scissors
= no_inbody_headers
=
298 committer_date_is_author_date
=
300 allow_rerere_autoupdate
=
314 utf8
=t
;; # this is now default
323 -r|
--resolved|
--continue)
330 rebasing
=t threeway
=t keep
=t scissors
=f no_inbody_headers
=t
;;
332 die
"-d option is no longer supported. Do not use."
335 shift; resolvemsg
=$1 ;;
336 --whitespace|
--directory)
337 git_apply_opt
="$git_apply_opt $(sq "$1=$2")"; shift ;;
339 git_apply_opt
="$git_apply_opt $(sq "$1$2")"; shift ;;
341 shift ; patch_format
="$1" ;;
342 --reject|
--ignore-whitespace|
--ignore-space-change)
343 git_apply_opt
="$git_apply_opt $1" ;;
344 --committer-date-is-author-date)
345 committer_date_is_author_date
=t
;;
348 --rerere-autoupdate|
--no-rerere-autoupdate)
349 allow_rerere_autoupdate
="$1" ;;
362 # If the dotest directory exists, but we have finished applying all the
363 # patches in them, clear it out.
364 if test -d "$dotest" &&
365 last
=$
(cat "$dotest/last") &&
366 next
=$
(cat "$dotest/next") &&
368 test "$next" -gt "$last"
375 case "$#,$skip$resolved$abort" in
377 # Explicit resume command and we do not have file, so
381 # No file input but without resume parameters; catch
382 # user error to feed us a patch from standard input
383 # when there is already $dotest. This is somewhat
384 # unreliable -- stdin could be /dev/null for example
385 # and the caller did not intend to feed us a patch but
386 # wanted to continue unattended.
393 die
"previous rebase directory $dotest still exists but mbox given."
396 case "$skip,$abort" in
398 die
"Please make up your mind. --skip or --abort?"
402 git read-tree
--reset -u HEAD HEAD
403 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
405 git update-ref ORIG_HEAD
$orig_head
408 if test -f "$dotest/rebasing"
410 exec git rebase
--abort
413 test -f "$dotest/dirtyindex" ||
{
414 git read-tree
--reset -u HEAD ORIG_HEAD
420 rm -f "$dotest/dirtyindex"
422 # Make sure we are not given --skip, --resolved, nor --abort
423 test "$skip$resolved$abort" = "" ||
424 die
"Resolve operation not in progress, we are not resuming."
427 mkdir
-p "$dotest" ||
exit
429 if test -n "$prefix" && test $# != 0
434 test -n "$first" && {
442 set "$@" "$prefix$arg" ;;
448 check_patch_format
"$@"
452 # -i can and must be given when resuming; everything
454 echo " $git_apply_opt" >"$dotest/apply-opt"
455 echo "$threeway" >"$dotest/threeway"
456 echo "$sign" >"$dotest/sign"
457 echo "$utf8" >"$dotest/utf8"
458 echo "$keep" >"$dotest/keep"
459 echo "$keepcr" >"$dotest/keepcr"
460 echo "$scissors" >"$dotest/scissors"
461 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
462 echo "$GIT_QUIET" >"$dotest/quiet"
463 echo 1 >"$dotest/next"
464 if test -n "$rebasing"
466 : >"$dotest/rebasing"
468 : >"$dotest/applying"
469 if test -n "$HAS_HEAD"
471 git update-ref ORIG_HEAD HEAD
473 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
482 files
=$
(git ls-files
) ;;
484 files
=$
(git diff-index
--cached --name-only HEAD
--) ;;
488 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
489 die
"Dirty index: cannot apply patches (dirty: $files)"
493 if test "$(cat "$dotest/utf8
")" = t
499 if test "$(cat "$dotest/keep
")" = t
503 if test "$(cat "$dotest/keepcr
")" = t
507 case "$(cat "$dotest/scissors
")" in
509 scissors
=--scissors ;;
511 scissors
=--no-scissors ;;
513 if test "$(cat "$dotest/no_inbody_headers
")" = t
515 no_inbody_headers
=--no-inbody-headers
519 if test "$(cat "$dotest/quiet
")" = t
523 if test "$(cat "$dotest/threeway
")" = t
527 git_apply_opt
=$
(cat "$dotest/apply-opt")
528 if test "$(cat "$dotest/sign
")" = t
530 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
532 s/^/Signed-off-by: /'
538 last
=`cat "$dotest/last"`
539 this
=`cat "$dotest/next"`
542 this
=`expr "$this" + 1`
546 if test "$this" -gt "$last"
553 while test "$this" -le "$last"
555 msgnum
=`printf "%0${prec}d" $this`
556 next
=`expr "$this" + 1`
557 test -f "$dotest/$msgnum" ||
{
563 # If we are not resuming, parse and extract the patch information
564 # into separate files:
565 # - info records the authorship and title
566 # - msg is the rest of commit log message
567 # - patch is the patch body.
569 # When we are resuming, these files are either already prepared
570 # by the user, or the user can tell us to do so by --resolved flag.
573 git mailinfo
$keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
574 <"$dotest/$msgnum" >"$dotest/info" ||
577 # skip pine's internal folder data
578 sane_grep
'^Author: Mail System Internal Data$' \
579 <"$dotest"/info
>/dev
/null
&&
582 test -s "$dotest/patch" ||
{
583 echo "Patch is empty. Was it split wrong?"
586 if test -f "$dotest/rebasing" &&
587 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
588 -e q
"$dotest/$msgnum") &&
589 test "$(git cat-file -t "$commit")" = commit
591 git cat-file commit
"$commit" |
592 sed -e '1,/^$/d' >"$dotest/msg-clean"
595 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
599 git stripspace
> "$dotest/msg-clean"
604 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
605 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
606 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
608 if test -z "$GIT_AUTHOR_EMAIL"
610 echo "Patch does not have a valid e-mail address."
614 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
618 if test '' != "$SIGNOFF"
621 sed -ne '/^Signed-off-by: /p' \
622 "$dotest/msg-clean" |
626 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
627 test '' = "$LAST_SIGNED_OFF_BY" && echo
634 if test -s "$dotest/msg-clean"
636 cat "$dotest/msg-clean"
638 if test '' != "$ADD_SIGNOFF"
642 } >"$dotest/final-commit"
645 case "$resolved$interactive" in
647 # This is used only for interactive view option.
648 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
654 if test "$interactive" = t
657 die
"cannot be interactive without stdin connected to a terminal."
659 while test "$action" = again
661 echo "Commit Body is:"
662 echo "--------------------------"
663 cat "$dotest/final-commit"
664 echo "--------------------------"
665 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
669 [aA
]*) action
=yes interactive
= ;;
670 [nN
]*) action
=skip
;;
671 [eE
]*) git_editor
"$dotest/final-commit"
674 : ${GIT_PAGER=$(git var GIT_PAGER)}
677 $GIT_PAGER "$dotest/patch" ;;
684 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
686 if test $action = skip
692 if test -x "$GIT_DIR"/hooks
/applypatch-msg
694 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
698 say
"Applying: $FIRSTLINE"
702 # When we are allowed to fall back to 3-way later, don't give
703 # false errors during the initial attempt.
705 if test "$threeway" = t
707 squelch
='>/dev/null 2>&1 '
709 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
713 # Resolved means the user did all the hard work, and
714 # we do not have to do any patch application. Just
715 # trust what the user has in the index file and the
718 git diff-index
--quiet --cached HEAD
-- && {
719 echo "No changes - did you forget to use 'git add'?"
720 stop_here_user_resolve
$this
722 unmerged
=$
(git ls-files
-u)
723 if test -n "$unmerged"
725 echo "You still have unmerged paths in your index"
726 echo "did you forget to use 'git add'?"
727 stop_here_user_resolve
$this
734 if test $apply_status = 1 && test "$threeway" = t
738 # Applying the patch to an earlier tree and merging the
739 # result may have produced the same tree as ours.
740 git diff-index
--quiet --cached HEAD
-- && {
741 say No changes
-- Patch already applied.
745 # clear apply_status -- we have successfully merged.
749 if test $apply_status != 0
751 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
752 stop_here_user_resolve
$this
755 if test -x "$GIT_DIR"/hooks
/pre-applypatch
757 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
760 tree
=$
(git write-tree
) &&
762 if test -n "$ignore_date"
766 parent
=$
(git rev-parse
--verify -q HEAD
) ||
767 say
>&2 "applying to an empty history"
769 if test -n "$committer_date_is_author_date"
771 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
772 export GIT_COMMITTER_DATE
774 git commit-tree
$tree ${parent:+-p} $parent <"$dotest/final-commit"
776 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
779 if test -x "$GIT_DIR"/hooks
/post-applypatch
781 "$GIT_DIR"/hooks
/post-applypatch