list-objects-filter: use BUG rather than die
[git] / match-trees.c
1 #include "cache.h"
2 #include "tree.h"
3 #include "tree-walk.h"
4 #include "object-store.h"
5
6 static 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
19 static 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
32 static 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
51 static void *fill_tree_desc_strict(struct tree_desc *desc,
52                                    const struct object_id *hash)
53 {
54         void *buffer;
55         enum object_type type;
56         unsigned long size;
57
58         buffer = read_object_file(hash, &type, &size);
59         if (!buffer)
60                 die("unable to read tree (%s)", oid_to_hex(hash));
61         if (type != OBJ_TREE)
62                 die("%s is not a tree", oid_to_hex(hash));
63         init_tree_desc(desc, buffer, size);
64         return buffer;
65 }
66
67 static 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
74 /*
75  * Inspect two trees, and give a score that tells how similar they are.
76  */
77 static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
78 {
79         struct tree_desc one;
80         struct tree_desc two;
81         void *one_buf = fill_tree_desc_strict(&one, hash1);
82         void *two_buf = fill_tree_desc_strict(&two, hash2);
83         int score = 0;
84
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);
89                 int cmp;
90
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)
94                         /* two lacks this entry */
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)
103                         /* path1 does not appear in two */
104                         score += score_missing(e1.mode, e1.path);
105                 else if (cmp > 0)
106                         /* path2 does not appear in one */
107                         score += score_missing(e2.mode, e2.path);
108                 else if (oidcmp(e1.oid, e2.oid))
109                         /* they are different */
110                         score += score_differs(e1.mode, e2.mode, e1.path);
111                 else
112                         /* same subtree or blob */
113                         score += score_matches(e1.mode, e2.mode, e1.path);
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  */
123 static void match_trees(const struct object_id *hash1,
124                         const struct object_id *hash2,
125                         int *best_score,
126                         char **best_match,
127                         const char *base,
128                         int recurse_limit)
129 {
130         struct tree_desc one;
131         void *one_buf = fill_tree_desc_strict(&one, hash1);
132
133         while (one.size) {
134                 const char *path;
135                 const struct object_id *elem;
136                 unsigned mode;
137                 int score;
138
139                 elem = tree_entry_extract(&one, &path, &mode);
140                 if (!S_ISDIR(mode))
141                         goto next;
142                 score = score_trees(elem, hash2);
143                 if (*best_score < score) {
144                         free(*best_match);
145                         *best_match = xstrfmt("%s%s", base, path);
146                         *best_score = score;
147                 }
148                 if (recurse_limit) {
149                         char *newbase = xstrfmt("%s%s/", base, path);
150                         match_trees(elem, hash2, best_score, best_match,
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 /*
162  * A tree "oid1" has a subdirectory at "prefix".  Come up with a tree object by
163  * replacing it with another tree "oid2".
164  */
165 static int splice_tree(const struct object_id *oid1, const char *prefix,
166                        const struct object_id *oid2, struct object_id *result)
167 {
168         char *subpath;
169         int toplen;
170         char *buf;
171         unsigned long sz;
172         struct tree_desc desc;
173         struct object_id *rewrite_here;
174         const struct object_id *rewrite_with;
175         struct object_id subtree;
176         enum object_type type;
177         int status;
178
179         subpath = strchrnul(prefix, '/');
180         toplen = subpath - prefix;
181         if (*subpath)
182                 subpath++;
183
184         buf = read_object_file(oid1, &type, &sz);
185         if (!buf)
186                 die("cannot read tree %s", oid_to_hex(oid1));
187         init_tree_desc(&desc, buf, sz);
188
189         rewrite_here = NULL;
190         while (desc.size) {
191                 const char *name;
192                 unsigned mode;
193                 const struct object_id *oid;
194
195                 oid = tree_entry_extract(&desc, &name, &mode);
196                 if (strlen(name) == toplen &&
197                     !memcmp(name, prefix, toplen)) {
198                         if (!S_ISDIR(mode))
199                                 die("entry %s in tree %s is not a tree", name,
200                                     oid_to_hex(oid1));
201                         rewrite_here = (struct object_id *)oid;
202                         break;
203                 }
204                 update_tree_entry(&desc);
205         }
206         if (!rewrite_here)
207                 die("entry %.*s not found in tree %s", toplen, prefix,
208                     oid_to_hex(oid1));
209         if (*subpath) {
210                 status = splice_tree(rewrite_here, subpath, oid2, &subtree);
211                 if (status)
212                         return status;
213                 rewrite_with = &subtree;
214         } else {
215                 rewrite_with = oid2;
216         }
217         oidcpy(rewrite_here, rewrite_with);
218         status = write_object_file(buf, sz, tree_type, result);
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  */
231 void shift_tree(const struct object_id *hash1,
232                 const struct object_id *hash2,
233                 struct object_id *shifted,
234                 int depth_limit)
235 {
236         char *add_prefix;
237         char *del_prefix;
238         int add_score, del_score;
239
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
247         add_score = del_score = score_trees(hash1, hash2);
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          */
255         match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
256
257         /*
258          * See if two's subtree resembles one; if so we need to
259          * pick only subtree of two.
260          */
261         match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
262
263         /* Assume we do not have to do any shifting */
264         oidcpy(shifted, hash2);
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
273                 if (get_tree_entry(hash2, del_prefix, shifted, &mode))
274                         die("cannot find path %s in tree %s",
275                             del_prefix, oid_to_hex(hash2));
276                 return;
277         }
278
279         if (!*add_prefix)
280                 return;
281
282         splice_tree(hash1, add_prefix, hash2, shifted);
283 }
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  */
290 void shift_tree_by(const struct object_id *hash1,
291                    const struct object_id *hash2,
292                    struct object_id *shifted,
293                    const char *shift_prefix)
294 {
295         struct object_id sub1, sub2;
296         unsigned mode1, mode2;
297         unsigned candidate = 0;
298
299         /* Can hash2 be a tree at shift_prefix in tree hash1? */
300         if (!get_tree_entry(hash1, shift_prefix, &sub1, &mode1) &&
301             S_ISDIR(mode1))
302                 candidate |= 1;
303
304         /* Can hash1 be a tree at shift_prefix in tree hash2? */
305         if (!get_tree_entry(hash2, shift_prefix, &sub2, &mode2) &&
306             S_ISDIR(mode2))
307                 candidate |= 2;
308
309         if (candidate == 3) {
310                 /* Both are plausible -- we need to evaluate the score */
311                 int best_score = score_trees(hash1, hash2);
312                 int score;
313
314                 candidate = 0;
315                 score = score_trees(&sub1, hash2);
316                 if (score > best_score) {
317                         candidate = 1;
318                         best_score = score;
319                 }
320                 score = score_trees(&sub2, hash1);
321                 if (score > best_score)
322                         candidate = 2;
323         }
324
325         if (!candidate) {
326                 /* Neither is plausible -- do not shift */
327                 oidcpy(shifted, hash2);
328                 return;
329         }
330
331         if (candidate == 1)
332                 /*
333                  * shift tree2 down by adding shift_prefix above it
334                  * to match tree1.
335                  */
336                 splice_tree(hash1, shift_prefix, hash2, shifted);
337         else
338                 /*
339                  * shift tree2 up by removing shift_prefix from it
340                  * to match tree1.
341                  */
342                 oidcpy(shifted, &sub2);
343 }