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