4 #include "object-store.h"
6 static int score_missing(unsigned mode, const char *path)
12 else if (S_ISLNK(mode))
19 static int score_differs(unsigned mode1, unsigned mode2, const char *path)
23 if (S_ISDIR(mode1) != S_ISDIR(mode2))
25 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
32 static int score_matches(unsigned mode1, unsigned mode2, const char *path)
36 /* Heh, we found SHA-1 collisions between different kind of objects */
37 if (S_ISDIR(mode1) != S_ISDIR(mode2))
39 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
42 else if (S_ISDIR(mode1))
44 else if (S_ISLNK(mode1))
51 static void *fill_tree_desc_strict(struct tree_desc *desc,
52 const struct object_id *hash)
55 enum object_type type;
58 buffer = read_object_file(hash, &type, &size);
60 die("unable to read tree (%s)", oid_to_hex(hash));
62 die("%s is not a tree", oid_to_hex(hash));
63 init_tree_desc(desc, buffer, size);
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)
81 void *one_buf = fill_tree_desc_strict(&one, hash1);
82 void *two_buf = fill_tree_desc_strict(&two, hash2);
86 struct name_entry e1, e2;
87 int got_entry_from_one = tree_entry(&one, &e1);
88 int got_entry_from_two = tree_entry(&two, &e2);
91 if (got_entry_from_one && got_entry_from_two)
92 cmp = base_name_entries_compare(&e1, &e2);
93 else if (got_entry_from_one)
94 /* two lacks this entry */
96 else if (got_entry_from_two)
97 /* two has more entries */
103 /* path1 does not appear in two */
104 score += score_missing(e1.mode, e1.path);
106 /* path2 does not appear in one */
107 score += score_missing(e2.mode, e2.path);
108 else if (oidcmp(e1.oid, e2.oid))
109 /* they are different */
110 score += score_differs(e1.mode, e2.mode, e1.path);
112 /* same subtree or blob */
113 score += score_matches(e1.mode, e2.mode, e1.path);
121 * Match one itself and its subtrees with two and pick the best match.
123 static void match_trees(const struct object_id *hash1,
124 const struct object_id *hash2,
130 struct tree_desc one;
131 void *one_buf = fill_tree_desc_strict(&one, hash1);
135 const struct object_id *elem;
139 elem = tree_entry_extract(&one, &path, &mode);
142 score = score_trees(elem, hash2);
143 if (*best_score < score) {
145 *best_match = xstrfmt("%s%s", base, path);
149 char *newbase = xstrfmt("%s%s/", base, path);
150 match_trees(elem, hash2, best_score, best_match,
151 newbase, recurse_limit - 1);
156 update_tree_entry(&one);
162 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
163 * replacing it with another tree "oid2".
165 static int splice_tree(const struct object_id *oid1, const char *prefix,
166 const struct object_id *oid2, struct object_id *result)
172 struct tree_desc desc;
173 struct object_id *rewrite_here;
174 const struct object_id *rewrite_with;
175 struct object_id subtree;
176 enum object_type type;
179 subpath = strchrnul(prefix, '/');
180 toplen = subpath - prefix;
184 buf = read_object_file(oid1, &type, &sz);
186 die("cannot read tree %s", oid_to_hex(oid1));
187 init_tree_desc(&desc, buf, sz);
193 const struct object_id *oid;
195 oid = tree_entry_extract(&desc, &name, &mode);
196 if (strlen(name) == toplen &&
197 !memcmp(name, prefix, toplen)) {
199 die("entry %s in tree %s is not a tree", name,
201 rewrite_here = (struct object_id *)oid;
204 update_tree_entry(&desc);
207 die("entry %.*s not found in tree %s", toplen, prefix,
210 status = splice_tree(rewrite_here, subpath, oid2, &subtree);
213 rewrite_with = &subtree;
217 oidcpy(rewrite_here, rewrite_with);
218 status = write_object_file(buf, sz, tree_type, result);
224 * We are trying to come up with a merge between one and two that
225 * results in a tree shape similar to one. The tree two might
226 * correspond to a subtree of one, in which case it needs to be
227 * shifted down by prefixing otherwise empty directories. On the
228 * other hand, it could cover tree one and we might need to pick a
231 void shift_tree(const struct object_id *hash1,
232 const struct object_id *hash2,
233 struct object_id *shifted,
238 int add_score, del_score;
241 * NEEDSWORK: this limits the recursion depth to hardcoded
242 * value '2' to avoid excessive overhead.
247 add_score = del_score = score_trees(hash1, hash2);
248 add_prefix = xcalloc(1, 1);
249 del_prefix = xcalloc(1, 1);
252 * See if one's subtree resembles two; if so we need to prefix
253 * two with a few fake trees to match the prefix.
255 match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
258 * See if two's subtree resembles one; if so we need to
259 * pick only subtree of two.
261 match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
263 /* Assume we do not have to do any shifting */
264 oidcpy(shifted, hash2);
266 if (add_score < del_score) {
267 /* We need to pick a subtree of two */
273 if (get_tree_entry(hash2, del_prefix, shifted, &mode))
274 die("cannot find path %s in tree %s",
275 del_prefix, oid_to_hex(hash2));
282 splice_tree(hash1, add_prefix, hash2, shifted);
286 * The user says the trees will be shifted by this much.
287 * Unfortunately we cannot fundamentally tell which one to
288 * be prefixed, as recursive merge can work in either direction.
290 void shift_tree_by(const struct object_id *hash1,
291 const struct object_id *hash2,
292 struct object_id *shifted,
293 const char *shift_prefix)
295 struct object_id sub1, sub2;
296 unsigned mode1, mode2;
297 unsigned candidate = 0;
299 /* Can hash2 be a tree at shift_prefix in tree hash1? */
300 if (!get_tree_entry(hash1, shift_prefix, &sub1, &mode1) &&
304 /* Can hash1 be a tree at shift_prefix in tree hash2? */
305 if (!get_tree_entry(hash2, shift_prefix, &sub2, &mode2) &&
309 if (candidate == 3) {
310 /* Both are plausible -- we need to evaluate the score */
311 int best_score = score_trees(hash1, hash2);
315 score = score_trees(&sub1, hash2);
316 if (score > best_score) {
320 score = score_trees(&sub2, hash1);
321 if (score > best_score)
326 /* Neither is plausible -- do not shift */
327 oidcpy(shifted, hash2);
333 * shift tree2 down by adding shift_prefix above it
336 splice_tree(hash1, shift_prefix, hash2, shifted);
339 * shift tree2 up by removing shift_prefix from it
342 oidcpy(shifted, &sub2);