git-am: Add command line parameter `--keep-cr` passing it to git-mailsplit
[git/mingw.git] / git-am.sh
blobdc8d242beb473410087f237136958116c0d09339
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 c,scissors strip everything before a scissors line
20 whitespace= pass it through git-apply
21 ignore-space-change pass it through git-apply
22 ignore-whitespace pass it through git-apply
23 directory= pass it through git-apply
24 C= pass it through git-apply
25 p= pass it through git-apply
26 patch-format= format the patch(es) are in
27 reject pass it through git-apply
28 resolvemsg= override error message when patch failure occurs
29 continue continue applying patches after resolving a conflict
30 r,resolved synonyms for --continue
31 skip skip the current patch
32 abort restore the original branch and abort the patching operation.
33 committer-date-is-author-date lie about committer date
34 ignore-date use current timestamp for author date
35 rerere-autoupdate update the index with reused conflict resolution if possible
36 rebasing* (internal use for git-rebase)"
38 . git-sh-setup
39 prefix=$(git rev-parse --show-prefix)
40 set_reflog_action am
41 require_work_tree
42 cd_to_toplevel
44 git var GIT_COMMITTER_IDENT >/dev/null ||
45 die "You need to set your committer info first"
47 if git rev-parse --verify -q HEAD >/dev/null
48 then
49 HAS_HEAD=yes
50 else
51 HAS_HEAD=
54 sq () {
55 git rev-parse --sq-quote "$@"
58 stop_here () {
59 echo "$1" >"$dotest/next"
60 exit 1
63 stop_here_user_resolve () {
64 if [ -n "$resolvemsg" ]; then
65 printf '%s\n' "$resolvemsg"
66 stop_here $1
68 cmdline="git am"
69 if test '' != "$interactive"
70 then
71 cmdline="$cmdline -i"
73 if test '' != "$threeway"
74 then
75 cmdline="$cmdline -3"
77 echo "When you have resolved this problem run \"$cmdline --resolved\"."
78 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
79 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
81 stop_here $1
84 go_next () {
85 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
86 "$dotest/patch" "$dotest/info"
87 echo "$next" >"$dotest/next"
88 this=$next
91 cannot_fallback () {
92 echo "$1"
93 echo "Cannot fall back to three-way merge."
94 exit 1
97 fall_back_3way () {
98 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
100 rm -fr "$dotest"/patch-merge-*
101 mkdir "$dotest/patch-merge-tmp-dir"
103 # First see if the patch records the index info that we can use.
104 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
105 "$dotest/patch" &&
106 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
107 git write-tree >"$dotest/patch-merge-base+" ||
108 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
110 say Using index info to reconstruct a base tree...
111 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
112 git apply --cached <"$dotest/patch"
113 then
114 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
115 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
116 else
117 cannot_fallback "Did you hand edit your patch?
118 It does not apply to blobs recorded in its index."
121 test -f "$dotest/patch-merge-index" &&
122 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
123 orig_tree=$(cat "$dotest/patch-merge-base") &&
124 rm -fr "$dotest"/patch-merge-* || exit 1
126 say Falling back to patching base and 3-way merge...
128 # This is not so wrong. Depending on which base we picked,
129 # orig_tree may be wildly different from ours, but his_tree
130 # has the same set of wildly different changes in parts the
131 # patch did not touch, so recursive ends up canceling them,
132 # saying that we reverted all those changes.
134 eval GITHEAD_$his_tree='"$FIRSTLINE"'
135 export GITHEAD_$his_tree
136 if test -n "$GIT_QUIET"
137 then
138 export GIT_MERGE_VERBOSITY=0
140 git-merge-recursive $orig_tree -- HEAD $his_tree || {
141 git rerere $allow_rerere_autoupdate
142 echo Failed to merge in the changes.
143 exit 1
145 unset GITHEAD_$his_tree
148 clean_abort () {
149 test $# = 0 || echo >&2 "$@"
150 rm -fr "$dotest"
151 exit 1
154 patch_format=
156 check_patch_format () {
157 # early return if patch_format was set from the command line
158 if test -n "$patch_format"
159 then
160 return 0
163 # we default to mbox format if input is from stdin and for
164 # directories
165 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
166 then
167 patch_format=mbox
168 return 0
171 # otherwise, check the first few lines of the first patch to try
172 # to detect its format
174 read l1
175 read l2
176 read l3
177 case "$l1" in
178 "From "* | "From: "*)
179 patch_format=mbox
181 '# This series applies on GIT commit'*)
182 patch_format=stgit-series
184 "# HG changeset patch")
185 patch_format=hg
188 # if the second line is empty and the third is
189 # a From, Author or Date entry, this is very
190 # likely an StGIT patch
191 case "$l2,$l3" in
192 ,"From: "* | ,"Author: "* | ,"Date: "*)
193 patch_format=stgit
197 esac
199 esac
200 if test -z "$patch_format" &&
201 test -n "$l1" &&
202 test -n "$l2" &&
203 test -n "$l3"
204 then
205 # This begins with three non-empty lines. Is this a
206 # piece of e-mail a-la RFC2822? Grab all the headers,
207 # discarding the indented remainder of folded lines,
208 # and see if it looks like that they all begin with the
209 # header field names...
210 tr -d '\015' <"$1" |
211 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
212 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
213 patch_format=mbox
215 } < "$1" || clean_abort
218 split_patches () {
219 case "$patch_format" in
220 mbox)
221 if test -n "$rebasing$keepcr"
222 then
223 keep_cr=--keep-cr
224 else
225 keep_cr=
227 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
228 clean_abort
230 stgit-series)
231 if test $# -ne 1
232 then
233 clean_abort "Only one StGIT patch series can be applied at once"
235 series_dir=`dirname "$1"`
236 series_file="$1"
237 shift
239 set x
240 while read filename
242 set "$@" "$series_dir/$filename"
243 done
244 # remove the safety x
245 shift
246 # remove the arg coming from the first-line comment
247 shift
248 } < "$series_file" || clean_abort
249 # set the patch format appropriately
250 patch_format=stgit
251 # now handle the actual StGIT patches
252 split_patches "$@"
254 stgit)
255 this=0
256 for stgit in "$@"
258 this=`expr "$this" + 1`
259 msgnum=`printf "%0${prec}d" $this`
260 # Perl version of StGIT parse_patch. The first nonemptyline
261 # not starting with Author, From or Date is the
262 # subject, and the body starts with the next nonempty
263 # line not starting with Author, From or Date
264 perl -ne 'BEGIN { $subject = 0 }
265 if ($subject > 1) { print ; }
266 elsif (/^\s+$/) { next ; }
267 elsif (/^Author:/) { print s/Author/From/ ; }
268 elsif (/^(From|Date)/) { print ; }
269 elsif ($subject) {
270 $subject = 2 ;
271 print "\n" ;
272 print ;
273 } else {
274 print "Subject: ", $_ ;
275 $subject = 1;
277 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
278 done
279 echo "$this" > "$dotest/last"
280 this=
281 msgnum=
284 if test -n "$parse_patch" ; then
285 clean_abort "Patch format $patch_format is not supported."
286 else
287 clean_abort "Patch format detection failed."
290 esac
293 prec=4
294 dotest="$GIT_DIR/rebase-apply"
295 sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
296 resolvemsg= resume= scissors= no_inbody_headers=
297 git_apply_opt=
298 committer_date_is_author_date=
299 ignore_date=
300 allow_rerere_autoupdate=
302 while test $# != 0
304 case "$1" in
305 -i|--interactive)
306 interactive=t ;;
307 -b|--binary)
308 : ;;
309 -3|--3way)
310 threeway=t ;;
311 -s|--signoff)
312 sign=t ;;
313 -u|--utf8)
314 utf8=t ;; # this is now default
315 --no-utf8)
316 utf8= ;;
317 -k|--keep)
318 keep=t ;;
319 -c|--scissors)
320 scissors=t ;;
321 --no-scissors)
322 scissors=f ;;
323 -r|--resolved|--continue)
324 resolved=t ;;
325 --skip)
326 skip=t ;;
327 --abort)
328 abort=t ;;
329 --rebasing)
330 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
331 -d|--dotest)
332 die "-d option is no longer supported. Do not use."
334 --resolvemsg)
335 shift; resolvemsg=$1 ;;
336 --whitespace|--directory)
337 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
338 -C|-p)
339 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
340 --patch-format)
341 shift ; patch_format="$1" ;;
342 --reject|--ignore-whitespace|--ignore-space-change)
343 git_apply_opt="$git_apply_opt $1" ;;
344 --committer-date-is-author-date)
345 committer_date_is_author_date=t ;;
346 --ignore-date)
347 ignore_date=t ;;
348 --rerere-autoupdate|--no-rerere-autoupdate)
349 allow_rerere_autoupdate="$1" ;;
350 -q|--quiet)
351 GIT_QUIET=t ;;
352 --keep-cr)
353 keepcr=t ;;
355 shift; break ;;
357 usage ;;
358 esac
359 shift
360 done
362 # If the dotest directory exists, but we have finished applying all the
363 # patches in them, clear it out.
364 if test -d "$dotest" &&
365 last=$(cat "$dotest/last") &&
366 next=$(cat "$dotest/next") &&
367 test $# != 0 &&
368 test "$next" -gt "$last"
369 then
370 rm -fr "$dotest"
373 if test -d "$dotest"
374 then
375 case "$#,$skip$resolved$abort" in
376 0,*t*)
377 # Explicit resume command and we do not have file, so
378 # we are happy.
379 : ;;
381 # No file input but without resume parameters; catch
382 # user error to feed us a patch from standard input
383 # when there is already $dotest. This is somewhat
384 # unreliable -- stdin could be /dev/null for example
385 # and the caller did not intend to feed us a patch but
386 # wanted to continue unattended.
387 test -t 0
390 false
392 esac ||
393 die "previous rebase directory $dotest still exists but mbox given."
394 resume=yes
396 case "$skip,$abort" in
397 t,t)
398 die "Please make up your mind. --skip or --abort?"
401 git rerere clear
402 git read-tree --reset -u HEAD HEAD
403 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
404 git reset HEAD
405 git update-ref ORIG_HEAD $orig_head
408 if test -f "$dotest/rebasing"
409 then
410 exec git rebase --abort
412 git rerere clear
413 test -f "$dotest/dirtyindex" || {
414 git read-tree --reset -u HEAD ORIG_HEAD
415 git reset ORIG_HEAD
417 rm -fr "$dotest"
418 exit ;;
419 esac
420 rm -f "$dotest/dirtyindex"
421 else
422 # Make sure we are not given --skip, --resolved, nor --abort
423 test "$skip$resolved$abort" = "" ||
424 die "Resolve operation not in progress, we are not resuming."
426 # Start afresh.
427 mkdir -p "$dotest" || exit
429 if test -n "$prefix" && test $# != 0
430 then
431 first=t
432 for arg
434 test -n "$first" && {
435 set x
436 first=
438 case "$arg" in
440 set "$@" "$arg" ;;
442 set "$@" "$prefix$arg" ;;
443 esac
444 done
445 shift
448 check_patch_format "$@"
450 split_patches "$@"
452 # -i can and must be given when resuming; everything
453 # else is kept
454 echo " $git_apply_opt" >"$dotest/apply-opt"
455 echo "$threeway" >"$dotest/threeway"
456 echo "$sign" >"$dotest/sign"
457 echo "$utf8" >"$dotest/utf8"
458 echo "$keep" >"$dotest/keep"
459 echo "$keepcr" >"$dotest/keepcr"
460 echo "$scissors" >"$dotest/scissors"
461 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
462 echo "$GIT_QUIET" >"$dotest/quiet"
463 echo 1 >"$dotest/next"
464 if test -n "$rebasing"
465 then
466 : >"$dotest/rebasing"
467 else
468 : >"$dotest/applying"
469 if test -n "$HAS_HEAD"
470 then
471 git update-ref ORIG_HEAD HEAD
472 else
473 git update-ref -d ORIG_HEAD >/dev/null 2>&1
478 case "$resolved" in
480 case "$HAS_HEAD" in
482 files=$(git ls-files) ;;
484 files=$(git diff-index --cached --name-only HEAD --) ;;
485 esac || exit
486 if test "$files"
487 then
488 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
489 die "Dirty index: cannot apply patches (dirty: $files)"
491 esac
493 if test "$(cat "$dotest/utf8")" = t
494 then
495 utf8=-u
496 else
497 utf8=-n
499 if test "$(cat "$dotest/keep")" = t
500 then
501 keep=-k
503 if test "$(cat "$dotest/keepcr")" = t
504 then
505 keepcr=--keep-cr
507 case "$(cat "$dotest/scissors")" in
509 scissors=--scissors ;;
511 scissors=--no-scissors ;;
512 esac
513 if test "$(cat "$dotest/no_inbody_headers")" = t
514 then
515 no_inbody_headers=--no-inbody-headers
516 else
517 no_inbody_headers=
519 if test "$(cat "$dotest/quiet")" = t
520 then
521 GIT_QUIET=t
523 if test "$(cat "$dotest/threeway")" = t
524 then
525 threeway=t
527 git_apply_opt=$(cat "$dotest/apply-opt")
528 if test "$(cat "$dotest/sign")" = t
529 then
530 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
531 s/>.*/>/
532 s/^/Signed-off-by: /'
534 else
535 SIGNOFF=
538 last=`cat "$dotest/last"`
539 this=`cat "$dotest/next"`
540 if test "$skip" = t
541 then
542 this=`expr "$this" + 1`
543 resume=
546 if test "$this" -gt "$last"
547 then
548 say Nothing to do.
549 rm -fr "$dotest"
550 exit
553 while test "$this" -le "$last"
555 msgnum=`printf "%0${prec}d" $this`
556 next=`expr "$this" + 1`
557 test -f "$dotest/$msgnum" || {
558 resume=
559 go_next
560 continue
563 # If we are not resuming, parse and extract the patch information
564 # into separate files:
565 # - info records the authorship and title
566 # - msg is the rest of commit log message
567 # - patch is the patch body.
569 # When we are resuming, these files are either already prepared
570 # by the user, or the user can tell us to do so by --resolved flag.
571 case "$resume" in
573 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
574 <"$dotest/$msgnum" >"$dotest/info" ||
575 stop_here $this
577 # skip pine's internal folder data
578 sane_grep '^Author: Mail System Internal Data$' \
579 <"$dotest"/info >/dev/null &&
580 go_next && continue
582 test -s "$dotest/patch" || {
583 echo "Patch is empty. Was it split wrong?"
584 stop_here $this
586 if test -f "$dotest/rebasing" &&
587 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
588 -e q "$dotest/$msgnum") &&
589 test "$(git cat-file -t "$commit")" = commit
590 then
591 git cat-file commit "$commit" |
592 sed -e '1,/^$/d' >"$dotest/msg-clean"
593 else
595 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
596 echo
597 cat "$dotest/msg"
599 git stripspace > "$dotest/msg-clean"
602 esac
604 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
605 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
606 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
608 if test -z "$GIT_AUTHOR_EMAIL"
609 then
610 echo "Patch does not have a valid e-mail address."
611 stop_here $this
614 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
616 case "$resume" in
618 if test '' != "$SIGNOFF"
619 then
620 LAST_SIGNED_OFF_BY=`
621 sed -ne '/^Signed-off-by: /p' \
622 "$dotest/msg-clean" |
623 sed -ne '$p'
625 ADD_SIGNOFF=`
626 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
627 test '' = "$LAST_SIGNED_OFF_BY" && echo
628 echo "$SIGNOFF"
630 else
631 ADD_SIGNOFF=
634 if test -s "$dotest/msg-clean"
635 then
636 cat "$dotest/msg-clean"
638 if test '' != "$ADD_SIGNOFF"
639 then
640 echo "$ADD_SIGNOFF"
642 } >"$dotest/final-commit"
645 case "$resolved$interactive" in
647 # This is used only for interactive view option.
648 git diff-index -p --cached HEAD -- >"$dotest/patch"
650 esac
651 esac
653 resume=
654 if test "$interactive" = t
655 then
656 test -t 0 ||
657 die "cannot be interactive without stdin connected to a terminal."
658 action=again
659 while test "$action" = again
661 echo "Commit Body is:"
662 echo "--------------------------"
663 cat "$dotest/final-commit"
664 echo "--------------------------"
665 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
666 read reply
667 case "$reply" in
668 [yY]*) action=yes ;;
669 [aA]*) action=yes interactive= ;;
670 [nN]*) action=skip ;;
671 [eE]*) git_editor "$dotest/final-commit"
672 action=again ;;
673 [vV]*) action=again
674 : ${GIT_PAGER=$(git var GIT_PAGER)}
675 : ${LESS=-FRSX}
676 export LESS
677 $GIT_PAGER "$dotest/patch" ;;
678 *) action=again ;;
679 esac
680 done
681 else
682 action=yes
684 FIRSTLINE=$(sed 1q "$dotest/final-commit")
686 if test $action = skip
687 then
688 go_next
689 continue
692 if test -x "$GIT_DIR"/hooks/applypatch-msg
693 then
694 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
695 stop_here $this
698 say "Applying: $FIRSTLINE"
700 case "$resolved" in
702 # When we are allowed to fall back to 3-way later, don't give
703 # false errors during the initial attempt.
704 squelch=
705 if test "$threeway" = t
706 then
707 squelch='>/dev/null 2>&1 '
709 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
710 apply_status=$?
713 # Resolved means the user did all the hard work, and
714 # we do not have to do any patch application. Just
715 # trust what the user has in the index file and the
716 # working tree.
717 resolved=
718 git diff-index --quiet --cached HEAD -- && {
719 echo "No changes - did you forget to use 'git add'?"
720 stop_here_user_resolve $this
722 unmerged=$(git ls-files -u)
723 if test -n "$unmerged"
724 then
725 echo "You still have unmerged paths in your index"
726 echo "did you forget to use 'git add'?"
727 stop_here_user_resolve $this
729 apply_status=0
730 git rerere
732 esac
734 if test $apply_status = 1 && test "$threeway" = t
735 then
736 if (fall_back_3way)
737 then
738 # Applying the patch to an earlier tree and merging the
739 # result may have produced the same tree as ours.
740 git diff-index --quiet --cached HEAD -- && {
741 say No changes -- Patch already applied.
742 go_next
743 continue
745 # clear apply_status -- we have successfully merged.
746 apply_status=0
749 if test $apply_status != 0
750 then
751 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
752 stop_here_user_resolve $this
755 if test -x "$GIT_DIR"/hooks/pre-applypatch
756 then
757 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
760 tree=$(git write-tree) &&
761 commit=$(
762 if test -n "$ignore_date"
763 then
764 GIT_AUTHOR_DATE=
766 parent=$(git rev-parse --verify -q HEAD) ||
767 say >&2 "applying to an empty history"
769 if test -n "$committer_date_is_author_date"
770 then
771 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
772 export GIT_COMMITTER_DATE
773 fi &&
774 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
775 ) &&
776 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
777 stop_here $this
779 if test -x "$GIT_DIR"/hooks/post-applypatch
780 then
781 "$GIT_DIR"/hooks/post-applypatch
784 go_next
785 done
787 rm -fr "$dotest"
788 git gc --auto