blob: c092855dd7c277d41288ee89f033bef0a8677a9f [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=
7OPTIONS_SPEC="\
Štěpán Němec0adda932010-10-08 19:31:17 +02008git am [options] [(<mbox>|<Maildir>)...]
Stephan Beyer09f8d052008-07-25 20:22:23 +02009git am [options] (--resolved | --skip | --abort)
Pierre Habouzit78443d92007-11-04 11:30:57 +010010--
Junio C Hamano66006c62007-11-08 23:04:31 -080011i,interactive run interactively
Jay Soffian98ef23b2009-01-28 10:03:10 -050012b,binary* (historical option -- no-op)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100133,3way allow fall back on 3way merging if needed
Stephen Boyd0e987a12009-06-16 15:33:01 -070014q,quiet be quiet
Pierre Habouzit78443d92007-11-04 11:30:57 +010015s,signoff add a Signed-off-by line to the commit message
16u,utf8 recode into utf8 (default)
Jörg Sommerfe1fa942008-02-03 00:58:06 +010017k,keep pass -k flag to git-mailinfo
Thomas Rastf7e5ea12012-01-16 11:53:00 +010018keep-non-patch pass -b flag to git-mailinfo
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +010019keep-cr pass --keep-cr flag to git-mailsplit for mbox format
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +010020no-keep-cr do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
Junio C Hamano017678b2009-08-26 21:36:05 -070021c,scissors strip everything before a scissors line
Pierre Habouzit78443d92007-11-04 11:30:57 +010022whitespace= pass it through git-apply
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +020023ignore-space-change pass it through git-apply
24ignore-whitespace pass it through git-apply
Junio C Hamanob47dfe92009-01-11 22:21:48 -080025directory= pass it through git-apply
maximilian attems77e9e492011-08-03 11:37:29 +020026exclude= pass it through git-apply
Johannes Berg58725ef2012-03-28 13:11:28 +020027include= pass it through git-apply
Pierre Habouzit78443d92007-11-04 11:30:57 +010028C= pass it through git-apply
29p= pass it through git-apply
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +020030patch-format= format the patch(es) are in
martin f. krafftb80da422009-01-23 11:31:21 +110031reject pass it through git-apply
Pierre Habouzit78443d92007-11-04 11:30:57 +010032resolvemsg= override error message when patch failure occurs
Jeff Kingc8089af2010-02-11 17:27:14 -050033continue continue applying patches after resolving a conflict
34r,resolved synonyms for --continue
Junio C Hamano3041c322008-03-04 00:25:06 -080035skip skip the current patch
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +090036abort restore the original branch and abort the patching operation.
Junio C Hamano3f01ad62009-01-22 16:14:58 -080037committer-date-is-author-date lie about committer date
Nanako Shiraishia79ec622009-01-24 10:18:02 +090038ignore-date use current timestamp for author date
Junio C Hamanocb6020b2009-12-04 00:20:48 -080039rerere-autoupdate update the index with reused conflict resolution if possible
Jay Soffian98ef23b2009-01-28 10:03:10 -050040rebasing* (internal use for git-rebase)"
Pierre Habouzit78443d92007-11-04 11:30:57 +010041
freku045@student.liu.secf1fe882005-12-13 23:30:31 +010042. git-sh-setup
Ævar Arnfjörð Bjarmasonb9a97262011-05-21 18:43:42 +000043. git-sh-i18n
Junio C Hamanobb034f82008-03-04 00:25:04 -080044prefix=$(git rev-parse --show-prefix)
Shawn O. Pearcef9474132006-12-28 02:34:48 -050045set_reflog_action am
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -050046require_work_tree
Jeff Kingc1491842008-03-01 01:22:55 -050047cd_to_toplevel
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070048
Stephan Beyer460abee2008-07-12 17:46:59 +020049git var GIT_COMMITTER_IDENT >/dev/null ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +000050 die "$(gettext "You need to set your committer info first")"
Junio C Hamanod64e6b02006-02-18 20:51:26 -080051
Nanako Shiraishif79d4c82009-04-10 09:34:42 +090052if git rev-parse --verify -q HEAD >/dev/null
53then
54 HAS_HEAD=yes
55else
56 HAS_HEAD=
57fi
58
Ramkumar Ramachandrad2c46312010-06-02 10:33:35 +020059cmdline="git am"
60if test '' != "$interactive"
61then
62 cmdline="$cmdline -i"
63fi
64if test '' != "$threeway"
65then
66 cmdline="$cmdline -3"
67fi
68
Junio C Hamanob47dfe92009-01-11 22:21:48 -080069sq () {
Christian Couder47c97392009-04-24 08:29:01 +020070 git rev-parse --sq-quote "$@"
Junio C Hamanob47dfe92009-01-11 22:21:48 -080071}
72
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070073stop_here () {
74 echo "$1" >"$dotest/next"
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080075 git rev-parse --verify -q HEAD >"$dotest/abort-safety"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070076 exit 1
77}
78
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080079safe_to_abort () {
80 if test -f "$dotest/dirtyindex"
81 then
82 return 1
83 fi
84
85 if ! test -s "$dotest/abort-safety"
86 then
87 return 0
88 fi
89
90 abort_safety=$(cat "$dotest/abort-safety")
91 if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
92 then
93 return 0
94 fi
Jiang Xin42e65042012-07-25 22:53:11 +080095 gettextln "You seem to have moved HEAD since the last 'am' failure.
Jon Seymourde88c1c2011-08-07 21:58:14 +100096Not rewinding to ORIG_HEAD" >&2
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080097 return 1
98}
99
Robert Shearmanced94562006-05-02 13:32:43 +0100100stop_here_user_resolve () {
Seancc120052006-05-13 23:34:08 -0400101 if [ -n "$resolvemsg" ]; then
Jeff Kinga23bfae2007-05-26 00:33:03 -0700102 printf '%s\n' "$resolvemsg"
Seancc120052006-05-13 23:34:08 -0400103 stop_here $1
104 fi
Jiang Xinc7108bf2012-07-25 22:53:08 +0800105 eval_gettextln "When you have resolved this problem, run \"\$cmdline --resolved\".
106If you prefer to skip this patch, run \"\$cmdline --skip\" instead.
107To restore the original branch and stop patching, run \"\$cmdline --abort\"."
Robert Shearmanced94562006-05-02 13:32:43 +0100108
109 stop_here $1
110}
111
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700112go_next () {
113 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
114 "$dotest/patch" "$dotest/info"
115 echo "$next" >"$dotest/next"
116 this=$next
117}
118
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700119cannot_fallback () {
120 echo "$1"
Jon Seymourde88c1c2011-08-07 21:58:14 +1000121 gettextln "Cannot fall back to three-way merge."
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700122 exit 1
123}
124
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700125fall_back_3way () {
126 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
127
128 rm -fr "$dotest"/patch-merge-*
129 mkdir "$dotest/patch-merge-tmp-dir"
130
131 # First see if the patch records the index info that we can use.
Junio C Hamano4056afb2012-02-22 21:55:06 -0800132 cmd="git apply $git_apply_opt --build-fake-ancestor" &&
133 cmd="$cmd "'"$dotest/patch-merge-tmp-index" "$dotest/patch"' &&
134 eval "$cmd" &&
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700135 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700136 git write-tree >"$dotest/patch-merge-base+" ||
Ævar Arnfjörð Bjarmasona424ca12011-05-21 18:43:48 +0000137 cannot_fallback "$(gettext "Repository lacks necessary blobs to fall back on 3-way merge.")"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700138
Jiang Xin42e65042012-07-25 22:53:11 +0800139 say "$(gettext "Using index info to reconstruct a base tree...")"
Junio C Hamano4056afb2012-02-22 21:55:06 -0800140
141 cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
Junio C Hamano5d868612012-03-28 09:55:21 -0700142
143 if test -z "$GIT_QUIET"
144 then
145 eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
146 fi
147
Junio C Hamano4056afb2012-02-22 21:55:06 -0800148 cmd="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
149 if eval "$cmd"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700150 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700151 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
152 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700153 else
Ævar Arnfjörð Bjarmasona424ca12011-05-21 18:43:48 +0000154 cannot_fallback "$(gettext "Did you hand edit your patch?
155It does not apply to blobs recorded in its index.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700156 fi
157
158 test -f "$dotest/patch-merge-index" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700159 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700160 orig_tree=$(cat "$dotest/patch-merge-base") &&
161 rm -fr "$dotest"/patch-merge-* || exit 1
162
Ævar Arnfjörð Bjarmason865207a2011-05-21 18:43:51 +0000163 say "$(gettext "Falling back to patching base and 3-way merge...")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700164
165 # This is not so wrong. Depending on which base we picked,
166 # orig_tree may be wildly different from ours, but his_tree
167 # has the same set of wildly different changes in parts the
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500168 # patch did not touch, so recursive ends up canceling them,
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700169 # saying that we reverted all those changes.
170
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700171 eval GITHEAD_$his_tree='"$FIRSTLINE"'
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500172 export GITHEAD_$his_tree
Stephen Boyd0e987a12009-06-16 15:33:01 -0700173 if test -n "$GIT_QUIET"
174 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700175 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boyd0e987a12009-06-16 15:33:01 -0700176 fi
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500177 git-merge-recursive $orig_tree -- HEAD $his_tree || {
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800178 git rerere $allow_rerere_autoupdate
Jiang Xin42e65042012-07-25 22:53:11 +0800179 die "$(gettext "Failed to merge in the changes.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700180 }
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500181 unset GITHEAD_$his_tree
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700182}
183
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200184clean_abort () {
185 test $# = 0 || echo >&2 "$@"
186 rm -fr "$dotest"
187 exit 1
188}
189
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200190patch_format=
191
192check_patch_format () {
193 # early return if patch_format was set from the command line
194 if test -n "$patch_format"
195 then
196 return 0
197 fi
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200198
199 # we default to mbox format if input is from stdin and for
200 # directories
201 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
202 then
203 patch_format=mbox
204 return 0
205 fi
206
David Barr0e8341f2011-08-08 04:49:04 +0200207 # otherwise, check the first few non-blank lines of the first
208 # patch to try to detect its format
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200209 {
David Barr0e8341f2011-08-08 04:49:04 +0200210 # Start from first line containing non-whitespace
211 l1=
212 while test -z "$l1"
213 do
Jim Meyeringf0c57932012-02-25 18:34:26 +0100214 read l1 || break
David Barr0e8341f2011-08-08 04:49:04 +0200215 done
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200216 read l2
217 read l3
218 case "$l1" in
219 "From "* | "From: "*)
220 patch_format=mbox
221 ;;
222 '# This series applies on GIT commit'*)
223 patch_format=stgit-series
224 ;;
225 "# HG changeset patch")
226 patch_format=hg
227 ;;
228 *)
229 # if the second line is empty and the third is
230 # a From, Author or Date entry, this is very
231 # likely an StGIT patch
232 case "$l2,$l3" in
233 ,"From: "* | ,"Author: "* | ,"Date: "*)
234 patch_format=stgit
235 ;;
236 *)
237 ;;
238 esac
239 ;;
240 esac
Junio C Hamano0fcb2ca2009-08-06 20:08:12 -0500241 if test -z "$patch_format" &&
242 test -n "$l1" &&
243 test -n "$l2" &&
244 test -n "$l3"
245 then
246 # This begins with three non-empty lines. Is this a
247 # piece of e-mail a-la RFC2822? Grab all the headers,
248 # discarding the indented remainder of folded lines,
249 # and see if it looks like that they all begin with the
250 # header field names...
Stephen Boyde3f67d32010-01-25 16:33:59 -0800251 tr -d '\015' <"$1" |
252 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800253 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
Junio C Hamano0fcb2ca2009-08-06 20:08:12 -0500254 patch_format=mbox
255 fi
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200256 } < "$1" || clean_abort
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200257}
258
259split_patches () {
260 case "$patch_format" in
261 mbox)
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700262 if test t = "$keepcr"
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100263 then
264 keep_cr=--keep-cr
265 else
266 keep_cr=
267 fi
Junio C Hamanoc2ca1d72009-08-04 22:31:59 -0500268 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200269 clean_abort
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200270 ;;
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200271 stgit-series)
272 if test $# -ne 1
273 then
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000274 clean_abort "$(gettext "Only one StGIT patch series can be applied at once")"
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200275 fi
276 series_dir=`dirname "$1"`
277 series_file="$1"
278 shift
279 {
280 set x
281 while read filename
282 do
283 set "$@" "$series_dir/$filename"
284 done
285 # remove the safety x
286 shift
287 # remove the arg coming from the first-line comment
288 shift
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200289 } < "$series_file" || clean_abort
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200290 # set the patch format appropriately
291 patch_format=stgit
292 # now handle the actual StGIT patches
293 split_patches "$@"
294 ;;
295 stgit)
296 this=0
297 for stgit in "$@"
298 do
299 this=`expr "$this" + 1`
300 msgnum=`printf "%0${prec}d" $this`
301 # Perl version of StGIT parse_patch. The first nonemptyline
302 # not starting with Author, From or Date is the
303 # subject, and the body starts with the next nonempty
304 # line not starting with Author, From or Date
305 perl -ne 'BEGIN { $subject = 0 }
306 if ($subject > 1) { print ; }
307 elsif (/^\s+$/) { next ; }
Giuseppe Bilotta45d51dc2011-08-29 18:44:07 +0200308 elsif (/^Author:/) { s/Author/From/ ; print ;}
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200309 elsif (/^(From|Date)/) { print ; }
310 elsif ($subject) {
311 $subject = 2 ;
312 print "\n" ;
313 print ;
314 } else {
315 print "Subject: ", $_ ;
316 $subject = 1;
317 }
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200318 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200319 done
320 echo "$this" > "$dotest/last"
321 this=
322 msgnum=
323 ;;
Giuseppe Bilotta0cfd1122011-08-29 18:44:06 +0200324 hg)
325 this=0
326 for hg in "$@"
327 do
328 this=$(( $this + 1 ))
329 msgnum=$(printf "%0${prec}d" $this)
330 # hg stores changeset metadata in #-commented lines preceding
331 # the commit message and diff(s). The only metadata we care about
332 # are the User and Date (Node ID and Parent are hashes which are
333 # only relevant to the hg repository and thus not useful to us)
334 # Since we cannot guarantee that the commit message is in
335 # git-friendly format, we put no Subject: line and just consume
336 # all of the message as the body
Dmitry V. Levin5185b972013-01-15 00:59:33 +0400337 LANG=C LC_ALL=C perl -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
Giuseppe Bilotta0cfd1122011-08-29 18:44:06 +0200338 if ($subject) { print ; }
339 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
340 elsif (/^\# Date /) {
341 my ($hashsign, $str, $time, $tz) = split ;
342 $tz = sprintf "%+05d", (0-$tz)/36;
343 print "Date: " .
344 strftime("%a, %d %b %Y %H:%M:%S ",
345 localtime($time))
346 . "$tz\n";
347 } elsif (/^\# /) { next ; }
348 else {
349 print "\n", $_ ;
350 $subject = 1;
351 }
352 ' <"$hg" >"$dotest/$msgnum" || clean_abort
353 done
354 echo "$this" >"$dotest/last"
355 this=
356 msgnum=
357 ;;
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200358 *)
Giuseppe Bilottadff4b0e2011-08-29 17:22:06 +0200359 if test -n "$patch_format"
360 then
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000361 clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
Nicolas Sebrecht46caf502009-08-06 20:08:13 -0500362 else
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000363 clean_abort "$(gettext "Patch format detection failed.")"
Nicolas Sebrecht46caf502009-08-06 20:08:13 -0500364 fi
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200365 ;;
366 esac
367}
368
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700369prec=4
Johannes Schindelin51ef1da2008-07-21 12:51:02 +0200370dotest="$GIT_DIR/rebase-apply"
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100371sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100372resolvemsg= resume= scissors= no_inbody_headers=
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200373git_apply_opt=
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800374committer_date_is_author_date=
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900375ignore_date=
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800376allow_rerere_autoupdate=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700377
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +0100378if test "$(git config --bool --get am.keepcr)" = true
379then
380 keepcr=t
381fi
382
David Kastrup822f7c72007-09-23 22:42:08 +0200383while test $# != 0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700384do
385 case "$1" in
Pierre Habouzit78443d92007-11-04 11:30:57 +0100386 -i|--interactive)
387 interactive=t ;;
388 -b|--binary)
Jiang Xin42e65042012-07-25 22:53:11 +0800389 gettextln >&2 "The -b/--binary option has been a no-op for long time, and
390it will be removed. Please do not use it anymore."
Thomas Rast4eeb1de2012-03-12 22:47:19 +0100391 ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100392 -3|--3way)
393 threeway=t ;;
Johannes Sixtdfdd7e62007-11-06 21:33:58 +0100394 -s|--signoff)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100395 sign=t ;;
396 -u|--utf8)
397 utf8=t ;; # this is now default
398 --no-utf8)
399 utf8= ;;
400 -k|--keep)
401 keep=t ;;
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100402 --keep-non-patch)
403 keep=b ;;
Junio C Hamano017678b2009-08-26 21:36:05 -0700404 -c|--scissors)
405 scissors=t ;;
406 --no-scissors)
407 scissors=f ;;
Jeff Kingc8089af2010-02-11 17:27:14 -0500408 -r|--resolved|--continue)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100409 resolved=t ;;
410 --skip)
411 skip=t ;;
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900412 --abort)
413 abort=t ;;
Junio C Hamano3041c322008-03-04 00:25:06 -0800414 --rebasing)
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700415 rebasing=t threeway=t ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100416 --resolvemsg)
417 shift; resolvemsg=$1 ;;
Johannes Berg58725ef2012-03-28 13:11:28 +0200418 --whitespace|--directory|--exclude|--include)
Junio C Hamanob47dfe92009-01-11 22:21:48 -0800419 git_apply_opt="$git_apply_opt $(sq "$1=$2")"; shift ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100420 -C|-p)
Junio C Hamanob47dfe92009-01-11 22:21:48 -0800421 git_apply_opt="$git_apply_opt $(sq "$1$2")"; shift ;;
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200422 --patch-format)
423 shift ; patch_format="$1" ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200424 --reject|--ignore-whitespace|--ignore-space-change)
martin f. krafftb80da422009-01-23 11:31:21 +1100425 git_apply_opt="$git_apply_opt $1" ;;
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800426 --committer-date-is-author-date)
427 committer_date_is_author_date=t ;;
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900428 --ignore-date)
429 ignore_date=t ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800430 --rerere-autoupdate|--no-rerere-autoupdate)
431 allow_rerere_autoupdate="$1" ;;
Stephen Boyd0e987a12009-06-16 15:33:01 -0700432 -q|--quiet)
433 GIT_QUIET=t ;;
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100434 --keep-cr)
435 keepcr=t ;;
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +0100436 --no-keep-cr)
437 keepcr=f ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700438 --)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100439 shift; break ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700440 *)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100441 usage ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700442 esac
Pierre Habouzit78443d92007-11-04 11:30:57 +0100443 shift
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700444done
445
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800446# If the dotest directory exists, but we have finished applying all the
447# patches in them, clear it out.
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700448if test -d "$dotest" &&
449 last=$(cat "$dotest/last") &&
450 next=$(cat "$dotest/next") &&
451 test $# != 0 &&
452 test "$next" -gt "$last"
453then
454 rm -fr "$dotest"
455fi
456
457if test -d "$dotest"
458then
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900459 case "$#,$skip$resolved$abort" in
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700460 0,*t*)
461 # Explicit resume command and we do not have file, so
462 # we are happy.
463 : ;;
464 0,)
465 # No file input but without resume parameters; catch
466 # user error to feed us a patch from standard input
Junio C Hamanoe72c7402008-03-04 00:25:05 -0800467 # when there is already $dotest. This is somewhat
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700468 # unreliable -- stdin could be /dev/null for example
469 # and the caller did not intend to feed us a patch but
470 # wanted to continue unattended.
Jay Soffian98ef23b2009-01-28 10:03:10 -0500471 test -t 0
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700472 ;;
473 *)
474 false
475 ;;
476 esac ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000477 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
Junio C Hamano271440e2005-10-25 23:35:37 -0700478 resume=yes
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900479
Olivier Marin95f8ebb2008-07-21 15:39:06 +0200480 case "$skip,$abort" in
Junio C Hamano2d56a132009-02-26 11:24:29 -0800481 t,t)
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000482 die "$(gettext "Please make up your mind. --skip or --abort?")"
Junio C Hamano2d56a132009-02-26 11:24:29 -0800483 ;;
Olivier Marin95f8ebb2008-07-21 15:39:06 +0200484 t,)
485 git rerere clear
486 git read-tree --reset -u HEAD HEAD
487 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
488 git reset HEAD
489 git update-ref ORIG_HEAD $orig_head
490 ;;
491 ,t)
Junio C Hamano2d56a132009-02-26 11:24:29 -0800492 if test -f "$dotest/rebasing"
493 then
494 exec git rebase --abort
495 fi
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900496 git rerere clear
Junio C Hamano7b3b7e32010-12-21 10:35:53 -0800497 if safe_to_abort
498 then
Michael J Gruberc7671842009-02-26 10:52:53 +0100499 git read-tree --reset -u HEAD ORIG_HEAD
500 git reset ORIG_HEAD
Junio C Hamano7b3b7e32010-12-21 10:35:53 -0800501 fi
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900502 rm -fr "$dotest"
503 exit ;;
504 esac
Michael J Gruberc7671842009-02-26 10:52:53 +0100505 rm -f "$dotest/dirtyindex"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700506else
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900507 # Make sure we are not given --skip, --resolved, nor --abort
508 test "$skip$resolved$abort" = "" ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000509 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700510
511 # Start afresh.
512 mkdir -p "$dotest" || exit
513
Junio C Hamanobb034f82008-03-04 00:25:04 -0800514 if test -n "$prefix" && test $# != 0
515 then
516 first=t
517 for arg
518 do
519 test -n "$first" && {
520 set x
521 first=
522 }
Pat Thoyts5e9677c2010-09-30 14:24:07 +0100523 if is_absolute_path "$arg"
524 then
525 set "$@" "$arg"
526 else
527 set "$@" "$prefix$arg"
528 fi
Junio C Hamanobb034f82008-03-04 00:25:04 -0800529 done
530 shift
531 fi
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200532
533 check_patch_format "$@"
534
535 split_patches "$@"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700536
Junio C Hamano017678b2009-08-26 21:36:05 -0700537 # -i can and must be given when resuming; everything
538 # else is kept
Junio C Hamano9b9f5a22008-12-05 11:19:43 -0800539 echo " $git_apply_opt" >"$dotest/apply-opt"
Junio C Hamano22db2402008-12-04 15:38:27 -0800540 echo "$threeway" >"$dotest/threeway"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700541 echo "$sign" >"$dotest/sign"
542 echo "$utf8" >"$dotest/utf8"
543 echo "$keep" >"$dotest/keep"
Junio C Hamano017678b2009-08-26 21:36:05 -0700544 echo "$scissors" >"$dotest/scissors"
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100545 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
Stephen Boyd0e987a12009-06-16 15:33:01 -0700546 echo "$GIT_QUIET" >"$dotest/quiet"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700547 echo 1 >"$dotest/next"
Junio C Hamano3041c322008-03-04 00:25:06 -0800548 if test -n "$rebasing"
549 then
550 : >"$dotest/rebasing"
551 else
552 : >"$dotest/applying"
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900553 if test -n "$HAS_HEAD"
554 then
555 git update-ref ORIG_HEAD HEAD
556 else
557 git update-ref -d ORIG_HEAD >/dev/null 2>&1
558 fi
Junio C Hamano3041c322008-03-04 00:25:06 -0800559 fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700560fi
561
Jeff King2a6f08a2011-08-15 17:13:07 -0700562git update-index -q --refresh
563
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800564case "$resolved" in
565'')
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900566 case "$HAS_HEAD" in
567 '')
568 files=$(git ls-files) ;;
569 ?*)
570 files=$(git diff-index --cached --name-only HEAD --) ;;
571 esac || exit
Michael J Gruberc7671842009-02-26 10:52:53 +0100572 if test "$files"
573 then
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900574 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000575 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
Michael J Gruberc7671842009-02-26 10:52:53 +0100576 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800577esac
578
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100579# Now, decide what command line options we will give to the git
580# commands we invoke, based on the result of parsing command line
581# options and previous invocation state stored in $dotest/ files.
582
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700583if test "$(cat "$dotest/utf8")" = t
584then
585 utf8=-u
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800586else
587 utf8=-n
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700588fi
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100589keep=$(cat "$dotest/keep")
590case "$keep" in
591t)
592 keep=-k ;;
593b)
594 keep=-b ;;
595*)
596 keep= ;;
597esac
Junio C Hamano017678b2009-08-26 21:36:05 -0700598case "$(cat "$dotest/scissors")" in
599t)
600 scissors=--scissors ;;
601f)
602 scissors=--no-scissors ;;
603esac
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100604if test "$(cat "$dotest/no_inbody_headers")" = t
605then
606 no_inbody_headers=--no-inbody-headers
607else
608 no_inbody_headers=
609fi
Stephen Boyd0e987a12009-06-16 15:33:01 -0700610if test "$(cat "$dotest/quiet")" = t
611then
612 GIT_QUIET=t
613fi
Junio C Hamano22db2402008-12-04 15:38:27 -0800614if test "$(cat "$dotest/threeway")" = t
615then
616 threeway=t
617fi
Junio C Hamano9b9f5a22008-12-05 11:19:43 -0800618git_apply_opt=$(cat "$dotest/apply-opt")
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700619if test "$(cat "$dotest/sign")" = t
620then
Todd Zullinger5354a562008-07-30 13:48:33 -0400621 SIGNOFF=`git var GIT_COMMITTER_IDENT | sed -e '
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700622 s/>.*/>/
623 s/^/Signed-off-by: /'
624 `
625else
626 SIGNOFF=
627fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700628
629last=`cat "$dotest/last"`
630this=`cat "$dotest/next"`
631if test "$skip" = t
632then
633 this=`expr "$this" + 1`
Jan Harkes69224712005-12-17 01:01:06 -0500634 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700635fi
636
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700637while test "$this" -le "$last"
638do
639 msgnum=`printf "%0${prec}d" $this`
640 next=`expr "$this" + 1`
641 test -f "$dotest/$msgnum" || {
Jan Harkes69224712005-12-17 01:01:06 -0500642 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700643 go_next
644 continue
645 }
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800646
647 # If we are not resuming, parse and extract the patch information
648 # into separate files:
649 # - info records the authorship and title
650 # - msg is the rest of commit log message
651 # - patch is the patch body.
652 #
653 # When we are resuming, these files are either already prepared
654 # by the user, or the user can tell us to do so by --resolved flag.
Junio C Hamano271440e2005-10-25 23:35:37 -0700655 case "$resume" in
656 '')
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700657 if test -f "$dotest/rebasing"
658 then
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700659 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
660 -e q "$dotest/$msgnum") &&
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700661 test "$(git cat-file -t "$commit")" = commit ||
662 stop_here $this
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700663 git cat-file commit "$commit" |
664 sed -e '1,/^$/d' >"$dotest/msg-clean"
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700665 echo "$commit" >"$dotest/original-commit"
666 get_author_ident_from_commit "$commit" >"$dotest/author-script"
Junio C Hamano4ae6d462013-01-31 19:26:21 -0800667 git diff-tree --root --binary --full-index "$commit" >"$dotest/patch"
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700668 else
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700669 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
670 <"$dotest/$msgnum" >"$dotest/info" ||
671 stop_here $this
672
673 # skip pine's internal folder data
674 sane_grep '^Author: Mail System Internal Data$' \
675 <"$dotest"/info >/dev/null &&
676 go_next && continue
677
678 test -s "$dotest/patch" || {
679 eval_gettextln "Patch is empty. Was it split wrong?
680If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
681To restore the original branch and stop patching run \"\$cmdline --abort\"."
682 stop_here $this
683 }
684 rm -f "$dotest/original-commit" "$dotest/author-script"
Junio C Hamanoc970a6f2009-11-27 15:06:37 -0800685 {
686 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
687 echo
688 cat "$dotest/msg"
689 } |
690 git stripspace > "$dotest/msg-clean"
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700691 fi
Junio C Hamano271440e2005-10-25 23:35:37 -0700692 ;;
693 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700694
Jay Soffian43c23252010-06-16 03:12:40 -0400695 if test -f "$dotest/author-script"
696 then
697 eval $(cat "$dotest/author-script")
698 else
699 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
700 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
701 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
702 fi
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800703
Junio C Hamano3b789592007-12-05 13:16:35 -0800704 if test -z "$GIT_AUTHOR_EMAIL"
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800705 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000706 gettextln "Patch does not have a valid e-mail address."
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800707 stop_here $this
708 fi
709
Junio C Hamano2c674192005-10-20 22:31:56 -0700710 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700711
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800712 case "$resume" in
713 '')
714 if test '' != "$SIGNOFF"
715 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700716 LAST_SIGNED_OFF_BY=`
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800717 sed -ne '/^Signed-off-by: /p' \
718 "$dotest/msg-clean" |
Jeff Kingb4ce54f2008-03-12 17:34:34 -0400719 sed -ne '$p'
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700720 `
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800721 ADD_SIGNOFF=`
722 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700723 test '' = "$LAST_SIGNED_OFF_BY" && echo
724 echo "$SIGNOFF"
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800725 }`
726 else
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700727 ADD_SIGNOFF=
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800728 fi
729 {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700730 if test -s "$dotest/msg-clean"
731 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700732 cat "$dotest/msg-clean"
733 fi
734 if test '' != "$ADD_SIGNOFF"
735 then
736 echo "$ADD_SIGNOFF"
737 fi
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800738 } >"$dotest/final-commit"
739 ;;
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800740 *)
Junio C Hamano6d286442006-02-23 22:14:47 -0800741 case "$resolved$interactive" in
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800742 tt)
743 # This is used only for interactive view option.
Junio C Hamano38762c42007-11-28 16:15:04 -0800744 git diff-index -p --cached HEAD -- >"$dotest/patch"
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800745 ;;
746 esac
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800747 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700748
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800749 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700750 if test "$interactive" = t
751 then
Junio C Hamanoa1451102005-10-12 18:31:41 -0700752 test -t 0 ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000753 die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700754 action=again
755 while test "$action" = again
756 do
Jon Seymourde88c1c2011-08-07 21:58:14 +1000757 gettextln "Commit Body is:"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700758 echo "--------------------------"
759 cat "$dotest/final-commit"
760 echo "--------------------------"
Ævar Arnfjörð Bjarmason7a74d042011-05-21 18:43:50 +0000761 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
762 # in your translation. The program will only accept English
763 # input at this point.
764 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700765 read reply
766 case "$reply" in
Junio C Hamanof89ad672005-10-25 23:43:59 -0700767 [yY]*) action=yes ;;
768 [aA]*) action=yes interactive= ;;
769 [nN]*) action=skip ;;
Adam Robenef0c2ab2007-07-19 22:09:35 -0700770 [eE]*) git_editor "$dotest/final-commit"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700771 action=again ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700772 [vV]*) action=again
Jonathan Niederf6dff112010-02-14 23:04:13 -0600773 git_pager "$dotest/patch" ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700774 *) action=again ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700775 esac
776 done
777 else
778 action=yes
779 fi
Ramkumar Ramachandra92f65e62010-06-02 10:33:37 +0200780
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700781 if test $action = skip
782 then
783 go_next
784 continue
785 fi
786
787 if test -x "$GIT_DIR"/hooks/applypatch-msg
788 then
789 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
790 stop_here $this
791 fi
792
Simon Ruderichb34a9122013-03-21 03:40:17 +0100793 if test -f "$dotest/final-commit"
794 then
795 FIRSTLINE=$(sed 1q "$dotest/final-commit")
796 else
797 FIRSTLINE=""
798 fi
799
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000800 say "$(eval_gettext "Applying: \$FIRSTLINE")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700801
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800802 case "$resolved" in
803 '')
Stephen Boyd3ddd1702009-06-16 15:32:58 -0700804 # When we are allowed to fall back to 3-way later, don't give
805 # false errors during the initial attempt.
806 squelch=
807 if test "$threeway" = t
808 then
809 squelch='>/dev/null 2>&1 '
810 fi
811 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800812 apply_status=$?
813 ;;
814 t)
815 # Resolved means the user did all the hard work, and
816 # we do not have to do any patch application. Just
817 # trust what the user has in the index file and the
818 # working tree.
819 resolved=
Junio C Hamano38762c42007-11-28 16:15:04 -0800820 git diff-index --quiet --cached HEAD -- && {
Jon Seymourde88c1c2011-08-07 21:58:14 +1000821 gettextln "No changes - did you forget to use 'git add'?
Ævar Arnfjörð Bjarmasonbd8643a2011-05-21 18:43:44 +0000822If there is nothing left to stage, chances are that something else
Jon Seymourde88c1c2011-08-07 21:58:14 +1000823already introduced the same changes; you might want to skip this patch."
Robert Shearmanced94562006-05-02 13:32:43 +0100824 stop_here_user_resolve $this
Alex Riesen06aff472007-03-25 01:56:13 +0100825 }
Junio C Hamano5be60072007-07-02 22:52:14 -0700826 unmerged=$(git ls-files -u)
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700827 if test -n "$unmerged"
828 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000829 gettextln "You still have unmerged paths in your index
830did you forget to use 'git add'?"
Robert Shearmanced94562006-05-02 13:32:43 +0100831 stop_here_user_resolve $this
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700832 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800833 apply_status=0
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100834 git rerere
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800835 ;;
836 esac
837
Junio C Hamano0ba17dd2010-04-09 16:58:28 -0700838 if test $apply_status != 0 && test "$threeway" = t
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700839 then
Junio C Hamano73319032005-10-13 11:46:43 -0700840 if (fall_back_3way)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700841 then
Junio C Hamano73319032005-10-13 11:46:43 -0700842 # Applying the patch to an earlier tree and merging the
843 # result may have produced the same tree as ours.
Junio C Hamano38762c42007-11-28 16:15:04 -0800844 git diff-index --quiet --cached HEAD -- && {
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000845 say "$(gettext "No changes -- Patch already applied.")"
Alex Riesen06aff472007-03-25 01:56:13 +0100846 go_next
847 continue
848 }
Junio C Hamano73319032005-10-13 11:46:43 -0700849 # clear apply_status -- we have successfully merged.
850 apply_status=0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700851 fi
852 fi
853 if test $apply_status != 0
854 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000855 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
Paul Gortmaker14bf2d52012-07-13 11:51:30 -0400856 if test "$(git config --bool advice.amworkdir)" != false
857 then
Nguyễn Thái Ngọc Duya312a272012-08-22 21:48:03 +0700858 eval_gettextln 'The copy of the patch that failed is found in:
859 $dotest/patch'
Paul Gortmaker14bf2d52012-07-13 11:51:30 -0400860 fi
Robert Shearmanced94562006-05-02 13:32:43 +0100861 stop_here_user_resolve $this
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700862 fi
863
864 if test -x "$GIT_DIR"/hooks/pre-applypatch
865 then
866 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
867 fi
868
Junio C Hamano5be60072007-07-02 22:52:14 -0700869 tree=$(git write-tree) &&
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800870 commit=$(
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900871 if test -n "$ignore_date"
872 then
873 GIT_AUTHOR_DATE=
874 fi
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900875 parent=$(git rev-parse --verify -q HEAD) ||
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000876 say >&2 "$(gettext "applying to an empty history")"
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900877
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800878 if test -n "$committer_date_is_author_date"
879 then
880 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
881 export GIT_COMMITTER_DATE
882 fi &&
Ben Jacksonea10b602009-04-18 20:42:07 -0700883 git commit-tree $tree ${parent:+-p} $parent <"$dotest/final-commit"
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800884 ) &&
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700885 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700886 stop_here $this
887
Thomas Rast96e19482010-03-12 18:04:29 +0100888 if test -f "$dotest/original-commit"; then
889 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
890 fi
891
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700892 if test -x "$GIT_DIR"/hooks/post-applypatch
893 then
894 "$GIT_DIR"/hooks/post-applypatch
895 fi
896
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700897 go_next
898done
899
Thomas Rasteb2151b2010-03-12 18:04:33 +0100900if test -s "$dotest"/rewritten; then
901 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
902 if test -x "$GIT_DIR"/hooks/post-rewrite; then
Thomas Rast96e19482010-03-12 18:04:29 +0100903 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
Thomas Rasteb2151b2010-03-12 18:04:33 +0100904 fi
Thomas Rast96e19482010-03-12 18:04:29 +0100905fi
906
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700907rm -fr "$dotest"
Jonathan Nieder29b67542010-02-22 08:35:46 -0600908git gc --auto