3 # Copyright (c) 2005, 2006 Junio C Hamano
8 git am [options] [<mbox>|<Maildir>...]
9 git am [options] --resolved
10 git am [options] --skip
12 d,dotest= (removed -- do not use)
13 i,interactive run interactively
14 b,binary pass --allow-binary-replacement to git-apply
15 3,3way allow fall back on 3way merging if needed
16 s,signoff add a Signed-off-by line to the commit message
17 u,utf8 recode into utf8 (default)
18 k,keep pass -k flag to git-mailinfo
19 whitespace= pass it through git-apply
20 C= pass it through git-apply
21 p= 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 rebasing (internal use for git-rebase)"
29 prefix=$(git rev-parse --show-prefix)
34 git var GIT_COMMITTER_IDENT >/dev/null ||
35 die "You need to set your committer info first"
38 echo "$1" >"$dotest/next"
42 stop_here_user_resolve () {
43 if [ -n "$resolvemsg" ]; then
44 printf '%s\n' "$resolvemsg"
48 if test '' != "$interactive"
52 if test '' != "$threeway"
56 echo "When you have resolved this problem run \"$cmdline --resolved\"."
57 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
58 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
64 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
65 "$dotest/patch" "$dotest/info"
66 echo "$next" >"$dotest/next"
72 echo "Cannot fall back to three-way merge."
77 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
79 rm -fr "$dotest"/patch-merge-*
80 mkdir "$dotest/patch-merge-tmp-dir"
82 # First see if the patch records the index info that we can use.
83 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
85 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
86 git write-tree >"$dotest/patch-merge-base+" ||
87 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
89 echo Using index info to reconstruct a base tree...
90 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
91 git apply $binary --cached <"$dotest/patch"
93 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
94 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
96 cannot_fallback "Did you hand edit your patch?
97 It does not apply to blobs recorded in its index."
100 test -f "$dotest/patch-merge-index" &&
101 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
102 orig_tree=$(cat "$dotest/patch-merge-base") &&
103 rm -fr "$dotest"/patch-merge-* || exit 1
105 echo Falling back to patching base and 3-way merge...
107 # This is not so wrong. Depending on which base we picked,
108 # orig_tree may be wildly different from ours, but his_tree
109 # has the same set of wildly different changes in parts the
110 # patch did not touch, so recursive ends up canceling them,
111 # saying that we reverted all those changes.
113 eval GITHEAD_$his_tree='"$FIRSTLINE"'
114 export GITHEAD_$his_tree
115 git-merge-recursive $orig_tree -- HEAD $his_tree || {
117 echo Failed to merge in the changes.
120 unset GITHEAD_$his_tree
124 dotest="$GIT_DIR/rebase-apply"
125 sign= utf8=t keep= skip= interactive= resolved= binary= rebasing= abort=
141 utf8=t ;; # this is now default
153 rebasing=t threeway=t keep=t binary=t ;;
155 die "-d option is no longer supported. Do not use."
158 shift; resolvemsg=$1 ;;
160 git_apply_opt="$git_apply_opt $1=$2"; shift ;;
162 git_apply_opt="$git_apply_opt $1$2"; shift ;;
171 # If the dotest directory exists, but we have finished applying all the
172 # patches in them, clear it out.
173 if test -d "$dotest" &&
174 last=$(cat "$dotest/last") &&
175 next=$(cat "$dotest/next") &&
177 test "$next" -gt "$last"
184 case "$#,$skip$resolved$abort" in
186 # Explicit resume command and we do not have file, so
190 # No file input but without resume parameters; catch
191 # user error to feed us a patch from standard input
192 # when there is already $dotest. This is somewhat
193 # unreliable -- stdin could be /dev/null for example
194 # and the caller did not intend to feed us a patch but
195 # wanted to continue unattended.
202 die "previous rebase directory $dotest still exists but mbox given."
205 case "$skip,$abort" in
208 git read-tree --reset -u HEAD HEAD
209 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
211 git update-ref ORIG_HEAD $orig_head
215 git read-tree --reset -u HEAD ORIG_HEAD
221 # Make sure we are not given --skip, --resolved, nor --abort
222 test "$skip$resolved$abort" = "" ||
223 die "Resolve operation not in progress, we are not resuming."
226 mkdir -p "$dotest" || exit
228 if test -n "$prefix" && test $# != 0
233 test -n "$first" && {
241 set "$@" "$prefix$arg" ;;
246 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
251 # -b, -s, -u, -k and --whitespace flags are kept for the
252 # resuming session after a patch failure.
253 # -3 and -i can and must be given when resuming.
254 echo "$binary" >"$dotest/binary"
255 echo " $ws" >"$dotest/whitespace"
256 echo "$sign" >"$dotest/sign"
257 echo "$utf8" >"$dotest/utf8"
258 echo "$keep" >"$dotest/keep"
259 echo 1 >"$dotest/next"
260 if test -n "$rebasing"
262 : >"$dotest/rebasing"
264 : >"$dotest/applying"
265 git update-ref ORIG_HEAD HEAD
271 files=$(git diff-index --cached --name-only HEAD --) || exit
272 if [ "$files" ]; then
273 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
278 if test "$(cat "$dotest/binary")" = t
280 binary=--allow-binary-replacement
282 if test "$(cat "$dotest/utf8")" = t
288 if test "$(cat "$dotest/keep")" = t
292 ws=`cat "$dotest/whitespace"`
293 if test "$(cat "$dotest/sign")" = t
295 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
297 s/^/Signed-off-by: /'
303 last=`cat "$dotest/last"`
304 this=`cat "$dotest/next"`
307 this=`expr "$this" + 1`
311 if test "$this" -gt "$last"
318 while test "$this" -le "$last"
320 msgnum=`printf "%0${prec}d" $this`
321 next=`expr "$this" + 1`
322 test -f "$dotest/$msgnum" || {
328 # If we are not resuming, parse and extract the patch information
329 # into separate files:
330 # - info records the authorship and title
331 # - msg is the rest of commit log message
332 # - patch is the patch body.
334 # When we are resuming, these files are either already prepared
335 # by the user, or the user can tell us to do so by --resolved flag.
338 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
339 <"$dotest/$msgnum" >"$dotest/info" ||
342 # skip pine's internal folder data
343 grep '^Author: Mail System Internal Data$' \
344 <"$dotest"/info >/dev/null &&
347 test -s "$dotest/patch" || {
348 echo "Patch is empty. Was it split wrong?"
351 if test -f "$dotest/rebasing" &&
352 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
353 -e q "$dotest/$msgnum") &&
354 test "$(git cat-file -t "$commit")" = commit
356 git cat-file commit "$commit" |
357 sed -e '1,/^$/d' >"$dotest/msg-clean"
359 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
360 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
362 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
363 git stripspace > "$dotest/msg-clean"
368 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
369 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
370 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
372 if test -z "$GIT_AUTHOR_EMAIL"
374 echo "Patch does not have a valid e-mail address."
378 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
382 if test '' != "$SIGNOFF"
385 sed -ne '/^Signed-off-by: /p' \
386 "$dotest/msg-clean" |
390 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
391 test '' = "$LAST_SIGNED_OFF_BY" && echo
398 if test -s "$dotest/msg-clean"
400 cat "$dotest/msg-clean"
402 if test '' != "$ADD_SIGNOFF"
406 } >"$dotest/final-commit"
409 case "$resolved$interactive" in
411 # This is used only for interactive view option.
412 git diff-index -p --cached HEAD -- >"$dotest/patch"
418 if test "$interactive" = t
421 die "cannot be interactive without stdin connected to a terminal."
423 while test "$action" = again
425 echo "Commit Body is:"
426 echo "--------------------------"
427 cat "$dotest/final-commit"
428 echo "--------------------------"
429 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
433 [aA]*) action=yes interactive= ;;
434 [nN]*) action=skip ;;
435 [eE]*) git_editor "$dotest/final-commit"
438 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
445 FIRSTLINE=$(sed 1q "$dotest/final-commit")
447 if test $action = skip
453 if test -x "$GIT_DIR"/hooks/applypatch-msg
455 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
459 printf 'Applying: %s\n' "$FIRSTLINE"
463 git apply $git_apply_opt $binary --index "$dotest/patch"
467 # Resolved means the user did all the hard work, and
468 # we do not have to do any patch application. Just
469 # trust what the user has in the index file and the
472 git diff-index --quiet --cached HEAD -- && {
473 echo "No changes - did you forget to use 'git add'?"
474 stop_here_user_resolve $this
476 unmerged=$(git ls-files -u)
477 if test -n "$unmerged"
479 echo "You still have unmerged paths in your index"
480 echo "did you forget to use 'git add'?"
481 stop_here_user_resolve $this
488 if test $apply_status = 1 && test "$threeway" = t
492 # Applying the patch to an earlier tree and merging the
493 # result may have produced the same tree as ours.
494 git diff-index --quiet --cached HEAD -- && {
495 echo No changes -- Patch already applied.
499 # clear apply_status -- we have successfully merged.
503 if test $apply_status != 0
505 echo Patch failed at $msgnum.
506 stop_here_user_resolve $this
509 if test -x "$GIT_DIR"/hooks/pre-applypatch
511 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
514 tree=$(git write-tree) &&
515 parent=$(git rev-parse --verify HEAD) &&
516 commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
517 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
520 if test -x "$GIT_DIR"/hooks/post-applypatch
522 "$GIT_DIR"/hooks/post-applypatch