3 # Copyright (c) 2005, 2006 Junio C Hamano
5 USAGE
='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way]
6 [--interactive] [--whitespace=<option>] <mbox>...
7 or, when resuming [--skip | --resolved]'
11 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
14 echo "$1" >"$dotest/next"
18 stop_here_user_resolve
() {
19 if [ -n "$resolvemsg" ]; then
23 cmdline
=$
(basename $0)
24 if test '' != "$interactive"
28 if test '' != "$threeway"
32 if test '.dotest' != "$dotest"
34 cmdline
="$cmdline -d=$dotest"
36 echo "When you have resolved this problem run \"$cmdline --resolved\"."
37 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
43 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
44 "$dotest/patch" "$dotest/info"
45 echo "$next" >"$dotest/next"
51 echo "Cannot fall back to three-way merge."
56 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
58 rm -fr "$dotest"/patch-merge-
*
59 mkdir
"$dotest/patch-merge-tmp-dir"
61 # First see if the patch records the index info that we can use.
62 git-apply
-z --index-info "$dotest/patch" \
63 >"$dotest/patch-merge-index-info" &&
64 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
65 git-update-index
-z --index-info <"$dotest/patch-merge-index-info" &&
66 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
67 git-write-tree
>"$dotest/patch-merge-base+" ||
68 cannot_fallback
"Patch does not record usable index information."
70 echo Using index info to reconstruct a base tree...
71 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
72 git-apply
$binary --cached <"$dotest/patch"
74 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
75 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
77 cannot_fallback
"Did you hand edit your patch?
78 It does not apply to blobs recorded in its index."
81 test -f "$dotest/patch-merge-index" &&
82 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git-write-tree
) &&
83 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
84 rm -fr "$dotest"/patch-merge-
* ||
exit 1
86 echo Falling back to patching base and
3-way merge...
88 # This is not so wrong. Depending on which base we picked,
89 # orig_tree may be wildly different from ours, but his_tree
90 # has the same set of wildly different changes in parts the
91 # patch did not touch, so resolve ends up canceling them,
92 # saying that we reverted all those changes.
94 git-merge-resolve
$orig_tree -- HEAD
$his_tree ||
{
95 if test -d "$GIT_DIR/rr-cache"
99 echo Failed to merge
in the changes.
105 dotest
=.dotest sign
= utf8
= keep
= skip
= interactive
= resolved
= binary
= ws
= resolvemsg
=
107 while case "$#" in 0) break;; esac
110 -d=*|
--d=*|
--do=*|
--dot=*|
--dote=*|
--dotes=*|
--dotest=*)
111 dotest
=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
112 -d|
--d|
--do|
--dot|
--dote|
--dotes|
--dotest)
113 case "$#" in 1) usage
;; esac; shift
116 -i|
--i|
--in|
--int|
--inte|
--inter|
--intera|
--interac|
--interact|\
117 --interacti|
--interactiv|
--interactive)
118 interactive
=t
; shift ;;
120 -b|
--b|
--bi|
--bin|
--bina|
--binar|
--binary)
123 -3|
--3|
--3w|
--3wa|
--3way)
125 -s|
--s|
--si|
--sig|
--sign|
--signo|
--signof|
--signoff)
127 -u|
--u|
--ut|
--utf|
--utf8)
129 -k|
--k|
--ke|
--kee|
--keep)
132 -r|
--r|
--re|
--res|
--reso|
--resol|
--resolv|
--resolve|
--resolved)
142 resolvemsg
=$
(echo "$1" |
sed -e "s/^--resolvemsg=//"); shift ;;
153 # If the dotest directory exists, but we have finished applying all the
154 # patches in them, clear it out.
155 if test -d "$dotest" &&
156 last
=$
(cat "$dotest/last") &&
157 next
=$
(cat "$dotest/next") &&
159 test "$next" -gt "$last"
166 case "$#,$skip$resolved" in
168 # Explicit resume command and we do not have file, so
172 # No file input but without resume parameters; catch
173 # user error to feed us a patch from standard input
174 # when there is already .dotest. This is somewhat
175 # unreliable -- stdin could be /dev/null for example
176 # and the caller did not intend to feed us a patch but
177 # wanted to continue unattended.
184 die
"previous dotest directory $dotest still exists but mbox given."
187 # Make sure we are not given --skip nor --resolved
188 test ",$skip,$resolved," = ,,, ||
189 die
"Resolve operation not in progress, we are not resuming."
192 mkdir
-p "$dotest" ||
exit
194 git-mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
199 # -b, -s, -u, -k and --whitespace flags are kept for the
200 # resuming session after a patch failure.
201 # -3 and -i can and must be given when resuming.
202 echo "$binary" >"$dotest/binary"
203 echo " $ws" >"$dotest/whitespace"
204 echo "$sign" >"$dotest/sign"
205 echo "$utf8" >"$dotest/utf8"
206 echo "$keep" >"$dotest/keep"
207 echo 1 >"$dotest/next"
212 files
=$
(git-diff-index
--cached --name-only HEAD
) ||
exit
213 if [ "$files" ]; then
214 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
219 if test "$(cat "$dotest/binary
")" = t
221 binary
=--allow-binary-replacement
223 if test "$(cat "$dotest/utf8
")" = t
227 if test "$(cat "$dotest/keep
")" = t
231 ws
=`cat "$dotest/whitespace"`
232 if test "$(cat "$dotest/sign
")" = t
234 SIGNOFF
=`git-var GIT_COMMITTER_IDENT | sed -e '
236 s/^/Signed-off-by: /'
242 last
=`cat "$dotest/last"`
243 this
=`cat "$dotest/next"`
246 if test -d "$GIT_DIR/rr-cache"
250 this
=`expr "$this" + 1`
254 if test "$this" -gt "$last"
261 while test "$this" -le "$last"
263 msgnum
=`printf "%0${prec}d" $this`
264 next
=`expr "$this" + 1`
265 test -f "$dotest/$msgnum" ||
{
271 # If we are not resuming, parse and extract the patch information
272 # into separate files:
273 # - info records the authorship and title
274 # - msg is the rest of commit log message
275 # - patch is the patch body.
277 # When we are resuming, these files are either already prepared
278 # by the user, or the user can tell us to do so by --resolved flag.
281 git-mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
282 <"$dotest/$msgnum" >"$dotest/info" ||
284 git-stripspace
< "$dotest/msg" > "$dotest/msg-clean"
288 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
289 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
290 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
292 if test -z "$GIT_AUTHOR_EMAIL"
294 echo "Patch does not have a valid e-mail address."
298 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
300 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
301 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
305 if test '' != "$SIGNOFF"
308 sed -ne '/^Signed-off-by: /p' \
309 "$dotest/msg-clean" |
313 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
314 test '' = "$LAST_SIGNED_OFF_BY" && echo
322 if test -s "$dotest/msg-clean"
325 cat "$dotest/msg-clean"
327 if test '' != "$ADD_SIGNOFF"
331 } >"$dotest/final-commit"
334 case "$resolved$interactive" in
336 # This is used only for interactive view option.
337 git-diff-index
-p --cached HEAD
>"$dotest/patch"
343 if test "$interactive" = t
346 die
"cannot be interactive without stdin connected to a terminal."
348 while test "$action" = again
350 echo "Commit Body is:"
351 echo "--------------------------"
352 cat "$dotest/final-commit"
353 echo "--------------------------"
354 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
358 [aA
]*) action
=yes interactive
= ;;
359 [nN
]*) action
=skip
;;
360 [eE
]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
363 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
371 if test $action = skip
377 if test -x "$GIT_DIR"/hooks
/applypatch-msg
379 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
384 echo "Applying '$SUBJECT'"
389 git-apply
$binary --index $ws "$dotest/patch"
393 # Resolved means the user did all the hard work, and
394 # we do not have to do any patch application. Just
395 # trust what the user has in the index file and the
398 changed
="$(git-diff-index --cached --name-only HEAD)"
399 if test '' = "$changed"
401 echo "No changes - did you forget to use 'git add'?"
402 stop_here_user_resolve
$this
404 unmerged
=$
(git-ls-files
-u)
405 if test -n "$unmerged"
407 echo "You still have unmerged paths in your index"
408 echo "did you forget to use 'git add'?"
409 stop_here_user_resolve
$this
412 if test -d "$GIT_DIR/rr-cache"
419 if test $apply_status = 1 && test "$threeway" = t
423 # Applying the patch to an earlier tree and merging the
424 # result may have produced the same tree as ours.
425 changed
="$(git-diff-index --cached --name-only HEAD)"
426 if test '' = "$changed"
428 echo No changes
-- Patch already applied.
432 # clear apply_status -- we have successfully merged.
436 if test $apply_status != 0
438 echo Patch failed
at $msgnum.
439 stop_here_user_resolve
$this
442 if test -x "$GIT_DIR"/hooks
/pre-applypatch
444 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
447 tree
=$
(git-write-tree
) &&
448 echo Wrote tree
$tree &&
449 parent
=$
(git-rev-parse
--verify HEAD
) &&
450 commit
=$
(git-commit-tree
$tree -p $parent <"$dotest/final-commit") &&
451 echo Committed
: $commit &&
452 git-update-ref
-m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD
$commit $parent ||
455 if test -x "$GIT_DIR"/hooks
/post-applypatch
457 "$GIT_DIR"/hooks
/post-applypatch