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