5 #include "object-store.h"
7 static int score_missing(unsigned mode
)
13 else if (S_ISLNK(mode
))
20 static int score_differs(unsigned mode1
, unsigned mode2
)
24 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
26 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
33 static int score_matches(unsigned mode1
, unsigned mode2
)
37 /* Heh, we found SHA-1 collisions between different kind of objects */
38 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
40 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
43 else if (S_ISDIR(mode1
))
45 else if (S_ISLNK(mode1
))
52 static void *fill_tree_desc_strict(struct tree_desc
*desc
,
53 const struct object_id
*hash
)
56 enum object_type type
;
59 buffer
= repo_read_object_file(the_repository
, hash
, &type
, &size
);
61 die("unable to read tree (%s)", oid_to_hex(hash
));
63 die("%s is not a tree", oid_to_hex(hash
));
64 init_tree_desc(desc
, buffer
, size
);
68 static int base_name_entries_compare(const struct name_entry
*a
,
69 const struct name_entry
*b
)
71 return base_name_compare(a
->path
, tree_entry_len(a
), a
->mode
,
72 b
->path
, tree_entry_len(b
), b
->mode
);
76 * Inspect two trees, and give a score that tells how similar they are.
78 static int score_trees(const struct object_id
*hash1
, const struct object_id
*hash2
)
82 void *one_buf
= fill_tree_desc_strict(&one
, hash1
);
83 void *two_buf
= fill_tree_desc_strict(&two
, hash2
);
89 if (one
.size
&& two
.size
)
90 cmp
= base_name_entries_compare(&one
.entry
, &two
.entry
);
92 /* two lacks this entry */
95 /* two has more entries */
101 /* path1 does not appear in two */
102 score
+= score_missing(one
.entry
.mode
);
103 update_tree_entry(&one
);
104 } else if (cmp
> 0) {
105 /* path2 does not appear in one */
106 score
+= score_missing(two
.entry
.mode
);
107 update_tree_entry(&two
);
109 /* path appears in both */
110 if (!oideq(&one
.entry
.oid
, &two
.entry
.oid
)) {
111 /* they are different */
112 score
+= score_differs(one
.entry
.mode
,
115 /* same subtree or blob */
116 score
+= score_matches(one
.entry
.mode
,
119 update_tree_entry(&one
);
120 update_tree_entry(&two
);
129 * Match one itself and its subtrees with two and pick the best match.
131 static void match_trees(const struct object_id
*hash1
,
132 const struct object_id
*hash2
,
138 struct tree_desc one
;
139 void *one_buf
= fill_tree_desc_strict(&one
, hash1
);
143 const struct object_id
*elem
;
147 elem
= tree_entry_extract(&one
, &path
, &mode
);
150 score
= score_trees(elem
, hash2
);
151 if (*best_score
< score
) {
153 *best_match
= xstrfmt("%s%s", base
, path
);
157 char *newbase
= xstrfmt("%s%s/", base
, path
);
158 match_trees(elem
, hash2
, best_score
, best_match
,
159 newbase
, recurse_limit
- 1);
164 update_tree_entry(&one
);
170 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
171 * replacing it with another tree "oid2".
173 static int splice_tree(const struct object_id
*oid1
, const char *prefix
,
174 const struct object_id
*oid2
, struct object_id
*result
)
180 struct tree_desc desc
;
181 unsigned char *rewrite_here
;
182 const struct object_id
*rewrite_with
;
183 struct object_id subtree
;
184 enum object_type type
;
187 subpath
= strchrnul(prefix
, '/');
188 toplen
= subpath
- prefix
;
192 buf
= repo_read_object_file(the_repository
, oid1
, &type
, &sz
);
194 die("cannot read tree %s", oid_to_hex(oid1
));
195 init_tree_desc(&desc
, buf
, sz
);
202 tree_entry_extract(&desc
, &name
, &mode
);
203 if (strlen(name
) == toplen
&&
204 !memcmp(name
, prefix
, toplen
)) {
206 die("entry %s in tree %s is not a tree", name
,
210 * We cast here for two reasons:
212 * - to flip the "char *" (for the path) to "unsigned
213 * char *" (for the hash stored after it)
215 * - to discard the "const"; this is OK because we
216 * know it points into our non-const "buf"
218 rewrite_here
= (unsigned char *)(desc
.entry
.path
+
219 strlen(desc
.entry
.path
) +
223 update_tree_entry(&desc
);
226 die("entry %.*s not found in tree %s", toplen
, prefix
,
229 struct object_id tree_oid
;
230 oidread(&tree_oid
, rewrite_here
);
231 status
= splice_tree(&tree_oid
, subpath
, oid2
, &subtree
);
234 rewrite_with
= &subtree
;
238 hashcpy(rewrite_here
, rewrite_with
->hash
);
239 status
= write_object_file(buf
, sz
, OBJ_TREE
, result
);
245 * We are trying to come up with a merge between one and two that
246 * results in a tree shape similar to one. The tree two might
247 * correspond to a subtree of one, in which case it needs to be
248 * shifted down by prefixing otherwise empty directories. On the
249 * other hand, it could cover tree one and we might need to pick a
252 void shift_tree(struct repository
*r
,
253 const struct object_id
*hash1
,
254 const struct object_id
*hash2
,
255 struct object_id
*shifted
,
260 int add_score
, del_score
;
263 * NEEDSWORK: this limits the recursion depth to hardcoded
264 * value '2' to avoid excessive overhead.
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 */
295 if (get_tree_entry(r
, hash2
, del_prefix
, shifted
, &mode
))
296 die("cannot find path %s in tree %s",
297 del_prefix
, oid_to_hex(hash2
));
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(struct repository
*r
,
313 const struct object_id
*hash1
,
314 const struct object_id
*hash2
,
315 struct object_id
*shifted
,
316 const char *shift_prefix
)
318 struct object_id sub1
, sub2
;
319 unsigned short mode1
, mode2
;
320 unsigned candidate
= 0;
322 /* Can hash2 be a tree at shift_prefix in tree hash1? */
323 if (!get_tree_entry(r
, hash1
, shift_prefix
, &sub1
, &mode1
) &&
327 /* Can hash1 be a tree at shift_prefix in tree hash2? */
328 if (!get_tree_entry(r
, hash2
, shift_prefix
, &sub2
, &mode2
) &&
332 if (candidate
== 3) {
333 /* Both are plausible -- we need to evaluate the score */
334 int best_score
= score_trees(hash1
, hash2
);
338 score
= score_trees(&sub1
, hash2
);
339 if (score
> best_score
) {
343 score
= score_trees(&sub2
, hash1
);
344 if (score
> best_score
)
349 /* Neither is plausible -- do not shift */
350 oidcpy(shifted
, hash2
);
356 * shift tree2 down by adding shift_prefix above it
359 splice_tree(hash1
, shift_prefix
, hash2
, shifted
);
362 * shift tree2 up by removing shift_prefix from it
365 oidcpy(shifted
, &sub2
);