| 1 | #define USE_THE_REPOSITORY_VARIABLE |
| 2 | |
| 3 | #include "git-compat-util.h" |
| 4 | #include "tree-walk.h" |
| 5 | #include "dir.h" |
| 6 | #include "gettext.h" |
| 7 | #include "hex.h" |
| 8 | #include "object-file.h" |
| 9 | #include "odb.h" |
| 10 | #include "trace2.h" |
| 11 | #include "tree.h" |
| 12 | #include "pathspec.h" |
| 13 | #include "json-writer.h" |
| 14 | #include "environment.h" |
| 15 | #include "read-cache-ll.h" |
| 16 | |
| 17 | static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err) |
| 18 | { |
| 19 | const char *path; |
| 20 | unsigned int len; |
| 21 | uint16_t mode; |
| 22 | const unsigned hashsz = desc->algo->rawsz; |
| 23 | |
| 24 | if (size < hashsz + 3 || buf[size - (hashsz + 1)]) { |
| 25 | strbuf_addstr(err, _("too-short tree object")); |
| 26 | return -1; |
| 27 | } |
| 28 | |
| 29 | path = parse_mode(buf, &mode); |
| 30 | if (!path) { |
| 31 | strbuf_addstr(err, _("malformed mode in tree entry")); |
| 32 | return -1; |
| 33 | } |
| 34 | if (!*path) { |
| 35 | strbuf_addstr(err, _("empty filename in tree entry")); |
| 36 | return -1; |
| 37 | } |
| 38 | len = strlen(path) + 1; |
| 39 | |
| 40 | /* Initialize the descriptor entry */ |
| 41 | desc->entry.path = path; |
| 42 | desc->entry.mode = (desc->flags & TREE_DESC_RAW_MODES) ? mode : canon_mode(mode); |
| 43 | desc->entry.pathlen = len - 1; |
| 44 | oidread(&desc->entry.oid, (const unsigned char *)path + len, |
| 45 | desc->algo); |
| 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | static int init_tree_desc_internal(struct tree_desc *desc, |
| 51 | const struct object_id *oid, |
| 52 | const void *buffer, unsigned long size, |
| 53 | struct strbuf *err, |
| 54 | enum tree_desc_flags flags) |
| 55 | { |
| 56 | desc->algo = (oid && oid->algo) ? &hash_algos[oid->algo] : the_hash_algo; |
| 57 | desc->buffer = buffer; |
| 58 | desc->size = size; |
| 59 | desc->flags = flags; |
| 60 | if (size) |
| 61 | return decode_tree_entry(desc, buffer, size, err); |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | void init_tree_desc(struct tree_desc *desc, const struct object_id *tree_oid, |
| 66 | const void *buffer, unsigned long size) |
| 67 | { |
| 68 | struct strbuf err = STRBUF_INIT; |
| 69 | if (init_tree_desc_internal(desc, tree_oid, buffer, size, &err, 0)) |
| 70 | die("%s", err.buf); |
| 71 | strbuf_release(&err); |
| 72 | } |
| 73 | |
| 74 | int init_tree_desc_gently(struct tree_desc *desc, const struct object_id *oid, |
| 75 | const void *buffer, unsigned long size, |
| 76 | enum tree_desc_flags flags) |
| 77 | { |
| 78 | struct strbuf err = STRBUF_INIT; |
| 79 | int result = init_tree_desc_internal(desc, oid, buffer, size, &err, flags); |
| 80 | if (result) |
| 81 | error("%s", err.buf); |
| 82 | strbuf_release(&err); |
| 83 | return result; |
| 84 | } |
| 85 | |
| 86 | void *fill_tree_descriptor(struct repository *r, |
| 87 | struct tree_desc *desc, |
| 88 | const struct object_id *oid) |
| 89 | { |
| 90 | size_t size = 0; |
| 91 | void *buf = NULL; |
| 92 | |
| 93 | if (oid) { |
| 94 | buf = odb_read_object_peeled(r->objects, oid, OBJ_TREE, &size, NULL); |
| 95 | if (!buf) |
| 96 | die(_("unable to read tree (%s)"), oid_to_hex(oid)); |
| 97 | } |
| 98 | init_tree_desc(desc, oid, buf, size); |
| 99 | return buf; |
| 100 | } |
| 101 | |
| 102 | static void entry_clear(struct name_entry *a) |
| 103 | { |
| 104 | memset(a, 0, sizeof(*a)); |
| 105 | } |
| 106 | |
| 107 | static void entry_extract(struct tree_desc *t, struct name_entry *a) |
| 108 | { |
| 109 | *a = t->entry; |
| 110 | } |
| 111 | |
| 112 | static int update_tree_entry_internal(struct tree_desc *desc, struct strbuf *err) |
| 113 | { |
| 114 | const void *buf = desc->buffer; |
| 115 | const unsigned char *end = (const unsigned char *)desc->entry.path + desc->entry.pathlen + 1 + desc->algo->rawsz; |
| 116 | unsigned long size = desc->size; |
| 117 | unsigned long len = end - (const unsigned char *)buf; |
| 118 | |
| 119 | if (size < len) |
| 120 | die(_("too-short tree file")); |
| 121 | buf = end; |
| 122 | size -= len; |
| 123 | desc->buffer = buf; |
| 124 | desc->size = size; |
| 125 | if (size) |
| 126 | return decode_tree_entry(desc, buf, size, err); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void update_tree_entry(struct tree_desc *desc) |
| 131 | { |
| 132 | struct strbuf err = STRBUF_INIT; |
| 133 | if (update_tree_entry_internal(desc, &err)) |
| 134 | die("%s", err.buf); |
| 135 | strbuf_release(&err); |
| 136 | } |
| 137 | |
| 138 | int update_tree_entry_gently(struct tree_desc *desc) |
| 139 | { |
| 140 | struct strbuf err = STRBUF_INIT; |
| 141 | if (update_tree_entry_internal(desc, &err)) { |
| 142 | error("%s", err.buf); |
| 143 | strbuf_release(&err); |
| 144 | /* Stop processing this tree after error */ |
| 145 | desc->size = 0; |
| 146 | return -1; |
| 147 | } |
| 148 | strbuf_release(&err); |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | int tree_entry(struct tree_desc *desc, struct name_entry *entry) |
| 153 | { |
| 154 | if (!desc->size) |
| 155 | return 0; |
| 156 | |
| 157 | *entry = desc->entry; |
| 158 | update_tree_entry(desc); |
| 159 | return 1; |
| 160 | } |
| 161 | |
| 162 | int tree_entry_gently(struct tree_desc *desc, struct name_entry *entry) |
| 163 | { |
| 164 | if (!desc->size) |
| 165 | return 0; |
| 166 | |
| 167 | *entry = desc->entry; |
| 168 | if (update_tree_entry_gently(desc)) |
| 169 | return 0; |
| 170 | return 1; |
| 171 | } |
| 172 | |
| 173 | static int traverse_trees_atexit_registered; |
| 174 | static int traverse_trees_count; |
| 175 | static int traverse_trees_cur_depth; |
| 176 | static int traverse_trees_max_depth; |
| 177 | |
| 178 | static void trace2_traverse_trees_statistics_atexit(void) |
| 179 | { |
| 180 | struct json_writer jw = JSON_WRITER_INIT; |
| 181 | |
| 182 | jw_object_begin(&jw, 0); |
| 183 | jw_object_intmax(&jw, "traverse_trees_count", traverse_trees_count); |
| 184 | jw_object_intmax(&jw, "traverse_trees_max_depth", traverse_trees_max_depth); |
| 185 | jw_end(&jw); |
| 186 | |
| 187 | trace2_data_json("traverse_trees", the_repository, "statistics", &jw); |
| 188 | |
| 189 | jw_release(&jw); |
| 190 | } |
| 191 | |
| 192 | void setup_traverse_info(struct traverse_info *info, const char *base) |
| 193 | { |
| 194 | size_t pathlen = strlen(base); |
| 195 | static struct traverse_info dummy; |
| 196 | |
| 197 | memset(info, 0, sizeof(*info)); |
| 198 | if (pathlen && base[pathlen-1] == '/') |
| 199 | pathlen--; |
| 200 | info->pathlen = pathlen ? pathlen + 1 : 0; |
| 201 | info->name = base; |
| 202 | info->namelen = pathlen; |
| 203 | if (pathlen) |
| 204 | info->prev = &dummy; |
| 205 | |
| 206 | if (trace2_is_enabled() && !traverse_trees_atexit_registered) { |
| 207 | atexit(trace2_traverse_trees_statistics_atexit); |
| 208 | traverse_trees_atexit_registered = 1; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | char *make_traverse_path(char *path, size_t pathlen, |
| 213 | const struct traverse_info *info, |
| 214 | const char *name, size_t namelen) |
| 215 | { |
| 216 | /* Always points to the end of the name we're about to add */ |
| 217 | size_t pos = st_add(info->pathlen, namelen); |
| 218 | |
| 219 | if (pos >= pathlen) |
| 220 | BUG("too small buffer passed to make_traverse_path"); |
| 221 | |
| 222 | path[pos] = 0; |
| 223 | for (;;) { |
| 224 | if (pos < namelen) |
| 225 | BUG("traverse_info pathlen does not match strings"); |
| 226 | pos -= namelen; |
| 227 | memcpy(path + pos, name, namelen); |
| 228 | |
| 229 | if (!pos) |
| 230 | break; |
| 231 | path[--pos] = '/'; |
| 232 | |
| 233 | if (!info) |
| 234 | BUG("traverse_info ran out of list items"); |
| 235 | name = info->name; |
| 236 | namelen = info->namelen; |
| 237 | info = info->prev; |
| 238 | } |
| 239 | return path; |
| 240 | } |
| 241 | |
| 242 | void strbuf_make_traverse_path(struct strbuf *out, |
| 243 | const struct traverse_info *info, |
| 244 | const char *name, size_t namelen) |
| 245 | { |
| 246 | size_t len = traverse_path_len(info, namelen); |
| 247 | |
| 248 | strbuf_grow(out, len); |
| 249 | make_traverse_path(out->buf + out->len, out->alloc - out->len, |
| 250 | info, name, namelen); |
| 251 | strbuf_setlen(out, out->len + len); |
| 252 | } |
| 253 | |
| 254 | struct tree_desc_skip { |
| 255 | struct tree_desc_skip *prev; |
| 256 | const void *ptr; |
| 257 | }; |
| 258 | |
| 259 | struct tree_desc_x { |
| 260 | struct tree_desc d; |
| 261 | struct tree_desc_skip *skip; |
| 262 | }; |
| 263 | |
| 264 | static int check_entry_match(const char *a, int a_len, const char *b, int b_len) |
| 265 | { |
| 266 | /* |
| 267 | * The caller wants to pick *a* from a tree or nothing. |
| 268 | * We are looking at *b* in a tree. |
| 269 | * |
| 270 | * (0) If a and b are the same name, we are trivially happy. |
| 271 | * |
| 272 | * There are three possibilities where *a* could be hiding |
| 273 | * behind *b*. |
| 274 | * |
| 275 | * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no |
| 276 | * matter what. |
| 277 | * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree; |
| 278 | * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree. |
| 279 | * |
| 280 | * Otherwise we know *a* won't appear in the tree without |
| 281 | * scanning further. |
| 282 | */ |
| 283 | |
| 284 | int cmp = name_compare(a, a_len, b, b_len); |
| 285 | |
| 286 | /* Most common case first -- reading sync'd trees */ |
| 287 | if (!cmp) |
| 288 | return cmp; |
| 289 | |
| 290 | if (0 < cmp) { |
| 291 | /* a comes after b; it does not matter if it is case (3) |
| 292 | if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/') |
| 293 | return 1; |
| 294 | */ |
| 295 | return 1; /* keep looking */ |
| 296 | } |
| 297 | |
| 298 | /* b comes after a; are we looking at case (2)? */ |
| 299 | if (a_len < b_len && !memcmp(a, b, a_len) && b[a_len] < '/') |
| 300 | return 1; /* keep looking */ |
| 301 | |
| 302 | return -1; /* a cannot appear in the tree */ |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * From the extended tree_desc, extract the first name entry, while |
| 307 | * paying attention to the candidate "first" name. Most importantly, |
| 308 | * when looking for an entry, if there are entries that sorts earlier |
| 309 | * in the tree object representation than that name, skip them and |
| 310 | * process the named entry first. We will remember that we haven't |
| 311 | * processed the first entry yet, and in the later call skip the |
| 312 | * entry we processed early when update_extended_entry() is called. |
| 313 | * |
| 314 | * E.g. if the underlying tree object has these entries: |
| 315 | * |
| 316 | * blob "t-1" |
| 317 | * blob "t-2" |
| 318 | * tree "t" |
| 319 | * blob "t=1" |
| 320 | * |
| 321 | * and the "first" asks for "t", remember that we still need to |
| 322 | * process "t-1" and "t-2" but extract "t". After processing the |
| 323 | * entry "t" from this call, the caller will let us know by calling |
| 324 | * update_extended_entry() that we can remember "t" has been processed |
| 325 | * already. |
| 326 | */ |
| 327 | |
| 328 | static void extended_entry_extract(struct tree_desc_x *t, |
| 329 | struct name_entry *a, |
| 330 | const char *first, |
| 331 | int first_len) |
| 332 | { |
| 333 | const char *path; |
| 334 | int len; |
| 335 | struct tree_desc probe; |
| 336 | struct tree_desc_skip *skip; |
| 337 | |
| 338 | /* |
| 339 | * Extract the first entry from the tree_desc, but skip the |
| 340 | * ones that we already returned in earlier rounds. |
| 341 | */ |
| 342 | while (1) { |
| 343 | if (!t->d.size) { |
| 344 | entry_clear(a); |
| 345 | break; /* not found */ |
| 346 | } |
| 347 | entry_extract(&t->d, a); |
| 348 | for (skip = t->skip; skip; skip = skip->prev) |
| 349 | if (a->path == skip->ptr) |
| 350 | break; /* found */ |
| 351 | if (!skip) |
| 352 | break; |
| 353 | /* We have processed this entry already. */ |
| 354 | update_tree_entry(&t->d); |
| 355 | } |
| 356 | |
| 357 | if (!first || !a->path) |
| 358 | return; |
| 359 | |
| 360 | /* |
| 361 | * The caller wants "first" from this tree, or nothing. |
| 362 | */ |
| 363 | path = a->path; |
| 364 | len = tree_entry_len(a); |
| 365 | switch (check_entry_match(first, first_len, path, len)) { |
| 366 | case -1: |
| 367 | entry_clear(a); |
| 368 | case 0: |
| 369 | return; |
| 370 | default: |
| 371 | break; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * We need to look-ahead -- we suspect that a subtree whose |
| 376 | * name is "first" may be hiding behind the current entry "path". |
| 377 | */ |
| 378 | probe = t->d; |
| 379 | while (probe.size) { |
| 380 | entry_extract(&probe, a); |
| 381 | path = a->path; |
| 382 | len = tree_entry_len(a); |
| 383 | switch (check_entry_match(first, first_len, path, len)) { |
| 384 | case -1: |
| 385 | entry_clear(a); |
| 386 | case 0: |
| 387 | return; |
| 388 | default: |
| 389 | update_tree_entry(&probe); |
| 390 | break; |
| 391 | } |
| 392 | /* keep looking */ |
| 393 | } |
| 394 | entry_clear(a); |
| 395 | } |
| 396 | |
| 397 | static void update_extended_entry(struct tree_desc_x *t, struct name_entry *a) |
| 398 | { |
| 399 | if (t->d.entry.path == a->path) { |
| 400 | update_tree_entry(&t->d); |
| 401 | } else { |
| 402 | /* we have returned this entry early */ |
| 403 | struct tree_desc_skip *skip = xmalloc(sizeof(*skip)); |
| 404 | skip->ptr = a->path; |
| 405 | skip->prev = t->skip; |
| 406 | t->skip = skip; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | static void free_extended_entry(struct tree_desc_x *t) |
| 411 | { |
| 412 | struct tree_desc_skip *p, *s; |
| 413 | |
| 414 | for (s = t->skip; s; s = p) { |
| 415 | p = s->prev; |
| 416 | free(s); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | static inline int prune_traversal(struct index_state *istate, |
| 421 | struct name_entry *e, |
| 422 | struct traverse_info *info, |
| 423 | struct strbuf *base, |
| 424 | int still_interesting) |
| 425 | { |
| 426 | if (!info->pathspec || still_interesting == 2) |
| 427 | return 2; |
| 428 | if (still_interesting < 0) |
| 429 | return still_interesting; |
| 430 | return tree_entry_interesting(istate, e, base, |
| 431 | info->pathspec); |
| 432 | } |
| 433 | |
| 434 | int traverse_trees(struct index_state *istate, |
| 435 | int n, struct tree_desc *t, |
| 436 | struct traverse_info *info) |
| 437 | { |
| 438 | int ret = 0; |
| 439 | struct name_entry *entry; |
| 440 | int i; |
| 441 | struct tree_desc_x *tx; |
| 442 | struct strbuf base = STRBUF_INIT; |
| 443 | int interesting = 1; |
| 444 | char *traverse_path; |
| 445 | struct repository *r = istate ? istate->repo : the_repository; |
| 446 | |
| 447 | if (traverse_trees_cur_depth > r->settings.max_allowed_tree_depth) |
| 448 | return error("exceeded maximum allowed tree depth"); |
| 449 | |
| 450 | traverse_trees_count++; |
| 451 | traverse_trees_cur_depth++; |
| 452 | |
| 453 | if (traverse_trees_cur_depth > traverse_trees_max_depth) |
| 454 | traverse_trees_max_depth = traverse_trees_cur_depth; |
| 455 | |
| 456 | ALLOC_ARRAY(entry, n); |
| 457 | ALLOC_ARRAY(tx, n); |
| 458 | |
| 459 | for (i = 0; i < n; i++) { |
| 460 | tx[i].d = t[i]; |
| 461 | tx[i].skip = NULL; |
| 462 | } |
| 463 | |
| 464 | if (info->prev) { |
| 465 | strbuf_make_traverse_path(&base, info->prev, |
| 466 | info->name, info->namelen); |
| 467 | strbuf_addch(&base, '/'); |
| 468 | traverse_path = xstrndup(base.buf, base.len); |
| 469 | } else { |
| 470 | traverse_path = xstrndup(info->name, info->pathlen); |
| 471 | } |
| 472 | info->traverse_path = traverse_path; |
| 473 | for (;;) { |
| 474 | int trees_used; |
| 475 | unsigned long mask, dirmask; |
| 476 | const char *first = NULL; |
| 477 | int first_len = 0; |
| 478 | struct name_entry *e = NULL; |
| 479 | int len; |
| 480 | |
| 481 | for (i = 0; i < n; i++) { |
| 482 | e = entry + i; |
| 483 | extended_entry_extract(tx + i, e, NULL, 0); |
| 484 | } |
| 485 | |
| 486 | /* |
| 487 | * A tree may have "t-2" at the current location even |
| 488 | * though it may have "t" that is a subtree behind it, |
| 489 | * and another tree may return "t". We want to grab |
| 490 | * all "t" from all trees to match in such a case. |
| 491 | */ |
| 492 | for (i = 0; i < n; i++) { |
| 493 | e = entry + i; |
| 494 | if (!e->path) |
| 495 | continue; |
| 496 | len = tree_entry_len(e); |
| 497 | if (!first) { |
| 498 | first = e->path; |
| 499 | first_len = len; |
| 500 | continue; |
| 501 | } |
| 502 | if (name_compare(e->path, len, first, first_len) < 0) { |
| 503 | first = e->path; |
| 504 | first_len = len; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | if (first) { |
| 509 | for (i = 0; i < n; i++) { |
| 510 | e = entry + i; |
| 511 | extended_entry_extract(tx + i, e, first, first_len); |
| 512 | /* Cull the ones that are not the earliest */ |
| 513 | if (!e->path) |
| 514 | continue; |
| 515 | len = tree_entry_len(e); |
| 516 | if (name_compare(e->path, len, first, first_len)) |
| 517 | entry_clear(e); |
| 518 | } |
| 519 | } |
| 520 | |
| 521 | /* Now we have in entry[i] the earliest name from the trees */ |
| 522 | mask = 0; |
| 523 | dirmask = 0; |
| 524 | for (i = 0; i < n; i++) { |
| 525 | if (!entry[i].path) |
| 526 | continue; |
| 527 | mask |= 1ul << i; |
| 528 | if (S_ISDIR(entry[i].mode)) |
| 529 | dirmask |= 1ul << i; |
| 530 | e = &entry[i]; |
| 531 | } |
| 532 | if (!mask) |
| 533 | break; |
| 534 | interesting = prune_traversal(istate, e, info, &base, interesting); |
| 535 | if (interesting < 0) |
| 536 | break; |
| 537 | if (interesting) { |
| 538 | trees_used = info->fn(n, mask, dirmask, entry, info); |
| 539 | if (trees_used < 0) { |
| 540 | ret = trees_used; |
| 541 | if (!info->show_all_errors) |
| 542 | break; |
| 543 | } |
| 544 | mask &= trees_used; |
| 545 | } |
| 546 | for (i = 0; i < n; i++) |
| 547 | if (mask & (1ul << i)) |
| 548 | update_extended_entry(tx + i, entry + i); |
| 549 | } |
| 550 | for (i = 0; i < n; i++) |
| 551 | free_extended_entry(tx + i); |
| 552 | free(tx); |
| 553 | free(entry); |
| 554 | free(traverse_path); |
| 555 | info->traverse_path = NULL; |
| 556 | strbuf_release(&base); |
| 557 | |
| 558 | traverse_trees_cur_depth--; |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | struct dir_state { |
| 563 | void *tree; |
| 564 | unsigned long size; |
| 565 | struct object_id oid; |
| 566 | }; |
| 567 | |
| 568 | static int find_tree_entry(struct repository *r, struct tree_desc *t, |
| 569 | const char *name, struct object_id *result, |
| 570 | unsigned short *mode) |
| 571 | { |
| 572 | int namelen = strlen(name); |
| 573 | while (t->size) { |
| 574 | const char *entry; |
| 575 | struct object_id oid; |
| 576 | int entrylen, cmp; |
| 577 | |
| 578 | oidcpy(&oid, tree_entry_extract(t, &entry, mode)); |
| 579 | entrylen = tree_entry_len(&t->entry); |
| 580 | update_tree_entry(t); |
| 581 | if (entrylen > namelen) |
| 582 | continue; |
| 583 | cmp = memcmp(name, entry, entrylen); |
| 584 | if (cmp > 0) |
| 585 | continue; |
| 586 | if (cmp < 0) |
| 587 | break; |
| 588 | if (entrylen == namelen) { |
| 589 | oidcpy(result, &oid); |
| 590 | return 0; |
| 591 | } |
| 592 | if (name[entrylen] != '/') |
| 593 | continue; |
| 594 | if (!S_ISDIR(*mode)) |
| 595 | break; |
| 596 | if (++entrylen == namelen) { |
| 597 | oidcpy(result, &oid); |
| 598 | return 0; |
| 599 | } |
| 600 | return get_tree_entry(r, &oid, name + entrylen, result, mode); |
| 601 | } |
| 602 | return -1; |
| 603 | } |
| 604 | |
| 605 | int get_tree_entry(struct repository *r, |
| 606 | const struct object_id *tree_oid, |
| 607 | const char *name, |
| 608 | struct object_id *oid, |
| 609 | unsigned short *mode) |
| 610 | { |
| 611 | int retval; |
| 612 | void *tree; |
| 613 | size_t size; |
| 614 | struct object_id root; |
| 615 | |
| 616 | tree = odb_read_object_peeled(r->objects, tree_oid, OBJ_TREE, &size, &root); |
| 617 | if (!tree) |
| 618 | return -1; |
| 619 | |
| 620 | if (name[0] == '\0') { |
| 621 | oidcpy(oid, &root); |
| 622 | free(tree); |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | if (!size) { |
| 627 | retval = -1; |
| 628 | } else { |
| 629 | struct tree_desc t; |
| 630 | init_tree_desc(&t, tree_oid, tree, size); |
| 631 | retval = find_tree_entry(r, &t, name, oid, mode); |
| 632 | } |
| 633 | free(tree); |
| 634 | return retval; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * This is Linux's built-in max for the number of symlinks to follow. |
| 639 | * That limit, of course, does not affect git, but it's a reasonable |
| 640 | * choice. |
| 641 | */ |
| 642 | #define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40 |
| 643 | |
| 644 | /** |
| 645 | * Find a tree entry by following symlinks in tree_sha (which is |
| 646 | * assumed to be the root of the repository). In the event that a |
| 647 | * symlink points outside the repository (e.g. a link to /foo or a |
| 648 | * root-level link to ../foo), the portion of the link which is |
| 649 | * outside the repository will be returned in result_path, and *mode |
| 650 | * will be set to 0. It is assumed that result_path is uninitialized. |
| 651 | * If there are no symlinks, or the end result of the symlink chain |
| 652 | * points to an object inside the repository, result will be filled in |
| 653 | * with the sha1 of the found object, and *mode will hold the mode of |
| 654 | * the object. |
| 655 | * |
| 656 | * See the code for enum get_oid_result for a description of |
| 657 | * the return values. |
| 658 | */ |
| 659 | enum get_oid_result get_tree_entry_follow_symlinks(struct repository *r, |
| 660 | struct object_id *tree_oid, const char *name, |
| 661 | struct object_id *result, struct strbuf *result_path, |
| 662 | unsigned short *mode) |
| 663 | { |
| 664 | int retval = MISSING_OBJECT; |
| 665 | struct dir_state *parents = NULL; |
| 666 | size_t parents_alloc = 0; |
| 667 | size_t i, parents_nr = 0; |
| 668 | struct object_id current_tree_oid; |
| 669 | struct strbuf namebuf = STRBUF_INIT; |
| 670 | struct tree_desc t; |
| 671 | int follows_remaining = GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS; |
| 672 | |
| 673 | init_tree_desc(&t, NULL, NULL, 0UL); |
| 674 | strbuf_addstr(&namebuf, name); |
| 675 | oidcpy(¤t_tree_oid, tree_oid); |
| 676 | |
| 677 | while (1) { |
| 678 | int find_result; |
| 679 | char *first_slash; |
| 680 | char *remainder = NULL; |
| 681 | |
| 682 | if (!t.buffer) { |
| 683 | void *tree; |
| 684 | struct object_id root; |
| 685 | size_t size; |
| 686 | tree = odb_read_object_peeled(r->objects, ¤t_tree_oid, |
| 687 | OBJ_TREE, &size, &root); |
| 688 | if (!tree) |
| 689 | goto done; |
| 690 | |
| 691 | ALLOC_GROW(parents, parents_nr + 1, parents_alloc); |
| 692 | parents[parents_nr].tree = tree; |
| 693 | parents[parents_nr].size = size; |
| 694 | oidcpy(&parents[parents_nr].oid, &root); |
| 695 | parents_nr++; |
| 696 | |
| 697 | if (namebuf.buf[0] == '\0') { |
| 698 | oidcpy(result, &root); |
| 699 | retval = FOUND; |
| 700 | goto done; |
| 701 | } |
| 702 | |
| 703 | if (!size) |
| 704 | goto done; |
| 705 | |
| 706 | /* descend */ |
| 707 | init_tree_desc(&t, ¤t_tree_oid, tree, size); |
| 708 | } |
| 709 | |
| 710 | /* Handle symlinks to e.g. a//b by removing leading slashes */ |
| 711 | while (namebuf.buf[0] == '/') { |
| 712 | strbuf_remove(&namebuf, 0, 1); |
| 713 | } |
| 714 | |
| 715 | /* Split namebuf into a first component and a remainder */ |
| 716 | if ((first_slash = strchr(namebuf.buf, '/'))) { |
| 717 | *first_slash = 0; |
| 718 | remainder = first_slash + 1; |
| 719 | } |
| 720 | |
| 721 | if (!strcmp(namebuf.buf, "..")) { |
| 722 | struct dir_state *parent; |
| 723 | /* |
| 724 | * We could end up with .. in the namebuf if it |
| 725 | * appears in a symlink. |
| 726 | */ |
| 727 | |
| 728 | if (parents_nr == 1) { |
| 729 | if (remainder) |
| 730 | *first_slash = '/'; |
| 731 | strbuf_add(result_path, namebuf.buf, |
| 732 | namebuf.len); |
| 733 | *mode = 0; |
| 734 | retval = FOUND; |
| 735 | goto done; |
| 736 | } |
| 737 | parent = &parents[parents_nr - 1]; |
| 738 | free(parent->tree); |
| 739 | parents_nr--; |
| 740 | parent = &parents[parents_nr - 1]; |
| 741 | init_tree_desc(&t, &parent->oid, parent->tree, parent->size); |
| 742 | strbuf_remove(&namebuf, 0, remainder ? 3 : 2); |
| 743 | continue; |
| 744 | } |
| 745 | |
| 746 | /* We could end up here via a symlink to dir/.. */ |
| 747 | if (namebuf.buf[0] == '\0') { |
| 748 | oidcpy(result, &parents[parents_nr - 1].oid); |
| 749 | retval = FOUND; |
| 750 | goto done; |
| 751 | } |
| 752 | |
| 753 | /* Look up the first (or only) path component in the tree. */ |
| 754 | find_result = find_tree_entry(r, &t, namebuf.buf, |
| 755 | ¤t_tree_oid, mode); |
| 756 | if (find_result) { |
| 757 | goto done; |
| 758 | } |
| 759 | |
| 760 | if (S_ISDIR(*mode)) { |
| 761 | if (!remainder) { |
| 762 | oidcpy(result, ¤t_tree_oid); |
| 763 | retval = FOUND; |
| 764 | goto done; |
| 765 | } |
| 766 | /* Descend the tree */ |
| 767 | t.buffer = NULL; |
| 768 | strbuf_remove(&namebuf, 0, |
| 769 | 1 + first_slash - namebuf.buf); |
| 770 | } else if (S_ISREG(*mode)) { |
| 771 | if (!remainder) { |
| 772 | oidcpy(result, ¤t_tree_oid); |
| 773 | retval = FOUND; |
| 774 | } else { |
| 775 | retval = NOT_DIR; |
| 776 | } |
| 777 | goto done; |
| 778 | } else if (S_ISLNK(*mode)) { |
| 779 | /* Follow a symlink */ |
| 780 | unsigned long link_len; |
| 781 | size_t link_len_st = 0; |
| 782 | size_t len; |
| 783 | char *contents, *contents_start; |
| 784 | struct dir_state *parent; |
| 785 | enum object_type type; |
| 786 | |
| 787 | if (follows_remaining-- == 0) { |
| 788 | /* Too many symlinks followed */ |
| 789 | retval = SYMLINK_LOOP; |
| 790 | goto done; |
| 791 | } |
| 792 | |
| 793 | /* |
| 794 | * At this point, we have followed at a least |
| 795 | * one symlink, so on error we need to report this. |
| 796 | */ |
| 797 | retval = DANGLING_SYMLINK; |
| 798 | |
| 799 | contents = odb_read_object(r->objects, |
| 800 | ¤t_tree_oid, &type, |
| 801 | &link_len_st); |
| 802 | link_len = cast_size_t_to_ulong(link_len_st); |
| 803 | |
| 804 | if (!contents) |
| 805 | goto done; |
| 806 | |
| 807 | if (contents[0] == '/') { |
| 808 | strbuf_addstr(result_path, contents); |
| 809 | free(contents); |
| 810 | *mode = 0; |
| 811 | retval = FOUND; |
| 812 | goto done; |
| 813 | } |
| 814 | |
| 815 | if (remainder) |
| 816 | len = first_slash - namebuf.buf; |
| 817 | else |
| 818 | len = namebuf.len; |
| 819 | |
| 820 | contents_start = contents; |
| 821 | |
| 822 | parent = &parents[parents_nr - 1]; |
| 823 | init_tree_desc(&t, &parent->oid, parent->tree, parent->size); |
| 824 | strbuf_splice(&namebuf, 0, len, |
| 825 | contents_start, link_len); |
| 826 | if (remainder) |
| 827 | namebuf.buf[link_len] = '/'; |
| 828 | free(contents); |
| 829 | } |
| 830 | } |
| 831 | done: |
| 832 | for (i = 0; i < parents_nr; i++) |
| 833 | free(parents[i].tree); |
| 834 | free(parents); |
| 835 | |
| 836 | strbuf_release(&namebuf); |
| 837 | return retval; |
| 838 | } |
| 839 | |
| 840 | static int match_entry(const struct pathspec_item *item, |
| 841 | const struct name_entry *entry, int pathlen, |
| 842 | const char *match, int matchlen, |
| 843 | enum interesting *never_interesting) |
| 844 | { |
| 845 | int m = -1; /* signals that we haven't called strncmp() */ |
| 846 | |
| 847 | if (item->magic & PATHSPEC_ICASE) |
| 848 | /* |
| 849 | * "Never interesting" trick requires exact |
| 850 | * matching. We could do something clever with inexact |
| 851 | * matching, but it's trickier (and not to forget that |
| 852 | * strcasecmp is locale-dependent, at least in |
| 853 | * glibc). Just disable it for now. It can't be worse |
| 854 | * than the wildcard's codepath of '[Tt][Hi][Is][Ss]' |
| 855 | * pattern. |
| 856 | */ |
| 857 | *never_interesting = entry_not_interesting; |
| 858 | else if (*never_interesting != entry_not_interesting) { |
| 859 | /* |
| 860 | * We have not seen any match that sorts later |
| 861 | * than the current path. |
| 862 | */ |
| 863 | |
| 864 | /* |
| 865 | * Does match sort strictly earlier than path |
| 866 | * with their common parts? |
| 867 | */ |
| 868 | m = strncmp(match, entry->path, |
| 869 | (matchlen < pathlen) ? matchlen : pathlen); |
| 870 | if (m < 0) |
| 871 | return 0; |
| 872 | |
| 873 | /* |
| 874 | * If we come here even once, that means there is at |
| 875 | * least one pathspec that would sort equal to or |
| 876 | * later than the path we are currently looking at. |
| 877 | * In other words, if we have never reached this point |
| 878 | * after iterating all pathspecs, it means all |
| 879 | * pathspecs are either outside of base, or inside the |
| 880 | * base but sorts strictly earlier than the current |
| 881 | * one. In either case, they will never match the |
| 882 | * subsequent entries. In such a case, we initialized |
| 883 | * the variable to -1 and that is what will be |
| 884 | * returned, allowing the caller to terminate early. |
| 885 | */ |
| 886 | *never_interesting = entry_not_interesting; |
| 887 | } |
| 888 | |
| 889 | if (pathlen > matchlen) |
| 890 | return 0; |
| 891 | |
| 892 | if (matchlen > pathlen) { |
| 893 | if (match[pathlen] != '/') |
| 894 | return 0; |
| 895 | /* |
| 896 | * Reject non-directories as partial pathnames, except |
| 897 | * when match is a submodule with a trailing slash and |
| 898 | * nothing else (to handle 'submod/' and 'submod' |
| 899 | * uniformly). |
| 900 | */ |
| 901 | if (!S_ISDIR(entry->mode) && |
| 902 | (!S_ISGITLINK(entry->mode) || matchlen > pathlen + 1)) |
| 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | if (m == -1) |
| 907 | /* |
| 908 | * we cheated and did not do strncmp(), so we do |
| 909 | * that here. |
| 910 | */ |
| 911 | m = ps_strncmp(item, match, entry->path, pathlen); |
| 912 | |
| 913 | /* |
| 914 | * If common part matched earlier then it is a hit, |
| 915 | * because we rejected the case where path is not a |
| 916 | * leading directory and is shorter than match. |
| 917 | */ |
| 918 | if (!m) |
| 919 | /* |
| 920 | * match_entry does not check if the prefix part is |
| 921 | * matched case-sensitively. If the entry is a |
| 922 | * directory and part of prefix, it'll be rematched |
| 923 | * eventually by basecmp with special treatment for |
| 924 | * the prefix. |
| 925 | */ |
| 926 | return 1; |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | /* :(icase)-aware string compare */ |
| 932 | static int basecmp(const struct pathspec_item *item, |
| 933 | const char *base, const char *match, int len) |
| 934 | { |
| 935 | if (item->magic & PATHSPEC_ICASE) { |
| 936 | int ret, n = len > item->prefix ? item->prefix : len; |
| 937 | ret = strncmp(base, match, n); |
| 938 | if (ret) |
| 939 | return ret; |
| 940 | base += n; |
| 941 | match += n; |
| 942 | len -= n; |
| 943 | } |
| 944 | return ps_strncmp(item, base, match, len); |
| 945 | } |
| 946 | |
| 947 | static int match_dir_prefix(const struct pathspec_item *item, |
| 948 | const char *base, |
| 949 | const char *match, int matchlen) |
| 950 | { |
| 951 | if (basecmp(item, base, match, matchlen)) |
| 952 | return 0; |
| 953 | |
| 954 | /* |
| 955 | * If the base is a subdirectory of a path which |
| 956 | * was specified, all of them are interesting. |
| 957 | */ |
| 958 | if (!matchlen || |
| 959 | base[matchlen] == '/' || |
| 960 | match[matchlen - 1] == '/') |
| 961 | return 1; |
| 962 | |
| 963 | /* Just a random prefix match */ |
| 964 | return 0; |
| 965 | } |
| 966 | |
| 967 | /* |
| 968 | * Perform matching on the leading non-wildcard part of |
| 969 | * pathspec. item->nowildcard_len must be greater than zero. Return |
| 970 | * non-zero if base is matched. |
| 971 | */ |
| 972 | static int match_wildcard_base(const struct pathspec_item *item, |
| 973 | const char *base, int baselen, |
| 974 | int *matched) |
| 975 | { |
| 976 | const char *match = item->match; |
| 977 | /* the wildcard part is not considered in this function */ |
| 978 | int matchlen = item->nowildcard_len; |
| 979 | |
| 980 | if (baselen) { |
| 981 | int dirlen; |
| 982 | /* |
| 983 | * Return early if base is longer than the |
| 984 | * non-wildcard part but it does not match. |
| 985 | */ |
| 986 | if (baselen >= matchlen) { |
| 987 | *matched = matchlen; |
| 988 | return !basecmp(item, base, match, matchlen); |
| 989 | } |
| 990 | |
| 991 | dirlen = matchlen; |
| 992 | while (dirlen && match[dirlen - 1] != '/') |
| 993 | dirlen--; |
| 994 | |
| 995 | /* |
| 996 | * Return early if base is shorter than the |
| 997 | * non-wildcard part but it does not match. Note that |
| 998 | * base ends with '/' so we are sure it really matches |
| 999 | * directory |
| 1000 | */ |
| 1001 | if (basecmp(item, base, match, baselen)) |
| 1002 | return 0; |
| 1003 | *matched = baselen; |
| 1004 | } else |
| 1005 | *matched = 0; |
| 1006 | /* |
| 1007 | * we could have checked entry against the non-wildcard part |
| 1008 | * that is not in base and does similar never_interesting |
| 1009 | * optimization as in match_entry. For now just be happy with |
| 1010 | * base comparison. |
| 1011 | */ |
| 1012 | return entry_interesting; |
| 1013 | } |
| 1014 | |
| 1015 | /* |
| 1016 | * Is a tree entry interesting given the pathspec we have? |
| 1017 | * |
| 1018 | * Pre-condition: either baselen == 0 (i.e. empty path) |
| 1019 | * or base[baselen-1] == '/' (i.e. with trailing slash). |
| 1020 | */ |
| 1021 | static enum interesting do_match(struct index_state *istate, |
| 1022 | const struct name_entry *entry, |
| 1023 | struct strbuf *base, |
| 1024 | const struct pathspec *ps, |
| 1025 | int exclude) |
| 1026 | { |
| 1027 | int i; |
| 1028 | int pathlen, baselen = base->len; |
| 1029 | enum interesting never_interesting = ps->has_wildcard ? |
| 1030 | entry_not_interesting : all_entries_not_interesting; |
| 1031 | |
| 1032 | GUARD_PATHSPEC(ps, |
| 1033 | PATHSPEC_FROMTOP | |
| 1034 | PATHSPEC_MAXDEPTH | |
| 1035 | PATHSPEC_LITERAL | |
| 1036 | PATHSPEC_GLOB | |
| 1037 | PATHSPEC_ICASE | |
| 1038 | PATHSPEC_EXCLUDE | |
| 1039 | PATHSPEC_ATTR); |
| 1040 | |
| 1041 | if (!ps->nr) { |
| 1042 | if (!ps->recursive || |
| 1043 | !(ps->magic & PATHSPEC_MAXDEPTH) || |
| 1044 | ps->max_depth == -1) |
| 1045 | return all_entries_interesting; |
| 1046 | return within_depth(base->buf, baselen, |
| 1047 | !!S_ISDIR(entry->mode), |
| 1048 | ps->max_depth) ? |
| 1049 | entry_interesting : entry_not_interesting; |
| 1050 | } |
| 1051 | |
| 1052 | pathlen = tree_entry_len(entry); |
| 1053 | |
| 1054 | for (i = ps->nr - 1; i >= 0; i--) { |
| 1055 | const struct pathspec_item *item = ps->items+i; |
| 1056 | const char *match = item->match; |
| 1057 | const char *base_str = base->buf; |
| 1058 | int matchlen = item->len, matched = 0; |
| 1059 | |
| 1060 | if ((!exclude && item->magic & PATHSPEC_EXCLUDE) || |
| 1061 | ( exclude && !(item->magic & PATHSPEC_EXCLUDE))) |
| 1062 | continue; |
| 1063 | |
| 1064 | if (baselen >= matchlen) { |
| 1065 | /* If it doesn't match, move along... */ |
| 1066 | if (!match_dir_prefix(item, base_str, match, matchlen)) |
| 1067 | goto match_wildcards; |
| 1068 | |
| 1069 | if (!ps->recursive || |
| 1070 | !(ps->magic & PATHSPEC_MAXDEPTH) || |
| 1071 | ps->max_depth == -1) { |
| 1072 | if (!item->attr_match_nr) |
| 1073 | return all_entries_interesting; |
| 1074 | else |
| 1075 | goto interesting; |
| 1076 | } |
| 1077 | |
| 1078 | if (within_depth(base_str + matchlen + 1, |
| 1079 | baselen - matchlen - 1, |
| 1080 | !!S_ISDIR(entry->mode), |
| 1081 | ps->max_depth)) |
| 1082 | goto interesting; |
| 1083 | else |
| 1084 | return entry_not_interesting; |
| 1085 | } |
| 1086 | |
| 1087 | /* Either there must be no base, or the base must match. */ |
| 1088 | if (baselen == 0 || !basecmp(item, base_str, match, baselen)) { |
| 1089 | if (match_entry(item, entry, pathlen, |
| 1090 | match + baselen, matchlen - baselen, |
| 1091 | &never_interesting)) |
| 1092 | goto interesting; |
| 1093 | |
| 1094 | if (item->nowildcard_len < item->len) { |
| 1095 | if (!git_fnmatch(item, match + baselen, entry->path, |
| 1096 | item->nowildcard_len - baselen)) |
| 1097 | goto interesting; |
| 1098 | |
| 1099 | /* |
| 1100 | * Match all directories. We'll try to |
| 1101 | * match files later on. |
| 1102 | */ |
| 1103 | if (ps->recursive && S_ISDIR(entry->mode)) |
| 1104 | return entry_interesting; |
| 1105 | |
| 1106 | /* |
| 1107 | * When matching against submodules with |
| 1108 | * wildcard characters, ensure that the entry |
| 1109 | * at least matches up to the first wild |
| 1110 | * character. More accurate matching can then |
| 1111 | * be performed in the submodule itself. |
| 1112 | */ |
| 1113 | if (ps->recurse_submodules && |
| 1114 | S_ISGITLINK(entry->mode) && |
| 1115 | !ps_strncmp(item, match + baselen, |
| 1116 | entry->path, |
| 1117 | item->nowildcard_len - baselen)) |
| 1118 | goto interesting; |
| 1119 | } |
| 1120 | |
| 1121 | continue; |
| 1122 | } |
| 1123 | |
| 1124 | match_wildcards: |
| 1125 | if (item->nowildcard_len == item->len) |
| 1126 | continue; |
| 1127 | |
| 1128 | if (item->nowildcard_len && |
| 1129 | !match_wildcard_base(item, base_str, baselen, &matched)) |
| 1130 | continue; |
| 1131 | |
| 1132 | /* |
| 1133 | * Concatenate base and entry->path into one and do |
| 1134 | * fnmatch() on it. |
| 1135 | * |
| 1136 | * While we could avoid concatenation in certain cases |
| 1137 | * [1], which saves a memcpy and potentially a |
| 1138 | * realloc, it turns out not worth it. Measurement on |
| 1139 | * linux-2.6 does not show any clear improvements, |
| 1140 | * partly because of the nowildcard_len optimization |
| 1141 | * in git_fnmatch(). Avoid micro-optimizations here. |
| 1142 | * |
| 1143 | * [1] if match_wildcard_base() says the base |
| 1144 | * directory is already matched, we only need to match |
| 1145 | * the rest, which is shorter so _in theory_ faster. |
| 1146 | */ |
| 1147 | |
| 1148 | strbuf_add(base, entry->path, pathlen); |
| 1149 | |
| 1150 | if (!git_fnmatch(item, match, base->buf, |
| 1151 | item->nowildcard_len)) { |
| 1152 | strbuf_setlen(base, baselen); |
| 1153 | goto interesting; |
| 1154 | } |
| 1155 | |
| 1156 | /* |
| 1157 | * When matching against submodules with |
| 1158 | * wildcard characters, ensure that the entry |
| 1159 | * at least matches up to the first wild |
| 1160 | * character. More accurate matching can then |
| 1161 | * be performed in the submodule itself. |
| 1162 | */ |
| 1163 | if (ps->recurse_submodules && S_ISGITLINK(entry->mode) && |
| 1164 | !ps_strncmp(item, match, base->buf, |
| 1165 | item->nowildcard_len)) { |
| 1166 | strbuf_setlen(base, baselen); |
| 1167 | goto interesting; |
| 1168 | } |
| 1169 | |
| 1170 | strbuf_setlen(base, baselen); |
| 1171 | |
| 1172 | /* |
| 1173 | * Match all directories. We'll try to match files |
| 1174 | * later on. |
| 1175 | * max_depth is ignored but we may consider support it |
| 1176 | * in future, see |
| 1177 | * https://lore.kernel.org/git/7vmxo5l2g4.fsf@alter.siamese.dyndns.org/ |
| 1178 | */ |
| 1179 | if (ps->recursive && S_ISDIR(entry->mode)) |
| 1180 | return entry_interesting; |
| 1181 | continue; |
| 1182 | interesting: |
| 1183 | if (item->attr_match_nr) { |
| 1184 | int ret; |
| 1185 | |
| 1186 | /* |
| 1187 | * Must not return all_entries_not_interesting |
| 1188 | * prematurely. We do not know if all entries do not |
| 1189 | * match some attributes with current attr API. |
| 1190 | */ |
| 1191 | never_interesting = entry_not_interesting; |
| 1192 | |
| 1193 | /* |
| 1194 | * Consider all directories interesting (because some |
| 1195 | * of those files inside may match some attributes |
| 1196 | * even though the parent dir does not) |
| 1197 | * |
| 1198 | * FIXME: attributes _can_ match directories and we |
| 1199 | * can probably return all_entries_interesting or |
| 1200 | * all_entries_not_interesting here if matched. |
| 1201 | */ |
| 1202 | if (S_ISDIR(entry->mode)) |
| 1203 | return entry_interesting; |
| 1204 | |
| 1205 | strbuf_add(base, entry->path, pathlen); |
| 1206 | ret = match_pathspec_attrs(istate, base->buf, |
| 1207 | base->len, item); |
| 1208 | strbuf_setlen(base, baselen); |
| 1209 | if (!ret) |
| 1210 | continue; |
| 1211 | } |
| 1212 | return entry_interesting; |
| 1213 | } |
| 1214 | return never_interesting; /* No matches */ |
| 1215 | } |
| 1216 | |
| 1217 | /* |
| 1218 | * Is a tree entry interesting given the pathspec we have? |
| 1219 | * |
| 1220 | * Pre-condition: either baselen == 0 (i.e. empty path) |
| 1221 | * or base[baselen-1] == '/' (i.e. with trailing slash). |
| 1222 | */ |
| 1223 | enum interesting tree_entry_interesting(struct index_state *istate, |
| 1224 | const struct name_entry *entry, |
| 1225 | struct strbuf *base, |
| 1226 | const struct pathspec *ps) |
| 1227 | { |
| 1228 | enum interesting positive, negative; |
| 1229 | positive = do_match(istate, entry, base, ps, 0); |
| 1230 | |
| 1231 | /* |
| 1232 | * case | entry | positive | negative | result |
| 1233 | * -----+-------+----------+----------+------- |
| 1234 | * 1 | file | -1 | -1..2 | -1 |
| 1235 | * 2 | file | 0 | -1..2 | 0 |
| 1236 | * 3 | file | 1 | -1 | 1 |
| 1237 | * 4 | file | 1 | 0 | 1 |
| 1238 | * 5 | file | 1 | 1 | 0 |
| 1239 | * 6 | file | 1 | 2 | 0 |
| 1240 | * 7 | file | 2 | -1 | 2 |
| 1241 | * 8 | file | 2 | 0 | 1 |
| 1242 | * 9 | file | 2 | 1 | 0 |
| 1243 | * 10 | file | 2 | 2 | -1 |
| 1244 | * -----+-------+----------+----------+------- |
| 1245 | * 11 | dir | -1 | -1..2 | -1 |
| 1246 | * 12 | dir | 0 | -1..2 | 0 |
| 1247 | * 13 | dir | 1 | -1 | 1 |
| 1248 | * 14 | dir | 1 | 0 | 1 |
| 1249 | * 15 | dir | 1 | 1 | 1 (*) |
| 1250 | * 16 | dir | 1 | 2 | 0 |
| 1251 | * 17 | dir | 2 | -1 | 2 |
| 1252 | * 18 | dir | 2 | 0 | 1 |
| 1253 | * 19 | dir | 2 | 1 | 1 (*) |
| 1254 | * 20 | dir | 2 | 2 | -1 |
| 1255 | * |
| 1256 | * (*) An exclude pattern interested in a directory does not |
| 1257 | * necessarily mean it will exclude all of the directory. In |
| 1258 | * wildcard case, it can't decide until looking at individual |
| 1259 | * files inside. So don't write such directories off yet. |
| 1260 | */ |
| 1261 | |
| 1262 | if (!(ps->magic & PATHSPEC_EXCLUDE) || |
| 1263 | positive <= entry_not_interesting) /* #1, #2, #11, #12 */ |
| 1264 | return positive; |
| 1265 | |
| 1266 | negative = do_match(istate, entry, base, ps, 1); |
| 1267 | |
| 1268 | /* #8, #18 */ |
| 1269 | if (positive == all_entries_interesting && |
| 1270 | negative == entry_not_interesting) |
| 1271 | return entry_interesting; |
| 1272 | |
| 1273 | /* #3, #4, #7, #13, #14, #17 */ |
| 1274 | if (negative <= entry_not_interesting) |
| 1275 | return positive; |
| 1276 | |
| 1277 | /* #15, #19 */ |
| 1278 | if (S_ISDIR(entry->mode) && |
| 1279 | positive >= entry_interesting && |
| 1280 | negative == entry_interesting) |
| 1281 | return entry_interesting; |
| 1282 | |
| 1283 | if ((positive == entry_interesting && |
| 1284 | negative >= entry_interesting) || /* #5, #6, #16 */ |
| 1285 | (positive == all_entries_interesting && |
| 1286 | negative == entry_interesting)) /* #9 */ |
| 1287 | return entry_not_interesting; |
| 1288 | |
| 1289 | return all_entries_not_interesting; /* #10, #20 */ |
| 1290 | } |