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