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