blob: d0af786aec3f797943290cdc63fa77f393900160 [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
Junio C Hamano8273c792006-03-02 00:45:48 -08005USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way]
6 [--interactive] [--whitespace=<option>] <mbox>...
freku045@student.liu.secf1fe882005-12-13 23:30:31 +01007 or, when resuming [--skip | --resolved]'
8. git-sh-setup
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -07009
Junio C Hamanod64e6b02006-02-18 20:51:26 -080010git var GIT_COMMITTER_IDENT >/dev/null || exit
11
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070012stop_here () {
13 echo "$1" >"$dotest/next"
14 exit 1
15}
16
Robert Shearmanced94562006-05-02 13:32:43 +010017stop_here_user_resolve () {
Seancc120052006-05-13 23:34:08 -040018 if [ -n "$resolvemsg" ]; then
19 echo "$resolvemsg"
20 stop_here $1
21 fi
Robert Shearmanced94562006-05-02 13:32:43 +010022 cmdline=$(basename $0)
23 if test '' != "$interactive"
24 then
25 cmdline="$cmdline -i"
26 fi
27 if test '' != "$threeway"
28 then
29 cmdline="$cmdline -3"
30 fi
31 if test '.dotest' != "$dotest"
32 then
33 cmdline="$cmdline -d=$dotest"
34 fi
35 echo "When you have resolved this problem run \"$cmdline --resolved\"."
36 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
37
38 stop_here $1
39}
40
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070041go_next () {
42 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
43 "$dotest/patch" "$dotest/info"
44 echo "$next" >"$dotest/next"
45 this=$next
46}
47
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -070048cannot_fallback () {
49 echo "$1"
50 echo "Cannot fall back to three-way merge."
51 exit 1
52}
53
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070054fall_back_3way () {
55 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
56
57 rm -fr "$dotest"/patch-merge-*
58 mkdir "$dotest/patch-merge-tmp-dir"
59
60 # First see if the patch records the index info that we can use.
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -070061 git-apply -z --index-info "$dotest/patch" \
62 >"$dotest/patch-merge-index-info" &&
63 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
64 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
65 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
66 git-write-tree >"$dotest/patch-merge-base+" ||
67 cannot_fallback "Patch does not record usable index information."
68
69 echo Using index info to reconstruct a base tree...
70 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
Junio C Hamanob7627272006-05-15 17:25:43 -070071 git-apply $binary --cached <"$dotest/patch"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070072 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070073 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
74 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
Junio C Hamanofd7bcfb2006-08-12 16:16:47 -070075 else
76 cannot_fallback "Did you hand edit your patch?
77It does not apply to blobs recorded in its index."
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070078 fi
79
80 test -f "$dotest/patch-merge-index" &&
81 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
82 orig_tree=$(cat "$dotest/patch-merge-base") &&
83 rm -fr "$dotest"/patch-merge-* || exit 1
84
85 echo Falling back to patching base and 3-way merge...
86
87 # This is not so wrong. Depending on which base we picked,
88 # orig_tree may be wildly different from ours, but his_tree
89 # has the same set of wildly different changes in parts the
Pavel Roskin82e5a822006-07-10 01:50:18 -040090 # patch did not touch, so resolve ends up canceling them,
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070091 # saying that we reverted all those changes.
92
93 git-merge-resolve $orig_tree -- HEAD $his_tree || {
Junio C Hamano1536dd9c62006-02-11 18:55:43 -080094 if test -d "$GIT_DIR/rr-cache"
95 then
96 git-rerere
97 fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -070098 echo Failed to merge in the changes.
99 exit 1
100 }
101}
102
103prec=4
Shawn Pearce8ef1c7c2006-07-14 00:47:23 -0400104rloga=am
Seancc120052006-05-13 23:34:08 -0400105dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws= resolvemsg=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700106
107while case "$#" in 0) break;; esac
108do
109 case "$1" in
110 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
Dennis Stosberg8096fae2006-06-27 18:54:26 +0200111 dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700112 -d|--d|--do|--dot|--dote|--dotes|--dotest)
113 case "$#" in 1) usage ;; esac; shift
114 dotest="$1"; shift;;
115
116 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
117 --interacti|--interactiv|--interactive)
118 interactive=t; shift ;;
119
Junio C Hamano087b6742005-11-16 16:46:24 -0800120 -b|--b|--bi|--bin|--bina|--binar|--binary)
121 binary=t; shift ;;
122
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700123 -3|--3|--3w|--3wa|--3way)
124 threeway=t; shift ;;
125 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
126 sign=t; shift ;;
127 -u|--u|--ut|--utf|--utf8)
128 utf8=t; shift ;;
129 -k|--k|--ke|--kee|--keep)
130 keep=t; shift ;;
131
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800132 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
133 resolved=t; shift ;;
134
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700135 --sk|--ski|--skip)
136 skip=t; shift ;;
137
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800138 --whitespace=*)
139 ws=$1; shift ;;
140
Seancc120052006-05-13 23:34:08 -0400141 --resolvemsg=*)
142 resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//"); shift ;;
143
Shawn Pearce8ef1c7c2006-07-14 00:47:23 -0400144 --reflog-action=*)
145 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
146
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700147 --)
148 shift; break ;;
149 -*)
150 usage ;;
151 *)
152 break ;;
153 esac
154done
155
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800156# If the dotest directory exists, but we have finished applying all the
157# patches in them, clear it out.
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700158if test -d "$dotest" &&
159 last=$(cat "$dotest/last") &&
160 next=$(cat "$dotest/next") &&
161 test $# != 0 &&
162 test "$next" -gt "$last"
163then
164 rm -fr "$dotest"
165fi
166
167if test -d "$dotest"
168then
Lukas Sandströmcbd64af2006-07-19 22:28:00 +0200169 if test ",$#," != ",0," || ! tty -s
170 then
171 die "previous dotest directory $dotest still exists but mbox given."
172 fi
Junio C Hamano271440e2005-10-25 23:35:37 -0700173 resume=yes
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700174else
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800175 # Make sure we are not given --skip nor --resolved
176 test ",$skip,$resolved," = ,,, ||
Seancc120052006-05-13 23:34:08 -0400177 die "Resolve operation not in progress, we are not resuming."
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700178
179 # Start afresh.
180 mkdir -p "$dotest" || exit
181
H. Peter Anvinb3f041f2005-12-13 22:39:23 -0800182 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700183 rm -fr "$dotest"
184 exit 1
185 }
186
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800187 # -b, -s, -u, -k and --whitespace flags are kept for the
188 # resuming session after a patch failure.
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800189 # -3 and -i can and must be given when resuming.
Junio C Hamano087b6742005-11-16 16:46:24 -0800190 echo "$binary" >"$dotest/binary"
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800191 echo " $ws" >"$dotest/whitespace"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700192 echo "$sign" >"$dotest/sign"
193 echo "$utf8" >"$dotest/utf8"
194 echo "$keep" >"$dotest/keep"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700195 echo 1 >"$dotest/next"
196fi
197
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800198case "$resolved" in
199'')
200 files=$(git-diff-index --cached --name-only HEAD) || exit
201 if [ "$files" ]; then
202 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
203 exit 1
204 fi
205esac
206
Junio C Hamano087b6742005-11-16 16:46:24 -0800207if test "$(cat "$dotest/binary")" = t
208then
209 binary=--allow-binary-replacement
210fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700211if test "$(cat "$dotest/utf8")" = t
212then
213 utf8=-u
214fi
215if test "$(cat "$dotest/keep")" = t
216then
217 keep=-k
218fi
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800219ws=`cat "$dotest/whitespace"`
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700220if test "$(cat "$dotest/sign")" = t
221then
222 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
223 s/>.*/>/
224 s/^/Signed-off-by: /'
225 `
226else
227 SIGNOFF=
228fi
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700229
230last=`cat "$dotest/last"`
231this=`cat "$dotest/next"`
232if test "$skip" = t
233then
234 this=`expr "$this" + 1`
Jan Harkes69224712005-12-17 01:01:06 -0500235 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700236fi
237
238if test "$this" -gt "$last"
239then
240 echo Nothing to do.
241 rm -fr "$dotest"
242 exit
243fi
244
245while test "$this" -le "$last"
246do
247 msgnum=`printf "%0${prec}d" $this`
248 next=`expr "$this" + 1`
249 test -f "$dotest/$msgnum" || {
Jan Harkes69224712005-12-17 01:01:06 -0500250 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700251 go_next
252 continue
253 }
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800254
255 # If we are not resuming, parse and extract the patch information
256 # into separate files:
257 # - info records the authorship and title
258 # - msg is the rest of commit log message
259 # - patch is the patch body.
260 #
261 # When we are resuming, these files are either already prepared
262 # by the user, or the user can tell us to do so by --resolved flag.
Junio C Hamano271440e2005-10-25 23:35:37 -0700263 case "$resume" in
264 '')
265 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
266 <"$dotest/$msgnum" >"$dotest/info" ||
267 stop_here $this
268 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
269 ;;
270 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700271
272 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
273 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
274 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
Junio C Hamanoe0e3ba22005-12-14 16:31:06 -0800275
276 if test -z "$GIT_AUTHOR_EMAIL"
277 then
278 echo "Patch does not have a valid e-mail address."
279 stop_here $this
280 fi
281
Junio C Hamano2c674192005-10-20 22:31:56 -0700282 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700283
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800284 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700285 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800286
287 case "$resume" in
288 '')
289 if test '' != "$SIGNOFF"
290 then
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700291 LAST_SIGNED_OFF_BY=`
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800292 sed -ne '/^Signed-off-by: /p' \
293 "$dotest/msg-clean" |
294 tail -n 1
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700295 `
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800296 ADD_SIGNOFF=`
297 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700298 test '' = "$LAST_SIGNED_OFF_BY" && echo
299 echo "$SIGNOFF"
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800300 }`
301 else
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700302 ADD_SIGNOFF=
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800303 fi
304 {
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700305 echo "$SUBJECT"
306 if test -s "$dotest/msg-clean"
307 then
308 echo
309 cat "$dotest/msg-clean"
310 fi
311 if test '' != "$ADD_SIGNOFF"
312 then
313 echo "$ADD_SIGNOFF"
314 fi
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800315 } >"$dotest/final-commit"
316 ;;
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800317 *)
Junio C Hamano6d286442006-02-23 22:14:47 -0800318 case "$resolved$interactive" in
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800319 tt)
320 # This is used only for interactive view option.
321 git-diff-index -p --cached HEAD >"$dotest/patch"
322 ;;
323 esac
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800324 esac
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700325
Junio C Hamano4bfb6b62005-11-08 00:41:37 -0800326 resume=
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700327 if test "$interactive" = t
328 then
Junio C Hamanoa1451102005-10-12 18:31:41 -0700329 test -t 0 ||
330 die "cannot be interactive without stdin connected to a terminal."
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700331 action=again
332 while test "$action" = again
333 do
334 echo "Commit Body is:"
335 echo "--------------------------"
336 cat "$dotest/final-commit"
337 echo "--------------------------"
Jason Riedy97545632005-12-06 14:21:52 -0800338 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700339 read reply
340 case "$reply" in
Junio C Hamanof89ad672005-10-25 23:43:59 -0700341 [yY]*) action=yes ;;
342 [aA]*) action=yes interactive= ;;
343 [nN]*) action=skip ;;
344 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700345 action=again ;;
Junio C Hamanof89ad672005-10-25 23:43:59 -0700346 [vV]*) action=again
347 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
348 *) action=again ;;
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700349 esac
350 done
351 else
352 action=yes
353 fi
354
355 if test $action = skip
356 then
357 go_next
358 continue
359 fi
360
361 if test -x "$GIT_DIR"/hooks/applypatch-msg
362 then
363 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
364 stop_here $this
365 fi
366
367 echo
368 echo "Applying '$SUBJECT'"
369 echo
370
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800371 case "$resolved" in
372 '')
Junio C Hamano8c31cb82006-02-28 20:26:25 -0800373 git-apply $binary --index $ws "$dotest/patch"
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800374 apply_status=$?
375 ;;
376 t)
377 # Resolved means the user did all the hard work, and
378 # we do not have to do any patch application. Just
379 # trust what the user has in the index file and the
380 # working tree.
381 resolved=
Junio C Hamano6d286442006-02-23 22:14:47 -0800382 changed="$(git-diff-index --cached --name-only HEAD)"
383 if test '' = "$changed"
384 then
385 echo "No changes - did you forget update-index?"
Robert Shearmanced94562006-05-02 13:32:43 +0100386 stop_here_user_resolve $this
Junio C Hamano6d286442006-02-23 22:14:47 -0800387 fi
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700388 unmerged=$(git-ls-files -u)
389 if test -n "$unmerged"
390 then
391 echo "You still have unmerged paths in your index"
392 echo "did you forget update-index?"
Robert Shearmanced94562006-05-02 13:32:43 +0100393 stop_here_user_resolve $this
Junio C Hamanoc1d11282006-04-28 02:32:44 -0700394 fi
Junio C Hamano0c15cc92005-11-16 00:19:32 -0800395 apply_status=0
396 ;;
397 esac
398
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700399 if test $apply_status = 1 && test "$threeway" = t
400 then
Junio C Hamano73319032005-10-13 11:46:43 -0700401 if (fall_back_3way)
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700402 then
Junio C Hamano73319032005-10-13 11:46:43 -0700403 # Applying the patch to an earlier tree and merging the
404 # result may have produced the same tree as ours.
Junio C Hamano6d286442006-02-23 22:14:47 -0800405 changed="$(git-diff-index --cached --name-only HEAD)"
Junio C Hamano73319032005-10-13 11:46:43 -0700406 if test '' = "$changed"
407 then
408 echo No changes -- Patch already applied.
409 go_next
410 continue
411 fi
412 # clear apply_status -- we have successfully merged.
413 apply_status=0
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700414 fi
415 fi
416 if test $apply_status != 0
417 then
418 echo Patch failed at $msgnum.
Robert Shearmanced94562006-05-02 13:32:43 +0100419 stop_here_user_resolve $this
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700420 fi
421
422 if test -x "$GIT_DIR"/hooks/pre-applypatch
423 then
424 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
425 fi
426
427 tree=$(git-write-tree) &&
428 echo Wrote tree $tree &&
429 parent=$(git-rev-parse --verify HEAD) &&
430 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
431 echo Committed: $commit &&
Shawn Pearce8ef1c7c2006-07-14 00:47:23 -0400432 git-update-ref -m "$rloga: $SUBJECT" HEAD $commit $parent ||
Junio C Hamanod1c5f2a2005-10-07 03:44:18 -0700433 stop_here $this
434
435 if test -x "$GIT_DIR"/hooks/post-applypatch
436 then
437 "$GIT_DIR"/hooks/post-applypatch
438 fi
439
440 go_next
441done
442
443rm -fr "$dotest"