git-am foreign patch support: autodetect some patch formats
[git.git] / git-am.sh
blob552b888703e56462bb19372820c99246268f477a
1 #!/bin/sh
3 # Copyright (c) 2005, 2006 Junio C Hamano
5 SUBDIRECTORY_OK=Yes
6 OPTIONS_KEEPDASHDASH=
7 OPTIONS_SPEC="\
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 patch-format= format the patch(es) are in
22 reject pass it through git-apply
23 resolvemsg= override error message when patch failure occurs
24 r,resolved to be used after a patch failure
25 skip skip the current patch
26 abort restore the original branch and abort the patching operation.
27 committer-date-is-author-date lie about committer date
28 ignore-date use current timestamp for author date
29 rebasing* (internal use for git-rebase)"
31 . git-sh-setup
32 prefix=$(git rev-parse --show-prefix)
33 set_reflog_action am
34 require_work_tree
35 cd_to_toplevel
37 git var GIT_COMMITTER_IDENT >/dev/null ||
38 die "You need to set your committer info first"
40 if git rev-parse --verify -q HEAD >/dev/null
41 then
42 HAS_HEAD=yes
43 else
44 HAS_HEAD=
47 sq () {
48 git rev-parse --sq-quote "$@"
51 stop_here () {
52 echo "$1" >"$dotest/next"
53 exit 1
56 stop_here_user_resolve () {
57 if [ -n "$resolvemsg" ]; then
58 printf '%s\n' "$resolvemsg"
59 stop_here $1
61 cmdline="git am"
62 if test '' != "$interactive"
63 then
64 cmdline="$cmdline -i"
66 if test '' != "$threeway"
67 then
68 cmdline="$cmdline -3"
70 echo "When you have resolved this problem run \"$cmdline --resolved\"."
71 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
72 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
74 stop_here $1
77 go_next () {
78 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
79 "$dotest/patch" "$dotest/info"
80 echo "$next" >"$dotest/next"
81 this=$next
84 cannot_fallback () {
85 echo "$1"
86 echo "Cannot fall back to three-way merge."
87 exit 1
90 fall_back_3way () {
91 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
93 rm -fr "$dotest"/patch-merge-*
94 mkdir "$dotest/patch-merge-tmp-dir"
96 # First see if the patch records the index info that we can use.
97 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
98 "$dotest/patch" &&
99 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
100 git write-tree >"$dotest/patch-merge-base+" ||
101 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
103 echo Using index info to reconstruct a base tree...
104 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
105 git apply --cached <"$dotest/patch"
106 then
107 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
108 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
109 else
110 cannot_fallback "Did you hand edit your patch?
111 It does not apply to blobs recorded in its index."
114 test -f "$dotest/patch-merge-index" &&
115 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
116 orig_tree=$(cat "$dotest/patch-merge-base") &&
117 rm -fr "$dotest"/patch-merge-* || exit 1
119 echo Falling back to patching base and 3-way merge...
121 # This is not so wrong. Depending on which base we picked,
122 # orig_tree may be wildly different from ours, but his_tree
123 # has the same set of wildly different changes in parts the
124 # patch did not touch, so recursive ends up canceling them,
125 # saying that we reverted all those changes.
127 eval GITHEAD_$his_tree='"$FIRSTLINE"'
128 export GITHEAD_$his_tree
129 git-merge-recursive $orig_tree -- HEAD $his_tree || {
130 git rerere
131 echo Failed to merge in the changes.
132 exit 1
134 unset GITHEAD_$his_tree
137 patch_format=
139 check_patch_format () {
140 # early return if patch_format was set from the command line
141 if test -n "$patch_format"
142 then
143 return 0
146 # we default to mbox format if input is from stdin and for
147 # directories
148 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
149 then
150 patch_format=mbox
151 return 0
154 # otherwise, check the first few lines of the first patch to try
155 # to detect its format
157 read l1
158 read l2
159 read l3
160 case "$l1" in
161 "From "* | "From: "*)
162 patch_format=mbox
164 '# This series applies on GIT commit'*)
165 patch_format=stgit-series
167 "# HG changeset patch")
168 patch_format=hg
171 # if the second line is empty and the third is
172 # a From, Author or Date entry, this is very
173 # likely an StGIT patch
174 case "$l2,$l3" in
175 ,"From: "* | ,"Author: "* | ,"Date: "*)
176 patch_format=stgit
180 esac
182 esac
183 } < "$1"
186 split_patches () {
187 case "$patch_format" in
188 mbox)
189 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
190 rm -fr "$dotest"
191 exit 1
195 echo "Patch format $patch_format is not supported."
196 exit 1
198 esac
201 prec=4
202 dotest="$GIT_DIR/rebase-apply"
203 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
204 resolvemsg= resume=
205 git_apply_opt=
206 committer_date_is_author_date=
207 ignore_date=
209 while test $# != 0
211 case "$1" in
212 -i|--interactive)
213 interactive=t ;;
214 -b|--binary)
215 : ;;
216 -3|--3way)
217 threeway=t ;;
218 -s|--signoff)
219 sign=t ;;
220 -u|--utf8)
221 utf8=t ;; # this is now default
222 --no-utf8)
223 utf8= ;;
224 -k|--keep)
225 keep=t ;;
226 -r|--resolved)
227 resolved=t ;;
228 --skip)
229 skip=t ;;
230 --abort)
231 abort=t ;;
232 --rebasing)
233 rebasing=t threeway=t keep=t ;;
234 -d|--dotest)
235 die "-d option is no longer supported. Do not use."
237 --resolvemsg)
238 shift; resolvemsg=$1 ;;
239 --whitespace|--directory)
240 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
241 -C|-p)
242 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
243 --patch-format)
244 shift ; patch_format="$1" ;;
245 --reject)
246 git_apply_opt="$git_apply_opt $1" ;;
247 --committer-date-is-author-date)
248 committer_date_is_author_date=t ;;
249 --ignore-date)
250 ignore_date=t ;;
252 shift; break ;;
254 usage ;;
255 esac
256 shift
257 done
259 # If the dotest directory exists, but we have finished applying all the
260 # patches in them, clear it out.
261 if test -d "$dotest" &&
262 last=$(cat "$dotest/last") &&
263 next=$(cat "$dotest/next") &&
264 test $# != 0 &&
265 test "$next" -gt "$last"
266 then
267 rm -fr "$dotest"
270 if test -d "$dotest"
271 then
272 case "$#,$skip$resolved$abort" in
273 0,*t*)
274 # Explicit resume command and we do not have file, so
275 # we are happy.
276 : ;;
278 # No file input but without resume parameters; catch
279 # user error to feed us a patch from standard input
280 # when there is already $dotest. This is somewhat
281 # unreliable -- stdin could be /dev/null for example
282 # and the caller did not intend to feed us a patch but
283 # wanted to continue unattended.
284 test -t 0
287 false
289 esac ||
290 die "previous rebase directory $dotest still exists but mbox given."
291 resume=yes
293 case "$skip,$abort" in
294 t,t)
295 die "Please make up your mind. --skip or --abort?"
298 git rerere clear
299 git read-tree --reset -u HEAD HEAD
300 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
301 git reset HEAD
302 git update-ref ORIG_HEAD $orig_head
305 if test -f "$dotest/rebasing"
306 then
307 exec git rebase --abort
309 git rerere clear
310 test -f "$dotest/dirtyindex" || {
311 git read-tree --reset -u HEAD ORIG_HEAD
312 git reset ORIG_HEAD
314 rm -fr "$dotest"
315 exit ;;
316 esac
317 rm -f "$dotest/dirtyindex"
318 else
319 # Make sure we are not given --skip, --resolved, nor --abort
320 test "$skip$resolved$abort" = "" ||
321 die "Resolve operation not in progress, we are not resuming."
323 # Start afresh.
324 mkdir -p "$dotest" || exit
326 if test -n "$prefix" && test $# != 0
327 then
328 first=t
329 for arg
331 test -n "$first" && {
332 set x
333 first=
335 case "$arg" in
337 set "$@" "$arg" ;;
339 set "$@" "$prefix$arg" ;;
340 esac
341 done
342 shift
345 check_patch_format "$@"
347 split_patches "$@"
349 # -s, -u, -k, --whitespace, -3, -C and -p flags are kept
350 # for the resuming session after a patch failure.
351 # -i can and must be given when resuming.
352 echo " $git_apply_opt" >"$dotest/apply-opt"
353 echo "$threeway" >"$dotest/threeway"
354 echo "$sign" >"$dotest/sign"
355 echo "$utf8" >"$dotest/utf8"
356 echo "$keep" >"$dotest/keep"
357 echo 1 >"$dotest/next"
358 if test -n "$rebasing"
359 then
360 : >"$dotest/rebasing"
361 else
362 : >"$dotest/applying"
363 if test -n "$HAS_HEAD"
364 then
365 git update-ref ORIG_HEAD HEAD
366 else
367 git update-ref -d ORIG_HEAD >/dev/null 2>&1
372 case "$resolved" in
374 case "$HAS_HEAD" in
376 files=$(git ls-files) ;;
378 files=$(git diff-index --cached --name-only HEAD --) ;;
379 esac || exit
380 if test "$files"
381 then
382 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
383 die "Dirty index: cannot apply patches (dirty: $files)"
385 esac
387 if test "$(cat "$dotest/utf8")" = t
388 then
389 utf8=-u
390 else
391 utf8=-n
393 if test "$(cat "$dotest/keep")" = t
394 then
395 keep=-k
397 if test "$(cat "$dotest/threeway")" = t
398 then
399 threeway=t
401 git_apply_opt=$(cat "$dotest/apply-opt")
402 if test "$(cat "$dotest/sign")" = t
403 then
404 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
405 s/>.*/>/
406 s/^/Signed-off-by: /'
408 else
409 SIGNOFF=
412 last=`cat "$dotest/last"`
413 this=`cat "$dotest/next"`
414 if test "$skip" = t
415 then
416 this=`expr "$this" + 1`
417 resume=
420 if test "$this" -gt "$last"
421 then
422 echo Nothing to do.
423 rm -fr "$dotest"
424 exit
427 while test "$this" -le "$last"
429 msgnum=`printf "%0${prec}d" $this`
430 next=`expr "$this" + 1`
431 test -f "$dotest/$msgnum" || {
432 resume=
433 go_next
434 continue
437 # If we are not resuming, parse and extract the patch information
438 # into separate files:
439 # - info records the authorship and title
440 # - msg is the rest of commit log message
441 # - patch is the patch body.
443 # When we are resuming, these files are either already prepared
444 # by the user, or the user can tell us to do so by --resolved flag.
445 case "$resume" in
447 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
448 <"$dotest/$msgnum" >"$dotest/info" ||
449 stop_here $this
451 # skip pine's internal folder data
452 grep '^Author: Mail System Internal Data$' \
453 <"$dotest"/info >/dev/null &&
454 go_next && continue
456 test -s "$dotest/patch" || {
457 echo "Patch is empty. Was it split wrong?"
458 stop_here $this
460 if test -f "$dotest/rebasing" &&
461 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
462 -e q "$dotest/$msgnum") &&
463 test "$(git cat-file -t "$commit")" = commit
464 then
465 git cat-file commit "$commit" |
466 sed -e '1,/^$/d' >"$dotest/msg-clean"
467 else
468 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
469 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
471 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
472 git stripspace > "$dotest/msg-clean"
475 esac
477 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
478 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
479 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
481 if test -z "$GIT_AUTHOR_EMAIL"
482 then
483 echo "Patch does not have a valid e-mail address."
484 stop_here $this
487 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
489 case "$resume" in
491 if test '' != "$SIGNOFF"
492 then
493 LAST_SIGNED_OFF_BY=`
494 sed -ne '/^Signed-off-by: /p' \
495 "$dotest/msg-clean" |
496 sed -ne '$p'
498 ADD_SIGNOFF=`
499 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
500 test '' = "$LAST_SIGNED_OFF_BY" && echo
501 echo "$SIGNOFF"
503 else
504 ADD_SIGNOFF=
507 if test -s "$dotest/msg-clean"
508 then
509 cat "$dotest/msg-clean"
511 if test '' != "$ADD_SIGNOFF"
512 then
513 echo "$ADD_SIGNOFF"
515 } >"$dotest/final-commit"
518 case "$resolved$interactive" in
520 # This is used only for interactive view option.
521 git diff-index -p --cached HEAD -- >"$dotest/patch"
523 esac
524 esac
526 resume=
527 if test "$interactive" = t
528 then
529 test -t 0 ||
530 die "cannot be interactive without stdin connected to a terminal."
531 action=again
532 while test "$action" = again
534 echo "Commit Body is:"
535 echo "--------------------------"
536 cat "$dotest/final-commit"
537 echo "--------------------------"
538 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
539 read reply
540 case "$reply" in
541 [yY]*) action=yes ;;
542 [aA]*) action=yes interactive= ;;
543 [nN]*) action=skip ;;
544 [eE]*) git_editor "$dotest/final-commit"
545 action=again ;;
546 [vV]*) action=again
547 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
548 *) action=again ;;
549 esac
550 done
551 else
552 action=yes
554 FIRSTLINE=$(sed 1q "$dotest/final-commit")
556 if test $action = skip
557 then
558 go_next
559 continue
562 if test -x "$GIT_DIR"/hooks/applypatch-msg
563 then
564 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
565 stop_here $this
568 printf 'Applying: %s\n' "$FIRSTLINE"
570 case "$resolved" in
572 eval 'git apply '"$git_apply_opt"' --index "$dotest/patch"'
573 apply_status=$?
576 # Resolved means the user did all the hard work, and
577 # we do not have to do any patch application. Just
578 # trust what the user has in the index file and the
579 # working tree.
580 resolved=
581 git diff-index --quiet --cached HEAD -- && {
582 echo "No changes - did you forget to use 'git add'?"
583 stop_here_user_resolve $this
585 unmerged=$(git ls-files -u)
586 if test -n "$unmerged"
587 then
588 echo "You still have unmerged paths in your index"
589 echo "did you forget to use 'git add'?"
590 stop_here_user_resolve $this
592 apply_status=0
593 git rerere
595 esac
597 if test $apply_status = 1 && test "$threeway" = t
598 then
599 if (fall_back_3way)
600 then
601 # Applying the patch to an earlier tree and merging the
602 # result may have produced the same tree as ours.
603 git diff-index --quiet --cached HEAD -- && {
604 echo No changes -- Patch already applied.
605 go_next
606 continue
608 # clear apply_status -- we have successfully merged.
609 apply_status=0
612 if test $apply_status != 0
613 then
614 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
615 stop_here_user_resolve $this
618 if test -x "$GIT_DIR"/hooks/pre-applypatch
619 then
620 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
623 tree=$(git write-tree) &&
624 commit=$(
625 if test -n "$ignore_date"
626 then
627 GIT_AUTHOR_DATE=
629 parent=$(git rev-parse --verify -q HEAD) ||
630 echo >&2 "applying to an empty history"
632 if test -n "$committer_date_is_author_date"
633 then
634 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
635 export GIT_COMMITTER_DATE
636 fi &&
637 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
638 ) &&
639 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
640 stop_here $this
642 if test -x "$GIT_DIR"/hooks/post-applypatch
643 then
644 "$GIT_DIR"/hooks/post-applypatch
647 go_next
648 done
650 git gc --auto
652 rm -fr "$dotest"