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 cancelling 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.
94 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws= resolvemsg=
96 while case "$#" in 0) break;; esac
99 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
100 dotest=`expr "z$1" : 'z-[^=]*=\(.*\)'`; shift ;;
101 -d|--d|--do|--dot|--dote|--dotes|--dotest)
102 case "$#" in 1) usage ;; esac; shift
105 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
106 --interacti|--interactiv|--interactive)
107 interactive=t; shift ;;
109 -b|--b|--bi|--bin|--bina|--binar|--binary)
112 -3|--3|--3w|--3wa|--3way)
114 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
116 -u|--u|--ut|--utf|--utf8)
118 -k|--k|--ke|--kee|--keep)
121 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
131 resolvemsg=$(echo "$1" | sed -e "s/^--resolvemsg=//"); shift ;;
142 # If the dotest directory exists, but we have finished applying all the
143 # patches in them, clear it out.
144 if test -d "$dotest" &&
145 last=$(cat "$dotest/last") &&
146 next=$(cat "$dotest/next") &&
148 test "$next" -gt "$last"
155 test ",$#," = ",0," ||
156 die "previous dotest directory $dotest still exists but mbox given."
159 # Make sure we are not given --skip nor --resolved
160 test ",$skip,$resolved," = ,,, ||
161 die "Resolve operation not in progress, we are not resuming."
164 mkdir -p "$dotest" || exit
166 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
171 # -b, -s, -u, -k and --whitespace flags are kept for the
172 # resuming session after a patch failure.
173 # -3 and -i can and must be given when resuming.
174 echo "$binary" >"$dotest/binary"
175 echo " $ws" >"$dotest/whitespace"
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 ws=`cat "$dotest/whitespace"`
204 if test "$(cat "$dotest/sign")" = t
206 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
208 s/^/Signed-off-by: /'
214 last=`cat "$dotest/last"`
215 this=`cat "$dotest/next"`
218 this=`expr "$this" + 1`
222 if test "$this" -gt "$last"
229 while test "$this" -le "$last"
231 msgnum=`printf "%0${prec}d" $this`
232 next=`expr "$this" + 1`
233 test -f "$dotest/$msgnum" || {
239 # If we are not resuming, parse and extract the patch information
240 # into separate files:
241 # - info records the authorship and title
242 # - msg is the rest of commit log message
243 # - patch is the patch body.
245 # When we are resuming, these files are either already prepared
246 # by the user, or the user can tell us to do so by --resolved flag.
249 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
250 <"$dotest/$msgnum" >"$dotest/info" ||
252 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
256 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
257 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
258 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
260 if test -z "$GIT_AUTHOR_EMAIL"
262 echo "Patch does not have a valid e-mail address."
266 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
268 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
269 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
273 if test '' != "$SIGNOFF"
276 sed -ne '/^Signed-off-by: /p' \
277 "$dotest/msg-clean" |
281 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
282 test '' = "$LAST_SIGNED_OFF_BY" && echo
290 if test -s "$dotest/msg-clean"
293 cat "$dotest/msg-clean"
295 if test '' != "$ADD_SIGNOFF"
299 } >"$dotest/final-commit"
302 case "$resolved$interactive" in
304 # This is used only for interactive view option.
305 git-diff-index -p --cached HEAD >"$dotest/patch"
311 if test "$interactive" = t
314 die "cannot be interactive without stdin connected to a terminal."
316 while test "$action" = again
318 echo "Commit Body is:"
319 echo "--------------------------"
320 cat "$dotest/final-commit"
321 echo "--------------------------"
322 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
326 [aA]*) action=yes interactive= ;;
327 [nN]*) action=skip ;;
328 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
331 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
339 if test $action = skip
345 if test -x "$GIT_DIR"/hooks/applypatch-msg
347 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
352 echo "Applying '$SUBJECT'"
357 git-apply $binary --index $ws "$dotest/patch"
361 # Resolved means the user did all the hard work, and
362 # we do not have to do any patch application. Just
363 # trust what the user has in the index file and the
366 changed="$(git-diff-index --cached --name-only HEAD)"
367 if test '' = "$changed"
369 echo "No changes - did you forget update-index?"
370 stop_here_user_resolve $this
372 unmerged=$(git-ls-files -u)
373 if test -n "$unmerged"
375 echo "You still have unmerged paths in your index"
376 echo "did you forget update-index?"
377 stop_here_user_resolve $this
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.
403 stop_here_user_resolve $this
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 -m "am: $SUBJECT" HEAD $commit $parent ||
419 if test -x "$GIT_DIR"/hooks/post-applypatch
421 "$GIT_DIR"/hooks/post-applypatch