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