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