]> git.ipfire.org Git - thirdparty/git.git/blame - git-am.sh
fast-import.c::validate_raw_date(): really validate the value
[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
584c4356
JH
194 if test -z "$patch_format" &&
195 test -n "$l1" &&
196 test -n "$l2" &&
197 test -n "$l3"
198 then
199 # This begins with three non-empty lines. Is this a
200 # piece of e-mail a-la RFC2822? Grab all the headers,
201 # discarding the indented remainder of folded lines,
202 # and see if it looks like that they all begin with the
203 # header field names...
204 sed -n -e '/^$/q' -e '/^[ ]/d' -e p "$1" |
205 egrep -v '^[A-Za-z]+(-[A-Za-z]+)*:' >/dev/null ||
206 patch_format=mbox
207 fi
0cd29a03 208 } < "$1" || clean_abort
a5a6755a
GB
209}
210
211split_patches () {
212 case "$patch_format" in
213 mbox)
0cd29a03
GB
214 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
215 clean_abort
a5a6755a 216 ;;
c574e683
GB
217 stgit-series)
218 if test $# -ne 1
219 then
0cd29a03 220 clean_abort "Only one StGIT patch series can be applied at once"
c574e683
GB
221 fi
222 series_dir=`dirname "$1"`
223 series_file="$1"
224 shift
225 {
226 set x
227 while read filename
228 do
229 set "$@" "$series_dir/$filename"
230 done
231 # remove the safety x
232 shift
233 # remove the arg coming from the first-line comment
234 shift
0cd29a03 235 } < "$series_file" || clean_abort
c574e683
GB
236 # set the patch format appropriately
237 patch_format=stgit
238 # now handle the actual StGIT patches
239 split_patches "$@"
240 ;;
241 stgit)
242 this=0
243 for stgit in "$@"
244 do
245 this=`expr "$this" + 1`
246 msgnum=`printf "%0${prec}d" $this`
247 # Perl version of StGIT parse_patch. The first nonemptyline
248 # not starting with Author, From or Date is the
249 # subject, and the body starts with the next nonempty
250 # line not starting with Author, From or Date
251 perl -ne 'BEGIN { $subject = 0 }
252 if ($subject > 1) { print ; }
253 elsif (/^\s+$/) { next ; }
254 elsif (/^Author:/) { print s/Author/From/ ; }
255 elsif (/^(From|Date)/) { print ; }
256 elsif ($subject) {
257 $subject = 2 ;
258 print "\n" ;
259 print ;
260 } else {
261 print "Subject: ", $_ ;
262 $subject = 1;
263 }
0cd29a03 264 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
c574e683
GB
265 done
266 echo "$this" > "$dotest/last"
267 this=
268 msgnum=
269 ;;
a5a6755a 270 *)
af12fb7b
NS
271 if test -n "$parse_patch" ; then
272 clean_abort "Patch format $patch_format is not supported."
273 else
274 clean_abort "Patch format detection failed."
275 fi
a5a6755a
GB
276 ;;
277 esac
278}
279
d1c5f2a4 280prec=4
51ef1daa 281dotest="$GIT_DIR/rebase-apply"
a2003379 282sign= utf8=t keep= skip= interactive= resolved= rebasing= abort=
3f0a8f3c 283resolvemsg= resume=
67dad687 284git_apply_opt=
3f01ad66 285committer_date_is_author_date=
a79ec62d 286ignore_date=
d1c5f2a4 287
822f7c73 288while test $# != 0
d1c5f2a4
JH
289do
290 case "$1" in
78443d90
PH
291 -i|--interactive)
292 interactive=t ;;
293 -b|--binary)
cb3a160d 294 : ;;
78443d90
PH
295 -3|--3way)
296 threeway=t ;;
dfdd7e66 297 -s|--signoff)
78443d90
PH
298 sign=t ;;
299 -u|--utf8)
300 utf8=t ;; # this is now default
301 --no-utf8)
302 utf8= ;;
303 -k|--keep)
304 keep=t ;;
305 -r|--resolved)
306 resolved=t ;;
307 --skip)
308 skip=t ;;
3e5057a8
NS
309 --abort)
310 abort=t ;;
3041c324 311 --rebasing)
cb3a160d 312 rebasing=t threeway=t keep=t ;;
78443d90 313 -d|--dotest)
e72c7406
JH
314 die "-d option is no longer supported. Do not use."
315 ;;
78443d90
PH
316 --resolvemsg)
317 shift; resolvemsg=$1 ;;
b47dfe9e
JH
318 --whitespace|--directory)
319 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
78443d90 320 -C|-p)
b47dfe9e 321 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
a5a6755a
GB
322 --patch-format)
323 shift ; patch_format="$1" ;;
b80da424 324 --reject)
325 git_apply_opt="$git_apply_opt $1" ;;
3f01ad66
JH
326 --committer-date-is-author-date)
327 committer_date_is_author_date=t ;;
a79ec62d
NS
328 --ignore-date)
329 ignore_date=t ;;
0e987a12
SB
330 -q|--quiet)
331 GIT_QUIET=t ;;
d1c5f2a4 332 --)
78443d90 333 shift; break ;;
d1c5f2a4 334 *)
78443d90 335 usage ;;
d1c5f2a4 336 esac
78443d90 337 shift
d1c5f2a4
JH
338done
339
0c15cc92
JH
340# If the dotest directory exists, but we have finished applying all the
341# patches in them, clear it out.
d1c5f2a4
JH
342if test -d "$dotest" &&
343 last=$(cat "$dotest/last") &&
344 next=$(cat "$dotest/next") &&
345 test $# != 0 &&
346 test "$next" -gt "$last"
347then
348 rm -fr "$dotest"
349fi
350
351if test -d "$dotest"
352then
3e5057a8 353 case "$#,$skip$resolved$abort" in
c95b1389
JH
354 0,*t*)
355 # Explicit resume command and we do not have file, so
356 # we are happy.
357 : ;;
358 0,)
359 # No file input but without resume parameters; catch
360 # user error to feed us a patch from standard input
e72c7406 361 # when there is already $dotest. This is somewhat
c95b1389
JH
362 # unreliable -- stdin could be /dev/null for example
363 # and the caller did not intend to feed us a patch but
364 # wanted to continue unattended.
98ef23b3 365 test -t 0
c95b1389
JH
366 ;;
367 *)
368 false
369 ;;
370 esac ||
28ed6e7b 371 die "previous rebase directory $dotest still exists but mbox given."
271440e3 372 resume=yes
3e5057a8 373
95f8ebbf 374 case "$skip,$abort" in
2d56a136
JH
375 t,t)
376 die "Please make up your mind. --skip or --abort?"
377 ;;
95f8ebbf
OM
378 t,)
379 git rerere clear
380 git read-tree --reset -u HEAD HEAD
381 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
382 git reset HEAD
383 git update-ref ORIG_HEAD $orig_head
384 ;;
385 ,t)
2d56a136
JH
386 if test -f "$dotest/rebasing"
387 then
388 exec git rebase --abort
389 fi
3e5057a8 390 git rerere clear
c767184d
MG
391 test -f "$dotest/dirtyindex" || {
392 git read-tree --reset -u HEAD ORIG_HEAD
393 git reset ORIG_HEAD
394 }
3e5057a8
NS
395 rm -fr "$dotest"
396 exit ;;
397 esac
c767184d 398 rm -f "$dotest/dirtyindex"
d1c5f2a4 399else
3e5057a8
NS
400 # Make sure we are not given --skip, --resolved, nor --abort
401 test "$skip$resolved$abort" = "" ||
cc120056 402 die "Resolve operation not in progress, we are not resuming."
d1c5f2a4
JH
403
404 # Start afresh.
405 mkdir -p "$dotest" || exit
406
bb034f83
JH
407 if test -n "$prefix" && test $# != 0
408 then
409 first=t
410 for arg
411 do
412 test -n "$first" && {
413 set x
414 first=
415 }
416 case "$arg" in
417 /*)
418 set "$@" "$arg" ;;
419 *)
420 set "$@" "$prefix$arg" ;;
421 esac
422 done
423 shift
424 fi
a5a6755a
GB
425
426 check_patch_format "$@"
427
428 split_patches "$@"
d1c5f2a4 429
0e987a12 430 # -s, -u, -k, --whitespace, -3, -C, -q and -p flags are kept
a2003379 431 # for the resuming session after a patch failure.
22db240d 432 # -i can and must be given when resuming.
9b9f5a22 433 echo " $git_apply_opt" >"$dotest/apply-opt"
22db240d 434 echo "$threeway" >"$dotest/threeway"
d1c5f2a4
JH
435 echo "$sign" >"$dotest/sign"
436 echo "$utf8" >"$dotest/utf8"
437 echo "$keep" >"$dotest/keep"
0e987a12 438 echo "$GIT_QUIET" >"$dotest/quiet"
d1c5f2a4 439 echo 1 >"$dotest/next"
3041c324
JH
440 if test -n "$rebasing"
441 then
442 : >"$dotest/rebasing"
443 else
444 : >"$dotest/applying"
f79d4c8a
NS
445 if test -n "$HAS_HEAD"
446 then
447 git update-ref ORIG_HEAD HEAD
448 else
449 git update-ref -d ORIG_HEAD >/dev/null 2>&1
450 fi
3041c324 451 fi
d1c5f2a4
JH
452fi
453
0c15cc92
JH
454case "$resolved" in
455'')
f79d4c8a
NS
456 case "$HAS_HEAD" in
457 '')
458 files=$(git ls-files) ;;
459 ?*)
460 files=$(git diff-index --cached --name-only HEAD --) ;;
461 esac || exit
c767184d
MG
462 if test "$files"
463 then
f79d4c8a 464 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
c767184d
MG
465 die "Dirty index: cannot apply patches (dirty: $files)"
466 fi
0c15cc92
JH
467esac
468
d1c5f2a4
JH
469if test "$(cat "$dotest/utf8")" = t
470then
471 utf8=-u
bb1091a4
JH
472else
473 utf8=-n
d1c5f2a4
JH
474fi
475if test "$(cat "$dotest/keep")" = t
476then
477 keep=-k
478fi
0e987a12
SB
479if test "$(cat "$dotest/quiet")" = t
480then
481 GIT_QUIET=t
482fi
22db240d
JH
483if test "$(cat "$dotest/threeway")" = t
484then
485 threeway=t
486fi
9b9f5a22 487git_apply_opt=$(cat "$dotest/apply-opt")
d1c5f2a4
JH
488if test "$(cat "$dotest/sign")" = t
489then
5354a56f 490 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
d1c5f2a4
JH
491 s/>.*/>/
492 s/^/Signed-off-by: /'
493 `
494else
495 SIGNOFF=
496fi
d1c5f2a4
JH
497
498last=`cat "$dotest/last"`
499this=`cat "$dotest/next"`
500if test "$skip" = t
501then
502 this=`expr "$this" + 1`
69224716 503 resume=
d1c5f2a4
JH
504fi
505
506if test "$this" -gt "$last"
507then
0e987a12 508 say Nothing to do.
d1c5f2a4
JH
509 rm -fr "$dotest"
510 exit
511fi
512
513while test "$this" -le "$last"
514do
515 msgnum=`printf "%0${prec}d" $this`
516 next=`expr "$this" + 1`
517 test -f "$dotest/$msgnum" || {
69224716 518 resume=
d1c5f2a4
JH
519 go_next
520 continue
521 }
0c15cc92
JH
522
523 # If we are not resuming, parse and extract the patch information
524 # into separate files:
525 # - info records the authorship and title
526 # - msg is the rest of commit log message
527 # - patch is the patch body.
528 #
529 # When we are resuming, these files are either already prepared
530 # by the user, or the user can tell us to do so by --resolved flag.
271440e3
JH
531 case "$resume" in
532 '')
5be60078 533 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
271440e3
JH
534 <"$dotest/$msgnum" >"$dotest/info" ||
535 stop_here $this
ca193cf1
JS
536
537 # skip pine's internal folder data
538 grep '^Author: Mail System Internal Data$' \
539 <"$dotest"/info >/dev/null &&
540 go_next && continue
541
28ed6e7b 542 test -s "$dotest/patch" || {
6777c380 543 echo "Patch is empty. Was it split wrong?"
87ab7992
DZ
544 stop_here $this
545 }
5e835cac
JH
546 if test -f "$dotest/rebasing" &&
547 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
548 -e q "$dotest/$msgnum") &&
549 test "$(git cat-file -t "$commit")" = commit
550 then
551 git cat-file commit "$commit" |
552 sed -e '1,/^$/d' >"$dotest/msg-clean"
553 else
554 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
555 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
556
41a3e3aa 557 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
5e835cac
JH
558 git stripspace > "$dotest/msg-clean"
559 fi
271440e3
JH
560 ;;
561 esac
d1c5f2a4
JH
562
563 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
564 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
565 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
e0e3ba20 566
3b78959e 567 if test -z "$GIT_AUTHOR_EMAIL"
e0e3ba20 568 then
3b78959e 569 echo "Patch does not have a valid e-mail address."
e0e3ba20
JH
570 stop_here $this
571 fi
572
2c674191 573 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
d1c5f2a4 574
4bfb6b62
JH
575 case "$resume" in
576 '')
577 if test '' != "$SIGNOFF"
578 then
d1c5f2a4 579 LAST_SIGNED_OFF_BY=`
4bfb6b62
JH
580 sed -ne '/^Signed-off-by: /p' \
581 "$dotest/msg-clean" |
b4ce54fc 582 sed -ne '$p'
d1c5f2a4 583 `
4bfb6b62
JH
584 ADD_SIGNOFF=`
585 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
d1c5f2a4
JH
586 test '' = "$LAST_SIGNED_OFF_BY" && echo
587 echo "$SIGNOFF"
4bfb6b62
JH
588 }`
589 else
d1c5f2a4 590 ADD_SIGNOFF=
4bfb6b62
JH
591 fi
592 {
d1c5f2a4
JH
593 if test -s "$dotest/msg-clean"
594 then
d1c5f2a4
JH
595 cat "$dotest/msg-clean"
596 fi
597 if test '' != "$ADD_SIGNOFF"
598 then
599 echo "$ADD_SIGNOFF"
600 fi
4bfb6b62
JH
601 } >"$dotest/final-commit"
602 ;;
0c15cc92 603 *)
6d28644d 604 case "$resolved$interactive" in
0c15cc92
JH
605 tt)
606 # This is used only for interactive view option.
38762c47 607 git diff-index -p --cached HEAD -- >"$dotest/patch"
0c15cc92
JH
608 ;;
609 esac
4bfb6b62 610 esac
d1c5f2a4 611
4bfb6b62 612 resume=
d1c5f2a4
JH
613 if test "$interactive" = t
614 then
a1451104
JH
615 test -t 0 ||
616 die "cannot be interactive without stdin connected to a terminal."
d1c5f2a4
JH
617 action=again
618 while test "$action" = again
619 do
620 echo "Commit Body is:"
621 echo "--------------------------"
622 cat "$dotest/final-commit"
623 echo "--------------------------"
9754563c 624 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
d1c5f2a4
JH
625 read reply
626 case "$reply" in
f89ad67f
JH
627 [yY]*) action=yes ;;
628 [aA]*) action=yes interactive= ;;
629 [nN]*) action=skip ;;
ef0c2abf 630 [eE]*) git_editor "$dotest/final-commit"
d1c5f2a4 631 action=again ;;
f89ad67f
JH
632 [vV]*) action=again
633 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
634 *) action=again ;;
d1c5f2a4
JH
635 esac
636 done
637 else
638 action=yes
639 fi
1d9b2656 640 FIRSTLINE=$(sed 1q "$dotest/final-commit")
d1c5f2a4
JH
641
642 if test $action = skip
643 then
644 go_next
645 continue
646 fi
647
648 if test -x "$GIT_DIR"/hooks/applypatch-msg
649 then
650 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
651 stop_here $this
652 fi
653
0e987a12 654 say "Applying: $FIRSTLINE"
d1c5f2a4 655
0c15cc92
JH
656 case "$resolved" in
657 '')
3ddd1703
SB
658 # When we are allowed to fall back to 3-way later, don't give
659 # false errors during the initial attempt.
660 squelch=
661 if test "$threeway" = t
662 then
663 squelch='>/dev/null 2>&1 '
664 fi
665 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
0c15cc92
JH
666 apply_status=$?
667 ;;
668 t)
669 # Resolved means the user did all the hard work, and
670 # we do not have to do any patch application. Just
671 # trust what the user has in the index file and the
672 # working tree.
673 resolved=
38762c47 674 git diff-index --quiet --cached HEAD -- && {
64646d11 675 echo "No changes - did you forget to use 'git add'?"
ced9456a 676 stop_here_user_resolve $this
06aff47b 677 }
5be60078 678 unmerged=$(git ls-files -u)
c1d1128b
JH
679 if test -n "$unmerged"
680 then
681 echo "You still have unmerged paths in your index"
64646d11 682 echo "did you forget to use 'git add'?"
ced9456a 683 stop_here_user_resolve $this
c1d1128b 684 fi
0c15cc92 685 apply_status=0
b4372ef1 686 git rerere
0c15cc92
JH
687 ;;
688 esac
689
d1c5f2a4
JH
690 if test $apply_status = 1 && test "$threeway" = t
691 then
73319032 692 if (fall_back_3way)
d1c5f2a4 693 then
73319032
JH
694 # Applying the patch to an earlier tree and merging the
695 # result may have produced the same tree as ours.
38762c47 696 git diff-index --quiet --cached HEAD -- && {
0e987a12 697 say No changes -- Patch already applied.
06aff47b
AR
698 go_next
699 continue
700 }
73319032
JH
701 # clear apply_status -- we have successfully merged.
702 apply_status=0
d1c5f2a4
JH
703 fi
704 fi
705 if test $apply_status != 0
706 then
4d2e283a 707 printf 'Patch failed at %s %s\n' "$msgnum" "$FIRSTLINE"
ced9456a 708 stop_here_user_resolve $this
d1c5f2a4
JH
709 fi
710
711 if test -x "$GIT_DIR"/hooks/pre-applypatch
712 then
713 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
714 fi
715
5be60078 716 tree=$(git write-tree) &&
3f01ad66 717 commit=$(
a79ec62d
NS
718 if test -n "$ignore_date"
719 then
720 GIT_AUTHOR_DATE=
721 fi
f79d4c8a 722 parent=$(git rev-parse --verify -q HEAD) ||
0e987a12 723 say >&2 "applying to an empty history"
f79d4c8a 724
3f01ad66
JH
725 if test -n "$committer_date_is_author_date"
726 then
727 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
728 export GIT_COMMITTER_DATE
729 fi &&
ea10b60c 730 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
3f01ad66 731 ) &&
2e6e3e82 732 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
d1c5f2a4
JH
733 stop_here $this
734
735 if test -x "$GIT_DIR"/hooks/post-applypatch
736 then
737 "$GIT_DIR"/hooks/post-applypatch
738 fi
739
740 go_next
741done
742
e0cd252e
MS
743git gc --auto
744
d1c5f2a4 745rm -fr "$dotest"