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]'
10 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
13 echo "$1" >"$dotest/next"
17 stop_here_user_resolve
() {
18 if [ -n "$resolvemsg" ]; then
22 cmdline
=$
(basename $0)
23 if test '' != "$interactive"
27 if test '' != "$threeway"
31 if test '.dotest' != "$dotest"
33 cmdline
="$cmdline -d=$dotest"
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\"."
42 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
43 "$dotest/patch" "$dotest/info"
44 echo "$next" >"$dotest/next"
50 echo "Cannot fall back to three-way merge."
55 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
57 rm -fr "$dotest"/patch-merge-
*
58 mkdir
"$dotest/patch-merge-tmp-dir"
60 # First see if the patch records the index info that we can use.
61 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."
69 echo Using index info to reconstruct a base tree...
70 if GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
71 git-apply
$binary --cached <"$dotest/patch"
73 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
74 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
76 cannot_fallback
"Did you hand edit your patch?
77 It does not apply to blobs recorded in its index."
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
85 echo Falling back to patching base and
3-way merge...
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
90 # patch did not touch, so resolve ends up canceling them,
91 # saying that we reverted all those changes.
93 git-merge-resolve
$orig_tree -- HEAD
$his_tree ||
{
94 if test -d "$GIT_DIR/rr-cache"
98 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 ;;
145 rloga
=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
156 # If the dotest directory exists, but we have finished applying all the
157 # patches in them, clear it out.
158 if test -d "$dotest" &&
159 last
=$
(cat "$dotest/last") &&
160 next
=$
(cat "$dotest/next") &&
162 test "$next" -gt "$last"
169 if test ",$#," != ",0," ||
! tty
-s
171 die
"previous dotest directory $dotest still exists but mbox given."
175 # Make sure we are not given --skip nor --resolved
176 test ",$skip,$resolved," = ,,, ||
177 die
"Resolve operation not in progress, we are not resuming."
180 mkdir
-p "$dotest" ||
exit
182 git-mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
187 # -b, -s, -u, -k and --whitespace flags are kept for the
188 # resuming session after a patch failure.
189 # -3 and -i can and must be given when resuming.
190 echo "$binary" >"$dotest/binary"
191 echo " $ws" >"$dotest/whitespace"
192 echo "$sign" >"$dotest/sign"
193 echo "$utf8" >"$dotest/utf8"
194 echo "$keep" >"$dotest/keep"
195 echo 1 >"$dotest/next"
200 files
=$
(git-diff-index
--cached --name-only HEAD
) ||
exit
201 if [ "$files" ]; then
202 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
207 if test "$(cat "$dotest/binary
")" = t
209 binary
=--allow-binary-replacement
211 if test "$(cat "$dotest/utf8
")" = t
215 if test "$(cat "$dotest/keep
")" = t
219 ws
=`cat "$dotest/whitespace"`
220 if test "$(cat "$dotest/sign
")" = t
222 SIGNOFF
=`git-var GIT_COMMITTER_IDENT | sed -e '
224 s/^/Signed-off-by: /'
230 last
=`cat "$dotest/last"`
231 this
=`cat "$dotest/next"`
234 this
=`expr "$this" + 1`
238 if test "$this" -gt "$last"
245 while test "$this" -le "$last"
247 msgnum
=`printf "%0${prec}d" $this`
248 next
=`expr "$this" + 1`
249 test -f "$dotest/$msgnum" ||
{
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.
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.
265 git-mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
266 <"$dotest/$msgnum" >"$dotest/info" ||
268 git-stripspace
< "$dotest/msg" > "$dotest/msg-clean"
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
")"
276 if test -z "$GIT_AUTHOR_EMAIL"
278 echo "Patch does not have a valid e-mail address."
282 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
284 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
285 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
289 if test '' != "$SIGNOFF"
292 sed -ne '/^Signed-off-by: /p' \
293 "$dotest/msg-clean" |
297 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
298 test '' = "$LAST_SIGNED_OFF_BY" && echo
306 if test -s "$dotest/msg-clean"
309 cat "$dotest/msg-clean"
311 if test '' != "$ADD_SIGNOFF"
315 } >"$dotest/final-commit"
318 case "$resolved$interactive" in
320 # This is used only for interactive view option.
321 git-diff-index
-p --cached HEAD
>"$dotest/patch"
327 if test "$interactive" = t
330 die
"cannot be interactive without stdin connected to a terminal."
332 while test "$action" = again
334 echo "Commit Body is:"
335 echo "--------------------------"
336 cat "$dotest/final-commit"
337 echo "--------------------------"
338 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
342 [aA
]*) action
=yes interactive
= ;;
343 [nN
]*) action
=skip
;;
344 [eE
]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
347 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
355 if test $action = skip
361 if test -x "$GIT_DIR"/hooks
/applypatch-msg
363 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
368 echo "Applying '$SUBJECT'"
373 git-apply
$binary --index $ws "$dotest/patch"
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
382 changed
="$(git-diff-index --cached --name-only HEAD)"
383 if test '' = "$changed"
385 echo "No changes - did you forget update-index?"
386 stop_here_user_resolve
$this
388 unmerged
=$
(git-ls-files
-u)
389 if test -n "$unmerged"
391 echo "You still have unmerged paths in your index"
392 echo "did you forget update-index?"
393 stop_here_user_resolve
$this
399 if test $apply_status = 1 && test "$threeway" = t
403 # Applying the patch to an earlier tree and merging the
404 # result may have produced the same tree as ours.
405 changed
="$(git-diff-index --cached --name-only HEAD)"
406 if test '' = "$changed"
408 echo No changes
-- Patch already applied.
412 # clear apply_status -- we have successfully merged.
416 if test $apply_status != 0
418 echo Patch failed
at $msgnum.
419 stop_here_user_resolve
$this
422 if test -x "$GIT_DIR"/hooks
/pre-applypatch
424 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
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 &&
432 git-update-ref
-m "$rloga: $SUBJECT" HEAD
$commit $parent ||
435 if test -x "$GIT_DIR"/hooks
/post-applypatch
437 "$GIT_DIR"/hooks
/post-applypatch