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