1 #define DISABLE_SIGN_COMPARE_WARNINGS
3 #include "git-compat-util.h"
5 #include "match-trees.h"
9 #include "object-file.h"
11 #include "repository.h"
13 static int score_missing(unsigned mode
)
19 else if (S_ISLNK(mode
))
26 static int score_differs(unsigned mode1
, unsigned mode2
)
30 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
32 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
39 static int score_matches(unsigned mode1
, unsigned mode2
)
43 /* Heh, we found SHA-1 collisions between different kind of objects */
44 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
46 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
49 else if (S_ISDIR(mode1
))
51 else if (S_ISLNK(mode1
))
58 static void *fill_tree_desc_strict(struct repository
*r
,
59 struct tree_desc
*desc
,
60 const struct object_id
*hash
)
63 enum object_type type
;
66 buffer
= odb_read_object(r
->objects
, hash
, &type
, &size
);
68 die("unable to read tree (%s)", oid_to_hex(hash
));
70 die("%s is not a tree", oid_to_hex(hash
));
71 init_tree_desc(desc
, hash
, buffer
, size
);
75 static int base_name_entries_compare(const struct name_entry
*a
,
76 const struct name_entry
*b
)
78 return base_name_compare(a
->path
, tree_entry_len(a
), a
->mode
,
79 b
->path
, tree_entry_len(b
), b
->mode
);
83 * Inspect two trees, and give a score that tells how similar they are.
85 static int score_trees(struct repository
*r
,
86 const struct object_id
*hash1
, const struct object_id
*hash2
)
90 void *one_buf
= fill_tree_desc_strict(r
, &one
, hash1
);
91 void *two_buf
= fill_tree_desc_strict(r
, &two
, hash2
);
97 if (one
.size
&& two
.size
)
98 cmp
= base_name_entries_compare(&one
.entry
, &two
.entry
);
100 /* two lacks this entry */
103 /* two has more entries */
109 /* path1 does not appear in two */
110 score
+= score_missing(one
.entry
.mode
);
111 update_tree_entry(&one
);
112 } else if (cmp
> 0) {
113 /* path2 does not appear in one */
114 score
+= score_missing(two
.entry
.mode
);
115 update_tree_entry(&two
);
117 /* path appears in both */
118 if (!oideq(&one
.entry
.oid
, &two
.entry
.oid
)) {
119 /* they are different */
120 score
+= score_differs(one
.entry
.mode
,
123 /* same subtree or blob */
124 score
+= score_matches(one
.entry
.mode
,
127 update_tree_entry(&one
);
128 update_tree_entry(&two
);
137 * Match one itself and its subtrees with two and pick the best match.
139 static void match_trees(struct repository
*r
,
140 const struct object_id
*hash1
,
141 const struct object_id
*hash2
,
147 struct tree_desc one
;
148 void *one_buf
= fill_tree_desc_strict(r
, &one
, hash1
);
152 const struct object_id
*elem
;
156 elem
= tree_entry_extract(&one
, &path
, &mode
);
159 score
= score_trees(r
, elem
, hash2
);
160 if (*best_score
< score
) {
162 *best_match
= xstrfmt("%s%s", base
, path
);
166 char *newbase
= xstrfmt("%s%s/", base
, path
);
167 match_trees(r
, elem
, hash2
, best_score
, best_match
,
168 newbase
, recurse_limit
- 1);
173 update_tree_entry(&one
);
179 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
180 * replacing it with another tree "oid2".
182 static int splice_tree(struct repository
*r
,
183 const struct object_id
*oid1
, const char *prefix
,
184 const struct object_id
*oid2
, struct object_id
*result
)
190 struct tree_desc desc
;
191 unsigned char *rewrite_here
;
192 const struct object_id
*rewrite_with
;
193 struct object_id subtree
;
194 enum object_type type
;
197 subpath
= strchrnul(prefix
, '/');
198 toplen
= subpath
- prefix
;
202 buf
= odb_read_object(r
->objects
, oid1
, &type
, &sz
);
204 die("cannot read tree %s", oid_to_hex(oid1
));
205 init_tree_desc(&desc
, oid1
, buf
, sz
);
212 tree_entry_extract(&desc
, &name
, &mode
);
213 if (strlen(name
) == toplen
&&
214 !memcmp(name
, prefix
, toplen
)) {
216 die("entry %s in tree %s is not a tree", name
,
220 * We cast here for two reasons:
222 * - to flip the "char *" (for the path) to "unsigned
223 * char *" (for the hash stored after it)
225 * - to discard the "const"; this is OK because we
226 * know it points into our non-const "buf"
228 rewrite_here
= (unsigned char *)(desc
.entry
.path
+
229 strlen(desc
.entry
.path
) +
233 update_tree_entry(&desc
);
236 die("entry %.*s not found in tree %s", toplen
, prefix
,
239 struct object_id tree_oid
;
240 oidread(&tree_oid
, rewrite_here
, r
->hash_algo
);
241 status
= splice_tree(r
, &tree_oid
, subpath
, oid2
, &subtree
);
244 rewrite_with
= &subtree
;
248 hashcpy(rewrite_here
, rewrite_with
->hash
, r
->hash_algo
);
249 status
= write_object_file(buf
, sz
, OBJ_TREE
, result
);
255 * We are trying to come up with a merge between one and two that
256 * results in a tree shape similar to one. The tree two might
257 * correspond to a subtree of one, in which case it needs to be
258 * shifted down by prefixing otherwise empty directories. On the
259 * other hand, it could cover tree one and we might need to pick a
262 void shift_tree(struct repository
*r
,
263 const struct object_id
*hash1
,
264 const struct object_id
*hash2
,
265 struct object_id
*shifted
,
270 int add_score
, del_score
;
273 * NEEDSWORK: this limits the recursion depth to hardcoded
274 * value '2' to avoid excessive overhead.
279 add_score
= del_score
= score_trees(r
, hash1
, hash2
);
280 add_prefix
= xcalloc(1, 1);
281 del_prefix
= xcalloc(1, 1);
284 * See if one's subtree resembles two; if so we need to prefix
285 * two with a few fake trees to match the prefix.
287 match_trees(r
, hash1
, hash2
, &add_score
, &add_prefix
, "", depth_limit
);
290 * See if two's subtree resembles one; if so we need to
291 * pick only subtree of two.
293 match_trees(r
, hash2
, hash1
, &del_score
, &del_prefix
, "", depth_limit
);
295 /* Assume we do not have to do any shifting */
296 oidcpy(shifted
, hash2
);
298 if (add_score
< del_score
) {
299 /* We need to pick a subtree of two */
305 if (get_tree_entry(r
, hash2
, del_prefix
, shifted
, &mode
))
306 die("cannot find path %s in tree %s",
307 del_prefix
, oid_to_hex(hash2
));
314 splice_tree(r
, hash1
, add_prefix
, hash2
, shifted
);
322 * The user says the trees will be shifted by this much.
323 * Unfortunately we cannot fundamentally tell which one to
324 * be prefixed, as recursive merge can work in either direction.
326 void shift_tree_by(struct repository
*r
,
327 const struct object_id
*hash1
,
328 const struct object_id
*hash2
,
329 struct object_id
*shifted
,
330 const char *shift_prefix
)
332 struct object_id sub1
, sub2
;
333 unsigned short mode1
, mode2
;
334 unsigned candidate
= 0;
336 /* Can hash2 be a tree at shift_prefix in tree hash1? */
337 if (!get_tree_entry(r
, hash1
, shift_prefix
, &sub1
, &mode1
) &&
341 /* Can hash1 be a tree at shift_prefix in tree hash2? */
342 if (!get_tree_entry(r
, hash2
, shift_prefix
, &sub2
, &mode2
) &&
346 if (candidate
== 3) {
347 /* Both are plausible -- we need to evaluate the score */
348 int best_score
= score_trees(r
, hash1
, hash2
);
352 score
= score_trees(r
, &sub1
, hash2
);
353 if (score
> best_score
) {
357 score
= score_trees(r
, &sub2
, hash1
);
358 if (score
> best_score
)
363 /* Neither is plausible -- do not shift */
364 oidcpy(shifted
, hash2
);
370 * shift tree2 down by adding shift_prefix above it
373 splice_tree(r
, hash1
, shift_prefix
, hash2
, shifted
);
376 * shift tree2 up by removing shift_prefix from it
379 oidcpy(shifted
, &sub2
);