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