1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
5 #include "match-trees.h"
9 #include "object-store-ll.h"
11 static int score_missing(unsigned mode
)
17 else if (S_ISLNK(mode
))
24 static int score_differs(unsigned mode1
, unsigned mode2
)
28 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
30 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
37 static int score_matches(unsigned mode1
, unsigned mode2
)
41 /* Heh, we found SHA-1 collisions between different kind of objects */
42 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
44 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
47 else if (S_ISDIR(mode1
))
49 else if (S_ISLNK(mode1
))
56 static void *fill_tree_desc_strict(struct tree_desc
*desc
,
57 const struct object_id
*hash
)
60 enum object_type type
;
63 buffer
= repo_read_object_file(the_repository
, hash
, &type
, &size
);
65 die("unable to read tree (%s)", oid_to_hex(hash
));
67 die("%s is not a tree", oid_to_hex(hash
));
68 init_tree_desc(desc
, hash
, buffer
, size
);
72 static int base_name_entries_compare(const struct name_entry
*a
,
73 const struct name_entry
*b
)
75 return base_name_compare(a
->path
, tree_entry_len(a
), a
->mode
,
76 b
->path
, tree_entry_len(b
), b
->mode
);
80 * Inspect two trees, and give a score that tells how similar they are.
82 static int score_trees(const struct object_id
*hash1
, const struct object_id
*hash2
)
86 void *one_buf
= fill_tree_desc_strict(&one
, hash1
);
87 void *two_buf
= fill_tree_desc_strict(&two
, hash2
);
93 if (one
.size
&& two
.size
)
94 cmp
= base_name_entries_compare(&one
.entry
, &two
.entry
);
96 /* two lacks this entry */
99 /* two has more entries */
105 /* path1 does not appear in two */
106 score
+= score_missing(one
.entry
.mode
);
107 update_tree_entry(&one
);
108 } else if (cmp
> 0) {
109 /* path2 does not appear in one */
110 score
+= score_missing(two
.entry
.mode
);
111 update_tree_entry(&two
);
113 /* path appears in both */
114 if (!oideq(&one
.entry
.oid
, &two
.entry
.oid
)) {
115 /* they are different */
116 score
+= score_differs(one
.entry
.mode
,
119 /* same subtree or blob */
120 score
+= score_matches(one
.entry
.mode
,
123 update_tree_entry(&one
);
124 update_tree_entry(&two
);
133 * Match one itself and its subtrees with two and pick the best match.
135 static void match_trees(const struct object_id
*hash1
,
136 const struct object_id
*hash2
,
142 struct tree_desc one
;
143 void *one_buf
= fill_tree_desc_strict(&one
, hash1
);
147 const struct object_id
*elem
;
151 elem
= tree_entry_extract(&one
, &path
, &mode
);
154 score
= score_trees(elem
, hash2
);
155 if (*best_score
< score
) {
157 *best_match
= xstrfmt("%s%s", base
, path
);
161 char *newbase
= xstrfmt("%s%s/", base
, path
);
162 match_trees(elem
, hash2
, best_score
, best_match
,
163 newbase
, recurse_limit
- 1);
168 update_tree_entry(&one
);
174 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
175 * replacing it with another tree "oid2".
177 static int splice_tree(const struct object_id
*oid1
, const char *prefix
,
178 const struct object_id
*oid2
, struct object_id
*result
)
184 struct tree_desc desc
;
185 unsigned char *rewrite_here
;
186 const struct object_id
*rewrite_with
;
187 struct object_id subtree
;
188 enum object_type type
;
191 subpath
= strchrnul(prefix
, '/');
192 toplen
= subpath
- prefix
;
196 buf
= repo_read_object_file(the_repository
, oid1
, &type
, &sz
);
198 die("cannot read tree %s", oid_to_hex(oid1
));
199 init_tree_desc(&desc
, oid1
, buf
, sz
);
206 tree_entry_extract(&desc
, &name
, &mode
);
207 if (strlen(name
) == toplen
&&
208 !memcmp(name
, prefix
, toplen
)) {
210 die("entry %s in tree %s is not a tree", name
,
214 * We cast here for two reasons:
216 * - to flip the "char *" (for the path) to "unsigned
217 * char *" (for the hash stored after it)
219 * - to discard the "const"; this is OK because we
220 * know it points into our non-const "buf"
222 rewrite_here
= (unsigned char *)(desc
.entry
.path
+
223 strlen(desc
.entry
.path
) +
227 update_tree_entry(&desc
);
230 die("entry %.*s not found in tree %s", toplen
, prefix
,
233 struct object_id tree_oid
;
234 oidread(&tree_oid
, rewrite_here
, the_repository
->hash_algo
);
235 status
= splice_tree(&tree_oid
, subpath
, oid2
, &subtree
);
238 rewrite_with
= &subtree
;
242 hashcpy(rewrite_here
, rewrite_with
->hash
, the_repository
->hash_algo
);
243 status
= write_object_file(buf
, sz
, OBJ_TREE
, result
);
249 * We are trying to come up with a merge between one and two that
250 * results in a tree shape similar to one. The tree two might
251 * correspond to a subtree of one, in which case it needs to be
252 * shifted down by prefixing otherwise empty directories. On the
253 * other hand, it could cover tree one and we might need to pick a
256 void shift_tree(struct repository
*r
,
257 const struct object_id
*hash1
,
258 const struct object_id
*hash2
,
259 struct object_id
*shifted
,
264 int add_score
, del_score
;
267 * NEEDSWORK: this limits the recursion depth to hardcoded
268 * value '2' to avoid excessive overhead.
273 add_score
= del_score
= score_trees(hash1
, hash2
);
274 add_prefix
= xcalloc(1, 1);
275 del_prefix
= xcalloc(1, 1);
278 * See if one's subtree resembles two; if so we need to prefix
279 * two with a few fake trees to match the prefix.
281 match_trees(hash1
, hash2
, &add_score
, &add_prefix
, "", depth_limit
);
284 * See if two's subtree resembles one; if so we need to
285 * pick only subtree of two.
287 match_trees(hash2
, hash1
, &del_score
, &del_prefix
, "", depth_limit
);
289 /* Assume we do not have to do any shifting */
290 oidcpy(shifted
, hash2
);
292 if (add_score
< del_score
) {
293 /* We need to pick a subtree of two */
299 if (get_tree_entry(r
, hash2
, del_prefix
, shifted
, &mode
))
300 die("cannot find path %s in tree %s",
301 del_prefix
, oid_to_hex(hash2
));
308 splice_tree(hash1
, add_prefix
, hash2
, shifted
);
316 * The user says the trees will be shifted by this much.
317 * Unfortunately we cannot fundamentally tell which one to
318 * be prefixed, as recursive merge can work in either direction.
320 void shift_tree_by(struct repository
*r
,
321 const struct object_id
*hash1
,
322 const struct object_id
*hash2
,
323 struct object_id
*shifted
,
324 const char *shift_prefix
)
326 struct object_id sub1
, sub2
;
327 unsigned short mode1
, mode2
;
328 unsigned candidate
= 0;
330 /* Can hash2 be a tree at shift_prefix in tree hash1? */
331 if (!get_tree_entry(r
, hash1
, shift_prefix
, &sub1
, &mode1
) &&
335 /* Can hash1 be a tree at shift_prefix in tree hash2? */
336 if (!get_tree_entry(r
, hash2
, shift_prefix
, &sub2
, &mode2
) &&
340 if (candidate
== 3) {
341 /* Both are plausible -- we need to evaluate the score */
342 int best_score
= score_trees(hash1
, hash2
);
346 score
= score_trees(&sub1
, hash2
);
347 if (score
> best_score
) {
351 score
= score_trees(&sub2
, hash1
);
352 if (score
> best_score
)
357 /* Neither is plausible -- do not shift */
358 oidcpy(shifted
, hash2
);
364 * shift tree2 down by adding shift_prefix above it
367 splice_tree(hash1
, shift_prefix
, hash2
, shifted
);
370 * shift tree2 up by removing shift_prefix from it
373 oidcpy(shifted
, &sub2
);