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