blob: a67d0f98989706fa69df4cf7d6242ad9531eb337 [file] [log] [blame]
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -07001#!/bin/sh
2#
Junio C Hamanod64e6b02006-02-18 20:51:26 -08003# Copyright (c) 2005, 2006 Junio C Hamano
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -07004
Jeff Kingc1491842008-03-01 01:22:55 -05005SUBDIRECTORY_OK=Yes
Pierre Habouzit78443d92007-11-04 11:30:57 +01006OPTIONS_KEEPDASHDASH=
Nicolas Vigier88336622014-02-01 02:18:00 +00007OPTIONS_STUCKLONG=t
Pierre Habouzit78443d92007-11-04 11:30:57 +01008OPTIONS_SPEC="\
Štěpán Němec0adda932010-10-08 19:31:17 +02009git am [options] [(<mbox>|<Maildir>)...]
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +030010git am [options] (--continue | --skip | --abort)
Pierre Habouzit78443d92007-11-04 11:30:57 +010011--
Junio C Hamano66006c62007-11-08 23:04:31 -080012i,interactive run interactively
Jay Soffian98ef23b2009-01-28 10:03:10 -050013b,binary* (historical option -- no-op)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100143,3way allow fall back on 3way merging if needed
Stephen Boyd0e987a12009-06-16 15:33:01 -070015q,quiet be quiet
Pierre Habouzit78443d92007-11-04 11:30:57 +010016s,signoff add a Signed-off-by line to the commit message
17u,utf8 recode into utf8 (default)
Jörg Sommerfe1fa942008-02-03 00:58:06 +010018k,keep pass -k flag to git-mailinfo
Thomas Rastf7e5ea12012-01-16 11:53:00 +010019keep-non-patch pass -b flag to git-mailinfo
Paolo Bonzinia078f732014-11-25 15:00:56 +010020m,message-id pass -m flag to git-mailinfo
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +010021keep-cr pass --keep-cr flag to git-mailsplit for mbox format
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +010022no-keep-cr do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
Junio C Hamano017678b2009-08-26 21:36:05 -070023c,scissors strip everything before a scissors line
Pierre Habouzit78443d92007-11-04 11:30:57 +010024whitespace= pass it through git-apply
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +020025ignore-space-change pass it through git-apply
26ignore-whitespace pass it through git-apply
Junio C Hamanob47dfe92009-01-11 22:21:48 -080027directory= pass it through git-apply
maximilian attems77e9e492011-08-03 11:37:29 +020028exclude= pass it through git-apply
Johannes Berg58725ef2012-03-28 13:11:28 +020029include= pass it through git-apply
Pierre Habouzit78443d92007-11-04 11:30:57 +010030C= pass it through git-apply
31p= pass it through git-apply
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +020032patch-format= format the patch(es) are in
martin f. krafftb80da422009-01-23 11:31:21 +110033reject pass it through git-apply
Pierre Habouzit78443d92007-11-04 11:30:57 +010034resolvemsg= override error message when patch failure occurs
Jeff Kingc8089af2010-02-11 17:27:14 -050035continue continue applying patches after resolving a conflict
36r,resolved synonyms for --continue
Junio C Hamano3041c322008-03-04 00:25:06 -080037skip skip the current patch
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +090038abort restore the original branch and abort the patching operation.
Junio C Hamano3f01ad62009-01-22 16:14:58 -080039committer-date-is-author-date lie about committer date
Nanako Shiraishia79ec622009-01-24 10:18:02 +090040ignore-date use current timestamp for author date
Junio C Hamanocb6020b2009-12-04 00:20:48 -080041rerere-autoupdate update the index with reused conflict resolution if possible
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +000042S,gpg-sign? GPG-sign commits
Jay Soffian98ef23b2009-01-28 10:03:10 -050043rebasing* (internal use for git-rebase)"
Pierre Habouzit78443d92007-11-04 11:30:57 +010044
freku045@student.liu.secf1fe882005-12-13 23:30:31 +010045. git-sh-setup
Ævar Arnfjörð Bjarmasonb9a97262011-05-21 18:43:42 +000046. git-sh-i18n
Junio C Hamanobb034f82008-03-04 00:25:04 -080047prefix=$(git rev-parse --show-prefix)
Shawn O. Pearcef9474132006-12-28 02:34:48 -050048set_reflog_action am
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -050049require_work_tree
Jeff Kingc1491842008-03-01 01:22:55 -050050cd_to_toplevel
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070051
Stephan Beyer460abee2008-07-12 17:46:59 +020052git var GIT_COMMITTER_IDENT >/dev/null ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +000053 die "$(gettext "You need to set your committer info first")"
Junio C Hamanod64e6b02006-02-18 20:51:26 -080054
Nanako Shiraishif79d4c82009-04-10 09:34:42 +090055if git rev-parse --verify -q HEAD >/dev/null
56then
57 HAS_HEAD=yes
58else
59 HAS_HEAD=
60fi
61
Ramkumar Ramachandrad2c46312010-06-02 10:33:35 +020062cmdline="git am"
63if test '' != "$interactive"
64then
65 cmdline="$cmdline -i"
66fi
67if test '' != "$threeway"
68then
69 cmdline="$cmdline -3"
70fi
71
Junio C Hamanob47dfe92009-01-11 22:21:48 -080072sq () {
Christian Couder47c97392009-04-24 08:29:01 +020073 git rev-parse --sq-quote "$@"
Junio C Hamanob47dfe92009-01-11 22:21:48 -080074}
75
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070076stop_here () {
77 echo "$1" >"$dotest/next"
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080078 git rev-parse --verify -q HEAD >"$dotest/abort-safety"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070079 exit 1
80}
81
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080082safe_to_abort () {
83 if test -f "$dotest/dirtyindex"
84 then
85 return 1
86 fi
87
88 if ! test -s "$dotest/abort-safety"
89 then
90 return 0
91 fi
92
93 abort_safety=$(cat "$dotest/abort-safety")
94 if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
95 then
96 return 0
97 fi
Jiang Xin42e65042012-07-25 22:53:11 +080098 gettextln "You seem to have moved HEAD since the last 'am' failure.
Jon Seymourde88c1c2011-08-07 21:58:14 +100099Not rewinding to ORIG_HEAD" >&2
Junio C Hamano7b3b7e32010-12-21 10:35:53 -0800100 return 1
101}
102
Robert Shearmanced94562006-05-02 13:32:43 +0100103stop_here_user_resolve () {
Seancc120052006-05-13 23:34:08 -0400104 if [ -n "$resolvemsg" ]; then
Jeff Kinga23bfae2007-05-26 00:33:03 -0700105 printf '%s\n' "$resolvemsg"
Seancc120052006-05-13 23:34:08 -0400106 stop_here $1
107 fi
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +0300108 eval_gettextln "When you have resolved this problem, run \"\$cmdline --continue\".
Jiang Xinc7108bf2012-07-25 22:53:08 +0800109If you prefer to skip this patch, run \"\$cmdline --skip\" instead.
110To restore the original branch and stop patching, run \"\$cmdline --abort\"."
Robert Shearmanced94562006-05-02 13:32:43 +0100111
112 stop_here $1
113}
114
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700115go_next () {
116 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
117 "$dotest/patch" "$dotest/info"
118 echo "$next" >"$dotest/next"
119 this=$next
120}
121
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700122cannot_fallback () {
123 echo "$1"
Jon Seymourde88c1c2011-08-07 21:58:14 +1000124 gettextln "Cannot fall back to three-way merge."
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700125 exit 1
126}
127
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700128fall_back_3way () {
Elia Pinto75ee3d72014-03-25 10:22:22 -0700129 O_OBJECT=$(cd "$GIT_OBJECT_DIRECTORY" && pwd)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700130
131 rm -fr "$dotest"/patch-merge-*
132 mkdir "$dotest/patch-merge-tmp-dir"
133
134 # First see if the patch records the index info that we can use.
Junio C Hamano4056afb2012-02-22 21:55:06 -0800135 cmd="git apply $git_apply_opt --build-fake-ancestor" &&
136 cmd="$cmd "'"$dotest/patch-merge-tmp-index" "$dotest/patch"' &&
137 eval "$cmd" &&
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700138 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700139 git write-tree >"$dotest/patch-merge-base+" ||
Ævar Arnfjörð Bjarmasona424ca12011-05-21 18:43:48 +0000140 cannot_fallback "$(gettext "Repository lacks necessary blobs to fall back on 3-way merge.")"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700141
Jiang Xin42e65042012-07-25 22:53:11 +0800142 say "$(gettext "Using index info to reconstruct a base tree...")"
Junio C Hamano4056afb2012-02-22 21:55:06 -0800143
144 cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
Junio C Hamano5d868612012-03-28 09:55:21 -0700145
146 if test -z "$GIT_QUIET"
147 then
148 eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
149 fi
150
Junio C Hamano4056afb2012-02-22 21:55:06 -0800151 cmd="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
152 if eval "$cmd"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700153 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700154 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
155 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700156 else
Ævar Arnfjörð Bjarmasona424ca12011-05-21 18:43:48 +0000157 cannot_fallback "$(gettext "Did you hand edit your patch?
158It does not apply to blobs recorded in its index.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700159 fi
160
161 test -f "$dotest/patch-merge-index" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700162 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700163 orig_tree=$(cat "$dotest/patch-merge-base") &&
164 rm -fr "$dotest"/patch-merge-* || exit 1
165
Ævar Arnfjörð Bjarmason865207a2011-05-21 18:43:51 +0000166 say "$(gettext "Falling back to patching base and 3-way merge...")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700167
168 # This is not so wrong. Depending on which base we picked,
169 # orig_tree may be wildly different from ours, but his_tree
170 # has the same set of wildly different changes in parts the
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500171 # patch did not touch, so recursive ends up canceling them,
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700172 # saying that we reverted all those changes.
173
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700174 eval GITHEAD_$his_tree='"$FIRSTLINE"'
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500175 export GITHEAD_$his_tree
Stephen Boyd0e987a12009-06-16 15:33:01 -0700176 if test -n "$GIT_QUIET"
177 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700178 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boyd0e987a12009-06-16 15:33:01 -0700179 fi
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500180 git-merge-recursive $orig_tree -- HEAD $his_tree || {
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800181 git rerere $allow_rerere_autoupdate
Jiang Xin42e65042012-07-25 22:53:11 +0800182 die "$(gettext "Failed to merge in the changes.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700183 }
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500184 unset GITHEAD_$his_tree
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700185}
186
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200187clean_abort () {
188 test $# = 0 || echo >&2 "$@"
189 rm -fr "$dotest"
190 exit 1
191}
192
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200193patch_format=
194
195check_patch_format () {
196 # early return if patch_format was set from the command line
197 if test -n "$patch_format"
198 then
199 return 0
200 fi
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200201
202 # we default to mbox format if input is from stdin and for
203 # directories
204 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
205 then
206 patch_format=mbox
207 return 0
208 fi
209
David Barr0e8341f2011-08-08 04:49:04 +0200210 # otherwise, check the first few non-blank lines of the first
211 # patch to try to detect its format
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200212 {
David Barr0e8341f2011-08-08 04:49:04 +0200213 # Start from first line containing non-whitespace
214 l1=
215 while test -z "$l1"
216 do
Jim Meyeringf0c57932012-02-25 18:34:26 +0100217 read l1 || break
David Barr0e8341f2011-08-08 04:49:04 +0200218 done
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200219 read l2
220 read l3
221 case "$l1" in
222 "From "* | "From: "*)
223 patch_format=mbox
224 ;;
225 '# This series applies on GIT commit'*)
226 patch_format=stgit-series
227 ;;
228 "# HG changeset patch")
229 patch_format=hg
230 ;;
231 *)
232 # if the second line is empty and the third is
233 # a From, Author or Date entry, this is very
234 # likely an StGIT patch
235 case "$l2,$l3" in
236 ,"From: "* | ,"Author: "* | ,"Date: "*)
237 patch_format=stgit
238 ;;
239 *)
240 ;;
241 esac
242 ;;
243 esac
Junio C Hamano0fcb2ca2009-08-06 20:08:12 -0500244 if test -z "$patch_format" &&
245 test -n "$l1" &&
246 test -n "$l2" &&
247 test -n "$l3"
248 then
249 # This begins with three non-empty lines. Is this a
250 # piece of e-mail a-la RFC2822? Grab all the headers,
251 # discarding the indented remainder of folded lines,
252 # and see if it looks like that they all begin with the
253 # header field names...
Stephen Boyde3f67d32010-01-25 16:33:59 -0800254 tr -d '\015' <"$1" |
255 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800256 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
Junio C Hamano0fcb2ca2009-08-06 20:08:12 -0500257 patch_format=mbox
258 fi
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200259 } < "$1" || clean_abort
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200260}
261
262split_patches () {
263 case "$patch_format" in
264 mbox)
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700265 if test t = "$keepcr"
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100266 then
267 keep_cr=--keep-cr
268 else
269 keep_cr=
270 fi
Junio C Hamanoc2ca1d72009-08-04 22:31:59 -0500271 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200272 clean_abort
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200273 ;;
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200274 stgit-series)
275 if test $# -ne 1
276 then
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000277 clean_abort "$(gettext "Only one StGIT patch series can be applied at once")"
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200278 fi
Elia Pinto75ee3d72014-03-25 10:22:22 -0700279 series_dir=$(dirname "$1")
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200280 series_file="$1"
281 shift
282 {
283 set x
284 while read filename
285 do
286 set "$@" "$series_dir/$filename"
287 done
288 # remove the safety x
289 shift
290 # remove the arg coming from the first-line comment
291 shift
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200292 } < "$series_file" || clean_abort
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200293 # set the patch format appropriately
294 patch_format=stgit
295 # now handle the actual StGIT patches
296 split_patches "$@"
297 ;;
298 stgit)
299 this=0
300 for stgit in "$@"
301 do
Elia Pinto75ee3d72014-03-25 10:22:22 -0700302 this=$(expr "$this" + 1)
303 msgnum=$(printf "%0${prec}d" $this)
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200304 # Perl version of StGIT parse_patch. The first nonemptyline
305 # not starting with Author, From or Date is the
306 # subject, and the body starts with the next nonempty
307 # line not starting with Author, From or Date
Jeff Kingfcb06a82013-10-28 21:19:59 -0400308 @@PERL@@ -ne 'BEGIN { $subject = 0 }
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200309 if ($subject > 1) { print ; }
310 elsif (/^\s+$/) { next ; }
Giuseppe Bilotta45d51dc2011-08-29 18:44:07 +0200311 elsif (/^Author:/) { s/Author/From/ ; print ;}
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200312 elsif (/^(From|Date)/) { print ; }
313 elsif ($subject) {
314 $subject = 2 ;
315 print "\n" ;
316 print ;
317 } else {
318 print "Subject: ", $_ ;
319 $subject = 1;
320 }
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200321 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200322 done
323 echo "$this" > "$dotest/last"
324 this=
325 msgnum=
326 ;;
Giuseppe Bilotta0cfd1122011-08-29 18:44:06 +0200327 hg)
328 this=0
329 for hg in "$@"
330 do
331 this=$(( $this + 1 ))
332 msgnum=$(printf "%0${prec}d" $this)
333 # hg stores changeset metadata in #-commented lines preceding
334 # the commit message and diff(s). The only metadata we care about
335 # are the User and Date (Node ID and Parent are hashes which are
336 # only relevant to the hg repository and thus not useful to us)
337 # Since we cannot guarantee that the commit message is in
338 # git-friendly format, we put no Subject: line and just consume
339 # all of the message as the body
Jeff Kingfcb06a82013-10-28 21:19:59 -0400340 LANG=C LC_ALL=C @@PERL@@ -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
Giuseppe Bilotta0cfd1122011-08-29 18:44:06 +0200341 if ($subject) { print ; }
342 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
343 elsif (/^\# Date /) {
344 my ($hashsign, $str, $time, $tz) = split ;
345 $tz = sprintf "%+05d", (0-$tz)/36;
346 print "Date: " .
347 strftime("%a, %d %b %Y %H:%M:%S ",
348 localtime($time))
349 . "$tz\n";
350 } elsif (/^\# /) { next ; }
351 else {
352 print "\n", $_ ;
353 $subject = 1;
354 }
355 ' <"$hg" >"$dotest/$msgnum" || clean_abort
356 done
357 echo "$this" >"$dotest/last"
358 this=
359 msgnum=
360 ;;
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200361 *)
Giuseppe Bilottadff4b0e2011-08-29 17:22:06 +0200362 if test -n "$patch_format"
363 then
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000364 clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
Nicolas Sebrecht46caf502009-08-06 20:08:13 -0500365 else
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000366 clean_abort "$(gettext "Patch format detection failed.")"
Nicolas Sebrecht46caf502009-08-06 20:08:13 -0500367 fi
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200368 ;;
369 esac
370}
371
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700372prec=4
Johannes Schindelin51ef1da2008-07-21 12:51:02 +0200373dotest="$GIT_DIR/rebase-apply"
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100374sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
Paolo Bonzinia078f732014-11-25 15:00:56 +0100375messageid= resolvemsg= resume= scissors= no_inbody_headers=
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200376git_apply_opt=
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800377committer_date_is_author_date=
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900378ignore_date=
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800379allow_rerere_autoupdate=
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +0000380gpg_sign_opt=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700381
Paolo Bonzinia078f732014-11-25 15:00:56 +0100382if test "$(git config --bool --get am.messageid)" = true
383then
384 messageid=t
385fi
386
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +0100387if test "$(git config --bool --get am.keepcr)" = true
388then
389 keepcr=t
390fi
391
David Kastrup822f7c72007-09-23 22:42:08 +0200392while test $# != 0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700393do
394 case "$1" in
Pierre Habouzit78443d92007-11-04 11:30:57 +0100395 -i|--interactive)
396 interactive=t ;;
397 -b|--binary)
Jiang Xin42e65042012-07-25 22:53:11 +0800398 gettextln >&2 "The -b/--binary option has been a no-op for long time, and
399it will be removed. Please do not use it anymore."
Thomas Rast4eeb1de2012-03-12 22:47:19 +0100400 ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100401 -3|--3way)
402 threeway=t ;;
Johannes Sixtdfdd7e62007-11-06 21:33:58 +0100403 -s|--signoff)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100404 sign=t ;;
405 -u|--utf8)
406 utf8=t ;; # this is now default
407 --no-utf8)
408 utf8= ;;
Paolo Bonzinia078f732014-11-25 15:00:56 +0100409 -m|--message-id)
410 messageid=t ;;
411 --no-message-id)
412 messageid=f ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100413 -k|--keep)
414 keep=t ;;
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100415 --keep-non-patch)
416 keep=b ;;
Junio C Hamano017678b2009-08-26 21:36:05 -0700417 -c|--scissors)
418 scissors=t ;;
419 --no-scissors)
420 scissors=f ;;
Jeff Kingc8089af2010-02-11 17:27:14 -0500421 -r|--resolved|--continue)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100422 resolved=t ;;
423 --skip)
424 skip=t ;;
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900425 --abort)
426 abort=t ;;
Junio C Hamano3041c322008-03-04 00:25:06 -0800427 --rebasing)
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700428 rebasing=t threeway=t ;;
Nicolas Vigier88336622014-02-01 02:18:00 +0000429 --resolvemsg=*)
430 resolvemsg="${1#--resolvemsg=}" ;;
431 --whitespace=*|--directory=*|--exclude=*|--include=*)
432 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
433 -C*|-p*)
434 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
435 --patch-format=*)
436 patch_format="${1#--patch-format=}" ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200437 --reject|--ignore-whitespace|--ignore-space-change)
martin f. krafftb80da422009-01-23 11:31:21 +1100438 git_apply_opt="$git_apply_opt $1" ;;
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800439 --committer-date-is-author-date)
440 committer_date_is_author_date=t ;;
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900441 --ignore-date)
442 ignore_date=t ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800443 --rerere-autoupdate|--no-rerere-autoupdate)
444 allow_rerere_autoupdate="$1" ;;
Stephen Boyd0e987a12009-06-16 15:33:01 -0700445 -q|--quiet)
446 GIT_QUIET=t ;;
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100447 --keep-cr)
448 keepcr=t ;;
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +0100449 --no-keep-cr)
450 keepcr=f ;;
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +0000451 --gpg-sign)
452 gpg_sign_opt=-S ;;
453 --gpg-sign=*)
454 gpg_sign_opt="-S${1#--gpg-sign=}" ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700455 --)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100456 shift; break ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700457 *)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100458 usage ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700459 esac
Pierre Habouzit78443d92007-11-04 11:30:57 +0100460 shift
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700461done
462
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800463# If the dotest directory exists, but we have finished applying all the
464# patches in them, clear it out.
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700465if test -d "$dotest" &&
Ramkumar Ramachandrac30754f2013-05-12 17:26:35 +0530466 test -f "$dotest/last" &&
467 test -f "$dotest/next" &&
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700468 last=$(cat "$dotest/last") &&
469 next=$(cat "$dotest/next") &&
470 test $# != 0 &&
471 test "$next" -gt "$last"
472then
473 rm -fr "$dotest"
474fi
475
Ramkumar Ramachandrac30754f2013-05-12 17:26:35 +0530476if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700477then
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900478 case "$#,$skip$resolved$abort" in
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700479 0,*t*)
480 # Explicit resume command and we do not have file, so
481 # we are happy.
482 : ;;
483 0,)
484 # No file input but without resume parameters; catch
485 # user error to feed us a patch from standard input
Junio C Hamanoe72c7402008-03-04 00:25:05 -0800486 # when there is already $dotest. This is somewhat
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700487 # unreliable -- stdin could be /dev/null for example
488 # and the caller did not intend to feed us a patch but
489 # wanted to continue unattended.
Jay Soffian98ef23b2009-01-28 10:03:10 -0500490 test -t 0
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700491 ;;
492 *)
493 false
494 ;;
495 esac ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000496 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
Junio C Hamano271440e2005-10-25 23:35:37 -0700497 resume=yes
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900498
Olivier Marin95f8ebb2008-07-21 15:39:06 +0200499 case "$skip,$abort" in
Junio C Hamano2d56a132009-02-26 11:24:29 -0800500 t,t)
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000501 die "$(gettext "Please make up your mind. --skip or --abort?")"
Junio C Hamano2d56a132009-02-26 11:24:29 -0800502 ;;
Olivier Marin95f8ebb2008-07-21 15:39:06 +0200503 t,)
504 git rerere clear
505 git read-tree --reset -u HEAD HEAD
506 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
507 git reset HEAD
508 git update-ref ORIG_HEAD $orig_head
509 ;;
510 ,t)
Junio C Hamano2d56a132009-02-26 11:24:29 -0800511 if test -f "$dotest/rebasing"
512 then
513 exec git rebase --abort
514 fi
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900515 git rerere clear
Junio C Hamano7b3b7e32010-12-21 10:35:53 -0800516 if safe_to_abort
517 then
Michael J Gruberc7671842009-02-26 10:52:53 +0100518 git read-tree --reset -u HEAD ORIG_HEAD
519 git reset ORIG_HEAD
Junio C Hamano7b3b7e32010-12-21 10:35:53 -0800520 fi
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900521 rm -fr "$dotest"
522 exit ;;
523 esac
Michael J Gruberc7671842009-02-26 10:52:53 +0100524 rm -f "$dotest/dirtyindex"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700525else
Ramkumar Ramachandrab141f3c2013-06-15 18:13:11 +0530526 # Possible stray $dotest directory in the independent-run
527 # case; in the --rebasing case, it is upto the caller
528 # (git-rebase--am) to take care of stray directories.
529 if test -d "$dotest" && test -z "$rebasing"
530 then
531 case "$skip,$resolved,$abort" in
532 ,,t)
533 rm -fr "$dotest"
534 exit 0
535 ;;
536 *)
537 die "$(eval_gettext "Stray \$dotest directory found.
538Use \"git am --abort\" to remove it.")"
539 ;;
540 esac
541 fi
542
Justin Lebar01689902014-03-31 15:11:46 -0700543 # Make sure we are not given --skip, --continue, or --abort
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900544 test "$skip$resolved$abort" = "" ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000545 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700546
547 # Start afresh.
548 mkdir -p "$dotest" || exit
549
Junio C Hamanobb034f82008-03-04 00:25:04 -0800550 if test -n "$prefix" && test $# != 0
551 then
552 first=t
553 for arg
554 do
555 test -n "$first" && {
556 set x
557 first=
558 }
Pat Thoyts5e9677c2010-09-30 14:24:07 +0100559 if is_absolute_path "$arg"
560 then
561 set "$@" "$arg"
562 else
563 set "$@" "$prefix$arg"
564 fi
Junio C Hamanobb034f82008-03-04 00:25:04 -0800565 done
566 shift
567 fi
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200568
569 check_patch_format "$@"
570
571 split_patches "$@"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700572
Junio C Hamano017678b2009-08-26 21:36:05 -0700573 # -i can and must be given when resuming; everything
574 # else is kept
Junio C Hamano9b9f5a22008-12-05 11:19:43 -0800575 echo " $git_apply_opt" >"$dotest/apply-opt"
Junio C Hamano22db2402008-12-04 15:38:27 -0800576 echo "$threeway" >"$dotest/threeway"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700577 echo "$sign" >"$dotest/sign"
578 echo "$utf8" >"$dotest/utf8"
579 echo "$keep" >"$dotest/keep"
Paolo Bonzinia078f732014-11-25 15:00:56 +0100580 echo "$messageid" >"$dotest/messageid"
Junio C Hamano017678b2009-08-26 21:36:05 -0700581 echo "$scissors" >"$dotest/scissors"
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100582 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
Stephen Boyd0e987a12009-06-16 15:33:01 -0700583 echo "$GIT_QUIET" >"$dotest/quiet"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700584 echo 1 >"$dotest/next"
Junio C Hamano3041c322008-03-04 00:25:06 -0800585 if test -n "$rebasing"
586 then
587 : >"$dotest/rebasing"
588 else
589 : >"$dotest/applying"
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900590 if test -n "$HAS_HEAD"
591 then
592 git update-ref ORIG_HEAD HEAD
593 else
594 git update-ref -d ORIG_HEAD >/dev/null 2>&1
595 fi
Junio C Hamano3041c322008-03-04 00:25:06 -0800596 fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700597fi
598
Jeff King2a6f08a2011-08-15 17:13:07 -0700599git update-index -q --refresh
600
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800601case "$resolved" in
602'')
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900603 case "$HAS_HEAD" in
604 '')
605 files=$(git ls-files) ;;
606 ?*)
607 files=$(git diff-index --cached --name-only HEAD --) ;;
608 esac || exit
Michael J Gruberc7671842009-02-26 10:52:53 +0100609 if test "$files"
610 then
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900611 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000612 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
Michael J Gruberc7671842009-02-26 10:52:53 +0100613 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800614esac
615
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100616# Now, decide what command line options we will give to the git
617# commands we invoke, based on the result of parsing command line
618# options and previous invocation state stored in $dotest/ files.
619
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700620if test "$(cat "$dotest/utf8")" = t
621then
622 utf8=-u
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800623else
624 utf8=-n
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700625fi
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100626keep=$(cat "$dotest/keep")
627case "$keep" in
628t)
629 keep=-k ;;
630b)
631 keep=-b ;;
632*)
633 keep= ;;
634esac
Paolo Bonzinia078f732014-11-25 15:00:56 +0100635case "$(cat "$dotest/messageid")" in
636t)
637 messageid=-m ;;
638f)
639 messageid= ;;
640esac
Junio C Hamano017678b2009-08-26 21:36:05 -0700641case "$(cat "$dotest/scissors")" in
642t)
643 scissors=--scissors ;;
644f)
645 scissors=--no-scissors ;;
646esac
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100647if test "$(cat "$dotest/no_inbody_headers")" = t
648then
649 no_inbody_headers=--no-inbody-headers
650else
651 no_inbody_headers=
652fi
Stephen Boyd0e987a12009-06-16 15:33:01 -0700653if test "$(cat "$dotest/quiet")" = t
654then
655 GIT_QUIET=t
656fi
Junio C Hamano22db2402008-12-04 15:38:27 -0800657if test "$(cat "$dotest/threeway")" = t
658then
659 threeway=t
660fi
Junio C Hamano9b9f5a22008-12-05 11:19:43 -0800661git_apply_opt=$(cat "$dotest/apply-opt")
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700662if test "$(cat "$dotest/sign")" = t
663then
Elia Pinto75ee3d72014-03-25 10:22:22 -0700664 SIGNOFF=$(git var GIT_COMMITTER_IDENT | sed -e '
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700665 s/>.*/>/
666 s/^/Signed-off-by: /'
Elia Pinto75ee3d72014-03-25 10:22:22 -0700667 )
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700668else
669 SIGNOFF=
670fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700671
Elia Pinto75ee3d72014-03-25 10:22:22 -0700672last=$(cat "$dotest/last")
673this=$(cat "$dotest/next")
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700674if test "$skip" = t
675then
Elia Pinto75ee3d72014-03-25 10:22:22 -0700676 this=$(expr "$this" + 1)
Jan Harkes69224712005-12-17 01:01:06 -0500677 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700678fi
679
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700680while test "$this" -le "$last"
681do
Elia Pinto75ee3d72014-03-25 10:22:22 -0700682 msgnum=$(printf "%0${prec}d" $this)
683 next=$(expr "$this" + 1)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700684 test -f "$dotest/$msgnum" || {
Jan Harkes69224712005-12-17 01:01:06 -0500685 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700686 go_next
687 continue
688 }
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800689
690 # If we are not resuming, parse and extract the patch information
691 # into separate files:
692 # - info records the authorship and title
693 # - msg is the rest of commit log message
694 # - patch is the patch body.
695 #
696 # When we are resuming, these files are either already prepared
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +0300697 # by the user, or the user can tell us to do so by --continue flag.
Junio C Hamano271440e2005-10-25 23:35:37 -0700698 case "$resume" in
699 '')
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700700 if test -f "$dotest/rebasing"
701 then
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700702 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
703 -e q "$dotest/$msgnum") &&
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700704 test "$(git cat-file -t "$commit")" = commit ||
705 stop_here $this
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700706 git cat-file commit "$commit" |
707 sed -e '1,/^$/d' >"$dotest/msg-clean"
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700708 echo "$commit" >"$dotest/original-commit"
709 get_author_ident_from_commit "$commit" >"$dotest/author-script"
Junio C Hamano4ae6d462013-01-31 19:26:21 -0800710 git diff-tree --root --binary --full-index "$commit" >"$dotest/patch"
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700711 else
Paolo Bonzinia078f732014-11-25 15:00:56 +0100712 git mailinfo $keep $no_inbody_headers $messageid $scissors $utf8 "$dotest/msg" "$dotest/patch" \
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700713 <"$dotest/$msgnum" >"$dotest/info" ||
714 stop_here $this
715
716 # skip pine's internal folder data
717 sane_grep '^Author: Mail System Internal Data$' \
718 <"$dotest"/info >/dev/null &&
719 go_next && continue
720
721 test -s "$dotest/patch" || {
722 eval_gettextln "Patch is empty. Was it split wrong?
723If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
724To restore the original branch and stop patching run \"\$cmdline --abort\"."
725 stop_here $this
726 }
727 rm -f "$dotest/original-commit" "$dotest/author-script"
Junio C Hamanoc970a6f2009-11-27 15:06:37 -0800728 {
729 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
730 echo
731 cat "$dotest/msg"
732 } |
733 git stripspace > "$dotest/msg-clean"
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700734 fi
Junio C Hamano271440e2005-10-25 23:35:37 -0700735 ;;
736 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700737
Jay Soffian43c23252010-06-16 03:12:40 -0400738 if test -f "$dotest/author-script"
739 then
740 eval $(cat "$dotest/author-script")
741 else
742 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
743 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
744 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
745 fi
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800746
Junio C Hamano3b789592007-12-05 13:16:35 -0800747 if test -z "$GIT_AUTHOR_EMAIL"
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800748 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000749 gettextln "Patch does not have a valid e-mail address."
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800750 stop_here $this
751 fi
752
Junio C Hamano2c674192005-10-20 22:31:56 -0700753 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700754
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800755 case "$resume" in
756 '')
757 if test '' != "$SIGNOFF"
758 then
Elia Pinto75ee3d72014-03-25 10:22:22 -0700759 LAST_SIGNED_OFF_BY=$(
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800760 sed -ne '/^Signed-off-by: /p' \
761 "$dotest/msg-clean" |
Jeff Kingb4ce54f2008-03-12 17:34:34 -0400762 sed -ne '$p'
Elia Pinto75ee3d72014-03-25 10:22:22 -0700763 )
764 ADD_SIGNOFF=$(
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800765 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700766 test '' = "$LAST_SIGNED_OFF_BY" && echo
767 echo "$SIGNOFF"
Elia Pinto75ee3d72014-03-25 10:22:22 -0700768 })
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800769 else
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700770 ADD_SIGNOFF=
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800771 fi
772 {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700773 if test -s "$dotest/msg-clean"
774 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700775 cat "$dotest/msg-clean"
776 fi
777 if test '' != "$ADD_SIGNOFF"
778 then
779 echo "$ADD_SIGNOFF"
780 fi
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800781 } >"$dotest/final-commit"
782 ;;
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800783 *)
Junio C Hamano6d286442006-02-23 22:14:47 -0800784 case "$resolved$interactive" in
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800785 tt)
786 # This is used only for interactive view option.
Junio C Hamano38762c42007-11-28 16:15:04 -0800787 git diff-index -p --cached HEAD -- >"$dotest/patch"
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800788 ;;
789 esac
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800790 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700791
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800792 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700793 if test "$interactive" = t
794 then
Junio C Hamanoa1451102005-10-12 18:31:41 -0700795 test -t 0 ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000796 die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700797 action=again
798 while test "$action" = again
799 do
Jon Seymourde88c1c2011-08-07 21:58:14 +1000800 gettextln "Commit Body is:"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700801 echo "--------------------------"
802 cat "$dotest/final-commit"
803 echo "--------------------------"
Ævar Arnfjörð Bjarmason7a74d042011-05-21 18:43:50 +0000804 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
805 # in your translation. The program will only accept English
806 # input at this point.
807 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700808 read reply
809 case "$reply" in
Junio C Hamanof89ad672005-10-25 23:43:59 -0700810 [yY]*) action=yes ;;
811 [aA]*) action=yes interactive= ;;
812 [nN]*) action=skip ;;
Adam Robenef0c2ab2007-07-19 22:09:35 -0700813 [eE]*) git_editor "$dotest/final-commit"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700814 action=again ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700815 [vV]*) action=again
Jonathan Niederf6dff112010-02-14 23:04:13 -0600816 git_pager "$dotest/patch" ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700817 *) action=again ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700818 esac
819 done
820 else
821 action=yes
822 fi
Ramkumar Ramachandra92f65e62010-06-02 10:33:37 +0200823
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700824 if test $action = skip
825 then
826 go_next
827 continue
828 fi
829
830 if test -x "$GIT_DIR"/hooks/applypatch-msg
831 then
832 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
833 stop_here $this
834 fi
835
Simon Ruderichb34a9122013-03-21 03:40:17 +0100836 if test -f "$dotest/final-commit"
837 then
838 FIRSTLINE=$(sed 1q "$dotest/final-commit")
839 else
840 FIRSTLINE=""
841 fi
842
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000843 say "$(eval_gettext "Applying: \$FIRSTLINE")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700844
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800845 case "$resolved" in
846 '')
Stephen Boyd3ddd1702009-06-16 15:32:58 -0700847 # When we are allowed to fall back to 3-way later, don't give
848 # false errors during the initial attempt.
849 squelch=
850 if test "$threeway" = t
851 then
852 squelch='>/dev/null 2>&1 '
853 fi
854 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800855 apply_status=$?
856 ;;
857 t)
858 # Resolved means the user did all the hard work, and
859 # we do not have to do any patch application. Just
860 # trust what the user has in the index file and the
861 # working tree.
862 resolved=
Junio C Hamano38762c42007-11-28 16:15:04 -0800863 git diff-index --quiet --cached HEAD -- && {
Jon Seymourde88c1c2011-08-07 21:58:14 +1000864 gettextln "No changes - did you forget to use 'git add'?
Ævar Arnfjörð Bjarmasonbd8643a2011-05-21 18:43:44 +0000865If there is nothing left to stage, chances are that something else
Jon Seymourde88c1c2011-08-07 21:58:14 +1000866already introduced the same changes; you might want to skip this patch."
Robert Shearmanced94562006-05-02 13:32:43 +0100867 stop_here_user_resolve $this
Alex Riesen06aff472007-03-25 01:56:13 +0100868 }
Junio C Hamano5be60072007-07-02 22:52:14 -0700869 unmerged=$(git ls-files -u)
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700870 if test -n "$unmerged"
871 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000872 gettextln "You still have unmerged paths in your index
873did you forget to use 'git add'?"
Robert Shearmanced94562006-05-02 13:32:43 +0100874 stop_here_user_resolve $this
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700875 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800876 apply_status=0
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100877 git rerere
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800878 ;;
879 esac
880
Junio C Hamano0ba17dd2010-04-09 16:58:28 -0700881 if test $apply_status != 0 && test "$threeway" = t
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700882 then
Junio C Hamano73319032005-10-13 11:46:43 -0700883 if (fall_back_3way)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700884 then
Junio C Hamano73319032005-10-13 11:46:43 -0700885 # Applying the patch to an earlier tree and merging the
886 # result may have produced the same tree as ours.
Junio C Hamano38762c42007-11-28 16:15:04 -0800887 git diff-index --quiet --cached HEAD -- && {
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000888 say "$(gettext "No changes -- Patch already applied.")"
Alex Riesen06aff472007-03-25 01:56:13 +0100889 go_next
890 continue
891 }
Junio C Hamano73319032005-10-13 11:46:43 -0700892 # clear apply_status -- we have successfully merged.
893 apply_status=0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700894 fi
895 fi
896 if test $apply_status != 0
897 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000898 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
Paul Gortmaker14bf2d52012-07-13 11:51:30 -0400899 if test "$(git config --bool advice.amworkdir)" != false
900 then
Nguyễn Thái Ngọc Duya312a272012-08-22 21:48:03 +0700901 eval_gettextln 'The copy of the patch that failed is found in:
902 $dotest/patch'
Paul Gortmaker14bf2d52012-07-13 11:51:30 -0400903 fi
Robert Shearmanced94562006-05-02 13:32:43 +0100904 stop_here_user_resolve $this
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700905 fi
906
907 if test -x "$GIT_DIR"/hooks/pre-applypatch
908 then
909 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
910 fi
911
Junio C Hamano5be60072007-07-02 22:52:14 -0700912 tree=$(git write-tree) &&
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800913 commit=$(
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900914 if test -n "$ignore_date"
915 then
916 GIT_AUTHOR_DATE=
917 fi
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900918 parent=$(git rev-parse --verify -q HEAD) ||
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000919 say >&2 "$(gettext "applying to an empty history")"
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900920
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800921 if test -n "$committer_date_is_author_date"
922 then
923 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
924 export GIT_COMMITTER_DATE
925 fi &&
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +0000926 git commit-tree ${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree \
927 <"$dotest/final-commit"
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800928 ) &&
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700929 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700930 stop_here $this
931
Thomas Rast96e19482010-03-12 18:04:29 +0100932 if test -f "$dotest/original-commit"; then
933 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
934 fi
935
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700936 if test -x "$GIT_DIR"/hooks/post-applypatch
937 then
938 "$GIT_DIR"/hooks/post-applypatch
939 fi
940
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700941 go_next
942done
943
Thomas Rasteb2151b2010-03-12 18:04:33 +0100944if test -s "$dotest"/rewritten; then
945 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
946 if test -x "$GIT_DIR"/hooks/post-rewrite; then
Thomas Rast96e19482010-03-12 18:04:29 +0100947 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
Thomas Rasteb2151b2010-03-12 18:04:33 +0100948 fi
Thomas Rast96e19482010-03-12 18:04:29 +0100949fi
950
Ramkumar Ramachandraa1549e12013-05-12 17:26:38 +0530951# If am was called with --rebasing (from git-rebase--am), it's up to
952# the caller to take care of housekeeping.
953if ! test -f "$dotest/rebasing"
954then
955 rm -fr "$dotest"
956 git gc --auto
957fi