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]'
11 git var GIT_COMMITTER_IDENT >/dev/null || exit
14 echo "$1" >"$dotest/next"
18 stop_here_user_resolve () {
19 if [ -n "$resolvemsg" ]; then
23 cmdline=$(basename $0)
24 if test '' != "$interactive"
28 if test '' != "$threeway"
32 if test '.dotest' != "$dotest"
34 cmdline="$cmdline -d=$dotest"
36 echo "When you have resolved this problem run \"$cmdline --resolved\"."
37 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
43 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
44 "$dotest/patch" "$dotest/info"
45 echo "$next" >"$dotest/next"
51 echo "Cannot fall back to three-way merge."
56 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
58 rm -fr "$dotest"/patch-merge-*
59 mkdir "$dotest/patch-merge-tmp-dir"
61 # First see if the patch records the index info that we can use.
62 git-apply -z --index-info "$dotest/patch" \
63 >"$dotest/patch-merge-index-info" &&
64 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
65 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
66 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
67 git-write-tree >"$dotest/patch-merge-base+" ||
68 cannot_fallback "Patch does not record usable index information."
70 echo Using index info to reconstruct a base tree...
71 if GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
72 git-apply $binary --cached <"$dotest/patch"
74 mv "$dotest/patch-merge-base+" "$dotest/patch-merge-base"
75 mv "$dotest/patch-merge-tmp-index" "$dotest/patch-merge-index"
77 cannot_fallback "Did you hand edit your patch?
78 It does not apply to blobs recorded in its index."
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 recursive ends up canceling them,
92 # saying that we reverted all those changes.
94 eval GITHEAD_$his_tree='"$SUBJECT"'
95 export GITHEAD_$his_tree
96 git-merge-recursive $orig_tree -- HEAD $his_tree || {
97 if test -d "$GIT_DIR/rr-cache"
101 echo Failed to merge in the changes.
104 unset GITHEAD_$his_tree
108 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws= resolvemsg=
110 while case "$#" in 0) break;; esac
113 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
114 dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
115 -d|--d|--do|--dot|--dote|--dotes|--dotest)
116 case "$#" in 1) usage ;; esac; shift
119 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
120 --interacti|--interactiv|--interactive)
121 interactive=t; shift ;;
123 -b|--b|--bi|--bin|--bina|--binar|--binary)
126 -3|--3|--3w|--3wa|--3way)
128 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
130 -u|--u|--ut|--utf|--utf8)
132 -k|--k|--ke|--kee|--keep)
135 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
145 resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//"); shift ;;
156 # If the dotest directory exists, but we have finished applying all the
157 # patches in them, clear it out.
158 if test -d "$dotest" &&
159 last=$(cat "$dotest/last") &&
160 next=$(cat "$dotest/next") &&
162 test "$next" -gt "$last"
169 case "$#,$skip$resolved" in
171 # Explicit resume command and we do not have file, so
175 # No file input but without resume parameters; catch
176 # user error to feed us a patch from standard input
177 # when there is already .dotest. This is somewhat
178 # unreliable -- stdin could be /dev/null for example
179 # and the caller did not intend to feed us a patch but
180 # wanted to continue unattended.
187 die "previous dotest directory $dotest still exists but mbox given."
190 # Make sure we are not given --skip nor --resolved
191 test ",$skip,$resolved," = ,,, ||
192 die "Resolve operation not in progress, we are not resuming."
195 mkdir -p "$dotest" || exit
197 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
202 # -b, -s, -u, -k and --whitespace flags are kept for the
203 # resuming session after a patch failure.
204 # -3 and -i can and must be given when resuming.
205 echo "$binary" >"$dotest/binary"
206 echo " $ws" >"$dotest/whitespace"
207 echo "$sign" >"$dotest/sign"
208 echo "$utf8" >"$dotest/utf8"
209 echo "$keep" >"$dotest/keep"
210 echo 1 >"$dotest/next"
215 files=$(git-diff-index --cached --name-only HEAD) || exit
216 if [ "$files" ]; then
217 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
222 if test "$(cat "$dotest/binary")" = t
224 binary=--allow-binary-replacement
226 if test "$(cat "$dotest/utf8")" = t
230 if test "$(cat "$dotest/keep")" = t
234 ws=`cat "$dotest/whitespace"`
235 if test "$(cat "$dotest/sign")" = t
237 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
239 s/^/Signed-off-by: /'
245 last=`cat "$dotest/last"`
246 this=`cat "$dotest/next"`
249 if test -d "$GIT_DIR/rr-cache"
253 this=`expr "$this" + 1`
257 if test "$this" -gt "$last"
264 while test "$this" -le "$last"
266 msgnum=`printf "%0${prec}d" $this`
267 next=`expr "$this" + 1`
268 test -f "$dotest/$msgnum" || {
274 # If we are not resuming, parse and extract the patch information
275 # into separate files:
276 # - info records the authorship and title
277 # - msg is the rest of commit log message
278 # - patch is the patch body.
280 # When we are resuming, these files are either already prepared
281 # by the user, or the user can tell us to do so by --resolved flag.
284 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
285 <"$dotest/$msgnum" >"$dotest/info" ||
287 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
291 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
292 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
293 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
295 if test -z "$GIT_AUTHOR_EMAIL"
297 echo "Patch does not have a valid e-mail address."
301 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
303 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
304 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
308 if test '' != "$SIGNOFF"
311 sed -ne '/^Signed-off-by: /p' \
312 "$dotest/msg-clean" |
316 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
317 test '' = "$LAST_SIGNED_OFF_BY" && echo
325 if test -s "$dotest/msg-clean"
328 cat "$dotest/msg-clean"
330 if test '' != "$ADD_SIGNOFF"
334 } >"$dotest/final-commit"
337 case "$resolved$interactive" in
339 # This is used only for interactive view option.
340 git-diff-index -p --cached HEAD >"$dotest/patch"
346 if test "$interactive" = t
349 die "cannot be interactive without stdin connected to a terminal."
351 while test "$action" = again
353 echo "Commit Body is:"
354 echo "--------------------------"
355 cat "$dotest/final-commit"
356 echo "--------------------------"
357 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
361 [aA]*) action=yes interactive= ;;
362 [nN]*) action=skip ;;
363 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
366 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
374 if test $action = skip
380 if test -x "$GIT_DIR"/hooks/applypatch-msg
382 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
387 echo "Applying '$SUBJECT'"
392 git-apply $binary --index $ws "$dotest/patch"
396 # Resolved means the user did all the hard work, and
397 # we do not have to do any patch application. Just
398 # trust what the user has in the index file and the
401 changed="$(git-diff-index --cached --name-only HEAD)"
402 if test '' = "$changed"
404 echo "No changes - did you forget to use 'git add'?"
405 stop_here_user_resolve $this
407 unmerged=$(git-ls-files -u)
408 if test -n "$unmerged"
410 echo "You still have unmerged paths in your index"
411 echo "did you forget to use 'git add'?"
412 stop_here_user_resolve $this
415 if test -d "$GIT_DIR/rr-cache"
422 if test $apply_status = 1 && test "$threeway" = t
426 # Applying the patch to an earlier tree and merging the
427 # result may have produced the same tree as ours.
428 changed="$(git-diff-index --cached --name-only HEAD)"
429 if test '' = "$changed"
431 echo No changes -- Patch already applied.
435 # clear apply_status -- we have successfully merged.
439 if test $apply_status != 0
441 echo Patch failed at $msgnum.
442 stop_here_user_resolve $this
445 if test -x "$GIT_DIR"/hooks/pre-applypatch
447 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
450 tree=$(git-write-tree) &&
451 echo Wrote tree $tree &&
452 parent=$(git-rev-parse --verify HEAD) &&
453 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
454 echo Committed: $commit &&
455 git-update-ref -m "$GIT_REFLOG_ACTION: $SUBJECT" HEAD $commit $parent ||
458 if test -x "$GIT_DIR"/hooks/post-applypatch
460 "$GIT_DIR"/hooks/post-applypatch