5 USAGE='[--signoff] [--dotest=<dir>] [--utf8] [--binary] [--3way] <mbox>
6 or, when resuming [--skip | --resolved]'
10 echo "$1" >"$dotest/next"
15 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
16 "$dotest/patch" "$dotest/info"
17 echo "$next" >"$dotest/next"
22 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
24 rm -fr "$dotest"/patch-merge-*
25 mkdir "$dotest/patch-merge-tmp-dir"
27 # First see if the patch records the index info that we can use.
28 if git-apply -z --index-info "$dotest/patch" \
29 >"$dotest/patch-merge-index-info" 2>/dev/null &&
30 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
31 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
32 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
33 git-write-tree >"$dotest/patch-merge-base+" &&
34 # index has the base tree now.
36 cd "$dotest/patch-merge-tmp-dir" &&
37 GIT_INDEX_FILE="../patch-merge-tmp-index" \
38 GIT_OBJECT_DIRECTORY="$O_OBJECT" \
39 git-apply $binary --index <../patch
42 echo Using index info to reconstruct a base tree...
43 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
44 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
46 # Otherwise, try nearby trees that can be used to apply the
51 # Hoping the patch is against our recent commits...
52 git-rev-list --max-count=$N HEAD
54 # or hoping the patch is against known tags...
55 git-ls-remote --tags .
59 # See if we have it as a tree...
60 git-cat-file tree "$base" >/dev/null 2>&1 || continue
62 rm -fr "$dotest"/patch-merge-* &&
63 mkdir "$dotest/patch-merge-tmp-dir" || break
65 cd "$dotest/patch-merge-tmp-dir" &&
66 GIT_INDEX_FILE=../patch-merge-tmp-index &&
67 GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
68 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
69 git-read-tree "$base" &&
70 git-apply $binary --index &&
71 mv ../patch-merge-tmp-index ../patch-merge-index &&
72 echo "$base" >../patch-merge-base
73 ) <"$dotest/patch" 2>/dev/null && break
77 test -f "$dotest/patch-merge-index" &&
78 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
79 orig_tree=$(cat "$dotest/patch-merge-base") &&
80 rm -fr "$dotest"/patch-merge-* || exit 1
82 echo Falling back to patching base and 3-way merge...
84 # This is not so wrong. Depending on which base we picked,
85 # orig_tree may be wildly different from ours, but his_tree
86 # has the same set of wildly different changes in parts the
87 # patch did not touch, so resolve ends up cancelling them,
88 # saying that we reverted all those changes.
90 git-merge-resolve $orig_tree -- HEAD $his_tree || {
92 echo Failed to merge in the changes.
98 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary=
100 while case "$#" in 0) break;; esac
103 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
104 dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
105 -d|--d|--do|--dot|--dote|--dotes|--dotest)
106 case "$#" in 1) usage ;; esac; shift
109 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
110 --interacti|--interactiv|--interactive)
111 interactive=t; shift ;;
113 -b|--b|--bi|--bin|--bina|--binar|--binary)
116 -3|--3|--3w|--3wa|--3way)
118 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
120 -u|--u|--ut|--utf|--utf8)
122 -k|--k|--ke|--kee|--keep)
125 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
140 # If the dotest directory exists, but we have finished applying all the
141 # patches in them, clear it out.
142 if test -d "$dotest" &&
143 last=$(cat "$dotest/last") &&
144 next=$(cat "$dotest/next") &&
146 test "$next" -gt "$last"
153 test ",$#," = ",0," ||
154 die "previous dotest directory $dotest still exists but mbox given."
157 # Make sure we are not given --skip nor --resolved
158 test ",$skip,$resolved," = ,,, ||
159 die "we are not resuming."
162 mkdir -p "$dotest" || exit
164 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
169 # -b, -s, -u and -k flags are kept for the resuming session after
171 # -3 and -i can and must be given when resuming.
172 echo "$binary" >"$dotest/binary"
173 echo "$sign" >"$dotest/sign"
174 echo "$utf8" >"$dotest/utf8"
175 echo "$keep" >"$dotest/keep"
176 echo 1 >"$dotest/next"
181 files=$(git-diff-index --cached --name-only HEAD) || exit
182 if [ "$files" ]; then
183 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
188 if test "$(cat "$dotest/binary")" = t
190 binary=--allow-binary-replacement
192 if test "$(cat "$dotest/utf8")" = t
196 if test "$(cat "$dotest/keep")" = t
200 if test "$(cat "$dotest/sign")" = t
202 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
204 s/^/Signed-off-by: /'
210 last=`cat "$dotest/last"`
211 this=`cat "$dotest/next"`
214 this=`expr "$this" + 1`
218 if test "$this" -gt "$last"
225 while test "$this" -le "$last"
227 msgnum=`printf "%0${prec}d" $this`
228 next=`expr "$this" + 1`
229 test -f "$dotest/$msgnum" || {
235 # If we are not resuming, parse and extract the patch information
236 # into separate files:
237 # - info records the authorship and title
238 # - msg is the rest of commit log message
239 # - patch is the patch body.
241 # When we are resuming, these files are either already prepared
242 # by the user, or the user can tell us to do so by --resolved flag.
245 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
246 <"$dotest/$msgnum" >"$dotest/info" ||
248 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
252 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
253 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
254 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
256 if test -z "$GIT_AUTHOR_EMAIL"
258 echo "Patch does not have a valid e-mail address."
262 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
264 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
265 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
269 if test '' != "$SIGNOFF"
272 sed -ne '/^Signed-off-by: /p' \
273 "$dotest/msg-clean" |
277 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
278 test '' = "$LAST_SIGNED_OFF_BY" && echo
286 if test -s "$dotest/msg-clean"
289 cat "$dotest/msg-clean"
291 if test '' != "$ADD_SIGNOFF"
295 } >"$dotest/final-commit"
298 case "$resolved,$interactive" in
300 # This is used only for interactive view option.
301 git-diff-index -p --cached HEAD >"$dotest/patch"
307 if test "$interactive" = t
310 die "cannot be interactive without stdin connected to a terminal."
312 while test "$action" = again
314 echo "Commit Body is:"
315 echo "--------------------------"
316 cat "$dotest/final-commit"
317 echo "--------------------------"
318 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
322 [aA]*) action=yes interactive= ;;
323 [nN]*) action=skip ;;
324 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
327 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
335 if test $action = skip
341 if test -x "$GIT_DIR"/hooks/applypatch-msg
343 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
348 echo "Applying '$SUBJECT'"
353 git-apply $binary --index "$dotest/patch"
357 # Resolved means the user did all the hard work, and
358 # we do not have to do any patch application. Just
359 # trust what the user has in the index file and the
366 if test $apply_status = 1 && test "$threeway" = t
370 # Applying the patch to an earlier tree and merging the
371 # result may have produced the same tree as ours.
372 changed="$(git-diff-index --cached --name-only -z HEAD)"
373 if test '' = "$changed"
375 echo No changes -- Patch already applied.
379 # clear apply_status -- we have successfully merged.
383 if test $apply_status != 0
385 echo Patch failed at $msgnum.
389 if test -x "$GIT_DIR"/hooks/pre-applypatch
391 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
394 tree=$(git-write-tree) &&
395 echo Wrote tree $tree &&
396 parent=$(git-rev-parse --verify HEAD) &&
397 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
398 echo Committed: $commit &&
399 git-update-ref HEAD $commit $parent ||
402 if test -x "$GIT_DIR"/hooks/post-applypatch
404 "$GIT_DIR"/hooks/post-applypatch