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 C= pass it through git-apply
20 p= pass it through git-apply
21 resolvemsg= override error message when patch failure occurs
22 r,resolved to be used after a patch failure
23 skip skip the current patch
24 abort restore the original branch and abort the patching operation.
25 rebasing (internal use for git-rebase)"
28 prefix
=$
(git rev-parse
--show-prefix)
33 git var GIT_COMMITTER_IDENT
>/dev
/null ||
34 die
"You need to set your committer info first"
37 echo "$1" >"$dotest/next"
41 stop_here_user_resolve
() {
42 if [ -n "$resolvemsg" ]; then
43 printf '%s\n' "$resolvemsg"
47 if test '' != "$interactive"
51 if test '' != "$threeway"
55 echo "When you have resolved this problem run \"$cmdline --resolved\"."
56 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
57 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
63 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
64 "$dotest/patch" "$dotest/info"
65 echo "$next" >"$dotest/next"
71 echo "Cannot fall back to three-way merge."
76 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
78 rm -fr "$dotest"/patch-merge-
*
79 mkdir
"$dotest/patch-merge-tmp-dir"
81 # First see if the patch records the index info that we can use.
82 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
84 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
85 git write-tree
>"$dotest/patch-merge-base+" ||
86 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
88 echo Using index info to reconstruct a base tree...
89 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
90 git apply
--cached <"$dotest/patch"
92 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
93 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
95 cannot_fallback
"Did you hand edit your patch?
96 It does not apply to blobs recorded in its index."
99 test -f "$dotest/patch-merge-index" &&
100 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
101 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
102 rm -fr "$dotest"/patch-merge-
* ||
exit 1
104 echo Falling back to patching base and
3-way merge...
106 # This is not so wrong. Depending on which base we picked,
107 # orig_tree may be wildly different from ours, but his_tree
108 # has the same set of wildly different changes in parts the
109 # patch did not touch, so recursive ends up canceling them,
110 # saying that we reverted all those changes.
112 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
113 export GITHEAD_
$his_tree
114 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
116 echo Failed to merge
in the changes.
119 unset GITHEAD_
$his_tree
123 dotest
="$GIT_DIR/rebase-apply"
124 sign
= utf8
=t keep
= skip
= interactive
= resolved
= rebasing
= abort
= ws
=
140 utf8
=t
;; # this is now default
152 rebasing
=t threeway
=t keep
=t
;;
154 die
"-d option is no longer supported. Do not use."
157 shift; resolvemsg
=$1 ;;
159 ws
="--whitespace=$2"; shift ;;
161 git_apply_opt
="$git_apply_opt $1$2"; shift ;;
170 # If the dotest directory exists, but we have finished applying all the
171 # patches in them, clear it out.
172 if test -d "$dotest" &&
173 last
=$
(cat "$dotest/last") &&
174 next
=$
(cat "$dotest/next") &&
176 test "$next" -gt "$last"
183 case "$#,$skip$resolved$abort" in
185 # Explicit resume command and we do not have file, so
189 # No file input but without resume parameters; catch
190 # user error to feed us a patch from standard input
191 # when there is already $dotest. This is somewhat
192 # unreliable -- stdin could be /dev/null for example
193 # and the caller did not intend to feed us a patch but
194 # wanted to continue unattended.
201 die
"previous rebase directory $dotest still exists but mbox given."
204 case "$skip,$abort" in
207 git read-tree
--reset -u HEAD HEAD
208 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
210 git update-ref ORIG_HEAD
$orig_head
214 git read-tree
--reset -u HEAD ORIG_HEAD
220 # Make sure we are not given --skip, --resolved, nor --abort
221 test "$skip$resolved$abort" = "" ||
222 die
"Resolve operation not in progress, we are not resuming."
225 mkdir
-p "$dotest" ||
exit
227 if test -n "$prefix" && test $# != 0
232 test -n "$first" && {
240 set "$@" "$prefix$arg" ;;
245 git mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
250 # -s, -u, -k and --whitespace flags are kept for the
251 # resuming session after a patch failure.
252 # -3 and -i can and must be given when resuming.
253 echo " $ws" >"$dotest/whitespace"
254 echo "$sign" >"$dotest/sign"
255 echo "$utf8" >"$dotest/utf8"
256 echo "$keep" >"$dotest/keep"
257 echo 1 >"$dotest/next"
258 if test -n "$rebasing"
260 : >"$dotest/rebasing"
262 : >"$dotest/applying"
263 git update-ref ORIG_HEAD HEAD
269 files
=$
(git diff-index
--cached --name-only HEAD
--) ||
exit
270 if [ "$files" ]; then
271 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
276 if test "$(cat "$dotest/utf8
")" = t
282 if test "$(cat "$dotest/keep
")" = t
286 ws
=$
(cat "$dotest/whitespace")
287 if test "$(cat "$dotest/sign
")" = t
289 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
291 s/^/Signed-off-by: /'
297 last
=`cat "$dotest/last"`
298 this
=`cat "$dotest/next"`
301 this
=`expr "$this" + 1`
305 if test "$this" -gt "$last"
312 while test "$this" -le "$last"
314 msgnum
=`printf "%0${prec}d" $this`
315 next
=`expr "$this" + 1`
316 test -f "$dotest/$msgnum" ||
{
322 # If we are not resuming, parse and extract the patch information
323 # into separate files:
324 # - info records the authorship and title
325 # - msg is the rest of commit log message
326 # - patch is the patch body.
328 # When we are resuming, these files are either already prepared
329 # by the user, or the user can tell us to do so by --resolved flag.
332 git mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
333 <"$dotest/$msgnum" >"$dotest/info" ||
336 # skip pine's internal folder data
337 grep '^Author: Mail System Internal Data$' \
338 <"$dotest"/info
>/dev
/null
&&
341 test -s "$dotest/patch" ||
{
342 echo "Patch is empty. Was it split wrong?"
345 if test -f "$dotest/rebasing" &&
346 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
347 -e q
"$dotest/$msgnum") &&
348 test "$(git cat-file -t "$commit")" = commit
350 git cat-file commit
"$commit" |
351 sed -e '1,/^$/d' >"$dotest/msg-clean"
353 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
354 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
356 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
357 git stripspace
> "$dotest/msg-clean"
362 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
363 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
364 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
366 if test -z "$GIT_AUTHOR_EMAIL"
368 echo "Patch does not have a valid e-mail address."
372 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
376 if test '' != "$SIGNOFF"
379 sed -ne '/^Signed-off-by: /p' \
380 "$dotest/msg-clean" |
384 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
385 test '' = "$LAST_SIGNED_OFF_BY" && echo
392 if test -s "$dotest/msg-clean"
394 cat "$dotest/msg-clean"
396 if test '' != "$ADD_SIGNOFF"
400 } >"$dotest/final-commit"
403 case "$resolved$interactive" in
405 # This is used only for interactive view option.
406 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
412 if test "$interactive" = t
415 die
"cannot be interactive without stdin connected to a terminal."
417 while test "$action" = again
419 echo "Commit Body is:"
420 echo "--------------------------"
421 cat "$dotest/final-commit"
422 echo "--------------------------"
423 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
427 [aA
]*) action
=yes interactive
= ;;
428 [nN
]*) action
=skip
;;
429 [eE
]*) git_editor
"$dotest/final-commit"
432 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
439 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
441 if test $action = skip
447 if test -x "$GIT_DIR"/hooks
/applypatch-msg
449 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
453 printf 'Applying: %s\n' "$FIRSTLINE"
457 git apply
$git_apply_opt $ws --index "$dotest/patch"
461 # Resolved means the user did all the hard work, and
462 # we do not have to do any patch application. Just
463 # trust what the user has in the index file and the
466 git diff-index
--quiet --cached HEAD
-- && {
467 echo "No changes - did you forget to use 'git add'?"
468 stop_here_user_resolve
$this
470 unmerged
=$
(git ls-files
-u)
471 if test -n "$unmerged"
473 echo "You still have unmerged paths in your index"
474 echo "did you forget to use 'git add'?"
475 stop_here_user_resolve
$this
482 if test $apply_status = 1 && test "$threeway" = t
486 # Applying the patch to an earlier tree and merging the
487 # result may have produced the same tree as ours.
488 git diff-index
--quiet --cached HEAD
-- && {
489 echo No changes
-- Patch already applied.
493 # clear apply_status -- we have successfully merged.
497 if test $apply_status != 0
499 echo Patch failed
at $msgnum.
500 stop_here_user_resolve
$this
503 if test -x "$GIT_DIR"/hooks
/pre-applypatch
505 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
508 tree
=$
(git write-tree
) &&
509 parent
=$
(git rev-parse
--verify HEAD
) &&
510 commit
=$
(git commit-tree
$tree -p $parent <"$dotest/final-commit") &&
511 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
514 if test -x "$GIT_DIR"/hooks
/post-applypatch
516 "$GIT_DIR"/hooks
/post-applypatch