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