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.

1162 lines
28KB

  1. /*
  2. * Handle git attributes. See gitattributes(5) for a description of
  3. * the file syntax, and Documentation/technical/api-gitattributes.txt
  4. * for a description of the API.
  5. *
  6. * One basic design decision here is that we are not going to support
  7. * an insanely large number of attributes.
  8. */
  9. #include "cache.h"
  10. #include "config.h"
  11. #include "exec-cmd.h"
  12. #include "attr.h"
  13. #include "dir.h"
  14. #include "utf8.h"
  15. #include "quote.h"
  16. #include "thread-utils.h"
  17. const char git_attr__true[] = "(builtin)true";
  18. const char git_attr__false[] = "\0(builtin)false";
  19. static const char git_attr__unknown[] = "(builtin)unknown";
  20. #define ATTR__TRUE git_attr__true
  21. #define ATTR__FALSE git_attr__false
  22. #define ATTR__UNSET NULL
  23. #define ATTR__UNKNOWN git_attr__unknown
  24. #ifndef DEBUG_ATTR
  25. #define DEBUG_ATTR 0
  26. #endif
  27. struct git_attr {
  28. int attr_nr; /* unique attribute number */
  29. char name[FLEX_ARRAY]; /* attribute name */
  30. };
  31. const char *git_attr_name(const struct git_attr *attr)
  32. {
  33. return attr->name;
  34. }
  35. struct attr_hashmap {
  36. struct hashmap map;
  37. pthread_mutex_t mutex;
  38. };
  39. static inline void hashmap_lock(struct attr_hashmap *map)
  40. {
  41. pthread_mutex_lock(&map->mutex);
  42. }
  43. static inline void hashmap_unlock(struct attr_hashmap *map)
  44. {
  45. pthread_mutex_unlock(&map->mutex);
  46. }
  47. /*
  48. * The global dictionary of all interned attributes. This
  49. * is a singleton object which is shared between threads.
  50. * Access to this dictionary must be surrounded with a mutex.
  51. */
  52. static struct attr_hashmap g_attr_hashmap;
  53. /* The container for objects stored in "struct attr_hashmap" */
  54. struct attr_hash_entry {
  55. struct hashmap_entry ent;
  56. const char *key; /* the key; memory should be owned by value */
  57. size_t keylen; /* length of the key */
  58. void *value; /* the stored value */
  59. };
  60. /* attr_hashmap comparison function */
  61. static int attr_hash_entry_cmp(const void *unused_cmp_data,
  62. const struct hashmap_entry *eptr,
  63. const struct hashmap_entry *entry_or_key,
  64. const void *unused_keydata)
  65. {
  66. const struct attr_hash_entry *a, *b;
  67. a = container_of(eptr, const struct attr_hash_entry, ent);
  68. b = container_of(entry_or_key, const struct attr_hash_entry, ent);
  69. return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
  70. }
  71. /* Initialize an 'attr_hashmap' object */
  72. static void attr_hashmap_init(struct attr_hashmap *map)
  73. {
  74. hashmap_init(&map->map, attr_hash_entry_cmp, NULL, 0);
  75. }
  76. /*
  77. * Retrieve the 'value' stored in a hashmap given the provided 'key'.
  78. * If there is no matching entry, return NULL.
  79. */
  80. static void *attr_hashmap_get(struct attr_hashmap *map,
  81. const char *key, size_t keylen)
  82. {
  83. struct attr_hash_entry k;
  84. struct attr_hash_entry *e;
  85. if (!map->map.tablesize)
  86. attr_hashmap_init(map);
  87. hashmap_entry_init(&k.ent, memhash(key, keylen));
  88. k.key = key;
  89. k.keylen = keylen;
  90. e = hashmap_get_entry(&map->map, &k, ent, NULL);
  91. return e ? e->value : NULL;
  92. }
  93. /* Add 'value' to a hashmap based on the provided 'key'. */
  94. static void attr_hashmap_add(struct attr_hashmap *map,
  95. const char *key, size_t keylen,
  96. void *value)
  97. {
  98. struct attr_hash_entry *e;
  99. if (!map->map.tablesize)
  100. attr_hashmap_init(map);
  101. e = xmalloc(sizeof(struct attr_hash_entry));
  102. hashmap_entry_init(&e->ent, memhash(key, keylen));
  103. e->key = key;
  104. e->keylen = keylen;
  105. e->value = value;
  106. hashmap_add(&map->map, &e->ent);
  107. }
  108. struct all_attrs_item {
  109. const struct git_attr *attr;
  110. const char *value;
  111. /*
  112. * If 'macro' is non-NULL, indicates that 'attr' is a macro based on
  113. * the current attribute stack and contains a pointer to the match_attr
  114. * definition of the macro
  115. */
  116. const struct match_attr *macro;
  117. };
  118. /*
  119. * Reallocate and reinitialize the array of all attributes (which is used in
  120. * the attribute collection process) in 'check' based on the global dictionary
  121. * of attributes.
  122. */
  123. static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check)
  124. {
  125. int i;
  126. unsigned int size;
  127. hashmap_lock(map);
  128. size = hashmap_get_size(&map->map);
  129. if (size < check->all_attrs_nr)
  130. BUG("interned attributes shouldn't be deleted");
  131. /*
  132. * If the number of attributes in the global dictionary has increased
  133. * (or this attr_check instance doesn't have an initialized all_attrs
  134. * field), reallocate the provided attr_check instance's all_attrs
  135. * field and fill each entry with its corresponding git_attr.
  136. */
  137. if (size != check->all_attrs_nr) {
  138. struct attr_hash_entry *e;
  139. struct hashmap_iter iter;
  140. REALLOC_ARRAY(check->all_attrs, size);
  141. check->all_attrs_nr = size;
  142. hashmap_for_each_entry(&map->map, &iter, e,
  143. ent /* member name */) {
  144. const struct git_attr *a = e->value;
  145. check->all_attrs[a->attr_nr].attr = a;
  146. }
  147. }
  148. hashmap_unlock(map);
  149. /*
  150. * Re-initialize every entry in check->all_attrs.
  151. * This re-initialization can live outside of the locked region since
  152. * the attribute dictionary is no longer being accessed.
  153. */
  154. for (i = 0; i < check->all_attrs_nr; i++) {
  155. check->all_attrs[i].value = ATTR__UNKNOWN;
  156. check->all_attrs[i].macro = NULL;
  157. }
  158. }
  159. static int attr_name_valid(const char *name, size_t namelen)
  160. {
  161. /*
  162. * Attribute name cannot begin with '-' and must consist of
  163. * characters from [-A-Za-z0-9_.].
  164. */
  165. if (namelen <= 0 || *name == '-')
  166. return 0;
  167. while (namelen--) {
  168. char ch = *name++;
  169. if (! (ch == '-' || ch == '.' || ch == '_' ||
  170. ('0' <= ch && ch <= '9') ||
  171. ('a' <= ch && ch <= 'z') ||
  172. ('A' <= ch && ch <= 'Z')) )
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. static void report_invalid_attr(const char *name, size_t len,
  178. const char *src, int lineno)
  179. {
  180. struct strbuf err = STRBUF_INIT;
  181. strbuf_addf(&err, _("%.*s is not a valid attribute name"),
  182. (int) len, name);
  183. fprintf(stderr, "%s: %s:%d\n", err.buf, src, lineno);
  184. strbuf_release(&err);
  185. }
  186. /*
  187. * Given a 'name', lookup and return the corresponding attribute in the global
  188. * dictionary. If no entry is found, create a new attribute and store it in
  189. * the dictionary.
  190. */
  191. static const struct git_attr *git_attr_internal(const char *name, int namelen)
  192. {
  193. struct git_attr *a;
  194. if (!attr_name_valid(name, namelen))
  195. return NULL;
  196. hashmap_lock(&g_attr_hashmap);
  197. a = attr_hashmap_get(&g_attr_hashmap, name, namelen);
  198. if (!a) {
  199. FLEX_ALLOC_MEM(a, name, name, namelen);
  200. a->attr_nr = hashmap_get_size(&g_attr_hashmap.map);
  201. attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a);
  202. assert(a->attr_nr ==
  203. (hashmap_get_size(&g_attr_hashmap.map) - 1));
  204. }
  205. hashmap_unlock(&g_attr_hashmap);
  206. return a;
  207. }
  208. const struct git_attr *git_attr(const char *name)
  209. {
  210. return git_attr_internal(name, strlen(name));
  211. }
  212. /* What does a matched pattern decide? */
  213. struct attr_state {
  214. const struct git_attr *attr;
  215. const char *setto;
  216. };
  217. struct pattern {
  218. const char *pattern;
  219. int patternlen;
  220. int nowildcardlen;
  221. unsigned flags; /* PATTERN_FLAG_* */
  222. };
  223. /*
  224. * One rule, as from a .gitattributes file.
  225. *
  226. * If is_macro is true, then u.attr is a pointer to the git_attr being
  227. * defined.
  228. *
  229. * If is_macro is false, then u.pat is the filename pattern to which the
  230. * rule applies.
  231. *
  232. * In either case, num_attr is the number of attributes affected by
  233. * this rule, and state is an array listing them. The attributes are
  234. * listed as they appear in the file (macros unexpanded).
  235. */
  236. struct match_attr {
  237. union {
  238. struct pattern pat;
  239. const struct git_attr *attr;
  240. } u;
  241. char is_macro;
  242. unsigned num_attr;
  243. struct attr_state state[FLEX_ARRAY];
  244. };
  245. static const char blank[] = " \t\r\n";
  246. /*
  247. * Parse a whitespace-delimited attribute state (i.e., "attr",
  248. * "-attr", "!attr", or "attr=value") from the string starting at src.
  249. * If e is not NULL, write the results to *e. Return a pointer to the
  250. * remainder of the string (with leading whitespace removed), or NULL
  251. * if there was an error.
  252. */
  253. static const char *parse_attr(const char *src, int lineno, const char *cp,
  254. struct attr_state *e)
  255. {
  256. const char *ep, *equals;
  257. int len;
  258. ep = cp + strcspn(cp, blank);
  259. equals = strchr(cp, '=');
  260. if (equals && ep < equals)
  261. equals = NULL;
  262. if (equals)
  263. len = equals - cp;
  264. else
  265. len = ep - cp;
  266. if (!e) {
  267. if (*cp == '-' || *cp == '!') {
  268. cp++;
  269. len--;
  270. }
  271. if (!attr_name_valid(cp, len)) {
  272. report_invalid_attr(cp, len, src, lineno);
  273. return NULL;
  274. }
  275. } else {
  276. /*
  277. * As this function is always called twice, once with
  278. * e == NULL in the first pass and then e != NULL in
  279. * the second pass, no need for attr_name_valid()
  280. * check here.
  281. */
  282. if (*cp == '-' || *cp == '!') {
  283. e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
  284. cp++;
  285. len--;
  286. }
  287. else if (!equals)
  288. e->setto = ATTR__TRUE;
  289. else {
  290. e->setto = xmemdupz(equals + 1, ep - equals - 1);
  291. }
  292. e->attr = git_attr_internal(cp, len);
  293. }
  294. return ep + strspn(ep, blank);
  295. }
  296. static struct match_attr *parse_attr_line(const char *line, const char *src,
  297. int lineno, int macro_ok)
  298. {
  299. int namelen;
  300. int num_attr, i;
  301. const char *cp, *name, *states;
  302. struct match_attr *res = NULL;
  303. int is_macro;
  304. struct strbuf pattern = STRBUF_INIT;
  305. cp = line + strspn(line, blank);
  306. if (!*cp || *cp == '#')
  307. return NULL;
  308. name = cp;
  309. if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) {
  310. name = pattern.buf;
  311. namelen = pattern.len;
  312. } else {
  313. namelen = strcspn(name, blank);
  314. states = name + namelen;
  315. }
  316. if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
  317. starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
  318. if (!macro_ok) {
  319. fprintf_ln(stderr, _("%s not allowed: %s:%d"),
  320. name, src, lineno);
  321. goto fail_return;
  322. }
  323. is_macro = 1;
  324. name += strlen(ATTRIBUTE_MACRO_PREFIX);
  325. name += strspn(name, blank);
  326. namelen = strcspn(name, blank);
  327. if (!attr_name_valid(name, namelen)) {
  328. report_invalid_attr(name, namelen, src, lineno);
  329. goto fail_return;
  330. }
  331. }
  332. else
  333. is_macro = 0;
  334. states += strspn(states, blank);
  335. /* First pass to count the attr_states */
  336. for (cp = states, num_attr = 0; *cp; num_attr++) {
  337. cp = parse_attr(src, lineno, cp, NULL);
  338. if (!cp)
  339. goto fail_return;
  340. }
  341. res = xcalloc(1,
  342. sizeof(*res) +
  343. sizeof(struct attr_state) * num_attr +
  344. (is_macro ? 0 : namelen + 1));
  345. if (is_macro) {
  346. res->u.attr = git_attr_internal(name, namelen);
  347. } else {
  348. char *p = (char *)&(res->state[num_attr]);
  349. memcpy(p, name, namelen);
  350. res->u.pat.pattern = p;
  351. parse_path_pattern(&res->u.pat.pattern,
  352. &res->u.pat.patternlen,
  353. &res->u.pat.flags,
  354. &res->u.pat.nowildcardlen);
  355. if (res->u.pat.flags & PATTERN_FLAG_NEGATIVE) {
  356. warning(_("Negative patterns are ignored in git attributes\n"
  357. "Use '\\!' for literal leading exclamation."));
  358. goto fail_return;
  359. }
  360. }
  361. res->is_macro = is_macro;
  362. res->num_attr = num_attr;
  363. /* Second pass to fill the attr_states */
  364. for (cp = states, i = 0; *cp; i++) {
  365. cp = parse_attr(src, lineno, cp, &(res->state[i]));
  366. }
  367. strbuf_release(&pattern);
  368. return res;
  369. fail_return:
  370. strbuf_release(&pattern);
  371. free(res);
  372. return NULL;
  373. }
  374. /*
  375. * Like info/exclude and .gitignore, the attribute information can
  376. * come from many places.
  377. *
  378. * (1) .gitattributes file of the same directory;
  379. * (2) .gitattributes file of the parent directory if (1) does not have
  380. * any match; this goes recursively upwards, just like .gitignore.
  381. * (3) $GIT_DIR/info/attributes, which overrides both of the above.
  382. *
  383. * In the same file, later entries override the earlier match, so in the
  384. * global list, we would have entries from info/attributes the earliest
  385. * (reading the file from top to bottom), .gitattributes of the root
  386. * directory (again, reading the file from top to bottom) down to the
  387. * current directory, and then scan the list backwards to find the first match.
  388. * This is exactly the same as what is_excluded() does in dir.c to deal with
  389. * .gitignore file and info/excludes file as a fallback.
  390. */
  391. struct attr_stack {
  392. struct attr_stack *prev;
  393. char *origin;
  394. size_t originlen;
  395. unsigned num_matches;
  396. unsigned alloc;
  397. struct match_attr **attrs;
  398. };
  399. static void attr_stack_free(struct attr_stack *e)
  400. {
  401. int i;
  402. free(e->origin);
  403. for (i = 0; i < e->num_matches; i++) {
  404. struct match_attr *a = e->attrs[i];
  405. int j;
  406. for (j = 0; j < a->num_attr; j++) {
  407. const char *setto = a->state[j].setto;
  408. if (setto == ATTR__TRUE ||
  409. setto == ATTR__FALSE ||
  410. setto == ATTR__UNSET ||
  411. setto == ATTR__UNKNOWN)
  412. ;
  413. else
  414. free((char *) setto);
  415. }
  416. free(a);
  417. }
  418. free(e->attrs);
  419. free(e);
  420. }
  421. static void drop_attr_stack(struct attr_stack **stack)
  422. {
  423. while (*stack) {
  424. struct attr_stack *elem = *stack;
  425. *stack = elem->prev;
  426. attr_stack_free(elem);
  427. }
  428. }
  429. /* List of all attr_check structs; access should be surrounded by mutex */
  430. static struct check_vector {
  431. size_t nr;
  432. size_t alloc;
  433. struct attr_check **checks;
  434. pthread_mutex_t mutex;
  435. } check_vector;
  436. static inline void vector_lock(void)
  437. {
  438. pthread_mutex_lock(&check_vector.mutex);
  439. }
  440. static inline void vector_unlock(void)
  441. {
  442. pthread_mutex_unlock(&check_vector.mutex);
  443. }
  444. static void check_vector_add(struct attr_check *c)
  445. {
  446. vector_lock();
  447. ALLOC_GROW(check_vector.checks,
  448. check_vector.nr + 1,
  449. check_vector.alloc);
  450. check_vector.checks[check_vector.nr++] = c;
  451. vector_unlock();
  452. }
  453. static void check_vector_remove(struct attr_check *check)
  454. {
  455. int i;
  456. vector_lock();
  457. /* Find entry */
  458. for (i = 0; i < check_vector.nr; i++)
  459. if (check_vector.checks[i] == check)
  460. break;
  461. if (i >= check_vector.nr)
  462. BUG("no entry found");
  463. /* shift entries over */
  464. for (; i < check_vector.nr - 1; i++)
  465. check_vector.checks[i] = check_vector.checks[i + 1];
  466. check_vector.nr--;
  467. vector_unlock();
  468. }
  469. /* Iterate through all attr_check instances and drop their stacks */
  470. static void drop_all_attr_stacks(void)
  471. {
  472. int i;
  473. vector_lock();
  474. for (i = 0; i < check_vector.nr; i++) {
  475. drop_attr_stack(&check_vector.checks[i]->stack);
  476. }
  477. vector_unlock();
  478. }
  479. struct attr_check *attr_check_alloc(void)
  480. {
  481. struct attr_check *c = xcalloc(1, sizeof(struct attr_check));
  482. /* save pointer to the check struct */
  483. check_vector_add(c);
  484. return c;
  485. }
  486. struct attr_check *attr_check_initl(const char *one, ...)
  487. {
  488. struct attr_check *check;
  489. int cnt;
  490. va_list params;
  491. const char *param;
  492. va_start(params, one);
  493. for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++)
  494. ;
  495. va_end(params);
  496. check = attr_check_alloc();
  497. check->nr = cnt;
  498. check->alloc = cnt;
  499. check->items = xcalloc(cnt, sizeof(struct attr_check_item));
  500. check->items[0].attr = git_attr(one);
  501. va_start(params, one);
  502. for (cnt = 1; cnt < check->nr; cnt++) {
  503. const struct git_attr *attr;
  504. param = va_arg(params, const char *);
  505. if (!param)
  506. BUG("counted %d != ended at %d",
  507. check->nr, cnt);
  508. attr = git_attr(param);
  509. if (!attr)
  510. BUG("%s: not a valid attribute name", param);
  511. check->items[cnt].attr = attr;
  512. }
  513. va_end(params);
  514. return check;
  515. }
  516. struct attr_check *attr_check_dup(const struct attr_check *check)
  517. {
  518. struct attr_check *ret;
  519. if (!check)
  520. return NULL;
  521. ret = attr_check_alloc();
  522. ret->nr = check->nr;
  523. ret->alloc = check->alloc;
  524. ALLOC_ARRAY(ret->items, ret->nr);
  525. COPY_ARRAY(ret->items, check->items, ret->nr);
  526. return ret;
  527. }
  528. struct attr_check_item *attr_check_append(struct attr_check *check,
  529. const struct git_attr *attr)
  530. {
  531. struct attr_check_item *item;
  532. ALLOC_GROW(check->items, check->nr + 1, check->alloc);
  533. item = &check->items[check->nr++];
  534. item->attr = attr;
  535. return item;
  536. }
  537. void attr_check_reset(struct attr_check *check)
  538. {
  539. check->nr = 0;
  540. }
  541. void attr_check_clear(struct attr_check *check)
  542. {
  543. FREE_AND_NULL(check->items);
  544. check->alloc = 0;
  545. check->nr = 0;
  546. FREE_AND_NULL(check->all_attrs);
  547. check->all_attrs_nr = 0;
  548. drop_attr_stack(&check->stack);
  549. }
  550. void attr_check_free(struct attr_check *check)
  551. {
  552. if (check) {
  553. /* Remove check from the check vector */
  554. check_vector_remove(check);
  555. attr_check_clear(check);
  556. free(check);
  557. }
  558. }
  559. static const char *builtin_attr[] = {
  560. "[attr]binary -diff -merge -text",
  561. NULL,
  562. };
  563. static void handle_attr_line(struct attr_stack *res,
  564. const char *line,
  565. const char *src,
  566. int lineno,
  567. int macro_ok)
  568. {
  569. struct match_attr *a;
  570. a = parse_attr_line(line, src, lineno, macro_ok);
  571. if (!a)
  572. return;
  573. ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
  574. res->attrs[res->num_matches++] = a;
  575. }
  576. static struct attr_stack *read_attr_from_array(const char **list)
  577. {
  578. struct attr_stack *res;
  579. const char *line;
  580. int lineno = 0;
  581. res = xcalloc(1, sizeof(*res));
  582. while ((line = *(list++)) != NULL)
  583. handle_attr_line(res, line, "[builtin]", ++lineno, 1);
  584. return res;
  585. }
  586. /*
  587. * Callers into the attribute system assume there is a single, system-wide
  588. * global state where attributes are read from and when the state is flipped by
  589. * calling git_attr_set_direction(), the stack frames that have been
  590. * constructed need to be discarded so so that subsequent calls into the
  591. * attribute system will lazily read from the right place. Since changing
  592. * direction causes a global paradigm shift, it should not ever be called while
  593. * another thread could potentially be calling into the attribute system.
  594. */
  595. static enum git_attr_direction direction;
  596. void git_attr_set_direction(enum git_attr_direction new_direction)
  597. {
  598. if (is_bare_repository() && new_direction != GIT_ATTR_INDEX)
  599. BUG("non-INDEX attr direction in a bare repo");
  600. if (new_direction != direction)
  601. drop_all_attr_stacks();
  602. direction = new_direction;
  603. }
  604. static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
  605. {
  606. FILE *fp = fopen_or_warn(path, "r");
  607. struct attr_stack *res;
  608. char buf[2048];
  609. int lineno = 0;
  610. if (!fp)
  611. return NULL;
  612. res = xcalloc(1, sizeof(*res));
  613. while (fgets(buf, sizeof(buf), fp)) {
  614. char *bufp = buf;
  615. if (!lineno)
  616. skip_utf8_bom(&bufp, strlen(bufp));
  617. handle_attr_line(res, bufp, path, ++lineno, macro_ok);
  618. }
  619. fclose(fp);
  620. return res;
  621. }
  622. static struct attr_stack *read_attr_from_index(const struct index_state *istate,
  623. const char *path,
  624. int macro_ok)
  625. {
  626. struct attr_stack *res;
  627. char *buf, *sp;
  628. int lineno = 0;
  629. if (!istate)
  630. return NULL;
  631. buf = read_blob_data_from_index(istate, path, NULL);
  632. if (!buf)
  633. return NULL;
  634. res = xcalloc(1, sizeof(*res));
  635. for (sp = buf; *sp; ) {
  636. char *ep;
  637. int more;
  638. ep = strchrnul(sp, '\n');
  639. more = (*ep == '\n');
  640. *ep = '\0';
  641. handle_attr_line(res, sp, path, ++lineno, macro_ok);
  642. sp = ep + more;
  643. }
  644. free(buf);
  645. return res;
  646. }
  647. static struct attr_stack *read_attr(const struct index_state *istate,
  648. const char *path, int macro_ok)
  649. {
  650. struct attr_stack *res = NULL;
  651. if (direction == GIT_ATTR_INDEX) {
  652. res = read_attr_from_index(istate, path, macro_ok);
  653. } else if (!is_bare_repository()) {
  654. if (direction == GIT_ATTR_CHECKOUT) {
  655. res = read_attr_from_index(istate, path, macro_ok);
  656. if (!res)
  657. res = read_attr_from_file(path, macro_ok);
  658. } else if (direction == GIT_ATTR_CHECKIN) {
  659. res = read_attr_from_file(path, macro_ok);
  660. if (!res)
  661. /*
  662. * There is no checked out .gitattributes file
  663. * there, but we might have it in the index.
  664. * We allow operation in a sparsely checked out
  665. * work tree, so read from it.
  666. */
  667. res = read_attr_from_index(istate, path, macro_ok);
  668. }
  669. }
  670. if (!res)
  671. res = xcalloc(1, sizeof(*res));
  672. return res;
  673. }
  674. #if DEBUG_ATTR
  675. static void debug_info(const char *what, struct attr_stack *elem)
  676. {
  677. fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
  678. }
  679. static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
  680. {
  681. const char *value = v;
  682. if (ATTR_TRUE(value))
  683. value = "set";
  684. else if (ATTR_FALSE(value))
  685. value = "unset";
  686. else if (ATTR_UNSET(value))
  687. value = "unspecified";
  688. fprintf(stderr, "%s: %s => %s (%s)\n",
  689. what, attr->name, (char *) value, match);
  690. }
  691. #define debug_push(a) debug_info("push", (a))
  692. #define debug_pop(a) debug_info("pop", (a))
  693. #else
  694. #define debug_push(a) do { ; } while (0)
  695. #define debug_pop(a) do { ; } while (0)
  696. #define debug_set(a,b,c,d) do { ; } while (0)
  697. #endif /* DEBUG_ATTR */
  698. static const char *git_etc_gitattributes(void)
  699. {
  700. static const char *system_wide;
  701. if (!system_wide)
  702. system_wide = system_path(ETC_GITATTRIBUTES);
  703. return system_wide;
  704. }
  705. static const char *get_home_gitattributes(void)
  706. {
  707. if (!git_attributes_file)
  708. git_attributes_file = xdg_config_home("attributes");
  709. return git_attributes_file;
  710. }
  711. static int git_attr_system(void)
  712. {
  713. return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
  714. }
  715. static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
  716. static void push_stack(struct attr_stack **attr_stack_p,
  717. struct attr_stack *elem, char *origin, size_t originlen)
  718. {
  719. if (elem) {
  720. elem->origin = origin;
  721. if (origin)
  722. elem->originlen = originlen;
  723. elem->prev = *attr_stack_p;
  724. *attr_stack_p = elem;
  725. }
  726. }
  727. static void bootstrap_attr_stack(const struct index_state *istate,
  728. struct attr_stack **stack)
  729. {
  730. struct attr_stack *e;
  731. if (*stack)
  732. return;
  733. /* builtin frame */
  734. e = read_attr_from_array(builtin_attr);
  735. push_stack(stack, e, NULL, 0);
  736. /* system-wide frame */
  737. if (git_attr_system()) {
  738. e = read_attr_from_file(git_etc_gitattributes(), 1);
  739. push_stack(stack, e, NULL, 0);
  740. }
  741. /* home directory */
  742. if (get_home_gitattributes()) {
  743. e = read_attr_from_file(get_home_gitattributes(), 1);
  744. push_stack(stack, e, NULL, 0);
  745. }
  746. /* root directory */
  747. e = read_attr(istate, GITATTRIBUTES_FILE, 1);
  748. push_stack(stack, e, xstrdup(""), 0);
  749. /* info frame */
  750. if (startup_info->have_repository)
  751. e = read_attr_from_file(git_path_info_attributes(), 1);
  752. else
  753. e = NULL;
  754. if (!e)
  755. e = xcalloc(1, sizeof(struct attr_stack));
  756. push_stack(stack, e, NULL, 0);
  757. }
  758. static void prepare_attr_stack(const struct index_state *istate,
  759. const char *path, int dirlen,
  760. struct attr_stack **stack)
  761. {
  762. struct attr_stack *info;
  763. struct strbuf pathbuf = STRBUF_INIT;
  764. /*
  765. * At the bottom of the attribute stack is the built-in
  766. * set of attribute definitions, followed by the contents
  767. * of $(prefix)/etc/gitattributes and a file specified by
  768. * core.attributesfile. Then, contents from
  769. * .gitattributes files from directories closer to the
  770. * root to the ones in deeper directories are pushed
  771. * to the stack. Finally, at the very top of the stack
  772. * we always keep the contents of $GIT_DIR/info/attributes.
  773. *
  774. * When checking, we use entries from near the top of the
  775. * stack, preferring $GIT_DIR/info/attributes, then
  776. * .gitattributes in deeper directories to shallower ones,
  777. * and finally use the built-in set as the default.
  778. */
  779. bootstrap_attr_stack(istate, stack);
  780. /*
  781. * Pop the "info" one that is always at the top of the stack.
  782. */
  783. info = *stack;
  784. *stack = info->prev;
  785. /*
  786. * Pop the ones from directories that are not the prefix of
  787. * the path we are checking. Break out of the loop when we see
  788. * the root one (whose origin is an empty string "") or the builtin
  789. * one (whose origin is NULL) without popping it.
  790. */
  791. while ((*stack)->origin) {
  792. int namelen = (*stack)->originlen;
  793. struct attr_stack *elem;
  794. elem = *stack;
  795. if (namelen <= dirlen &&
  796. !strncmp(elem->origin, path, namelen) &&
  797. (!namelen || path[namelen] == '/'))
  798. break;
  799. debug_pop(elem);
  800. *stack = elem->prev;
  801. attr_stack_free(elem);
  802. }
  803. /*
  804. * bootstrap_attr_stack() should have added, and the
  805. * above loop should have stopped before popping, the
  806. * root element whose attr_stack->origin is set to an
  807. * empty string.
  808. */
  809. assert((*stack)->origin);
  810. strbuf_addstr(&pathbuf, (*stack)->origin);
  811. /* Build up to the directory 'path' is in */
  812. while (pathbuf.len < dirlen) {
  813. size_t len = pathbuf.len;
  814. struct attr_stack *next;
  815. char *origin;
  816. /* Skip path-separator */
  817. if (len < dirlen && is_dir_sep(path[len]))
  818. len++;
  819. /* Find the end of the next component */
  820. while (len < dirlen && !is_dir_sep(path[len]))
  821. len++;
  822. if (pathbuf.len > 0)
  823. strbuf_addch(&pathbuf, '/');
  824. strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len));
  825. strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE);
  826. next = read_attr(istate, pathbuf.buf, 0);
  827. /* reset the pathbuf to not include "/.gitattributes" */
  828. strbuf_setlen(&pathbuf, len);
  829. origin = xstrdup(pathbuf.buf);
  830. push_stack(stack, next, origin, len);
  831. }
  832. /*
  833. * Finally push the "info" one at the top of the stack.
  834. */
  835. push_stack(stack, info, NULL, 0);
  836. strbuf_release(&pathbuf);
  837. }
  838. static int path_matches(const char *pathname, int pathlen,
  839. int basename_offset,
  840. const struct pattern *pat,
  841. const char *base, int baselen)
  842. {
  843. const char *pattern = pat->pattern;
  844. int prefix = pat->nowildcardlen;
  845. int isdir = (pathlen && pathname[pathlen - 1] == '/');
  846. if ((pat->flags & PATTERN_FLAG_MUSTBEDIR) && !isdir)
  847. return 0;
  848. if (pat->flags & PATTERN_FLAG_NODIR) {
  849. return match_basename(pathname + basename_offset,
  850. pathlen - basename_offset - isdir,
  851. pattern, prefix,
  852. pat->patternlen, pat->flags);
  853. }
  854. return match_pathname(pathname, pathlen - isdir,
  855. base, baselen,
  856. pattern, prefix, pat->patternlen, pat->flags);
  857. }
  858. static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem);
  859. static int fill_one(const char *what, struct all_attrs_item *all_attrs,
  860. const struct match_attr *a, int rem)
  861. {
  862. int i;
  863. for (i = a->num_attr - 1; rem > 0 && i >= 0; i--) {
  864. const struct git_attr *attr = a->state[i].attr;
  865. const char **n = &(all_attrs[attr->attr_nr].value);
  866. const char *v = a->state[i].setto;
  867. if (*n == ATTR__UNKNOWN) {
  868. debug_set(what,
  869. a->is_macro ? a->u.attr->name : a->u.pat.pattern,
  870. attr, v);
  871. *n = v;
  872. rem--;
  873. rem = macroexpand_one(all_attrs, attr->attr_nr, rem);
  874. }
  875. }
  876. return rem;
  877. }
  878. static int fill(const char *path, int pathlen, int basename_offset,
  879. const struct attr_stack *stack,
  880. struct all_attrs_item *all_attrs, int rem)
  881. {
  882. for (; rem > 0 && stack; stack = stack->prev) {
  883. int i;
  884. const char *base = stack->origin ? stack->origin : "";
  885. for (i = stack->num_matches - 1; 0 < rem && 0 <= i; i--) {
  886. const struct match_attr *a = stack->attrs[i];
  887. if (a->is_macro)
  888. continue;
  889. if (path_matches(path, pathlen, basename_offset,
  890. &a->u.pat, base, stack->originlen))
  891. rem = fill_one("fill", all_attrs, a, rem);
  892. }
  893. }
  894. return rem;
  895. }
  896. static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem)
  897. {
  898. const struct all_attrs_item *item = &all_attrs[nr];
  899. if (item->macro && item->value == ATTR__TRUE)
  900. return fill_one("expand", all_attrs, item->macro, rem);
  901. else
  902. return rem;
  903. }
  904. /*
  905. * Marks the attributes which are macros based on the attribute stack.
  906. * This prevents having to search through the attribute stack each time
  907. * a macro needs to be expanded during the fill stage.
  908. */
  909. static void determine_macros(struct all_attrs_item *all_attrs,
  910. const struct attr_stack *stack)
  911. {
  912. for (; stack; stack = stack->prev) {
  913. int i;
  914. for (i = stack->num_matches - 1; i >= 0; i--) {
  915. const struct match_attr *ma = stack->attrs[i];
  916. if (ma->is_macro) {
  917. int n = ma->u.attr->attr_nr;
  918. if (!all_attrs[n].macro) {
  919. all_attrs[n].macro = ma;
  920. }
  921. }
  922. }
  923. }
  924. }
  925. /*
  926. * Collect attributes for path into the array pointed to by check->all_attrs.
  927. * If check->check_nr is non-zero, only attributes in check[] are collected.
  928. * Otherwise all attributes are collected.
  929. */
  930. static void collect_some_attrs(const struct index_state *istate,
  931. const char *path,
  932. struct attr_check *check)
  933. {
  934. int pathlen, rem, dirlen;
  935. const char *cp, *last_slash = NULL;
  936. int basename_offset;
  937. for (cp = path; *cp; cp++) {
  938. if (*cp == '/' && cp[1])
  939. last_slash = cp;
  940. }
  941. pathlen = cp - path;
  942. if (last_slash) {
  943. basename_offset = last_slash + 1 - path;
  944. dirlen = last_slash - path;
  945. } else {
  946. basename_offset = 0;
  947. dirlen = 0;
  948. }
  949. prepare_attr_stack(istate, path, dirlen, &check->stack);
  950. all_attrs_init(&g_attr_hashmap, check);
  951. determine_macros(check->all_attrs, check->stack);
  952. rem = check->all_attrs_nr;
  953. fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem);
  954. }
  955. void git_check_attr(const struct index_state *istate,
  956. const char *path,
  957. struct attr_check *check)
  958. {
  959. int i;
  960. collect_some_attrs(istate, path, check);
  961. for (i = 0; i < check->nr; i++) {
  962. size_t n = check->items[i].attr->attr_nr;
  963. const char *value = check->all_attrs[n].value;
  964. if (value == ATTR__UNKNOWN)
  965. value = ATTR__UNSET;
  966. check->items[i].value = value;
  967. }
  968. }
  969. void git_all_attrs(const struct index_state *istate,
  970. const char *path, struct attr_check *check)
  971. {
  972. int i;
  973. attr_check_reset(check);
  974. collect_some_attrs(istate, path, check);
  975. for (i = 0; i < check->all_attrs_nr; i++) {
  976. const char *name = check->all_attrs[i].attr->name;
  977. const char *value = check->all_attrs[i].value;
  978. struct attr_check_item *item;
  979. if (value == ATTR__UNSET || value == ATTR__UNKNOWN)
  980. continue;
  981. item = attr_check_append(check, git_attr(name));
  982. item->value = value;
  983. }
  984. }
  985. void attr_start(void)
  986. {
  987. pthread_mutex_init(&g_attr_hashmap.mutex, NULL);
  988. pthread_mutex_init(&check_vector.mutex, NULL);
  989. }