5 static int score_missing(unsigned mode, const char *path)
11 else if (S_ISLNK(mode))
18 static int score_differs(unsigned mode1, unsigned mode2, const char *path)
22 if (S_ISDIR(mode1) != S_ISDIR(mode2))
24 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
31 static int score_matches(unsigned mode1, unsigned mode2, const char *path)
35 /* Heh, we found SHA-1 collisions between different kind of objects */
36 if (S_ISDIR(mode1) != S_ISDIR(mode2))
38 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
41 else if (S_ISDIR(mode1))
43 else if (S_ISLNK(mode1))
50 static int base_name_entries_compare(const struct name_entry *a,
51 const struct name_entry *b)
53 return base_name_compare(a->path, tree_entry_len(a), a->mode,
54 b->path, tree_entry_len(b), b->mode);
58 * Inspect two trees, and give a score that tells how similar they are.
60 static int score_trees(const unsigned char *hash1, const unsigned char *hash2)
64 void *one_buf, *two_buf;
66 enum object_type type;
69 one_buf = read_sha1_file(hash1, &type, &size);
71 die("unable to read tree (%s)", sha1_to_hex(hash1));
73 die("%s is not a tree", sha1_to_hex(hash1));
74 init_tree_desc(&one, one_buf, size);
75 two_buf = read_sha1_file(hash2, &type, &size);
77 die("unable to read tree (%s)", sha1_to_hex(hash2));
79 die("%s is not a tree", sha1_to_hex(hash2));
80 init_tree_desc(&two, two_buf, size);
82 struct name_entry e1, e2;
83 int got_entry_from_one = tree_entry(&one, &e1);
84 int got_entry_from_two = tree_entry(&two, &e2);
87 if (got_entry_from_one && got_entry_from_two)
88 cmp = base_name_entries_compare(&e1, &e2);
89 else if (got_entry_from_one)
90 /* two lacks this entry */
92 else if (got_entry_from_two)
93 /* two has more entries */
99 /* path1 does not appear in two */
100 score += score_missing(e1.mode, e1.path);
102 /* path2 does not appear in one */
103 score += score_missing(e2.mode, e2.path);
104 else if (hashcmp(e1.sha1, e2.sha1))
105 /* they are different */
106 score += score_differs(e1.mode, e2.mode, e1.path);
108 /* same subtree or blob */
109 score += score_matches(e1.mode, e2.mode, e1.path);
117 * Match one itself and its subtrees with two and pick the best match.
119 static void match_trees(const unsigned char *hash1,
120 const unsigned char *hash2,
126 struct tree_desc one;
128 enum object_type type;
131 one_buf = read_sha1_file(hash1, &type, &size);
133 die("unable to read tree (%s)", sha1_to_hex(hash1));
134 if (type != OBJ_TREE)
135 die("%s is not a tree", sha1_to_hex(hash1));
136 init_tree_desc(&one, one_buf, size);
140 const unsigned char *elem;
144 elem = tree_entry_extract(&one, &path, &mode);
147 score = score_trees(elem, hash2);
148 if (*best_score < score) {
150 newpath = xmalloc(strlen(base) + strlen(path) + 1);
151 sprintf(newpath, "%s%s", base, path);
153 *best_match = newpath;
158 newbase = xmalloc(strlen(base) + strlen(path) + 2);
159 sprintf(newbase, "%s%s/", base, path);
160 match_trees(elem, hash2, best_score, best_match,
161 newbase, recurse_limit - 1);
166 update_tree_entry(&one);
172 * A tree "hash1" has a subdirectory at "prefix". Come up with a
173 * tree object by replacing it with another tree "hash2".
175 static int splice_tree(const unsigned char *hash1,
177 const unsigned char *hash2,
178 unsigned char *result)
184 struct tree_desc desc;
185 unsigned char *rewrite_here;
186 const unsigned char *rewrite_with;
187 unsigned char subtree[20];
188 enum object_type type;
191 subpath = strchr(prefix, '/');
193 toplen = strlen(prefix);
195 toplen = subpath - prefix;
199 buf = read_sha1_file(hash1, &type, &sz);
201 die("cannot read tree %s", sha1_to_hex(hash1));
202 init_tree_desc(&desc, buf, sz);
208 const unsigned char *sha1;
210 sha1 = tree_entry_extract(&desc, &name, &mode);
211 if (strlen(name) == toplen &&
212 !memcmp(name, prefix, toplen)) {
214 die("entry %s in tree %s is not a tree",
215 name, sha1_to_hex(hash1));
216 rewrite_here = (unsigned char *) sha1;
219 update_tree_entry(&desc);
222 die("entry %.*s not found in tree %s",
223 toplen, prefix, sha1_to_hex(hash1));
225 status = splice_tree(rewrite_here, subpath, hash2, subtree);
228 rewrite_with = subtree;
231 rewrite_with = hash2;
232 hashcpy(rewrite_here, rewrite_with);
233 status = write_sha1_file(buf, sz, tree_type, result);
239 * We are trying to come up with a merge between one and two that
240 * results in a tree shape similar to one. The tree two might
241 * correspond to a subtree of one, in which case it needs to be
242 * shifted down by prefixing otherwise empty directories. On the
243 * other hand, it could cover tree one and we might need to pick a
246 void shift_tree(const unsigned char *hash1,
247 const unsigned char *hash2,
248 unsigned char *shifted,
253 int add_score, del_score;
256 * NEEDSWORK: this limits the recursion depth to hardcoded
257 * value '2' to avoid excessive overhead.
262 add_score = del_score = score_trees(hash1, hash2);
263 add_prefix = xcalloc(1, 1);
264 del_prefix = xcalloc(1, 1);
267 * See if one's subtree resembles two; if so we need to prefix
268 * two with a few fake trees to match the prefix.
270 match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
273 * See if two's subtree resembles one; if so we need to
274 * pick only subtree of two.
276 match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
278 /* Assume we do not have to do any shifting */
279 hashcpy(shifted, hash2);
281 if (add_score < del_score) {
282 /* We need to pick a subtree of two */
288 if (get_tree_entry(hash2, del_prefix, shifted, &mode))
289 die("cannot find path %s in tree %s",
290 del_prefix, sha1_to_hex(hash2));
297 splice_tree(hash1, add_prefix, hash2, shifted);
301 * The user says the trees will be shifted by this much.
302 * Unfortunately we cannot fundamentally tell which one to
303 * be prefixed, as recursive merge can work in either direction.
305 void shift_tree_by(const unsigned char *hash1,
306 const unsigned char *hash2,
307 unsigned char *shifted,
308 const char *shift_prefix)
310 unsigned char sub1[20], sub2[20];
311 unsigned mode1, mode2;
312 unsigned candidate = 0;
314 /* Can hash2 be a tree at shift_prefix in tree hash1? */
315 if (!get_tree_entry(hash1, shift_prefix, sub1, &mode1) &&
319 /* Can hash1 be a tree at shift_prefix in tree hash2? */
320 if (!get_tree_entry(hash2, shift_prefix, sub2, &mode2) &&
324 if (candidate == 3) {
325 /* Both are plausible -- we need to evaluate the score */
326 int best_score = score_trees(hash1, hash2);
330 score = score_trees(sub1, hash2);
331 if (score > best_score) {
335 score = score_trees(sub2, hash1);
336 if (score > best_score)
341 /* Neither is plausible -- do not shift */
342 hashcpy(shifted, hash2);
348 * shift tree2 down by adding shift_prefix above it
351 splice_tree(hash1, shift_prefix, hash2, shifted);
354 * shift tree2 up by removing shift_prefix from it
357 hashcpy(shifted, sub2);