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 --allo-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"
27 prefix
=$
(git rev-parse
--show-prefix)
32 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
35 echo "$1" >"$dotest/next"
39 stop_here_user_resolve
() {
40 if [ -n "$resolvemsg" ]; then
41 printf '%s\n' "$resolvemsg"
44 cmdline
=$
(basename $0)
45 if test '' != "$interactive"
49 if test '' != "$threeway"
53 echo "When you have resolved this problem run \"$cmdline --resolved\"."
54 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
60 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
61 "$dotest/patch" "$dotest/info"
62 echo "$next" >"$dotest/next"
68 echo "Cannot fall back to three-way merge."
73 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
75 rm -fr "$dotest"/patch-merge-
*
76 mkdir
"$dotest/patch-merge-tmp-dir"
78 # First see if the patch records the index info that we can use.
79 git apply
--build-fake-ancestor "$dotest/patch-merge-tmp-index" \
81 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
82 git write-tree
>"$dotest/patch-merge-base+" ||
83 cannot_fallback
"Repository lacks necessary blobs to fall back on 3-way merge."
85 echo Using index info to reconstruct a base tree...
86 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
87 git apply
$binary --cached <"$dotest/patch"
89 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
90 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
92 cannot_fallback
"Did you hand edit your patch?
93 It does not apply to blobs recorded in its index."
96 test -f "$dotest/patch-merge-index" &&
97 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git write-tree
) &&
98 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
99 rm -fr "$dotest"/patch-merge-
* ||
exit 1
101 echo Falling back to patching base and
3-way merge...
103 # This is not so wrong. Depending on which base we picked,
104 # orig_tree may be wildly different from ours, but his_tree
105 # has the same set of wildly different changes in parts the
106 # patch did not touch, so recursive ends up canceling them,
107 # saying that we reverted all those changes.
109 eval GITHEAD_
$his_tree='"$SUBJECT"'
110 export GITHEAD_
$his_tree
111 git-merge-recursive
$orig_tree -- HEAD
$his_tree ||
{
113 echo Failed to merge
in the changes.
116 unset GITHEAD_
$his_tree
120 git stripspace
<"$1" |
sed -e 1q
125 sign
= utf8
=t keep
= skip
= interactive
= resolved
= binary
=
141 utf8
=t
;; # this is now default
151 die
"-d option is no longer supported. Do not use."
154 shift; resolvemsg
=$1 ;;
156 git_apply_opt
="$git_apply_opt $1=$2"; shift ;;
158 git_apply_opt
="$git_apply_opt $1$2"; shift ;;
167 # If the dotest directory exists, but we have finished applying all the
168 # patches in them, clear it out.
169 if test -d "$dotest" &&
170 last
=$
(cat "$dotest/last") &&
171 next
=$
(cat "$dotest/next") &&
173 test "$next" -gt "$last"
180 case "$#,$skip$resolved" in
182 # Explicit resume command and we do not have file, so
186 # No file input but without resume parameters; catch
187 # user error to feed us a patch from standard input
188 # when there is already $dotest. This is somewhat
189 # unreliable -- stdin could be /dev/null for example
190 # and the caller did not intend to feed us a patch but
191 # wanted to continue unattended.
198 die
"previous dotest directory $dotest still exists but mbox given."
201 # Make sure we are not given --skip nor --resolved
202 test ",$skip,$resolved," = ,,, ||
203 die
"Resolve operation not in progress, we are not resuming."
206 mkdir
-p "$dotest" ||
exit
208 if test -n "$prefix" && test $# != 0
213 test -n "$first" && {
221 set "$@" "$prefix$arg" ;;
226 git mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
231 # -b, -s, -u, -k and --whitespace flags are kept for the
232 # resuming session after a patch failure.
233 # -3 and -i can and must be given when resuming.
234 echo "$binary" >"$dotest/binary"
235 echo " $ws" >"$dotest/whitespace"
236 echo "$sign" >"$dotest/sign"
237 echo "$utf8" >"$dotest/utf8"
238 echo "$keep" >"$dotest/keep"
239 echo 1 >"$dotest/next"
244 files
=$
(git diff-index
--cached --name-only HEAD
--) ||
exit
245 if [ "$files" ]; then
246 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
251 if test "$(cat "$dotest/binary
")" = t
253 binary
=--allow-binary-replacement
255 if test "$(cat "$dotest/utf8
")" = t
261 if test "$(cat "$dotest/keep
")" = t
265 ws
=`cat "$dotest/whitespace"`
266 if test "$(cat "$dotest/sign
")" = t
268 SIGNOFF
=`git-var GIT_COMMITTER_IDENT | sed -e '
270 s/^/Signed-off-by: /'
276 last
=`cat "$dotest/last"`
277 this
=`cat "$dotest/next"`
281 this
=`expr "$this" + 1`
285 if test "$this" -gt "$last"
292 while test "$this" -le "$last"
294 msgnum
=`printf "%0${prec}d" $this`
295 next
=`expr "$this" + 1`
296 test -f "$dotest/$msgnum" ||
{
302 # If we are not resuming, parse and extract the patch information
303 # into separate files:
304 # - info records the authorship and title
305 # - msg is the rest of commit log message
306 # - patch is the patch body.
308 # When we are resuming, these files are either already prepared
309 # by the user, or the user can tell us to do so by --resolved flag.
312 git mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
313 <"$dotest/$msgnum" >"$dotest/info" ||
316 # skip pine's internal folder data
317 grep '^Author: Mail System Internal Data$' \
318 <"$dotest"/info
>/dev
/null
&&
321 test -s $dotest/patch ||
{
322 echo "Patch is empty. Was it split wrong?"
325 git stripspace
< "$dotest/msg" > "$dotest/msg-clean"
329 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
330 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
331 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
333 if test -z "$GIT_AUTHOR_EMAIL"
335 echo "Patch does not have a valid e-mail address."
339 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
341 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
342 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
346 if test '' != "$SIGNOFF"
349 sed -ne '/^Signed-off-by: /p' \
350 "$dotest/msg-clean" |
354 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
355 test '' = "$LAST_SIGNED_OFF_BY" && echo
362 printf '%s\n' "$SUBJECT"
363 if test -s "$dotest/msg-clean"
366 cat "$dotest/msg-clean"
368 if test '' != "$ADD_SIGNOFF"
372 } >"$dotest/final-commit"
375 case "$resolved$interactive" in
377 # This is used only for interactive view option.
378 git diff-index
-p --cached HEAD
-- >"$dotest/patch"
384 if test "$interactive" = t
387 die
"cannot be interactive without stdin connected to a terminal."
389 while test "$action" = again
391 echo "Commit Body is:"
392 echo "--------------------------"
393 cat "$dotest/final-commit"
394 echo "--------------------------"
395 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
399 [aA
]*) action
=yes interactive
= ;;
400 [nN
]*) action
=skip
;;
401 [eE
]*) git_editor
"$dotest/final-commit"
402 SUBJECT
=$
(reread_subject
"$dotest/final-commit")
405 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
413 if test $action = skip
419 if test -x "$GIT_DIR"/hooks
/applypatch-msg
421 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
425 printf 'Applying %s\n' "$SUBJECT"
429 git apply
$git_apply_opt $binary --index "$dotest/patch"
433 # Resolved means the user did all the hard work, and
434 # we do not have to do any patch application. Just
435 # trust what the user has in the index file and the
438 git diff-index
--quiet --cached HEAD
-- && {
439 echo "No changes - did you forget to use 'git add'?"
440 stop_here_user_resolve
$this
442 unmerged
=$
(git ls-files
-u)
443 if test -n "$unmerged"
445 echo "You still have unmerged paths in your index"
446 echo "did you forget to use 'git add'?"
447 stop_here_user_resolve
$this
454 if test $apply_status = 1 && test "$threeway" = t
458 # Applying the patch to an earlier tree and merging the
459 # result may have produced the same tree as ours.
460 git diff-index
--quiet --cached HEAD
-- && {
461 echo No changes
-- Patch already applied.
465 # clear apply_status -- we have successfully merged.
469 if test $apply_status != 0
471 echo Patch failed
at $msgnum.
472 stop_here_user_resolve
$this
475 if test -x "$GIT_DIR"/hooks
/pre-applypatch
477 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
480 tree
=$
(git write-tree
) &&
481 parent
=$
(git rev-parse
--verify HEAD
) &&
482 commit
=$
(git commit-tree
$tree -p $parent <"$dotest/final-commit") &&
483 git update-ref
-m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD
$commit $parent ||
486 if test -x "$GIT_DIR"/hooks
/post-applypatch
488 "$GIT_DIR"/hooks
/post-applypatch