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