Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "tree-walk.h" |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 3 | #include "unpack-trees.h" |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 4 | #include "dir.h" |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 5 | #include "tree.h" |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 6 | |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 7 | static const char *get_mode(const char *str, unsigned int *modep) |
| 8 | { |
| 9 | unsigned char c; |
| 10 | unsigned int mode = 0; |
| 11 | |
Martin Koegler | 64cc1c0 | 2008-01-06 18:21:10 +0100 | [diff] [blame] | 12 | if (*str == ' ') |
| 13 | return NULL; |
| 14 | |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 15 | while ((c = *str++) != ' ') { |
| 16 | if (c < '0' || c > '7') |
| 17 | return NULL; |
| 18 | mode = (mode << 3) + (c - '0'); |
| 19 | } |
| 20 | *modep = mode; |
| 21 | return str; |
| 22 | } |
| 23 | |
Martin Koegler | 64cc1c0 | 2008-01-06 18:21:10 +0100 | [diff] [blame] | 24 | static void decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size) |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 25 | { |
| 26 | const char *path; |
| 27 | unsigned int mode, len; |
| 28 | |
Martin Koegler | 64cc1c0 | 2008-01-06 18:21:10 +0100 | [diff] [blame] | 29 | if (size < 24 || buf[size - 21]) |
| 30 | die("corrupt tree file"); |
| 31 | |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 32 | path = get_mode(buf, &mode); |
Martin Koegler | 64cc1c0 | 2008-01-06 18:21:10 +0100 | [diff] [blame] | 33 | if (!path || !*path) |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 34 | die("corrupt tree file"); |
| 35 | len = strlen(path) + 1; |
| 36 | |
| 37 | /* Initialize the descriptor entry */ |
| 38 | desc->entry.path = path; |
| 39 | desc->entry.mode = mode; |
| 40 | desc->entry.sha1 = (const unsigned char *)(path + len); |
| 41 | } |
| 42 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 43 | void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size) |
| 44 | { |
| 45 | desc->buffer = buffer; |
| 46 | desc->size = size; |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 47 | if (size) |
| 48 | decode_tree_entry(desc, buffer, size); |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 51 | void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1) |
| 52 | { |
| 53 | unsigned long size = 0; |
| 54 | void *buf = NULL; |
| 55 | |
| 56 | if (sha1) { |
Peter Eriksen | 8e44025 | 2006-04-02 14:44:09 +0200 | [diff] [blame] | 57 | buf = read_object_with_reference(sha1, tree_type, &size, NULL); |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 58 | if (!buf) |
| 59 | die("unable to read tree %s", sha1_to_hex(sha1)); |
| 60 | } |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 61 | init_tree_desc(desc, buf, size); |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 62 | return buf; |
| 63 | } |
| 64 | |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 65 | static void entry_clear(struct name_entry *a) |
| 66 | { |
| 67 | memset(a, 0, sizeof(*a)); |
| 68 | } |
| 69 | |
| 70 | static void entry_extract(struct tree_desc *t, struct name_entry *a) |
| 71 | { |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 72 | *a = t->entry; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void update_tree_entry(struct tree_desc *desc) |
| 76 | { |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 77 | const void *buf = desc->buffer; |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 78 | const unsigned char *end = desc->entry.sha1 + 20; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 79 | unsigned long size = desc->size; |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 80 | unsigned long len = end - (const unsigned char *)buf; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 81 | |
| 82 | if (size < len) |
| 83 | die("corrupt tree file"); |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 84 | buf = end; |
| 85 | size -= len; |
| 86 | desc->buffer = buf; |
| 87 | desc->size = size; |
| 88 | if (size) |
| 89 | decode_tree_entry(desc, buf, size); |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 92 | int tree_entry(struct tree_desc *desc, struct name_entry *entry) |
| 93 | { |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 94 | if (!desc->size) |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | |
Linus Torvalds | 4651ece | 2007-03-21 10:09:56 -0700 | [diff] [blame] | 97 | *entry = desc->entry; |
| 98 | update_tree_entry(desc); |
Linus Torvalds | 4c068a9 | 2006-05-30 09:45:45 -0700 | [diff] [blame] | 99 | return 1; |
| 100 | } |
| 101 | |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 102 | void setup_traverse_info(struct traverse_info *info, const char *base) |
| 103 | { |
| 104 | int pathlen = strlen(base); |
Linus Torvalds | bcbe5a5 | 2008-03-06 15:44:48 -0800 | [diff] [blame] | 105 | static struct traverse_info dummy; |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 106 | |
| 107 | memset(info, 0, sizeof(*info)); |
| 108 | if (pathlen && base[pathlen-1] == '/') |
| 109 | pathlen--; |
| 110 | info->pathlen = pathlen ? pathlen + 1 : 0; |
| 111 | info->name.path = base; |
| 112 | info->name.sha1 = (void *)(base + pathlen + 1); |
Linus Torvalds | bcbe5a5 | 2008-03-06 15:44:48 -0800 | [diff] [blame] | 113 | if (pathlen) |
| 114 | info->prev = &dummy; |
Linus Torvalds | 40d934d | 2008-03-05 18:59:29 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | char *make_traverse_path(char *path, const struct traverse_info *info, const struct name_entry *n) |
| 118 | { |
| 119 | int len = tree_entry_len(n->path, n->sha1); |
| 120 | int pathlen = info->pathlen; |
| 121 | |
| 122 | path[pathlen + len] = 0; |
| 123 | for (;;) { |
| 124 | memcpy(path + pathlen, n->path, len); |
| 125 | if (!pathlen) |
| 126 | break; |
| 127 | path[--pathlen] = '/'; |
| 128 | n = &info->name; |
| 129 | len = tree_entry_len(n->path, n->sha1); |
| 130 | info = info->prev; |
| 131 | pathlen -= len; |
| 132 | } |
| 133 | return path; |
| 134 | } |
| 135 | |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 136 | struct tree_desc_skip { |
| 137 | struct tree_desc_skip *prev; |
| 138 | const void *ptr; |
| 139 | }; |
| 140 | |
| 141 | struct tree_desc_x { |
| 142 | struct tree_desc d; |
| 143 | struct tree_desc_skip *skip; |
| 144 | }; |
| 145 | |
| 146 | static int name_compare(const char *a, int a_len, |
| 147 | const char *b, int b_len) |
| 148 | { |
| 149 | int len = (a_len < b_len) ? a_len : b_len; |
| 150 | int cmp = memcmp(a, b, len); |
| 151 | if (cmp) |
| 152 | return cmp; |
| 153 | return (a_len - b_len); |
| 154 | } |
| 155 | |
| 156 | static int check_entry_match(const char *a, int a_len, const char *b, int b_len) |
| 157 | { |
| 158 | /* |
| 159 | * The caller wants to pick *a* from a tree or nothing. |
| 160 | * We are looking at *b* in a tree. |
| 161 | * |
| 162 | * (0) If a and b are the same name, we are trivially happy. |
| 163 | * |
| 164 | * There are three possibilities where *a* could be hiding |
| 165 | * behind *b*. |
| 166 | * |
| 167 | * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no |
| 168 | * matter what. |
| 169 | * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree; |
| 170 | * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree. |
| 171 | * |
| 172 | * Otherwise we know *a* won't appear in the tree without |
| 173 | * scanning further. |
| 174 | */ |
| 175 | |
| 176 | int cmp = name_compare(a, a_len, b, b_len); |
| 177 | |
| 178 | /* Most common case first -- reading sync'd trees */ |
| 179 | if (!cmp) |
| 180 | return cmp; |
| 181 | |
| 182 | if (0 < cmp) { |
| 183 | /* a comes after b; it does not matter if it is case (3) |
| 184 | if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/') |
| 185 | return 1; |
| 186 | */ |
| 187 | return 1; /* keep looking */ |
| 188 | } |
| 189 | |
| 190 | /* b comes after a; are we looking at case (2)? */ |
| 191 | if (a_len < b_len && !memcmp(a, b, a_len) && b[a_len] < '/') |
| 192 | return 1; /* keep looking */ |
| 193 | |
| 194 | return -1; /* a cannot appear in the tree */ |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * From the extended tree_desc, extract the first name entry, while |
| 199 | * paying attention to the candidate "first" name. Most importantly, |
| 200 | * when looking for an entry, if there are entries that sorts earlier |
| 201 | * in the tree object representation than that name, skip them and |
| 202 | * process the named entry first. We will remember that we haven't |
| 203 | * processed the first entry yet, and in the later call skip the |
| 204 | * entry we processed early when update_extended_entry() is called. |
| 205 | * |
| 206 | * E.g. if the underlying tree object has these entries: |
| 207 | * |
| 208 | * blob "t-1" |
| 209 | * blob "t-2" |
| 210 | * tree "t" |
| 211 | * blob "t=1" |
| 212 | * |
| 213 | * and the "first" asks for "t", remember that we still need to |
| 214 | * process "t-1" and "t-2" but extract "t". After processing the |
| 215 | * entry "t" from this call, the caller will let us know by calling |
| 216 | * update_extended_entry() that we can remember "t" has been processed |
| 217 | * already. |
| 218 | */ |
| 219 | |
| 220 | static void extended_entry_extract(struct tree_desc_x *t, |
| 221 | struct name_entry *a, |
| 222 | const char *first, |
| 223 | int first_len) |
| 224 | { |
| 225 | const char *path; |
| 226 | int len; |
| 227 | struct tree_desc probe; |
| 228 | struct tree_desc_skip *skip; |
| 229 | |
| 230 | /* |
| 231 | * Extract the first entry from the tree_desc, but skip the |
| 232 | * ones that we already returned in earlier rounds. |
| 233 | */ |
| 234 | while (1) { |
| 235 | if (!t->d.size) { |
| 236 | entry_clear(a); |
| 237 | break; /* not found */ |
| 238 | } |
| 239 | entry_extract(&t->d, a); |
| 240 | for (skip = t->skip; skip; skip = skip->prev) |
| 241 | if (a->path == skip->ptr) |
| 242 | break; /* found */ |
| 243 | if (!skip) |
| 244 | break; |
| 245 | /* We have processed this entry already. */ |
| 246 | update_tree_entry(&t->d); |
| 247 | } |
| 248 | |
| 249 | if (!first || !a->path) |
| 250 | return; |
| 251 | |
| 252 | /* |
| 253 | * The caller wants "first" from this tree, or nothing. |
| 254 | */ |
| 255 | path = a->path; |
| 256 | len = tree_entry_len(a->path, a->sha1); |
| 257 | switch (check_entry_match(first, first_len, path, len)) { |
| 258 | case -1: |
| 259 | entry_clear(a); |
| 260 | case 0: |
| 261 | return; |
| 262 | default: |
| 263 | break; |
| 264 | } |
| 265 | |
| 266 | /* |
| 267 | * We need to look-ahead -- we suspect that a subtree whose |
| 268 | * name is "first" may be hiding behind the current entry "path". |
| 269 | */ |
| 270 | probe = t->d; |
| 271 | while (probe.size) { |
| 272 | entry_extract(&probe, a); |
| 273 | path = a->path; |
| 274 | len = tree_entry_len(a->path, a->sha1); |
| 275 | switch (check_entry_match(first, first_len, path, len)) { |
| 276 | case -1: |
| 277 | entry_clear(a); |
| 278 | case 0: |
| 279 | return; |
| 280 | default: |
| 281 | update_tree_entry(&probe); |
| 282 | break; |
| 283 | } |
| 284 | /* keep looking */ |
| 285 | } |
| 286 | entry_clear(a); |
| 287 | } |
| 288 | |
| 289 | static void update_extended_entry(struct tree_desc_x *t, struct name_entry *a) |
| 290 | { |
| 291 | if (t->d.entry.path == a->path) { |
| 292 | update_tree_entry(&t->d); |
| 293 | } else { |
| 294 | /* we have returned this entry early */ |
| 295 | struct tree_desc_skip *skip = xmalloc(sizeof(*skip)); |
| 296 | skip->ptr = a->path; |
| 297 | skip->prev = t->skip; |
| 298 | t->skip = skip; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | static void free_extended_entry(struct tree_desc_x *t) |
| 303 | { |
| 304 | struct tree_desc_skip *p, *s; |
| 305 | |
| 306 | for (s = t->skip; s; s = p) { |
| 307 | p = s->prev; |
| 308 | free(s); |
| 309 | } |
| 310 | } |
| 311 | |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 312 | int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info) |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 313 | { |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 314 | int ret = 0; |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 315 | int error = 0; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 316 | struct name_entry *entry = xmalloc(n*sizeof(*entry)); |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 317 | int i; |
| 318 | struct tree_desc_x *tx = xcalloc(n, sizeof(*tx)); |
| 319 | |
| 320 | for (i = 0; i < n; i++) |
| 321 | tx[i].d = t[i]; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 322 | |
| 323 | for (;;) { |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 324 | unsigned long mask, dirmask; |
| 325 | const char *first = NULL; |
| 326 | int first_len = 0; |
| 327 | struct name_entry *e; |
| 328 | int len; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 329 | |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 330 | for (i = 0; i < n; i++) { |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 331 | e = entry + i; |
| 332 | extended_entry_extract(tx + i, e, NULL, 0); |
| 333 | } |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 334 | |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 335 | /* |
| 336 | * A tree may have "t-2" at the current location even |
| 337 | * though it may have "t" that is a subtree behind it, |
| 338 | * and another tree may return "t". We want to grab |
| 339 | * all "t" from all trees to match in such a case. |
| 340 | */ |
| 341 | for (i = 0; i < n; i++) { |
| 342 | e = entry + i; |
| 343 | if (!e->path) |
| 344 | continue; |
| 345 | len = tree_entry_len(e->path, e->sha1); |
| 346 | if (!first) { |
| 347 | first = e->path; |
| 348 | first_len = len; |
| 349 | continue; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 350 | } |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 351 | if (name_compare(e->path, len, first, first_len) < 0) { |
| 352 | first = e->path; |
| 353 | first_len = len; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | if (first) { |
| 358 | for (i = 0; i < n; i++) { |
| 359 | e = entry + i; |
| 360 | extended_entry_extract(tx + i, e, first, first_len); |
| 361 | /* Cull the ones that are not the earliest */ |
| 362 | if (!e->path) |
| 363 | continue; |
| 364 | len = tree_entry_len(e->path, e->sha1); |
| 365 | if (name_compare(e->path, len, first, first_len)) |
| 366 | entry_clear(e); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /* Now we have in entry[i] the earliest name from the trees */ |
| 371 | mask = 0; |
| 372 | dirmask = 0; |
| 373 | for (i = 0; i < n; i++) { |
| 374 | if (!entry[i].path) |
| 375 | continue; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 376 | mask |= 1ul << i; |
Linus Torvalds | 91e4f03 | 2008-03-05 20:06:18 -0800 | [diff] [blame] | 377 | if (S_ISDIR(entry[i].mode)) |
| 378 | dirmask |= 1ul << i; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 379 | } |
| 380 | if (!mask) |
| 381 | break; |
Linus Torvalds | 91e4f03 | 2008-03-05 20:06:18 -0800 | [diff] [blame] | 382 | ret = info->fn(n, mask, dirmask, entry, info); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 383 | if (ret < 0) { |
| 384 | error = ret; |
| 385 | if (!info->show_all_errors) |
| 386 | break; |
| 387 | } |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 388 | mask &= ret; |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 389 | ret = 0; |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 390 | for (i = 0; i < n; i++) |
Linus Torvalds | 5803c6f | 2008-03-05 19:44:06 -0800 | [diff] [blame] | 391 | if (mask & (1ul << i)) |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 392 | update_extended_entry(tx + i, entry + i); |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 393 | } |
| 394 | free(entry); |
Junio C Hamano | 1ee2657 | 2009-09-19 14:07:14 -0700 | [diff] [blame] | 395 | for (i = 0; i < n; i++) |
| 396 | free_extended_entry(tx + i); |
| 397 | free(tx); |
Matthieu Moy | e6c111b | 2010-08-11 10:38:07 +0200 | [diff] [blame] | 398 | return error; |
Junio C Hamano | 1b0c717 | 2006-03-29 22:55:43 -0800 | [diff] [blame] | 399 | } |
| 400 | |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 401 | static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode) |
| 402 | { |
| 403 | int namelen = strlen(name); |
| 404 | while (t->size) { |
| 405 | const char *entry; |
| 406 | const unsigned char *sha1; |
| 407 | int entrylen, cmp; |
| 408 | |
| 409 | sha1 = tree_entry_extract(t, &entry, mode); |
| 410 | update_tree_entry(t); |
Linus Torvalds | 304de2d | 2007-03-17 20:06:24 -0700 | [diff] [blame] | 411 | entrylen = tree_entry_len(entry, sha1); |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 412 | if (entrylen > namelen) |
| 413 | continue; |
| 414 | cmp = memcmp(name, entry, entrylen); |
| 415 | if (cmp > 0) |
| 416 | continue; |
| 417 | if (cmp < 0) |
| 418 | break; |
| 419 | if (entrylen == namelen) { |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 420 | hashcpy(result, sha1); |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 421 | return 0; |
| 422 | } |
| 423 | if (name[entrylen] != '/') |
| 424 | continue; |
| 425 | if (!S_ISDIR(*mode)) |
| 426 | break; |
| 427 | if (++entrylen == namelen) { |
Shawn Pearce | e702496 | 2006-08-23 02:49:00 -0400 | [diff] [blame] | 428 | hashcpy(result, sha1); |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 429 | return 0; |
| 430 | } |
| 431 | return get_tree_entry(sha1, name + entrylen, result, mode); |
| 432 | } |
| 433 | return -1; |
| 434 | } |
| 435 | |
| 436 | int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode) |
| 437 | { |
| 438 | int retval; |
| 439 | void *tree; |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 440 | unsigned long size; |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 441 | struct tree_desc t; |
Jeff King | d93b7d1 | 2007-01-09 11:11:47 -0500 | [diff] [blame] | 442 | unsigned char root[20]; |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 443 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 444 | tree = read_object_with_reference(tree_sha1, tree_type, &size, root); |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 445 | if (!tree) |
| 446 | return -1; |
Jeff King | d93b7d1 | 2007-01-09 11:11:47 -0500 | [diff] [blame] | 447 | |
| 448 | if (name[0] == '\0') { |
| 449 | hashcpy(sha1, root); |
René Scharfe | ef00650 | 2010-02-14 10:56:46 +0100 | [diff] [blame] | 450 | free(tree); |
Jeff King | d93b7d1 | 2007-01-09 11:11:47 -0500 | [diff] [blame] | 451 | return 0; |
| 452 | } |
| 453 | |
Linus Torvalds | 6fda5e5 | 2007-03-21 10:08:25 -0700 | [diff] [blame] | 454 | init_tree_desc(&t, tree, size); |
Junio C Hamano | 4dcff63 | 2006-04-19 14:05:47 -0700 | [diff] [blame] | 455 | retval = find_tree_entry(&t, name, sha1, mode); |
| 456 | free(tree); |
| 457 | return retval; |
| 458 | } |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 459 | |
Nguyễn Thái Ngọc Duy | 58c4d66 | 2010-12-15 22:02:43 +0700 | [diff] [blame] | 460 | static int match_entry(const struct name_entry *entry, int pathlen, |
| 461 | const char *match, int matchlen, |
| 462 | int *never_interesting) |
| 463 | { |
| 464 | int m = -1; /* signals that we haven't called strncmp() */ |
| 465 | |
| 466 | if (*never_interesting) { |
| 467 | /* |
| 468 | * We have not seen any match that sorts later |
| 469 | * than the current path. |
| 470 | */ |
| 471 | |
| 472 | /* |
| 473 | * Does match sort strictly earlier than path |
| 474 | * with their common parts? |
| 475 | */ |
| 476 | m = strncmp(match, entry->path, |
| 477 | (matchlen < pathlen) ? matchlen : pathlen); |
| 478 | if (m < 0) |
| 479 | return 0; |
| 480 | |
| 481 | /* |
| 482 | * If we come here even once, that means there is at |
| 483 | * least one pathspec that would sort equal to or |
| 484 | * later than the path we are currently looking at. |
| 485 | * In other words, if we have never reached this point |
| 486 | * after iterating all pathspecs, it means all |
| 487 | * pathspecs are either outside of base, or inside the |
| 488 | * base but sorts strictly earlier than the current |
| 489 | * one. In either case, they will never match the |
| 490 | * subsequent entries. In such a case, we initialized |
| 491 | * the variable to -1 and that is what will be |
| 492 | * returned, allowing the caller to terminate early. |
| 493 | */ |
| 494 | *never_interesting = 0; |
| 495 | } |
| 496 | |
| 497 | if (pathlen > matchlen) |
| 498 | return 0; |
| 499 | |
| 500 | if (matchlen > pathlen) { |
| 501 | if (match[pathlen] != '/') |
| 502 | return 0; |
| 503 | if (!S_ISDIR(entry->mode)) |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | if (m == -1) |
| 508 | /* |
| 509 | * we cheated and did not do strncmp(), so we do |
| 510 | * that here. |
| 511 | */ |
| 512 | m = strncmp(match, entry->path, pathlen); |
| 513 | |
| 514 | /* |
| 515 | * If common part matched earlier then it is a hit, |
| 516 | * because we rejected the case where path is not a |
| 517 | * leading directory and is shorter than match. |
| 518 | */ |
| 519 | if (!m) |
| 520 | return 1; |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static int match_dir_prefix(const char *base, int baselen, |
| 526 | const char *match, int matchlen) |
| 527 | { |
| 528 | if (strncmp(base, match, matchlen)) |
| 529 | return 0; |
| 530 | |
| 531 | /* |
| 532 | * If the base is a subdirectory of a path which |
| 533 | * was specified, all of them are interesting. |
| 534 | */ |
| 535 | if (!matchlen || |
| 536 | base[matchlen] == '/' || |
| 537 | match[matchlen - 1] == '/') |
| 538 | return 1; |
| 539 | |
| 540 | /* Just a random prefix match */ |
| 541 | return 0; |
| 542 | } |
| 543 | |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 544 | /* |
| 545 | * Is a tree entry interesting given the pathspec we have? |
| 546 | * |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 547 | * Pre-condition: either baselen == base_offset (i.e. empty path) |
| 548 | * or base[baselen-1] == '/' (i.e. with trailing slash). |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 549 | * |
| 550 | * Return: |
| 551 | * - 2 for "yes, and all subsequent entries will be" |
| 552 | * - 1 for yes |
| 553 | * - zero for no |
| 554 | * - negative for "no, and no subsequent entries will be either" |
| 555 | */ |
| 556 | int tree_entry_interesting(const struct name_entry *entry, |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 557 | struct strbuf *base, int base_offset, |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 558 | const struct pathspec *ps) |
| 559 | { |
| 560 | int i; |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 561 | int pathlen, baselen = base->len - base_offset; |
Nguyễn Thái Ngọc Duy | d38f280 | 2010-12-15 22:02:46 +0700 | [diff] [blame] | 562 | int never_interesting = ps->has_wildcard ? 0 : -1; |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 563 | |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 564 | if (!ps->nr) { |
| 565 | if (!ps->recursive || ps->max_depth == -1) |
| 566 | return 2; |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 567 | return !!within_depth(base->buf + base_offset, baselen, |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 568 | !!S_ISDIR(entry->mode), |
| 569 | ps->max_depth); |
| 570 | } |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 571 | |
| 572 | pathlen = tree_entry_len(entry->path, entry->sha1); |
| 573 | |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 574 | for (i = ps->nr - 1; i >= 0; i--) { |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 575 | const struct pathspec_item *item = ps->items+i; |
| 576 | const char *match = item->match; |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 577 | const char *base_str = base->buf + base_offset; |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 578 | int matchlen = item->len; |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 579 | |
| 580 | if (baselen >= matchlen) { |
| 581 | /* If it doesn't match, move along... */ |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 582 | if (!match_dir_prefix(base_str, baselen, match, matchlen)) |
Nguyễn Thái Ngọc Duy | d38f280 | 2010-12-15 22:02:46 +0700 | [diff] [blame] | 583 | goto match_wildcards; |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 584 | |
| 585 | if (!ps->recursive || ps->max_depth == -1) |
| 586 | return 2; |
| 587 | |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 588 | return !!within_depth(base_str + matchlen + 1, |
Nguyễn Thái Ngọc Duy | bc96cc8 | 2010-12-15 22:02:44 +0700 | [diff] [blame] | 589 | baselen - matchlen - 1, |
| 590 | !!S_ISDIR(entry->mode), |
| 591 | ps->max_depth); |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | /* Does the base match? */ |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 595 | if (!strncmp(base_str, match, baselen)) { |
Nguyễn Thái Ngọc Duy | 58c4d66 | 2010-12-15 22:02:43 +0700 | [diff] [blame] | 596 | if (match_entry(entry, pathlen, |
| 597 | match + baselen, matchlen - baselen, |
| 598 | &never_interesting)) |
| 599 | return 1; |
Nguyễn Thái Ngọc Duy | f1a2ddb | 2010-12-15 22:02:47 +0700 | [diff] [blame] | 600 | |
Junio C Hamano | 33e0f62 | 2011-04-05 09:30:36 -0700 | [diff] [blame] | 601 | if (ps->items[i].use_wildcard) { |
Nguyễn Thái Ngọc Duy | f1a2ddb | 2010-12-15 22:02:47 +0700 | [diff] [blame] | 602 | if (!fnmatch(match + baselen, entry->path, 0)) |
| 603 | return 1; |
| 604 | |
| 605 | /* |
| 606 | * Match all directories. We'll try to |
| 607 | * match files later on. |
| 608 | */ |
| 609 | if (ps->recursive && S_ISDIR(entry->mode)) |
| 610 | return 1; |
| 611 | } |
| 612 | |
| 613 | continue; |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 614 | } |
Nguyễn Thái Ngọc Duy | d38f280 | 2010-12-15 22:02:46 +0700 | [diff] [blame] | 615 | |
| 616 | match_wildcards: |
Junio C Hamano | 33e0f62 | 2011-04-05 09:30:36 -0700 | [diff] [blame] | 617 | if (!ps->items[i].use_wildcard) |
Nguyễn Thái Ngọc Duy | d38f280 | 2010-12-15 22:02:46 +0700 | [diff] [blame] | 618 | continue; |
| 619 | |
| 620 | /* |
| 621 | * Concatenate base and entry->path into one and do |
| 622 | * fnmatch() on it. |
| 623 | */ |
| 624 | |
| 625 | strbuf_add(base, entry->path, pathlen); |
| 626 | |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 627 | if (!fnmatch(match, base->buf + base_offset, 0)) { |
| 628 | strbuf_setlen(base, base_offset + baselen); |
Nguyễn Thái Ngọc Duy | d38f280 | 2010-12-15 22:02:46 +0700 | [diff] [blame] | 629 | return 1; |
| 630 | } |
Nguyễn Thái Ngọc Duy | 1376e50 | 2010-12-17 19:45:33 +0700 | [diff] [blame] | 631 | strbuf_setlen(base, base_offset + baselen); |
Nguyễn Thái Ngọc Duy | d38f280 | 2010-12-15 22:02:46 +0700 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | * Match all directories. We'll try to match files |
| 635 | * later on. |
| 636 | */ |
| 637 | if (ps->recursive && S_ISDIR(entry->mode)) |
| 638 | return 1; |
Nguyễn Thái Ngọc Duy | 2c389fc | 2010-12-15 22:02:40 +0700 | [diff] [blame] | 639 | } |
| 640 | return never_interesting; /* No matches */ |
| 641 | } |