Merge branch 'maint'
[git/jrn.git] / git-am.sh
blobc8b9cbb101f1319b6efd78ff6ae9c63efcdc991b
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 q,quiet be quiet
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 c,scissors strip everything before a scissors line
19 whitespace= pass it through git-apply
20 ignore-space-change pass it through git-apply
21 ignore-whitespace pass it through git-apply
22 directory= pass it through git-apply
23 C= pass it through git-apply
24 p= pass it through git-apply
25 patch-format= format the patch(es) are in
26 reject pass it through git-apply
27 resolvemsg= override error message when patch failure occurs
28 r,resolved to be used after a patch failure
29 skip skip the current patch
30 abort restore the original branch and abort the patching operation.
31 committer-date-is-author-date lie about committer date
32 ignore-date use current timestamp for author date
33 rerere-autoupdate update the index with reused conflict resolution if possible
34 rebasing* (internal use for git-rebase)"
36 . git-sh-setup
37 prefix=$(git rev-parse --show-prefix)
38 set_reflog_action am
39 require_work_tree
40 cd_to_toplevel
42 git var GIT_COMMITTER_IDENT >/dev/null ||
43 die "You need to set your committer info first"
45 if git rev-parse --verify -q HEAD >/dev/null
46 then
47 HAS_HEAD=yes
48 else
49 HAS_HEAD=
52 sq () {
53 git rev-parse --sq-quote "$@"
56 stop_here () {
57 echo "$1" >"$dotest/next"
58 exit 1
61 stop_here_user_resolve () {
62 if [ -n "$resolvemsg" ]; then
63 printf '%s\n' "$resolvemsg"
64 stop_here $1
66 cmdline="git am"
67 if test '' != "$interactive"
68 then
69 cmdline="$cmdline -i"
71 if test '' != "$threeway"
72 then
73 cmdline="$cmdline -3"
75 echo "When you have resolved this problem run \"$cmdline --resolved\"."
76 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
77 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
79 stop_here $1
82 go_next () {
83 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
84 "$dotest/patch" "$dotest/info"
85 echo "$next" >"$dotest/next"
86 this=$next
89 cannot_fallback () {
90 echo "$1"
91 echo "Cannot fall back to three-way merge."
92 exit 1
95 fall_back_3way () {
96 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
98 rm -fr "$dotest"/patch-merge-*
99 mkdir "$dotest/patch-merge-tmp-dir"
101 # First see if the patch records the index info that we can use.
102 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
103 "$dotest/patch" &&
104 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
105 git write-tree >"$dotest/patch-merge-base+" ||
106 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
108 say Using index info to reconstruct a base tree...
109 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
110 git apply --cached <"$dotest/patch"
111 then
112 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
113 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
114 else
115 cannot_fallback "Did you hand edit your patch?
116 It does not apply to blobs recorded in its index."
119 test -f "$dotest/patch-merge-index" &&
120 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
121 orig_tree=$(cat "$dotest/patch-merge-base") &&
122 rm -fr "$dotest"/patch-merge-* || exit 1
124 say Falling back to patching base and 3-way merge...
126 # This is not so wrong. Depending on which base we picked,
127 # orig_tree may be wildly different from ours, but his_tree
128 # has the same set of wildly different changes in parts the
129 # patch did not touch, so recursive ends up canceling them,
130 # saying that we reverted all those changes.
132 eval GITHEAD_$his_tree='"$FIRSTLINE"'
133 export GITHEAD_$his_tree
134 if test -n "$GIT_QUIET"
135 then
136 export GIT_MERGE_VERBOSITY=0
138 git-merge-recursive $orig_tree -- HEAD $his_tree || {
139 git rerere $allow_rerere_autoupdate
140 echo Failed to merge in the changes.
141 exit 1
143 unset GITHEAD_$his_tree
146 clean_abort () {
147 test $# = 0 || echo >&2 "$@"
148 rm -fr "$dotest"
149 exit 1
152 patch_format=
154 check_patch_format () {
155 # early return if patch_format was set from the command line
156 if test -n "$patch_format"
157 then
158 return 0
161 # we default to mbox format if input is from stdin and for
162 # directories
163 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
164 then
165 patch_format=mbox
166 return 0
169 # otherwise, check the first few lines of the first patch to try
170 # to detect its format
172 read l1
173 read l2
174 read l3
175 case "$l1" in
176 "From "* | "From: "*)
177 patch_format=mbox
179 '# This series applies on GIT commit'*)
180 patch_format=stgit-series
182 "# HG changeset patch")
183 patch_format=hg
186 # if the second line is empty and the third is
187 # a From, Author or Date entry, this is very
188 # likely an StGIT patch
189 case "$l2,$l3" in
190 ,"From: "* | ,"Author: "* | ,"Date: "*)
191 patch_format=stgit
195 esac
197 esac
198 if test -z "$patch_format" &&
199 test -n "$l1" &&
200 test -n "$l2" &&
201 test -n "$l3"
202 then
203 # This begins with three non-empty lines. Is this a
204 # piece of e-mail a-la RFC2822? Grab all the headers,
205 # discarding the indented remainder of folded lines,
206 # and see if it looks like that they all begin with the
207 # header field names...
208 tr -d '\015' <"$1" |
209 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
210 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
211 patch_format=mbox
213 } < "$1" || clean_abort
216 split_patches () {
217 case "$patch_format" in
218 mbox)
219 case "$rebasing" in
221 keep_cr= ;;
223 keep_cr=--keep-cr ;;
224 esac
225 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
226 clean_abort
228 stgit-series)
229 if test $# -ne 1
230 then
231 clean_abort "Only one StGIT patch series can be applied at once"
233 series_dir=`dirname "$1"`
234 series_file="$1"
235 shift
237 set x
238 while read filename
240 set "$@" "$series_dir/$filename"
241 done
242 # remove the safety x
243 shift
244 # remove the arg coming from the first-line comment
245 shift
246 } < "$series_file" || clean_abort
247 # set the patch format appropriately
248 patch_format=stgit
249 # now handle the actual StGIT patches
250 split_patches "$@"
252 stgit)
253 this=0
254 for stgit in "$@"
256 this=`expr "$this" + 1`
257 msgnum=`printf "%0${prec}d" $this`
258 # Perl version of StGIT parse_patch. The first nonemptyline
259 # not starting with Author, From or Date is the
260 # subject, and the body starts with the next nonempty
261 # line not starting with Author, From or Date
262 perl -ne 'BEGIN { $subject = 0 }
263 if ($subject > 1) { print ; }
264 elsif (/^\s+$/) { next ; }
265 elsif (/^Author:/) { print s/Author/From/ ; }
266 elsif (/^(From|Date)/) { print ; }
267 elsif ($subject) {
268 $subject = 2 ;
269 print "\n" ;
270 print ;
271 } else {
272 print "Subject: ", $_ ;
273 $subject = 1;
275 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
276 done
277 echo "$this" > "$dotest/last"
278 this=
279 msgnum=
282 if test -n "$parse_patch" ; then
283 clean_abort "Patch format $patch_format is not supported."
284 else
285 clean_abort "Patch format detection failed."
288 esac
291 prec=4
292 dotest="$GIT_DIR/rebase-apply"
293 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
294 resolvemsg= resume= scissors= no_inbody_headers=
295 git_apply_opt=
296 committer_date_is_author_date=
297 ignore_date=
298 allow_rerere_autoupdate=
300 while test $# != 0
302 case "$1" in
303 -i|--interactive)
304 interactive=t ;;
305 -b|--binary)
306 : ;;
307 -3|--3way)
308 threeway=t ;;
309 -s|--signoff)
310 sign=t ;;
311 -u|--utf8)
312 utf8=t ;; # this is now default
313 --no-utf8)
314 utf8= ;;
315 -k|--keep)
316 keep=t ;;
317 -c|--scissors)
318 scissors=t ;;
319 --no-scissors)
320 scissors=f ;;
321 -r|--resolved)
322 resolved=t ;;
323 --skip)
324 skip=t ;;
325 --abort)
326 abort=t ;;
327 --rebasing)
328 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
329 -d|--dotest)
330 die "-d option is no longer supported. Do not use."
332 --resolvemsg)
333 shift; resolvemsg=$1 ;;
334 --whitespace|--directory)
335 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
336 -C|-p)
337 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
338 --patch-format)
339 shift ; patch_format="$1" ;;
340 --reject|--ignore-whitespace|--ignore-space-change)
341 git_apply_opt="$git_apply_opt $1" ;;
342 --committer-date-is-author-date)
343 committer_date_is_author_date=t ;;
344 --ignore-date)
345 ignore_date=t ;;
346 --rerere-autoupdate|--no-rerere-autoupdate)
347 allow_rerere_autoupdate="$1" ;;
348 -q|--quiet)
349 GIT_QUIET=t ;;
351 shift; break ;;
353 usage ;;
354 esac
355 shift
356 done
358 # If the dotest directory exists, but we have finished applying all the
359 # patches in them, clear it out.
360 if test -d "$dotest" &&
361 last=$(cat "$dotest/last") &&
362 next=$(cat "$dotest/next") &&
363 test $# != 0 &&
364 test "$next" -gt "$last"
365 then
366 rm -fr "$dotest"
369 if test -d "$dotest"
370 then
371 case "$#,$skip$resolved$abort" in
372 0,*t*)
373 # Explicit resume command and we do not have file, so
374 # we are happy.
375 : ;;
377 # No file input but without resume parameters; catch
378 # user error to feed us a patch from standard input
379 # when there is already $dotest. This is somewhat
380 # unreliable -- stdin could be /dev/null for example
381 # and the caller did not intend to feed us a patch but
382 # wanted to continue unattended.
383 test -t 0
386 false
388 esac ||
389 die "previous rebase directory $dotest still exists but mbox given."
390 resume=yes
392 case "$skip,$abort" in
393 t,t)
394 die "Please make up your mind. --skip or --abort?"
397 git rerere clear
398 git read-tree --reset -u HEAD HEAD
399 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
400 git reset HEAD
401 git update-ref ORIG_HEAD $orig_head
404 if test -f "$dotest/rebasing"
405 then
406 exec git rebase --abort
408 git rerere clear
409 test -f "$dotest/dirtyindex" || {
410 git read-tree --reset -u HEAD ORIG_HEAD
411 git reset ORIG_HEAD
413 rm -fr "$dotest"
414 exit ;;
415 esac
416 rm -f "$dotest/dirtyindex"
417 else
418 # Make sure we are not given --skip, --resolved, nor --abort
419 test "$skip$resolved$abort" = "" ||
420 die "Resolve operation not in progress, we are not resuming."
422 # Start afresh.
423 mkdir -p "$dotest" || exit
425 if test -n "$prefix" && test $# != 0
426 then
427 first=t
428 for arg
430 test -n "$first" && {
431 set x
432 first=
434 case "$arg" in
436 set "$@" "$arg" ;;
438 set "$@" "$prefix$arg" ;;
439 esac
440 done
441 shift
444 check_patch_format "$@"
446 split_patches "$@"
448 # -i can and must be given when resuming; everything
449 # else is kept
450 echo " $git_apply_opt" >"$dotest/apply-opt"
451 echo "$threeway" >"$dotest/threeway"
452 echo "$sign" >"$dotest/sign"
453 echo "$utf8" >"$dotest/utf8"
454 echo "$keep" >"$dotest/keep"
455 echo "$scissors" >"$dotest/scissors"
456 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
457 echo "$GIT_QUIET" >"$dotest/quiet"
458 echo 1 >"$dotest/next"
459 if test -n "$rebasing"
460 then
461 : >"$dotest/rebasing"
462 else
463 : >"$dotest/applying"
464 if test -n "$HAS_HEAD"
465 then
466 git update-ref ORIG_HEAD HEAD
467 else
468 git update-ref -d ORIG_HEAD >/dev/null 2>&1
473 case "$resolved" in
475 case "$HAS_HEAD" in
477 files=$(git ls-files) ;;
479 files=$(git diff-index --cached --name-only HEAD --) ;;
480 esac || exit
481 if test "$files"
482 then
483 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
484 die "Dirty index: cannot apply patches (dirty: $files)"
486 esac
488 if test "$(cat "$dotest/utf8")" = t
489 then
490 utf8=-u
491 else
492 utf8=-n
494 if test "$(cat "$dotest/keep")" = t
495 then
496 keep=-k
498 case "$(cat "$dotest/scissors")" in
500 scissors=--scissors ;;
502 scissors=--no-scissors ;;
503 esac
504 if test "$(cat "$dotest/no_inbody_headers")" = t
505 then
506 no_inbody_headers=--no-inbody-headers
507 else
508 no_inbody_headers=
510 if test "$(cat "$dotest/quiet")" = t
511 then
512 GIT_QUIET=t
514 if test "$(cat "$dotest/threeway")" = t
515 then
516 threeway=t
518 git_apply_opt=$(cat "$dotest/apply-opt")
519 if test "$(cat "$dotest/sign")" = t
520 then
521 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
522 s/>.*/>/
523 s/^/Signed-off-by: /'
525 else
526 SIGNOFF=
529 last=`cat "$dotest/last"`
530 this=`cat "$dotest/next"`
531 if test "$skip" = t
532 then
533 this=`expr "$this" + 1`
534 resume=
537 if test "$this" -gt "$last"
538 then
539 say Nothing to do.
540 rm -fr "$dotest"
541 exit
544 while test "$this" -le "$last"
546 msgnum=`printf "%0${prec}d" $this`
547 next=`expr "$this" + 1`
548 test -f "$dotest/$msgnum" || {
549 resume=
550 go_next
551 continue
554 # If we are not resuming, parse and extract the patch information
555 # into separate files:
556 # - info records the authorship and title
557 # - msg is the rest of commit log message
558 # - patch is the patch body.
560 # When we are resuming, these files are either already prepared
561 # by the user, or the user can tell us to do so by --resolved flag.
562 case "$resume" in
564 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
565 <"$dotest/$msgnum" >"$dotest/info" ||
566 stop_here $this
568 # skip pine's internal folder data
569 sane_grep '^Author: Mail System Internal Data$' \
570 <"$dotest"/info >/dev/null &&
571 go_next && continue
573 test -s "$dotest/patch" || {
574 echo "Patch is empty. Was it split wrong?"
575 stop_here $this
577 if test -f "$dotest/rebasing" &&
578 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
579 -e q "$dotest/$msgnum") &&
580 test "$(git cat-file -t "$commit")" = commit
581 then
582 git cat-file commit "$commit" |
583 sed -e '1,/^$/d' >"$dotest/msg-clean"
584 else
586 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
587 echo
588 cat "$dotest/msg"
590 git stripspace > "$dotest/msg-clean"
593 esac
595 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
596 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
597 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
599 if test -z "$GIT_AUTHOR_EMAIL"
600 then
601 echo "Patch does not have a valid e-mail address."
602 stop_here $this
605 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
607 case "$resume" in
609 if test '' != "$SIGNOFF"
610 then
611 LAST_SIGNED_OFF_BY=`
612 sed -ne '/^Signed-off-by: /p' \
613 "$dotest/msg-clean" |
614 sed -ne '$p'
616 ADD_SIGNOFF=`
617 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
618 test '' = "$LAST_SIGNED_OFF_BY" && echo
619 echo "$SIGNOFF"
621 else
622 ADD_SIGNOFF=
625 if test -s "$dotest/msg-clean"
626 then
627 cat "$dotest/msg-clean"
629 if test '' != "$ADD_SIGNOFF"
630 then
631 echo "$ADD_SIGNOFF"
633 } >"$dotest/final-commit"
636 case "$resolved$interactive" in
638 # This is used only for interactive view option.
639 git diff-index -p --cached HEAD -- >"$dotest/patch"
641 esac
642 esac
644 resume=
645 if test "$interactive" = t
646 then
647 test -t 0 ||
648 die "cannot be interactive without stdin connected to a terminal."
649 action=again
650 while test "$action" = again
652 echo "Commit Body is:"
653 echo "--------------------------"
654 cat "$dotest/final-commit"
655 echo "--------------------------"
656 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
657 read reply
658 case "$reply" in
659 [yY]*) action=yes ;;
660 [aA]*) action=yes interactive= ;;
661 [nN]*) action=skip ;;
662 [eE]*) git_editor "$dotest/final-commit"
663 action=again ;;
664 [vV]*) action=again
665 : ${GIT_PAGER=$(git var GIT_PAGER)}
666 : ${LESS=-FRSX}
667 export LESS
668 $GIT_PAGER "$dotest/patch" ;;
669 *) action=again ;;
670 esac
671 done
672 else
673 action=yes
675 FIRSTLINE=$(sed 1q "$dotest/final-commit")
677 if test $action = skip
678 then
679 go_next
680 continue
683 if test -x "$GIT_DIR"/hooks/applypatch-msg
684 then
685 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
686 stop_here $this
689 say "Applying: $FIRSTLINE"
691 case "$resolved" in
693 # When we are allowed to fall back to 3-way later, don't give
694 # false errors during the initial attempt.
695 squelch=
696 if test "$threeway" = t
697 then
698 squelch='>/dev/null 2>&1 '
700 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
701 apply_status=$?
704 # Resolved means the user did all the hard work, and
705 # we do not have to do any patch application. Just
706 # trust what the user has in the index file and the
707 # working tree.
708 resolved=
709 git diff-index --quiet --cached HEAD -- && {
710 echo "No changes - did you forget to use 'git add'?"
711 stop_here_user_resolve $this
713 unmerged=$(git ls-files -u)
714 if test -n "$unmerged"
715 then
716 echo "You still have unmerged paths in your index"
717 echo "did you forget to use 'git add'?"
718 stop_here_user_resolve $this
720 apply_status=0
721 git rerere
723 esac
725 if test $apply_status = 1 && test "$threeway" = t
726 then
727 if (fall_back_3way)
728 then
729 # Applying the patch to an earlier tree and merging the
730 # result may have produced the same tree as ours.
731 git diff-index --quiet --cached HEAD -- && {
732 say No changes -- Patch already applied.
733 go_next
734 continue
736 # clear apply_status -- we have successfully merged.
737 apply_status=0
740 if test $apply_status != 0
741 then
742 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
743 stop_here_user_resolve $this
746 if test -x "$GIT_DIR"/hooks/pre-applypatch
747 then
748 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
751 tree=$(git write-tree) &&
752 commit=$(
753 if test -n "$ignore_date"
754 then
755 GIT_AUTHOR_DATE=
757 parent=$(git rev-parse --verify -q HEAD) ||
758 say >&2 "applying to an empty history"
760 if test -n "$committer_date_is_author_date"
761 then
762 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
763 export GIT_COMMITTER_DATE
764 fi &&
765 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
766 ) &&
767 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
768 stop_here $this
770 if test -x "$GIT_DIR"/hooks/post-applypatch
771 then
772 "$GIT_DIR"/hooks/post-applypatch
775 go_next
776 done
778 git gc --auto
780 rm -fr "$dotest"