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"
49 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
51 rm -fr "$dotest"/patch-merge-
*
52 mkdir
"$dotest/patch-merge-tmp-dir"
54 # First see if the patch records the index info that we can use.
55 if git-apply
-z --index-info "$dotest/patch" \
56 >"$dotest/patch-merge-index-info" 2>/dev
/null
&&
57 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
58 git-update-index
-z --index-info <"$dotest/patch-merge-index-info" &&
59 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
60 git-write-tree
>"$dotest/patch-merge-base+" &&
61 # index has the base tree now.
63 cd "$dotest/patch-merge-tmp-dir" &&
64 GIT_INDEX_FILE
="../patch-merge-tmp-index" \
65 GIT_OBJECT_DIRECTORY
="$O_OBJECT" \
66 git-apply
$binary --index <..
/patch
69 echo Using index info to reconstruct a base tree...
70 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
71 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
73 # Otherwise, try nearby trees that can be used to apply the
78 # Hoping the patch is against our recent commits...
79 git-rev-list
--max-count=$N HEAD
81 # or hoping the patch is against known tags...
82 git-ls-remote
--tags .
86 # See if we have it as a tree...
87 git-cat-file tree
"$base" >/dev
/null
2>&1 ||
continue
89 rm -fr "$dotest"/patch-merge-
* &&
90 mkdir
"$dotest/patch-merge-tmp-dir" ||
break
92 cd "$dotest/patch-merge-tmp-dir" &&
93 GIT_INDEX_FILE
=..
/patch-merge-tmp-index
&&
94 GIT_OBJECT_DIRECTORY
="$O_OBJECT" &&
95 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY
&&
96 git-read-tree
"$base" &&
97 git-apply
$binary --index &&
98 mv ..
/patch-merge-tmp-index ..
/patch-merge-index
&&
99 echo "$base" >..
/patch-merge-base
100 ) <"$dotest/patch" 2>/dev
/null
&& break
104 test -f "$dotest/patch-merge-index" &&
105 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git-write-tree
) &&
106 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
107 rm -fr "$dotest"/patch-merge-
* ||
exit 1
109 echo Falling back to patching base and
3-way merge...
111 # This is not so wrong. Depending on which base we picked,
112 # orig_tree may be wildly different from ours, but his_tree
113 # has the same set of wildly different changes in parts the
114 # patch did not touch, so resolve ends up cancelling them,
115 # saying that we reverted all those changes.
117 git-merge-resolve
$orig_tree -- HEAD
$his_tree ||
{
118 if test -d "$GIT_DIR/rr-cache"
122 echo Failed to merge
in the changes.
128 dotest
=.dotest sign
= utf8
= keep
= skip
= interactive
= resolved
= binary
= ws
= resolvemsg
=
130 while case "$#" in 0) break;; esac
133 -d=*|
--d=*|
--do=*|
--dot=*|
--dote=*|
--dotes=*|
--dotest=*)
134 dotest
=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
135 -d|
--d|
--do|
--dot|
--dote|
--dotes|
--dotest)
136 case "$#" in 1) usage
;; esac; shift
139 -i|
--i|
--in|
--int|
--inte|
--inter|
--intera|
--interac|
--interact|\
140 --interacti|
--interactiv|
--interactive)
141 interactive
=t
; shift ;;
143 -b|
--b|
--bi|
--bin|
--bina|
--binar|
--binary)
146 -3|
--3|
--3w|
--3wa|
--3way)
148 -s|
--s|
--si|
--sig|
--sign|
--signo|
--signof|
--signoff)
150 -u|
--u|
--ut|
--utf|
--utf8)
152 -k|
--k|
--ke|
--kee|
--keep)
155 -r|
--r|
--re|
--res|
--reso|
--resol|
--resolv|
--resolve|
--resolved)
165 resolvemsg
=$
(echo "$1" |
sed -e "s/^--resolvemsg=//"); shift ;;
176 # If the dotest directory exists, but we have finished applying all the
177 # patches in them, clear it out.
178 if test -d "$dotest" &&
179 last
=$
(cat "$dotest/last") &&
180 next
=$
(cat "$dotest/next") &&
182 test "$next" -gt "$last"
189 test ",$#," = ",0," ||
190 die
"previous dotest directory $dotest still exists but mbox given."
193 # Make sure we are not given --skip nor --resolved
194 test ",$skip,$resolved," = ,,, ||
195 die
"Resolve operation not in progress, we are not resuming."
198 mkdir
-p "$dotest" ||
exit
200 git-mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
205 # -b, -s, -u, -k and --whitespace flags are kept for the
206 # resuming session after a patch failure.
207 # -3 and -i can and must be given when resuming.
208 echo "$binary" >"$dotest/binary"
209 echo " $ws" >"$dotest/whitespace"
210 echo "$sign" >"$dotest/sign"
211 echo "$utf8" >"$dotest/utf8"
212 echo "$keep" >"$dotest/keep"
213 echo 1 >"$dotest/next"
218 files
=$
(git-diff-index
--cached --name-only HEAD
) ||
exit
219 if [ "$files" ]; then
220 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
225 if test "$(cat "$dotest/binary
")" = t
227 binary
=--allow-binary-replacement
229 if test "$(cat "$dotest/utf8
")" = t
233 if test "$(cat "$dotest/keep
")" = t
237 ws
=`cat "$dotest/whitespace"`
238 if test "$(cat "$dotest/sign
")" = t
240 SIGNOFF
=`git-var GIT_COMMITTER_IDENT | sed -e '
242 s/^/Signed-off-by: /'
248 last
=`cat "$dotest/last"`
249 this
=`cat "$dotest/next"`
252 this
=`expr "$this" + 1`
256 if test "$this" -gt "$last"
263 while test "$this" -le "$last"
265 msgnum
=`printf "%0${prec}d" $this`
266 next
=`expr "$this" + 1`
267 test -f "$dotest/$msgnum" ||
{
273 # If we are not resuming, parse and extract the patch information
274 # into separate files:
275 # - info records the authorship and title
276 # - msg is the rest of commit log message
277 # - patch is the patch body.
279 # When we are resuming, these files are either already prepared
280 # by the user, or the user can tell us to do so by --resolved flag.
283 git-mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
284 <"$dotest/$msgnum" >"$dotest/info" ||
286 git-stripspace
< "$dotest/msg" > "$dotest/msg-clean"
290 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
291 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
292 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
294 if test -z "$GIT_AUTHOR_EMAIL"
296 echo "Patch does not have a valid e-mail address."
300 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
302 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
303 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
307 if test '' != "$SIGNOFF"
310 sed -ne '/^Signed-off-by: /p' \
311 "$dotest/msg-clean" |
315 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
316 test '' = "$LAST_SIGNED_OFF_BY" && echo
324 if test -s "$dotest/msg-clean"
327 cat "$dotest/msg-clean"
329 if test '' != "$ADD_SIGNOFF"
333 } >"$dotest/final-commit"
336 case "$resolved$interactive" in
338 # This is used only for interactive view option.
339 git-diff-index
-p --cached HEAD
>"$dotest/patch"
345 if test "$interactive" = t
348 die
"cannot be interactive without stdin connected to a terminal."
350 while test "$action" = again
352 echo "Commit Body is:"
353 echo "--------------------------"
354 cat "$dotest/final-commit"
355 echo "--------------------------"
356 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
360 [aA
]*) action
=yes interactive
= ;;
361 [nN
]*) action
=skip
;;
362 [eE
]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
365 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
373 if test $action = skip
379 if test -x "$GIT_DIR"/hooks
/applypatch-msg
381 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
386 echo "Applying '$SUBJECT'"
391 git-apply
$binary --index $ws "$dotest/patch"
395 # Resolved means the user did all the hard work, and
396 # we do not have to do any patch application. Just
397 # trust what the user has in the index file and the
400 changed
="$(git-diff-index --cached --name-only HEAD)"
401 if test '' = "$changed"
403 echo "No changes - did you forget update-index?"
404 stop_here_user_resolve
$this
406 unmerged
=$
(git-ls-files
-u)
407 if test -n "$unmerged"
409 echo "You still have unmerged paths in your index"
410 echo "did you forget update-index?"
411 stop_here_user_resolve
$this
417 if test $apply_status = 1 && test "$threeway" = t
421 # Applying the patch to an earlier tree and merging the
422 # result may have produced the same tree as ours.
423 changed
="$(git-diff-index --cached --name-only HEAD)"
424 if test '' = "$changed"
426 echo No changes
-- Patch already applied.
430 # clear apply_status -- we have successfully merged.
434 if test $apply_status != 0
436 echo Patch failed
at $msgnum.
437 stop_here_user_resolve
$this
440 if test -x "$GIT_DIR"/hooks
/pre-applypatch
442 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
445 tree
=$
(git-write-tree
) &&
446 echo Wrote tree
$tree &&
447 parent
=$
(git-rev-parse
--verify HEAD
) &&
448 commit
=$
(git-commit-tree
$tree -p $parent <"$dotest/final-commit") &&
449 echo Committed
: $commit &&
450 git-update-ref HEAD
$commit $parent ||
453 if test -x "$GIT_DIR"/hooks
/post-applypatch
455 "$GIT_DIR"/hooks
/post-applypatch