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"
18 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
19 "$dotest/patch" "$dotest/info"
20 echo "$next" >"$dotest/next"
25 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
27 rm -fr "$dotest"/patch-merge-*
28 mkdir "$dotest/patch-merge-tmp-dir"
30 # First see if the patch records the index info that we can use.
31 if git-apply -z --index-info "$dotest/patch" \
32 >"$dotest/patch-merge-index-info" 2>/dev/null &&
33 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
34 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
35 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
36 git-write-tree >"$dotest/patch-merge-base+" &&
37 # index has the base tree now.
39 cd "$dotest/patch-merge-tmp-dir" &&
40 GIT_INDEX_FILE="../patch-merge-tmp-index" \
41 GIT_OBJECT_DIRECTORY="$O_OBJECT" \
42 git-apply $binary --index <../patch
45 echo Using index info to reconstruct a base tree...
46 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
47 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
49 # Otherwise, try nearby trees that can be used to apply the
54 # Hoping the patch is against our recent commits...
55 git-rev-list --max-count=$N HEAD
57 # or hoping the patch is against known tags...
58 git-ls-remote --tags .
62 # See if we have it as a tree...
63 git-cat-file tree "$base" >/dev/null 2>&1 || continue
65 rm -fr "$dotest"/patch-merge-* &&
66 mkdir "$dotest/patch-merge-tmp-dir" || break
68 cd "$dotest/patch-merge-tmp-dir" &&
69 GIT_INDEX_FILE=../patch-merge-tmp-index &&
70 GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
71 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
72 git-read-tree "$base" &&
73 git-apply $binary --index &&
74 mv ../patch-merge-tmp-index ../patch-merge-index &&
75 echo "$base" >../patch-merge-base
76 ) <"$dotest/patch" 2>/dev/null && break
80 test -f "$dotest/patch-merge-index" &&
81 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
82 orig_tree=$(cat "$dotest/patch-merge-base") &&
83 rm -fr "$dotest"/patch-merge-* || exit 1
85 echo Falling back to patching base and 3-way merge...
87 # This is not so wrong. Depending on which base we picked,
88 # orig_tree may be wildly different from ours, but his_tree
89 # has the same set of wildly different changes in parts the
90 # patch did not touch, so resolve ends up cancelling them,
91 # saying that we reverted all those changes.
93 git-merge-resolve $orig_tree -- HEAD $his_tree || {
94 if test -d "$GIT_DIR/rr-cache"
98 echo Failed to merge in the changes.
104 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws=
106 while case "$#" in 0) break;; esac
109 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
110 dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
111 -d|--d|--do|--dot|--dote|--dotes|--dotest)
112 case "$#" in 1) usage ;; esac; shift
115 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
116 --interacti|--interactiv|--interactive)
117 interactive=t; shift ;;
119 -b|--b|--bi|--bin|--bina|--binar|--binary)
122 -3|--3|--3w|--3wa|--3way)
124 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
126 -u|--u|--ut|--utf|--utf8)
128 -k|--k|--ke|--kee|--keep)
131 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
149 # If the dotest directory exists, but we have finished applying all the
150 # patches in them, clear it out.
151 if test -d "$dotest" &&
152 last=$(cat "$dotest/last") &&
153 next=$(cat "$dotest/next") &&
155 test "$next" -gt "$last"
162 test ",$#," = ",0," ||
163 die "previous dotest directory $dotest still exists but mbox given."
166 # Make sure we are not given --skip nor --resolved
167 test ",$skip,$resolved," = ,,, ||
168 die "we are not resuming."
171 mkdir -p "$dotest" || exit
173 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
178 # -b, -s, -u, -k and --whitespace flags are kept for the
179 # resuming session after a patch failure.
180 # -3 and -i can and must be given when resuming.
181 echo "$binary" >"$dotest/binary"
182 echo " $ws" >"$dotest/whitespace"
183 echo "$sign" >"$dotest/sign"
184 echo "$utf8" >"$dotest/utf8"
185 echo "$keep" >"$dotest/keep"
186 echo 1 >"$dotest/next"
191 files=$(git-diff-index --cached --name-only HEAD) || exit
192 if [ "$files" ]; then
193 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
198 if test "$(cat "$dotest/binary")" = t
200 binary=--allow-binary-replacement
202 if test "$(cat "$dotest/utf8")" = t
206 if test "$(cat "$dotest/keep")" = t
210 ws=`cat "$dotest/whitespace"`
211 if test "$(cat "$dotest/sign")" = t
213 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
215 s/^/Signed-off-by: /'
221 last=`cat "$dotest/last"`
222 this=`cat "$dotest/next"`
225 this=`expr "$this" + 1`
229 if test "$this" -gt "$last"
236 while test "$this" -le "$last"
238 msgnum=`printf "%0${prec}d" $this`
239 next=`expr "$this" + 1`
240 test -f "$dotest/$msgnum" || {
246 # If we are not resuming, parse and extract the patch information
247 # into separate files:
248 # - info records the authorship and title
249 # - msg is the rest of commit log message
250 # - patch is the patch body.
252 # When we are resuming, these files are either already prepared
253 # by the user, or the user can tell us to do so by --resolved flag.
256 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
257 <"$dotest/$msgnum" >"$dotest/info" ||
259 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
263 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
264 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
265 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
267 if test -z "$GIT_AUTHOR_EMAIL"
269 echo "Patch does not have a valid e-mail address."
273 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
275 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
276 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
280 if test '' != "$SIGNOFF"
283 sed -ne '/^Signed-off-by: /p' \
284 "$dotest/msg-clean" |
288 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
289 test '' = "$LAST_SIGNED_OFF_BY" && echo
297 if test -s "$dotest/msg-clean"
300 cat "$dotest/msg-clean"
302 if test '' != "$ADD_SIGNOFF"
306 } >"$dotest/final-commit"
309 case "$resolved$interactive" in
311 # This is used only for interactive view option.
312 git-diff-index -p --cached HEAD >"$dotest/patch"
318 if test "$interactive" = t
321 die "cannot be interactive without stdin connected to a terminal."
323 while test "$action" = again
325 echo "Commit Body is:"
326 echo "--------------------------"
327 cat "$dotest/final-commit"
328 echo "--------------------------"
329 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
333 [aA]*) action=yes interactive= ;;
334 [nN]*) action=skip ;;
335 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
338 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
346 if test $action = skip
352 if test -x "$GIT_DIR"/hooks/applypatch-msg
354 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
359 echo "Applying '$SUBJECT'"
364 git-apply $binary --index $ws "$dotest/patch"
368 # Resolved means the user did all the hard work, and
369 # we do not have to do any patch application. Just
370 # trust what the user has in the index file and the
373 changed="$(git-diff-index --cached --name-only HEAD)"
374 if test '' = "$changed"
376 echo "No changes - did you forget update-index?"
383 if test $apply_status = 1 && test "$threeway" = t
387 # Applying the patch to an earlier tree and merging the
388 # result may have produced the same tree as ours.
389 changed="$(git-diff-index --cached --name-only HEAD)"
390 if test '' = "$changed"
392 echo No changes -- Patch already applied.
396 # clear apply_status -- we have successfully merged.
400 if test $apply_status != 0
402 echo Patch failed at $msgnum.
406 if test -x "$GIT_DIR"/hooks/pre-applypatch
408 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
411 tree=$(git-write-tree) &&
412 echo Wrote tree $tree &&
413 parent=$(git-rev-parse --verify HEAD) &&
414 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
415 echo Committed: $commit &&
416 git-update-ref HEAD $commit $parent ||
419 if test -x "$GIT_DIR"/hooks/post-applypatch
421 "$GIT_DIR"/hooks/post-applypatch