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