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