gitmodules.5: url can be a relative path
[git/jrn.git] / git-am.sh
blob1df5b04146a118831f4b8d30652aa506c7e4b02e
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 rebasing* (internal use for git-rebase)"
39 . git-sh-setup
40 prefix=$(git rev-parse --show-prefix)
41 set_reflog_action am
42 require_work_tree
43 cd_to_toplevel
45 git var GIT_COMMITTER_IDENT >/dev/null ||
46 die "You need to set your committer info first"
48 if git rev-parse --verify -q HEAD >/dev/null
49 then
50 HAS_HEAD=yes
51 else
52 HAS_HEAD=
55 sq () {
56 git rev-parse --sq-quote "$@"
59 stop_here () {
60 echo "$1" >"$dotest/next"
61 exit 1
64 stop_here_user_resolve () {
65 if [ -n "$resolvemsg" ]; then
66 printf '%s\n' "$resolvemsg"
67 stop_here $1
69 cmdline="git am"
70 if test '' != "$interactive"
71 then
72 cmdline="$cmdline -i"
74 if test '' != "$threeway"
75 then
76 cmdline="$cmdline -3"
78 echo "When you have resolved this problem run \"$cmdline --resolved\"."
79 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
80 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
82 stop_here $1
85 go_next () {
86 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
87 "$dotest/patch" "$dotest/info"
88 echo "$next" >"$dotest/next"
89 this=$next
92 cannot_fallback () {
93 echo "$1"
94 echo "Cannot fall back to three-way merge."
95 exit 1
98 fall_back_3way () {
99 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
101 rm -fr "$dotest"/patch-merge-*
102 mkdir "$dotest/patch-merge-tmp-dir"
104 # First see if the patch records the index info that we can use.
105 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
106 "$dotest/patch" &&
107 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
108 git write-tree >"$dotest/patch-merge-base+" ||
109 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
111 say Using index info to reconstruct a base tree...
112 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
113 git apply --cached <"$dotest/patch"
114 then
115 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
116 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
117 else
118 cannot_fallback "Did you hand edit your patch?
119 It does not apply to blobs recorded in its index."
122 test -f "$dotest/patch-merge-index" &&
123 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
124 orig_tree=$(cat "$dotest/patch-merge-base") &&
125 rm -fr "$dotest"/patch-merge-* || exit 1
127 say Falling back to patching base and 3-way merge...
129 # This is not so wrong. Depending on which base we picked,
130 # orig_tree may be wildly different from ours, but his_tree
131 # has the same set of wildly different changes in parts the
132 # patch did not touch, so recursive ends up canceling them,
133 # saying that we reverted all those changes.
135 eval GITHEAD_$his_tree='"$FIRSTLINE"'
136 export GITHEAD_$his_tree
137 if test -n "$GIT_QUIET"
138 then
139 export GIT_MERGE_VERBOSITY=0
141 git-merge-recursive $orig_tree -- HEAD $his_tree || {
142 git rerere $allow_rerere_autoupdate
143 echo Failed to merge in the changes.
144 exit 1
146 unset GITHEAD_$his_tree
149 clean_abort () {
150 test $# = 0 || echo >&2 "$@"
151 rm -fr "$dotest"
152 exit 1
155 patch_format=
157 check_patch_format () {
158 # early return if patch_format was set from the command line
159 if test -n "$patch_format"
160 then
161 return 0
164 # we default to mbox format if input is from stdin and for
165 # directories
166 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
167 then
168 patch_format=mbox
169 return 0
172 # otherwise, check the first few lines of the first patch to try
173 # to detect its format
175 read l1
176 read l2
177 read l3
178 case "$l1" in
179 "From "* | "From: "*)
180 patch_format=mbox
182 '# This series applies on GIT commit'*)
183 patch_format=stgit-series
185 "# HG changeset patch")
186 patch_format=hg
189 # if the second line is empty and the third is
190 # a From, Author or Date entry, this is very
191 # likely an StGIT patch
192 case "$l2,$l3" in
193 ,"From: "* | ,"Author: "* | ,"Date: "*)
194 patch_format=stgit
198 esac
200 esac
201 if test -z "$patch_format" &&
202 test -n "$l1" &&
203 test -n "$l2" &&
204 test -n "$l3"
205 then
206 # This begins with three non-empty lines. Is this a
207 # piece of e-mail a-la RFC2822? Grab all the headers,
208 # discarding the indented remainder of folded lines,
209 # and see if it looks like that they all begin with the
210 # header field names...
211 tr -d '\015' <"$1" |
212 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
213 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
214 patch_format=mbox
216 } < "$1" || clean_abort
219 split_patches () {
220 case "$patch_format" in
221 mbox)
222 if test -n "$rebasing" || test t = "$keepcr"
223 then
224 keep_cr=--keep-cr
225 else
226 keep_cr=
228 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
229 clean_abort
231 stgit-series)
232 if test $# -ne 1
233 then
234 clean_abort "Only one StGIT patch series can be applied at once"
236 series_dir=`dirname "$1"`
237 series_file="$1"
238 shift
240 set x
241 while read filename
243 set "$@" "$series_dir/$filename"
244 done
245 # remove the safety x
246 shift
247 # remove the arg coming from the first-line comment
248 shift
249 } < "$series_file" || clean_abort
250 # set the patch format appropriately
251 patch_format=stgit
252 # now handle the actual StGIT patches
253 split_patches "$@"
255 stgit)
256 this=0
257 for stgit in "$@"
259 this=`expr "$this" + 1`
260 msgnum=`printf "%0${prec}d" $this`
261 # Perl version of StGIT parse_patch. The first nonemptyline
262 # not starting with Author, From or Date is the
263 # subject, and the body starts with the next nonempty
264 # line not starting with Author, From or Date
265 perl -ne 'BEGIN { $subject = 0 }
266 if ($subject > 1) { print ; }
267 elsif (/^\s+$/) { next ; }
268 elsif (/^Author:/) { print s/Author/From/ ; }
269 elsif (/^(From|Date)/) { print ; }
270 elsif ($subject) {
271 $subject = 2 ;
272 print "\n" ;
273 print ;
274 } else {
275 print "Subject: ", $_ ;
276 $subject = 1;
278 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
279 done
280 echo "$this" > "$dotest/last"
281 this=
282 msgnum=
285 if test -n "$parse_patch" ; then
286 clean_abort "Patch format $patch_format is not supported."
287 else
288 clean_abort "Patch format detection failed."
291 esac
294 prec=4
295 dotest="$GIT_DIR/rebase-apply"
296 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
297 resolvemsg= resume= scissors= no_inbody_headers=
298 git_apply_opt=
299 committer_date_is_author_date=
300 ignore_date=
301 allow_rerere_autoupdate=
303 if test "$(git config --bool --get am.keepcr)" = true
304 then
305 keepcr=t
308 while test $# != 0
310 case "$1" in
311 -i|--interactive)
312 interactive=t ;;
313 -b|--binary)
314 : ;;
315 -3|--3way)
316 threeway=t ;;
317 -s|--signoff)
318 sign=t ;;
319 -u|--utf8)
320 utf8=t ;; # this is now default
321 --no-utf8)
322 utf8= ;;
323 -k|--keep)
324 keep=t ;;
325 -c|--scissors)
326 scissors=t ;;
327 --no-scissors)
328 scissors=f ;;
329 -r|--resolved|--continue)
330 resolved=t ;;
331 --skip)
332 skip=t ;;
333 --abort)
334 abort=t ;;
335 --rebasing)
336 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
337 -d|--dotest)
338 die "-d option is no longer supported. Do not use."
340 --resolvemsg)
341 shift; resolvemsg=$1 ;;
342 --whitespace|--directory)
343 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
344 -C|-p)
345 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
346 --patch-format)
347 shift ; patch_format="$1" ;;
348 --reject|--ignore-whitespace|--ignore-space-change)
349 git_apply_opt="$git_apply_opt $1" ;;
350 --committer-date-is-author-date)
351 committer_date_is_author_date=t ;;
352 --ignore-date)
353 ignore_date=t ;;
354 --rerere-autoupdate|--no-rerere-autoupdate)
355 allow_rerere_autoupdate="$1" ;;
356 -q|--quiet)
357 GIT_QUIET=t ;;
358 --keep-cr)
359 keepcr=t ;;
360 --no-keep-cr)
361 keepcr=f ;;
363 shift; break ;;
365 usage ;;
366 esac
367 shift
368 done
370 # If the dotest directory exists, but we have finished applying all the
371 # patches in them, clear it out.
372 if test -d "$dotest" &&
373 last=$(cat "$dotest/last") &&
374 next=$(cat "$dotest/next") &&
375 test $# != 0 &&
376 test "$next" -gt "$last"
377 then
378 rm -fr "$dotest"
381 if test -d "$dotest"
382 then
383 case "$#,$skip$resolved$abort" in
384 0,*t*)
385 # Explicit resume command and we do not have file, so
386 # we are happy.
387 : ;;
389 # No file input but without resume parameters; catch
390 # user error to feed us a patch from standard input
391 # when there is already $dotest. This is somewhat
392 # unreliable -- stdin could be /dev/null for example
393 # and the caller did not intend to feed us a patch but
394 # wanted to continue unattended.
395 test -t 0
398 false
400 esac ||
401 die "previous rebase directory $dotest still exists but mbox given."
402 resume=yes
404 case "$skip,$abort" in
405 t,t)
406 die "Please make up your mind. --skip or --abort?"
409 git rerere clear
410 git read-tree --reset -u HEAD HEAD
411 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
412 git reset HEAD
413 git update-ref ORIG_HEAD $orig_head
416 if test -f "$dotest/rebasing"
417 then
418 exec git rebase --abort
420 git rerere clear
421 test -f "$dotest/dirtyindex" || {
422 git read-tree --reset -u HEAD ORIG_HEAD
423 git reset ORIG_HEAD
425 rm -fr "$dotest"
426 exit ;;
427 esac
428 rm -f "$dotest/dirtyindex"
429 else
430 # Make sure we are not given --skip, --resolved, nor --abort
431 test "$skip$resolved$abort" = "" ||
432 die "Resolve operation not in progress, we are not resuming."
434 # Start afresh.
435 mkdir -p "$dotest" || exit
437 if test -n "$prefix" && test $# != 0
438 then
439 first=t
440 for arg
442 test -n "$first" && {
443 set x
444 first=
446 case "$arg" in
448 set "$@" "$arg" ;;
450 set "$@" "$prefix$arg" ;;
451 esac
452 done
453 shift
456 check_patch_format "$@"
458 split_patches "$@"
460 # -i can and must be given when resuming; everything
461 # else is kept
462 echo " $git_apply_opt" >"$dotest/apply-opt"
463 echo "$threeway" >"$dotest/threeway"
464 echo "$sign" >"$dotest/sign"
465 echo "$utf8" >"$dotest/utf8"
466 echo "$keep" >"$dotest/keep"
467 echo "$keepcr" >"$dotest/keepcr"
468 echo "$scissors" >"$dotest/scissors"
469 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
470 echo "$GIT_QUIET" >"$dotest/quiet"
471 echo 1 >"$dotest/next"
472 if test -n "$rebasing"
473 then
474 : >"$dotest/rebasing"
475 else
476 : >"$dotest/applying"
477 if test -n "$HAS_HEAD"
478 then
479 git update-ref ORIG_HEAD HEAD
480 else
481 git update-ref -d ORIG_HEAD >/dev/null 2>&1
486 case "$resolved" in
488 case "$HAS_HEAD" in
490 files=$(git ls-files) ;;
492 files=$(git diff-index --cached --name-only HEAD --) ;;
493 esac || exit
494 if test "$files"
495 then
496 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
497 die "Dirty index: cannot apply patches (dirty: $files)"
499 esac
501 if test "$(cat "$dotest/utf8")" = t
502 then
503 utf8=-u
504 else
505 utf8=-n
507 if test "$(cat "$dotest/keep")" = t
508 then
509 keep=-k
511 case "$(cat "$dotest/keepcr")" in
513 keepcr=--keep-cr ;;
515 keepcr=--no-keep-cr ;;
516 esac
517 case "$(cat "$dotest/scissors")" in
519 scissors=--scissors ;;
521 scissors=--no-scissors ;;
522 esac
523 if test "$(cat "$dotest/no_inbody_headers")" = t
524 then
525 no_inbody_headers=--no-inbody-headers
526 else
527 no_inbody_headers=
529 if test "$(cat "$dotest/quiet")" = t
530 then
531 GIT_QUIET=t
533 if test "$(cat "$dotest/threeway")" = t
534 then
535 threeway=t
537 git_apply_opt=$(cat "$dotest/apply-opt")
538 if test "$(cat "$dotest/sign")" = t
539 then
540 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
541 s/>.*/>/
542 s/^/Signed-off-by: /'
544 else
545 SIGNOFF=
548 last=`cat "$dotest/last"`
549 this=`cat "$dotest/next"`
550 if test "$skip" = t
551 then
552 this=`expr "$this" + 1`
553 resume=
556 if test "$this" -gt "$last"
557 then
558 say Nothing to do.
559 rm -fr "$dotest"
560 exit
563 while test "$this" -le "$last"
565 msgnum=`printf "%0${prec}d" $this`
566 next=`expr "$this" + 1`
567 test -f "$dotest/$msgnum" || {
568 resume=
569 go_next
570 continue
573 # If we are not resuming, parse and extract the patch information
574 # into separate files:
575 # - info records the authorship and title
576 # - msg is the rest of commit log message
577 # - patch is the patch body.
579 # When we are resuming, these files are either already prepared
580 # by the user, or the user can tell us to do so by --resolved flag.
581 case "$resume" in
583 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
584 <"$dotest/$msgnum" >"$dotest/info" ||
585 stop_here $this
587 # skip pine's internal folder data
588 sane_grep '^Author: Mail System Internal Data$' \
589 <"$dotest"/info >/dev/null &&
590 go_next && continue
592 test -s "$dotest/patch" || {
593 echo "Patch is empty. Was it split wrong?"
594 stop_here $this
596 rm -f "$dotest/original-commit" "$dotest/author-script"
597 if test -f "$dotest/rebasing" &&
598 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
599 -e q "$dotest/$msgnum") &&
600 test "$(git cat-file -t "$commit")" = commit
601 then
602 git cat-file commit "$commit" |
603 sed -e '1,/^$/d' >"$dotest/msg-clean"
604 echo "$commit" > "$dotest/original-commit"
605 get_author_ident_from_commit "$commit" > "$dotest/author-script"
606 else
608 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
609 echo
610 cat "$dotest/msg"
612 git stripspace > "$dotest/msg-clean"
615 esac
617 if test -f "$dotest/author-script"
618 then
619 eval $(cat "$dotest/author-script")
620 else
621 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
622 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
623 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
626 if test -z "$GIT_AUTHOR_EMAIL"
627 then
628 echo "Patch does not have a valid e-mail address."
629 stop_here $this
632 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
634 case "$resume" in
636 if test '' != "$SIGNOFF"
637 then
638 LAST_SIGNED_OFF_BY=`
639 sed -ne '/^Signed-off-by: /p' \
640 "$dotest/msg-clean" |
641 sed -ne '$p'
643 ADD_SIGNOFF=`
644 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
645 test '' = "$LAST_SIGNED_OFF_BY" && echo
646 echo "$SIGNOFF"
648 else
649 ADD_SIGNOFF=
652 if test -s "$dotest/msg-clean"
653 then
654 cat "$dotest/msg-clean"
656 if test '' != "$ADD_SIGNOFF"
657 then
658 echo "$ADD_SIGNOFF"
660 } >"$dotest/final-commit"
663 case "$resolved$interactive" in
665 # This is used only for interactive view option.
666 git diff-index -p --cached HEAD -- >"$dotest/patch"
668 esac
669 esac
671 resume=
672 if test "$interactive" = t
673 then
674 test -t 0 ||
675 die "cannot be interactive without stdin connected to a terminal."
676 action=again
677 while test "$action" = again
679 echo "Commit Body is:"
680 echo "--------------------------"
681 cat "$dotest/final-commit"
682 echo "--------------------------"
683 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
684 read reply
685 case "$reply" in
686 [yY]*) action=yes ;;
687 [aA]*) action=yes interactive= ;;
688 [nN]*) action=skip ;;
689 [eE]*) git_editor "$dotest/final-commit"
690 action=again ;;
691 [vV]*) action=again
692 git_pager "$dotest/patch" ;;
693 *) action=again ;;
694 esac
695 done
696 else
697 action=yes
699 FIRSTLINE=$(sed 1q "$dotest/final-commit")
701 if test $action = skip
702 then
703 go_next
704 continue
707 if test -x "$GIT_DIR"/hooks/applypatch-msg
708 then
709 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
710 stop_here $this
713 say "Applying: $FIRSTLINE"
715 case "$resolved" in
717 # When we are allowed to fall back to 3-way later, don't give
718 # false errors during the initial attempt.
719 squelch=
720 if test "$threeway" = t
721 then
722 squelch='>/dev/null 2>&1 '
724 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
725 apply_status=$?
728 # Resolved means the user did all the hard work, and
729 # we do not have to do any patch application. Just
730 # trust what the user has in the index file and the
731 # working tree.
732 resolved=
733 git diff-index --quiet --cached HEAD -- && {
734 echo "No changes - did you forget to use 'git add'?"
735 stop_here_user_resolve $this
737 unmerged=$(git ls-files -u)
738 if test -n "$unmerged"
739 then
740 echo "You still have unmerged paths in your index"
741 echo "did you forget to use 'git add'?"
742 stop_here_user_resolve $this
744 apply_status=0
745 git rerere
747 esac
749 if test $apply_status != 0 && test "$threeway" = t
750 then
751 if (fall_back_3way)
752 then
753 # Applying the patch to an earlier tree and merging the
754 # result may have produced the same tree as ours.
755 git diff-index --quiet --cached HEAD -- && {
756 say No changes -- Patch already applied.
757 go_next
758 continue
760 # clear apply_status -- we have successfully merged.
761 apply_status=0
764 if test $apply_status != 0
765 then
766 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
767 stop_here_user_resolve $this
770 if test -x "$GIT_DIR"/hooks/pre-applypatch
771 then
772 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
775 tree=$(git write-tree) &&
776 commit=$(
777 if test -n "$ignore_date"
778 then
779 GIT_AUTHOR_DATE=
781 parent=$(git rev-parse --verify -q HEAD) ||
782 say >&2 "applying to an empty history"
784 if test -n "$committer_date_is_author_date"
785 then
786 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
787 export GIT_COMMITTER_DATE
788 fi &&
789 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
790 ) &&
791 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
792 stop_here $this
794 if test -f "$dotest/original-commit"; then
795 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
798 if test -x "$GIT_DIR"/hooks/post-applypatch
799 then
800 "$GIT_DIR"/hooks/post-applypatch
803 go_next
804 done
806 if test -s "$dotest"/rewritten; then
807 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
808 if test -x "$GIT_DIR"/hooks/post-rewrite; then
809 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
813 rm -fr "$dotest"
814 git gc --auto