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"
42 printf "%s" "$sqarg" |
43 sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
48 echo "$1" >"$dotest/next"
52 stop_here_user_resolve () {
53 if [ -n "$resolvemsg" ]; then
54 printf '%s\n' "$resolvemsg"
58 if test '' != "$interactive"
62 if test '' != "$threeway"
66 echo "When you have resolved this problem run \"$cmdline --resolved\"."
67 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
68 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
74 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
75 "$dotest/patch" "$dotest/info"
76 echo "$next" >"$dotest/next"
82 echo "Cannot fall back to three-way merge."
87 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
89 rm -fr "$dotest"/patch-merge-*
90 mkdir "$dotest/patch-merge-tmp-dir"
92 # First see if the patch records the index info that we can use.
93 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
95 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
96 git write-tree >"$dotest/patch-merge-base+" ||
97 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
99 echo Using index info to reconstruct a base tree...
100 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
101 git apply --cached <"$dotest/patch"
103 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
104 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
106 cannot_fallback "Did you hand edit your patch?
107 It does not apply to blobs recorded in its index."
110 test -f "$dotest/patch-merge-index" &&
111 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
112 orig_tree=$(cat "$dotest/patch-merge-base") &&
113 rm -fr "$dotest"/patch-merge-* || exit 1
115 echo Falling back to patching base and 3-way merge...
117 # This is not so wrong. Depending on which base we picked,
118 # orig_tree may be wildly different from ours, but his_tree
119 # has the same set of wildly different changes in parts the
120 # patch did not touch, so recursive ends up canceling them,
121 # saying that we reverted all those changes.
123 eval GITHEAD_$his_tree='"$FIRSTLINE"'
124 export GITHEAD_$his_tree
125 git-merge-recursive $orig_tree -- HEAD $his_tree || {
127 echo Failed to merge in the changes.
130 unset GITHEAD_$his_tree
134 dotest="$GIT_DIR/rebase-apply"
135 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
138 committer_date_is_author_date=
153 utf8=t ;; # this is now default
165 rebasing=t threeway=t keep=t ;;
167 die "-d option is no longer supported. Do not use."
170 shift; resolvemsg=$1 ;;
171 --whitespace|--directory)
172 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
174 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
176 git_apply_opt="$git_apply_opt $1" ;;
177 --committer-date-is-author-date)
178 committer_date_is_author_date=t ;;
189 # If the dotest directory exists, but we have finished applying all the
190 # patches in them, clear it out.
191 if test -d "$dotest" &&
192 last=$(cat "$dotest/last") &&
193 next=$(cat "$dotest/next") &&
195 test "$next" -gt "$last"
202 case "$#,$skip$resolved$abort" in
204 # Explicit resume command and we do not have file, so
208 # No file input but without resume parameters; catch
209 # user error to feed us a patch from standard input
210 # when there is already $dotest. This is somewhat
211 # unreliable -- stdin could be /dev/null for example
212 # and the caller did not intend to feed us a patch but
213 # wanted to continue unattended.
220 die "previous rebase directory $dotest still exists but mbox given."
223 case "$skip,$abort" in
225 die "Please make up your mind. --skip or --abort?"
229 git read-tree --reset -u HEAD HEAD
230 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
232 git update-ref ORIG_HEAD $orig_head
235 if test -f "$dotest/rebasing"
237 exec git rebase --abort
240 test -f "$dotest/dirtyindex" || {
241 git read-tree --reset -u HEAD ORIG_HEAD
247 rm -f "$dotest/dirtyindex"
249 # Make sure we are not given --skip, --resolved, nor --abort
250 test "$skip$resolved$abort" = "" ||
251 die "Resolve operation not in progress, we are not resuming."
254 mkdir -p "$dotest" || exit
256 if test -n "$prefix" && test $# != 0
261 test -n "$first" && {
269 set "$@" "$prefix$arg" ;;
274 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
279 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
280 # for the resuming session after a patch failure.
281 # -i can and must be given when resuming.
282 echo " $git_apply_opt" >"$dotest/apply-opt"
283 echo "$threeway" >"$dotest/threeway"
284 echo "$sign" >"$dotest/sign"
285 echo "$utf8" >"$dotest/utf8"
286 echo "$keep" >"$dotest/keep"
287 echo 1 >"$dotest/next"
288 if test -n "$rebasing"
290 : >"$dotest/rebasing"
292 : >"$dotest/applying"
293 git update-ref ORIG_HEAD HEAD
299 files=$(git diff-index --cached --name-only HEAD --) || exit
302 : >"$dotest/dirtyindex"
303 die "Dirty index: cannot apply patches (dirty: $files)"
307 if test "$(cat "$dotest/utf8")" = t
313 if test "$(cat "$dotest/keep")" = t
317 if test "$(cat "$dotest/threeway")" = t
321 git_apply_opt=$(cat "$dotest/apply-opt")
322 if test "$(cat "$dotest/sign")" = t
324 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
326 s/^/Signed-off-by: /'
332 last=`cat "$dotest/last"`
333 this=`cat "$dotest/next"`
336 this=`expr "$this" + 1`
340 if test "$this" -gt "$last"
347 while test "$this" -le "$last"
349 msgnum=`printf "%0${prec}d" $this`
350 next=`expr "$this" + 1`
351 test -f "$dotest/$msgnum" || {
357 # If we are not resuming, parse and extract the patch information
358 # into separate files:
359 # - info records the authorship and title
360 # - msg is the rest of commit log message
361 # - patch is the patch body.
363 # When we are resuming, these files are either already prepared
364 # by the user, or the user can tell us to do so by --resolved flag.
367 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
368 <"$dotest/$msgnum" >"$dotest/info" ||
371 # skip pine's internal folder data
372 grep '^Author: Mail System Internal Data$' \
373 <"$dotest"/info >/dev/null &&
376 test -s "$dotest/patch" || {
377 echo "Patch is empty. Was it split wrong?"
380 if test -f "$dotest/rebasing" &&
381 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
382 -e q "$dotest/$msgnum") &&
383 test "$(git cat-file -t "$commit")" = commit
385 git cat-file commit "$commit" |
386 sed -e '1,/^$/d' >"$dotest/msg-clean"
388 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
389 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
391 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
392 git stripspace > "$dotest/msg-clean"
397 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
398 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
399 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
401 if test -z "$GIT_AUTHOR_EMAIL"
403 echo "Patch does not have a valid e-mail address."
407 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
411 if test '' != "$SIGNOFF"
414 sed -ne '/^Signed-off-by: /p' \
415 "$dotest/msg-clean" |
419 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
420 test '' = "$LAST_SIGNED_OFF_BY" && echo
427 if test -s "$dotest/msg-clean"
429 cat "$dotest/msg-clean"
431 if test '' != "$ADD_SIGNOFF"
435 } >"$dotest/final-commit"
438 case "$resolved$interactive" in
440 # This is used only for interactive view option.
441 git diff-index -p --cached HEAD -- >"$dotest/patch"
447 if test "$interactive" = t
450 die "cannot be interactive without stdin connected to a terminal."
452 while test "$action" = again
454 echo "Commit Body is:"
455 echo "--------------------------"
456 cat "$dotest/final-commit"
457 echo "--------------------------"
458 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
462 [aA]*) action=yes interactive= ;;
463 [nN]*) action=skip ;;
464 [eE]*) git_editor "$dotest/final-commit"
467 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
474 FIRSTLINE=$(sed 1q "$dotest/final-commit")
476 if test $action = skip
482 if test -x "$GIT_DIR"/hooks/applypatch-msg
484 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
488 printf 'Applying: %s\n' "$FIRSTLINE"
492 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
496 # Resolved means the user did all the hard work, and
497 # we do not have to do any patch application. Just
498 # trust what the user has in the index file and the
501 git diff-index --quiet --cached HEAD -- && {
502 echo "No changes - did you forget to use 'git add'?"
503 stop_here_user_resolve $this
505 unmerged=$(git ls-files -u)
506 if test -n "$unmerged"
508 echo "You still have unmerged paths in your index"
509 echo "did you forget to use 'git add'?"
510 stop_here_user_resolve $this
517 if test $apply_status = 1 && test "$threeway" = t
521 # Applying the patch to an earlier tree and merging the
522 # result may have produced the same tree as ours.
523 git diff-index --quiet --cached HEAD -- && {
524 echo No changes -- Patch already applied.
528 # clear apply_status -- we have successfully merged.
532 if test $apply_status != 0
534 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
535 stop_here_user_resolve $this
538 if test -x "$GIT_DIR"/hooks/pre-applypatch
540 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
543 tree=$(git write-tree) &&
544 parent=$(git rev-parse --verify HEAD) &&
546 if test -n "$ignore_date"
550 if test -n "$committer_date_is_author_date"
552 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
553 export GIT_COMMITTER_DATE
555 git commit-tree $tree -p $parent <"$dotest/final-commit"
557 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
560 if test -x "$GIT_DIR"/hooks/post-applypatch
562 "$GIT_DIR"/hooks/post-applypatch