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.

699 lines
17KB

  1. #include "cache.h"
  2. #include "config.h"
  3. #include "dir.h"
  4. #include "pathspec.h"
  5. #include "attr.h"
  6. /*
  7. * Finds which of the given pathspecs match items in the index.
  8. *
  9. * For each pathspec, sets the corresponding entry in the seen[] array
  10. * (which should be specs items long, i.e. the same size as pathspec)
  11. * to the nature of the "closest" (i.e. most specific) match found for
  12. * that pathspec in the index, if it was a closer type of match than
  13. * the existing entry. As an optimization, matching is skipped
  14. * altogether if seen[] already only contains non-zero entries.
  15. *
  16. * If seen[] has not already been written to, it may make sense
  17. * to use find_pathspecs_matching_against_index() instead.
  18. */
  19. void add_pathspec_matches_against_index(const struct pathspec *pathspec,
  20. const struct index_state *istate,
  21. char *seen)
  22. {
  23. int num_unmatched = 0, i;
  24. /*
  25. * Since we are walking the index as if we were walking the directory,
  26. * we have to mark the matched pathspec as seen; otherwise we will
  27. * mistakenly think that the user gave a pathspec that did not match
  28. * anything.
  29. */
  30. for (i = 0; i < pathspec->nr; i++)
  31. if (!seen[i])
  32. num_unmatched++;
  33. if (!num_unmatched)
  34. return;
  35. for (i = 0; i < istate->cache_nr; i++) {
  36. const struct cache_entry *ce = istate->cache[i];
  37. ce_path_match(istate, ce, pathspec, seen);
  38. }
  39. }
  40. /*
  41. * Finds which of the given pathspecs match items in the index.
  42. *
  43. * This is a one-shot wrapper around add_pathspec_matches_against_index()
  44. * which allocates, populates, and returns a seen[] array indicating the
  45. * nature of the "closest" (i.e. most specific) matches which each of the
  46. * given pathspecs achieves against all items in the index.
  47. */
  48. char *find_pathspecs_matching_against_index(const struct pathspec *pathspec,
  49. const struct index_state *istate)
  50. {
  51. char *seen = xcalloc(pathspec->nr, 1);
  52. add_pathspec_matches_against_index(pathspec, istate, seen);
  53. return seen;
  54. }
  55. /*
  56. * Magic pathspec
  57. *
  58. * Possible future magic semantics include stuff like:
  59. *
  60. * { PATHSPEC_RECURSIVE, '*', "recursive" },
  61. * { PATHSPEC_REGEXP, '\0', "regexp" },
  62. *
  63. */
  64. static struct pathspec_magic {
  65. unsigned bit;
  66. char mnemonic; /* this cannot be ':'! */
  67. const char *name;
  68. } pathspec_magic[] = {
  69. { PATHSPEC_FROMTOP, '/', "top" },
  70. { PATHSPEC_LITERAL, '\0', "literal" },
  71. { PATHSPEC_GLOB, '\0', "glob" },
  72. { PATHSPEC_ICASE, '\0', "icase" },
  73. { PATHSPEC_EXCLUDE, '!', "exclude" },
  74. { PATHSPEC_ATTR, '\0', "attr" },
  75. };
  76. static void prefix_magic(struct strbuf *sb, int prefixlen, unsigned magic)
  77. {
  78. int i;
  79. strbuf_addstr(sb, ":(");
  80. for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++)
  81. if (magic & pathspec_magic[i].bit) {
  82. if (sb->buf[sb->len - 1] != '(')
  83. strbuf_addch(sb, ',');
  84. strbuf_addstr(sb, pathspec_magic[i].name);
  85. }
  86. strbuf_addf(sb, ",prefix:%d)", prefixlen);
  87. }
  88. static size_t strcspn_escaped(const char *s, const char *stop)
  89. {
  90. const char *i;
  91. for (i = s; *i; i++) {
  92. /* skip the escaped character */
  93. if (i[0] == '\\' && i[1]) {
  94. i++;
  95. continue;
  96. }
  97. if (strchr(stop, *i))
  98. break;
  99. }
  100. return i - s;
  101. }
  102. static inline int invalid_value_char(const char ch)
  103. {
  104. if (isalnum(ch) || strchr(",-_", ch))
  105. return 0;
  106. return -1;
  107. }
  108. static char *attr_value_unescape(const char *value)
  109. {
  110. const char *src;
  111. char *dst, *ret;
  112. ret = xmallocz(strlen(value));
  113. for (src = value, dst = ret; *src; src++, dst++) {
  114. if (*src == '\\') {
  115. if (!src[1])
  116. die(_("Escape character '\\' not allowed as "
  117. "last character in attr value"));
  118. src++;
  119. }
  120. if (invalid_value_char(*src))
  121. die("cannot use '%c' for value matching", *src);
  122. *dst = *src;
  123. }
  124. *dst = '\0';
  125. return ret;
  126. }
  127. static void parse_pathspec_attr_match(struct pathspec_item *item, const char *value)
  128. {
  129. struct string_list_item *si;
  130. struct string_list list = STRING_LIST_INIT_DUP;
  131. if (item->attr_check || item->attr_match)
  132. die(_("Only one 'attr:' specification is allowed."));
  133. if (!value || !*value)
  134. die(_("attr spec must not be empty"));
  135. string_list_split(&list, value, ' ', -1);
  136. string_list_remove_empty_items(&list, 0);
  137. item->attr_check = attr_check_alloc();
  138. item->attr_match = xcalloc(list.nr, sizeof(struct attr_match));
  139. for_each_string_list_item(si, &list) {
  140. size_t attr_len;
  141. char *attr_name;
  142. const struct git_attr *a;
  143. int j = item->attr_match_nr++;
  144. const char *attr = si->string;
  145. struct attr_match *am = &item->attr_match[j];
  146. switch (*attr) {
  147. case '!':
  148. am->match_mode = MATCH_UNSPECIFIED;
  149. attr++;
  150. attr_len = strlen(attr);
  151. break;
  152. case '-':
  153. am->match_mode = MATCH_UNSET;
  154. attr++;
  155. attr_len = strlen(attr);
  156. break;
  157. default:
  158. attr_len = strcspn(attr, "=");
  159. if (attr[attr_len] != '=')
  160. am->match_mode = MATCH_SET;
  161. else {
  162. const char *v = &attr[attr_len + 1];
  163. am->match_mode = MATCH_VALUE;
  164. am->value = attr_value_unescape(v);
  165. }
  166. break;
  167. }
  168. attr_name = xmemdupz(attr, attr_len);
  169. a = git_attr(attr_name);
  170. if (!a)
  171. die(_("invalid attribute name %s"), attr_name);
  172. attr_check_append(item->attr_check, a);
  173. free(attr_name);
  174. }
  175. if (item->attr_check->nr != item->attr_match_nr)
  176. BUG("should have same number of entries");
  177. string_list_clear(&list, 0);
  178. }
  179. static inline int get_literal_global(void)
  180. {
  181. static int literal = -1;
  182. if (literal < 0)
  183. literal = git_env_bool(GIT_LITERAL_PATHSPECS_ENVIRONMENT, 0);
  184. return literal;
  185. }
  186. static inline int get_glob_global(void)
  187. {
  188. static int glob = -1;
  189. if (glob < 0)
  190. glob = git_env_bool(GIT_GLOB_PATHSPECS_ENVIRONMENT, 0);
  191. return glob;
  192. }
  193. static inline int get_noglob_global(void)
  194. {
  195. static int noglob = -1;
  196. if (noglob < 0)
  197. noglob = git_env_bool(GIT_NOGLOB_PATHSPECS_ENVIRONMENT, 0);
  198. return noglob;
  199. }
  200. static inline int get_icase_global(void)
  201. {
  202. static int icase = -1;
  203. if (icase < 0)
  204. icase = git_env_bool(GIT_ICASE_PATHSPECS_ENVIRONMENT, 0);
  205. return icase;
  206. }
  207. static int get_global_magic(int element_magic)
  208. {
  209. int global_magic = 0;
  210. if (get_literal_global())
  211. global_magic |= PATHSPEC_LITERAL;
  212. /* --glob-pathspec is overridden by :(literal) */
  213. if (get_glob_global() && !(element_magic & PATHSPEC_LITERAL))
  214. global_magic |= PATHSPEC_GLOB;
  215. if (get_glob_global() && get_noglob_global())
  216. die(_("global 'glob' and 'noglob' pathspec settings are incompatible"));
  217. if (get_icase_global())
  218. global_magic |= PATHSPEC_ICASE;
  219. if ((global_magic & PATHSPEC_LITERAL) &&
  220. (global_magic & ~PATHSPEC_LITERAL))
  221. die(_("global 'literal' pathspec setting is incompatible "
  222. "with all other global pathspec settings"));
  223. /* --noglob-pathspec adds :(literal) _unless_ :(glob) is specified */
  224. if (get_noglob_global() && !(element_magic & PATHSPEC_GLOB))
  225. global_magic |= PATHSPEC_LITERAL;
  226. return global_magic;
  227. }
  228. /*
  229. * Parse the pathspec element looking for long magic
  230. *
  231. * saves all magic in 'magic'
  232. * if prefix magic is used, save the prefix length in 'prefix_len'
  233. * returns the position in 'elem' after all magic has been parsed
  234. */
  235. static const char *parse_long_magic(unsigned *magic, int *prefix_len,
  236. struct pathspec_item *item,
  237. const char *elem)
  238. {
  239. const char *pos;
  240. const char *nextat;
  241. for (pos = elem + 2; *pos && *pos != ')'; pos = nextat) {
  242. size_t len = strcspn_escaped(pos, ",)");
  243. int i;
  244. if (pos[len] == ',')
  245. nextat = pos + len + 1; /* handle ',' */
  246. else
  247. nextat = pos + len; /* handle ')' and '\0' */
  248. if (!len)
  249. continue;
  250. if (starts_with(pos, "prefix:")) {
  251. char *endptr;
  252. *prefix_len = strtol(pos + 7, &endptr, 10);
  253. if (endptr - pos != len)
  254. die(_("invalid parameter for pathspec magic 'prefix'"));
  255. continue;
  256. }
  257. if (starts_with(pos, "attr:")) {
  258. char *attr_body = xmemdupz(pos + 5, len - 5);
  259. parse_pathspec_attr_match(item, attr_body);
  260. *magic |= PATHSPEC_ATTR;
  261. free(attr_body);
  262. continue;
  263. }
  264. for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
  265. if (strlen(pathspec_magic[i].name) == len &&
  266. !strncmp(pathspec_magic[i].name, pos, len)) {
  267. *magic |= pathspec_magic[i].bit;
  268. break;
  269. }
  270. }
  271. if (ARRAY_SIZE(pathspec_magic) <= i)
  272. die(_("Invalid pathspec magic '%.*s' in '%s'"),
  273. (int) len, pos, elem);
  274. }
  275. if (*pos != ')')
  276. die(_("Missing ')' at the end of pathspec magic in '%s'"),
  277. elem);
  278. pos++;
  279. return pos;
  280. }
  281. /*
  282. * Parse the pathspec element looking for short magic
  283. *
  284. * saves all magic in 'magic'
  285. * returns the position in 'elem' after all magic has been parsed
  286. */
  287. static const char *parse_short_magic(unsigned *magic, const char *elem)
  288. {
  289. const char *pos;
  290. for (pos = elem + 1; *pos && *pos != ':'; pos++) {
  291. char ch = *pos;
  292. int i;
  293. /* Special case alias for '!' */
  294. if (ch == '^') {
  295. *magic |= PATHSPEC_EXCLUDE;
  296. continue;
  297. }
  298. if (!is_pathspec_magic(ch))
  299. break;
  300. for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
  301. if (pathspec_magic[i].mnemonic == ch) {
  302. *magic |= pathspec_magic[i].bit;
  303. break;
  304. }
  305. }
  306. if (ARRAY_SIZE(pathspec_magic) <= i)
  307. die(_("Unimplemented pathspec magic '%c' in '%s'"),
  308. ch, elem);
  309. }
  310. if (*pos == ':')
  311. pos++;
  312. return pos;
  313. }
  314. static const char *parse_element_magic(unsigned *magic, int *prefix_len,
  315. struct pathspec_item *item,
  316. const char *elem)
  317. {
  318. if (elem[0] != ':' || get_literal_global())
  319. return elem; /* nothing to do */
  320. else if (elem[1] == '(')
  321. /* longhand */
  322. return parse_long_magic(magic, prefix_len, item, elem);
  323. else
  324. /* shorthand */
  325. return parse_short_magic(magic, elem);
  326. }
  327. /*
  328. * Perform the initialization of a pathspec_item based on a pathspec element.
  329. */
  330. static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
  331. const char *prefix, int prefixlen,
  332. const char *elt)
  333. {
  334. unsigned magic = 0, element_magic = 0;
  335. const char *copyfrom = elt;
  336. char *match;
  337. int pathspec_prefix = -1;
  338. item->attr_check = NULL;
  339. item->attr_match = NULL;
  340. item->attr_match_nr = 0;
  341. /* PATHSPEC_LITERAL_PATH ignores magic */
  342. if (flags & PATHSPEC_LITERAL_PATH) {
  343. magic = PATHSPEC_LITERAL;
  344. } else {
  345. copyfrom = parse_element_magic(&element_magic,
  346. &pathspec_prefix,
  347. item,
  348. elt);
  349. magic |= element_magic;
  350. magic |= get_global_magic(element_magic);
  351. }
  352. item->magic = magic;
  353. if (pathspec_prefix >= 0 &&
  354. (prefixlen || (prefix && *prefix)))
  355. BUG("'prefix' magic is supposed to be used at worktree's root");
  356. if ((magic & PATHSPEC_LITERAL) && (magic & PATHSPEC_GLOB))
  357. die(_("%s: 'literal' and 'glob' are incompatible"), elt);
  358. /* Create match string which will be used for pathspec matching */
  359. if (pathspec_prefix >= 0) {
  360. match = xstrdup(copyfrom);
  361. prefixlen = pathspec_prefix;
  362. } else if (magic & PATHSPEC_FROMTOP) {
  363. match = xstrdup(copyfrom);
  364. prefixlen = 0;
  365. } else {
  366. match = prefix_path_gently(prefix, prefixlen,
  367. &prefixlen, copyfrom);
  368. if (!match)
  369. die(_("%s: '%s' is outside repository"), elt, copyfrom);
  370. }
  371. item->match = match;
  372. item->len = strlen(item->match);
  373. item->prefix = prefixlen;
  374. /*
  375. * Prefix the pathspec (keep all magic) and assign to
  376. * original. Useful for passing to another command.
  377. */
  378. if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
  379. !get_literal_global()) {
  380. struct strbuf sb = STRBUF_INIT;
  381. /* Preserve the actual prefix length of each pattern */
  382. prefix_magic(&sb, prefixlen, element_magic);
  383. strbuf_addstr(&sb, match);
  384. item->original = strbuf_detach(&sb, NULL);
  385. } else {
  386. item->original = xstrdup(elt);
  387. }
  388. if (magic & PATHSPEC_LITERAL) {
  389. item->nowildcard_len = item->len;
  390. } else {
  391. item->nowildcard_len = simple_length(item->match);
  392. if (item->nowildcard_len < prefixlen)
  393. item->nowildcard_len = prefixlen;
  394. }
  395. item->flags = 0;
  396. if (magic & PATHSPEC_GLOB) {
  397. /*
  398. * FIXME: should we enable ONESTAR in _GLOB for
  399. * pattern "* * / * . c"?
  400. */
  401. } else {
  402. if (item->nowildcard_len < item->len &&
  403. item->match[item->nowildcard_len] == '*' &&
  404. no_wildcard(item->match + item->nowildcard_len + 1))
  405. item->flags |= PATHSPEC_ONESTAR;
  406. }
  407. /* sanity checks, pathspec matchers assume these are sane */
  408. if (item->nowildcard_len > item->len ||
  409. item->prefix > item->len) {
  410. BUG("error initializing pathspec_item");
  411. }
  412. }
  413. static int pathspec_item_cmp(const void *a_, const void *b_)
  414. {
  415. struct pathspec_item *a, *b;
  416. a = (struct pathspec_item *)a_;
  417. b = (struct pathspec_item *)b_;
  418. return strcmp(a->match, b->match);
  419. }
  420. static void NORETURN unsupported_magic(const char *pattern,
  421. unsigned magic)
  422. {
  423. struct strbuf sb = STRBUF_INIT;
  424. int i;
  425. for (i = 0; i < ARRAY_SIZE(pathspec_magic); i++) {
  426. const struct pathspec_magic *m = pathspec_magic + i;
  427. if (!(magic & m->bit))
  428. continue;
  429. if (sb.len)
  430. strbuf_addstr(&sb, ", ");
  431. if (m->mnemonic)
  432. strbuf_addf(&sb, _("'%s' (mnemonic: '%c')"),
  433. m->name, m->mnemonic);
  434. else
  435. strbuf_addf(&sb, "'%s'", m->name);
  436. }
  437. /*
  438. * We may want to substitute "this command" with a command
  439. * name. E.g. when add--interactive dies when running
  440. * "checkout -p"
  441. */
  442. die(_("%s: pathspec magic not supported by this command: %s"),
  443. pattern, sb.buf);
  444. }
  445. void parse_pathspec(struct pathspec *pathspec,
  446. unsigned magic_mask, unsigned flags,
  447. const char *prefix, const char **argv)
  448. {
  449. struct pathspec_item *item;
  450. const char *entry = argv ? *argv : NULL;
  451. int i, n, prefixlen, nr_exclude = 0;
  452. memset(pathspec, 0, sizeof(*pathspec));
  453. if (flags & PATHSPEC_MAXDEPTH_VALID)
  454. pathspec->magic |= PATHSPEC_MAXDEPTH;
  455. /* No arguments, no prefix -> no pathspec */
  456. if (!entry && !prefix)
  457. return;
  458. if ((flags & PATHSPEC_PREFER_CWD) &&
  459. (flags & PATHSPEC_PREFER_FULL))
  460. BUG("PATHSPEC_PREFER_CWD and PATHSPEC_PREFER_FULL are incompatible");
  461. /* No arguments with prefix -> prefix pathspec */
  462. if (!entry) {
  463. if (flags & PATHSPEC_PREFER_FULL)
  464. return;
  465. if (!(flags & PATHSPEC_PREFER_CWD))
  466. BUG("PATHSPEC_PREFER_CWD requires arguments");
  467. pathspec->items = item = xcalloc(1, sizeof(*item));
  468. item->match = xstrdup(prefix);
  469. item->original = xstrdup(prefix);
  470. item->nowildcard_len = item->len = strlen(prefix);
  471. item->prefix = item->len;
  472. pathspec->nr = 1;
  473. return;
  474. }
  475. n = 0;
  476. while (argv[n]) {
  477. if (*argv[n] == '\0')
  478. die("empty string is not a valid pathspec. "
  479. "please use . instead if you meant to match all paths");
  480. n++;
  481. }
  482. pathspec->nr = n;
  483. ALLOC_ARRAY(pathspec->items, n + 1);
  484. item = pathspec->items;
  485. prefixlen = prefix ? strlen(prefix) : 0;
  486. for (i = 0; i < n; i++) {
  487. entry = argv[i];
  488. init_pathspec_item(item + i, flags, prefix, prefixlen, entry);
  489. if (item[i].magic & PATHSPEC_EXCLUDE)
  490. nr_exclude++;
  491. if (item[i].magic & magic_mask)
  492. unsupported_magic(entry, item[i].magic & magic_mask);
  493. if ((flags & PATHSPEC_SYMLINK_LEADING_PATH) &&
  494. has_symlink_leading_path(item[i].match, item[i].len)) {
  495. die(_("pathspec '%s' is beyond a symbolic link"), entry);
  496. }
  497. if (item[i].nowildcard_len < item[i].len)
  498. pathspec->has_wildcard = 1;
  499. pathspec->magic |= item[i].magic;
  500. }
  501. /*
  502. * If everything is an exclude pattern, add one positive pattern
  503. * that matches everything. We allocated an extra one for this.
  504. */
  505. if (nr_exclude == n) {
  506. int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
  507. init_pathspec_item(item + n, 0, prefix, plen, "");
  508. pathspec->nr++;
  509. }
  510. if (pathspec->magic & PATHSPEC_MAXDEPTH) {
  511. if (flags & PATHSPEC_KEEP_ORDER)
  512. BUG("PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
  513. QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
  514. }
  515. }
  516. void copy_pathspec(struct pathspec *dst, const struct pathspec *src)
  517. {
  518. int i, j;
  519. *dst = *src;
  520. ALLOC_ARRAY(dst->items, dst->nr);
  521. COPY_ARRAY(dst->items, src->items, dst->nr);
  522. for (i = 0; i < dst->nr; i++) {
  523. struct pathspec_item *d = &dst->items[i];
  524. struct pathspec_item *s = &src->items[i];
  525. d->match = xstrdup(s->match);
  526. d->original = xstrdup(s->original);
  527. ALLOC_ARRAY(d->attr_match, d->attr_match_nr);
  528. COPY_ARRAY(d->attr_match, s->attr_match, d->attr_match_nr);
  529. for (j = 0; j < d->attr_match_nr; j++) {
  530. const char *value = s->attr_match[j].value;
  531. d->attr_match[j].value = xstrdup_or_null(value);
  532. }
  533. d->attr_check = attr_check_dup(s->attr_check);
  534. }
  535. }
  536. void clear_pathspec(struct pathspec *pathspec)
  537. {
  538. int i, j;
  539. for (i = 0; i < pathspec->nr; i++) {
  540. free(pathspec->items[i].match);
  541. free(pathspec->items[i].original);
  542. for (j = 0; j < pathspec->items[i].attr_match_nr; j++)
  543. free(pathspec->items[i].attr_match[j].value);
  544. free(pathspec->items[i].attr_match);
  545. if (pathspec->items[i].attr_check)
  546. attr_check_free(pathspec->items[i].attr_check);
  547. }
  548. FREE_AND_NULL(pathspec->items);
  549. pathspec->nr = 0;
  550. }
  551. int match_pathspec_attrs(const struct index_state *istate,
  552. const char *name, int namelen,
  553. const struct pathspec_item *item)
  554. {
  555. int i;
  556. char *to_free = NULL;
  557. if (name[namelen])
  558. name = to_free = xmemdupz(name, namelen);
  559. git_check_attr(istate, name, item->attr_check);
  560. free(to_free);
  561. for (i = 0; i < item->attr_match_nr; i++) {
  562. const char *value;
  563. int matched;
  564. enum attr_match_mode match_mode;
  565. value = item->attr_check->items[i].value;
  566. match_mode = item->attr_match[i].match_mode;
  567. if (ATTR_TRUE(value))
  568. matched = (match_mode == MATCH_SET);
  569. else if (ATTR_FALSE(value))
  570. matched = (match_mode == MATCH_UNSET);
  571. else if (ATTR_UNSET(value))
  572. matched = (match_mode == MATCH_UNSPECIFIED);
  573. else
  574. matched = (match_mode == MATCH_VALUE &&
  575. !strcmp(item->attr_match[i].value, value));
  576. if (!matched)
  577. return 0;
  578. }
  579. return 1;
  580. }