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