Git 2.44
[git.git] / match-trees.c
blob0885ac681cd5055f80fb5088b04c92eed8e7f291
1 #include "git-compat-util.h"
2 #include "hex.h"
3 #include "match-trees.h"
4 #include "strbuf.h"
5 #include "tree.h"
6 #include "tree-walk.h"
7 #include "object-store-ll.h"
9 static int score_missing(unsigned mode)
11 int score;
13 if (S_ISDIR(mode))
14 score = -1000;
15 else if (S_ISLNK(mode))
16 score = -500;
17 else
18 score = -50;
19 return score;
22 static int score_differs(unsigned mode1, unsigned mode2)
24 int score;
26 if (S_ISDIR(mode1) != S_ISDIR(mode2))
27 score = -100;
28 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
29 score = -50;
30 else
31 score = -5;
32 return score;
35 static int score_matches(unsigned mode1, unsigned mode2)
37 int score;
39 /* Heh, we found SHA-1 collisions between different kind of objects */
40 if (S_ISDIR(mode1) != S_ISDIR(mode2))
41 score = -100;
42 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
43 score = -50;
45 else if (S_ISDIR(mode1))
46 score = 1000;
47 else if (S_ISLNK(mode1))
48 score = 500;
49 else
50 score = 250;
51 return score;
54 static void *fill_tree_desc_strict(struct tree_desc *desc,
55 const struct object_id *hash)
57 void *buffer;
58 enum object_type type;
59 unsigned long size;
61 buffer = repo_read_object_file(the_repository, hash, &type, &size);
62 if (!buffer)
63 die("unable to read tree (%s)", oid_to_hex(hash));
64 if (type != OBJ_TREE)
65 die("%s is not a tree", oid_to_hex(hash));
66 init_tree_desc(desc, buffer, size);
67 return buffer;
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)
82 struct tree_desc one;
83 struct tree_desc two;
84 void *one_buf = fill_tree_desc_strict(&one, hash1);
85 void *two_buf = fill_tree_desc_strict(&two, hash2);
86 int score = 0;
88 for (;;) {
89 int cmp;
91 if (one.size && two.size)
92 cmp = base_name_entries_compare(&one.entry, &two.entry);
93 else if (one.size)
94 /* two lacks this entry */
95 cmp = -1;
96 else if (two.size)
97 /* two has more entries */
98 cmp = 1;
99 else
100 break;
102 if (cmp < 0) {
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);
110 } else {
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,
115 two.entry.mode);
116 } else {
117 /* same subtree or blob */
118 score += score_matches(one.entry.mode,
119 two.entry.mode);
121 update_tree_entry(&one);
122 update_tree_entry(&two);
125 free(one_buf);
126 free(two_buf);
127 return score;
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,
135 int *best_score,
136 char **best_match,
137 const char *base,
138 int recurse_limit)
140 struct tree_desc one;
141 void *one_buf = fill_tree_desc_strict(&one, hash1);
143 while (one.size) {
144 const char *path;
145 const struct object_id *elem;
146 unsigned short mode;
147 int score;
149 elem = tree_entry_extract(&one, &path, &mode);
150 if (!S_ISDIR(mode))
151 goto next;
152 score = score_trees(elem, hash2);
153 if (*best_score < score) {
154 free(*best_match);
155 *best_match = xstrfmt("%s%s", base, path);
156 *best_score = score;
158 if (recurse_limit) {
159 char *newbase = xstrfmt("%s%s/", base, path);
160 match_trees(elem, hash2, best_score, best_match,
161 newbase, recurse_limit - 1);
162 free(newbase);
165 next:
166 update_tree_entry(&one);
168 free(one_buf);
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)
178 char *subpath;
179 int toplen;
180 char *buf;
181 unsigned long sz;
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;
187 int status;
189 subpath = strchrnul(prefix, '/');
190 toplen = subpath - prefix;
191 if (*subpath)
192 subpath++;
194 buf = repo_read_object_file(the_repository, oid1, &type, &sz);
195 if (!buf)
196 die("cannot read tree %s", oid_to_hex(oid1));
197 init_tree_desc(&desc, buf, sz);
199 rewrite_here = NULL;
200 while (desc.size) {
201 const char *name;
202 unsigned short mode;
204 tree_entry_extract(&desc, &name, &mode);
205 if (strlen(name) == toplen &&
206 !memcmp(name, prefix, toplen)) {
207 if (!S_ISDIR(mode))
208 die("entry %s in tree %s is not a tree", name,
209 oid_to_hex(oid1));
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) +
223 break;
225 update_tree_entry(&desc);
227 if (!rewrite_here)
228 die("entry %.*s not found in tree %s", toplen, prefix,
229 oid_to_hex(oid1));
230 if (*subpath) {
231 struct object_id tree_oid;
232 oidread(&tree_oid, rewrite_here);
233 status = splice_tree(&tree_oid, subpath, oid2, &subtree);
234 if (status)
235 return status;
236 rewrite_with = &subtree;
237 } else {
238 rewrite_with = oid2;
240 hashcpy(rewrite_here, rewrite_with->hash);
241 status = write_object_file(buf, sz, OBJ_TREE, result);
242 free(buf);
243 return status;
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
252 * subtree of it.
254 void shift_tree(struct repository *r,
255 const struct object_id *hash1,
256 const struct object_id *hash2,
257 struct object_id *shifted,
258 int depth_limit)
260 char *add_prefix;
261 char *del_prefix;
262 int add_score, del_score;
265 * NEEDSWORK: this limits the recursion depth to hardcoded
266 * value '2' to avoid excessive overhead.
268 if (!depth_limit)
269 depth_limit = 2;
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 */
292 unsigned short mode;
294 if (!*del_prefix)
295 return;
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));
300 return;
303 if (!*add_prefix)
304 return;
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) &&
326 S_ISDIR(mode1))
327 candidate |= 1;
329 /* Can hash1 be a tree at shift_prefix in tree hash2? */
330 if (!get_tree_entry(r, hash2, shift_prefix, &sub2, &mode2) &&
331 S_ISDIR(mode2))
332 candidate |= 2;
334 if (candidate == 3) {
335 /* Both are plausible -- we need to evaluate the score */
336 int best_score = score_trees(hash1, hash2);
337 int score;
339 candidate = 0;
340 score = score_trees(&sub1, hash2);
341 if (score > best_score) {
342 candidate = 1;
343 best_score = score;
345 score = score_trees(&sub2, hash1);
346 if (score > best_score)
347 candidate = 2;
350 if (!candidate) {
351 /* Neither is plausible -- do not shift */
352 oidcpy(shifted, hash2);
353 return;
356 if (candidate == 1)
358 * shift tree2 down by adding shift_prefix above it
359 * to match tree1.
361 splice_tree(hash1, shift_prefix, hash2, shifted);
362 else
364 * shift tree2 up by removing shift_prefix from it
365 * to match tree1.
367 oidcpy(shifted, &sub2);