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 directory= pass it through git-apply
20 C= pass it through git-apply
21 p= pass it through git-apply
22 patch-format= format the patch(es) are in
23 reject pass it through git-apply
24 resolvemsg= override error message when patch failure occurs
25 r,resolved to be used after a patch failure
26 skip skip the current patch
27 abort restore the original branch and abort the patching operation.
28 committer-date-is-author-date lie about committer date
29 ignore-date use current timestamp for author date
30 rebasing* (internal use for git-rebase)"
33 prefix
=$
(git rev-parse
--show-prefix)
38 git var GIT_COMMITTER_IDENT
>/dev
/null ||
39 die
"You need to set your committer info first"
41 if git rev-parse
--verify -q HEAD
>/dev
/null
49 git rev-parse
--sq-quote "$@"
53 echo "$1" >"$dotest/next"
57 stop_here_user_resolve
() {
58 if [ -n "$resolvemsg" ]; then
59 printf '%s\n' "$resolvemsg"
63 if test '' != "$interactive"
67 if test '' != "$threeway"
71 echo "When you have resolved this problem run \"$cmdline --resolved\"."
72 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
73 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
79 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
80 "$dotest/patch" "$dotest/info"
81 echo "$next" >"$dotest/next"
87 echo "Cannot fall back to three-way merge."
92 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
94 rm -fr "$dotest"/patch-merge-
*
95 mkdir
"$dotest/patch-merge-tmp-dir"
97 # First see if the patch records the index info that we can use.
98 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
100 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
101 git write-tree
>"$dotest/patch-merge-base+" ||
102 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
104 say Using index info to reconstruct a base tree...
105 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
106 git apply
--cached <"$dotest/patch"
108 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
109 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
111 cannot_fallback
"Did you hand edit your patch?
112 It does not apply to blobs recorded in its index."
115 test -f "$dotest/patch-merge-index" &&
116 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
117 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
118 rm -fr "$dotest"/patch-merge-
* ||
exit 1
120 say Falling back to patching base and
3-way merge...
122 # This is not so wrong. Depending on which base we picked,
123 # orig_tree may be wildly different from ours, but his_tree
124 # has the same set of wildly different changes in parts the
125 # patch did not touch, so recursive ends up canceling them,
126 # saying that we reverted all those changes.
128 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
129 export GITHEAD_
$his_tree
130 if test -n "$GIT_QUIET"
132 export GIT_MERGE_VERBOSITY
=0
134 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
136 echo Failed to merge
in the changes.
139 unset GITHEAD_
$his_tree
143 test $# = 0 ||
echo >&2 "$@"
150 check_patch_format
() {
151 # early return if patch_format was set from the command line
152 if test -n "$patch_format"
157 # we default to mbox format if input is from stdin and for
159 if test $# = 0 ||
test "x$1" = "x-" ||
test -d "$1"
165 # otherwise, check the first few lines of the first patch to try
166 # to detect its format
172 "From "* |
"From: "*)
175 '# This series applies on GIT commit'*)
176 patch_format
=stgit-series
178 "# HG changeset patch")
182 # if the second line is empty and the third is
183 # a From, Author or Date entry, this is very
184 # likely an StGIT patch
186 ,"From: "* |
,"Author: "* |
,"Date: "*)
194 if test -z "$patch_format" &&
199 # This begins with three non-empty lines. Is this a
200 # piece of e-mail a-la RFC2822? Grab all the headers,
201 # discarding the indented remainder of folded lines,
202 # and see if it looks like that they all begin with the
203 # header field names...
204 sed -n -e '/^$/q' -e '/^[ ]/d' -e p
"$1" |
205 egrep -v '^[A-Za-z]+(-[A-Za-z]+)*:' >/dev
/null ||
208 } < "$1" || clean_abort
212 case "$patch_format" in
220 git mailsplit
-d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
226 clean_abort
"Only one StGIT patch series can be applied at once"
228 series_dir
=`dirname "$1"`
235 set "$@" "$series_dir/$filename"
237 # remove the safety x
239 # remove the arg coming from the first-line comment
241 } < "$series_file" || clean_abort
242 # set the patch format appropriately
244 # now handle the actual StGIT patches
251 this
=`expr "$this" + 1`
252 msgnum
=`printf "%0${prec}d" $this`
253 # Perl version of StGIT parse_patch. The first nonemptyline
254 # not starting with Author, From or Date is the
255 # subject, and the body starts with the next nonempty
256 # line not starting with Author, From or Date
257 perl
-ne 'BEGIN { $subject = 0 }
258 if ($subject > 1) { print ; }
259 elsif (/^\s+$/) { next ; }
260 elsif (/^Author:/) { print s/Author/From/ ; }
261 elsif (/^(From|Date)/) { print ; }
267 print "Subject: ", $_ ;
270 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
272 echo "$this" > "$dotest/last"
277 if test -n "$parse_patch" ; then
278 clean_abort
"Patch format $patch_format is not supported."
280 clean_abort
"Patch format detection failed."
287 dotest
="$GIT_DIR/rebase-apply"
288 sign
= utf8
=t keep
= skip
= interactive
= resolved
= rebasing
= abort
=
291 committer_date_is_author_date
=
306 utf8
=t
;; # this is now default
318 rebasing
=t threeway
=t keep
=t
;;
320 die
"-d option is no longer supported. Do not use."
323 shift; resolvemsg
=$1 ;;
324 --whitespace|
--directory)
325 git_apply_opt
="$git_apply_opt $(sq "$1=$2")"; shift ;;
327 git_apply_opt
="$git_apply_opt $(sq "$1$2")"; shift ;;
329 shift ; patch_format
="$1" ;;
331 git_apply_opt
="$git_apply_opt $1" ;;
332 --committer-date-is-author-date)
333 committer_date_is_author_date
=t
;;
346 # If the dotest directory exists, but we have finished applying all the
347 # patches in them, clear it out.
348 if test -d "$dotest" &&
349 last
=$
(cat "$dotest/last") &&
350 next
=$
(cat "$dotest/next") &&
352 test "$next" -gt "$last"
359 case "$#,$skip$resolved$abort" in
361 # Explicit resume command and we do not have file, so
365 # No file input but without resume parameters; catch
366 # user error to feed us a patch from standard input
367 # when there is already $dotest. This is somewhat
368 # unreliable -- stdin could be /dev/null for example
369 # and the caller did not intend to feed us a patch but
370 # wanted to continue unattended.
377 die
"previous rebase directory $dotest still exists but mbox given."
380 case "$skip,$abort" in
382 die
"Please make up your mind. --skip or --abort?"
386 git read-tree
--reset -u HEAD HEAD
387 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
389 git update-ref ORIG_HEAD
$orig_head
392 if test -f "$dotest/rebasing"
394 exec git rebase
--abort
397 test -f "$dotest/dirtyindex" ||
{
398 git read-tree
--reset -u HEAD ORIG_HEAD
404 rm -f "$dotest/dirtyindex"
406 # Make sure we are not given --skip, --resolved, nor --abort
407 test "$skip$resolved$abort" = "" ||
408 die
"Resolve operation not in progress, we are not resuming."
411 mkdir
-p "$dotest" ||
exit
413 if test -n "$prefix" && test $# != 0
418 test -n "$first" && {
426 set "$@" "$prefix$arg" ;;
432 check_patch_format
"$@"
436 # -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
437 # for the resuming session after a patch failure.
438 # -i can and must be given when resuming.
439 echo " $git_apply_opt" >"$dotest/apply-opt"
440 echo "$threeway" >"$dotest/threeway"
441 echo "$sign" >"$dotest/sign"
442 echo "$utf8" >"$dotest/utf8"
443 echo "$keep" >"$dotest/keep"
444 echo "$GIT_QUIET" >"$dotest/quiet"
445 echo 1 >"$dotest/next"
446 if test -n "$rebasing"
448 : >"$dotest/rebasing"
450 : >"$dotest/applying"
451 if test -n "$HAS_HEAD"
453 git update-ref ORIG_HEAD HEAD
455 git update-ref
-d ORIG_HEAD
>/dev
/null
2>&1
464 files
=$
(git ls-files
) ;;
466 files
=$
(git diff-index
--cached --name-only HEAD
--) ;;
470 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
471 die
"Dirty index: cannot apply patches (dirty: $files)"
475 if test "$(cat "$dotest/utf8
")" = t
481 if test "$(cat "$dotest/keep
")" = t
485 if test "$(cat "$dotest/quiet
")" = t
489 if test "$(cat "$dotest/threeway
")" = t
493 git_apply_opt
=$
(cat "$dotest/apply-opt")
494 if test "$(cat "$dotest/sign
")" = t
496 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
498 s/^/Signed-off-by: /'
504 last
=`cat "$dotest/last"`
505 this
=`cat "$dotest/next"`
508 this
=`expr "$this" + 1`
512 if test "$this" -gt "$last"
519 while test "$this" -le "$last"
521 msgnum
=`printf "%0${prec}d" $this`
522 next
=`expr "$this" + 1`
523 test -f "$dotest/$msgnum" ||
{
529 # If we are not resuming, parse and extract the patch information
530 # into separate files:
531 # - info records the authorship and title
532 # - msg is the rest of commit log message
533 # - patch is the patch body.
535 # When we are resuming, these files are either already prepared
536 # by the user, or the user can tell us to do so by --resolved flag.
539 git mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
540 <"$dotest/$msgnum" >"$dotest/info" ||
543 # skip pine's internal folder data
544 grep '^Author: Mail System Internal Data$' \
545 <"$dotest"/info
>/dev
/null
&&
548 test -s "$dotest/patch" ||
{
549 echo "Patch is empty. Was it split wrong?"
552 if test -f "$dotest/rebasing" &&
553 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
554 -e q
"$dotest/$msgnum") &&
555 test "$(git cat-file -t "$commit")" = commit
557 git cat-file commit
"$commit" |
558 sed -e '1,/^$/d' >"$dotest/msg-clean"
560 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
561 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
563 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
564 git stripspace
> "$dotest/msg-clean"
569 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
570 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
571 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
573 if test -z "$GIT_AUTHOR_EMAIL"
575 echo "Patch does not have a valid e-mail address."
579 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
583 if test '' != "$SIGNOFF"
586 sed -ne '/^Signed-off-by: /p' \
587 "$dotest/msg-clean" |
591 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
592 test '' = "$LAST_SIGNED_OFF_BY" && echo
599 if test -s "$dotest/msg-clean"
601 cat "$dotest/msg-clean"
603 if test '' != "$ADD_SIGNOFF"
607 } >"$dotest/final-commit"
610 case "$resolved$interactive" in
612 # This is used only for interactive view option.
613 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
619 if test "$interactive" = t
622 die
"cannot be interactive without stdin connected to a terminal."
624 while test "$action" = again
626 echo "Commit Body is:"
627 echo "--------------------------"
628 cat "$dotest/final-commit"
629 echo "--------------------------"
630 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
634 [aA
]*) action
=yes interactive
= ;;
635 [nN
]*) action
=skip
;;
636 [eE
]*) git_editor
"$dotest/final-commit"
639 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
646 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
648 if test $action = skip
654 if test -x "$GIT_DIR"/hooks
/applypatch-msg
656 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
660 say
"Applying: $FIRSTLINE"
664 # When we are allowed to fall back to 3-way later, don't give
665 # false errors during the initial attempt.
667 if test "$threeway" = t
669 squelch
='>/dev/null 2>&1 '
671 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
675 # Resolved means the user did all the hard work, and
676 # we do not have to do any patch application. Just
677 # trust what the user has in the index file and the
680 git diff-index
--quiet --cached HEAD
-- && {
681 echo "No changes - did you forget to use 'git add'?"
682 stop_here_user_resolve
$this
684 unmerged
=$
(git ls-files
-u)
685 if test -n "$unmerged"
687 echo "You still have unmerged paths in your index"
688 echo "did you forget to use 'git add'?"
689 stop_here_user_resolve
$this
696 if test $apply_status = 1 && test "$threeway" = t
700 # Applying the patch to an earlier tree and merging the
701 # result may have produced the same tree as ours.
702 git diff-index
--quiet --cached HEAD
-- && {
703 say No changes
-- Patch already applied.
707 # clear apply_status -- we have successfully merged.
711 if test $apply_status != 0
713 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
714 stop_here_user_resolve
$this
717 if test -x "$GIT_DIR"/hooks
/pre-applypatch
719 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
722 tree
=$
(git write-tree
) &&
724 if test -n "$ignore_date"
728 parent
=$
(git rev-parse
--verify -q HEAD
) ||
729 say
>&2 "applying to an empty history"
731 if test -n "$committer_date_is_author_date"
733 GIT_COMMITTER_DATE
="$GIT_AUTHOR_DATE"
734 export GIT_COMMITTER_DATE
736 git commit-tree
$tree ${parent:+-p} $parent <"$dotest/final-commit"
738 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
741 if test -x "$GIT_DIR"/hooks
/post-applypatch
743 "$GIT_DIR"/hooks
/post-applypatch