am: format is in $patch_format, not parse_patch
[git/gitweb.git] / git-am.sh
blob78e3ee039f921a0ae5d121340153fee93ab45154
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 whitespace= pass it through git-apply
19 directory= pass it through git-apply
20 C= pass it through git-apply
21 p= pass it through git-apply
22 patch-format= format the patch(es) are in
23 reject pass it through git-apply
24 resolvemsg= override error message when patch failure occurs
25 r,resolved to be used after a patch failure
26 skip skip the current patch
27 abort restore the original branch and abort the patching operation.
28 committer-date-is-author-date lie about committer date
29 ignore-date use current timestamp for author date
30 rebasing* (internal use for git-rebase)"
32 . git-sh-setup
33 prefix=$(git rev-parse --show-prefix)
34 set_reflog_action am
35 require_work_tree
36 cd_to_toplevel
38 git var GIT_COMMITTER_IDENT >/dev/null ||
39 die "You need to set your committer info first"
41 if git rev-parse --verify -q HEAD >/dev/null
42 then
43 HAS_HEAD=yes
44 else
45 HAS_HEAD=
48 sq () {
49 git rev-parse --sq-quote "$@"
52 stop_here () {
53 echo "$1" >"$dotest/next"
54 exit 1
57 stop_here_user_resolve () {
58 if [ -n "$resolvemsg" ]; then
59 printf '%s\n' "$resolvemsg"
60 stop_here $1
62 cmdline="git am"
63 if test '' != "$interactive"
64 then
65 cmdline="$cmdline -i"
67 if test '' != "$threeway"
68 then
69 cmdline="$cmdline -3"
71 echo "When you have resolved this problem run \"$cmdline --resolved\"."
72 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
73 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
75 stop_here $1
78 go_next () {
79 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
80 "$dotest/patch" "$dotest/info"
81 echo "$next" >"$dotest/next"
82 this=$next
85 cannot_fallback () {
86 echo "$1"
87 echo "Cannot fall back to three-way merge."
88 exit 1
91 fall_back_3way () {
92 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
94 rm -fr "$dotest"/patch-merge-*
95 mkdir "$dotest/patch-merge-tmp-dir"
97 # First see if the patch records the index info that we can use.
98 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
99 "$dotest/patch" &&
100 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
101 git write-tree >"$dotest/patch-merge-base+" ||
102 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
104 say Using index info to reconstruct a base tree...
105 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
106 git apply --cached <"$dotest/patch"
107 then
108 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
109 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
110 else
111 cannot_fallback "Did you hand edit your patch?
112 It does not apply to blobs recorded in its index."
115 test -f "$dotest/patch-merge-index" &&
116 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
117 orig_tree=$(cat "$dotest/patch-merge-base") &&
118 rm -fr "$dotest"/patch-merge-* || exit 1
120 say Falling back to patching base and 3-way merge...
122 # This is not so wrong. Depending on which base we picked,
123 # orig_tree may be wildly different from ours, but his_tree
124 # has the same set of wildly different changes in parts the
125 # patch did not touch, so recursive ends up canceling them,
126 # saying that we reverted all those changes.
128 eval GITHEAD_$his_tree='"$FIRSTLINE"'
129 export GITHEAD_$his_tree
130 if test -n "$GIT_QUIET"
131 then
132 export GIT_MERGE_VERBOSITY=0
134 git-merge-recursive $orig_tree -- HEAD $his_tree || {
135 git rerere
136 echo Failed to merge in the changes.
137 exit 1
139 unset GITHEAD_$his_tree
142 clean_abort () {
143 test $# = 0 || echo >&2 "$@"
144 rm -fr "$dotest"
145 exit 1
148 patch_format=
150 check_patch_format () {
151 # early return if patch_format was set from the command line
152 if test -n "$patch_format"
153 then
154 return 0
157 # we default to mbox format if input is from stdin and for
158 # directories
159 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
160 then
161 patch_format=mbox
162 return 0
165 # otherwise, check the first few lines of the first patch to try
166 # to detect its format
168 read l1
169 read l2
170 read l3
171 case "$l1" in
172 "From "* | "From: "*)
173 patch_format=mbox
175 '# This series applies on GIT commit'*)
176 patch_format=stgit-series
178 "# HG changeset patch")
179 patch_format=hg
182 # if the second line is empty and the third is
183 # a From, Author or Date entry, this is very
184 # likely an StGIT patch
185 case "$l2,$l3" in
186 ,"From: "* | ,"Author: "* | ,"Date: "*)
187 patch_format=stgit
191 esac
193 esac
194 if test -z "$patch_format" &&
195 test -n "$l1" &&
196 test -n "$l2" &&
197 test -n "$l3"
198 then
199 # This begins with three non-empty lines. Is this a
200 # piece of e-mail a-la RFC2822? Grab all the headers,
201 # discarding the indented remainder of folded lines,
202 # and see if it looks like that they all begin with the
203 # header field names...
204 sed -n -e '/^$/q' -e '/^[ ]/d' -e p "$1" |
205 egrep -v '^[A-Za-z]+(-[A-Za-z]+)*:' >/dev/null ||
206 patch_format=mbox
208 } < "$1" || clean_abort
211 split_patches () {
212 case "$patch_format" in
213 mbox)
214 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
215 clean_abort
217 stgit-series)
218 if test $# -ne 1
219 then
220 clean_abort "Only one StGIT patch series can be applied at once"
222 series_dir=`dirname "$1"`
223 series_file="$1"
224 shift
226 set x
227 while read filename
229 set "$@" "$series_dir/$filename"
230 done
231 # remove the safety x
232 shift
233 # remove the arg coming from the first-line comment
234 shift
235 } < "$series_file" || clean_abort
236 # set the patch format appropriately
237 patch_format=stgit
238 # now handle the actual StGIT patches
239 split_patches "$@"
241 stgit)
242 this=0
243 for stgit in "$@"
245 this=`expr "$this" + 1`
246 msgnum=`printf "%0${prec}d" $this`
247 # Perl version of StGIT parse_patch. The first nonemptyline
248 # not starting with Author, From or Date is the
249 # subject, and the body starts with the next nonempty
250 # line not starting with Author, From or Date
251 perl -ne 'BEGIN { $subject = 0 }
252 if ($subject > 1) { print ; }
253 elsif (/^\s+$/) { next ; }
254 elsif (/^Author:/) { print s/Author/From/ ; }
255 elsif (/^(From|Date)/) { print ; }
256 elsif ($subject) {
257 $subject = 2 ;
258 print "\n" ;
259 print ;
260 } else {
261 print "Subject: ", $_ ;
262 $subject = 1;
264 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
265 done
266 echo "$this" > "$dotest/last"
267 this=
268 msgnum=
271 if test -n "$patch_format"
272 then
273 clean_abort "Patch format $patch_format is not supported."
274 else
275 clean_abort "Patch format detection failed."
278 esac
281 prec=4
282 dotest="$GIT_DIR/rebase-apply"
283 sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
284 resolvemsg= resume=
285 git_apply_opt=
286 committer_date_is_author_date=
287 ignore_date=
289 while test $# != 0
291 case "$1" in
292 -i|--interactive)
293 interactive=t ;;
294 -b|--binary)
295 : ;;
296 -3|--3way)
297 threeway=t ;;
298 -s|--signoff)
299 sign=t ;;
300 -u|--utf8)
301 utf8=t ;; # this is now default
302 --no-utf8)
303 utf8= ;;
304 -k|--keep)
305 keep=t ;;
306 -r|--resolved)
307 resolved=t ;;
308 --skip)
309 skip=t ;;
310 --abort)
311 abort=t ;;
312 --rebasing)
313 rebasing=t threeway=t keep=t ;;
314 -d|--dotest)
315 die "-d option is no longer supported. Do not use."
317 --resolvemsg)
318 shift; resolvemsg=$1 ;;
319 --whitespace|--directory)
320 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
321 -C|-p)
322 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
323 --patch-format)
324 shift ; patch_format="$1" ;;
325 --reject)
326 git_apply_opt="$git_apply_opt $1" ;;
327 --committer-date-is-author-date)
328 committer_date_is_author_date=t ;;
329 --ignore-date)
330 ignore_date=t ;;
331 -q|--quiet)
332 GIT_QUIET=t ;;
334 shift; break ;;
336 usage ;;
337 esac
338 shift
339 done
341 # If the dotest directory exists, but we have finished applying all the
342 # patches in them, clear it out.
343 if test -d "$dotest" &&
344 last=$(cat "$dotest/last") &&
345 next=$(cat "$dotest/next") &&
346 test $# != 0 &&
347 test "$next" -gt "$last"
348 then
349 rm -fr "$dotest"
352 if test -d "$dotest"
353 then
354 case "$#,$skip$resolved$abort" in
355 0,*t*)
356 # Explicit resume command and we do not have file, so
357 # we are happy.
358 : ;;
360 # No file input but without resume parameters; catch
361 # user error to feed us a patch from standard input
362 # when there is already $dotest. This is somewhat
363 # unreliable -- stdin could be /dev/null for example
364 # and the caller did not intend to feed us a patch but
365 # wanted to continue unattended.
366 test -t 0
369 false
371 esac ||
372 die "previous rebase directory $dotest still exists but mbox given."
373 resume=yes
375 case "$skip,$abort" in
376 t,t)
377 die "Please make up your mind. --skip or --abort?"
380 git rerere clear
381 git read-tree --reset -u HEAD HEAD
382 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
383 git reset HEAD
384 git update-ref ORIG_HEAD $orig_head
387 if test -f "$dotest/rebasing"
388 then
389 exec git rebase --abort
391 git rerere clear
392 test -f "$dotest/dirtyindex" || {
393 git read-tree --reset -u HEAD ORIG_HEAD
394 git reset ORIG_HEAD
396 rm -fr "$dotest"
397 exit ;;
398 esac
399 rm -f "$dotest/dirtyindex"
400 else
401 # Make sure we are not given --skip, --resolved, nor --abort
402 test "$skip$resolved$abort" = "" ||
403 die "Resolve operation not in progress, we are not resuming."
405 # Start afresh.
406 mkdir -p "$dotest" || exit
408 if test -n "$prefix" && test $# != 0
409 then
410 first=t
411 for arg
413 test -n "$first" && {
414 set x
415 first=
417 case "$arg" in
419 set "$@" "$arg" ;;
421 set "$@" "$prefix$arg" ;;
422 esac
423 done
424 shift
427 check_patch_format "$@"
429 split_patches "$@"
431 # -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
432 # for the resuming session after a patch failure.
433 # -i can and must be given when resuming.
434 echo " $git_apply_opt" >"$dotest/apply-opt"
435 echo "$threeway" >"$dotest/threeway"
436 echo "$sign" >"$dotest/sign"
437 echo "$utf8" >"$dotest/utf8"
438 echo "$keep" >"$dotest/keep"
439 echo "$GIT_QUIET" >"$dotest/quiet"
440 echo 1 >"$dotest/next"
441 if test -n "$rebasing"
442 then
443 : >"$dotest/rebasing"
444 else
445 : >"$dotest/applying"
446 if test -n "$HAS_HEAD"
447 then
448 git update-ref ORIG_HEAD HEAD
449 else
450 git update-ref -d ORIG_HEAD >/dev/null 2>&1
455 case "$resolved" in
457 case "$HAS_HEAD" in
459 files=$(git ls-files) ;;
461 files=$(git diff-index --cached --name-only HEAD --) ;;
462 esac || exit
463 if test "$files"
464 then
465 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
466 die "Dirty index: cannot apply patches (dirty: $files)"
468 esac
470 if test "$(cat "$dotest/utf8")" = t
471 then
472 utf8=-u
473 else
474 utf8=-n
476 if test "$(cat "$dotest/keep")" = t
477 then
478 keep=-k
480 if test "$(cat "$dotest/quiet")" = t
481 then
482 GIT_QUIET=t
484 if test "$(cat "$dotest/threeway")" = t
485 then
486 threeway=t
488 git_apply_opt=$(cat "$dotest/apply-opt")
489 if test "$(cat "$dotest/sign")" = t
490 then
491 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
492 s/>.*/>/
493 s/^/Signed-off-by: /'
495 else
496 SIGNOFF=
499 last=`cat "$dotest/last"`
500 this=`cat "$dotest/next"`
501 if test "$skip" = t
502 then
503 this=`expr "$this" + 1`
504 resume=
507 if test "$this" -gt "$last"
508 then
509 say Nothing to do.
510 rm -fr "$dotest"
511 exit
514 while test "$this" -le "$last"
516 msgnum=`printf "%0${prec}d" $this`
517 next=`expr "$this" + 1`
518 test -f "$dotest/$msgnum" || {
519 resume=
520 go_next
521 continue
524 # If we are not resuming, parse and extract the patch information
525 # into separate files:
526 # - info records the authorship and title
527 # - msg is the rest of commit log message
528 # - patch is the patch body.
530 # When we are resuming, these files are either already prepared
531 # by the user, or the user can tell us to do so by --resolved flag.
532 case "$resume" in
534 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
535 <"$dotest/$msgnum" >"$dotest/info" ||
536 stop_here $this
538 # skip pine's internal folder data
539 grep '^Author: Mail System Internal Data$' \
540 <"$dotest"/info >/dev/null &&
541 go_next && continue
543 test -s "$dotest/patch" || {
544 echo "Patch is empty. Was it split wrong?"
545 stop_here $this
547 if test -f "$dotest/rebasing" &&
548 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
549 -e q "$dotest/$msgnum") &&
550 test "$(git cat-file -t "$commit")" = commit
551 then
552 git cat-file commit "$commit" |
553 sed -e '1,/^$/d' >"$dotest/msg-clean"
554 else
555 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
556 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
558 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
559 git stripspace > "$dotest/msg-clean"
562 esac
564 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
565 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
566 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
568 if test -z "$GIT_AUTHOR_EMAIL"
569 then
570 echo "Patch does not have a valid e-mail address."
571 stop_here $this
574 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
576 case "$resume" in
578 if test '' != "$SIGNOFF"
579 then
580 LAST_SIGNED_OFF_BY=`
581 sed -ne '/^Signed-off-by: /p' \
582 "$dotest/msg-clean" |
583 sed -ne '$p'
585 ADD_SIGNOFF=`
586 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
587 test '' = "$LAST_SIGNED_OFF_BY" && echo
588 echo "$SIGNOFF"
590 else
591 ADD_SIGNOFF=
594 if test -s "$dotest/msg-clean"
595 then
596 cat "$dotest/msg-clean"
598 if test '' != "$ADD_SIGNOFF"
599 then
600 echo "$ADD_SIGNOFF"
602 } >"$dotest/final-commit"
605 case "$resolved$interactive" in
607 # This is used only for interactive view option.
608 git diff-index -p --cached HEAD -- >"$dotest/patch"
610 esac
611 esac
613 resume=
614 if test "$interactive" = t
615 then
616 test -t 0 ||
617 die "cannot be interactive without stdin connected to a terminal."
618 action=again
619 while test "$action" = again
621 echo "Commit Body is:"
622 echo "--------------------------"
623 cat "$dotest/final-commit"
624 echo "--------------------------"
625 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
626 read reply
627 case "$reply" in
628 [yY]*) action=yes ;;
629 [aA]*) action=yes interactive= ;;
630 [nN]*) action=skip ;;
631 [eE]*) git_editor "$dotest/final-commit"
632 action=again ;;
633 [vV]*) action=again
634 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
635 *) action=again ;;
636 esac
637 done
638 else
639 action=yes
641 FIRSTLINE=$(sed 1q "$dotest/final-commit")
643 if test $action = skip
644 then
645 go_next
646 continue
649 if test -x "$GIT_DIR"/hooks/applypatch-msg
650 then
651 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
652 stop_here $this
655 say "Applying: $FIRSTLINE"
657 case "$resolved" in
659 # When we are allowed to fall back to 3-way later, don't give
660 # false errors during the initial attempt.
661 squelch=
662 if test "$threeway" = t
663 then
664 squelch='>/dev/null 2>&1 '
666 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
667 apply_status=$?
670 # Resolved means the user did all the hard work, and
671 # we do not have to do any patch application. Just
672 # trust what the user has in the index file and the
673 # working tree.
674 resolved=
675 git diff-index --quiet --cached HEAD -- && {
676 echo "No changes - did you forget to use 'git add'?"
677 stop_here_user_resolve $this
679 unmerged=$(git ls-files -u)
680 if test -n "$unmerged"
681 then
682 echo "You still have unmerged paths in your index"
683 echo "did you forget to use 'git add'?"
684 stop_here_user_resolve $this
686 apply_status=0
687 git rerere
689 esac
691 if test $apply_status = 1 && test "$threeway" = t
692 then
693 if (fall_back_3way)
694 then
695 # Applying the patch to an earlier tree and merging the
696 # result may have produced the same tree as ours.
697 git diff-index --quiet --cached HEAD -- && {
698 say No changes -- Patch already applied.
699 go_next
700 continue
702 # clear apply_status -- we have successfully merged.
703 apply_status=0
706 if test $apply_status != 0
707 then
708 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
709 stop_here_user_resolve $this
712 if test -x "$GIT_DIR"/hooks/pre-applypatch
713 then
714 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
717 tree=$(git write-tree) &&
718 commit=$(
719 if test -n "$ignore_date"
720 then
721 GIT_AUTHOR_DATE=
723 parent=$(git rev-parse --verify -q HEAD) ||
724 say >&2 "applying to an empty history"
726 if test -n "$committer_date_is_author_date"
727 then
728 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
729 export GIT_COMMITTER_DATE
730 fi &&
731 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
732 ) &&
733 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
734 stop_here $this
736 if test -x "$GIT_DIR"/hooks/post-applypatch
737 then
738 "$GIT_DIR"/hooks/post-applypatch
741 go_next
742 done
744 git gc --auto
746 rm -fr "$dotest"