]> git.ipfire.org Git - thirdparty/git.git/blame - git-am.sh
t/README: A new section about test coverage
[thirdparty/git.git] / git-am.sh
CommitLineData
d1c5f2a4
JH
1#!/bin/sh
2#
d64e6b04 3# Copyright (c) 2005, 2006 Junio C Hamano
d1c5f2a4 4
c1491841 5SUBDIRECTORY_OK=Yes
78443d90
PH
6OPTIONS_KEEPDASHDASH=
7OPTIONS_SPEC="\
588c038a 8git am [options] [<mbox>|<Maildir>...]
09f8d055 9git am [options] (--resolved | --skip | --abort)
78443d90 10--
66006c63 11i,interactive run interactively
98ef23b3 12b,binary* (historical option -- no-op)
78443d90 133,3way allow fall back on 3way merging if needed
0e987a12 14q,quiet be quiet
78443d90
PH
15s,signoff add a Signed-off-by line to the commit message
16u,utf8 recode into utf8 (default)
fe1fa946 17k,keep pass -k flag to git-mailinfo
ad2c9280 18keep-cr pass --keep-cr flag to git-mailsplit for mbox format
e80d4cbe 19no-keep-cr do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
017678b4 20c,scissors strip everything before a scissors line
78443d90 21whitespace= pass it through git-apply
86c91f91
GB
22ignore-space-change pass it through git-apply
23ignore-whitespace pass it through git-apply
b47dfe9e 24directory= pass it through git-apply
78443d90
PH
25C= pass it through git-apply
26p= pass it through git-apply
a5a6755a 27patch-format= format the patch(es) are in
b80da424 28reject pass it through git-apply
78443d90 29resolvemsg= override error message when patch failure occurs
c8089af6
JK
30continue continue applying patches after resolving a conflict
31r,resolved synonyms for --continue
3041c324 32skip skip the current patch
3e5057a8 33abort restore the original branch and abort the patching operation.
3f01ad66 34committer-date-is-author-date lie about committer date
a79ec62d 35ignore-date use current timestamp for author date
cb6020bb 36rerere-autoupdate update the index with reused conflict resolution if possible
98ef23b3 37rebasing* (internal use for git-rebase)"
78443d90 38
cf1fe88c 39. git-sh-setup
bb034f83 40prefix=$(git rev-parse --show-prefix)
f9474132 41set_reflog_action am
7eff28a9 42require_work_tree
c1491841 43cd_to_toplevel
d1c5f2a4 44
460abee5
SB
45git var GIT_COMMITTER_IDENT >/dev/null ||
46 die "You need to set your committer info first"
d64e6b04 47
f79d4c8a
NS
48if git rev-parse --verify -q HEAD >/dev/null
49then
50 HAS_HEAD=yes
51else
52 HAS_HEAD=
53fi
54
d2c46310
RR
55cmdline="git am"
56if test '' != "$interactive"
57then
58 cmdline="$cmdline -i"
59fi
60if test '' != "$threeway"
61then
62 cmdline="$cmdline -3"
63fi
64
b47dfe9e 65sq () {
47c9739e 66 git rev-parse --sq-quote "$@"
b47dfe9e
JH
67}
68
d1c5f2a4
JH
69stop_here () {
70 echo "$1" >"$dotest/next"
71 exit 1
72}
73
ced9456a 74stop_here_user_resolve () {
cc120056 75 if [ -n "$resolvemsg" ]; then
a23bfaed 76 printf '%s\n' "$resolvemsg"
cc120056
SE
77 stop_here $1
78 fi
ced9456a
RS
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\"."
3e5057a8 81 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
ced9456a
RS
82
83 stop_here $1
84}
85
d1c5f2a4
JH
86go_next () {
87 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
88 "$dotest/patch" "$dotest/info"
89 echo "$next" >"$dotest/next"
90 this=$next
91}
92
fd7bcfb5
JH
93cannot_fallback () {
94 echo "$1"
95 echo "Cannot fall back to three-way merge."
96 exit 1
97}
98
d1c5f2a4
JH
99fall_back_3way () {
100 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
101
102 rm -fr "$dotest"/patch-merge-*
103 mkdir "$dotest/patch-merge-tmp-dir"
104
105 # First see if the patch records the index info that we can use.
7a988699
JS
106 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
107 "$dotest/patch" &&
fd7bcfb5 108 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
5be60078 109 git write-tree >"$dotest/patch-merge-base+" ||
b1440cc8 110 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
fd7bcfb5 111
0e987a12 112 say Using index info to reconstruct a base tree...
fd7bcfb5 113 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
cb3a160d 114 git apply --cached <"$dotest/patch"
d1c5f2a4 115 then
d1c5f2a4
JH
116 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
117 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
fd7bcfb5
JH
118 else
119 cannot_fallback "Did you hand edit your patch?
120It does not apply to blobs recorded in its index."
d1c5f2a4
JH
121 fi
122
123 test -f "$dotest/patch-merge-index" &&
5be60078 124 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
d1c5f2a4
JH
125 orig_tree=$(cat "$dotest/patch-merge-base") &&
126 rm -fr "$dotest"/patch-merge-* || exit 1
127
0e987a12 128 say Falling back to patching base and 3-way merge...
d1c5f2a4
JH
129
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
579c9bb1 133 # patch did not touch, so recursive ends up canceling them,
d1c5f2a4
JH
134 # saying that we reverted all those changes.
135
2e6e3e82 136 eval GITHEAD_$his_tree='"$FIRSTLINE"'
579c9bb1 137 export GITHEAD_$his_tree
0e987a12
SB
138 if test -n "$GIT_QUIET"
139 then
140 export GIT_MERGE_VERBOSITY=0
141 fi
579c9bb1 142 git-merge-recursive $orig_tree -- HEAD $his_tree || {
cb6020bb 143 git rerere $allow_rerere_autoupdate
d1c5f2a4
JH
144 echo Failed to merge in the changes.
145 exit 1
146 }
579c9bb1 147 unset GITHEAD_$his_tree
d1c5f2a4
JH
148}
149
0cd29a03
GB
150clean_abort () {
151 test $# = 0 || echo >&2 "$@"
152 rm -fr "$dotest"
153 exit 1
154}
155
a5a6755a
GB
156patch_format=
157
158check_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
163 fi
15ced753
GB
164
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
171 fi
172
173 # otherwise, check the first few lines of the first patch to try
174 # to detect its format
175 {
176 read l1
177 read l2
178 read l3
179 case "$l1" in
180 "From "* | "From: "*)
181 patch_format=mbox
182 ;;
183 '# This series applies on GIT commit'*)
184 patch_format=stgit-series
185 ;;
186 "# HG changeset patch")
187 patch_format=hg
188 ;;
189 *)
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
196 ;;
197 *)
198 ;;
199 esac
200 ;;
201 esac
0fcb2caf
JH
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...
e3f67d30
SB
212 tr -d '\015' <"$1" |
213 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
e1622bfc 214 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
0fcb2caf
JH
215 patch_format=mbox
216 fi
0cd29a03 217 } < "$1" || clean_abort
a5a6755a
GB
218}
219
220split_patches () {
221 case "$patch_format" in
222 mbox)
e80d4cbe 223 if test -n "$rebasing" || test t = "$keepcr"
ad2c9280
SH
224 then
225 keep_cr=--keep-cr
226 else
227 keep_cr=
228 fi
c2ca1d79 229 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
0cd29a03 230 clean_abort
a5a6755a 231 ;;
c574e683
GB
232 stgit-series)
233 if test $# -ne 1
234 then
0cd29a03 235 clean_abort "Only one StGIT patch series can be applied at once"
c574e683
GB
236 fi
237 series_dir=`dirname "$1"`
238 series_file="$1"
239 shift
240 {
241 set x
242 while read filename
243 do
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
0cd29a03 250 } < "$series_file" || clean_abort
c574e683
GB
251 # set the patch format appropriately
252 patch_format=stgit
253 # now handle the actual StGIT patches
254 split_patches "$@"
255 ;;
256 stgit)
257 this=0
258 for stgit in "$@"
259 do
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;
278 }
0cd29a03 279 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
c574e683
GB
280 done
281 echo "$this" > "$dotest/last"
282 this=
283 msgnum=
284 ;;
a5a6755a 285 *)
46caf505
NS
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."
290 fi
a5a6755a
GB
291 ;;
292 esac
293}
294
d1c5f2a4 295prec=4
51ef1daa 296dotest="$GIT_DIR/rebase-apply"
ad2c9280 297sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
d25e5159 298resolvemsg= resume= scissors= no_inbody_headers=
67dad687 299git_apply_opt=
3f01ad66 300committer_date_is_author_date=
a79ec62d 301ignore_date=
cb6020bb 302allow_rerere_autoupdate=
d1c5f2a4 303
e80d4cbe
SH
304if test "$(git config --bool --get am.keepcr)" = true
305then
306 keepcr=t
307fi
308
822f7c73 309while test $# != 0
d1c5f2a4
JH
310do
311 case "$1" in
78443d90
PH
312 -i|--interactive)
313 interactive=t ;;
314 -b|--binary)
cb3a160d 315 : ;;
78443d90
PH
316 -3|--3way)
317 threeway=t ;;
dfdd7e66 318 -s|--signoff)
78443d90
PH
319 sign=t ;;
320 -u|--utf8)
321 utf8=t ;; # this is now default
322 --no-utf8)
323 utf8= ;;
324 -k|--keep)
325 keep=t ;;
017678b4
JH
326 -c|--scissors)
327 scissors=t ;;
328 --no-scissors)
329 scissors=f ;;
c8089af6 330 -r|--resolved|--continue)
78443d90
PH
331 resolved=t ;;
332 --skip)
333 skip=t ;;
3e5057a8
NS
334 --abort)
335 abort=t ;;
3041c324 336 --rebasing)
d25e5159 337 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
78443d90 338 -d|--dotest)
e72c7406
JH
339 die "-d option is no longer supported. Do not use."
340 ;;
78443d90
PH
341 --resolvemsg)
342 shift; resolvemsg=$1 ;;
b47dfe9e
JH
343 --whitespace|--directory)
344 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
78443d90 345 -C|-p)
b47dfe9e 346 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
a5a6755a
GB
347 --patch-format)
348 shift ; patch_format="$1" ;;
86c91f91 349 --reject|--ignore-whitespace|--ignore-space-change)
b80da424 350 git_apply_opt="$git_apply_opt $1" ;;
3f01ad66
JH
351 --committer-date-is-author-date)
352 committer_date_is_author_date=t ;;
a79ec62d
NS
353 --ignore-date)
354 ignore_date=t ;;
cb6020bb
JH
355 --rerere-autoupdate|--no-rerere-autoupdate)
356 allow_rerere_autoupdate="$1" ;;
0e987a12
SB
357 -q|--quiet)
358 GIT_QUIET=t ;;
ad2c9280
SH
359 --keep-cr)
360 keepcr=t ;;
e80d4cbe
SH
361 --no-keep-cr)
362 keepcr=f ;;
d1c5f2a4 363 --)
78443d90 364 shift; break ;;
d1c5f2a4 365 *)
78443d90 366 usage ;;
d1c5f2a4 367 esac
78443d90 368 shift
d1c5f2a4
JH
369done
370
0c15cc92
JH
371# If the dotest directory exists, but we have finished applying all the
372# patches in them, clear it out.
d1c5f2a4
JH
373if test -d "$dotest" &&
374 last=$(cat "$dotest/last") &&
375 next=$(cat "$dotest/next") &&
376 test $# != 0 &&
377 test "$next" -gt "$last"
378then
379 rm -fr "$dotest"
380fi
381
382if test -d "$dotest"
383then
3e5057a8 384 case "$#,$skip$resolved$abort" in
c95b1389
JH
385 0,*t*)
386 # Explicit resume command and we do not have file, so
387 # we are happy.
388 : ;;
389 0,)
390 # No file input but without resume parameters; catch
391 # user error to feed us a patch from standard input
e72c7406 392 # when there is already $dotest. This is somewhat
c95b1389
JH
393 # unreliable -- stdin could be /dev/null for example
394 # and the caller did not intend to feed us a patch but
395 # wanted to continue unattended.
98ef23b3 396 test -t 0
c95b1389
JH
397 ;;
398 *)
399 false
400 ;;
401 esac ||
28ed6e7b 402 die "previous rebase directory $dotest still exists but mbox given."
271440e3 403 resume=yes
3e5057a8 404
95f8ebbf 405 case "$skip,$abort" in
2d56a136
JH
406 t,t)
407 die "Please make up your mind. --skip or --abort?"
408 ;;
95f8ebbf
OM
409 t,)
410 git rerere clear
411 git read-tree --reset -u HEAD HEAD
412 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
413 git reset HEAD
414 git update-ref ORIG_HEAD $orig_head
415 ;;
416 ,t)
2d56a136
JH
417 if test -f "$dotest/rebasing"
418 then
419 exec git rebase --abort
420 fi
3e5057a8 421 git rerere clear
c767184d
MG
422 test -f "$dotest/dirtyindex" || {
423 git read-tree --reset -u HEAD ORIG_HEAD
424 git reset ORIG_HEAD
425 }
3e5057a8
NS
426 rm -fr "$dotest"
427 exit ;;
428 esac
c767184d 429 rm -f "$dotest/dirtyindex"
d1c5f2a4 430else
3e5057a8
NS
431 # Make sure we are not given --skip, --resolved, nor --abort
432 test "$skip$resolved$abort" = "" ||
cc120056 433 die "Resolve operation not in progress, we are not resuming."
d1c5f2a4
JH
434
435 # Start afresh.
436 mkdir -p "$dotest" || exit
437
bb034f83
JH
438 if test -n "$prefix" && test $# != 0
439 then
440 first=t
441 for arg
442 do
443 test -n "$first" && {
444 set x
445 first=
446 }
447 case "$arg" in
448 /*)
449 set "$@" "$arg" ;;
450 *)
451 set "$@" "$prefix$arg" ;;
452 esac
453 done
454 shift
455 fi
a5a6755a
GB
456
457 check_patch_format "$@"
458
459 split_patches "$@"
d1c5f2a4 460
017678b4
JH
461 # -i can and must be given when resuming; everything
462 # else is kept
9b9f5a22 463 echo " $git_apply_opt" >"$dotest/apply-opt"
22db240d 464 echo "$threeway" >"$dotest/threeway"
d1c5f2a4
JH
465 echo "$sign" >"$dotest/sign"
466 echo "$utf8" >"$dotest/utf8"
467 echo "$keep" >"$dotest/keep"
ad2c9280 468 echo "$keepcr" >"$dotest/keepcr"
017678b4 469 echo "$scissors" >"$dotest/scissors"
d25e5159 470 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
0e987a12 471 echo "$GIT_QUIET" >"$dotest/quiet"
d1c5f2a4 472 echo 1 >"$dotest/next"
3041c324
JH
473 if test -n "$rebasing"
474 then
475 : >"$dotest/rebasing"
476 else
477 : >"$dotest/applying"
f79d4c8a
NS
478 if test -n "$HAS_HEAD"
479 then
480 git update-ref ORIG_HEAD HEAD
481 else
482 git update-ref -d ORIG_HEAD >/dev/null 2>&1
483 fi
3041c324 484 fi
d1c5f2a4
JH
485fi
486
0c15cc92
JH
487case "$resolved" in
488'')
f79d4c8a
NS
489 case "$HAS_HEAD" in
490 '')
491 files=$(git ls-files) ;;
492 ?*)
493 files=$(git diff-index --cached --name-only HEAD --) ;;
494 esac || exit
c767184d
MG
495 if test "$files"
496 then
f79d4c8a 497 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
c767184d
MG
498 die "Dirty index: cannot apply patches (dirty: $files)"
499 fi
0c15cc92
JH
500esac
501
d1c5f2a4
JH
502if test "$(cat "$dotest/utf8")" = t
503then
504 utf8=-u
bb1091a4
JH
505else
506 utf8=-n
d1c5f2a4
JH
507fi
508if test "$(cat "$dotest/keep")" = t
509then
510 keep=-k
511fi
e80d4cbe
SH
512case "$(cat "$dotest/keepcr")" in
513t)
514 keepcr=--keep-cr ;;
515f)
516 keepcr=--no-keep-cr ;;
517esac
017678b4
JH
518case "$(cat "$dotest/scissors")" in
519t)
520 scissors=--scissors ;;
521f)
522 scissors=--no-scissors ;;
523esac
d25e5159
LS
524if test "$(cat "$dotest/no_inbody_headers")" = t
525then
526 no_inbody_headers=--no-inbody-headers
527else
528 no_inbody_headers=
529fi
0e987a12
SB
530if test "$(cat "$dotest/quiet")" = t
531then
532 GIT_QUIET=t
533fi
22db240d
JH
534if test "$(cat "$dotest/threeway")" = t
535then
536 threeway=t
537fi
9b9f5a22 538git_apply_opt=$(cat "$dotest/apply-opt")
d1c5f2a4
JH
539if test "$(cat "$dotest/sign")" = t
540then
5354a56f 541 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
d1c5f2a4
JH
542 s/>.*/>/
543 s/^/Signed-off-by: /'
544 `
545else
546 SIGNOFF=
547fi
d1c5f2a4
JH
548
549last=`cat "$dotest/last"`
550this=`cat "$dotest/next"`
551if test "$skip" = t
552then
553 this=`expr "$this" + 1`
69224716 554 resume=
d1c5f2a4
JH
555fi
556
557if test "$this" -gt "$last"
558then
0e987a12 559 say Nothing to do.
d1c5f2a4
JH
560 rm -fr "$dotest"
561 exit
562fi
563
564while test "$this" -le "$last"
565do
566 msgnum=`printf "%0${prec}d" $this`
567 next=`expr "$this" + 1`
568 test -f "$dotest/$msgnum" || {
69224716 569 resume=
d1c5f2a4
JH
570 go_next
571 continue
572 }
0c15cc92
JH
573
574 # If we are not resuming, parse and extract the patch information
575 # into separate files:
576 # - info records the authorship and title
577 # - msg is the rest of commit log message
578 # - patch is the patch body.
579 #
580 # When we are resuming, these files are either already prepared
581 # by the user, or the user can tell us to do so by --resolved flag.
271440e3
JH
582 case "$resume" in
583 '')
d25e5159 584 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
271440e3
JH
585 <"$dotest/$msgnum" >"$dotest/info" ||
586 stop_here $this
ca193cf1
JS
587
588 # skip pine's internal folder data
e1622bfc 589 sane_grep '^Author: Mail System Internal Data$' \
ca193cf1
JS
590 <"$dotest"/info >/dev/null &&
591 go_next && continue
592
28ed6e7b 593 test -s "$dotest/patch" || {
6777c380 594 echo "Patch is empty. Was it split wrong?"
dc267b1a
RR
595 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
596 echo "To restore the original branch and stop patching run \"$cmdline --abort\"."
87ab7992
DZ
597 stop_here $this
598 }
43c23251 599 rm -f "$dotest/original-commit" "$dotest/author-script"
5e835cac
JH
600 if test -f "$dotest/rebasing" &&
601 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
602 -e q "$dotest/$msgnum") &&
603 test "$(git cat-file -t "$commit")" = commit
604 then
605 git cat-file commit "$commit" |
606 sed -e '1,/^$/d' >"$dotest/msg-clean"
96e19488 607 echo "$commit" > "$dotest/original-commit"
43c23251 608 get_author_ident_from_commit "$commit" > "$dotest/author-script"
5e835cac 609 else
c970a6fd
JH
610 {
611 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
612 echo
613 cat "$dotest/msg"
614 } |
615 git stripspace > "$dotest/msg-clean"
5e835cac 616 fi
271440e3
JH
617 ;;
618 esac
d1c5f2a4 619
43c23251
JS
620 if test -f "$dotest/author-script"
621 then
622 eval $(cat "$dotest/author-script")
623 else
624 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
625 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
626 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
627 fi
e0e3ba20 628
3b78959e 629 if test -z "$GIT_AUTHOR_EMAIL"
e0e3ba20 630 then
3b78959e 631 echo "Patch does not have a valid e-mail address."
e0e3ba20
JH
632 stop_here $this
633 fi
634
2c674191 635 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
d1c5f2a4 636
4bfb6b62
JH
637 case "$resume" in
638 '')
639 if test '' != "$SIGNOFF"
640 then
d1c5f2a4 641 LAST_SIGNED_OFF_BY=`
4bfb6b62
JH
642 sed -ne '/^Signed-off-by: /p' \
643 "$dotest/msg-clean" |
b4ce54fc 644 sed -ne '$p'
d1c5f2a4 645 `
4bfb6b62
JH
646 ADD_SIGNOFF=`
647 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
d1c5f2a4
JH
648 test '' = "$LAST_SIGNED_OFF_BY" && echo
649 echo "$SIGNOFF"
4bfb6b62
JH
650 }`
651 else
d1c5f2a4 652 ADD_SIGNOFF=
4bfb6b62
JH
653 fi
654 {
d1c5f2a4
JH
655 if test -s "$dotest/msg-clean"
656 then
d1c5f2a4
JH
657 cat "$dotest/msg-clean"
658 fi
659 if test '' != "$ADD_SIGNOFF"
660 then
661 echo "$ADD_SIGNOFF"
662 fi
4bfb6b62
JH
663 } >"$dotest/final-commit"
664 ;;
0c15cc92 665 *)
6d28644d 666 case "$resolved$interactive" in
0c15cc92
JH
667 tt)
668 # This is used only for interactive view option.
38762c47 669 git diff-index -p --cached HEAD -- >"$dotest/patch"
0c15cc92
JH
670 ;;
671 esac
4bfb6b62 672 esac
d1c5f2a4 673
4bfb6b62 674 resume=
d1c5f2a4
JH
675 if test "$interactive" = t
676 then
a1451104
JH
677 test -t 0 ||
678 die "cannot be interactive without stdin connected to a terminal."
d1c5f2a4
JH
679 action=again
680 while test "$action" = again
681 do
682 echo "Commit Body is:"
683 echo "--------------------------"
684 cat "$dotest/final-commit"
685 echo "--------------------------"
9754563c 686 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
d1c5f2a4
JH
687 read reply
688 case "$reply" in
f89ad67f
JH
689 [yY]*) action=yes ;;
690 [aA]*) action=yes interactive= ;;
691 [nN]*) action=skip ;;
ef0c2abf 692 [eE]*) git_editor "$dotest/final-commit"
d1c5f2a4 693 action=again ;;
f89ad67f 694 [vV]*) action=again
f6dff119 695 git_pager "$dotest/patch" ;;
f89ad67f 696 *) action=again ;;
d1c5f2a4
JH
697 esac
698 done
699 else
700 action=yes
701 fi
92f65e6a
RR
702
703 if test -f "$dotest/final-commit"
704 then
705 FIRSTLINE=$(sed 1q "$dotest/final-commit")
706 else
707 FIRSTLINE=""
708 fi
d1c5f2a4
JH
709
710 if test $action = skip
711 then
712 go_next
713 continue
714 fi
715
716 if test -x "$GIT_DIR"/hooks/applypatch-msg
717 then
718 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
719 stop_here $this
720 fi
721
0e987a12 722 say "Applying: $FIRSTLINE"
d1c5f2a4 723
0c15cc92
JH
724 case "$resolved" in
725 '')
3ddd1703
SB
726 # When we are allowed to fall back to 3-way later, don't give
727 # false errors during the initial attempt.
728 squelch=
729 if test "$threeway" = t
730 then
731 squelch='>/dev/null 2>&1 '
732 fi
733 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
0c15cc92
JH
734 apply_status=$?
735 ;;
736 t)
737 # Resolved means the user did all the hard work, and
738 # we do not have to do any patch application. Just
739 # trust what the user has in the index file and the
740 # working tree.
741 resolved=
38762c47 742 git diff-index --quiet --cached HEAD -- && {
64646d11 743 echo "No changes - did you forget to use 'git add'?"
05bdcfe5
JK
744 echo "If there is nothing left to stage, chances are that something else"
745 echo "already introduced the same changes; you might want to skip this patch."
ced9456a 746 stop_here_user_resolve $this
06aff47b 747 }
5be60078 748 unmerged=$(git ls-files -u)
c1d1128b
JH
749 if test -n "$unmerged"
750 then
751 echo "You still have unmerged paths in your index"
64646d11 752 echo "did you forget to use 'git add'?"
ced9456a 753 stop_here_user_resolve $this
c1d1128b 754 fi
0c15cc92 755 apply_status=0
b4372ef1 756 git rerere
0c15cc92
JH
757 ;;
758 esac
759
0ba17dd0 760 if test $apply_status != 0 && test "$threeway" = t
d1c5f2a4 761 then
73319032 762 if (fall_back_3way)
d1c5f2a4 763 then
73319032
JH
764 # Applying the patch to an earlier tree and merging the
765 # result may have produced the same tree as ours.
38762c47 766 git diff-index --quiet --cached HEAD -- && {
0e987a12 767 say No changes -- Patch already applied.
06aff47b
AR
768 go_next
769 continue
770 }
73319032
JH
771 # clear apply_status -- we have successfully merged.
772 apply_status=0
d1c5f2a4
JH
773 fi
774 fi
775 if test $apply_status != 0
776 then
4d2e283a 777 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
ced9456a 778 stop_here_user_resolve $this
d1c5f2a4
JH
779 fi
780
781 if test -x "$GIT_DIR"/hooks/pre-applypatch
782 then
783 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
784 fi
785
5be60078 786 tree=$(git write-tree) &&
3f01ad66 787 commit=$(
a79ec62d
NS
788 if test -n "$ignore_date"
789 then
790 GIT_AUTHOR_DATE=
791 fi
f79d4c8a 792 parent=$(git rev-parse --verify -q HEAD) ||
0e987a12 793 say >&2 "applying to an empty history"
f79d4c8a 794
3f01ad66
JH
795 if test -n "$committer_date_is_author_date"
796 then
797 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
798 export GIT_COMMITTER_DATE
799 fi &&
ea10b60c 800 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
3f01ad66 801 ) &&
2e6e3e82 802 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
d1c5f2a4
JH
803 stop_here $this
804
96e19488
TR
805 if test -f "$dotest/original-commit"; then
806 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
807 fi
808
d1c5f2a4
JH
809 if test -x "$GIT_DIR"/hooks/post-applypatch
810 then
811 "$GIT_DIR"/hooks/post-applypatch
812 fi
813
814 go_next
815done
816
eb2151bb
TR
817if test -s "$dotest"/rewritten; then
818 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
819 if test -x "$GIT_DIR"/hooks/post-rewrite; then
96e19488 820 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
eb2151bb 821 fi
96e19488
TR
822fi
823
d1c5f2a4 824rm -fr "$dotest"
29b67543 825git gc --auto