blob: ee61a77d717a7210a1cf614914ab62788fd5e4fc [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
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +010020keep-cr pass --keep-cr flag to git-mailsplit for mbox format
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +010021no-keep-cr do not pass --keep-cr flag to git-mailsplit independent of am.keepcr
Junio C Hamano017678b2009-08-26 21:36:05 -070022c,scissors strip everything before a scissors line
Pierre Habouzit78443d92007-11-04 11:30:57 +010023whitespace= pass it through git-apply
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +020024ignore-space-change pass it through git-apply
25ignore-whitespace pass it through git-apply
Junio C Hamanob47dfe92009-01-11 22:21:48 -080026directory= pass it through git-apply
maximilian attems77e9e492011-08-03 11:37:29 +020027exclude= pass it through git-apply
Johannes Berg58725ef2012-03-28 13:11:28 +020028include= pass it through git-apply
Pierre Habouzit78443d92007-11-04 11:30:57 +010029C= pass it through git-apply
30p= pass it through git-apply
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +020031patch-format= format the patch(es) are in
martin f. krafftb80da422009-01-23 11:31:21 +110032reject pass it through git-apply
Pierre Habouzit78443d92007-11-04 11:30:57 +010033resolvemsg= override error message when patch failure occurs
Jeff Kingc8089af2010-02-11 17:27:14 -050034continue continue applying patches after resolving a conflict
35r,resolved synonyms for --continue
Junio C Hamano3041c322008-03-04 00:25:06 -080036skip skip the current patch
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +090037abort restore the original branch and abort the patching operation.
Junio C Hamano3f01ad62009-01-22 16:14:58 -080038committer-date-is-author-date lie about committer date
Nanako Shiraishia79ec622009-01-24 10:18:02 +090039ignore-date use current timestamp for author date
Junio C Hamanocb6020b2009-12-04 00:20:48 -080040rerere-autoupdate update the index with reused conflict resolution if possible
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +000041S,gpg-sign? GPG-sign commits
Jay Soffian98ef23b2009-01-28 10:03:10 -050042rebasing* (internal use for git-rebase)"
Pierre Habouzit78443d92007-11-04 11:30:57 +010043
freku045@student.liu.secf1fe882005-12-13 23:30:31 +010044. git-sh-setup
Ævar Arnfjörð Bjarmasonb9a97262011-05-21 18:43:42 +000045. git-sh-i18n
Junio C Hamanobb034f82008-03-04 00:25:04 -080046prefix=$(git rev-parse --show-prefix)
Shawn O. Pearcef9474132006-12-28 02:34:48 -050047set_reflog_action am
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -050048require_work_tree
Jeff Kingc1491842008-03-01 01:22:55 -050049cd_to_toplevel
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070050
Stephan Beyer460abee2008-07-12 17:46:59 +020051git var GIT_COMMITTER_IDENT >/dev/null ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +000052 die "$(gettext "You need to set your committer info first")"
Junio C Hamanod64e6b02006-02-18 20:51:26 -080053
Nanako Shiraishif79d4c82009-04-10 09:34:42 +090054if git rev-parse --verify -q HEAD >/dev/null
55then
56 HAS_HEAD=yes
57else
58 HAS_HEAD=
59fi
60
Ramkumar Ramachandrad2c46312010-06-02 10:33:35 +020061cmdline="git am"
62if test '' != "$interactive"
63then
64 cmdline="$cmdline -i"
65fi
66if test '' != "$threeway"
67then
68 cmdline="$cmdline -3"
69fi
70
Junio C Hamanob47dfe92009-01-11 22:21:48 -080071sq () {
Christian Couder47c97392009-04-24 08:29:01 +020072 git rev-parse --sq-quote "$@"
Junio C Hamanob47dfe92009-01-11 22:21:48 -080073}
74
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070075stop_here () {
76 echo "$1" >"$dotest/next"
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080077 git rev-parse --verify -q HEAD >"$dotest/abort-safety"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070078 exit 1
79}
80
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080081safe_to_abort () {
82 if test -f "$dotest/dirtyindex"
83 then
84 return 1
85 fi
86
87 if ! test -s "$dotest/abort-safety"
88 then
89 return 0
90 fi
91
92 abort_safety=$(cat "$dotest/abort-safety")
93 if test "z$(git rev-parse --verify -q HEAD)" = "z$abort_safety"
94 then
95 return 0
96 fi
Jiang Xin42e65042012-07-25 22:53:11 +080097 gettextln "You seem to have moved HEAD since the last 'am' failure.
Jon Seymourde88c1c2011-08-07 21:58:14 +100098Not rewinding to ORIG_HEAD" >&2
Junio C Hamano7b3b7e32010-12-21 10:35:53 -080099 return 1
100}
101
Robert Shearmanced94562006-05-02 13:32:43 +0100102stop_here_user_resolve () {
Seancc120052006-05-13 23:34:08 -0400103 if [ -n "$resolvemsg" ]; then
Jeff Kinga23bfae2007-05-26 00:33:03 -0700104 printf '%s\n' "$resolvemsg"
Seancc120052006-05-13 23:34:08 -0400105 stop_here $1
106 fi
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +0300107 eval_gettextln "When you have resolved this problem, run \"\$cmdline --continue\".
Jiang Xinc7108bf2012-07-25 22:53:08 +0800108If you prefer to skip this patch, run \"\$cmdline --skip\" instead.
109To restore the original branch and stop patching, run \"\$cmdline --abort\"."
Robert Shearmanced94562006-05-02 13:32:43 +0100110
111 stop_here $1
112}
113
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700114go_next () {
115 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
116 "$dotest/patch" "$dotest/info"
117 echo "$next" >"$dotest/next"
118 this=$next
119}
120
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700121cannot_fallback () {
122 echo "$1"
Jon Seymourde88c1c2011-08-07 21:58:14 +1000123 gettextln "Cannot fall back to three-way merge."
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700124 exit 1
125}
126
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700127fall_back_3way () {
Elia Pinto75ee3d72014-03-25 10:22:22 -0700128 O_OBJECT=$(cd "$GIT_OBJECT_DIRECTORY" && pwd)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700129
130 rm -fr "$dotest"/patch-merge-*
131 mkdir "$dotest/patch-merge-tmp-dir"
132
133 # First see if the patch records the index info that we can use.
Junio C Hamano4056afb2012-02-22 21:55:06 -0800134 cmd="git apply $git_apply_opt --build-fake-ancestor" &&
135 cmd="$cmd "'"$dotest/patch-merge-tmp-index" "$dotest/patch"' &&
136 eval "$cmd" &&
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700137 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
Junio C Hamano5be60072007-07-02 22:52:14 -0700138 git write-tree >"$dotest/patch-merge-base+" ||
Ævar Arnfjörð Bjarmasona424ca12011-05-21 18:43:48 +0000139 cannot_fallback "$(gettext "Repository lacks necessary blobs to fall back on 3-way merge.")"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700140
Jiang Xin42e65042012-07-25 22:53:11 +0800141 say "$(gettext "Using index info to reconstruct a base tree...")"
Junio C Hamano4056afb2012-02-22 21:55:06 -0800142
143 cmd='GIT_INDEX_FILE="$dotest/patch-merge-tmp-index"'
Junio C Hamano5d868612012-03-28 09:55:21 -0700144
145 if test -z "$GIT_QUIET"
146 then
147 eval "$cmd git diff-index --cached --diff-filter=AM --name-status HEAD"
148 fi
149
Junio C Hamano4056afb2012-02-22 21:55:06 -0800150 cmd="$cmd git apply --cached $git_apply_opt"' <"$dotest/patch"'
151 if eval "$cmd"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700152 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700153 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
154 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -0700155 else
Ævar Arnfjörð Bjarmasona424ca12011-05-21 18:43:48 +0000156 cannot_fallback "$(gettext "Did you hand edit your patch?
157It does not apply to blobs recorded in its index.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700158 fi
159
160 test -f "$dotest/patch-merge-index" &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700161 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700162 orig_tree=$(cat "$dotest/patch-merge-base") &&
163 rm -fr "$dotest"/patch-merge-* || exit 1
164
Ævar Arnfjörð Bjarmason865207a2011-05-21 18:43:51 +0000165 say "$(gettext "Falling back to patching base and 3-way merge...")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700166
167 # This is not so wrong. Depending on which base we picked,
168 # orig_tree may be wildly different from ours, but his_tree
169 # has the same set of wildly different changes in parts the
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500170 # patch did not touch, so recursive ends up canceling them,
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700171 # saying that we reverted all those changes.
172
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700173 eval GITHEAD_$his_tree='"$FIRSTLINE"'
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500174 export GITHEAD_$his_tree
Stephen Boyd0e987a12009-06-16 15:33:01 -0700175 if test -n "$GIT_QUIET"
176 then
Junio C Hamano69ae92b2010-10-13 11:36:36 -0700177 GIT_MERGE_VERBOSITY=0 && export GIT_MERGE_VERBOSITY
Stephen Boyd0e987a12009-06-16 15:33:01 -0700178 fi
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500179 git-merge-recursive $orig_tree -- HEAD $his_tree || {
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800180 git rerere $allow_rerere_autoupdate
Jiang Xin42e65042012-07-25 22:53:11 +0800181 die "$(gettext "Failed to merge in the changes.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700182 }
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500183 unset GITHEAD_$his_tree
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700184}
185
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200186clean_abort () {
187 test $# = 0 || echo >&2 "$@"
188 rm -fr "$dotest"
189 exit 1
190}
191
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200192patch_format=
193
194check_patch_format () {
195 # early return if patch_format was set from the command line
196 if test -n "$patch_format"
197 then
198 return 0
199 fi
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200200
201 # we default to mbox format if input is from stdin and for
202 # directories
203 if test $# = 0 || test "x$1" = "x-" || test -d "$1"
204 then
205 patch_format=mbox
206 return 0
207 fi
208
David Barr0e8341f2011-08-08 04:49:04 +0200209 # otherwise, check the first few non-blank lines of the first
210 # patch to try to detect its format
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200211 {
David Barr0e8341f2011-08-08 04:49:04 +0200212 # Start from first line containing non-whitespace
213 l1=
214 while test -z "$l1"
215 do
Jim Meyeringf0c57932012-02-25 18:34:26 +0100216 read l1 || break
David Barr0e8341f2011-08-08 04:49:04 +0200217 done
Giuseppe Bilotta15ced752009-05-27 23:20:12 +0200218 read l2
219 read l3
220 case "$l1" in
221 "From "* | "From: "*)
222 patch_format=mbox
223 ;;
224 '# This series applies on GIT commit'*)
225 patch_format=stgit-series
226 ;;
227 "# HG changeset patch")
228 patch_format=hg
229 ;;
230 *)
231 # if the second line is empty and the third is
232 # a From, Author or Date entry, this is very
233 # likely an StGIT patch
234 case "$l2,$l3" in
235 ,"From: "* | ,"Author: "* | ,"Date: "*)
236 patch_format=stgit
237 ;;
238 *)
239 ;;
240 esac
241 ;;
242 esac
Junio C Hamano0fcb2ca2009-08-06 20:08:12 -0500243 if test -z "$patch_format" &&
244 test -n "$l1" &&
245 test -n "$l2" &&
246 test -n "$l3"
247 then
248 # This begins with three non-empty lines. Is this a
249 # piece of e-mail a-la RFC2822? Grab all the headers,
250 # discarding the indented remainder of folded lines,
251 # and see if it looks like that they all begin with the
252 # header field names...
Stephen Boyde3f67d32010-01-25 16:33:59 -0800253 tr -d '\015' <"$1" |
254 sed -n -e '/^$/q' -e '/^[ ]/d' -e p |
Junio C Hamanoe1622bf2009-11-23 15:56:32 -0800255 sane_egrep -v '^[!-9;-~]+:' >/dev/null ||
Junio C Hamano0fcb2ca2009-08-06 20:08:12 -0500256 patch_format=mbox
257 fi
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200258 } < "$1" || clean_abort
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200259}
260
261split_patches () {
262 case "$patch_format" in
263 mbox)
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700264 if test t = "$keepcr"
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100265 then
266 keep_cr=--keep-cr
267 else
268 keep_cr=
269 fi
Junio C Hamanoc2ca1d72009-08-04 22:31:59 -0500270 git mailsplit -d"$prec" -o"$dotest" -b $keep_cr -- "$@" > "$dotest/last" ||
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200271 clean_abort
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200272 ;;
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200273 stgit-series)
274 if test $# -ne 1
275 then
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000276 clean_abort "$(gettext "Only one StGIT patch series can be applied at once")"
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200277 fi
Elia Pinto75ee3d72014-03-25 10:22:22 -0700278 series_dir=$(dirname "$1")
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200279 series_file="$1"
280 shift
281 {
282 set x
283 while read filename
284 do
285 set "$@" "$series_dir/$filename"
286 done
287 # remove the safety x
288 shift
289 # remove the arg coming from the first-line comment
290 shift
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200291 } < "$series_file" || clean_abort
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200292 # set the patch format appropriately
293 patch_format=stgit
294 # now handle the actual StGIT patches
295 split_patches "$@"
296 ;;
297 stgit)
298 this=0
299 for stgit in "$@"
300 do
Elia Pinto75ee3d72014-03-25 10:22:22 -0700301 this=$(expr "$this" + 1)
302 msgnum=$(printf "%0${prec}d" $this)
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200303 # Perl version of StGIT parse_patch. The first nonemptyline
304 # not starting with Author, From or Date is the
305 # subject, and the body starts with the next nonempty
306 # line not starting with Author, From or Date
Jeff Kingfcb06a82013-10-28 21:19:59 -0400307 @@PERL@@ -ne 'BEGIN { $subject = 0 }
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200308 if ($subject > 1) { print ; }
309 elsif (/^\s+$/) { next ; }
Giuseppe Bilotta45d51dc2011-08-29 18:44:07 +0200310 elsif (/^Author:/) { s/Author/From/ ; print ;}
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200311 elsif (/^(From|Date)/) { print ; }
312 elsif ($subject) {
313 $subject = 2 ;
314 print "\n" ;
315 print ;
316 } else {
317 print "Subject: ", $_ ;
318 $subject = 1;
319 }
Giuseppe Bilotta0cd29a02009-05-27 11:25:19 +0200320 ' < "$stgit" > "$dotest/$msgnum" || clean_abort
Giuseppe Bilottac574e682009-05-27 11:25:18 +0200321 done
322 echo "$this" > "$dotest/last"
323 this=
324 msgnum=
325 ;;
Giuseppe Bilotta0cfd1122011-08-29 18:44:06 +0200326 hg)
327 this=0
328 for hg in "$@"
329 do
330 this=$(( $this + 1 ))
331 msgnum=$(printf "%0${prec}d" $this)
332 # hg stores changeset metadata in #-commented lines preceding
333 # the commit message and diff(s). The only metadata we care about
334 # are the User and Date (Node ID and Parent are hashes which are
335 # only relevant to the hg repository and thus not useful to us)
336 # Since we cannot guarantee that the commit message is in
337 # git-friendly format, we put no Subject: line and just consume
338 # all of the message as the body
Jeff Kingfcb06a82013-10-28 21:19:59 -0400339 LANG=C LC_ALL=C @@PERL@@ -M'POSIX qw(strftime)' -ne 'BEGIN { $subject = 0 }
Giuseppe Bilotta0cfd1122011-08-29 18:44:06 +0200340 if ($subject) { print ; }
341 elsif (/^\# User /) { s/\# User/From:/ ; print ; }
342 elsif (/^\# Date /) {
343 my ($hashsign, $str, $time, $tz) = split ;
344 $tz = sprintf "%+05d", (0-$tz)/36;
345 print "Date: " .
346 strftime("%a, %d %b %Y %H:%M:%S ",
347 localtime($time))
348 . "$tz\n";
349 } elsif (/^\# /) { next ; }
350 else {
351 print "\n", $_ ;
352 $subject = 1;
353 }
354 ' <"$hg" >"$dotest/$msgnum" || clean_abort
355 done
356 echo "$this" >"$dotest/last"
357 this=
358 msgnum=
359 ;;
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200360 *)
Giuseppe Bilottadff4b0e2011-08-29 17:22:06 +0200361 if test -n "$patch_format"
362 then
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000363 clean_abort "$(eval_gettext "Patch format \$patch_format is not supported.")"
Nicolas Sebrecht46caf502009-08-06 20:08:13 -0500364 else
Ævar Arnfjörð Bjarmasond62a1462011-05-21 18:43:49 +0000365 clean_abort "$(gettext "Patch format detection failed.")"
Nicolas Sebrecht46caf502009-08-06 20:08:13 -0500366 fi
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200367 ;;
368 esac
369}
370
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700371prec=4
Johannes Schindelin51ef1da2008-07-21 12:51:02 +0200372dotest="$GIT_DIR/rebase-apply"
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100373sign= utf8=t keep= keepcr= skip= interactive= resolved= rebasing= abort=
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100374resolvemsg= resume= scissors= no_inbody_headers=
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200375git_apply_opt=
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800376committer_date_is_author_date=
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900377ignore_date=
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800378allow_rerere_autoupdate=
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +0000379gpg_sign_opt=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700380
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +0100381if test "$(git config --bool --get am.keepcr)" = true
382then
383 keepcr=t
384fi
385
David Kastrup822f7c72007-09-23 22:42:08 +0200386while test $# != 0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700387do
388 case "$1" in
Pierre Habouzit78443d92007-11-04 11:30:57 +0100389 -i|--interactive)
390 interactive=t ;;
391 -b|--binary)
Jiang Xin42e65042012-07-25 22:53:11 +0800392 gettextln >&2 "The -b/--binary option has been a no-op for long time, and
393it will be removed. Please do not use it anymore."
Thomas Rast4eeb1de2012-03-12 22:47:19 +0100394 ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100395 -3|--3way)
396 threeway=t ;;
Johannes Sixtdfdd7e62007-11-06 21:33:58 +0100397 -s|--signoff)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100398 sign=t ;;
399 -u|--utf8)
400 utf8=t ;; # this is now default
401 --no-utf8)
402 utf8= ;;
403 -k|--keep)
404 keep=t ;;
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100405 --keep-non-patch)
406 keep=b ;;
Junio C Hamano017678b2009-08-26 21:36:05 -0700407 -c|--scissors)
408 scissors=t ;;
409 --no-scissors)
410 scissors=f ;;
Jeff Kingc8089af2010-02-11 17:27:14 -0500411 -r|--resolved|--continue)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100412 resolved=t ;;
413 --skip)
414 skip=t ;;
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900415 --abort)
416 abort=t ;;
Junio C Hamano3041c322008-03-04 00:25:06 -0800417 --rebasing)
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700418 rebasing=t threeway=t ;;
Nicolas Vigier88336622014-02-01 02:18:00 +0000419 --resolvemsg=*)
420 resolvemsg="${1#--resolvemsg=}" ;;
421 --whitespace=*|--directory=*|--exclude=*|--include=*)
422 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
423 -C*|-p*)
424 git_apply_opt="$git_apply_opt $(sq "$1")" ;;
425 --patch-format=*)
426 patch_format="${1#--patch-format=}" ;;
Giuseppe Bilotta86c91f92009-08-04 13:16:49 +0200427 --reject|--ignore-whitespace|--ignore-space-change)
martin f. krafftb80da422009-01-23 11:31:21 +1100428 git_apply_opt="$git_apply_opt $1" ;;
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800429 --committer-date-is-author-date)
430 committer_date_is_author_date=t ;;
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900431 --ignore-date)
432 ignore_date=t ;;
Junio C Hamanocb6020b2009-12-04 00:20:48 -0800433 --rerere-autoupdate|--no-rerere-autoupdate)
434 allow_rerere_autoupdate="$1" ;;
Stephen Boyd0e987a12009-06-16 15:33:01 -0700435 -q|--quiet)
436 GIT_QUIET=t ;;
Stefan-W. Hahnad2c9282010-02-27 15:20:26 +0100437 --keep-cr)
438 keepcr=t ;;
Stefan-W. Hahne80d4cb2010-02-27 15:20:27 +0100439 --no-keep-cr)
440 keepcr=f ;;
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +0000441 --gpg-sign)
442 gpg_sign_opt=-S ;;
443 --gpg-sign=*)
444 gpg_sign_opt="-S${1#--gpg-sign=}" ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700445 --)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100446 shift; break ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700447 *)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100448 usage ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700449 esac
Pierre Habouzit78443d92007-11-04 11:30:57 +0100450 shift
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700451done
452
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800453# If the dotest directory exists, but we have finished applying all the
454# patches in them, clear it out.
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700455if test -d "$dotest" &&
Ramkumar Ramachandrac30754f2013-05-12 17:26:35 +0530456 test -f "$dotest/last" &&
457 test -f "$dotest/next" &&
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700458 last=$(cat "$dotest/last") &&
459 next=$(cat "$dotest/next") &&
460 test $# != 0 &&
461 test "$next" -gt "$last"
462then
463 rm -fr "$dotest"
464fi
465
Ramkumar Ramachandrac30754f2013-05-12 17:26:35 +0530466if test -d "$dotest" && test -f "$dotest/last" && test -f "$dotest/next"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700467then
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900468 case "$#,$skip$resolved$abort" in
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700469 0,*t*)
470 # Explicit resume command and we do not have file, so
471 # we are happy.
472 : ;;
473 0,)
474 # No file input but without resume parameters; catch
475 # user error to feed us a patch from standard input
Junio C Hamanoe72c7402008-03-04 00:25:05 -0800476 # when there is already $dotest. This is somewhat
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700477 # unreliable -- stdin could be /dev/null for example
478 # and the caller did not intend to feed us a patch but
479 # wanted to continue unattended.
Jay Soffian98ef23b2009-01-28 10:03:10 -0500480 test -t 0
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700481 ;;
482 *)
483 false
484 ;;
485 esac ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000486 die "$(eval_gettext "previous rebase directory \$dotest still exists but mbox given.")"
Junio C Hamano271440e2005-10-25 23:35:37 -0700487 resume=yes
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900488
Olivier Marin95f8ebb2008-07-21 15:39:06 +0200489 case "$skip,$abort" in
Junio C Hamano2d56a132009-02-26 11:24:29 -0800490 t,t)
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000491 die "$(gettext "Please make up your mind. --skip or --abort?")"
Junio C Hamano2d56a132009-02-26 11:24:29 -0800492 ;;
Olivier Marin95f8ebb2008-07-21 15:39:06 +0200493 t,)
494 git rerere clear
495 git read-tree --reset -u HEAD HEAD
496 orig_head=$(cat "$GIT_DIR/ORIG_HEAD")
497 git reset HEAD
498 git update-ref ORIG_HEAD $orig_head
499 ;;
500 ,t)
Junio C Hamano2d56a132009-02-26 11:24:29 -0800501 if test -f "$dotest/rebasing"
502 then
503 exec git rebase --abort
504 fi
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900505 git rerere clear
Junio C Hamano7b3b7e32010-12-21 10:35:53 -0800506 if safe_to_abort
507 then
Michael J Gruberc7671842009-02-26 10:52:53 +0100508 git read-tree --reset -u HEAD ORIG_HEAD
509 git reset ORIG_HEAD
Junio C Hamano7b3b7e32010-12-21 10:35:53 -0800510 fi
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900511 rm -fr "$dotest"
512 exit ;;
513 esac
Michael J Gruberc7671842009-02-26 10:52:53 +0100514 rm -f "$dotest/dirtyindex"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700515else
Ramkumar Ramachandrab141f3c2013-06-15 18:13:11 +0530516 # Possible stray $dotest directory in the independent-run
517 # case; in the --rebasing case, it is upto the caller
518 # (git-rebase--am) to take care of stray directories.
519 if test -d "$dotest" && test -z "$rebasing"
520 then
521 case "$skip,$resolved,$abort" in
522 ,,t)
523 rm -fr "$dotest"
524 exit 0
525 ;;
526 *)
527 die "$(eval_gettext "Stray \$dotest directory found.
528Use \"git am --abort\" to remove it.")"
529 ;;
530 esac
531 fi
532
Justin Lebar01689902014-03-31 15:11:46 -0700533 # Make sure we are not given --skip, --continue, or --abort
Nanako Shiraishi3e5057a2008-07-16 19:39:10 +0900534 test "$skip$resolved$abort" = "" ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000535 die "$(gettext "Resolve operation not in progress, we are not resuming.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700536
537 # Start afresh.
538 mkdir -p "$dotest" || exit
539
Junio C Hamanobb034f82008-03-04 00:25:04 -0800540 if test -n "$prefix" && test $# != 0
541 then
542 first=t
543 for arg
544 do
545 test -n "$first" && {
546 set x
547 first=
548 }
Pat Thoyts5e9677c2010-09-30 14:24:07 +0100549 if is_absolute_path "$arg"
550 then
551 set "$@" "$arg"
552 else
553 set "$@" "$prefix$arg"
554 fi
Junio C Hamanobb034f82008-03-04 00:25:04 -0800555 done
556 shift
557 fi
Giuseppe Bilottaa5a67552009-05-27 11:25:16 +0200558
559 check_patch_format "$@"
560
561 split_patches "$@"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700562
Junio C Hamano017678b2009-08-26 21:36:05 -0700563 # -i can and must be given when resuming; everything
564 # else is kept
Junio C Hamano9b9f5a22008-12-05 11:19:43 -0800565 echo " $git_apply_opt" >"$dotest/apply-opt"
Junio C Hamano22db2402008-12-04 15:38:27 -0800566 echo "$threeway" >"$dotest/threeway"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700567 echo "$sign" >"$dotest/sign"
568 echo "$utf8" >"$dotest/utf8"
569 echo "$keep" >"$dotest/keep"
Junio C Hamano017678b2009-08-26 21:36:05 -0700570 echo "$scissors" >"$dotest/scissors"
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100571 echo "$no_inbody_headers" >"$dotest/no_inbody_headers"
Stephen Boyd0e987a12009-06-16 15:33:01 -0700572 echo "$GIT_QUIET" >"$dotest/quiet"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700573 echo 1 >"$dotest/next"
Junio C Hamano3041c322008-03-04 00:25:06 -0800574 if test -n "$rebasing"
575 then
576 : >"$dotest/rebasing"
577 else
578 : >"$dotest/applying"
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900579 if test -n "$HAS_HEAD"
580 then
581 git update-ref ORIG_HEAD HEAD
582 else
583 git update-ref -d ORIG_HEAD >/dev/null 2>&1
584 fi
Junio C Hamano3041c322008-03-04 00:25:06 -0800585 fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700586fi
587
Jeff King2a6f08a2011-08-15 17:13:07 -0700588git update-index -q --refresh
589
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800590case "$resolved" in
591'')
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900592 case "$HAS_HEAD" in
593 '')
594 files=$(git ls-files) ;;
595 ?*)
596 files=$(git diff-index --cached --name-only HEAD --) ;;
597 esac || exit
Michael J Gruberc7671842009-02-26 10:52:53 +0100598 if test "$files"
599 then
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900600 test -n "$HAS_HEAD" && : >"$dotest/dirtyindex"
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000601 die "$(eval_gettext "Dirty index: cannot apply patches (dirty: \$files)")"
Michael J Gruberc7671842009-02-26 10:52:53 +0100602 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800603esac
604
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100605# Now, decide what command line options we will give to the git
606# commands we invoke, based on the result of parsing command line
607# options and previous invocation state stored in $dotest/ files.
608
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700609if test "$(cat "$dotest/utf8")" = t
610then
611 utf8=-u
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800612else
613 utf8=-n
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700614fi
Thomas Rastf7e5ea12012-01-16 11:53:00 +0100615keep=$(cat "$dotest/keep")
616case "$keep" in
617t)
618 keep=-k ;;
619b)
620 keep=-b ;;
621*)
622 keep= ;;
623esac
Junio C Hamano017678b2009-08-26 21:36:05 -0700624case "$(cat "$dotest/scissors")" in
625t)
626 scissors=--scissors ;;
627f)
628 scissors=--no-scissors ;;
629esac
Lukas Sandströmd25e5152009-11-20 17:12:47 +0100630if test "$(cat "$dotest/no_inbody_headers")" = t
631then
632 no_inbody_headers=--no-inbody-headers
633else
634 no_inbody_headers=
635fi
Stephen Boyd0e987a12009-06-16 15:33:01 -0700636if test "$(cat "$dotest/quiet")" = t
637then
638 GIT_QUIET=t
639fi
Junio C Hamano22db2402008-12-04 15:38:27 -0800640if test "$(cat "$dotest/threeway")" = t
641then
642 threeway=t
643fi
Junio C Hamano9b9f5a22008-12-05 11:19:43 -0800644git_apply_opt=$(cat "$dotest/apply-opt")
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700645if test "$(cat "$dotest/sign")" = t
646then
Elia Pinto75ee3d72014-03-25 10:22:22 -0700647 SIGNOFF=$(git var GIT_COMMITTER_IDENT | sed -e '
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700648 s/>.*/>/
649 s/^/Signed-off-by: /'
Elia Pinto75ee3d72014-03-25 10:22:22 -0700650 )
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700651else
652 SIGNOFF=
653fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700654
Elia Pinto75ee3d72014-03-25 10:22:22 -0700655last=$(cat "$dotest/last")
656this=$(cat "$dotest/next")
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700657if test "$skip" = t
658then
Elia Pinto75ee3d72014-03-25 10:22:22 -0700659 this=$(expr "$this" + 1)
Jan Harkes69224712005-12-17 01:01:06 -0500660 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700661fi
662
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700663while test "$this" -le "$last"
664do
Elia Pinto75ee3d72014-03-25 10:22:22 -0700665 msgnum=$(printf "%0${prec}d" $this)
666 next=$(expr "$this" + 1)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700667 test -f "$dotest/$msgnum" || {
Jan Harkes69224712005-12-17 01:01:06 -0500668 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700669 go_next
670 continue
671 }
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800672
673 # If we are not resuming, parse and extract the patch information
674 # into separate files:
675 # - info records the authorship and title
676 # - msg is the rest of commit log message
677 # - patch is the patch body.
678 #
679 # When we are resuming, these files are either already prepared
Kevin Bracey8ceb6fb2013-06-26 23:06:41 +0300680 # by the user, or the user can tell us to do so by --continue flag.
Junio C Hamano271440e2005-10-25 23:35:37 -0700681 case "$resume" in
682 '')
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700683 if test -f "$dotest/rebasing"
684 then
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700685 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
686 -e q "$dotest/$msgnum") &&
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700687 test "$(git cat-file -t "$commit")" = commit ||
688 stop_here $this
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700689 git cat-file commit "$commit" |
690 sed -e '1,/^$/d' >"$dotest/msg-clean"
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700691 echo "$commit" >"$dotest/original-commit"
692 get_author_ident_from_commit "$commit" >"$dotest/author-script"
Junio C Hamano4ae6d462013-01-31 19:26:21 -0800693 git diff-tree --root --binary --full-index "$commit" >"$dotest/patch"
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700694 else
Martin von Zweigbergk0fbb95d2012-06-26 07:51:57 -0700695 git mailinfo $keep $no_inbody_headers $scissors $utf8 "$dotest/msg" "$dotest/patch" \
696 <"$dotest/$msgnum" >"$dotest/info" ||
697 stop_here $this
698
699 # skip pine's internal folder data
700 sane_grep '^Author: Mail System Internal Data$' \
701 <"$dotest"/info >/dev/null &&
702 go_next && continue
703
704 test -s "$dotest/patch" || {
705 eval_gettextln "Patch is empty. Was it split wrong?
706If you would prefer to skip this patch, instead run \"\$cmdline --skip\".
707To restore the original branch and stop patching run \"\$cmdline --abort\"."
708 stop_here $this
709 }
710 rm -f "$dotest/original-commit" "$dotest/author-script"
Junio C Hamanoc970a6f2009-11-27 15:06:37 -0800711 {
712 sed -n '/^Subject/ s/Subject: //p' "$dotest/info"
713 echo
714 cat "$dotest/msg"
715 } |
716 git stripspace > "$dotest/msg-clean"
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700717 fi
Junio C Hamano271440e2005-10-25 23:35:37 -0700718 ;;
719 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700720
Jay Soffian43c23252010-06-16 03:12:40 -0400721 if test -f "$dotest/author-script"
722 then
723 eval $(cat "$dotest/author-script")
724 else
725 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
726 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
727 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
728 fi
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800729
Junio C Hamano3b789592007-12-05 13:16:35 -0800730 if test -z "$GIT_AUTHOR_EMAIL"
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800731 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000732 gettextln "Patch does not have a valid e-mail address."
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800733 stop_here $this
734 fi
735
Junio C Hamano2c674192005-10-20 22:31:56 -0700736 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700737
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800738 case "$resume" in
739 '')
740 if test '' != "$SIGNOFF"
741 then
Elia Pinto75ee3d72014-03-25 10:22:22 -0700742 LAST_SIGNED_OFF_BY=$(
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800743 sed -ne '/^Signed-off-by: /p' \
744 "$dotest/msg-clean" |
Jeff Kingb4ce54f2008-03-12 17:34:34 -0400745 sed -ne '$p'
Elia Pinto75ee3d72014-03-25 10:22:22 -0700746 )
747 ADD_SIGNOFF=$(
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800748 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700749 test '' = "$LAST_SIGNED_OFF_BY" && echo
750 echo "$SIGNOFF"
Elia Pinto75ee3d72014-03-25 10:22:22 -0700751 })
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800752 else
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700753 ADD_SIGNOFF=
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800754 fi
755 {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700756 if test -s "$dotest/msg-clean"
757 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700758 cat "$dotest/msg-clean"
759 fi
760 if test '' != "$ADD_SIGNOFF"
761 then
762 echo "$ADD_SIGNOFF"
763 fi
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800764 } >"$dotest/final-commit"
765 ;;
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800766 *)
Junio C Hamano6d286442006-02-23 22:14:47 -0800767 case "$resolved$interactive" in
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800768 tt)
769 # This is used only for interactive view option.
Junio C Hamano38762c42007-11-28 16:15:04 -0800770 git diff-index -p --cached HEAD -- >"$dotest/patch"
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800771 ;;
772 esac
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800773 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700774
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800775 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700776 if test "$interactive" = t
777 then
Junio C Hamanoa1451102005-10-12 18:31:41 -0700778 test -t 0 ||
Ævar Arnfjörð Bjarmason79087252011-05-21 18:43:47 +0000779 die "$(gettext "cannot be interactive without stdin connected to a terminal.")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700780 action=again
781 while test "$action" = again
782 do
Jon Seymourde88c1c2011-08-07 21:58:14 +1000783 gettextln "Commit Body is:"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700784 echo "--------------------------"
785 cat "$dotest/final-commit"
786 echo "--------------------------"
Ævar Arnfjörð Bjarmason7a74d042011-05-21 18:43:50 +0000787 # TRANSLATORS: Make sure to include [y], [n], [e], [v] and [a]
788 # in your translation. The program will only accept English
789 # input at this point.
790 gettext "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700791 read reply
792 case "$reply" in
Junio C Hamanof89ad672005-10-25 23:43:59 -0700793 [yY]*) action=yes ;;
794 [aA]*) action=yes interactive= ;;
795 [nN]*) action=skip ;;
Adam Robenef0c2ab2007-07-19 22:09:35 -0700796 [eE]*) git_editor "$dotest/final-commit"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700797 action=again ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700798 [vV]*) action=again
Jonathan Niederf6dff112010-02-14 23:04:13 -0600799 git_pager "$dotest/patch" ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700800 *) action=again ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700801 esac
802 done
803 else
804 action=yes
805 fi
Ramkumar Ramachandra92f65e62010-06-02 10:33:37 +0200806
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700807 if test $action = skip
808 then
809 go_next
810 continue
811 fi
812
813 if test -x "$GIT_DIR"/hooks/applypatch-msg
814 then
815 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
816 stop_here $this
817 fi
818
Simon Ruderichb34a9122013-03-21 03:40:17 +0100819 if test -f "$dotest/final-commit"
820 then
821 FIRSTLINE=$(sed 1q "$dotest/final-commit")
822 else
823 FIRSTLINE=""
824 fi
825
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000826 say "$(eval_gettext "Applying: \$FIRSTLINE")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700827
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800828 case "$resolved" in
829 '')
Stephen Boyd3ddd1702009-06-16 15:32:58 -0700830 # When we are allowed to fall back to 3-way later, don't give
831 # false errors during the initial attempt.
832 squelch=
833 if test "$threeway" = t
834 then
835 squelch='>/dev/null 2>&1 '
836 fi
837 eval "git apply $squelch$git_apply_opt"' --index "$dotest/patch"'
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800838 apply_status=$?
839 ;;
840 t)
841 # Resolved means the user did all the hard work, and
842 # we do not have to do any patch application. Just
843 # trust what the user has in the index file and the
844 # working tree.
845 resolved=
Junio C Hamano38762c42007-11-28 16:15:04 -0800846 git diff-index --quiet --cached HEAD -- && {
Jon Seymourde88c1c2011-08-07 21:58:14 +1000847 gettextln "No changes - did you forget to use 'git add'?
Ævar Arnfjörð Bjarmasonbd8643a2011-05-21 18:43:44 +0000848If there is nothing left to stage, chances are that something else
Jon Seymourde88c1c2011-08-07 21:58:14 +1000849already introduced the same changes; you might want to skip this patch."
Robert Shearmanced94562006-05-02 13:32:43 +0100850 stop_here_user_resolve $this
Alex Riesen06aff472007-03-25 01:56:13 +0100851 }
Junio C Hamano5be60072007-07-02 22:52:14 -0700852 unmerged=$(git ls-files -u)
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700853 if test -n "$unmerged"
854 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000855 gettextln "You still have unmerged paths in your index
856did you forget to use 'git add'?"
Robert Shearmanced94562006-05-02 13:32:43 +0100857 stop_here_user_resolve $this
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700858 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800859 apply_status=0
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100860 git rerere
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800861 ;;
862 esac
863
Junio C Hamano0ba17dd2010-04-09 16:58:28 -0700864 if test $apply_status != 0 && test "$threeway" = t
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700865 then
Junio C Hamano73319032005-10-13 11:46:43 -0700866 if (fall_back_3way)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700867 then
Junio C Hamano73319032005-10-13 11:46:43 -0700868 # Applying the patch to an earlier tree and merging the
869 # result may have produced the same tree as ours.
Junio C Hamano38762c42007-11-28 16:15:04 -0800870 git diff-index --quiet --cached HEAD -- && {
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000871 say "$(gettext "No changes -- Patch already applied.")"
Alex Riesen06aff472007-03-25 01:56:13 +0100872 go_next
873 continue
874 }
Junio C Hamano73319032005-10-13 11:46:43 -0700875 # clear apply_status -- we have successfully merged.
876 apply_status=0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700877 fi
878 fi
879 if test $apply_status != 0
880 then
Jon Seymourde88c1c2011-08-07 21:58:14 +1000881 eval_gettextln 'Patch failed at $msgnum $FIRSTLINE'
Paul Gortmaker14bf2d52012-07-13 11:51:30 -0400882 if test "$(git config --bool advice.amworkdir)" != false
883 then
Nguyễn Thái Ngọc Duya312a272012-08-22 21:48:03 +0700884 eval_gettextln 'The copy of the patch that failed is found in:
885 $dotest/patch'
Paul Gortmaker14bf2d52012-07-13 11:51:30 -0400886 fi
Robert Shearmanced94562006-05-02 13:32:43 +0100887 stop_here_user_resolve $this
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700888 fi
889
890 if test -x "$GIT_DIR"/hooks/pre-applypatch
891 then
892 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
893 fi
894
Junio C Hamano5be60072007-07-02 22:52:14 -0700895 tree=$(git write-tree) &&
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800896 commit=$(
Nanako Shiraishia79ec622009-01-24 10:18:02 +0900897 if test -n "$ignore_date"
898 then
899 GIT_AUTHOR_DATE=
900 fi
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900901 parent=$(git rev-parse --verify -q HEAD) ||
Ævar Arnfjörð Bjarmasondff1a982011-05-21 18:43:52 +0000902 say >&2 "$(gettext "applying to an empty history")"
Nanako Shiraishif79d4c82009-04-10 09:34:42 +0900903
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800904 if test -n "$committer_date_is_author_date"
905 then
906 GIT_COMMITTER_DATE="$GIT_AUTHOR_DATE"
907 export GIT_COMMITTER_DATE
908 fi &&
Nicolas Vigier3b4e395f2014-02-01 02:18:01 +0000909 git commit-tree ${parent:+-p} $parent ${gpg_sign_opt:+"$gpg_sign_opt"} $tree \
910 <"$dotest/final-commit"
Junio C Hamano3f01ad62009-01-22 16:14:58 -0800911 ) &&
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700912 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700913 stop_here $this
914
Thomas Rast96e19482010-03-12 18:04:29 +0100915 if test -f "$dotest/original-commit"; then
916 echo "$(cat "$dotest/original-commit") $commit" >> "$dotest/rewritten"
917 fi
918
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700919 if test -x "$GIT_DIR"/hooks/post-applypatch
920 then
921 "$GIT_DIR"/hooks/post-applypatch
922 fi
923
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700924 go_next
925done
926
Thomas Rasteb2151b2010-03-12 18:04:33 +0100927if test -s "$dotest"/rewritten; then
928 git notes copy --for-rewrite=rebase < "$dotest"/rewritten
929 if test -x "$GIT_DIR"/hooks/post-rewrite; then
Thomas Rast96e19482010-03-12 18:04:29 +0100930 "$GIT_DIR"/hooks/post-rewrite rebase < "$dotest"/rewritten
Thomas Rasteb2151b2010-03-12 18:04:33 +0100931 fi
Thomas Rast96e19482010-03-12 18:04:29 +0100932fi
933
Ramkumar Ramachandraa1549e12013-05-12 17:26:38 +0530934# If am was called with --rebasing (from git-rebase--am), it's up to
935# the caller to take care of housekeeping.
936if ! test -f "$dotest/rebasing"
937then
938 rm -fr "$dotest"
939 git gc --auto
940fi