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 cmdline=$(basename $0)
19 if test '' != "$interactive"
23 if test '' != "$threeway"
27 if test '.dotest' != "$dotest"
29 cmdline="$cmdline -d=$dotest"
31 echo "When you have resolved this problem run \"$cmdline --resolved\"."
32 echo "If you would prefer to skip this patch, instead run \"$cmdline --skip\"."
38 rm -f "$dotest/$msgnum" "$dotest/msg" "$dotest/msg-clean" \
39 "$dotest/patch" "$dotest/info"
40 echo "$next" >"$dotest/next"
45 O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
47 rm -fr "$dotest"/patch-merge-*
48 mkdir "$dotest/patch-merge-tmp-dir"
50 # First see if the patch records the index info that we can use.
51 if git-apply -z --index-info "$dotest/patch" \
52 >"$dotest/patch-merge-index-info" 2>/dev/null &&
53 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
54 git-update-index -z --index-info <"$dotest/patch-merge-index-info" &&
55 GIT_INDEX_FILE="$dotest/patch-merge-tmp-index" \
56 git-write-tree >"$dotest/patch-merge-base+" &&
57 # index has the base tree now.
59 cd "$dotest/patch-merge-tmp-dir" &&
60 GIT_INDEX_FILE="../patch-merge-tmp-index" \
61 GIT_OBJECT_DIRECTORY="$O_OBJECT" \
62 git-apply $binary --index <../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"
69 # Otherwise, try nearby trees that can be used to apply the
74 # Hoping the patch is against our recent commits...
75 git-rev-list --max-count=$N HEAD
77 # or hoping the patch is against known tags...
78 git-ls-remote --tags .
82 # See if we have it as a tree...
83 git-cat-file tree "$base" >/dev/null 2>&1 || continue
85 rm -fr "$dotest"/patch-merge-* &&
86 mkdir "$dotest/patch-merge-tmp-dir" || break
88 cd "$dotest/patch-merge-tmp-dir" &&
89 GIT_INDEX_FILE=../patch-merge-tmp-index &&
90 GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
91 export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
92 git-read-tree "$base" &&
93 git-apply $binary --index &&
94 mv ../patch-merge-tmp-index ../patch-merge-index &&
95 echo "$base" >../patch-merge-base
96 ) <"$dotest/patch" 2>/dev/null && break
100 test -f "$dotest/patch-merge-index" &&
101 his_tree=$(GIT_INDEX_FILE="$dotest/patch-merge-index" git-write-tree) &&
102 orig_tree=$(cat "$dotest/patch-merge-base") &&
103 rm -fr "$dotest"/patch-merge-* || exit 1
105 echo Falling back to patching base and 3-way merge...
107 # This is not so wrong. Depending on which base we picked,
108 # orig_tree may be wildly different from ours, but his_tree
109 # has the same set of wildly different changes in parts the
110 # patch did not touch, so resolve ends up cancelling them,
111 # saying that we reverted all those changes.
113 git-merge-resolve $orig_tree -- HEAD $his_tree || {
114 if test -d "$GIT_DIR/rr-cache"
118 echo Failed to merge in the changes.
124 dotest=.dotest sign= utf8= keep= skip= interactive= resolved= binary= ws=
126 while case "$#" in 0) break;; esac
129 -d=*|--d=*|--do=*|--dot=*|--dote=*|--dotes=*|--dotest=*)
130 dotest=`expr "$1" : '-[^=]*=\(.*\)'`; shift ;;
131 -d|--d|--do|--dot|--dote|--dotes|--dotest)
132 case "$#" in 1) usage ;; esac; shift
135 -i|--i|--in|--int|--inte|--inter|--intera|--interac|--interact|\
136 --interacti|--interactiv|--interactive)
137 interactive=t; shift ;;
139 -b|--b|--bi|--bin|--bina|--binar|--binary)
142 -3|--3|--3w|--3wa|--3way)
144 -s|--s|--si|--sig|--sign|--signo|--signof|--signoff)
146 -u|--u|--ut|--utf|--utf8)
148 -k|--k|--ke|--kee|--keep)
151 -r|--r|--re|--res|--reso|--resol|--resolv|--resolve|--resolved)
169 # If the dotest directory exists, but we have finished applying all the
170 # patches in them, clear it out.
171 if test -d "$dotest" &&
172 last=$(cat "$dotest/last") &&
173 next=$(cat "$dotest/next") &&
175 test "$next" -gt "$last"
182 test ",$#," = ",0," ||
183 die "previous dotest directory $dotest still exists but mbox given."
186 # Make sure we are not given --skip nor --resolved
187 test ",$skip,$resolved," = ,,, ||
188 die "we are not resuming."
191 mkdir -p "$dotest" || exit
193 git-mailsplit -d"$prec" -o"$dotest" -b -- "$@" > "$dotest/last" || {
198 # -b, -s, -u, -k and --whitespace flags are kept for the
199 # resuming session after a patch failure.
200 # -3 and -i can and must be given when resuming.
201 echo "$binary" >"$dotest/binary"
202 echo " $ws" >"$dotest/whitespace"
203 echo "$sign" >"$dotest/sign"
204 echo "$utf8" >"$dotest/utf8"
205 echo "$keep" >"$dotest/keep"
206 echo 1 >"$dotest/next"
211 files=$(git-diff-index --cached --name-only HEAD) || exit
212 if [ "$files" ]; then
213 echo "Dirty index: cannot apply patches (dirty: $files)" >&2
218 if test "$(cat "$dotest/binary")" = t
220 binary=--allow-binary-replacement
222 if test "$(cat "$dotest/utf8")" = t
226 if test "$(cat "$dotest/keep")" = t
230 ws=`cat "$dotest/whitespace"`
231 if test "$(cat "$dotest/sign")" = t
233 SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
235 s/^/Signed-off-by: /'
241 last=`cat "$dotest/last"`
242 this=`cat "$dotest/next"`
245 this=`expr "$this" + 1`
249 if test "$this" -gt "$last"
256 while test "$this" -le "$last"
258 msgnum=`printf "%0${prec}d" $this`
259 next=`expr "$this" + 1`
260 test -f "$dotest/$msgnum" || {
266 # If we are not resuming, parse and extract the patch information
267 # into separate files:
268 # - info records the authorship and title
269 # - msg is the rest of commit log message
270 # - patch is the patch body.
272 # When we are resuming, these files are either already prepared
273 # by the user, or the user can tell us to do so by --resolved flag.
276 git-mailinfo $keep $utf8 "$dotest/msg" "$dotest/patch" \
277 <"$dotest/$msgnum" >"$dotest/info" ||
279 git-stripspace < "$dotest/msg" > "$dotest/msg-clean"
283 GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$dotest/info")"
284 GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$dotest/info")"
285 GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$dotest/info")"
287 if test -z "$GIT_AUTHOR_EMAIL"
289 echo "Patch does not have a valid e-mail address."
293 export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_AUTHOR_DATE
295 SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$dotest/info")"
296 case "$keep_subject" in -k) SUBJECT="[PATCH] $SUBJECT" ;; esac
300 if test '' != "$SIGNOFF"
303 sed -ne '/^Signed-off-by: /p' \
304 "$dotest/msg-clean" |
308 test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" || {
309 test '' = "$LAST_SIGNED_OFF_BY" && echo
317 if test -s "$dotest/msg-clean"
320 cat "$dotest/msg-clean"
322 if test '' != "$ADD_SIGNOFF"
326 } >"$dotest/final-commit"
329 case "$resolved$interactive" in
331 # This is used only for interactive view option.
332 git-diff-index -p --cached HEAD >"$dotest/patch"
338 if test "$interactive" = t
341 die "cannot be interactive without stdin connected to a terminal."
343 while test "$action" = again
345 echo "Commit Body is:"
346 echo "--------------------------"
347 cat "$dotest/final-commit"
348 echo "--------------------------"
349 printf "Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all "
353 [aA]*) action=yes interactive= ;;
354 [nN]*) action=skip ;;
355 [eE]*) "${VISUAL:-${EDITOR:-vi}}" "$dotest/final-commit"
358 LESS=-S ${PAGER:-less} "$dotest/patch" ;;
366 if test $action = skip
372 if test -x "$GIT_DIR"/hooks/applypatch-msg
374 "$GIT_DIR"/hooks/applypatch-msg "$dotest/final-commit" ||
379 echo "Applying '$SUBJECT'"
384 git-apply $binary --index $ws "$dotest/patch"
388 # Resolved means the user did all the hard work, and
389 # we do not have to do any patch application. Just
390 # trust what the user has in the index file and the
393 changed="$(git-diff-index --cached --name-only HEAD)"
394 if test '' = "$changed"
396 echo "No changes - did you forget update-index?"
397 stop_here_user_resolve $this
399 unmerged=$(git-ls-files -u)
400 if test -n "$unmerged"
402 echo "You still have unmerged paths in your index"
403 echo "did you forget update-index?"
404 stop_here_user_resolve $this
410 if test $apply_status = 1 && test "$threeway" = t
414 # Applying the patch to an earlier tree and merging the
415 # result may have produced the same tree as ours.
416 changed="$(git-diff-index --cached --name-only HEAD)"
417 if test '' = "$changed"
419 echo No changes -- Patch already applied.
423 # clear apply_status -- we have successfully merged.
427 if test $apply_status != 0
429 echo Patch failed at $msgnum.
430 stop_here_user_resolve $this
433 if test -x "$GIT_DIR"/hooks/pre-applypatch
435 "$GIT_DIR"/hooks/pre-applypatch || stop_here $this
438 tree=$(git-write-tree) &&
439 echo Wrote tree $tree &&
440 parent=$(git-rev-parse --verify HEAD) &&
441 commit=$(git-commit-tree $tree -p $parent <"$dotest/final-commit") &&
442 echo Committed: $commit &&
443 git-update-ref HEAD $commit $parent ||
446 if test -x "$GIT_DIR"/hooks/post-applypatch
448 "$GIT_DIR"/hooks/post-applypatch