]> git.ipfire.org Git - thirdparty/git.git/blame - match-trees.c
match-trees: drop unused path parameter from score functions
[thirdparty/git.git] / match-trees.c
CommitLineData
68faf689
JH
1#include "cache.h"
2#include "tree.h"
3#include "tree-walk.h"
cbd53a21 4#include "object-store.h"
68faf689 5
667f71c3 6static int score_missing(unsigned mode)
68faf689
JH
7{
8 int score;
9
10 if (S_ISDIR(mode))
11 score = -1000;
12 else if (S_ISLNK(mode))
13 score = -500;
14 else
15 score = -50;
16 return score;
17}
18
667f71c3 19static int score_differs(unsigned mode1, unsigned mode2)
68faf689
JH
20{
21 int score;
22
23 if (S_ISDIR(mode1) != S_ISDIR(mode2))
24 score = -100;
25 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
26 score = -50;
27 else
28 score = -5;
29 return score;
30}
31
667f71c3 32static int score_matches(unsigned mode1, unsigned mode2)
68faf689
JH
33{
34 int score;
35
36 /* Heh, we found SHA-1 collisions between different kind of objects */
37 if (S_ISDIR(mode1) != S_ISDIR(mode2))
38 score = -100;
39 else if (S_ISLNK(mode1) != S_ISLNK(mode2))
40 score = -50;
41
42 else if (S_ISDIR(mode1))
43 score = 1000;
44 else if (S_ISLNK(mode1))
45 score = 500;
46 else
47 score = 250;
48 return score;
49}
50
10a3fb00 51static void *fill_tree_desc_strict(struct tree_desc *desc,
b6aec868 52 const struct object_id *hash)
10a3fb00
RS
53{
54 void *buffer;
55 enum object_type type;
56 unsigned long size;
57
b4f5aca4 58 buffer = read_object_file(hash, &type, &size);
10a3fb00 59 if (!buffer)
b6aec868 60 die("unable to read tree (%s)", oid_to_hex(hash));
10a3fb00 61 if (type != OBJ_TREE)
b6aec868 62 die("%s is not a tree", oid_to_hex(hash));
10a3fb00
RS
63 init_tree_desc(desc, buffer, size);
64 return buffer;
65}
66
d8febde3
RS
67static int base_name_entries_compare(const struct name_entry *a,
68 const struct name_entry *b)
69{
70 return base_name_compare(a->path, tree_entry_len(a), a->mode,
71 b->path, tree_entry_len(b), b->mode);
72}
73
68faf689
JH
74/*
75 * Inspect two trees, and give a score that tells how similar they are.
76 */
b6aec868 77static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
68faf689
JH
78{
79 struct tree_desc one;
80 struct tree_desc two;
10a3fb00
RS
81 void *one_buf = fill_tree_desc_strict(&one, hash1);
82 void *two_buf = fill_tree_desc_strict(&two, hash2);
68faf689 83 int score = 0;
68faf689 84
d8febde3 85 for (;;) {
68faf689
JH
86 int cmp;
87
2ec41507
JK
88 if (one.size && two.size)
89 cmp = base_name_entries_compare(&one.entry, &two.entry);
90 else if (one.size)
68faf689 91 /* two lacks this entry */
d8febde3 92 cmp = -1;
2ec41507 93 else if (two.size)
d8febde3
RS
94 /* two has more entries */
95 cmp = 1;
96 else
97 break;
98
2ec41507 99 if (cmp < 0) {
68faf689 100 /* path1 does not appear in two */
667f71c3 101 score += score_missing(one.entry.mode);
2ec41507
JK
102 update_tree_entry(&one);
103 } else if (cmp > 0) {
68faf689 104 /* path2 does not appear in one */
667f71c3 105 score += score_missing(two.entry.mode);
2ec41507
JK
106 update_tree_entry(&two);
107 } else {
108 /* path appears in both */
9001dc2a 109 if (!oideq(one.entry.oid, two.entry.oid)) {
2ec41507
JK
110 /* they are different */
111 score += score_differs(one.entry.mode,
667f71c3 112 two.entry.mode);
2ec41507
JK
113 } else {
114 /* same subtree or blob */
115 score += score_matches(one.entry.mode,
667f71c3 116 two.entry.mode);
2ec41507
JK
117 }
118 update_tree_entry(&one);
119 update_tree_entry(&two);
120 }
68faf689
JH
121 }
122 free(one_buf);
123 free(two_buf);
124 return score;
125}
126
127/*
128 * Match one itself and its subtrees with two and pick the best match.
129 */
b6aec868 130static void match_trees(const struct object_id *hash1,
131 const struct object_id *hash2,
68faf689
JH
132 int *best_score,
133 char **best_match,
538dfe73 134 const char *base,
68faf689
JH
135 int recurse_limit)
136{
137 struct tree_desc one;
10a3fb00 138 void *one_buf = fill_tree_desc_strict(&one, hash1);
68faf689
JH
139
140 while (one.size) {
141 const char *path;
ce6663a9 142 const struct object_id *elem;
68faf689
JH
143 unsigned mode;
144 int score;
145
146 elem = tree_entry_extract(&one, &path, &mode);
147 if (!S_ISDIR(mode))
148 goto next;
b6aec868 149 score = score_trees(elem, hash2);
68faf689 150 if (*best_score < score) {
68faf689 151 free(*best_match);
28310186 152 *best_match = xstrfmt("%s%s", base, path);
68faf689
JH
153 *best_score = score;
154 }
155 if (recurse_limit) {
28310186 156 char *newbase = xstrfmt("%s%s/", base, path);
b6aec868 157 match_trees(elem, hash2, best_score, best_match,
68faf689
JH
158 newbase, recurse_limit - 1);
159 free(newbase);
160 }
161
162 next:
163 update_tree_entry(&one);
164 }
165 free(one_buf);
166}
167
168/*
3b34934d
PO
169 * A tree "oid1" has a subdirectory at "prefix". Come up with a tree object by
170 * replacing it with another tree "oid2".
68faf689 171 */
3b34934d
PO
172static int splice_tree(const struct object_id *oid1, const char *prefix,
173 const struct object_id *oid2, struct object_id *result)
68faf689
JH
174{
175 char *subpath;
176 int toplen;
177 char *buf;
178 unsigned long sz;
179 struct tree_desc desc;
3b34934d
PO
180 struct object_id *rewrite_here;
181 const struct object_id *rewrite_with;
182 struct object_id subtree;
68faf689
JH
183 enum object_type type;
184 int status;
185
2c5495f7
RM
186 subpath = strchrnul(prefix, '/');
187 toplen = subpath - prefix;
188 if (*subpath)
68faf689 189 subpath++;
68faf689 190
b4f5aca4 191 buf = read_object_file(oid1, &type, &sz);
68faf689 192 if (!buf)
3b34934d 193 die("cannot read tree %s", oid_to_hex(oid1));
68faf689
JH
194 init_tree_desc(&desc, buf, sz);
195
196 rewrite_here = NULL;
197 while (desc.size) {
198 const char *name;
199 unsigned mode;
ce6663a9 200 const struct object_id *oid;
68faf689 201
ce6663a9 202 oid = tree_entry_extract(&desc, &name, &mode);
68faf689
JH
203 if (strlen(name) == toplen &&
204 !memcmp(name, prefix, toplen)) {
205 if (!S_ISDIR(mode))
3b34934d
PO
206 die("entry %s in tree %s is not a tree", name,
207 oid_to_hex(oid1));
208 rewrite_here = (struct object_id *)oid;
68faf689
JH
209 break;
210 }
211 update_tree_entry(&desc);
212 }
213 if (!rewrite_here)
3b34934d
PO
214 die("entry %.*s not found in tree %s", toplen, prefix,
215 oid_to_hex(oid1));
2c5495f7 216 if (*subpath) {
3b34934d 217 status = splice_tree(rewrite_here, subpath, oid2, &subtree);
68faf689
JH
218 if (status)
219 return status;
3b34934d
PO
220 rewrite_with = &subtree;
221 } else {
222 rewrite_with = oid2;
68faf689 223 }
3b34934d 224 oidcpy(rewrite_here, rewrite_with);
a09c985e 225 status = write_object_file(buf, sz, tree_type, result);
68faf689
JH
226 free(buf);
227 return status;
228}
229
230/*
231 * We are trying to come up with a merge between one and two that
232 * results in a tree shape similar to one. The tree two might
233 * correspond to a subtree of one, in which case it needs to be
234 * shifted down by prefixing otherwise empty directories. On the
235 * other hand, it could cover tree one and we might need to pick a
236 * subtree of it.
237 */
82db3d44 238void shift_tree(const struct object_id *hash1,
239 const struct object_id *hash2,
240 struct object_id *shifted,
68faf689
JH
241 int depth_limit)
242{
243 char *add_prefix;
244 char *del_prefix;
245 int add_score, del_score;
246
85e51b78
JH
247 /*
248 * NEEDSWORK: this limits the recursion depth to hardcoded
249 * value '2' to avoid excessive overhead.
250 */
251 if (!depth_limit)
252 depth_limit = 2;
253
b6aec868 254 add_score = del_score = score_trees(hash1, hash2);
68faf689
JH
255 add_prefix = xcalloc(1, 1);
256 del_prefix = xcalloc(1, 1);
257
258 /*
259 * See if one's subtree resembles two; if so we need to prefix
260 * two with a few fake trees to match the prefix.
261 */
b6aec868 262 match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
68faf689
JH
263
264 /*
265 * See if two's subtree resembles one; if so we need to
266 * pick only subtree of two.
267 */
b6aec868 268 match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
68faf689
JH
269
270 /* Assume we do not have to do any shifting */
82db3d44 271 oidcpy(shifted, hash2);
68faf689
JH
272
273 if (add_score < del_score) {
274 /* We need to pick a subtree of two */
275 unsigned mode;
276
277 if (!*del_prefix)
278 return;
279
916bc35b 280 if (get_tree_entry(hash2, del_prefix, shifted, &mode))
68faf689 281 die("cannot find path %s in tree %s",
82db3d44 282 del_prefix, oid_to_hex(hash2));
68faf689
JH
283 return;
284 }
285
286 if (!*add_prefix)
287 return;
288
3b34934d 289 splice_tree(hash1, add_prefix, hash2, shifted);
68faf689 290}
85e51b78
JH
291
292/*
293 * The user says the trees will be shifted by this much.
294 * Unfortunately we cannot fundamentally tell which one to
295 * be prefixed, as recursive merge can work in either direction.
296 */
82db3d44 297void shift_tree_by(const struct object_id *hash1,
298 const struct object_id *hash2,
299 struct object_id *shifted,
85e51b78
JH
300 const char *shift_prefix)
301{
82db3d44 302 struct object_id sub1, sub2;
85e51b78
JH
303 unsigned mode1, mode2;
304 unsigned candidate = 0;
305
306 /* Can hash2 be a tree at shift_prefix in tree hash1? */
916bc35b 307 if (!get_tree_entry(hash1, shift_prefix, &sub1, &mode1) &&
85e51b78
JH
308 S_ISDIR(mode1))
309 candidate |= 1;
310
311 /* Can hash1 be a tree at shift_prefix in tree hash2? */
916bc35b 312 if (!get_tree_entry(hash2, shift_prefix, &sub2, &mode2) &&
85e51b78
JH
313 S_ISDIR(mode2))
314 candidate |= 2;
315
316 if (candidate == 3) {
317 /* Both are plausible -- we need to evaluate the score */
b6aec868 318 int best_score = score_trees(hash1, hash2);
85e51b78
JH
319 int score;
320
321 candidate = 0;
b6aec868 322 score = score_trees(&sub1, hash2);
85e51b78
JH
323 if (score > best_score) {
324 candidate = 1;
325 best_score = score;
326 }
b6aec868 327 score = score_trees(&sub2, hash1);
85e51b78
JH
328 if (score > best_score)
329 candidate = 2;
330 }
331
332 if (!candidate) {
333 /* Neither is plausible -- do not shift */
82db3d44 334 oidcpy(shifted, hash2);
85e51b78
JH
335 return;
336 }
337
338 if (candidate == 1)
339 /*
340 * shift tree2 down by adding shift_prefix above it
341 * to match tree1.
342 */
3b34934d 343 splice_tree(hash1, shift_prefix, hash2, shifted);
85e51b78
JH
344 else
345 /*
346 * shift tree2 up by removing shift_prefix from it
347 * to match tree1.
348 */
82db3d44 349 oidcpy(shifted, &sub2);
85e51b78 350}