3 # Copyright (c) 2005, 2006 Junio C Hamano
5 USAGE
='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way] <mbox>
6 or, when resuming [--skip | --resolved]'
9 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
12 echo "$1" >"$dotest/next"
17 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
18 "$dotest/patch" "$dotest/info"
19 echo "$next" >"$dotest/next"
24 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
26 rm -fr "$dotest"/patch-merge-
*
27 mkdir
"$dotest/patch-merge-tmp-dir"
29 # First see if the patch records the index info that we can use.
30 if git-apply
-z --index-info "$dotest/patch" \
31 >"$dotest/patch-merge-index-info" 2>/dev
/null
&&
32 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
33 git-update-index
-z --index-info <"$dotest/patch-merge-index-info" &&
34 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
35 git-write-tree
>"$dotest/patch-merge-base+" &&
36 # index has the base tree now.
38 cd "$dotest/patch-merge-tmp-dir" &&
39 GIT_INDEX_FILE
="../patch-merge-tmp-index" \
40 GIT_OBJECT_DIRECTORY
="$O_OBJECT" \
41 git-apply
$binary --index <..
/patch
44 echo Using index info to reconstruct a base tree...
45 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
46 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
48 # Otherwise, try nearby trees that can be used to apply the
53 # Hoping the patch is against our recent commits...
54 git-rev-list
--max-count=$N HEAD
56 # or hoping the patch is against known tags...
57 git-ls-remote
--tags .
61 # See if we have it as a tree...
62 git-cat-file tree
"$base" >/dev
/null
2>&1 ||
continue
64 rm -fr "$dotest"/patch-merge-
* &&
65 mkdir
"$dotest/patch-merge-tmp-dir" ||
break
67 cd "$dotest/patch-merge-tmp-dir" &&
68 GIT_INDEX_FILE
=..
/patch-merge-tmp-index
&&
69 GIT_OBJECT_DIRECTORY
="$O_OBJECT" &&
70 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY
&&
71 git-read-tree
"$base" &&
72 git-apply
$binary --index &&
73 mv ..
/patch-merge-tmp-index ..
/patch-merge-index
&&
74 echo "$base" >..
/patch-merge-base
75 ) <"$dotest/patch" 2>/dev
/null
&& break
79 test -f "$dotest/patch-merge-index" &&
80 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git-write-tree
) &&
81 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
82 rm -fr "$dotest"/patch-merge-
* ||
exit 1
84 echo Falling back to patching base and
3-way merge...
86 # This is not so wrong. Depending on which base we picked,
87 # orig_tree may be wildly different from ours, but his_tree
88 # has the same set of wildly different changes in parts the
89 # patch did not touch, so resolve ends up cancelling them,
90 # saying that we reverted all those changes.
92 git-merge-resolve
$orig_tree -- HEAD
$his_tree ||
{
93 if test -d "$GIT_DIR/rr-cache"
97 echo Failed to merge
in the changes.
103 dotest
=.dotest sign
= utf8
= keep
= skip
= interactive
= resolved
= binary
= ws
=
105 while case "$#" in 0) break;; esac
108 -d=*|
--d=*|
--do=*|
--dot=*|
--dote=*|
--dotes=*|
--dotest=*)
109 dotest
=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
110 -d|
--d|
--do|
--dot|
--dote|
--dotes|
--dotest)
111 case "$#" in 1) usage
;; esac; shift
114 -i|
--i|
--in|
--int|
--inte|
--inter|
--intera|
--interac|
--interact|\
115 --interacti|
--interactiv|
--interactive)
116 interactive
=t
; shift ;;
118 -b|
--b|
--bi|
--bin|
--bina|
--binar|
--binary)
121 -3|
--3|
--3w|
--3wa|
--3way)
123 -s|
--s|
--si|
--sig|
--sign|
--signo|
--signof|
--signoff)
125 -u|
--u|
--ut|
--utf|
--utf8)
127 -k|
--k|
--ke|
--kee|
--keep)
130 -r|
--r|
--re|
--res|
--reso|
--resol|
--resolv|
--resolve|
--resolved)
148 # If the dotest directory exists, but we have finished applying all the
149 # patches in them, clear it out.
150 if test -d "$dotest" &&
151 last
=$
(cat "$dotest/last") &&
152 next
=$
(cat "$dotest/next") &&
154 test "$next" -gt "$last"
161 test ",$#," = ",0," ||
162 die
"previous dotest directory $dotest still exists but mbox given."
165 # Make sure we are not given --skip nor --resolved
166 test ",$skip,$resolved," = ,,, ||
167 die
"we are not resuming."
170 mkdir
-p "$dotest" ||
exit
172 git-mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
177 # -b, -s, -u, -k and --whitespace flags are kept for the
178 # resuming session after a patch failure.
179 # -3 and -i can and must be given when resuming.
180 echo "$binary" >"$dotest/binary"
181 echo " $ws" >"$dotest/whitespace"
182 echo "$sign" >"$dotest/sign"
183 echo "$utf8" >"$dotest/utf8"
184 echo "$keep" >"$dotest/keep"
185 echo 1 >"$dotest/next"
190 files
=$
(git-diff-index
--cached --name-only HEAD
) ||
exit
191 if [ "$files" ]; then
192 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
197 if test "$(cat "$dotest/binary
")" = t
199 binary
=--allow-binary-replacement
201 if test "$(cat "$dotest/utf8
")" = t
205 if test "$(cat "$dotest/keep
")" = t
209 ws
=`cat "$dotest/whitespace"`
210 if test "$(cat "$dotest/sign
")" = t
212 SIGNOFF
=`git-var GIT_COMMITTER_IDENT | sed -e '
214 s/^/Signed-off-by: /'
220 last
=`cat "$dotest/last"`
221 this
=`cat "$dotest/next"`
224 this
=`expr "$this" + 1`
228 if test "$this" -gt "$last"
235 while test "$this" -le "$last"
237 msgnum
=`printf "%0${prec}d" $this`
238 next
=`expr "$this" + 1`
239 test -f "$dotest/$msgnum" ||
{
245 # If we are not resuming, parse and extract the patch information
246 # into separate files:
247 # - info records the authorship and title
248 # - msg is the rest of commit log message
249 # - patch is the patch body.
251 # When we are resuming, these files are either already prepared
252 # by the user, or the user can tell us to do so by --resolved flag.
255 git-mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
256 <"$dotest/$msgnum" >"$dotest/info" ||
258 git-stripspace
< "$dotest/msg" > "$dotest/msg-clean"
262 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
263 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
264 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
266 if test -z "$GIT_AUTHOR_EMAIL"
268 echo "Patch does not have a valid e-mail address."
272 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
274 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
275 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
279 if test '' != "$SIGNOFF"
282 sed -ne '/^Signed-off-by: /p' \
283 "$dotest/msg-clean" |
287 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
288 test '' = "$LAST_SIGNED_OFF_BY" && echo
296 if test -s "$dotest/msg-clean"
299 cat "$dotest/msg-clean"
301 if test '' != "$ADD_SIGNOFF"
305 } >"$dotest/final-commit"
308 case "$resolved$interactive" in
310 # This is used only for interactive view option.
311 git-diff-index
-p --cached HEAD
>"$dotest/patch"
317 if test "$interactive" = t
320 die
"cannot be interactive without stdin connected to a terminal."
322 while test "$action" = again
324 echo "Commit Body is:"
325 echo "--------------------------"
326 cat "$dotest/final-commit"
327 echo "--------------------------"
328 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
332 [aA
]*) action
=yes interactive
= ;;
333 [nN
]*) action
=skip
;;
334 [eE
]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
337 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
345 if test $action = skip
351 if test -x "$GIT_DIR"/hooks
/applypatch-msg
353 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
358 echo "Applying '$SUBJECT'"
363 git-apply
$binary --index $ws "$dotest/patch"
367 # Resolved means the user did all the hard work, and
368 # we do not have to do any patch application. Just
369 # trust what the user has in the index file and the
372 changed
="$(git-diff-index --cached --name-only HEAD)"
373 if test '' = "$changed"
375 echo "No changes - did you forget update-index?"
382 if test $apply_status = 1 && test "$threeway" = t
386 # Applying the patch to an earlier tree and merging the
387 # result may have produced the same tree as ours.
388 changed
="$(git-diff-index --cached --name-only HEAD)"
389 if test '' = "$changed"
391 echo No changes
-- Patch already applied.
395 # clear apply_status -- we have successfully merged.
399 if test $apply_status != 0
401 echo Patch failed
at $msgnum.
405 if test -x "$GIT_DIR"/hooks
/pre-applypatch
407 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
410 tree
=$
(git-write-tree
) &&
411 echo Wrote tree
$tree &&
412 parent
=$
(git-rev-parse
--verify HEAD
) &&
413 commit
=$
(git-commit-tree
$tree -p $parent <"$dotest/final-commit") &&
414 echo Committed
: $commit &&
415 git-update-ref HEAD
$commit $parent ||
418 if test -x "$GIT_DIR"/hooks
/post-applypatch
420 "$GIT_DIR"/hooks
/post-applypatch