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 directory= 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"
40 printf "%s" "$sqarg" |
41 sed -e 's/'\''/'\''\\'\'''\''/g' -e 's/.*/ '\''&'\''/'
46 echo "$1" >"$dotest/next"
50 stop_here_user_resolve
() {
51 if [ -n "$resolvemsg" ]; then
52 printf '%s\n' "$resolvemsg"
56 if test '' != "$interactive"
60 if test '' != "$threeway"
64 echo "When you have resolved this problem run \"$cmdline --resolved\"."
65 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
66 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
72 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
73 "$dotest/patch" "$dotest/info"
74 echo "$next" >"$dotest/next"
80 echo "Cannot fall back to three-way merge."
85 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
87 rm -fr "$dotest"/patch-merge-
*
88 mkdir
"$dotest/patch-merge-tmp-dir"
90 # First see if the patch records the index info that we can use.
91 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
93 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
94 git write-tree
>"$dotest/patch-merge-base+" ||
95 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
97 echo Using index info to reconstruct a base tree...
98 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
99 git apply
--cached <"$dotest/patch"
101 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
102 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
104 cannot_fallback
"Did you hand edit your patch?
105 It does not apply to blobs recorded in its index."
108 test -f "$dotest/patch-merge-index" &&
109 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
110 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
111 rm -fr "$dotest"/patch-merge-
* ||
exit 1
113 echo Falling back to patching base and
3-way merge...
115 # This is not so wrong. Depending on which base we picked,
116 # orig_tree may be wildly different from ours, but his_tree
117 # has the same set of wildly different changes in parts the
118 # patch did not touch, so recursive ends up canceling them,
119 # saying that we reverted all those changes.
121 eval GITHEAD_
$his_tree='"$FIRSTLINE"'
122 export GITHEAD_
$his_tree
123 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
125 echo Failed to merge
in the changes.
128 unset GITHEAD_
$his_tree
132 dotest
="$GIT_DIR/rebase-apply"
133 sign
= utf8
=t keep
= skip
= interactive
= resolved
= rebasing
= abort
=
149 utf8
=t
;; # this is now default
161 rebasing
=t threeway
=t keep
=t
;;
163 die
"-d option is no longer supported. Do not use."
166 shift; resolvemsg
=$1 ;;
167 --whitespace|
--directory)
168 git_apply_opt
="$git_apply_opt $(sq "$1=$2")"; shift ;;
170 git_apply_opt
="$git_apply_opt $(sq "$1$2")"; shift ;;
179 # If the dotest directory exists, but we have finished applying all the
180 # patches in them, clear it out.
181 if test -d "$dotest" &&
182 last
=$
(cat "$dotest/last") &&
183 next
=$
(cat "$dotest/next") &&
185 test "$next" -gt "$last"
192 case "$#,$skip$resolved$abort" in
194 # Explicit resume command and we do not have file, so
198 # No file input but without resume parameters; catch
199 # user error to feed us a patch from standard input
200 # when there is already $dotest. This is somewhat
201 # unreliable -- stdin could be /dev/null for example
202 # and the caller did not intend to feed us a patch but
203 # wanted to continue unattended.
210 die
"previous rebase directory $dotest still exists but mbox given."
213 case "$skip,$abort" in
216 git read-tree
--reset -u HEAD HEAD
217 orig_head
=$
(cat "$GIT_DIR/ORIG_HEAD")
219 git update-ref ORIG_HEAD
$orig_head
223 git read-tree
--reset -u HEAD ORIG_HEAD
229 # Make sure we are not given --skip, --resolved, nor --abort
230 test "$skip$resolved$abort" = "" ||
231 die
"Resolve operation not in progress, we are not resuming."
234 mkdir
-p "$dotest" ||
exit
236 if test -n "$prefix" && test $# != 0
241 test -n "$first" && {
249 set "$@" "$prefix$arg" ;;
254 git mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
259 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
260 # for the resuming session after a patch failure.
261 # -i can and must be given when resuming.
262 echo " $git_apply_opt" >"$dotest/apply-opt"
263 echo "$threeway" >"$dotest/threeway"
264 echo "$sign" >"$dotest/sign"
265 echo "$utf8" >"$dotest/utf8"
266 echo "$keep" >"$dotest/keep"
267 echo 1 >"$dotest/next"
268 if test -n "$rebasing"
270 : >"$dotest/rebasing"
272 : >"$dotest/applying"
273 git update-ref ORIG_HEAD HEAD
279 files
=$
(git diff-index
--cached --name-only HEAD
--) ||
exit
280 if [ "$files" ]; then
281 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
286 if test "$(cat "$dotest/utf8
")" = t
292 if test "$(cat "$dotest/keep
")" = t
296 if test "$(cat "$dotest/threeway
")" = t
300 git_apply_opt
=$
(cat "$dotest/apply-opt")
301 if test "$(cat "$dotest/sign
")" = t
303 SIGNOFF
=`git var GIT_COMMITTER_IDENT | sed -e '
305 s/^/Signed-off-by: /'
311 last
=`cat "$dotest/last"`
312 this
=`cat "$dotest/next"`
315 this
=`expr "$this" + 1`
319 if test "$this" -gt "$last"
326 while test "$this" -le "$last"
328 msgnum
=`printf "%0${prec}d" $this`
329 next
=`expr "$this" + 1`
330 test -f "$dotest/$msgnum" ||
{
336 # If we are not resuming, parse and extract the patch information
337 # into separate files:
338 # - info records the authorship and title
339 # - msg is the rest of commit log message
340 # - patch is the patch body.
342 # When we are resuming, these files are either already prepared
343 # by the user, or the user can tell us to do so by --resolved flag.
346 git mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
347 <"$dotest/$msgnum" >"$dotest/info" ||
350 # skip pine's internal folder data
351 grep '^Author: Mail System Internal Data$' \
352 <"$dotest"/info
>/dev
/null
&&
355 test -s "$dotest/patch" ||
{
356 echo "Patch is empty. Was it split wrong?"
359 if test -f "$dotest/rebasing" &&
360 commit
=$
(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
361 -e q
"$dotest/$msgnum") &&
362 test "$(git cat-file -t "$commit")" = commit
364 git cat-file commit
"$commit" |
365 sed -e '1,/^$/d' >"$dotest/msg-clean"
367 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
368 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
370 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
371 git stripspace
> "$dotest/msg-clean"
376 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
377 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
378 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
380 if test -z "$GIT_AUTHOR_EMAIL"
382 echo "Patch does not have a valid e-mail address."
386 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
390 if test '' != "$SIGNOFF"
393 sed -ne '/^Signed-off-by: /p' \
394 "$dotest/msg-clean" |
398 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
399 test '' = "$LAST_SIGNED_OFF_BY" && echo
406 if test -s "$dotest/msg-clean"
408 cat "$dotest/msg-clean"
410 if test '' != "$ADD_SIGNOFF"
414 } >"$dotest/final-commit"
417 case "$resolved$interactive" in
419 # This is used only for interactive view option.
420 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
426 if test "$interactive" = t
429 die
"cannot be interactive without stdin connected to a terminal."
431 while test "$action" = again
433 echo "Commit Body is:"
434 echo "--------------------------"
435 cat "$dotest/final-commit"
436 echo "--------------------------"
437 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
441 [aA
]*) action
=yes interactive
= ;;
442 [nN
]*) action
=skip
;;
443 [eE
]*) git_editor
"$dotest/final-commit"
446 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
453 FIRSTLINE
=$
(sed 1q
"$dotest/final-commit")
455 if test $action = skip
461 if test -x "$GIT_DIR"/hooks
/applypatch-msg
463 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
467 printf 'Applying: %s\n' "$FIRSTLINE"
471 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
475 # Resolved means the user did all the hard work, and
476 # we do not have to do any patch application. Just
477 # trust what the user has in the index file and the
480 git diff-index
--quiet --cached HEAD
-- && {
481 echo "No changes - did you forget to use 'git add'?"
482 stop_here_user_resolve
$this
484 unmerged
=$
(git ls-files
-u)
485 if test -n "$unmerged"
487 echo "You still have unmerged paths in your index"
488 echo "did you forget to use 'git add'?"
489 stop_here_user_resolve
$this
496 if test $apply_status = 1 && test "$threeway" = t
500 # Applying the patch to an earlier tree and merging the
501 # result may have produced the same tree as ours.
502 git diff-index
--quiet --cached HEAD
-- && {
503 echo No changes
-- Patch already applied.
507 # clear apply_status -- we have successfully merged.
511 if test $apply_status != 0
513 echo Patch failed
at $msgnum.
514 stop_here_user_resolve
$this
517 if test -x "$GIT_DIR"/hooks
/pre-applypatch
519 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
522 tree
=$
(git write-tree
) &&
523 parent
=$
(git rev-parse
--verify HEAD
) &&
524 commit
=$
(git commit-tree
$tree -p $parent <"$dotest/final-commit") &&
525 git update-ref
-m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD
$commit $parent ||
528 if test -x "$GIT_DIR"/hooks
/post-applypatch
530 "$GIT_DIR"/hooks
/post-applypatch