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