7 echo >&2 "usage: $0 [--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way] <mbox>"
8 echo >&2 " or, when resuming"
9 echo >&2 " $0 [--skip | --resolved]"
14 echo "$1" >"$dotest/next"
19 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
20 "$dotest/patch" "$dotest/info"
21 echo "$next" >"$dotest/next"
26 O_OBJECT
=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
28 rm -fr "$dotest"/patch-merge-
*
29 mkdir
"$dotest/patch-merge-tmp-dir"
31 # First see if the patch records the index info that we can use.
32 if git-apply
-z --index-info "$dotest/patch" \
33 >"$dotest/patch-merge-index-info" 2>/dev
/null
&&
34 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
35 git-update-index
-z --index-info <"$dotest/patch-merge-index-info" &&
36 GIT_INDEX_FILE
="$dotest/patch-merge-tmp-index" \
37 git-write-tree
>"$dotest/patch-merge-base+" &&
38 # index has the base tree now.
40 cd "$dotest/patch-merge-tmp-dir" &&
41 GIT_INDEX_FILE
="../patch-merge-tmp-index" \
42 GIT_OBJECT_DIRECTORY
="$O_OBJECT" \
43 git-apply
$binary --index <..
/patch
46 echo Using index info to reconstruct a base tree...
47 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
48 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
50 # Otherwise, try nearby trees that can be used to apply the
55 # Hoping the patch is against our recent commits...
56 git-rev-list
--max-count=$N HEAD
58 # or hoping the patch is against known tags...
59 git-ls-remote
--tags .
63 # See if we have it as a tree...
64 git-cat-file tree
"$base" >/dev
/null
2>&1 ||
continue
66 rm -fr "$dotest"/patch-merge-
* &&
67 mkdir
"$dotest/patch-merge-tmp-dir" ||
break
69 cd "$dotest/patch-merge-tmp-dir" &&
70 GIT_INDEX_FILE
=..
/patch-merge-tmp-index
&&
71 GIT_OBJECT_DIRECTORY
="$O_OBJECT" &&
72 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY
&&
73 git-read-tree
"$base" &&
74 git-apply
$binary --index &&
75 mv ..
/patch-merge-tmp-index ..
/patch-merge-index
&&
76 echo "$base" >..
/patch-merge-base
77 ) <"$dotest/patch" 2>/dev
/null
&& break
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 cancelling them,
92 # saying that we reverted all those changes.
94 git-merge-resolve
$orig_tree -- HEAD
$his_tree ||
{
95 echo Failed to merge
in the changes.
101 dotest
=.dotest sign
= utf8
= keep
= skip
= interactive
= resolved
= binary
=
103 while case "$#" in 0) break;; esac
106 -d=*|
--d=*|
--do=*|
--dot=*|
--dote=*|
--dotes=*|
--dotest=*)
107 dotest
=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
108 -d|
--d|
--do|
--dot|
--dote|
--dotes|
--dotest)
109 case "$#" in 1) usage
;; esac; shift
112 -i|
--i|
--in|
--int|
--inte|
--inter|
--intera|
--interac|
--interact|\
113 --interacti|
--interactiv|
--interactive)
114 interactive
=t
; shift ;;
116 -b|
--b|
--bi|
--bin|
--bina|
--binar|
--binary)
119 -3|
--3|
--3w|
--3wa|
--3way)
121 -s|
--s|
--si|
--sig|
--sign|
--signo|
--signof|
--signoff)
123 -u|
--u|
--ut|
--utf|
--utf8)
125 -k|
--k|
--ke|
--kee|
--keep)
128 -r|
--r|
--re|
--res|
--reso|
--resol|
--resolv|
--resolve|
--resolved)
143 # If the dotest directory exists, but we have finished applying all the
144 # patches in them, clear it out.
145 if test -d "$dotest" &&
146 last
=$
(cat "$dotest/last") &&
147 next
=$
(cat "$dotest/next") &&
149 test "$next" -gt "$last"
156 test ",$#," = ",0," ||
157 die
"previous dotest directory $dotest still exists but mbox given."
160 # Make sure we are not given --skip nor --resolved
161 test ",$skip,$resolved," = ,,, ||
162 die
"we are not resuming."
165 mkdir
-p "$dotest" ||
exit
167 git-mailsplit
-d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" ||
{
172 # -b, -s, -u and -k flags are kept for the resuming session after
174 # -3 and -i can and must be given when resuming.
175 echo "$binary" >"$dotest/binary"
176 echo "$sign" >"$dotest/sign"
177 echo "$utf8" >"$dotest/utf8"
178 echo "$keep" >"$dotest/keep"
179 echo 1 >"$dotest/next"
184 files
=$
(git-diff-index
--cached --name-only HEAD
) ||
exit
185 if [ "$files" ]; then
186 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
191 if test "$(cat "$dotest/binary
")" = t
193 binary
=--allow-binary-replacement
195 if test "$(cat "$dotest/utf8
")" = t
199 if test "$(cat "$dotest/keep
")" = t
203 if test "$(cat "$dotest/sign
")" = t
205 SIGNOFF
=`git-var GIT_COMMITTER_IDENT | sed -e '
207 s/^/Signed-off-by: /'
213 last
=`cat "$dotest/last"`
214 this
=`cat "$dotest/next"`
217 this
=`expr "$this" + 1`
220 if test "$this" -gt "$last"
227 while test "$this" -le "$last"
229 msgnum
=`printf "%0${prec}d" $this`
230 next
=`expr "$this" + 1`
231 test -f "$dotest/$msgnum" ||
{
236 # If we are not resuming, parse and extract the patch information
237 # into separate files:
238 # - info records the authorship and title
239 # - msg is the rest of commit log message
240 # - patch is the patch body.
242 # When we are resuming, these files are either already prepared
243 # by the user, or the user can tell us to do so by --resolved flag.
246 git-mailinfo
$keep $utf8 "$dotest/msg" "$dotest/patch" \
247 <"$dotest/$msgnum" >"$dotest/info" ||
249 git-stripspace
< "$dotest/msg" > "$dotest/msg-clean"
253 GIT_AUTHOR_NAME
="$(sed -n '/^Author/ s/Author: //p' "$dotest/info
")"
254 GIT_AUTHOR_EMAIL
="$(sed -n '/^Email/ s/Email: //p' "$dotest/info
")"
255 GIT_AUTHOR_DATE
="$(sed -n '/^Date/ s/Date: //p' "$dotest/info
")"
256 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
258 SUBJECT
="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info
")"
259 case "$keep_subject" in -k) SUBJECT
="[PATCH] $SUBJECT" ;; esac
263 if test '' != "$SIGNOFF"
266 sed -ne '/^Signed-off-by: /p' \
267 "$dotest/msg-clean" |
271 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
272 test '' = "$LAST_SIGNED_OFF_BY" && echo
280 if test -s "$dotest/msg-clean"
283 cat "$dotest/msg-clean"
285 if test '' != "$ADD_SIGNOFF"
289 } >"$dotest/final-commit"
292 case "$resolved,$interactive" in
294 # This is used only for interactive view option.
295 git-diff-index
-p --cached HEAD
>"$dotest/patch"
301 if test "$interactive" = t
304 die
"cannot be interactive without stdin connected to a terminal."
306 while test "$action" = again
308 echo "Commit Body is:"
309 echo "--------------------------"
310 cat "$dotest/final-commit"
311 echo "--------------------------"
312 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
316 [aA
]*) action
=yes interactive
= ;;
317 [nN
]*) action
=skip
;;
318 [eE
]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
321 LESS
=-S ${PAGER:-less} "$dotest/patch" ;;
329 if test $action = skip
335 if test -x "$GIT_DIR"/hooks
/applypatch-msg
337 "$GIT_DIR"/hooks
/applypatch-msg
"$dotest/final-commit" ||
342 echo "Applying '$SUBJECT'"
347 git-apply
$binary --index "$dotest/patch"
351 # Resolved means the user did all the hard work, and
352 # we do not have to do any patch application. Just
353 # trust what the user has in the index file and the
360 if test $apply_status = 1 && test "$threeway" = t
364 # Applying the patch to an earlier tree and merging the
365 # result may have produced the same tree as ours.
366 changed
="$(git-diff-index --cached --name-only -z HEAD)"
367 if test '' = "$changed"
369 echo No changes
-- Patch already applied.
373 # clear apply_status -- we have successfully merged.
377 if test $apply_status != 0
379 echo Patch failed
at $msgnum.
383 if test -x "$GIT_DIR"/hooks
/pre-applypatch
385 "$GIT_DIR"/hooks
/pre-applypatch || stop_here
$this
388 tree
=$
(git-write-tree
) &&
389 echo Wrote tree
$tree &&
390 parent
=$
(git-rev-parse
--verify HEAD
) &&
391 commit
=$
(git-commit-tree
$tree -p $parent <"$dotest/final-commit") &&
392 echo Committed
: $commit &&
393 git-update-ref HEAD
$commit $parent ||
396 if test -x "$GIT_DIR"/hooks
/post-applypatch
398 "$GIT_DIR"/hooks
/post-applypatch