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 cmdline
=$
(basename $0)
19 if test '' != "$interactive"
23 if test '' != "$threeway"
27 if test '.dotest' != "$dotest"
29 cmdline
="$cmdline -d=$dotest"
31 echo "When you have resolved this problem run \"$cmdline --resolved\"."
32 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
38 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
39 "$dotest/patch" "$dotest/info"
40 echo "$next" >"$dotest/next"
45 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
47 rm -fr "$dotest"/patch-merge-
*
48 mkdir
"$dotest/patch-merge-tmp-dir"
50 # First see if the patch records the index info that we can use.
51 if git-apply
-z --index-info "$dotest/patch" \
52 >"$dotest/patch-merge-index-info" 2>/dev
/null
&&
53 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
54 git-update-index
-z --index-info <"$dotest/patch-merge-index-info" &&
55 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
56 git-write-tree
>"$dotest/patch-merge-base+" &&
57 # index has the base tree now.
58 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
59 git-apply
$binary --cached <"$dotest/patch"
61 echo Using index info to reconstruct a base tree...
62 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
63 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
66 test -f "$dotest/patch-merge-index" &&
67 his_tree
=$
(GIT_INDEX_FILE
="$dotest/patch-merge-index" git-write-tree
) &&
68 orig_tree
=$
(cat "$dotest/patch-merge-base") &&
69 rm -fr "$dotest"/patch-merge-
* ||
exit 1
71 echo Falling back to patching base and
3-way merge...
73 # This is not so wrong. Depending on which base we picked,
74 # orig_tree may be wildly different from ours, but his_tree
75 # has the same set of wildly different changes in parts the
76 # patch did not touch, so resolve ends up cancelling them,
77 # saying that we reverted all those changes.
79 git-merge-resolve
$orig_tree -- HEAD
$his_tree ||
{
80 if test -d "$GIT_DIR/rr-cache"
84 echo Failed to merge
in the changes.
90 dotest
=.dotest sign
= utf8
= keep
= skip
= interactive
= resolved
= binary
= ws
=
92 while case "$#" in 0) break;; esac
95 -d=*|
--d=*|
--do=*|
--dot=*|
--dote=*|
--dotes=*|
--dotest=*)
96 dotest
=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
97 -d|
--d|
--do|
--dot|
--dote|
--dotes|
--dotest)
98 case "$#" in 1) usage
;; esac; shift
101 -i|
--i|
--in|
--int|
--inte|
--inter|
--intera|
--interac|
--interact|\
102 --interacti|
--interactiv|
--interactive)
103 interactive
=t
; shift ;;
105 -b|
--b|
--bi|
--bin|
--bina|
--binar|
--binary)
108 -3|
--3|
--3w|
--3wa|
--3way)
110 -s|
--s|
--si|
--sig|
--sign|
--signo|
--signof|
--signoff)
112 -u|
--u|
--ut|
--utf|
--utf8)
114 -k|
--k|
--ke|
--kee|
--keep)
117 -r|
--r|
--re|
--res|
--reso|
--resol|
--resolv|
--resolve|
--resolved)
135 # If the dotest directory exists, but we have finished applying all the
136 # patches in them, clear it out.
137 if test -d "$dotest" &&
138 last
=$
(cat "$dotest/last") &&
139 next
=$
(cat "$dotest/next") &&
141 test "$next" -gt "$last"
148 test ",$#," = ",0," ||
149 die
"previous dotest directory $dotest still exists but mbox given."
152 # Make sure we are not given --skip nor --resolved
153 test ",$skip,$resolved," = ,,, ||
154 die
"we are not resuming."
157 mkdir
-p "$dotest" ||
exit
159 git-mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
164 # -b, -s, -u, -k and --whitespace flags are kept for the
165 # resuming session after a patch failure.
166 # -3 and -i can and must be given when resuming.
167 echo "$binary" >"$dotest/binary"
168 echo " $ws" >"$dotest/whitespace"
169 echo "$sign" >"$dotest/sign"
170 echo "$utf8" >"$dotest/utf8"
171 echo "$keep" >"$dotest/keep"
172 echo 1 >"$dotest/next"
177 files
=$
(git-diff-index
--cached --name-only HEAD
) ||
exit
178 if [ "$files" ]; then
179 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
184 if test "$(cat "$dotest/binary
")" = t
186 binary
=--allow-binary-replacement
188 if test "$(cat "$dotest/utf8
")" = t
192 if test "$(cat "$dotest/keep
")" = t
196 ws
=`cat "$dotest/whitespace"`
197 if test "$(cat "$dotest/sign
")" = t
199 SIGNOFF
=`git-var GIT_COMMITTER_IDENT | sed -e '
201 s/^/Signed-off-by: /'
207 last
=`cat "$dotest/last"`
208 this
=`cat "$dotest/next"`
211 this
=`expr "$this" + 1`
215 if test "$this" -gt "$last"
222 while test "$this" -le "$last"
224 msgnum
=`printf "%0${prec}d" $this`
225 next
=`expr "$this" + 1`
226 test -f "$dotest/$msgnum" ||
{
232 # If we are not resuming, parse and extract the patch information
233 # into separate files:
234 # - info records the authorship and title
235 # - msg is the rest of commit log message
236 # - patch is the patch body.
238 # When we are resuming, these files are either already prepared
239 # by the user, or the user can tell us to do so by --resolved flag.
242 git-mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
243 <"$dotest/$msgnum" >"$dotest/info" ||
245 git-stripspace
< "$dotest/msg" > "$dotest/msg-clean"
249 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
250 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
251 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
253 if test -z "$GIT_AUTHOR_EMAIL"
255 echo "Patch does not have a valid e-mail address."
259 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
261 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
262 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
266 if test '' != "$SIGNOFF"
269 sed -ne '/^Signed-off-by: /p' \
270 "$dotest/msg-clean" |
274 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
275 test '' = "$LAST_SIGNED_OFF_BY" && echo
283 if test -s "$dotest/msg-clean"
286 cat "$dotest/msg-clean"
288 if test '' != "$ADD_SIGNOFF"
292 } >"$dotest/final-commit"
295 case "$resolved$interactive" in
297 # This is used only for interactive view option.
298 git-diff-index
-p --cached HEAD
>"$dotest/patch"
304 if test "$interactive" = t
307 die
"cannot be interactive without stdin connected to a terminal."
309 while test "$action" = again
311 echo "Commit Body is:"
312 echo "--------------------------"
313 cat "$dotest/final-commit"
314 echo "--------------------------"
315 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
319 [aA
]*) action
=yes interactive
= ;;
320 [nN
]*) action
=skip
;;
321 [eE
]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
324 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
332 if test $action = skip
338 if test -x "$GIT_DIR"/hooks
/applypatch-msg
340 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
345 echo "Applying '$SUBJECT'"
350 git-apply
$binary --index $ws "$dotest/patch"
354 # Resolved means the user did all the hard work, and
355 # we do not have to do any patch application. Just
356 # trust what the user has in the index file and the
359 changed
="$(git-diff-index --cached --name-only HEAD)"
360 if test '' = "$changed"
362 echo "No changes - did you forget update-index?"
363 stop_here_user_resolve
$this
365 unmerged
=$
(git-ls-files
-u)
366 if test -n "$unmerged"
368 echo "You still have unmerged paths in your index"
369 echo "did you forget update-index?"
370 stop_here_user_resolve
$this
376 if test $apply_status = 1 && test "$threeway" = t
380 # Applying the patch to an earlier tree and merging the
381 # result may have produced the same tree as ours.
382 changed
="$(git-diff-index --cached --name-only HEAD)"
383 if test '' = "$changed"
385 echo No changes
-- Patch already applied.
389 # clear apply_status -- we have successfully merged.
393 if test $apply_status != 0
395 echo Patch failed
at $msgnum.
396 stop_here_user_resolve
$this
399 if test -x "$GIT_DIR"/hooks
/pre-applypatch
401 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
404 tree
=$
(git-write-tree
) &&
405 echo Wrote tree
$tree &&
406 parent
=$
(git-rev-parse
--verify HEAD
) &&
407 commit
=$
(git-commit-tree
$tree -p $parent <"$dotest/final-commit") &&
408 echo Committed
: $commit &&
409 git-update-ref HEAD
$commit $parent ||
412 if test -x "$GIT_DIR"/hooks
/post-applypatch
414 "$GIT_DIR"/hooks
/post-applypatch