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