gitweb: Add git_merge sub
[git/gsoc2010-gitweb.git] / git-am.sh
blobc9eaa385b0d64414ae95b9afddcedd991f7a5c67
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 keep-cr pass --keep-cr flag to git-mailsplit for mbox format
19 no-keep-cr do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
20 c,scissors strip everything before a scissors line
21 whitespace= pass it through git-apply
22 ignore-space-change pass it through git-apply
23 ignore-whitespace pass it through git-apply
24 directory= pass it through git-apply
25 C= pass it through git-apply
26 p= pass it through git-apply
27 patch-format= format the patch(es) are in
28 reject pass it through git-apply
29 resolvemsg= override error message when patch failure occurs
30 continue continue applying patches after resolving a conflict
31 r,resolved synonyms for --continue
32 skip skip the current patch
33 abort restore the original branch and abort the patching operation.
34 committer-date-is-author-date lie about committer date
35 ignore-date use current timestamp for author date
36 rerere-autoupdate update the index with reused conflict resolution if possible
37 add-commit-name* (internal use for git-rebase)
38 rebasing* (internal use for git-rebase)"
40 . git-sh-setup
41 prefix=$(git rev-parse --show-prefix)
42 set_reflog_action am
43 require_work_tree
44 cd_to_toplevel
46 git var GIT_COMMITTER_IDENT >/dev/null ||
47 die "You need to set your committer info first"
49 if git rev-parse --verify -q HEAD >/dev/null
50 then
51 HAS_HEAD=yes
52 else
53 HAS_HEAD=
56 sq () {
57 git rev-parse --sq-quote "$@"
60 stop_here () {
61 echo "$1" >"$dotest/next"
62 exit 1
65 stop_here_user_resolve () {
66 if [ -n "$resolvemsg" ]; then
67 printf '%s\n' "$resolvemsg"
68 stop_here $1
70 cmdline="git am"
71 if test '' != "$interactive"
72 then
73 cmdline="$cmdline -i"
75 if test '' != "$threeway"
76 then
77 cmdline="$cmdline -3"
79 echo "When you have resolved this problem run \"$cmdline --resolved\"."
80 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
81 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
83 stop_here $1
86 go_next () {
87 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
88 "$dotest/patch" "$dotest/info"
89 echo "$next" >"$dotest/next"
90 this=$next
93 cannot_fallback () {
94 echo "$1"
95 echo "Cannot fall back to three-way merge."
96 exit 1
99 fall_back_3way () {
100 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
102 rm -fr "$dotest"/patch-merge-*
103 mkdir "$dotest/patch-merge-tmp-dir"
105 # First see if the patch records the index info that we can use.
106 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
107 "$dotest/patch" &&
108 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
109 git write-tree >"$dotest/patch-merge-base+" ||
110 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
112 say Using index info to reconstruct a base tree...
113 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
114 git apply --cached <"$dotest/patch"
115 then
116 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
117 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
118 else
119 cannot_fallback "Did you hand edit your patch?
120 It does not apply to blobs recorded in its index."
123 test -f "$dotest/patch-merge-index" &&
124 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
125 orig_tree=$(cat "$dotest/patch-merge-base") &&
126 rm -fr "$dotest"/patch-merge-* || exit 1
128 say Falling back to patching base and 3-way merge...
130 # This is not so wrong. Depending on which base we picked,
131 # orig_tree may be wildly different from ours, but his_tree
132 # has the same set of wildly different changes in parts the
133 # patch did not touch, so recursive ends up canceling them,
134 # saying that we reverted all those changes.
136 eval GITHEAD_$his_tree='"$FIRSTLINE"'
137 export GITHEAD_$his_tree
138 if test -n "$GIT_QUIET"
139 then
140 export GIT_MERGE_VERBOSITY=0
142 git-merge-recursive $orig_tree -- HEAD $his_tree || {
143 git rerere $allow_rerere_autoupdate
144 echo Failed to merge in the changes.
145 exit 1
147 unset GITHEAD_$his_tree
150 clean_abort () {
151 test $# = 0 || echo >&2 "$@"
152 rm -fr "$dotest"
153 exit 1
156 patch_format=
158 check_patch_format () {
159 # early return if patch_format was set from the command line
160 if test -n "$patch_format"
161 then
162 return 0
165 # we default to mbox format if input is from stdin and for
166 # directories
167 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
168 then
169 patch_format=mbox
170 return 0
173 # otherwise, check the first few lines of the first patch to try
174 # to detect its format
176 read l1
177 read l2
178 read l3
179 case "$l1" in
180 "From "* | "From: "*)
181 patch_format=mbox
183 '# This series applies on GIT commit'*)
184 patch_format=stgit-series
186 "# HG changeset patch")
187 patch_format=hg
190 # if the second line is empty and the third is
191 # a From, Author or Date entry, this is very
192 # likely an StGIT patch
193 case "$l2,$l3" in
194 ,"From: "* | ,"Author: "* | ,"Date: "*)
195 patch_format=stgit
199 esac
201 esac
202 if test -z "$patch_format" &&
203 test -n "$l1" &&
204 test -n "$l2" &&
205 test -n "$l3"
206 then
207 # This begins with three non-empty lines. Is this a
208 # piece of e-mail a-la RFC2822? Grab all the headers,
209 # discarding the indented remainder of folded lines,
210 # and see if it looks like that they all begin with the
211 # header field names...
212 tr -d '\015' <"$1" |
213 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
214 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
215 patch_format=mbox
217 } < "$1" || clean_abort
220 split_patches () {
221 case "$patch_format" in
222 mbox)
223 if test -n "$rebasing" || test t = "$keepcr"
224 then
225 keep_cr=--keep-cr
226 else
227 keep_cr=
229 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
230 clean_abort
232 stgit-series)
233 if test $# -ne 1
234 then
235 clean_abort "Only one StGIT patch series can be applied at once"
237 series_dir=`dirname "$1"`
238 series_file="$1"
239 shift
241 set x
242 while read filename
244 set "$@" "$series_dir/$filename"
245 done
246 # remove the safety x
247 shift
248 # remove the arg coming from the first-line comment
249 shift
250 } < "$series_file" || clean_abort
251 # set the patch format appropriately
252 patch_format=stgit
253 # now handle the actual StGIT patches
254 split_patches "$@"
256 stgit)
257 this=0
258 for stgit in "$@"
260 this=`expr "$this" + 1`
261 msgnum=`printf "%0${prec}d" $this`
262 # Perl version of StGIT parse_patch. The first nonemptyline
263 # not starting with Author, From or Date is the
264 # subject, and the body starts with the next nonempty
265 # line not starting with Author, From or Date
266 perl -ne 'BEGIN { $subject = 0 }
267 if ($subject > 1) { print ; }
268 elsif (/^\s+$/) { next ; }
269 elsif (/^Author:/) { print s/Author/From/ ; }
270 elsif (/^(From|Date)/) { print ; }
271 elsif ($subject) {
272 $subject = 2 ;
273 print "\n" ;
274 print ;
275 } else {
276 print "Subject: ", $_ ;
277 $subject = 1;
279 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
280 done
281 echo "$this" > "$dotest/last"
282 this=
283 msgnum=
286 if test -n "$parse_patch" ; then
287 clean_abort "Patch format $patch_format is not supported."
288 else
289 clean_abort "Patch format detection failed."
292 esac
295 prec=4
296 dotest="$GIT_DIR/rebase-apply"
297 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
298 resolvemsg= resume= scissors= no_inbody_headers=
299 git_apply_opt=
300 committer_date_is_author_date=
301 ignore_date=
302 allow_rerere_autoupdate=
303 add_commit_name=
305 if test "$(git config --bool --get am.keepcr)" = true
306 then
307 keepcr=t
310 while test $# != 0
312 case "$1" in
313 -i|--interactive)
314 interactive=t ;;
315 -b|--binary)
316 : ;;
317 -3|--3way)
318 threeway=t ;;
319 -s|--signoff)
320 sign=t ;;
321 -u|--utf8)
322 utf8=t ;; # this is now default
323 --no-utf8)
324 utf8= ;;
325 -k|--keep)
326 keep=t ;;
327 -c|--scissors)
328 scissors=t ;;
329 --no-scissors)
330 scissors=f ;;
331 -r|--resolved|--continue)
332 resolved=t ;;
333 --skip)
334 skip=t ;;
335 --abort)
336 abort=t ;;
337 --rebasing)
338 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
339 -d|--dotest)
340 die "-d option is no longer supported. Do not use."
342 --resolvemsg)
343 shift; resolvemsg=$1 ;;
344 --whitespace|--directory)
345 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
346 -C|-p)
347 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
348 --patch-format)
349 shift ; patch_format="$1" ;;
350 --reject|--ignore-whitespace|--ignore-space-change)
351 git_apply_opt="$git_apply_opt $1" ;;
352 --committer-date-is-author-date)
353 committer_date_is_author_date=t ;;
354 --ignore-date)
355 ignore_date=t ;;
356 --rerere-autoupdate|--no-rerere-autoupdate)
357 allow_rerere_autoupdate="$1" ;;
358 -q|--quiet)
359 GIT_QUIET=t ;;
360 --add-commit-name)
361 add_commit_name=t ;;
362 --keep-cr)
363 keepcr=t ;;
364 --no-keep-cr)
365 keepcr=f ;;
367 shift; break ;;
369 usage ;;
370 esac
371 shift
372 done
374 # If the dotest directory exists, but we have finished applying all the
375 # patches in them, clear it out.
376 if test -d "$dotest" &&
377 last=$(cat "$dotest/last") &&
378 next=$(cat "$dotest/next") &&
379 test $# != 0 &&
380 test "$next" -gt "$last"
381 then
382 rm -fr "$dotest"
385 if test -d "$dotest"
386 then
387 case "$#,$skip$resolved$abort" in
388 0,*t*)
389 # Explicit resume command and we do not have file, so
390 # we are happy.
391 : ;;
393 # No file input but without resume parameters; catch
394 # user error to feed us a patch from standard input
395 # when there is already $dotest. This is somewhat
396 # unreliable -- stdin could be /dev/null for example
397 # and the caller did not intend to feed us a patch but
398 # wanted to continue unattended.
399 test -t 0
402 false
404 esac ||
405 die "previous rebase directory $dotest still exists but mbox given."
406 resume=yes
408 case "$skip,$abort" in
409 t,t)
410 die "Please make up your mind. --skip or --abort?"
413 git rerere clear
414 git read-tree --reset -u HEAD HEAD
415 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
416 git reset HEAD
417 git update-ref ORIG_HEAD $orig_head
420 if test -f "$dotest/rebasing"
421 then
422 exec git rebase --abort
424 git rerere clear
425 test -f "$dotest/dirtyindex" || {
426 git read-tree --reset -u HEAD ORIG_HEAD
427 git reset ORIG_HEAD
429 rm -fr "$dotest"
430 exit ;;
431 esac
432 rm -f "$dotest/dirtyindex"
433 else
434 # Make sure we are not given --skip, --resolved, nor --abort
435 test "$skip$resolved$abort" = "" ||
436 die "Resolve operation not in progress, we are not resuming."
438 # Start afresh.
439 mkdir -p "$dotest" || exit
441 if test -n "$prefix" && test $# != 0
442 then
443 first=t
444 for arg
446 test -n "$first" && {
447 set x
448 first=
450 case "$arg" in
452 set "$@" "$arg" ;;
454 set "$@" "$prefix$arg" ;;
455 esac
456 done
457 shift
460 check_patch_format "$@"
462 split_patches "$@"
464 # -i can and must be given when resuming; everything
465 # else is kept
466 echo " $git_apply_opt" >"$dotest/apply-opt"
467 echo "$threeway" >"$dotest/threeway"
468 echo "$sign" >"$dotest/sign"
469 echo "$utf8" >"$dotest/utf8"
470 echo "$keep" >"$dotest/keep"
471 echo "$keepcr" >"$dotest/keepcr"
472 echo "$scissors" >"$dotest/scissors"
473 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
474 echo "$add_commit_name" > "$dotest/add_commit_name"
475 echo "$GIT_QUIET" >"$dotest/quiet"
476 echo 1 >"$dotest/next"
477 if test -n "$rebasing"
478 then
479 : >"$dotest/rebasing"
480 else
481 : >"$dotest/applying"
482 if test -n "$HAS_HEAD"
483 then
484 git update-ref ORIG_HEAD HEAD
485 else
486 git update-ref -d ORIG_HEAD >/dev/null 2>&1
491 case "$resolved" in
493 case "$HAS_HEAD" in
495 files=$(git ls-files) ;;
497 files=$(git diff-index --cached --name-only HEAD --) ;;
498 esac || exit
499 if test "$files"
500 then
501 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
502 die "Dirty index: cannot apply patches (dirty: $files)"
504 esac
506 if test "$(cat "$dotest/utf8")" = t
507 then
508 utf8=-u
509 else
510 utf8=-n
512 if test "$(cat "$dotest/keep")" = t
513 then
514 keep=-k
516 case "$(cat "$dotest/keepcr")" in
518 keepcr=--keep-cr ;;
520 keepcr=--no-keep-cr ;;
521 esac
522 case "$(cat "$dotest/scissors")" in
524 scissors=--scissors ;;
526 scissors=--no-scissors ;;
527 esac
528 if test "$(cat "$dotest/no_inbody_headers")" = t
529 then
530 no_inbody_headers=--no-inbody-headers
531 else
532 no_inbody_headers=
534 if test "$(cat "$dotest/add_commit_name")" = t
535 then
536 add_commit_name=t
538 if test "$(cat "$dotest/quiet")" = t
539 then
540 GIT_QUIET=t
542 if test "$(cat "$dotest/threeway")" = t
543 then
544 threeway=t
546 git_apply_opt=$(cat "$dotest/apply-opt")
547 if test "$(cat "$dotest/sign")" = t
548 then
549 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
550 s/>.*/>/
551 s/^/Signed-off-by: /'
553 else
554 SIGNOFF=
557 last=`cat "$dotest/last"`
558 this=`cat "$dotest/next"`
559 if test "$skip" = t
560 then
561 this=`expr "$this" + 1`
562 resume=
565 if test "$this" -gt "$last"
566 then
567 say Nothing to do.
568 rm -fr "$dotest"
569 exit
572 while test "$this" -le "$last"
574 msgnum=`printf "%0${prec}d" $this`
575 next=`expr "$this" + 1`
576 test -f "$dotest/$msgnum" || {
577 resume=
578 go_next
579 continue
582 # If we are not resuming, parse and extract the patch information
583 # into separate files:
584 # - info records the authorship and title
585 # - msg is the rest of commit log message
586 # - patch is the patch body.
588 # When we are resuming, these files are either already prepared
589 # by the user, or the user can tell us to do so by --resolved flag.
590 case "$resume" in
592 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
593 <"$dotest/$msgnum" >"$dotest/info" ||
594 stop_here $this
596 # skip pine's internal folder data
597 sane_grep '^Author: Mail System Internal Data$' \
598 <"$dotest"/info >/dev/null &&
599 go_next && continue
601 test -s "$dotest/patch" || {
602 echo "Patch is empty. Was it split wrong?"
603 stop_here $this
605 rm -f "$dotest/original-commit"
606 if test -f "$dotest/rebasing" &&
607 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
608 -e q "$dotest/$msgnum") &&
609 test "$(git cat-file -t "$commit")" = commit
610 then
611 git cat-file commit "$commit" |
612 sed -e '1,/^$/d' >"$dotest/msg-clean"
613 echo "$commit" > "$dotest/original-commit"
614 else
616 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
617 echo
618 cat "$dotest/msg"
620 git stripspace > "$dotest/msg-clean"
623 esac
625 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
626 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
627 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
629 if test -z "$GIT_AUTHOR_EMAIL"
630 then
631 echo "Patch does not have a valid e-mail address."
632 stop_here $this
635 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
637 case "$resume" in
639 if test '' != "$SIGNOFF"
640 then
641 LAST_SIGNED_OFF_BY=`
642 sed -ne '/^Signed-off-by: /p' \
643 "$dotest/msg-clean" |
644 sed -ne '$p'
646 ADD_SIGNOFF=`
647 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
648 test '' = "$LAST_SIGNED_OFF_BY" && echo
649 echo "$SIGNOFF"
651 else
652 ADD_SIGNOFF=
655 if test -s "$dotest/msg-clean"
656 then
657 cat "$dotest/msg-clean"
659 if test '' != "$ADD_SIGNOFF"
660 then
661 echo "$ADD_SIGNOFF"
663 if test "$add_commit_name" = t
664 then
665 test -z "$commit" && die "Internal error: expecting a commit name."
666 echo "(cherry picked from commit $commit)"
668 } >"$dotest/final-commit"
671 case "$resolved$interactive" in
673 # This is used only for interactive view option.
674 git diff-index -p --cached HEAD -- >"$dotest/patch"
676 esac
677 esac
679 resume=
680 if test "$interactive" = t
681 then
682 test -t 0 ||
683 die "cannot be interactive without stdin connected to a terminal."
684 action=again
685 while test "$action" = again
687 echo "Commit Body is:"
688 echo "--------------------------"
689 cat "$dotest/final-commit"
690 echo "--------------------------"
691 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
692 read reply
693 case "$reply" in
694 [yY]*) action=yes ;;
695 [aA]*) action=yes interactive= ;;
696 [nN]*) action=skip ;;
697 [eE]*) git_editor "$dotest/final-commit"
698 action=again ;;
699 [vV]*) action=again
700 git_pager "$dotest/patch" ;;
701 *) action=again ;;
702 esac
703 done
704 else
705 action=yes
707 FIRSTLINE=$(sed 1q "$dotest/final-commit")
709 if test $action = skip
710 then
711 go_next
712 continue
715 if test -x "$GIT_DIR"/hooks/applypatch-msg
716 then
717 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
718 stop_here $this
721 say "Applying: $FIRSTLINE"
723 case "$resolved" in
725 # When we are allowed to fall back to 3-way later, don't give
726 # false errors during the initial attempt.
727 squelch=
728 if test "$threeway" = t
729 then
730 squelch='>/dev/null 2>&1 '
732 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
733 apply_status=$?
736 # Resolved means the user did all the hard work, and
737 # we do not have to do any patch application. Just
738 # trust what the user has in the index file and the
739 # working tree.
740 resolved=
741 git diff-index --quiet --cached HEAD -- && {
742 echo "No changes - did you forget to use 'git add'?"
743 echo "If there is nothing left to stage, chances are that something else"
744 echo "already introduced the same changes; you might want to skip this patch."
745 stop_here_user_resolve $this
747 unmerged=$(git ls-files -u)
748 if test -n "$unmerged"
749 then
750 echo "You still have unmerged paths in your index"
751 echo "did you forget to use 'git add'?"
752 stop_here_user_resolve $this
754 apply_status=0
755 git rerere
757 esac
759 if test $apply_status != 0 && test "$threeway" = t
760 then
761 if (fall_back_3way)
762 then
763 # Applying the patch to an earlier tree and merging the
764 # result may have produced the same tree as ours.
765 git diff-index --quiet --cached HEAD -- && {
766 say No changes -- Patch already applied.
767 go_next
768 continue
770 # clear apply_status -- we have successfully merged.
771 apply_status=0
774 if test $apply_status != 0
775 then
776 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
777 stop_here_user_resolve $this
780 if test -x "$GIT_DIR"/hooks/pre-applypatch
781 then
782 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
785 tree=$(git write-tree) &&
786 commit=$(
787 if test -n "$ignore_date"
788 then
789 GIT_AUTHOR_DATE=
791 parent=$(git rev-parse --verify -q HEAD) ||
792 say >&2 "applying to an empty history"
794 if test -n "$committer_date_is_author_date"
795 then
796 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
797 export GIT_COMMITTER_DATE
798 fi &&
799 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
800 ) &&
801 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
802 stop_here $this
804 if test -f "$dotest/original-commit"; then
805 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
808 if test -x "$GIT_DIR"/hooks/post-applypatch
809 then
810 "$GIT_DIR"/hooks/post-applypatch
813 go_next
814 done
816 if test -s "$dotest"/rewritten; then
817 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
818 if test -x "$GIT_DIR"/hooks/post-rewrite; then
819 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
823 rm -fr "$dotest"
824 git gc --auto