Merge branch 'jc/i18n-shell-script-gettext'
[git/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)
6c15a1c6
JH
383 echo >&2 "The $1 option has been a no-op for long time, and"
384 echo >&2 "it will be removed. Please do not use it anymore."
4eeb1de1 385 ;;
78443d90
PH
386 -3|--3way)
387 threeway=t ;;
dfdd7e66 388 -s|--signoff)
78443d90
PH
389 sign=t ;;
390 -u|--utf8)
391 utf8=t ;; # this is now default
392 --no-utf8)
393 utf8= ;;
394 -k|--keep)
395 keep=t ;;
f7e5ea17
TR
396 --keep-non-patch)
397 keep=b ;;
017678b4
JH
398 -c|--scissors)
399 scissors=t ;;
400 --no-scissors)
401 scissors=f ;;
c8089af6 402 -r|--resolved|--continue)
78443d90
PH
403 resolved=t ;;
404 --skip)
405 skip=t ;;
3e5057a8
NS
406 --abort)
407 abort=t ;;
3041c324 408 --rebasing)
d25e5159 409 rebasing=t threeway=t keep=t scissors=f no_inbody_headers=t ;;
78443d90 410 -d|--dotest)
79087259 411 die "$(gettext "-d option is no longer supported. Do not use.")"
e72c7406 412 ;;
78443d90
PH
413 --resolvemsg)
414 shift; resolvemsg=$1 ;;
77e9e496 415 --whitespace|--directory|--exclude)
b47dfe9e 416 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
78443d90 417 -C|-p)
b47dfe9e 418 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
a5a6755a
GB
419 --patch-format)
420 shift ; patch_format="$1" ;;
86c91f91 421 --reject|--ignore-whitespace|--ignore-space-change)
b80da424 422 git_apply_opt="$git_apply_opt $1" ;;
3f01ad66
JH
423 --committer-date-is-author-date)
424 committer_date_is_author_date=t ;;
a79ec62d
NS
425 --ignore-date)
426 ignore_date=t ;;
cb6020bb
JH
427 --rerere-autoupdate|--no-rerere-autoupdate)
428 allow_rerere_autoupdate="$1" ;;
0e987a12
SB
429 -q|--quiet)
430 GIT_QUIET=t ;;
ad2c9280
SH
431 --keep-cr)
432 keepcr=t ;;
e80d4cbe
SH
433 --no-keep-cr)
434 keepcr=f ;;
d1c5f2a4 435 --)
78443d90 436 shift; break ;;
d1c5f2a4 437 *)
78443d90 438 usage ;;
d1c5f2a4 439 esac
78443d90 440 shift
d1c5f2a4
JH
441done
442
0c15cc92
JH
443# If the dotest directory exists, but we have finished applying all the
444# patches in them, clear it out.
d1c5f2a4
JH
445if test -d "$dotest" &&
446 last=$(cat "$dotest/last") &&
447 next=$(cat "$dotest/next") &&
448 test $# != 0 &&
449 test "$next" -gt "$last"
450then
451 rm -fr "$dotest"
452fi
453
454if test -d "$dotest"
455then
3e5057a8 456 case "$#,$skip$resolved$abort" in
c95b1389
JH
457 0,*t*)
458 # Explicit resume command and we do not have file, so
459 # we are happy.
460 : ;;
461 0,)
462 # No file input but without resume parameters; catch
463 # user error to feed us a patch from standard input
e72c7406 464 # when there is already $dotest. This is somewhat
c95b1389
JH
465 # unreliable -- stdin could be /dev/null for example
466 # and the caller did not intend to feed us a patch but
467 # wanted to continue unattended.
98ef23b3 468 test -t 0
c95b1389
JH
469 ;;
470 *)
471 false
472 ;;
473 esac ||
79087259 474 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
271440e3 475 resume=yes
3e5057a8 476
95f8ebbf 477 case "$skip,$abort" in
2d56a136 478 t,t)
79087259 479 die "$(gettext "Please make up your mind. --skip or --abort?")"
2d56a136 480 ;;
95f8ebbf
OM
481 t,)
482 git rerere clear
483 git read-tree --reset -u HEAD HEAD
484 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
485 git reset HEAD
486 git update-ref ORIG_HEAD $orig_head
487 ;;
488 ,t)
2d56a136
JH
489 if test -f "$dotest/rebasing"
490 then
491 exec git rebase --abort
492 fi
3e5057a8 493 git rerere clear
7b3b7e37
JH
494 if safe_to_abort
495 then
c767184d
MG
496 git read-tree --reset -u HEAD ORIG_HEAD
497 git reset ORIG_HEAD
7b3b7e37 498 fi
3e5057a8
NS
499 rm -fr "$dotest"
500 exit ;;
501 esac
c767184d 502 rm -f "$dotest/dirtyindex"
d1c5f2a4 503else
3e5057a8
NS
504 # Make sure we are not given --skip, --resolved, nor --abort
505 test "$skip$resolved$abort" = "" ||
79087259 506 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
d1c5f2a4
JH
507
508 # Start afresh.
509 mkdir -p "$dotest" || exit
510
bb034f83
JH
511 if test -n "$prefix" && test $# != 0
512 then
513 first=t
514 for arg
515 do
516 test -n "$first" && {
517 set x
518 first=
519 }
5e9677cb
PT
520 if is_absolute_path "$arg"
521 then
522 set "$@" "$arg"
523 else
524 set "$@" "$prefix$arg"
525 fi
bb034f83
JH
526 done
527 shift
528 fi
a5a6755a
GB
529
530 check_patch_format "$@"
531
532 split_patches "$@"
d1c5f2a4 533
017678b4
JH
534 # -i can and must be given when resuming; everything
535 # else is kept
9b9f5a22 536 echo " $git_apply_opt" >"$dotest/apply-opt"
22db240d 537 echo "$threeway" >"$dotest/threeway"
d1c5f2a4
JH
538 echo "$sign" >"$dotest/sign"
539 echo "$utf8" >"$dotest/utf8"
540 echo "$keep" >"$dotest/keep"
017678b4 541 echo "$scissors" >"$dotest/scissors"
d25e5159 542 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
0e987a12 543 echo "$GIT_QUIET" >"$dotest/quiet"
d1c5f2a4 544 echo 1 >"$dotest/next"
3041c324
JH
545 if test -n "$rebasing"
546 then
547 : >"$dotest/rebasing"
548 else
549 : >"$dotest/applying"
f79d4c8a
NS
550 if test -n "$HAS_HEAD"
551 then
552 git update-ref ORIG_HEAD HEAD
553 else
554 git update-ref -d ORIG_HEAD >/dev/null 2>&1
555 fi
3041c324 556 fi
d1c5f2a4
JH
557fi
558
2a6f08ac
JK
559git update-index -q --refresh
560
0c15cc92
JH
561case "$resolved" in
562'')
f79d4c8a
NS
563 case "$HAS_HEAD" in
564 '')
565 files=$(git ls-files) ;;
566 ?*)
567 files=$(git diff-index --cached --name-only HEAD --) ;;
568 esac || exit
c767184d
MG
569 if test "$files"
570 then
f79d4c8a 571 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
79087259 572 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
c767184d 573 fi
0c15cc92
JH
574esac
575
f7e5ea17
TR
576# Now, decide what command line options we will give to the git
577# commands we invoke, based on the result of parsing command line
578# options and previous invocation state stored in $dotest/ files.
579
d1c5f2a4
JH
580if test "$(cat "$dotest/utf8")" = t
581then
582 utf8=-u
bb1091a4
JH
583else
584 utf8=-n
d1c5f2a4 585fi
f7e5ea17
TR
586keep=$(cat "$dotest/keep")
587case "$keep" in
588t)
589 keep=-k ;;
590b)
591 keep=-b ;;
592*)
593 keep= ;;
594esac
017678b4
JH
595case "$(cat "$dotest/scissors")" in
596t)
597 scissors=--scissors ;;
598f)
599 scissors=--no-scissors ;;
600esac
d25e5159
LS
601if test "$(cat "$dotest/no_inbody_headers")" = t
602then
603 no_inbody_headers=--no-inbody-headers
604else
605 no_inbody_headers=
606fi
0e987a12
SB
607if test "$(cat "$dotest/quiet")" = t
608then
609 GIT_QUIET=t
610fi
22db240d
JH
611if test "$(cat "$dotest/threeway")" = t
612then
613 threeway=t
614fi
9b9f5a22 615git_apply_opt=$(cat "$dotest/apply-opt")
d1c5f2a4
JH
616if test "$(cat "$dotest/sign")" = t
617then
5354a56f 618 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
d1c5f2a4
JH
619 s/>.*/>/
620 s/^/Signed-off-by: /'
621 `
622else
623 SIGNOFF=
624fi
d1c5f2a4
JH
625
626last=`cat "$dotest/last"`
627this=`cat "$dotest/next"`
628if test "$skip" = t
629then
630 this=`expr "$this" + 1`
69224716 631 resume=
d1c5f2a4
JH
632fi
633
d1c5f2a4
JH
634while test "$this" -le "$last"
635do
636 msgnum=`printf "%0${prec}d" $this`
637 next=`expr "$this" + 1`
638 test -f "$dotest/$msgnum" || {
69224716 639 resume=
d1c5f2a4
JH
640 go_next
641 continue
642 }
0c15cc92
JH
643
644 # If we are not resuming, parse and extract the patch information
645 # into separate files:
646 # - info records the authorship and title
647 # - msg is the rest of commit log message
648 # - patch is the patch body.
649 #
650 # When we are resuming, these files are either already prepared
651 # by the user, or the user can tell us to do so by --resolved flag.
271440e3
JH
652 case "$resume" in
653 '')
d25e5159 654 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
271440e3
JH
655 <"$dotest/$msgnum" >"$dotest/info" ||
656 stop_here $this
ca193cf1
JS
657
658 # skip pine's internal folder data
e1622bfc 659 sane_grep '^Author: Mail System Internal Data$' \
ca193cf1
JS
660 <"$dotest"/info >/dev/null &&
661 go_next && continue
662
28ed6e7b 663 test -s "$dotest/patch" || {
de88c1ce 664 eval_gettextln "Patch is empty. Was it split wrong?
a4372c37 665If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
de88c1ce 666To restore the original branch and stop patching run \"\$cmdline --abort\"."
87ab7992
DZ
667 stop_here $this
668 }
43c23251 669 rm -f "$dotest/original-commit" "$dotest/author-script"
5e835cac
JH
670 if test -f "$dotest/rebasing" &&
671 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
672 -e q "$dotest/$msgnum") &&
673 test "$(git cat-file -t "$commit")" = commit
674 then
675 git cat-file commit "$commit" |
676 sed -e '1,/^$/d' >"$dotest/msg-clean"
96e19488 677 echo "$commit" > "$dotest/original-commit"
43c23251 678 get_author_ident_from_commit "$commit" > "$dotest/author-script"
5e835cac 679 else
c970a6fd
JH
680 {
681 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
682 echo
683 cat "$dotest/msg"
684 } |
685 git stripspace > "$dotest/msg-clean"
5e835cac 686 fi
271440e3
JH
687 ;;
688 esac
d1c5f2a4 689
43c23251
JS
690 if test -f "$dotest/author-script"
691 then
692 eval $(cat "$dotest/author-script")
693 else
694 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
695 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
696 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
697 fi
e0e3ba20 698
3b78959e 699 if test -z "$GIT_AUTHOR_EMAIL"
e0e3ba20 700 then
de88c1ce 701 gettextln "Patch does not have a valid e-mail address."
e0e3ba20
JH
702 stop_here $this
703 fi
704
2c674191 705 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
d1c5f2a4 706
4bfb6b62
JH
707 case "$resume" in
708 '')
709 if test '' != "$SIGNOFF"
710 then
d1c5f2a4 711 LAST_SIGNED_OFF_BY=`
4bfb6b62
JH
712 sed -ne '/^Signed-off-by: /p' \
713 "$dotest/msg-clean" |
b4ce54fc 714 sed -ne '$p'
d1c5f2a4 715 `
4bfb6b62
JH
716 ADD_SIGNOFF=`
717 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
d1c5f2a4
JH
718 test '' = "$LAST_SIGNED_OFF_BY" && echo
719 echo "$SIGNOFF"
4bfb6b62
JH
720 }`
721 else
d1c5f2a4 722 ADD_SIGNOFF=
4bfb6b62
JH
723 fi
724 {
d1c5f2a4
JH
725 if test -s "$dotest/msg-clean"
726 then
d1c5f2a4
JH
727 cat "$dotest/msg-clean"
728 fi
729 if test '' != "$ADD_SIGNOFF"
730 then
731 echo "$ADD_SIGNOFF"
732 fi
4bfb6b62
JH
733 } >"$dotest/final-commit"
734 ;;
0c15cc92 735 *)
6d28644d 736 case "$resolved$interactive" in
0c15cc92
JH
737 tt)
738 # This is used only for interactive view option.
38762c47 739 git diff-index -p --cached HEAD -- >"$dotest/patch"
0c15cc92
JH
740 ;;
741 esac
4bfb6b62 742 esac
d1c5f2a4 743
4bfb6b62 744 resume=
d1c5f2a4
JH
745 if test "$interactive" = t
746 then
a1451104 747 test -t 0 ||
79087259 748 die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
d1c5f2a4
JH
749 action=again
750 while test "$action" = again
751 do
de88c1ce 752 gettextln "Commit Body is:"
d1c5f2a4
JH
753 echo "--------------------------"
754 cat "$dotest/final-commit"
755 echo "--------------------------"
7a74d04a
ÆAB
756 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
757 # in your translation. The program will only accept English
758 # input at this point.
759 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
d1c5f2a4
JH
760 read reply
761 case "$reply" in
f89ad67f
JH
762 [yY]*) action=yes ;;
763 [aA]*) action=yes interactive= ;;
764 [nN]*) action=skip ;;
ef0c2abf 765 [eE]*) git_editor "$dotest/final-commit"
d1c5f2a4 766 action=again ;;
f89ad67f 767 [vV]*) action=again
f6dff119 768 git_pager "$dotest/patch" ;;
f89ad67f 769 *) action=again ;;
d1c5f2a4
JH
770 esac
771 done
772 else
773 action=yes
774 fi
92f65e6a
RR
775
776 if test -f "$dotest/final-commit"
777 then
778 FIRSTLINE=$(sed 1q "$dotest/final-commit")
779 else
780 FIRSTLINE=""
781 fi
d1c5f2a4
JH
782
783 if test $action = skip
784 then
785 go_next
786 continue
787 fi
788
789 if test -x "$GIT_DIR"/hooks/applypatch-msg
790 then
791 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
792 stop_here $this
793 fi
794
dff1a983 795 say "$(eval_gettext "Applying: \$FIRSTLINE")"
d1c5f2a4 796
0c15cc92
JH
797 case "$resolved" in
798 '')
3ddd1703
SB
799 # When we are allowed to fall back to 3-way later, don't give
800 # false errors during the initial attempt.
801 squelch=
802 if test "$threeway" = t
803 then
804 squelch='>/dev/null 2>&1 '
805 fi
806 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
0c15cc92
JH
807 apply_status=$?
808 ;;
809 t)
810 # Resolved means the user did all the hard work, and
811 # we do not have to do any patch application. Just
812 # trust what the user has in the index file and the
813 # working tree.
814 resolved=
38762c47 815 git diff-index --quiet --cached HEAD -- && {
de88c1ce 816 gettextln "No changes - did you forget to use 'git add'?
bd8643ae 817If there is nothing left to stage, chances are that something else
de88c1ce 818already introduced the same changes; you might want to skip this patch."
ced9456a 819 stop_here_user_resolve $this
06aff47b 820 }
5be60078 821 unmerged=$(git ls-files -u)
c1d1128b
JH
822 if test -n "$unmerged"
823 then
de88c1ce
JS
824 gettextln "You still have unmerged paths in your index
825did you forget to use 'git add'?"
ced9456a 826 stop_here_user_resolve $this
c1d1128b 827 fi
0c15cc92 828 apply_status=0
b4372ef1 829 git rerere
0c15cc92
JH
830 ;;
831 esac
832
0ba17dd0 833 if test $apply_status != 0 && test "$threeway" = t
d1c5f2a4 834 then
73319032 835 if (fall_back_3way)
d1c5f2a4 836 then
73319032
JH
837 # Applying the patch to an earlier tree and merging the
838 # result may have produced the same tree as ours.
38762c47 839 git diff-index --quiet --cached HEAD -- && {
dff1a983 840 say "$(gettext "No changes -- Patch already applied.")"
06aff47b
AR
841 go_next
842 continue
843 }
73319032
JH
844 # clear apply_status -- we have successfully merged.
845 apply_status=0
d1c5f2a4
JH
846 fi
847 fi
848 if test $apply_status != 0
849 then
de88c1ce 850 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
ced9456a 851 stop_here_user_resolve $this
d1c5f2a4
JH
852 fi
853
854 if test -x "$GIT_DIR"/hooks/pre-applypatch
855 then
856 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
857 fi
858
5be60078 859 tree=$(git write-tree) &&
3f01ad66 860 commit=$(
a79ec62d
NS
861 if test -n "$ignore_date"
862 then
863 GIT_AUTHOR_DATE=
864 fi
f79d4c8a 865 parent=$(git rev-parse --verify -q HEAD) ||
dff1a983 866 say >&2 "$(gettext "applying to an empty history")"
f79d4c8a 867
3f01ad66
JH
868 if test -n "$committer_date_is_author_date"
869 then
870 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
871 export GIT_COMMITTER_DATE
872 fi &&
ea10b60c 873 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
3f01ad66 874 ) &&
2e6e3e82 875 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
d1c5f2a4
JH
876 stop_here $this
877
96e19488
TR
878 if test -f "$dotest/original-commit"; then
879 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
880 fi
881
d1c5f2a4
JH
882 if test -x "$GIT_DIR"/hooks/post-applypatch
883 then
884 "$GIT_DIR"/hooks/post-applypatch
885 fi
886
887 go_next
888done
889
eb2151bb
TR
890if test -s "$dotest"/rewritten; then
891 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
892 if test -x "$GIT_DIR"/hooks/post-rewrite; then
96e19488 893 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
eb2151bb 894 fi
96e19488
TR
895fi
896
d1c5f2a4 897rm -fr "$dotest"
29b67543 898git gc --auto