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
14 s,signoff add a Signed-off-by line to the commit message
15 u,utf8 recode into utf8 (default)
16 k,keep pass -k flag to git-mailinfo
17 whitespace= pass it through git-apply
18 directory= pass it through git-apply
19 C= pass it through git-apply
20 p= pass it through git-apply
21 reject pass it through git-apply
22 resolvemsg= override error message when patch failure occurs
23 r,resolved to be used after a patch failure
24 skip skip the current patch
25 abort restore the original branch and abort the patching operation.
26 committer-date-is-author-date lie about committer date
27 ignore-date use current timestamp for author date
28 rebasing* (internal use for git-rebase)"
31 prefix=$(git rev-parse --show-prefix)
36 git var GIT_COMMITTER_IDENT >/dev/null ||
37 die "You need to set your committer info first"
39 if git rev-parse --verify -q HEAD >/dev/null
49 printf "%s" "$sqarg" |
50 sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
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 echo 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 echo 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 git-merge-recursive $orig_tree -- HEAD $his_tree || {
134 echo Failed to merge in the changes.
137 unset GITHEAD_$his_tree
141 dotest="$GIT_DIR/rebase-apply"
142 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
145 committer_date_is_author_date=
160 utf8=t ;; # this is now default
172 rebasing=t threeway=t keep=t ;;
174 die "-d option is no longer supported. Do not use."
177 shift; resolvemsg=$1 ;;
178 --whitespace|--directory)
179 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
181 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
183 git_apply_opt="$git_apply_opt $1" ;;
184 --committer-date-is-author-date)
185 committer_date_is_author_date=t ;;
196 # If the dotest directory exists, but we have finished applying all the
197 # patches in them, clear it out.
198 if test -d "$dotest" &&
199 last=$(cat "$dotest/last") &&
200 next=$(cat "$dotest/next") &&
202 test "$next" -gt "$last"
209 case "$#,$skip$resolved$abort" in
211 # Explicit resume command and we do not have file, so
215 # No file input but without resume parameters; catch
216 # user error to feed us a patch from standard input
217 # when there is already $dotest. This is somewhat
218 # unreliable -- stdin could be /dev/null for example
219 # and the caller did not intend to feed us a patch but
220 # wanted to continue unattended.
227 die "previous rebase directory $dotest still exists but mbox given."
230 case "$skip,$abort" in
232 die "Please make up your mind. --skip or --abort?"
236 git read-tree --reset -u HEAD HEAD
237 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
239 git update-ref ORIG_HEAD $orig_head
242 if test -f "$dotest/rebasing"
244 exec git rebase --abort
247 test -f "$dotest/dirtyindex" || {
248 git read-tree --reset -u HEAD ORIG_HEAD
254 rm -f "$dotest/dirtyindex"
256 # Make sure we are not given --skip, --resolved, nor --abort
257 test "$skip$resolved$abort" = "" ||
258 die "Resolve operation not in progress, we are not resuming."
261 mkdir -p "$dotest" || exit
263 if test -n "$prefix" && test $# != 0
268 test -n "$first" && {
276 set "$@" "$prefix$arg" ;;
281 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
286 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
287 # for the resuming session after a patch failure.
288 # -i can and must be given when resuming.
289 echo " $git_apply_opt" >"$dotest/apply-opt"
290 echo "$threeway" >"$dotest/threeway"
291 echo "$sign" >"$dotest/sign"
292 echo "$utf8" >"$dotest/utf8"
293 echo "$keep" >"$dotest/keep"
294 echo 1 >"$dotest/next"
295 if test -n "$rebasing"
297 : >"$dotest/rebasing"
299 : >"$dotest/applying"
300 if test -n "$HAS_HEAD"
302 git update-ref ORIG_HEAD HEAD
304 git update-ref -d ORIG_HEAD >/dev/null 2>&1
313 files=$(git ls-files) ;;
315 files=$(git diff-index --cached --name-only HEAD --) ;;
319 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
320 die "Dirty index: cannot apply patches (dirty: $files)"
324 if test "$(cat "$dotest/utf8")" = t
330 if test "$(cat "$dotest/keep")" = t
334 if test "$(cat "$dotest/threeway")" = t
338 git_apply_opt=$(cat "$dotest/apply-opt")
339 if test "$(cat "$dotest/sign")" = t
341 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
343 s/^/Signed-off-by: /'
349 last=`cat "$dotest/last"`
350 this=`cat "$dotest/next"`
353 this=`expr "$this" + 1`
357 if test "$this" -gt "$last"
364 while test "$this" -le "$last"
366 msgnum=`printf "%0${prec}d" $this`
367 next=`expr "$this" + 1`
368 test -f "$dotest/$msgnum" || {
374 # If we are not resuming, parse and extract the patch information
375 # into separate files:
376 # - info records the authorship and title
377 # - msg is the rest of commit log message
378 # - patch is the patch body.
380 # When we are resuming, these files are either already prepared
381 # by the user, or the user can tell us to do so by --resolved flag.
384 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
385 <"$dotest/$msgnum" >"$dotest/info" ||
388 # skip pine's internal folder data
389 grep '^Author: Mail System Internal Data$' \
390 <"$dotest"/info >/dev/null &&
393 test -s "$dotest/patch" || {
394 echo "Patch is empty. Was it split wrong?"
397 if test -f "$dotest/rebasing" &&
398 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
399 -e q "$dotest/$msgnum") &&
400 test "$(git cat-file -t "$commit")" = commit
402 git cat-file commit "$commit" |
403 sed -e '1,/^$/d' >"$dotest/msg-clean"
405 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
406 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
408 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
409 git stripspace > "$dotest/msg-clean"
414 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
415 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
416 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
418 if test -z "$GIT_AUTHOR_EMAIL"
420 echo "Patch does not have a valid e-mail address."
424 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
428 if test '' != "$SIGNOFF"
431 sed -ne '/^Signed-off-by: /p' \
432 "$dotest/msg-clean" |
436 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
437 test '' = "$LAST_SIGNED_OFF_BY" && echo
444 if test -s "$dotest/msg-clean"
446 cat "$dotest/msg-clean"
448 if test '' != "$ADD_SIGNOFF"
452 } >"$dotest/final-commit"
455 case "$resolved$interactive" in
457 # This is used only for interactive view option.
458 git diff-index -p --cached HEAD -- >"$dotest/patch"
464 if test "$interactive" = t
467 die "cannot be interactive without stdin connected to a terminal."
469 while test "$action" = again
471 echo "Commit Body is:"
472 echo "--------------------------"
473 cat "$dotest/final-commit"
474 echo "--------------------------"
475 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
479 [aA]*) action=yes interactive= ;;
480 [nN]*) action=skip ;;
481 [eE]*) git_editor "$dotest/final-commit"
484 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
491 FIRSTLINE=$(sed 1q "$dotest/final-commit")
493 if test $action = skip
499 if test -x "$GIT_DIR"/hooks/applypatch-msg
501 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
505 printf 'Applying: %s\n' "$FIRSTLINE"
509 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
513 # Resolved means the user did all the hard work, and
514 # we do not have to do any patch application. Just
515 # trust what the user has in the index file and the
518 git diff-index --quiet --cached HEAD -- && {
519 echo "No changes - did you forget to use 'git add'?"
520 stop_here_user_resolve $this
522 unmerged=$(git ls-files -u)
523 if test -n "$unmerged"
525 echo "You still have unmerged paths in your index"
526 echo "did you forget to use 'git add'?"
527 stop_here_user_resolve $this
534 if test $apply_status = 1 && test "$threeway" = t
538 # Applying the patch to an earlier tree and merging the
539 # result may have produced the same tree as ours.
540 git diff-index --quiet --cached HEAD -- && {
541 echo No changes -- Patch already applied.
545 # clear apply_status -- we have successfully merged.
549 if test $apply_status != 0
551 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
552 stop_here_user_resolve $this
555 if test -x "$GIT_DIR"/hooks/pre-applypatch
557 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
560 tree=$(git write-tree) &&
562 if test -n "$ignore_date"
566 parent=$(git rev-parse --verify -q HEAD) ||
567 echo >&2 "applying to an empty history"
569 if test -n "$committer_date_is_author_date"
571 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
572 export GIT_COMMITTER_DATE
574 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
576 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
579 if test -x "$GIT_DIR"/hooks/post-applypatch
581 "$GIT_DIR"/hooks/post-applypatch