3 # Copyright (c) 2005, 2006 Junio C Hamano
8 git am [options] [<mbox>|<Maildir>...]
9 git am [options] (--resolved | --skip | --abort)
11 d,dotest= (removed -- do not use)
12 i,interactive run interactively
13 b,binary (historical option -- no-op)
14 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 reject pass it through git-apply
23 resolvemsg= override error message when patch failure occurs
24 r,resolved to be used after a patch failure
25 skip skip the current patch
26 abort restore the original branch and abort the patching operation.
27 committer-date-is-author-date lie about committer date
28 ignore-date use current timestamp for author date
29 rebasing (internal use for git-rebase)"
32 prefix=$(git rev-parse --show-prefix)
37 git var GIT_COMMITTER_IDENT >/dev/null ||
38 die "You need to set your committer info first"
43 printf "%s" "$sqarg" |
44 sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
49 echo "$1" >"$dotest/next"
53 stop_here_user_resolve () {
54 if [ -n "$resolvemsg" ]; then
55 printf '%s\n' "$resolvemsg"
59 if test '' != "$interactive"
63 if test '' != "$threeway"
67 echo "When you have resolved this problem run \"$cmdline --resolved\"."
68 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
69 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
75 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
76 "$dotest/patch" "$dotest/info"
77 echo "$next" >"$dotest/next"
83 echo "Cannot fall back to three-way merge."
88 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
90 rm -fr "$dotest"/patch-merge-*
91 mkdir "$dotest/patch-merge-tmp-dir"
93 # First see if the patch records the index info that we can use.
94 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
96 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
97 git write-tree >"$dotest/patch-merge-base+" ||
98 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
100 echo Using index info to reconstruct a base tree...
101 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
102 git apply --cached <"$dotest/patch"
104 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
105 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
107 cannot_fallback "Did you hand edit your patch?
108 It does not apply to blobs recorded in its index."
111 test -f "$dotest/patch-merge-index" &&
112 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
113 orig_tree=$(cat "$dotest/patch-merge-base") &&
114 rm -fr "$dotest"/patch-merge-* || exit 1
116 echo Falling back to patching base and 3-way merge...
118 # This is not so wrong. Depending on which base we picked,
119 # orig_tree may be wildly different from ours, but his_tree
120 # has the same set of wildly different changes in parts the
121 # patch did not touch, so recursive ends up canceling them,
122 # saying that we reverted all those changes.
124 eval GITHEAD_$his_tree='"$FIRSTLINE"'
125 export GITHEAD_$his_tree
126 git-merge-recursive $orig_tree -- HEAD $his_tree || {
128 echo Failed to merge in the changes.
131 unset GITHEAD_$his_tree
135 dotest="$GIT_DIR/rebase-apply"
136 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
139 committer_date_is_author_date=
154 utf8=t ;; # this is now default
166 rebasing=t threeway=t keep=t ;;
168 die "-d option is no longer supported. Do not use."
171 shift; resolvemsg=$1 ;;
172 --whitespace|--directory)
173 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
175 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
177 git_apply_opt="$git_apply_opt $1" ;;
178 --committer-date-is-author-date)
179 committer_date_is_author_date=t ;;
190 # If the dotest directory exists, but we have finished applying all the
191 # patches in them, clear it out.
192 if test -d "$dotest" &&
193 last=$(cat "$dotest/last") &&
194 next=$(cat "$dotest/next") &&
196 test "$next" -gt "$last"
203 case "$#,$skip$resolved$abort" in
205 # Explicit resume command and we do not have file, so
209 # No file input but without resume parameters; catch
210 # user error to feed us a patch from standard input
211 # when there is already $dotest. This is somewhat
212 # unreliable -- stdin could be /dev/null for example
213 # and the caller did not intend to feed us a patch but
214 # wanted to continue unattended.
221 die "previous rebase directory $dotest still exists but mbox given."
224 case "$skip,$abort" in
227 git read-tree --reset -u HEAD HEAD
228 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
230 git update-ref ORIG_HEAD $orig_head
234 git read-tree --reset -u HEAD ORIG_HEAD
240 # Make sure we are not given --skip, --resolved, nor --abort
241 test "$skip$resolved$abort" = "" ||
242 die "Resolve operation not in progress, we are not resuming."
245 mkdir -p "$dotest" || exit
247 if test -n "$prefix" && test $# != 0
252 test -n "$first" && {
260 set "$@" "$prefix$arg" ;;
265 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
270 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
271 # for the resuming session after a patch failure.
272 # -i can and must be given when resuming.
273 echo " $git_apply_opt" >"$dotest/apply-opt"
274 echo "$threeway" >"$dotest/threeway"
275 echo "$sign" >"$dotest/sign"
276 echo "$utf8" >"$dotest/utf8"
277 echo "$keep" >"$dotest/keep"
278 echo 1 >"$dotest/next"
279 if test -n "$rebasing"
281 : >"$dotest/rebasing"
283 : >"$dotest/applying"
284 git update-ref ORIG_HEAD HEAD
290 files=$(git diff-index --cached --name-only HEAD --) || exit
291 if [ "$files" ]; then
292 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
297 if test "$(cat "$dotest/utf8")" = t
303 if test "$(cat "$dotest/keep")" = t
307 if test "$(cat "$dotest/threeway")" = t
311 git_apply_opt=$(cat "$dotest/apply-opt")
312 if test "$(cat "$dotest/sign")" = t
314 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
316 s/^/Signed-off-by: /'
322 last=`cat "$dotest/last"`
323 this=`cat "$dotest/next"`
326 this=`expr "$this" + 1`
330 if test "$this" -gt "$last"
337 while test "$this" -le "$last"
339 msgnum=`printf "%0${prec}d" $this`
340 next=`expr "$this" + 1`
341 test -f "$dotest/$msgnum" || {
347 # If we are not resuming, parse and extract the patch information
348 # into separate files:
349 # - info records the authorship and title
350 # - msg is the rest of commit log message
351 # - patch is the patch body.
353 # When we are resuming, these files are either already prepared
354 # by the user, or the user can tell us to do so by --resolved flag.
357 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
358 <"$dotest/$msgnum" >"$dotest/info" ||
361 # skip pine's internal folder data
362 grep '^Author: Mail System Internal Data$' \
363 <"$dotest"/info >/dev/null &&
366 test -s "$dotest/patch" || {
367 echo "Patch is empty. Was it split wrong?"
370 if test -f "$dotest/rebasing" &&
371 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
372 -e q "$dotest/$msgnum") &&
373 test "$(git cat-file -t "$commit")" = commit
375 git cat-file commit "$commit" |
376 sed -e '1,/^$/d' >"$dotest/msg-clean"
378 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
379 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
381 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
382 git stripspace > "$dotest/msg-clean"
387 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
388 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
389 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
391 if test -z "$GIT_AUTHOR_EMAIL"
393 echo "Patch does not have a valid e-mail address."
397 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
401 if test '' != "$SIGNOFF"
404 sed -ne '/^Signed-off-by: /p' \
405 "$dotest/msg-clean" |
409 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
410 test '' = "$LAST_SIGNED_OFF_BY" && echo
417 if test -s "$dotest/msg-clean"
419 cat "$dotest/msg-clean"
421 if test '' != "$ADD_SIGNOFF"
425 } >"$dotest/final-commit"
428 case "$resolved$interactive" in
430 # This is used only for interactive view option.
431 git diff-index -p --cached HEAD -- >"$dotest/patch"
437 if test "$interactive" = t
440 die "cannot be interactive without stdin connected to a terminal."
442 while test "$action" = again
444 echo "Commit Body is:"
445 echo "--------------------------"
446 cat "$dotest/final-commit"
447 echo "--------------------------"
448 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
452 [aA]*) action=yes interactive= ;;
453 [nN]*) action=skip ;;
454 [eE]*) git_editor "$dotest/final-commit"
457 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
464 FIRSTLINE=$(sed 1q "$dotest/final-commit")
466 if test $action = skip
472 if test -x "$GIT_DIR"/hooks/applypatch-msg
474 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
478 printf 'Applying: %s\n' "$FIRSTLINE"
482 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
486 # Resolved means the user did all the hard work, and
487 # we do not have to do any patch application. Just
488 # trust what the user has in the index file and the
491 git diff-index --quiet --cached HEAD -- && {
492 echo "No changes - did you forget to use 'git add'?"
493 stop_here_user_resolve $this
495 unmerged=$(git ls-files -u)
496 if test -n "$unmerged"
498 echo "You still have unmerged paths in your index"
499 echo "did you forget to use 'git add'?"
500 stop_here_user_resolve $this
507 if test $apply_status = 1 && test "$threeway" = t
511 # Applying the patch to an earlier tree and merging the
512 # result may have produced the same tree as ours.
513 git diff-index --quiet --cached HEAD -- && {
514 echo No changes -- Patch already applied.
518 # clear apply_status -- we have successfully merged.
522 if test $apply_status != 0
524 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
525 stop_here_user_resolve $this
528 if test -x "$GIT_DIR"/hooks/pre-applypatch
530 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
533 tree=$(git write-tree) &&
534 parent=$(git rev-parse --verify HEAD) &&
536 if test -n "$ignore_date"
540 if test -n "$committer_date_is_author_date"
542 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
543 export GIT_COMMITTER_DATE
545 git commit-tree $tree -p $parent <"$dotest/final-commit"
547 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
550 if test -x "$GIT_DIR"/hooks/post-applypatch
552 "$GIT_DIR"/hooks/post-applypatch