blob: 75886a8f2fb0c2244ba921398ae2339dc9ffea26 [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="\
8git-am [options] <mbox>|<Maildir>...
9git-am [options] --resolved
10git-am [options] --skip
11--
Junio C Hamanoe72c7402008-03-04 00:25:05 -080012d,dotest= (removed -- do not use)
Junio C Hamano66006c62007-11-08 23:04:31 -080013i,interactive run interactively
Pierre Habouzit78443d92007-11-04 11:30:57 +010014b,binary pass --allo-binary-replacement to git-apply
153,3way allow fall back on 3way merging if needed
16s,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
Pierre Habouzit78443d92007-11-04 11:30:57 +010019whitespace= pass it through git-apply
20C= pass it through git-apply
21p= pass it through git-apply
22resolvemsg= override error message when patch failure occurs
23r,resolved to be used after a patch failure
Junio C Hamano3041c322008-03-04 00:25:06 -080024skip skip the current patch
25rebasing (internal use for git-rebase)"
Pierre Habouzit78443d92007-11-04 11:30:57 +010026
freku045@student.liu.secf1fe882005-12-13 23:30:31 +010027. git-sh-setup
Junio C Hamanobb034f82008-03-04 00:25:04 -080028prefix=$(git rev-parse --show-prefix)
Shawn O. Pearcef9474132006-12-28 02:34:48 -050029set_reflog_action am
Shawn O. Pearce7eff28a2006-12-30 23:32:38 -050030require_work_tree
Jeff Kingc1491842008-03-01 01:22:55 -050031cd_to_toplevel
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070032
Junio C Hamanod64e6b02006-02-18 20:51:26 -080033git var GIT_COMMITTER_IDENT >/dev/null || exit
34
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070035stop_here () {
36 echo "$1" >"$dotest/next"
37 exit 1
38}
39
Robert Shearmanced94562006-05-02 13:32:43 +010040stop_here_user_resolve () {
Seancc120052006-05-13 23:34:08 -040041 if [ -n "$resolvemsg" ]; then
Jeff Kinga23bfae2007-05-26 00:33:03 -070042 printf '%s\n' "$resolvemsg"
Seancc120052006-05-13 23:34:08 -040043 stop_here $1
44 fi
Robert Shearmanced94562006-05-02 13:32:43 +010045 cmdline=$(basename $0)
46 if test '' != "$interactive"
47 then
48 cmdline="$cmdline -i"
49 fi
50 if test '' != "$threeway"
51 then
52 cmdline="$cmdline -3"
53 fi
Robert Shearmanced94562006-05-02 13:32:43 +010054 echo "When you have resolved this problem run \"$cmdline --resolved\"."
55 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
56
57 stop_here $1
58}
59
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070060go_next () {
61 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
62 "$dotest/patch" "$dotest/info"
63 echo "$next" >"$dotest/next"
64 this=$next
65}
66
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -070067cannot_fallback () {
68 echo "$1"
69 echo "Cannot fall back to three-way merge."
70 exit 1
71}
72
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070073fall_back_3way () {
74 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
75
76 rm -fr "$dotest"/patch-merge-*
77 mkdir "$dotest/patch-merge-tmp-dir"
78
79 # First see if the patch records the index info that we can use.
Johannes Schindelin7a988692007-09-17 23:34:06 +010080 git apply --build-fake-ancestor "$dotest/patch-merge-tmp-index" \
81 "$dotest/patch" &&
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -070082 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
Junio C Hamano5be60072007-07-02 22:52:14 -070083 git write-tree >"$dotest/patch-merge-base+" ||
Junio C Hamanob1440cc2007-02-24 01:05:27 -080084 cannot_fallback "Repository lacks necessary blobs to fall back on 3-way merge."
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -070085
86 echo Using index info to reconstruct a base tree...
87 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
Junio C Hamano5be60072007-07-02 22:52:14 -070088 git apply $binary --cached <"$dotest/patch"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070089 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070090 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
91 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -070092 else
93 cannot_fallback "Did you hand edit your patch?
94It does not apply to blobs recorded in its index."
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070095 fi
96
97 test -f "$dotest/patch-merge-index" &&
Junio C Hamano5be60072007-07-02 22:52:14 -070098 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git write-tree) &&
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070099 orig_tree=$(cat "$dotest/patch-merge-base") &&
100 rm -fr "$dotest"/patch-merge-* || exit 1
101
102 echo Falling back to patching base and 3-way merge...
103
104 # This is not so wrong. Depending on which base we picked,
105 # orig_tree may be wildly different from ours, but his_tree
106 # has the same set of wildly different changes in parts the
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500107 # patch did not touch, so recursive ends up canceling them,
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700108 # saying that we reverted all those changes.
109
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700110 eval GITHEAD_$his_tree='"$FIRSTLINE"'
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500111 export GITHEAD_$his_tree
112 git-merge-recursive $orig_tree -- HEAD $his_tree || {
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100113 git rerere
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700114 echo Failed to merge in the changes.
115 exit 1
116 }
Shawn O. Pearce579c9bb2006-12-28 02:35:27 -0500117 unset GITHEAD_$his_tree
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700118}
119
120prec=4
Junio C Hamanoe72c7402008-03-04 00:25:05 -0800121dotest=".dotest"
Junio C Hamano3041c322008-03-04 00:25:06 -0800122sign= utf8=t keep= skip= interactive= resolved= binary= rebasing=
Gerrit Pape3f0a8f32007-08-06 14:15:30 +0000123resolvemsg= resume=
Michael S. Tsirkin67dad682007-02-08 15:57:08 +0200124git_apply_opt=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700125
David Kastrup822f7c72007-09-23 22:42:08 +0200126while test $# != 0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700127do
128 case "$1" in
Pierre Habouzit78443d92007-11-04 11:30:57 +0100129 -i|--interactive)
130 interactive=t ;;
131 -b|--binary)
132 binary=t ;;
133 -3|--3way)
134 threeway=t ;;
Johannes Sixtdfdd7e62007-11-06 21:33:58 +0100135 -s|--signoff)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100136 sign=t ;;
137 -u|--utf8)
138 utf8=t ;; # this is now default
139 --no-utf8)
140 utf8= ;;
141 -k|--keep)
142 keep=t ;;
143 -r|--resolved)
144 resolved=t ;;
145 --skip)
146 skip=t ;;
Junio C Hamano3041c322008-03-04 00:25:06 -0800147 --rebasing)
148 rebasing=t threeway=t keep=t binary=t ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100149 -d|--dotest)
Junio C Hamanoe72c7402008-03-04 00:25:05 -0800150 die "-d option is no longer supported. Do not use."
151 ;;
Pierre Habouzit78443d92007-11-04 11:30:57 +0100152 --resolvemsg)
153 shift; resolvemsg=$1 ;;
154 --whitespace)
155 git_apply_opt="$git_apply_opt $1=$2"; shift ;;
156 -C|-p)
157 git_apply_opt="$git_apply_opt $1$2"; shift ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700158 --)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100159 shift; break ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700160 *)
Pierre Habouzit78443d92007-11-04 11:30:57 +0100161 usage ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700162 esac
Pierre Habouzit78443d92007-11-04 11:30:57 +0100163 shift
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700164done
165
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800166# If the dotest directory exists, but we have finished applying all the
167# patches in them, clear it out.
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700168if test -d "$dotest" &&
169 last=$(cat "$dotest/last") &&
170 next=$(cat "$dotest/next") &&
171 test $# != 0 &&
172 test "$next" -gt "$last"
173then
174 rm -fr "$dotest"
175fi
176
177if test -d "$dotest"
178then
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700179 case "$#,$skip$resolved" in
180 0,*t*)
181 # Explicit resume command and we do not have file, so
182 # we are happy.
183 : ;;
184 0,)
185 # No file input but without resume parameters; catch
186 # user error to feed us a patch from standard input
Junio C Hamanoe72c7402008-03-04 00:25:05 -0800187 # when there is already $dotest. This is somewhat
Junio C Hamanoc95b1382006-09-15 23:19:02 -0700188 # unreliable -- stdin could be /dev/null for example
189 # and the caller did not intend to feed us a patch but
190 # wanted to continue unattended.
191 tty -s
192 ;;
193 *)
194 false
195 ;;
196 esac ||
197 die "previous dotest directory $dotest still exists but mbox given."
Junio C Hamano271440e2005-10-25 23:35:37 -0700198 resume=yes
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700199else
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800200 # Make sure we are not given --skip nor --resolved
201 test ",$skip,$resolved," = ,,, ||
Seancc120052006-05-13 23:34:08 -0400202 die "Resolve operation not in progress, we are not resuming."
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700203
204 # Start afresh.
205 mkdir -p "$dotest" || exit
206
Junio C Hamanobb034f82008-03-04 00:25:04 -0800207 if test -n "$prefix" && test $# != 0
208 then
209 first=t
210 for arg
211 do
212 test -n "$first" && {
213 set x
214 first=
215 }
216 case "$arg" in
217 /*)
218 set "$@" "$arg" ;;
219 *)
220 set "$@" "$prefix$arg" ;;
221 esac
222 done
223 shift
224 fi
Junio C Hamano5be60072007-07-02 22:52:14 -0700225 git mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700226 rm -fr "$dotest"
227 exit 1
228 }
229
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800230 # -b, -s, -u, -k and --whitespace flags are kept for the
231 # resuming session after a patch failure.
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800232 # -3 and -i can and must be given when resuming.
Junio C Hamano087b6742005-11-16 16:46:24 -0800233 echo "$binary" >"$dotest/binary"
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800234 echo " $ws" >"$dotest/whitespace"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700235 echo "$sign" >"$dotest/sign"
236 echo "$utf8" >"$dotest/utf8"
237 echo "$keep" >"$dotest/keep"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700238 echo 1 >"$dotest/next"
Junio C Hamano3041c322008-03-04 00:25:06 -0800239 if test -n "$rebasing"
240 then
241 : >"$dotest/rebasing"
242 else
243 : >"$dotest/applying"
244 fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700245fi
246
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800247case "$resolved" in
248'')
Junio C Hamano38762c42007-11-28 16:15:04 -0800249 files=$(git diff-index --cached --name-only HEAD --) || exit
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800250 if [ "$files" ]; then
251 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
252 exit 1
253 fi
254esac
255
Junio C Hamano087b6742005-11-16 16:46:24 -0800256if test "$(cat "$dotest/binary")" = t
257then
258 binary=--allow-binary-replacement
259fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700260if test "$(cat "$dotest/utf8")" = t
261then
262 utf8=-u
Junio C Hamanobb1091a2007-01-09 21:31:36 -0800263else
264 utf8=-n
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700265fi
266if test "$(cat "$dotest/keep")" = t
267then
268 keep=-k
269fi
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800270ws=`cat "$dotest/whitespace"`
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700271if test "$(cat "$dotest/sign")" = t
272then
273 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
274 s/>.*/>/
275 s/^/Signed-off-by: /'
276 `
277else
278 SIGNOFF=
279fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700280
281last=`cat "$dotest/last"`
282this=`cat "$dotest/next"`
283if test "$skip" = t
284then
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100285 git rerere clear
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700286 this=`expr "$this" + 1`
Jan Harkes69224712005-12-17 01:01:06 -0500287 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700288fi
289
290if test "$this" -gt "$last"
291then
292 echo Nothing to do.
293 rm -fr "$dotest"
294 exit
295fi
296
297while test "$this" -le "$last"
298do
299 msgnum=`printf "%0${prec}d" $this`
300 next=`expr "$this" + 1`
301 test -f "$dotest/$msgnum" || {
Jan Harkes69224712005-12-17 01:01:06 -0500302 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700303 go_next
304 continue
305 }
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800306
307 # If we are not resuming, parse and extract the patch information
308 # into separate files:
309 # - info records the authorship and title
310 # - msg is the rest of commit log message
311 # - patch is the patch body.
312 #
313 # When we are resuming, these files are either already prepared
314 # by the user, or the user can tell us to do so by --resolved flag.
Junio C Hamano271440e2005-10-25 23:35:37 -0700315 case "$resume" in
316 '')
Junio C Hamano5be60072007-07-02 22:52:14 -0700317 git mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
Junio C Hamano271440e2005-10-25 23:35:37 -0700318 <"$dotest/$msgnum" >"$dotest/info" ||
319 stop_here $this
Johannes Schindelinca193cf2007-07-24 01:02:25 +0100320
321 # skip pine's internal folder data
322 grep '^Author: Mail System Internal Data$' \
323 <"$dotest"/info >/dev/null &&
324 go_next && continue
325
Don Zickus87ab7992007-03-12 15:52:04 -0400326 test -s $dotest/patch || {
Josh Triplett6777c382007-04-23 17:32:04 -0700327 echo "Patch is empty. Was it split wrong?"
Don Zickus87ab7992007-03-12 15:52:04 -0400328 stop_here $this
329 }
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700330 if test -f "$dotest/rebasing" &&
331 commit=$(sed -e 's/^From \([0-9a-f]*\) .*/\1/' \
332 -e q "$dotest/$msgnum") &&
333 test "$(git cat-file -t "$commit")" = commit
334 then
335 git cat-file commit "$commit" |
336 sed -e '1,/^$/d' >"$dotest/msg-clean"
337 else
338 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
339 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700340
Junio C Hamano88760462008-04-19 00:25:15 -0700341 (printf '%s\n\n' "$SUBJECT"; cat "$dotest/msg") |
Junio C Hamano5e835ca2008-04-16 12:50:48 -0700342 git stripspace > "$dotest/msg-clean"
343 fi
Junio C Hamano271440e2005-10-25 23:35:37 -0700344 ;;
345 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700346
347 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
348 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
349 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800350
Junio C Hamano3b789592007-12-05 13:16:35 -0800351 if test -z "$GIT_AUTHOR_EMAIL"
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800352 then
Junio C Hamano3b789592007-12-05 13:16:35 -0800353 echo "Patch does not have a valid e-mail address."
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800354 stop_here $this
355 fi
356
Junio C Hamano2c674192005-10-20 22:31:56 -0700357 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700358
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800359 case "$resume" in
360 '')
361 if test '' != "$SIGNOFF"
362 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700363 LAST_SIGNED_OFF_BY=`
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800364 sed -ne '/^Signed-off-by: /p' \
365 "$dotest/msg-clean" |
Jeff Kingb4ce54f2008-03-12 17:34:34 -0400366 sed -ne '$p'
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700367 `
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800368 ADD_SIGNOFF=`
369 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700370 test '' = "$LAST_SIGNED_OFF_BY" && echo
371 echo "$SIGNOFF"
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800372 }`
373 else
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700374 ADD_SIGNOFF=
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800375 fi
376 {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700377 if test -s "$dotest/msg-clean"
378 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700379 cat "$dotest/msg-clean"
380 fi
381 if test '' != "$ADD_SIGNOFF"
382 then
383 echo "$ADD_SIGNOFF"
384 fi
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800385 } >"$dotest/final-commit"
386 ;;
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800387 *)
Junio C Hamano6d286442006-02-23 22:14:47 -0800388 case "$resolved$interactive" in
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800389 tt)
390 # This is used only for interactive view option.
Junio C Hamano38762c42007-11-28 16:15:04 -0800391 git diff-index -p --cached HEAD -- >"$dotest/patch"
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800392 ;;
393 esac
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800394 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700395
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800396 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700397 if test "$interactive" = t
398 then
Junio C Hamanoa1451102005-10-12 18:31:41 -0700399 test -t 0 ||
400 die "cannot be interactive without stdin connected to a terminal."
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700401 action=again
402 while test "$action" = again
403 do
404 echo "Commit Body is:"
405 echo "--------------------------"
406 cat "$dotest/final-commit"
407 echo "--------------------------"
Jason Riedy97545632005-12-06 14:21:52 -0800408 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700409 read reply
410 case "$reply" in
Junio C Hamanof89ad672005-10-25 23:43:59 -0700411 [yY]*) action=yes ;;
412 [aA]*) action=yes interactive= ;;
413 [nN]*) action=skip ;;
Adam Robenef0c2ab2007-07-19 22:09:35 -0700414 [eE]*) git_editor "$dotest/final-commit"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700415 action=again ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700416 [vV]*) action=again
417 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
418 *) action=again ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700419 esac
420 done
421 else
422 action=yes
423 fi
Junio C Hamano11dc4e72008-04-16 16:46:26 -0700424 FIRSTLINE=$(head -1 "$dotest/final-commit")
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700425
426 if test $action = skip
427 then
428 go_next
429 continue
430 fi
431
432 if test -x "$GIT_DIR"/hooks/applypatch-msg
433 then
434 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
435 stop_here $this
436 fi
437
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700438 printf 'Applying %s\n' "$FIRSTLINE"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700439
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800440 case "$resolved" in
441 '')
Junio C Hamano5be60072007-07-02 22:52:14 -0700442 git apply $git_apply_opt $binary --index "$dotest/patch"
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800443 apply_status=$?
444 ;;
445 t)
446 # Resolved means the user did all the hard work, and
447 # we do not have to do any patch application. Just
448 # trust what the user has in the index file and the
449 # working tree.
450 resolved=
Junio C Hamano38762c42007-11-28 16:15:04 -0800451 git diff-index --quiet --cached HEAD -- && {
Shawn O. Pearce64646d12006-12-21 21:30:17 -0500452 echo "No changes - did you forget to use 'git add'?"
Robert Shearmanced94562006-05-02 13:32:43 +0100453 stop_here_user_resolve $this
Alex Riesen06aff472007-03-25 01:56:13 +0100454 }
Junio C Hamano5be60072007-07-02 22:52:14 -0700455 unmerged=$(git ls-files -u)
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700456 if test -n "$unmerged"
457 then
458 echo "You still have unmerged paths in your index"
Shawn O. Pearce64646d12006-12-21 21:30:17 -0500459 echo "did you forget to use 'git add'?"
Robert Shearmanced94562006-05-02 13:32:43 +0100460 stop_here_user_resolve $this
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700461 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800462 apply_status=0
Johannes Schindelinb4372ef2007-07-06 13:05:59 +0100463 git rerere
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800464 ;;
465 esac
466
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700467 if test $apply_status = 1 && test "$threeway" = t
468 then
Junio C Hamano73319032005-10-13 11:46:43 -0700469 if (fall_back_3way)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700470 then
Junio C Hamano73319032005-10-13 11:46:43 -0700471 # Applying the patch to an earlier tree and merging the
472 # result may have produced the same tree as ours.
Junio C Hamano38762c42007-11-28 16:15:04 -0800473 git diff-index --quiet --cached HEAD -- && {
Alex Riesen06aff472007-03-25 01:56:13 +0100474 echo No changes -- Patch already applied.
475 go_next
476 continue
477 }
Junio C Hamano73319032005-10-13 11:46:43 -0700478 # clear apply_status -- we have successfully merged.
479 apply_status=0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700480 fi
481 fi
482 if test $apply_status != 0
483 then
484 echo Patch failed at $msgnum.
Robert Shearmanced94562006-05-02 13:32:43 +0100485 stop_here_user_resolve $this
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700486 fi
487
488 if test -x "$GIT_DIR"/hooks/pre-applypatch
489 then
490 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
491 fi
492
Junio C Hamano5be60072007-07-02 22:52:14 -0700493 tree=$(git write-tree) &&
Junio C Hamano5be60072007-07-02 22:52:14 -0700494 parent=$(git rev-parse --verify HEAD) &&
495 commit=$(git commit-tree $tree -p $parent <"$dotest/final-commit") &&
Linus Torvalds2e6e3e8292008-04-15 12:56:50 -0700496 git update-ref -m "$GIT_REFLOG_ACTION: $FIRSTLINE" HEAD $commit $parent ||
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700497 stop_here $this
498
499 if test -x "$GIT_DIR"/hooks/post-applypatch
500 then
501 "$GIT_DIR"/hooks/post-applypatch
502 fi
503
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700504 go_next
505done
506
Michael Stefaniuce0cd2522008-01-04 19:59:26 +0100507git gc --auto
508
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700509rm -fr "$dotest"