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