Fall back to three-way merge when applying a patch.
After git-apply fails, attempt to find a base tree that the patch cleanly applies to, and do a three-way merge using that base tree into the current index, if .dotest/.3way file exists. This flag can be controlled by giving -m flag to git-applymbox command. When the fall-back merge fails, the working tree can be resolved the same way as you would normally hand resolve a conflicting merge. When making commit, use .dotest/final-commit as the log message template. Or you could just choose to 'git-checkout-index -f -a' to revert the failed merge. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Oct 6, 2005 at 14:25 UTC
47f0b6d5d49247b85898083d1ccf4f899ef7294a
3 files changed
+86
-5
Documentation/git-applymbox.txt
+9
-1
@@ -8,7 +8,7 @@ git-applymbox - Apply a series of patches in a mailbox
8
9
SYNOPSIS
10
--------
11
-'git-applymbox' [-u] [-k] [-q] ( -c .dotest/<num> | <mbox> ) [ <signoff> ]
11
+'git-applymbox' [-u] [-k] [-q] [-m] ( -c .dotest/<num> | <mbox> ) [ <signoff> ]
12
13
DESCRIPTION
14
-----------
@@ -33,6 +33,14 @@ OPTIONS
33
munging, and is most useful when used to read back 'git
34
format-patch --mbox' output.
35
36
+-m::
37
+ Patches are applied with `git-apply` command, and unless
38
+ it cleanly applies without fuzz, the processing fails.
39
+ With this flag, if a tree that the patch applies cleanly
40
+ is found in a repository, the patch is applied to the
41
+ tree and then a 3-way merge between the resulting tree
42
+ and the current tree.
43
+
44
-u::
45
By default, the commit log message, author name and
46
author email are taken from the e-mail without any
git-applymbox.sh
+5
-3
@@ -9,8 +9,6 @@
9
## You give it a mbox-format collection of emails, and it will try to
10
## apply them to the kernel using "applypatch"
11
##
12
-## applymbox [-u] [-k] [-q] (-c .dotest/msg-number | mail_archive) [Signoff_file]"
13
-##
12
## The patch application may fail in the middle. In which case:
13
## (1) look at .dotest/patch and fix it up to apply
14
## (2) re-run applymbox with -c .dotest/msg-number for the current one.
@@ -21,7 +19,7 @@
19
. git-sh-setup || die "Not a git archive"
20
21
usage () {
24
- echo >&2 "applymbox [-u] [-k] [-q] (-c .dotest/<num> | mbox) [signoff]"
22
+ echo >&2 "applymbox [-u] [-k] [-q] [-m] (-c .dotest/<num> | mbox) [signoff]"
23
exit 1
24
}
25
@@ -33,6 +31,7 @@ do
31
-k) keep_subject=-k ;;
32
-q) query_apply=t ;;
33
-c) continue="$2"; resume=f; shift ;;
34
+ -m) fallback_3way=t ;;
35
-*) usage ;;
36
*) break ;;
37
esac
@@ -56,6 +55,9 @@ fi
55
case "$query_apply" in
56
t) touch .dotest/.query_apply
57
esac
58
+case "$fall_back_3way" in
59
+t) : >.dotest/.3way
60
+esac
61
case "$keep_subject" in
62
-k) : >.dotest/.keep_subject
63
esac
git-applypatch.sh
+72
-1
@@ -22,6 +22,8 @@ query_apply=.dotest/.query_apply
22
## if this file exists.
23
keep_subject=.dotest/.keep_subject
24
25
+## We do not attempt the 3-way merge fallback unless this file exists.
26
+fall_back_3way=.dotest/.3way
27
28
MSGFILE=$1
29
PATCHFILE=$2
@@ -102,10 +104,79 @@ echo Applying "'$SUBJECT'"
104
echo
105
106
git-apply --index "$PATCHFILE" || {
107
+
108
+ # git-apply exits with status 1 when the patch does not apply,
109
+ # but it die()s with other failures, most notably upon corrupt
110
+ # patch. In the latter case, there is no point to try applying
111
+ # it to another tree and do 3-way merge.
112
+ test $? = 1 || exit 1
113
+
114
+ test -f "$fall_back_3way" || exit 1
115
+
116
# Here if we know which revision the patch applies to,
117
# we create a temporary working tree and index, apply the
118
# patch, and attempt 3-way merge with the resulting tree.
108
- exit 1
119
+
120
+ O_OBJECT=`cd "$GIT_OBJECT_DIRECTORY" && pwd`
121
+ rm -fr .patch-merge-*
122
+
123
+ (
124
+ N=10
125
+
126
+ # if the patch records the base tree...
127
+ sed -ne '
128
+ /^diff /q
129
+ /^applies-to: \([0-9a-f]*\)$/{
130
+ s//\1/p
131
+ q
132
+ }
133
+ ' "$PATCHFILE"
134
+
135
+ # or hoping the patch is against our recent commits...
136
+ git-rev-list --max-count=$N HEAD
137
+
138
+ # or hoping the patch is against known tags...
139
+ git-ls-remote --tags .
140
+ ) |
141
+ while read base junk
142
+ do
143
+ # Try it if we have it as a tree.
144
+ git-cat-file tree "$base" >/dev/null 2>&1 || continue
145
+
146
+ rm -fr .patch-merge-tmp-* &&
147
+ mkdir .patch-merge-tmp-dir || break
148
+ (
149
+ cd .patch-merge-tmp-dir &&
150
+ GIT_INDEX_FILE=../.patch-merge-tmp-index &&
151
+ GIT_OBJECT_DIRECTORY="$O_OBJECT" &&
152
+ export GIT_INDEX_FILE GIT_OBJECT_DIRECTORY &&
153
+ git-read-tree "$base" &&
154
+ git-apply --index &&
155
+ mv ../.patch-merge-tmp-index ../.patch-merge-index &&
156
+ echo "$base" >../.patch-merge-base
157
+ ) <"$PATCHFILE" 2>/dev/null && break
158
+ done
159
+
160
+ test -f .patch-merge-index &&
161
+ his_tree=$(GIT_INDEX_FILE=.patch-merge-index git-write-tree) &&
162
+ orig_tree=$(cat .patch-merge-base) &&
163
+ rm -fr .patch-merge-* || exit 1
164
+
165
+ echo Falling back to patching base and 3-way merge using $orig_tree...
166
+
167
+ # This is not so wrong. Depending on which base we picked,
168
+ # orig_tree may be wildly different from ours, but his_tree
169
+ # has the same set of wildly different changes in parts the
170
+ # patch did not touch, so resolve ends up cancelling them,
171
+ # saying that we reverted all those changes.
172
+
173
+ if git-merge-resolve $orig_tree -- HEAD $his_tree
174
+ then
175
+ echo Done.
176
+ else
177
+ echo Failed to merge in the changes.
178
+ exit 1
179
+ fi
180
}
181
182
if test -x "$GIT_DIR"/hooks/pre-applypatch