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