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 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"
40 if git rev-parse --verify -q HEAD >/dev/null
48 git rev-parse --sq-quote "$@"
52 echo "$1" >"$dotest/next"
56 stop_here_user_resolve () {
57 if [ -n "$resolvemsg" ]; then
58 printf '%s\n' "$resolvemsg"
62 if test '' != "$interactive"
66 if test '' != "$threeway"
70 echo "When you have resolved this problem run \"$cmdline --resolved\"."
71 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
72 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
78 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
79 "$dotest/patch" "$dotest/info"
80 echo "$next" >"$dotest/next"
86 echo "Cannot fall back to three-way merge."
91 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
93 rm -fr "$dotest"/patch-merge-*
94 mkdir "$dotest/patch-merge-tmp-dir"
96 # First see if the patch records the index info that we can use.
97 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
99 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
100 git write-tree >"$dotest/patch-merge-base+" ||
101 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
103 say Using index info to reconstruct a base tree...
104 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
105 git apply --cached <"$dotest/patch"
107 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
108 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
110 cannot_fallback "Did you hand edit your patch?
111 It does not apply to blobs recorded in its index."
114 test -f "$dotest/patch-merge-index" &&
115 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
116 orig_tree=$(cat "$dotest/patch-merge-base") &&
117 rm -fr "$dotest"/patch-merge-* || exit 1
119 say Falling back to patching base and 3-way merge...
121 # This is not so wrong. Depending on which base we picked,
122 # orig_tree may be wildly different from ours, but his_tree
123 # has the same set of wildly different changes in parts the
124 # patch did not touch, so recursive ends up canceling them,
125 # saying that we reverted all those changes.
127 eval GITHEAD_$his_tree='"$FIRSTLINE"'
128 export GITHEAD_$his_tree
129 if test -n "$GIT_QUIET"
131 export GIT_MERGE_VERBOSITY=0
133 git-merge-recursive $orig_tree -- HEAD $his_tree || {
135 echo Failed to merge in the changes.
138 unset GITHEAD_$his_tree
142 dotest="$GIT_DIR/rebase-apply"
143 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
146 committer_date_is_author_date=
161 utf8=t ;; # this is now default
173 rebasing=t threeway=t keep=t ;;
175 die "-d option is no longer supported. Do not use."
178 shift; resolvemsg=$1 ;;
179 --whitespace|--directory)
180 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
182 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
184 git_apply_opt="$git_apply_opt $1" ;;
185 --committer-date-is-author-date)
186 committer_date_is_author_date=t ;;
199 # If the dotest directory exists, but we have finished applying all the
200 # patches in them, clear it out.
201 if test -d "$dotest" &&
202 last=$(cat "$dotest/last") &&
203 next=$(cat "$dotest/next") &&
205 test "$next" -gt "$last"
212 case "$#,$skip$resolved$abort" in
214 # Explicit resume command and we do not have file, so
218 # No file input but without resume parameters; catch
219 # user error to feed us a patch from standard input
220 # when there is already $dotest. This is somewhat
221 # unreliable -- stdin could be /dev/null for example
222 # and the caller did not intend to feed us a patch but
223 # wanted to continue unattended.
230 die "previous rebase directory $dotest still exists but mbox given."
233 case "$skip,$abort" in
235 die "Please make up your mind. --skip or --abort?"
239 git read-tree --reset -u HEAD HEAD
240 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
242 git update-ref ORIG_HEAD $orig_head
245 if test -f "$dotest/rebasing"
247 exec git rebase --abort
250 test -f "$dotest/dirtyindex" || {
251 git read-tree --reset -u HEAD ORIG_HEAD
257 rm -f "$dotest/dirtyindex"
259 # Make sure we are not given --skip, --resolved, nor --abort
260 test "$skip$resolved$abort" = "" ||
261 die "Resolve operation not in progress, we are not resuming."
264 mkdir -p "$dotest" || exit
266 if test -n "$prefix" && test $# != 0
271 test -n "$first" && {
279 set "$@" "$prefix$arg" ;;
284 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
289 # -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
290 # for the resuming session after a patch failure.
291 # -i can and must be given when resuming.
292 echo " $git_apply_opt" >"$dotest/apply-opt"
293 echo "$threeway" >"$dotest/threeway"
294 echo "$sign" >"$dotest/sign"
295 echo "$utf8" >"$dotest/utf8"
296 echo "$keep" >"$dotest/keep"
297 echo "$GIT_QUIET" >"$dotest/quiet"
298 echo 1 >"$dotest/next"
299 if test -n "$rebasing"
301 : >"$dotest/rebasing"
303 : >"$dotest/applying"
304 if test -n "$HAS_HEAD"
306 git update-ref ORIG_HEAD HEAD
308 git update-ref -d ORIG_HEAD >/dev/null 2>&1
317 files=$(git ls-files) ;;
319 files=$(git diff-index --cached --name-only HEAD --) ;;
323 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
324 die "Dirty index: cannot apply patches (dirty: $files)"
328 if test "$(cat "$dotest/utf8")" = t
334 if test "$(cat "$dotest/keep")" = t
338 if test "$(cat "$dotest/quiet")" = t
342 if test "$(cat "$dotest/threeway")" = t
346 git_apply_opt=$(cat "$dotest/apply-opt")
347 if test "$(cat "$dotest/sign")" = t
349 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
351 s/^/Signed-off-by: /'
357 last=`cat "$dotest/last"`
358 this=`cat "$dotest/next"`
361 this=`expr "$this" + 1`
365 if test "$this" -gt "$last"
372 while test "$this" -le "$last"
374 msgnum=`printf "%0${prec}d" $this`
375 next=`expr "$this" + 1`
376 test -f "$dotest/$msgnum" || {
382 # If we are not resuming, parse and extract the patch information
383 # into separate files:
384 # - info records the authorship and title
385 # - msg is the rest of commit log message
386 # - patch is the patch body.
388 # When we are resuming, these files are either already prepared
389 # by the user, or the user can tell us to do so by --resolved flag.
392 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
393 <"$dotest/$msgnum" >"$dotest/info" ||
396 # skip pine's internal folder data
397 grep '^Author: Mail System Internal Data$' \
398 <"$dotest"/info >/dev/null &&
401 test -s "$dotest/patch" || {
402 echo "Patch is empty. Was it split wrong?"
405 if test -f "$dotest/rebasing" &&
406 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
407 -e q "$dotest/$msgnum") &&
408 test "$(git cat-file -t "$commit")" = commit
410 git cat-file commit "$commit" |
411 sed -e '1,/^$/d' >"$dotest/msg-clean"
413 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
414 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
416 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
417 git stripspace > "$dotest/msg-clean"
422 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
423 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
424 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
426 if test -z "$GIT_AUTHOR_EMAIL"
428 echo "Patch does not have a valid e-mail address."
432 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
436 if test '' != "$SIGNOFF"
439 sed -ne '/^Signed-off-by: /p' \
440 "$dotest/msg-clean" |
444 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
445 test '' = "$LAST_SIGNED_OFF_BY" && echo
452 if test -s "$dotest/msg-clean"
454 cat "$dotest/msg-clean"
456 if test '' != "$ADD_SIGNOFF"
460 } >"$dotest/final-commit"
463 case "$resolved$interactive" in
465 # This is used only for interactive view option.
466 git diff-index -p --cached HEAD -- >"$dotest/patch"
472 if test "$interactive" = t
475 die "cannot be interactive without stdin connected to a terminal."
477 while test "$action" = again
479 echo "Commit Body is:"
480 echo "--------------------------"
481 cat "$dotest/final-commit"
482 echo "--------------------------"
483 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
487 [aA]*) action=yes interactive= ;;
488 [nN]*) action=skip ;;
489 [eE]*) git_editor "$dotest/final-commit"
492 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
499 FIRSTLINE=$(sed 1q "$dotest/final-commit")
501 if test $action = skip
507 if test -x "$GIT_DIR"/hooks/applypatch-msg
509 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
513 say "Applying: $FIRSTLINE"
517 # When we are allowed to fall back to 3-way later, don't give
518 # false errors during the initial attempt.
520 if test "$threeway" = t
522 squelch='>/dev/null 2>&1 '
524 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
528 # Resolved means the user did all the hard work, and
529 # we do not have to do any patch application. Just
530 # trust what the user has in the index file and the
533 git diff-index --quiet --cached HEAD -- && {
534 echo "No changes - did you forget to use 'git add'?"
535 stop_here_user_resolve $this
537 unmerged=$(git ls-files -u)
538 if test -n "$unmerged"
540 echo "You still have unmerged paths in your index"
541 echo "did you forget to use 'git add'?"
542 stop_here_user_resolve $this
549 if test $apply_status = 1 && test "$threeway" = t
553 # Applying the patch to an earlier tree and merging the
554 # result may have produced the same tree as ours.
555 git diff-index --quiet --cached HEAD -- && {
556 say No changes -- Patch already applied.
560 # clear apply_status -- we have successfully merged.
564 if test $apply_status != 0
566 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
567 stop_here_user_resolve $this
570 if test -x "$GIT_DIR"/hooks/pre-applypatch
572 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
575 tree=$(git write-tree) &&
577 if test -n "$ignore_date"
581 parent=$(git rev-parse --verify -q HEAD) ||
582 say >&2 "applying to an empty history"
584 if test -n "$committer_date_is_author_date"
586 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
587 export GIT_COMMITTER_DATE
589 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
591 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
594 if test -x "$GIT_DIR"/hooks/post-applypatch
596 "$GIT_DIR"/hooks/post-applypatch