Merge branch 'js/t6042-timing-fix'
[git.git] / match-trees.c
blob18ab825bef57f3ea63f70adf9a5c4e4a4f000bc4
1 #include "cache.h"
2 #include "tree.h"
3 #include "tree-walk.h"
4 #include "object-store.h"
6 static int score_missing(unsigned mode, const char *path)
8 int score;
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;
19 static int score_differs(unsigned mode1, unsigned mode2, const char *path)
21 int score;
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;
32 static int score_matches(unsigned mode1, unsigned mode2, const char *path)
34 int score;
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;
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;
51 static void *fill_tree_desc_strict(struct tree_desc *desc,
52 const struct object_id *hash)
54 void *buffer;
55 enum object_type type;
56 unsigned long size;
58 buffer = read_object_file(hash, &type, &size);
59 if (!buffer)
60 die("unable to read tree (%s)", oid_to_hex(hash));
61 if (type != OBJ_TREE)
62 die("%s is not a tree", oid_to_hex(hash));
63 init_tree_desc(desc, buffer, size);
64 return buffer;
67 static int base_name_entries_compare(const struct name_entry *a,
68 const struct name_entry *b)
70 return base_name_compare(a->path, tree_entry_len(a), a->mode,
71 b->path, tree_entry_len(b), b->mode);
75 * Inspect two trees, and give a score that tells how similar they are.
77 static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
79 struct tree_desc one;
80 struct tree_desc two;
81 void *one_buf = fill_tree_desc_strict(&one, hash1);
82 void *two_buf = fill_tree_desc_strict(&two, hash2);
83 int score = 0;
85 for (;;) {
86 int cmp;
88 if (one.size && two.size)
89 cmp = base_name_entries_compare(&one.entry, &two.entry);
90 else if (one.size)
91 /* two lacks this entry */
92 cmp = -1;
93 else if (two.size)
94 /* two has more entries */
95 cmp = 1;
96 else
97 break;
99 if (cmp < 0) {
100 /* path1 does not appear in two */
101 score += score_missing(one.entry.mode, one.entry.path);
102 update_tree_entry(&one);
103 } else if (cmp > 0) {
104 /* path2 does not appear in one */
105 score += score_missing(two.entry.mode, two.entry.path);
106 update_tree_entry(&two);
107 } else {
108 /* path appears in both */
109 if (!oideq(&one.entry.oid, &two.entry.oid)) {
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);
120 update_tree_entry(&one);
121 update_tree_entry(&two);
124 free(one_buf);
125 free(two_buf);
126 return score;
130 * Match one itself and its subtrees with two and pick the best match.
132 static void match_trees(const struct object_id *hash1,
133 const struct object_id *hash2,
134 int *best_score,
135 char **best_match,
136 const char *base,
137 int recurse_limit)
139 struct tree_desc one;
140 void *one_buf = fill_tree_desc_strict(&one, hash1);
142 while (one.size) {
143 const char *path;
144 const struct object_id *elem;
145 unsigned mode;
146 int score;
148 elem = tree_entry_extract(&one, &path, &mode);
149 if (!S_ISDIR(mode))
150 goto next;
151 score = score_trees(elem, hash2);
152 if (*best_score < score) {
153 free(*best_match);
154 *best_match = xstrfmt("%s%s", base, path);
155 *best_score = score;
157 if (recurse_limit) {
158 char *newbase = xstrfmt("%s%s/", base, path);
159 match_trees(elem, hash2, best_score, best_match,
160 newbase, recurse_limit - 1);
161 free(newbase);
164 next:
165 update_tree_entry(&one);
167 free(one_buf);
171 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
172 * replacing it with another tree "oid2".
174 static int splice_tree(const struct object_id *oid1, const char *prefix,
175 const struct object_id *oid2, struct object_id *result)
177 char *subpath;
178 int toplen;
179 char *buf;
180 unsigned long sz;
181 struct tree_desc desc;
182 unsigned char *rewrite_here;
183 const struct object_id *rewrite_with;
184 struct object_id subtree;
185 enum object_type type;
186 int status;
188 subpath = strchrnul(prefix, '/');
189 toplen = subpath - prefix;
190 if (*subpath)
191 subpath++;
193 buf = read_object_file(oid1, &type, &sz);
194 if (!buf)
195 die("cannot read tree %s", oid_to_hex(oid1));
196 init_tree_desc(&desc, buf, sz);
198 rewrite_here = NULL;
199 while (desc.size) {
200 const char *name;
201 unsigned mode;
203 tree_entry_extract(&desc, &name, &mode);
204 if (strlen(name) == toplen &&
205 !memcmp(name, prefix, toplen)) {
206 if (!S_ISDIR(mode))
207 die("entry %s in tree %s is not a tree", name,
208 oid_to_hex(oid1));
211 * We cast here for two reasons:
213 * - to flip the "char *" (for the path) to "unsigned
214 * char *" (for the hash stored after it)
216 * - to discard the "const"; this is OK because we
217 * know it points into our non-const "buf"
219 rewrite_here = (unsigned char *)(desc.entry.path +
220 strlen(desc.entry.path) +
222 break;
224 update_tree_entry(&desc);
226 if (!rewrite_here)
227 die("entry %.*s not found in tree %s", toplen, prefix,
228 oid_to_hex(oid1));
229 if (*subpath) {
230 struct object_id tree_oid;
231 hashcpy(tree_oid.hash, rewrite_here);
232 status = splice_tree(&tree_oid, subpath, oid2, &subtree);
233 if (status)
234 return status;
235 rewrite_with = &subtree;
236 } else {
237 rewrite_with = oid2;
239 hashcpy(rewrite_here, rewrite_with->hash);
240 status = write_object_file(buf, sz, tree_type, result);
241 free(buf);
242 return status;
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.
253 void shift_tree(const struct object_id *hash1,
254 const struct object_id *hash2,
255 struct object_id *shifted,
256 int depth_limit)
258 char *add_prefix;
259 char *del_prefix;
260 int add_score, del_score;
263 * NEEDSWORK: this limits the recursion depth to hardcoded
264 * value '2' to avoid excessive overhead.
266 if (!depth_limit)
267 depth_limit = 2;
269 add_score = del_score = score_trees(hash1, hash2);
270 add_prefix = xcalloc(1, 1);
271 del_prefix = xcalloc(1, 1);
274 * See if one's subtree resembles two; if so we need to prefix
275 * two with a few fake trees to match the prefix.
277 match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
280 * See if two's subtree resembles one; if so we need to
281 * pick only subtree of two.
283 match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
285 /* Assume we do not have to do any shifting */
286 oidcpy(shifted, hash2);
288 if (add_score < del_score) {
289 /* We need to pick a subtree of two */
290 unsigned mode;
292 if (!*del_prefix)
293 return;
295 if (get_tree_entry(hash2, del_prefix, shifted, &mode))
296 die("cannot find path %s in tree %s",
297 del_prefix, oid_to_hex(hash2));
298 return;
301 if (!*add_prefix)
302 return;
304 splice_tree(hash1, add_prefix, hash2, shifted);
308 * The user says the trees will be shifted by this much.
309 * Unfortunately we cannot fundamentally tell which one to
310 * be prefixed, as recursive merge can work in either direction.
312 void shift_tree_by(const struct object_id *hash1,
313 const struct object_id *hash2,
314 struct object_id *shifted,
315 const char *shift_prefix)
317 struct object_id sub1, sub2;
318 unsigned mode1, mode2;
319 unsigned candidate = 0;
321 /* Can hash2 be a tree at shift_prefix in tree hash1? */
322 if (!get_tree_entry(hash1, shift_prefix, &sub1, &mode1) &&
323 S_ISDIR(mode1))
324 candidate |= 1;
326 /* Can hash1 be a tree at shift_prefix in tree hash2? */
327 if (!get_tree_entry(hash2, shift_prefix, &sub2, &mode2) &&
328 S_ISDIR(mode2))
329 candidate |= 2;
331 if (candidate == 3) {
332 /* Both are plausible -- we need to evaluate the score */
333 int best_score = score_trees(hash1, hash2);
334 int score;
336 candidate = 0;
337 score = score_trees(&sub1, hash2);
338 if (score > best_score) {
339 candidate = 1;
340 best_score = score;
342 score = score_trees(&sub2, hash1);
343 if (score > best_score)
344 candidate = 2;
347 if (!candidate) {
348 /* Neither is plausible -- do not shift */
349 oidcpy(shifted, hash2);
350 return;
353 if (candidate == 1)
355 * shift tree2 down by adding shift_prefix above it
356 * to match tree1.
358 splice_tree(hash1, shift_prefix, hash2, shifted);
359 else
361 * shift tree2 up by removing shift_prefix from it
362 * to match tree1.
364 oidcpy(shifted, &sub2);