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