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 if [ -n "$resolvemsg" ]; then
22 cmdline=$(basename $0)
23 if test '' != "$interactive"
27 if test '' != "$threeway"
31 if test '.dotest' != "$dotest"
33 cmdline="$cmdline -d=$dotest"
35 echo "When you have resolved this problem run \"$cmdline --resolved\"."
36 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
42 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
43 "$dotest/patch" "$dotest/info"
44 echo "$next" >"$dotest/next"
49 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
51 rm -fr "$dotest"/patch-merge-*
52 mkdir "$dotest/patch-merge-tmp-dir"
54 # First see if the patch records the index info that we can use.
55 if git-apply -z --index-info "$dotest/patch" \
56 >"$dotest/patch-merge-index-info" 2>/dev/null &&
57 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
58 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
59 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
60 git-write-tree >"$dotest/patch-merge-base+" &&
61 # index has the base tree now.
62 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
63 git-apply $binary --cached <"$dotest/patch"
65 echo Using index info to reconstruct a base tree...
66 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
67 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
70 test -f "$dotest/patch-merge-index" &&
71 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
72 orig_tree=$(cat "$dotest/patch-merge-base") &&
73 rm -fr "$dotest"/patch-merge-* || exit 1
75 echo Falling back to patching base and 3-way merge...
77 # This is not so wrong. Depending on which base we picked,
78 # orig_tree may be wildly different from ours, but his_tree
79 # has the same set of wildly different changes in parts the
80 # patch did not touch, so resolve ends up canceling them,
81 # saying that we reverted all those changes.
83 git-merge-resolve $orig_tree -- HEAD $his_tree || {
84 if test -d "$GIT_DIR/rr-cache"
88 echo Failed to merge in the changes.
95 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws= resolvemsg=
97 while case "$#" in 0) break;; esac
100 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
101 dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
102 -d|--d|--do|--dot|--dote|--dotes|--dotest)
103 case "$#" in 1) usage ;; esac; shift
106 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
107 --interacti|--interactiv|--interactive)
108 interactive=t; shift ;;
110 -b|--b|--bi|--bin|--bina|--binar|--binary)
113 -3|--3|--3w|--3wa|--3way)
115 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
117 -u|--u|--ut|--utf|--utf8)
119 -k|--k|--ke|--kee|--keep)
122 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
132 resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//"); shift ;;
135 rloga=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
146 # If the dotest directory exists, but we have finished applying all the
147 # patches in them, clear it out.
148 if test -d "$dotest" &&
149 last=$(cat "$dotest/last") &&
150 next=$(cat "$dotest/next") &&
152 test "$next" -gt "$last"
159 test ",$#," = ",0," ||
160 die "previous dotest directory $dotest still exists but mbox given."
163 # Make sure we are not given --skip nor --resolved
164 test ",$skip,$resolved," = ,,, ||
165 die "Resolve operation not in progress, we are not resuming."
168 mkdir -p "$dotest" || exit
170 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
175 # -b, -s, -u, -k and --whitespace flags are kept for the
176 # resuming session after a patch failure.
177 # -3 and -i can and must be given when resuming.
178 echo "$binary" >"$dotest/binary"
179 echo " $ws" >"$dotest/whitespace"
180 echo "$sign" >"$dotest/sign"
181 echo "$utf8" >"$dotest/utf8"
182 echo "$keep" >"$dotest/keep"
183 echo 1 >"$dotest/next"
188 files=$(git-diff-index --cached --name-only HEAD) || exit
189 if [ "$files" ]; then
190 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
195 if test "$(cat "$dotest/binary")" = t
197 binary=--allow-binary-replacement
199 if test "$(cat "$dotest/utf8")" = t
203 if test "$(cat "$dotest/keep")" = t
207 ws=`cat "$dotest/whitespace"`
208 if test "$(cat "$dotest/sign")" = t
210 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
212 s/^/Signed-off-by: /'
218 last=`cat "$dotest/last"`
219 this=`cat "$dotest/next"`
222 this=`expr "$this" + 1`
226 if test "$this" -gt "$last"
233 while test "$this" -le "$last"
235 msgnum=`printf "%0${prec}d" $this`
236 next=`expr "$this" + 1`
237 test -f "$dotest/$msgnum" || {
243 # If we are not resuming, parse and extract the patch information
244 # into separate files:
245 # - info records the authorship and title
246 # - msg is the rest of commit log message
247 # - patch is the patch body.
249 # When we are resuming, these files are either already prepared
250 # by the user, or the user can tell us to do so by --resolved flag.
253 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
254 <"$dotest/$msgnum" >"$dotest/info" ||
256 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
260 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
261 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
262 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
264 if test -z "$GIT_AUTHOR_EMAIL"
266 echo "Patch does not have a valid e-mail address."
270 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
272 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
273 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
277 if test '' != "$SIGNOFF"
280 sed -ne '/^Signed-off-by: /p' \
281 "$dotest/msg-clean" |
285 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
286 test '' = "$LAST_SIGNED_OFF_BY" && echo
294 if test -s "$dotest/msg-clean"
297 cat "$dotest/msg-clean"
299 if test '' != "$ADD_SIGNOFF"
303 } >"$dotest/final-commit"
306 case "$resolved$interactive" in
308 # This is used only for interactive view option.
309 git-diff-index -p --cached HEAD >"$dotest/patch"
315 if test "$interactive" = t
318 die "cannot be interactive without stdin connected to a terminal."
320 while test "$action" = again
322 echo "Commit Body is:"
323 echo "--------------------------"
324 cat "$dotest/final-commit"
325 echo "--------------------------"
326 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
330 [aA]*) action=yes interactive= ;;
331 [nN]*) action=skip ;;
332 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
335 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
343 if test $action = skip
349 if test -x "$GIT_DIR"/hooks/applypatch-msg
351 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
356 echo "Applying '$SUBJECT'"
361 git-apply $binary --index $ws "$dotest/patch"
365 # Resolved means the user did all the hard work, and
366 # we do not have to do any patch application. Just
367 # trust what the user has in the index file and the
370 changed="$(git-diff-index --cached --name-only HEAD)"
371 if test '' = "$changed"
373 echo "No changes - did you forget update-index?"
374 stop_here_user_resolve $this
376 unmerged=$(git-ls-files -u)
377 if test -n "$unmerged"
379 echo "You still have unmerged paths in your index"
380 echo "did you forget update-index?"
381 stop_here_user_resolve $this
387 if test $apply_status = 1 && test "$threeway" = t
391 # Applying the patch to an earlier tree and merging the
392 # result may have produced the same tree as ours.
393 changed="$(git-diff-index --cached --name-only HEAD)"
394 if test '' = "$changed"
396 echo No changes -- Patch already applied.
400 # clear apply_status -- we have successfully merged.
404 if test $apply_status != 0
406 echo Patch failed at $msgnum.
407 stop_here_user_resolve $this
410 if test -x "$GIT_DIR"/hooks/pre-applypatch
412 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
415 tree=$(git-write-tree) &&
416 echo Wrote tree $tree &&
417 parent=$(git-rev-parse --verify HEAD) &&
418 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
419 echo Committed: $commit &&
420 git-update-ref -m "$rloga: $SUBJECT" HEAD $commit $parent ||
423 if test -x "$GIT_DIR"/hooks/post-applypatch
425 "$GIT_DIR"/hooks/post-applypatch