Merge branch 'mm/doc-tt'
[git] / match-trees.c
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
50 static void *fill_tree_desc_strict(struct tree_desc *desc,
51                                    const struct object_id *hash)
52 {
53         void *buffer;
54         enum object_type type;
55         unsigned long size;
56
57         buffer = read_sha1_file(hash->hash, &type, &size);
58         if (!buffer)
59                 die("unable to read tree (%s)", oid_to_hex(hash));
60         if (type != OBJ_TREE)
61                 die("%s is not a tree", oid_to_hex(hash));
62         init_tree_desc(desc, buffer, size);
63         return buffer;
64 }
65
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
73 /*
74  * Inspect two trees, and give a score that tells how similar they are.
75  */
76 static int score_trees(const struct object_id *hash1, const struct object_id *hash2)
77 {
78         struct tree_desc one;
79         struct tree_desc two;
80         void *one_buf = fill_tree_desc_strict(&one, hash1);
81         void *two_buf = fill_tree_desc_strict(&two, hash2);
82         int score = 0;
83
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);
88                 int cmp;
89
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)
93                         /* two lacks this entry */
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)
102                         /* path1 does not appear in two */
103                         score += score_missing(e1.mode, e1.path);
104                 else if (cmp > 0)
105                         /* path2 does not appear in one */
106                         score += score_missing(e2.mode, e2.path);
107                 else if (oidcmp(e1.oid, e2.oid))
108                         /* they are different */
109                         score += score_differs(e1.mode, e2.mode, e1.path);
110                 else
111                         /* same subtree or blob */
112                         score += score_matches(e1.mode, e2.mode, e1.path);
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  */
122 static void match_trees(const struct object_id *hash1,
123                         const struct object_id *hash2,
124                         int *best_score,
125                         char **best_match,
126                         const char *base,
127                         int recurse_limit)
128 {
129         struct tree_desc one;
130         void *one_buf = fill_tree_desc_strict(&one, hash1);
131
132         while (one.size) {
133                 const char *path;
134                 const struct object_id *elem;
135                 unsigned mode;
136                 int score;
137
138                 elem = tree_entry_extract(&one, &path, &mode);
139                 if (!S_ISDIR(mode))
140                         goto next;
141                 score = score_trees(elem, hash2);
142                 if (*best_score < score) {
143                         free(*best_match);
144                         *best_match = xstrfmt("%s%s", base, path);
145                         *best_score = score;
146                 }
147                 if (recurse_limit) {
148                         char *newbase = xstrfmt("%s%s/", base, path);
149                         match_trees(elem, hash2, best_score, best_match,
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 /*
161  * A tree "hash1" has a subdirectory at "prefix".  Come up with a
162  * tree object by replacing it with another tree "hash2".
163  */
164 static int splice_tree(const unsigned char *hash1,
165                        const char *prefix,
166                        const unsigned char *hash2,
167                        unsigned char *result)
168 {
169         char *subpath;
170         int toplen;
171         char *buf;
172         unsigned long sz;
173         struct tree_desc desc;
174         unsigned char *rewrite_here;
175         const unsigned char *rewrite_with;
176         unsigned char subtree[20];
177         enum object_type type;
178         int status;
179
180         subpath = strchrnul(prefix, '/');
181         toplen = subpath - prefix;
182         if (*subpath)
183                 subpath++;
184
185         buf = read_sha1_file(hash1, &type, &sz);
186         if (!buf)
187                 die("cannot read tree %s", sha1_to_hex(hash1));
188         init_tree_desc(&desc, buf, sz);
189
190         rewrite_here = NULL;
191         while (desc.size) {
192                 const char *name;
193                 unsigned mode;
194                 const struct object_id *oid;
195
196                 oid = tree_entry_extract(&desc, &name, &mode);
197                 if (strlen(name) == toplen &&
198                     !memcmp(name, prefix, toplen)) {
199                         if (!S_ISDIR(mode))
200                                 die("entry %s in tree %s is not a tree",
201                                     name, sha1_to_hex(hash1));
202                         rewrite_here = (unsigned char *) oid->hash;
203                         break;
204                 }
205                 update_tree_entry(&desc);
206         }
207         if (!rewrite_here)
208                 die("entry %.*s not found in tree %s",
209                     toplen, prefix, sha1_to_hex(hash1));
210         if (*subpath) {
211                 status = splice_tree(rewrite_here, subpath, hash2, subtree);
212                 if (status)
213                         return status;
214                 rewrite_with = subtree;
215         }
216         else
217                 rewrite_with = hash2;
218         hashcpy(rewrite_here, rewrite_with);
219         status = write_sha1_file(buf, sz, tree_type, result);
220         free(buf);
221         return status;
222 }
223
224 /*
225  * We are trying to come up with a merge between one and two that
226  * results in a tree shape similar to one.  The tree two might
227  * correspond to a subtree of one, in which case it needs to be
228  * shifted down by prefixing otherwise empty directories.  On the
229  * other hand, it could cover tree one and we might need to pick a
230  * subtree of it.
231  */
232 void shift_tree(const struct object_id *hash1,
233                 const struct object_id *hash2,
234                 struct object_id *shifted,
235                 int depth_limit)
236 {
237         char *add_prefix;
238         char *del_prefix;
239         int add_score, del_score;
240
241         /*
242          * NEEDSWORK: this limits the recursion depth to hardcoded
243          * value '2' to avoid excessive overhead.
244          */
245         if (!depth_limit)
246                 depth_limit = 2;
247
248         add_score = del_score = score_trees(hash1, hash2);
249         add_prefix = xcalloc(1, 1);
250         del_prefix = xcalloc(1, 1);
251
252         /*
253          * See if one's subtree resembles two; if so we need to prefix
254          * two with a few fake trees to match the prefix.
255          */
256         match_trees(hash1, hash2, &add_score, &add_prefix, "", depth_limit);
257
258         /*
259          * See if two's subtree resembles one; if so we need to
260          * pick only subtree of two.
261          */
262         match_trees(hash2, hash1, &del_score, &del_prefix, "", depth_limit);
263
264         /* Assume we do not have to do any shifting */
265         oidcpy(shifted, hash2);
266
267         if (add_score < del_score) {
268                 /* We need to pick a subtree of two */
269                 unsigned mode;
270
271                 if (!*del_prefix)
272                         return;
273
274                 if (get_tree_entry(hash2->hash, del_prefix, shifted->hash, &mode))
275                         die("cannot find path %s in tree %s",
276                             del_prefix, oid_to_hex(hash2));
277                 return;
278         }
279
280         if (!*add_prefix)
281                 return;
282
283         splice_tree(hash1->hash, add_prefix, hash2->hash, shifted->hash);
284 }
285
286 /*
287  * The user says the trees will be shifted by this much.
288  * Unfortunately we cannot fundamentally tell which one to
289  * be prefixed, as recursive merge can work in either direction.
290  */
291 void shift_tree_by(const struct object_id *hash1,
292                    const struct object_id *hash2,
293                    struct object_id *shifted,
294                    const char *shift_prefix)
295 {
296         struct object_id sub1, sub2;
297         unsigned mode1, mode2;
298         unsigned candidate = 0;
299
300         /* Can hash2 be a tree at shift_prefix in tree hash1? */
301         if (!get_tree_entry(hash1->hash, shift_prefix, sub1.hash, &mode1) &&
302             S_ISDIR(mode1))
303                 candidate |= 1;
304
305         /* Can hash1 be a tree at shift_prefix in tree hash2? */
306         if (!get_tree_entry(hash2->hash, shift_prefix, sub2.hash, &mode2) &&
307             S_ISDIR(mode2))
308                 candidate |= 2;
309
310         if (candidate == 3) {
311                 /* Both are plausible -- we need to evaluate the score */
312                 int best_score = score_trees(hash1, hash2);
313                 int score;
314
315                 candidate = 0;
316                 score = score_trees(&sub1, hash2);
317                 if (score > best_score) {
318                         candidate = 1;
319                         best_score = score;
320                 }
321                 score = score_trees(&sub2, hash1);
322                 if (score > best_score)
323                         candidate = 2;
324         }
325
326         if (!candidate) {
327                 /* Neither is plausible -- do not shift */
328                 oidcpy(shifted, hash2);
329                 return;
330         }
331
332         if (candidate == 1)
333                 /*
334                  * shift tree2 down by adding shift_prefix above it
335                  * to match tree1.
336                  */
337                 splice_tree(hash1->hash, shift_prefix, hash2->hash, shifted->hash);
338         else
339                 /*
340                  * shift tree2 up by removing shift_prefix from it
341                  * to match tree1.
342                  */
343                 oidcpy(shifted, &sub2);
344 }