]> git.ipfire.org Git - thirdparty/git.git/blame - match-trees.c
Merge branch 'jk/verify-sig-merge-into-void'
[thirdparty/git.git] / match-trees.c
CommitLineData
68faf689
JH
1#include "cache.h"
2#include "tree.h"
3#include "tree-walk.h"
cbd53a21 4#include "object-store.h"
68faf689
JH
5
6static int score_missing(unsigned mode, const char *path)
7{
8 int score;
9
10 if (S_ISDIR(mode))
11 score = -1000;
12 else if (S_ISLNK(mode))
13 score = -500;
14 else
15 score = -50;
16 return score;
17}
18
19static int score_differs(unsigned mode1, unsigned mode2, const char *path)
20{
21 int score;
22
23 if (S_ISDIR(mode1) != S_ISDIR(mode2))
24 score = -100;
25 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
26 score = -50;
27 else
28 score = -5;
29 return score;
30}
31
32static int score_matches(unsigned mode1, unsigned mode2, const char *path)
33{
34 int score;
35
36 /* Heh, we found SHA-1 collisions between different kind of objects */
37 if (S_ISDIR(mode1) != S_ISDIR(mode2))
38 score = -100;
39 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
40 score = -50;
41
42 else if (S_ISDIR(mode1))
43 score = 1000;
44 else if (S_ISLNK(mode1))
45 score = 500;
46 else
47 score = 250;
48 return score;
49}
50
10a3fb00 51static void *fill_tree_desc_strict(struct tree_desc *desc,
b6aec868 52 const struct object_id *hash)
10a3fb00
RS
53{
54 void *buffer;
55 enum object_type type;
56 unsigned long size;
57
b4f5aca4 58 buffer = read_object_file(hash, &type, &size);
10a3fb00 59 if (!buffer)
b6aec868 60 die("unable to read tree (%s)", oid_to_hex(hash));
10a3fb00 61 if (type != OBJ_TREE)
b6aec868 62 die("%s is not a tree", oid_to_hex(hash));
10a3fb00
RS
63 init_tree_desc(desc, buffer, size);
64 return buffer;
65}
66
d8febde3
RS
67static int base_name_entries_compare(const struct name_entry *a,
68 const struct name_entry *b)
69{
70 return base_name_compare(a->path, tree_entry_len(a), a->mode,
71 b->path, tree_entry_len(b), b->mode);
72}
73
68faf689
JH
74/*
75 * Inspect two trees, and give a score that tells how similar they are.
76 */
b6aec868 77static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
68faf689
JH
78{
79 struct tree_desc one;
80 struct tree_desc two;
10a3fb00
RS
81 void *one_buf = fill_tree_desc_strict(&one, hash1);
82 void *two_buf = fill_tree_desc_strict(&two, hash2);
68faf689 83 int score = 0;
68faf689 84
d8febde3 85 for (;;) {
68faf689
JH
86 int cmp;
87
2ec41507
JK
88 if (one.size && two.size)
89 cmp = base_name_entries_compare(&one.entry, &two.entry);
90 else if (one.size)
68faf689 91 /* two lacks this entry */
d8febde3 92 cmp = -1;
2ec41507 93 else if (two.size)
d8febde3
RS
94 /* two has more entries */
95 cmp = 1;
96 else
97 break;
98
2ec41507 99 if (cmp < 0) {
68faf689 100 /* path1 does not appear in two */
2ec41507
JK
101 score += score_missing(one.entry.mode, one.entry.path);
102 update_tree_entry(&one);
103 } else if (cmp > 0) {
68faf689 104 /* path2 does not appear in one */
2ec41507
JK
105 score += score_missing(two.entry.mode, two.entry.path);
106 update_tree_entry(&two);
107 } else {
108 /* path appears in both */
9001dc2a 109 if (!oideq(one.entry.oid, two.entry.oid)) {
2ec41507
JK
110 /* they are different */
111 score += score_differs(one.entry.mode,
112 two.entry.mode,
113 one.entry.path);
114 } else {
115 /* same subtree or blob */
116 score += score_matches(one.entry.mode,
117 two.entry.mode,
118 one.entry.path);
119 }
120 update_tree_entry(&one);
121 update_tree_entry(&two);
122 }
68faf689
JH
123 }
124 free(one_buf);
125 free(two_buf);
126 return score;
127}
128
129/*
130 * Match one itself and its subtrees with two and pick the best match.
131 */
b6aec868 132static void match_trees(const struct object_id *hash1,
133 const struct object_id *hash2,
68faf689
JH
134 int *best_score,
135 char **best_match,
538dfe73 136 const char *base,
68faf689
JH
137 int recurse_limit)
138{
139 struct tree_desc one;
10a3fb00 140 void *one_buf = fill_tree_desc_strict(&one, hash1);
68faf689
JH
141
142 while (one.size) {
143 const char *path;
ce6663a9 144 const struct object_id *elem;
68faf689
JH
145 unsigned mode;
146 int score;
147
148 elem = tree_entry_extract(&one, &path, &mode);
149 if (!S_ISDIR(mode))
150 goto next;
b6aec868 151 score = score_trees(elem, hash2);
68faf689 152 if (*best_score < score) {
68faf689 153 free(*best_match);
28310186 154 *best_match = xstrfmt("%s%s", base, path);
68faf689
JH
155 *best_score = score;
156 }
157 if (recurse_limit) {
28310186 158 char *newbase = xstrfmt("%s%s/", base, path);
b6aec868 159 match_trees(elem, hash2, best_score, best_match,
68faf689
JH
160 newbase, recurse_limit - 1);
161 free(newbase);
162 }
163
164 next:
165 update_tree_entry(&one);
166 }
167 free(one_buf);
168}
169
170/*
3b34934d
PO
171 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
172 * replacing it with another tree "oid2".
68faf689 173 */
3b34934d
PO
174static int splice_tree(const struct object_id *oid1, const char *prefix,
175 const struct object_id *oid2, struct object_id *result)
68faf689
JH
176{
177 char *subpath;
178 int toplen;
179 char *buf;
180 unsigned long sz;
181 struct tree_desc desc;
3b34934d
PO
182 struct object_id *rewrite_here;
183 const struct object_id *rewrite_with;
184 struct object_id subtree;
68faf689
JH
185 enum object_type type;
186 int status;
187
2c5495f7
RM
188 subpath = strchrnul(prefix, '/');
189 toplen = subpath - prefix;
190 if (*subpath)
68faf689 191 subpath++;
68faf689 192
b4f5aca4 193 buf = read_object_file(oid1, &type, &sz);
68faf689 194 if (!buf)
3b34934d 195 die("cannot read tree %s", oid_to_hex(oid1));
68faf689
JH
196 init_tree_desc(&desc, buf, sz);
197
198 rewrite_here = NULL;
199 while (desc.size) {
200 const char *name;
201 unsigned mode;
ce6663a9 202 const struct object_id *oid;
68faf689 203
ce6663a9 204 oid = tree_entry_extract(&desc, &name, &mode);
68faf689
JH
205 if (strlen(name) == toplen &&
206 !memcmp(name, prefix, toplen)) {
207 if (!S_ISDIR(mode))
3b34934d
PO
208 die("entry %s in tree %s is not a tree", name,
209 oid_to_hex(oid1));
210 rewrite_here = (struct object_id *)oid;
68faf689
JH
211 break;
212 }
213 update_tree_entry(&desc);
214 }
215 if (!rewrite_here)
3b34934d
PO
216 die("entry %.*s not found in tree %s", toplen, prefix,
217 oid_to_hex(oid1));
2c5495f7 218 if (*subpath) {
3b34934d 219 status = splice_tree(rewrite_here, subpath, oid2, &subtree);
68faf689
JH
220 if (status)
221 return status;
3b34934d
PO
222 rewrite_with = &subtree;
223 } else {
224 rewrite_with = oid2;
68faf689 225 }
3b34934d 226 oidcpy(rewrite_here, rewrite_with);
a09c985e 227 status = write_object_file(buf, sz, tree_type, result);
68faf689
JH
228 free(buf);
229 return status;
230}
231
232/*
233 * We are trying to come up with a merge between one and two that
234 * results in a tree shape similar to one. The tree two might
235 * correspond to a subtree of one, in which case it needs to be
236 * shifted down by prefixing otherwise empty directories. On the
237 * other hand, it could cover tree one and we might need to pick a
238 * subtree of it.
239 */
82db3d44 240void shift_tree(const struct object_id *hash1,
241 const struct object_id *hash2,
242 struct object_id *shifted,
68faf689
JH
243 int depth_limit)
244{
245 char *add_prefix;
246 char *del_prefix;
247 int add_score, del_score;
248
85e51b78
JH
249 /*
250 * NEEDSWORK: this limits the recursion depth to hardcoded
251 * value '2' to avoid excessive overhead.
252 */
253 if (!depth_limit)
254 depth_limit = 2;
255
b6aec868 256 add_score = del_score = score_trees(hash1, hash2);
68faf689
JH
257 add_prefix = xcalloc(1, 1);
258 del_prefix = xcalloc(1, 1);
259
260 /*
261 * See if one's subtree resembles two; if so we need to prefix
262 * two with a few fake trees to match the prefix.
263 */
b6aec868 264 match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
68faf689
JH
265
266 /*
267 * See if two's subtree resembles one; if so we need to
268 * pick only subtree of two.
269 */
b6aec868 270 match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
68faf689
JH
271
272 /* Assume we do not have to do any shifting */
82db3d44 273 oidcpy(shifted, hash2);
68faf689
JH
274
275 if (add_score < del_score) {
276 /* We need to pick a subtree of two */
277 unsigned mode;
278
279 if (!*del_prefix)
280 return;
281
916bc35b 282 if (get_tree_entry(hash2, del_prefix, shifted, &mode))
68faf689 283 die("cannot find path %s in tree %s",
82db3d44 284 del_prefix, oid_to_hex(hash2));
68faf689
JH
285 return;
286 }
287
288 if (!*add_prefix)
289 return;
290
3b34934d 291 splice_tree(hash1, add_prefix, hash2, shifted);
68faf689 292}
85e51b78
JH
293
294/*
295 * The user says the trees will be shifted by this much.
296 * Unfortunately we cannot fundamentally tell which one to
297 * be prefixed, as recursive merge can work in either direction.
298 */
82db3d44 299void shift_tree_by(const struct object_id *hash1,
300 const struct object_id *hash2,
301 struct object_id *shifted,
85e51b78
JH
302 const char *shift_prefix)
303{
82db3d44 304 struct object_id sub1, sub2;
85e51b78
JH
305 unsigned mode1, mode2;
306 unsigned candidate = 0;
307
308 /* Can hash2 be a tree at shift_prefix in tree hash1? */
916bc35b 309 if (!get_tree_entry(hash1, shift_prefix, &sub1, &mode1) &&
85e51b78
JH
310 S_ISDIR(mode1))
311 candidate |= 1;
312
313 /* Can hash1 be a tree at shift_prefix in tree hash2? */
916bc35b 314 if (!get_tree_entry(hash2, shift_prefix, &sub2, &mode2) &&
85e51b78
JH
315 S_ISDIR(mode2))
316 candidate |= 2;
317
318 if (candidate == 3) {
319 /* Both are plausible -- we need to evaluate the score */
b6aec868 320 int best_score = score_trees(hash1, hash2);
85e51b78
JH
321 int score;
322
323 candidate = 0;
b6aec868 324 score = score_trees(&sub1, hash2);
85e51b78
JH
325 if (score > best_score) {
326 candidate = 1;
327 best_score = score;
328 }
b6aec868 329 score = score_trees(&sub2, hash1);
85e51b78
JH
330 if (score > best_score)
331 candidate = 2;
332 }
333
334 if (!candidate) {
335 /* Neither is plausible -- do not shift */
82db3d44 336 oidcpy(shifted, hash2);
85e51b78
JH
337 return;
338 }
339
340 if (candidate == 1)
341 /*
342 * shift tree2 down by adding shift_prefix above it
343 * to match tree1.
344 */
3b34934d 345 splice_tree(hash1, shift_prefix, hash2, shifted);
85e51b78
JH
346 else
347 /*
348 * shift tree2 up by removing shift_prefix from it
349 * to match tree1.
350 */
82db3d44 351 oidcpy(shifted, &sub2);
85e51b78 352}