]>
git.ipfire.org Git - thirdparty/git.git/blob - match-trees.c
1 #include "git-compat-util.h"
3 #include "match-trees.h"
7 #include "object-store-ll.h"
9 static int score_missing(unsigned mode
)
15 else if (S_ISLNK(mode
))
22 static int score_differs(unsigned mode1
, unsigned mode2
)
26 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
28 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
35 static int score_matches(unsigned mode1
, unsigned mode2
)
39 /* Heh, we found SHA-1 collisions between different kind of objects */
40 if (S_ISDIR(mode1
) != S_ISDIR(mode2
))
42 else if (S_ISLNK(mode1
) != S_ISLNK(mode2
))
45 else if (S_ISDIR(mode1
))
47 else if (S_ISLNK(mode1
))
54 static void *fill_tree_desc_strict(struct tree_desc
*desc
,
55 const struct object_id
*hash
)
58 enum object_type type
;
61 buffer
= repo_read_object_file(the_repository
, hash
, &type
, &size
);
63 die("unable to read tree (%s)", oid_to_hex(hash
));
65 die("%s is not a tree", oid_to_hex(hash
));
66 init_tree_desc(desc
, buffer
, size
);
70 static int base_name_entries_compare(const struct name_entry
*a
,
71 const struct name_entry
*b
)
73 return base_name_compare(a
->path
, tree_entry_len(a
), a
->mode
,
74 b
->path
, tree_entry_len(b
), b
->mode
);
78 * Inspect two trees, and give a score that tells how similar they are.
80 static int score_trees(const struct object_id
*hash1
, const struct object_id
*hash2
)
84 void *one_buf
= fill_tree_desc_strict(&one
, hash1
);
85 void *two_buf
= fill_tree_desc_strict(&two
, hash2
);
91 if (one
.size
&& two
.size
)
92 cmp
= base_name_entries_compare(&one
.entry
, &two
.entry
);
94 /* two lacks this entry */
97 /* two has more entries */
103 /* path1 does not appear in two */
104 score
+= score_missing(one
.entry
.mode
);
105 update_tree_entry(&one
);
106 } else if (cmp
> 0) {
107 /* path2 does not appear in one */
108 score
+= score_missing(two
.entry
.mode
);
109 update_tree_entry(&two
);
111 /* path appears in both */
112 if (!oideq(&one
.entry
.oid
, &two
.entry
.oid
)) {
113 /* they are different */
114 score
+= score_differs(one
.entry
.mode
,
117 /* same subtree or blob */
118 score
+= score_matches(one
.entry
.mode
,
121 update_tree_entry(&one
);
122 update_tree_entry(&two
);
131 * Match one itself and its subtrees with two and pick the best match.
133 static void match_trees(const struct object_id
*hash1
,
134 const struct object_id
*hash2
,
140 struct tree_desc one
;
141 void *one_buf
= fill_tree_desc_strict(&one
, hash1
);
145 const struct object_id
*elem
;
149 elem
= tree_entry_extract(&one
, &path
, &mode
);
152 score
= score_trees(elem
, hash2
);
153 if (*best_score
< score
) {
155 *best_match
= xstrfmt("%s%s", base
, path
);
159 char *newbase
= xstrfmt("%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 "oid1" has a subdirectory at "prefix". Come up with a tree object by
173 * replacing it with another tree "oid2".
175 static int splice_tree(const struct object_id
*oid1
, const char *prefix
,
176 const struct object_id
*oid2
, struct object_id
*result
)
182 struct tree_desc desc
;
183 unsigned char *rewrite_here
;
184 const struct object_id
*rewrite_with
;
185 struct object_id subtree
;
186 enum object_type type
;
189 subpath
= strchrnul(prefix
, '/');
190 toplen
= subpath
- prefix
;
194 buf
= repo_read_object_file(the_repository
, oid1
, &type
, &sz
);
196 die("cannot read tree %s", oid_to_hex(oid1
));
197 init_tree_desc(&desc
, buf
, sz
);
204 tree_entry_extract(&desc
, &name
, &mode
);
205 if (strlen(name
) == toplen
&&
206 !memcmp(name
, prefix
, toplen
)) {
208 die("entry %s in tree %s is not a tree", name
,
212 * We cast here for two reasons:
214 * - to flip the "char *" (for the path) to "unsigned
215 * char *" (for the hash stored after it)
217 * - to discard the "const"; this is OK because we
218 * know it points into our non-const "buf"
220 rewrite_here
= (unsigned char *)(desc
.entry
.path
+
221 strlen(desc
.entry
.path
) +
225 update_tree_entry(&desc
);
228 die("entry %.*s not found in tree %s", toplen
, prefix
,
231 struct object_id tree_oid
;
232 oidread(&tree_oid
, rewrite_here
);
233 status
= splice_tree(&tree_oid
, subpath
, oid2
, &subtree
);
236 rewrite_with
= &subtree
;
240 hashcpy(rewrite_here
, rewrite_with
->hash
);
241 status
= write_object_file(buf
, sz
, OBJ_TREE
, result
);
247 * We are trying to come up with a merge between one and two that
248 * results in a tree shape similar to one. The tree two might
249 * correspond to a subtree of one, in which case it needs to be
250 * shifted down by prefixing otherwise empty directories. On the
251 * other hand, it could cover tree one and we might need to pick a
254 void shift_tree(struct repository
*r
,
255 const struct object_id
*hash1
,
256 const struct object_id
*hash2
,
257 struct object_id
*shifted
,
262 int add_score
, del_score
;
265 * NEEDSWORK: this limits the recursion depth to hardcoded
266 * value '2' to avoid excessive overhead.
271 add_score
= del_score
= score_trees(hash1
, hash2
);
272 add_prefix
= xcalloc(1, 1);
273 del_prefix
= xcalloc(1, 1);
276 * See if one's subtree resembles two; if so we need to prefix
277 * two with a few fake trees to match the prefix.
279 match_trees(hash1
, hash2
, &add_score
, &add_prefix
, "", depth_limit
);
282 * See if two's subtree resembles one; if so we need to
283 * pick only subtree of two.
285 match_trees(hash2
, hash1
, &del_score
, &del_prefix
, "", depth_limit
);
287 /* Assume we do not have to do any shifting */
288 oidcpy(shifted
, hash2
);
290 if (add_score
< del_score
) {
291 /* We need to pick a subtree of two */
297 if (get_tree_entry(r
, hash2
, del_prefix
, shifted
, &mode
))
298 die("cannot find path %s in tree %s",
299 del_prefix
, oid_to_hex(hash2
));
306 splice_tree(hash1
, add_prefix
, hash2
, shifted
);
310 * The user says the trees will be shifted by this much.
311 * Unfortunately we cannot fundamentally tell which one to
312 * be prefixed, as recursive merge can work in either direction.
314 void shift_tree_by(struct repository
*r
,
315 const struct object_id
*hash1
,
316 const struct object_id
*hash2
,
317 struct object_id
*shifted
,
318 const char *shift_prefix
)
320 struct object_id sub1
, sub2
;
321 unsigned short mode1
, mode2
;
322 unsigned candidate
= 0;
324 /* Can hash2 be a tree at shift_prefix in tree hash1? */
325 if (!get_tree_entry(r
, hash1
, shift_prefix
, &sub1
, &mode1
) &&
329 /* Can hash1 be a tree at shift_prefix in tree hash2? */
330 if (!get_tree_entry(r
, hash2
, shift_prefix
, &sub2
, &mode2
) &&
334 if (candidate
== 3) {
335 /* Both are plausible -- we need to evaluate the score */
336 int best_score
= score_trees(hash1
, hash2
);
340 score
= score_trees(&sub1
, hash2
);
341 if (score
> best_score
) {
345 score
= score_trees(&sub2
, hash1
);
346 if (score
> best_score
)
351 /* Neither is plausible -- do not shift */
352 oidcpy(shifted
, hash2
);
358 * shift tree2 down by adding shift_prefix above it
361 splice_tree(hash1
, shift_prefix
, hash2
, shifted
);
364 * shift tree2 up by removing shift_prefix from it
367 oidcpy(shifted
, &sub2
);