THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1063 lines
27 KiB

  1. #include "cache.h"
  2. #include "tree-walk.h"
  3. #include "unpack-trees.h"
  4. #include "dir.h"
  5. #include "tree.h"
  6. #include "pathspec.h"
  7. static const char *get_mode(const char *str, unsigned int *modep)
  8. {
  9. unsigned char c;
  10. unsigned int mode = 0;
  11. if (*str == ' ')
  12. return NULL;
  13. while ((c = *str++) != ' ') {
  14. if (c < '0' || c > '7')
  15. return NULL;
  16. mode = (mode << 3) + (c - '0');
  17. }
  18. *modep = mode;
  19. return str;
  20. }
  21. static void decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size)
  22. {
  23. const char *path;
  24. unsigned int mode, len;
  25. if (size < 24 || buf[size - 21])
  26. die("corrupt tree file");
  27. path = get_mode(buf, &mode);
  28. if (!path || !*path)
  29. die("corrupt tree file");
  30. len = strlen(path) + 1;
  31. /* Initialize the descriptor entry */
  32. desc->entry.path = path;
  33. desc->entry.mode = canon_mode(mode);
  34. desc->entry.sha1 = (const unsigned char *)(path + len);
  35. }
  36. void init_tree_desc(struct tree_desc *desc, const void *buffer, unsigned long size)
  37. {
  38. desc->buffer = buffer;
  39. desc->size = size;
  40. if (size)
  41. decode_tree_entry(desc, buffer, size);
  42. }
  43. void *fill_tree_descriptor(struct tree_desc *desc, const unsigned char *sha1)
  44. {
  45. unsigned long size = 0;
  46. void *buf = NULL;
  47. if (sha1) {
  48. buf = read_object_with_reference(sha1, tree_type, &size, NULL);
  49. if (!buf)
  50. die("unable to read tree %s", sha1_to_hex(sha1));
  51. }
  52. init_tree_desc(desc, buf, size);
  53. return buf;
  54. }
  55. static void entry_clear(struct name_entry *a)
  56. {
  57. memset(a, 0, sizeof(*a));
  58. }
  59. static void entry_extract(struct tree_desc *t, struct name_entry *a)
  60. {
  61. *a = t->entry;
  62. }
  63. void update_tree_entry(struct tree_desc *desc)
  64. {
  65. const void *buf = desc->buffer;
  66. const unsigned char *end = desc->entry.sha1 + 20;
  67. unsigned long size = desc->size;
  68. unsigned long len = end - (const unsigned char *)buf;
  69. if (size < len)
  70. die("corrupt tree file");
  71. buf = end;
  72. size -= len;
  73. desc->buffer = buf;
  74. desc->size = size;
  75. if (size)
  76. decode_tree_entry(desc, buf, size);
  77. }
  78. int tree_entry(struct tree_desc *desc, struct name_entry *entry)
  79. {
  80. if (!desc->size)
  81. return 0;
  82. *entry = desc->entry;
  83. update_tree_entry(desc);
  84. return 1;
  85. }
  86. void setup_traverse_info(struct traverse_info *info, const char *base)
  87. {
  88. int pathlen = strlen(base);
  89. static struct traverse_info dummy;
  90. memset(info, 0, sizeof(*info));
  91. if (pathlen && base[pathlen-1] == '/')
  92. pathlen--;
  93. info->pathlen = pathlen ? pathlen + 1 : 0;
  94. info->name.path = base;
  95. info->name.sha1 = (void *)(base + pathlen + 1);
  96. if (pathlen)
  97. info->prev = &dummy;
  98. }
  99. char *make_traverse_path(char *path, const struct traverse_info *info, const struct name_entry *n)
  100. {
  101. int len = tree_entry_len(n);
  102. int pathlen = info->pathlen;
  103. path[pathlen + len] = 0;
  104. for (;;) {
  105. memcpy(path + pathlen, n->path, len);
  106. if (!pathlen)
  107. break;
  108. path[--pathlen] = '/';
  109. n = &info->name;
  110. len = tree_entry_len(n);
  111. info = info->prev;
  112. pathlen -= len;
  113. }
  114. return path;
  115. }
  116. struct tree_desc_skip {
  117. struct tree_desc_skip *prev;
  118. const void *ptr;
  119. };
  120. struct tree_desc_x {
  121. struct tree_desc d;
  122. struct tree_desc_skip *skip;
  123. };
  124. static int check_entry_match(const char *a, int a_len, const char *b, int b_len)
  125. {
  126. /*
  127. * The caller wants to pick *a* from a tree or nothing.
  128. * We are looking at *b* in a tree.
  129. *
  130. * (0) If a and b are the same name, we are trivially happy.
  131. *
  132. * There are three possibilities where *a* could be hiding
  133. * behind *b*.
  134. *
  135. * (1) *a* == "t", *b* == "ab" i.e. *b* sorts earlier than *a* no
  136. * matter what.
  137. * (2) *a* == "t", *b* == "t-2" and "t" is a subtree in the tree;
  138. * (3) *a* == "t-2", *b* == "t" and "t-2" is a blob in the tree.
  139. *
  140. * Otherwise we know *a* won't appear in the tree without
  141. * scanning further.
  142. */
  143. int cmp = name_compare(a, a_len, b, b_len);
  144. /* Most common case first -- reading sync'd trees */
  145. if (!cmp)
  146. return cmp;
  147. if (0 < cmp) {
  148. /* a comes after b; it does not matter if it is case (3)
  149. if (b_len < a_len && !memcmp(a, b, b_len) && a[b_len] < '/')
  150. return 1;
  151. */
  152. return 1; /* keep looking */
  153. }
  154. /* b comes after a; are we looking at case (2)? */
  155. if (a_len < b_len && !memcmp(a, b, a_len) && b[a_len] < '/')
  156. return 1; /* keep looking */
  157. return -1; /* a cannot appear in the tree */
  158. }
  159. /*
  160. * From the extended tree_desc, extract the first name entry, while
  161. * paying attention to the candidate "first" name. Most importantly,
  162. * when looking for an entry, if there are entries that sorts earlier
  163. * in the tree object representation than that name, skip them and
  164. * process the named entry first. We will remember that we haven't
  165. * processed the first entry yet, and in the later call skip the
  166. * entry we processed early when update_extended_entry() is called.
  167. *
  168. * E.g. if the underlying tree object has these entries:
  169. *
  170. * blob "t-1"
  171. * blob "t-2"
  172. * tree "t"
  173. * blob "t=1"
  174. *
  175. * and the "first" asks for "t", remember that we still need to
  176. * process "t-1" and "t-2" but extract "t". After processing the
  177. * entry "t" from this call, the caller will let us know by calling
  178. * update_extended_entry() that we can remember "t" has been processed
  179. * already.
  180. */
  181. static void extended_entry_extract(struct tree_desc_x *t,
  182. struct name_entry *a,
  183. const char *first,
  184. int first_len)
  185. {
  186. const char *path;
  187. int len;
  188. struct tree_desc probe;
  189. struct tree_desc_skip *skip;
  190. /*
  191. * Extract the first entry from the tree_desc, but skip the
  192. * ones that we already returned in earlier rounds.
  193. */
  194. while (1) {
  195. if (!t->d.size) {
  196. entry_clear(a);
  197. break; /* not found */
  198. }
  199. entry_extract(&t->d, a);
  200. for (skip = t->skip; skip; skip = skip->prev)
  201. if (a->path == skip->ptr)
  202. break; /* found */
  203. if (!skip)
  204. break;
  205. /* We have processed this entry already. */
  206. update_tree_entry(&t->d);
  207. }
  208. if (!first || !a->path)
  209. return;
  210. /*
  211. * The caller wants "first" from this tree, or nothing.
  212. */
  213. path = a->path;
  214. len = tree_entry_len(a);
  215. switch (check_entry_match(first, first_len, path, len)) {
  216. case -1:
  217. entry_clear(a);
  218. case 0:
  219. return;
  220. default:
  221. break;
  222. }
  223. /*
  224. * We need to look-ahead -- we suspect that a subtree whose
  225. * name is "first" may be hiding behind the current entry "path".
  226. */
  227. probe = t->d;
  228. while (probe.size) {
  229. entry_extract(&probe, a);
  230. path = a->path;
  231. len = tree_entry_len(a);
  232. switch (check_entry_match(first, first_len, path, len)) {
  233. case -1:
  234. entry_clear(a);
  235. case 0:
  236. return;
  237. default:
  238. update_tree_entry(&probe);
  239. break;
  240. }
  241. /* keep looking */
  242. }
  243. entry_clear(a);
  244. }
  245. static void update_extended_entry(struct tree_desc_x *t, struct name_entry *a)
  246. {
  247. if (t->d.entry.path == a->path) {
  248. update_tree_entry(&t->d);
  249. } else {
  250. /* we have returned this entry early */
  251. struct tree_desc_skip *skip = xmalloc(sizeof(*skip));
  252. skip->ptr = a->path;
  253. skip->prev = t->skip;
  254. t->skip = skip;
  255. }
  256. }
  257. static void free_extended_entry(struct tree_desc_x *t)
  258. {
  259. struct tree_desc_skip *p, *s;
  260. for (s = t->skip; s; s = p) {
  261. p = s->prev;
  262. free(s);
  263. }
  264. }
  265. static inline int prune_traversal(struct name_entry *e,
  266. struct traverse_info *info,
  267. struct strbuf *base,
  268. int still_interesting)
  269. {
  270. if (!info->pathspec || still_interesting == 2)
  271. return 2;
  272. if (still_interesting < 0)
  273. return still_interesting;
  274. return tree_entry_interesting(e, base, 0, info->pathspec);
  275. }
  276. int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
  277. {
  278. int error = 0;
  279. struct name_entry *entry = xmalloc(n*sizeof(*entry));
  280. int i;
  281. struct tree_desc_x *tx = xcalloc(n, sizeof(*tx));
  282. struct strbuf base = STRBUF_INIT;
  283. int interesting = 1;
  284. char *traverse_path;
  285. for (i = 0; i < n; i++)
  286. tx[i].d = t[i];
  287. if (info->prev) {
  288. strbuf_grow(&base, info->pathlen);
  289. make_traverse_path(base.buf, info->prev, &info->name);
  290. base.buf[info->pathlen-1] = '/';
  291. strbuf_setlen(&base, info->pathlen);
  292. traverse_path = xstrndup(base.buf, info->pathlen);
  293. } else {
  294. traverse_path = xstrndup(info->name.path, info->pathlen);
  295. }
  296. info->traverse_path = traverse_path;
  297. for (;;) {
  298. int trees_used;
  299. unsigned long mask, dirmask;
  300. const char *first = NULL;
  301. int first_len = 0;
  302. struct name_entry *e = NULL;
  303. int len;
  304. for (i = 0; i < n; i++) {
  305. e = entry + i;
  306. extended_entry_extract(tx + i, e, NULL, 0);
  307. }
  308. /*
  309. * A tree may have "t-2" at the current location even
  310. * though it may have "t" that is a subtree behind it,
  311. * and another tree may return "t". We want to grab
  312. * all "t" from all trees to match in such a case.
  313. */
  314. for (i = 0; i < n; i++) {
  315. e = entry + i;
  316. if (!e->path)
  317. continue;
  318. len = tree_entry_len(e);
  319. if (!first) {
  320. first = e->path;
  321. first_len = len;
  322. continue;
  323. }
  324. if (name_compare(e->path, len, first, first_len) < 0) {
  325. first = e->path;
  326. first_len = len;
  327. }
  328. }
  329. if (first) {
  330. for (i = 0; i < n; i++) {
  331. e = entry + i;
  332. extended_entry_extract(tx + i, e, first, first_len);
  333. /* Cull the ones that are not the earliest */
  334. if (!e->path)
  335. continue;
  336. len = tree_entry_len(e);
  337. if (name_compare(e->path, len, first, first_len))
  338. entry_clear(e);
  339. }
  340. }
  341. /* Now we have in entry[i] the earliest name from the trees */
  342. mask = 0;
  343. dirmask = 0;
  344. for (i = 0; i < n; i++) {
  345. if (!entry[i].path)
  346. continue;
  347. mask |= 1ul << i;
  348. if (S_ISDIR(entry[i].mode))
  349. dirmask |= 1ul << i;
  350. e = &entry[i];
  351. }
  352. if (!mask)
  353. break;
  354. interesting = prune_traversal(e, info, &base, interesting);
  355. if (interesting < 0)
  356. break;
  357. if (interesting) {
  358. trees_used = info->fn(n, mask, dirmask, entry, info);
  359. if (trees_used < 0) {
  360. error = trees_used;
  361. if (!info->show_all_errors)
  362. break;
  363. }
  364. mask &= trees_used;
  365. }
  366. for (i = 0; i < n; i++)
  367. if (mask & (1ul << i))
  368. update_extended_entry(tx + i, entry + i);
  369. }
  370. free(entry);
  371. for (i = 0; i < n; i++)
  372. free_extended_entry(tx + i);
  373. free(tx);
  374. free(traverse_path);
  375. info->traverse_path = NULL;
  376. strbuf_release(&base);
  377. return error;
  378. }
  379. struct dir_state {
  380. void *tree;
  381. unsigned long size;
  382. unsigned char sha1[20];
  383. };
  384. static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char *result, unsigned *mode)
  385. {
  386. int namelen = strlen(name);
  387. while (t->size) {
  388. const char *entry;
  389. const unsigned char *sha1;
  390. int entrylen, cmp;
  391. sha1 = tree_entry_extract(t, &entry, mode);
  392. entrylen = tree_entry_len(&t->entry);
  393. update_tree_entry(t);
  394. if (entrylen > namelen)
  395. continue;
  396. cmp = memcmp(name, entry, entrylen);
  397. if (cmp > 0)
  398. continue;
  399. if (cmp < 0)
  400. break;
  401. if (entrylen == namelen) {
  402. hashcpy(result, sha1);
  403. return 0;
  404. }
  405. if (name[entrylen] != '/')
  406. continue;
  407. if (!S_ISDIR(*mode))
  408. break;
  409. if (++entrylen == namelen) {
  410. hashcpy(result, sha1);
  411. return 0;
  412. }
  413. return get_tree_entry(sha1, name + entrylen, result, mode);
  414. }
  415. return -1;
  416. }
  417. int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned char *sha1, unsigned *mode)
  418. {
  419. int retval;
  420. void *tree;
  421. unsigned long size;
  422. unsigned char root[20];
  423. tree = read_object_with_reference(tree_sha1, tree_type, &size, root);
  424. if (!tree)
  425. return -1;
  426. if (name[0] == '\0') {
  427. hashcpy(sha1, root);
  428. free(tree);
  429. return 0;
  430. }
  431. if (!size) {
  432. retval = -1;
  433. } else {
  434. struct tree_desc t;
  435. init_tree_desc(&t, tree, size);
  436. retval = find_tree_entry(&t, name, sha1, mode);
  437. }
  438. free(tree);
  439. return retval;
  440. }
  441. /*
  442. * This is Linux's built-in max for the number of symlinks to follow.
  443. * That limit, of course, does not affect git, but it's a reasonable
  444. * choice.
  445. */
  446. #define GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS 40
  447. /**
  448. * Find a tree entry by following symlinks in tree_sha (which is
  449. * assumed to be the root of the repository). In the event that a
  450. * symlink points outside the repository (e.g. a link to /foo or a
  451. * root-level link to ../foo), the portion of the link which is
  452. * outside the repository will be returned in result_path, and *mode
  453. * will be set to 0. It is assumed that result_path is uninitialized.
  454. * If there are no symlinks, or the end result of the symlink chain
  455. * points to an object inside the repository, result will be filled in
  456. * with the sha1 of the found object, and *mode will hold the mode of
  457. * the object.
  458. *
  459. * See the code for enum follow_symlink_result for a description of
  460. * the return values.
  461. */
  462. enum follow_symlinks_result get_tree_entry_follow_symlinks(unsigned char *tree_sha1, const char *name, unsigned char *result, struct strbuf *result_path, unsigned *mode)
  463. {
  464. int retval = MISSING_OBJECT;
  465. struct dir_state *parents = NULL;
  466. size_t parents_alloc = 0;
  467. ssize_t parents_nr = 0;
  468. unsigned char current_tree_sha1[20];
  469. struct strbuf namebuf = STRBUF_INIT;
  470. struct tree_desc t;
  471. int follows_remaining = GET_TREE_ENTRY_FOLLOW_SYMLINKS_MAX_LINKS;
  472. int i;
  473. init_tree_desc(&t, NULL, 0UL);
  474. strbuf_init(result_path, 0);
  475. strbuf_addstr(&namebuf, name);
  476. hashcpy(current_tree_sha1, tree_sha1);
  477. while (1) {
  478. int find_result;
  479. char *first_slash;
  480. char *remainder = NULL;
  481. if (!t.buffer) {
  482. void *tree;
  483. unsigned char root[20];
  484. unsigned long size;
  485. tree = read_object_with_reference(current_tree_sha1,
  486. tree_type, &size,
  487. root);
  488. if (!tree)
  489. goto done;
  490. ALLOC_GROW(parents, parents_nr + 1, parents_alloc);
  491. parents[parents_nr].tree = tree;
  492. parents[parents_nr].size = size;
  493. hashcpy(parents[parents_nr].sha1, root);
  494. parents_nr++;
  495. if (namebuf.buf[0] == '\0') {
  496. hashcpy(result, root);
  497. retval = FOUND;
  498. goto done;
  499. }
  500. if (!size)
  501. goto done;
  502. /* descend */
  503. init_tree_desc(&t, tree, size);
  504. }
  505. /* Handle symlinks to e.g. a//b by removing leading slashes */
  506. while (namebuf.buf[0] == '/') {
  507. strbuf_remove(&namebuf, 0, 1);
  508. }
  509. /* Split namebuf into a first component and a remainder */
  510. if ((first_slash = strchr(namebuf.buf, '/'))) {
  511. *first_slash = 0;
  512. remainder = first_slash + 1;
  513. }
  514. if (!strcmp(namebuf.buf, "..")) {
  515. struct dir_state *parent;
  516. /*
  517. * We could end up with .. in the namebuf if it
  518. * appears in a symlink.
  519. */
  520. if (parents_nr == 1) {
  521. if (remainder)
  522. *first_slash = '/';
  523. strbuf_add(result_path, namebuf.buf,
  524. namebuf.len);
  525. *mode = 0;
  526. retval = FOUND;
  527. goto done;
  528. }
  529. parent = &parents[parents_nr - 1];
  530. free(parent->tree);
  531. parents_nr--;
  532. parent = &parents[parents_nr - 1];
  533. init_tree_desc(&t, parent->tree, parent->size);
  534. strbuf_remove(&namebuf, 0, remainder ? 3 : 2);
  535. continue;
  536. }
  537. /* We could end up here via a symlink to dir/.. */
  538. if (namebuf.buf[0] == '\0') {
  539. hashcpy(result, parents[parents_nr - 1].sha1);
  540. retval = FOUND;
  541. goto done;
  542. }
  543. /* Look up the first (or only) path component in the tree. */
  544. find_result = find_tree_entry(&t, namebuf.buf,
  545. current_tree_sha1, mode);
  546. if (find_result) {
  547. goto done;
  548. }
  549. if (S_ISDIR(*mode)) {
  550. if (!remainder) {
  551. hashcpy(result, current_tree_sha1);
  552. retval = FOUND;
  553. goto done;
  554. }
  555. /* Descend the tree */
  556. t.buffer = NULL;
  557. strbuf_remove(&namebuf, 0,
  558. 1 + first_slash - namebuf.buf);
  559. } else if (S_ISREG(*mode)) {
  560. if (!remainder) {
  561. hashcpy(result, current_tree_sha1);
  562. retval = FOUND;
  563. } else {
  564. retval = NOT_DIR;
  565. }
  566. goto done;
  567. } else if (S_ISLNK(*mode)) {
  568. /* Follow a symlink */
  569. unsigned long link_len;
  570. size_t len;
  571. char *contents, *contents_start;
  572. struct dir_state *parent;
  573. enum object_type type;
  574. if (follows_remaining-- == 0) {
  575. /* Too many symlinks followed */
  576. retval = SYMLINK_LOOP;
  577. goto done;
  578. }
  579. /*
  580. * At this point, we have followed at a least
  581. * one symlink, so on error we need to report this.
  582. */
  583. retval = DANGLING_SYMLINK;
  584. contents = read_sha1_file(current_tree_sha1, &type,
  585. &link_len);
  586. if (!contents)
  587. goto done;
  588. if (contents[0] == '/') {
  589. strbuf_addstr(result_path, contents);
  590. free(contents);
  591. *mode = 0;
  592. retval = FOUND;
  593. goto done;
  594. }
  595. if (remainder)
  596. len = first_slash - namebuf.buf;
  597. else
  598. len = namebuf.len;
  599. contents_start = contents;
  600. parent = &parents[parents_nr - 1];
  601. init_tree_desc(&t, parent->tree, parent->size);
  602. strbuf_splice(&namebuf, 0, len,
  603. contents_start, link_len);
  604. if (remainder)
  605. namebuf.buf[link_len] = '/';
  606. free(contents);
  607. }
  608. }
  609. done:
  610. for (i = 0; i < parents_nr; i++)
  611. free(parents[i].tree);
  612. free(parents);
  613. strbuf_release(&namebuf);
  614. return retval;
  615. }
  616. static int match_entry(const struct pathspec_item *item,
  617. const struct name_entry *entry, int pathlen,
  618. const char *match, int matchlen,
  619. enum interesting *never_interesting)
  620. {
  621. int m = -1; /* signals that we haven't called strncmp() */
  622. if (item->magic & PATHSPEC_ICASE)
  623. /*
  624. * "Never interesting" trick requires exact
  625. * matching. We could do something clever with inexact
  626. * matching, but it's trickier (and not to forget that
  627. * strcasecmp is locale-dependent, at least in
  628. * glibc). Just disable it for now. It can't be worse
  629. * than the wildcard's codepath of '[Tt][Hi][Is][Ss]'
  630. * pattern.
  631. */
  632. *never_interesting = entry_not_interesting;
  633. else if (*never_interesting != entry_not_interesting) {
  634. /*
  635. * We have not seen any match that sorts later
  636. * than the current path.
  637. */
  638. /*
  639. * Does match sort strictly earlier than path
  640. * with their common parts?
  641. */
  642. m = strncmp(match, entry->path,
  643. (matchlen < pathlen) ? matchlen : pathlen);
  644. if (m < 0)
  645. return 0;
  646. /*
  647. * If we come here even once, that means there is at
  648. * least one pathspec that would sort equal to or
  649. * later than the path we are currently looking at.
  650. * In other words, if we have never reached this point
  651. * after iterating all pathspecs, it means all
  652. * pathspecs are either outside of base, or inside the
  653. * base but sorts strictly earlier than the current
  654. * one. In either case, they will never match the
  655. * subsequent entries. In such a case, we initialized
  656. * the variable to -1 and that is what will be
  657. * returned, allowing the caller to terminate early.
  658. */
  659. *never_interesting = entry_not_interesting;
  660. }
  661. if (pathlen > matchlen)
  662. return 0;
  663. if (matchlen > pathlen) {
  664. if (match[pathlen] != '/')
  665. return 0;
  666. if (!S_ISDIR(entry->mode) && !S_ISGITLINK(entry->mode))
  667. return 0;
  668. }
  669. if (m == -1)
  670. /*
  671. * we cheated and did not do strncmp(), so we do
  672. * that here.
  673. */
  674. m = ps_strncmp(item, match, entry->path, pathlen);
  675. /*
  676. * If common part matched earlier then it is a hit,
  677. * because we rejected the case where path is not a
  678. * leading directory and is shorter than match.
  679. */
  680. if (!m)
  681. /*
  682. * match_entry does not check if the prefix part is
  683. * matched case-sensitively. If the entry is a
  684. * directory and part of prefix, it'll be rematched
  685. * eventually by basecmp with special treatment for
  686. * the prefix.
  687. */
  688. return 1;
  689. return 0;
  690. }
  691. /* :(icase)-aware string compare */
  692. static int basecmp(const struct pathspec_item *item,
  693. const char *base, const char *match, int len)
  694. {
  695. if (item->magic & PATHSPEC_ICASE) {
  696. int ret, n = len > item->prefix ? item->prefix : len;
  697. ret = strncmp(base, match, n);
  698. if (ret)
  699. return ret;
  700. base += n;
  701. match += n;
  702. len -= n;
  703. }
  704. return ps_strncmp(item, base, match, len);
  705. }
  706. static int match_dir_prefix(const struct pathspec_item *item,
  707. const char *base,
  708. const char *match, int matchlen)
  709. {
  710. if (basecmp(item, base, match, matchlen))
  711. return 0;
  712. /*
  713. * If the base is a subdirectory of a path which
  714. * was specified, all of them are interesting.
  715. */
  716. if (!matchlen ||
  717. base[matchlen] == '/' ||
  718. match[matchlen - 1] == '/')
  719. return 1;
  720. /* Just a random prefix match */
  721. return 0;
  722. }
  723. /*
  724. * Perform matching on the leading non-wildcard part of
  725. * pathspec. item->nowildcard_len must be greater than zero. Return
  726. * non-zero if base is matched.
  727. */
  728. static int match_wildcard_base(const struct pathspec_item *item,
  729. const char *base, int baselen,
  730. int *matched)
  731. {
  732. const char *match = item->match;
  733. /* the wildcard part is not considered in this function */
  734. int matchlen = item->nowildcard_len;
  735. if (baselen) {
  736. int dirlen;
  737. /*
  738. * Return early if base is longer than the
  739. * non-wildcard part but it does not match.
  740. */
  741. if (baselen >= matchlen) {
  742. *matched = matchlen;
  743. return !basecmp(item, base, match, matchlen);
  744. }
  745. dirlen = matchlen;
  746. while (dirlen && match[dirlen - 1] != '/')
  747. dirlen--;
  748. /*
  749. * Return early if base is shorter than the
  750. * non-wildcard part but it does not match. Note that
  751. * base ends with '/' so we are sure it really matches
  752. * directory
  753. */
  754. if (basecmp(item, base, match, baselen))
  755. return 0;
  756. *matched = baselen;
  757. } else
  758. *matched = 0;
  759. /*
  760. * we could have checked entry against the non-wildcard part
  761. * that is not in base and does similar never_interesting
  762. * optimization as in match_entry. For now just be happy with
  763. * base comparison.
  764. */
  765. return entry_interesting;
  766. }
  767. /*
  768. * Is a tree entry interesting given the pathspec we have?
  769. *
  770. * Pre-condition: either baselen == base_offset (i.e. empty path)
  771. * or base[baselen-1] == '/' (i.e. with trailing slash).
  772. */
  773. static enum interesting do_match(const struct name_entry *entry,
  774. struct strbuf *base, int base_offset,
  775. const struct pathspec *ps,
  776. int exclude)
  777. {
  778. int i;
  779. int pathlen, baselen = base->len - base_offset;
  780. enum interesting never_interesting = ps->has_wildcard ?
  781. entry_not_interesting : all_entries_not_interesting;
  782. GUARD_PATHSPEC(ps,
  783. PATHSPEC_FROMTOP |
  784. PATHSPEC_MAXDEPTH |
  785. PATHSPEC_LITERAL |
  786. PATHSPEC_GLOB |
  787. PATHSPEC_ICASE |
  788. PATHSPEC_EXCLUDE);
  789. if (!ps->nr) {
  790. if (!ps->recursive ||
  791. !(ps->magic & PATHSPEC_MAXDEPTH) ||
  792. ps->max_depth == -1)
  793. return all_entries_interesting;
  794. return within_depth(base->buf + base_offset, baselen,
  795. !!S_ISDIR(entry->mode),
  796. ps->max_depth) ?
  797. entry_interesting : entry_not_interesting;
  798. }
  799. pathlen = tree_entry_len(entry);
  800. for (i = ps->nr - 1; i >= 0; i--) {
  801. const struct pathspec_item *item = ps->items+i;
  802. const char *match = item->match;
  803. const char *base_str = base->buf + base_offset;
  804. int matchlen = item->len, matched = 0;
  805. if ((!exclude && item->magic & PATHSPEC_EXCLUDE) ||
  806. ( exclude && !(item->magic & PATHSPEC_EXCLUDE)))
  807. continue;
  808. if (baselen >= matchlen) {
  809. /* If it doesn't match, move along... */
  810. if (!match_dir_prefix(item, base_str, match, matchlen))
  811. goto match_wildcards;
  812. if (!ps->recursive ||
  813. !(ps->magic & PATHSPEC_MAXDEPTH) ||
  814. ps->max_depth == -1)
  815. return all_entries_interesting;
  816. return within_depth(base_str + matchlen + 1,
  817. baselen - matchlen - 1,
  818. !!S_ISDIR(entry->mode),
  819. ps->max_depth) ?
  820. entry_interesting : entry_not_interesting;
  821. }
  822. /* Either there must be no base, or the base must match. */
  823. if (baselen == 0 || !basecmp(item, base_str, match, baselen)) {
  824. if (match_entry(item, entry, pathlen,
  825. match + baselen, matchlen - baselen,
  826. &never_interesting))
  827. return entry_interesting;
  828. if (item->nowildcard_len < item->len) {
  829. if (!git_fnmatch(item, match + baselen, entry->path,
  830. item->nowildcard_len - baselen))
  831. return entry_interesting;
  832. /*
  833. * Match all directories. We'll try to
  834. * match files later on.
  835. */
  836. if (ps->recursive && S_ISDIR(entry->mode))
  837. return entry_interesting;
  838. }
  839. continue;
  840. }
  841. match_wildcards:
  842. if (item->nowildcard_len == item->len)
  843. continue;
  844. if (item->nowildcard_len &&
  845. !match_wildcard_base(item, base_str, baselen, &matched))
  846. continue;
  847. /*
  848. * Concatenate base and entry->path into one and do
  849. * fnmatch() on it.
  850. *
  851. * While we could avoid concatenation in certain cases
  852. * [1], which saves a memcpy and potentially a
  853. * realloc, it turns out not worth it. Measurement on
  854. * linux-2.6 does not show any clear improvements,
  855. * partly because of the nowildcard_len optimization
  856. * in git_fnmatch(). Avoid micro-optimizations here.
  857. *
  858. * [1] if match_wildcard_base() says the base
  859. * directory is already matched, we only need to match
  860. * the rest, which is shorter so _in theory_ faster.
  861. */
  862. strbuf_add(base, entry->path, pathlen);
  863. if (!git_fnmatch(item, match, base->buf + base_offset,
  864. item->nowildcard_len)) {
  865. strbuf_setlen(base, base_offset + baselen);
  866. return entry_interesting;
  867. }
  868. strbuf_setlen(base, base_offset + baselen);
  869. /*
  870. * Match all directories. We'll try to match files
  871. * later on.
  872. * max_depth is ignored but we may consider support it
  873. * in future, see
  874. * http://thread.gmane.org/gmane.comp.version-control.git/163757/focus=163840
  875. */
  876. if (ps->recursive && S_ISDIR(entry->mode))
  877. return entry_interesting;
  878. }
  879. return never_interesting; /* No matches */
  880. }
  881. /*
  882. * Is a tree entry interesting given the pathspec we have?
  883. *
  884. * Pre-condition: either baselen == base_offset (i.e. empty path)
  885. * or base[baselen-1] == '/' (i.e. with trailing slash).
  886. */
  887. enum interesting tree_entry_interesting(const struct name_entry *entry,
  888. struct strbuf *base, int base_offset,
  889. const struct pathspec *ps)
  890. {
  891. enum interesting positive, negative;
  892. positive = do_match(entry, base, base_offset, ps, 0);
  893. /*
  894. * case | entry | positive | negative | result
  895. * -----+-------+----------+----------+-------
  896. * 1 | file | -1 | -1..2 | -1
  897. * 2 | file | 0 | -1..2 | 0
  898. * 3 | file | 1 | -1 | 1
  899. * 4 | file | 1 | 0 | 1
  900. * 5 | file | 1 | 1 | 0
  901. * 6 | file | 1 | 2 | 0
  902. * 7 | file | 2 | -1 | 2
  903. * 8 | file | 2 | 0 | 2
  904. * 9 | file | 2 | 1 | 0
  905. * 10 | file | 2 | 2 | -1
  906. * -----+-------+----------+----------+-------
  907. * 11 | dir | -1 | -1..2 | -1
  908. * 12 | dir | 0 | -1..2 | 0
  909. * 13 | dir | 1 | -1 | 1
  910. * 14 | dir | 1 | 0 | 1
  911. * 15 | dir | 1 | 1 | 1 (*)
  912. * 16 | dir | 1 | 2 | 0
  913. * 17 | dir | 2 | -1 | 2
  914. * 18 | dir | 2 | 0 | 2
  915. * 19 | dir | 2 | 1 | 1 (*)
  916. * 20 | dir | 2 | 2 | -1
  917. *
  918. * (*) An exclude pattern interested in a directory does not
  919. * necessarily mean it will exclude all of the directory. In
  920. * wildcard case, it can't decide until looking at individual
  921. * files inside. So don't write such directories off yet.
  922. */
  923. if (!(ps->magic & PATHSPEC_EXCLUDE) ||
  924. positive <= entry_not_interesting) /* #1, #2, #11, #12 */
  925. return positive;
  926. negative = do_match(entry, base, base_offset, ps, 1);
  927. /* #3, #4, #7, #8, #13, #14, #17, #18 */
  928. if (negative <= entry_not_interesting)
  929. return positive;
  930. /* #15, #19 */
  931. if (S_ISDIR(entry->mode) &&
  932. positive >= entry_interesting &&
  933. negative == entry_interesting)
  934. return entry_interesting;
  935. if ((positive == entry_interesting &&
  936. negative >= entry_interesting) || /* #5, #6, #16 */
  937. (positive == all_entries_interesting &&
  938. negative == entry_interesting)) /* #9 */
  939. return entry_not_interesting;
  940. return all_entries_not_interesting; /* #10, #20 */
  941. }