attr.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #ifndef ATTR_H
  2. #define ATTR_H
  3. /**
  4. * gitattributes mechanism gives a uniform way to associate various attributes
  5. * to set of paths.
  6. *
  7. *
  8. * Querying Specific Attributes
  9. * ----------------------------
  10. *
  11. * - Prepare `struct attr_check` using attr_check_initl() function, enumerating
  12. * the names of attributes whose values you are interested in, terminated with
  13. * a NULL pointer. Alternatively, an empty `struct attr_check` can be
  14. * prepared by calling `attr_check_alloc()` function and then attributes you
  15. * want to ask about can be added to it with `attr_check_append()` function.
  16. *
  17. * - Call `git_check_attr()` to check the attributes for the path.
  18. *
  19. * - Inspect `attr_check` structure to see how each of the attribute in the
  20. * array is defined for the path.
  21. *
  22. *
  23. * Example
  24. * -------
  25. *
  26. * To see how attributes "crlf" and "ident" are set for different paths.
  27. *
  28. * - Prepare a `struct attr_check` with two elements (because we are checking
  29. * two attributes):
  30. *
  31. * ------------
  32. * static struct attr_check *check;
  33. * static void setup_check(void)
  34. * {
  35. * if (check)
  36. * return; // already done
  37. * check = attr_check_initl("crlf", "ident", NULL);
  38. * }
  39. * ------------
  40. *
  41. * - Call `git_check_attr()` with the prepared `struct attr_check`:
  42. *
  43. * ------------
  44. * const char *path;
  45. *
  46. * setup_check();
  47. * git_check_attr(path, check);
  48. * ------------
  49. *
  50. * - Act on `.value` member of the result, left in `check->items[]`:
  51. *
  52. * ------------
  53. * const char *value = check->items[0].value;
  54. *
  55. * if (ATTR_TRUE(value)) {
  56. * The attribute is Set, by listing only the name of the
  57. * attribute in the gitattributes file for the path.
  58. * } else if (ATTR_FALSE(value)) {
  59. * The attribute is Unset, by listing the name of the
  60. * attribute prefixed with a dash - for the path.
  61. * } else if (ATTR_UNSET(value)) {
  62. * The attribute is neither set nor unset for the path.
  63. * } else if (!strcmp(value, "input")) {
  64. * If none of ATTR_TRUE(), ATTR_FALSE(), or ATTR_UNSET() is
  65. * true, the value is a string set in the gitattributes
  66. * file for the path by saying "attr=value".
  67. * } else if (... other check using value as string ...) {
  68. * ...
  69. * }
  70. * ------------
  71. *
  72. * To see how attributes in argv[] are set for different paths, only
  73. * the first step in the above would be different.
  74. *
  75. * ------------
  76. * static struct attr_check *check;
  77. * static void setup_check(const char **argv)
  78. * {
  79. * check = attr_check_alloc();
  80. * while (*argv) {
  81. * struct git_attr *attr = git_attr(*argv);
  82. * attr_check_append(check, attr);
  83. * argv++;
  84. * }
  85. * }
  86. * ------------
  87. *
  88. *
  89. * Querying All Attributes
  90. * -----------------------
  91. *
  92. * To get the values of all attributes associated with a file:
  93. *
  94. * - Prepare an empty `attr_check` structure by calling `attr_check_alloc()`.
  95. *
  96. * - Call `git_all_attrs()`, which populates the `attr_check` with the
  97. * attributes attached to the path.
  98. *
  99. * - Iterate over the `attr_check.items[]` array to examine the attribute
  100. * names and values. The name of the attribute described by an
  101. * `attr_check.items[]` object can be retrieved via
  102. * `git_attr_name(check->items[i].attr)`. (Please note that no items will be
  103. * returned for unset attributes, so `ATTR_UNSET()` will return false for all
  104. * returned `attr_check.items[]` objects.)
  105. *
  106. * - Free the `attr_check` struct by calling `attr_check_free()`.
  107. */
  108. struct index_state;
  109. /**
  110. * An attribute is an opaque object that is identified by its name. Pass the
  111. * name to `git_attr()` function to obtain the object of this type.
  112. * The internal representation of this structure is of no interest to the
  113. * calling programs. The name of the attribute can be retrieved by calling
  114. * `git_attr_name()`.
  115. */
  116. struct git_attr;
  117. /* opaque structures used internally for attribute collection */
  118. struct all_attrs_item;
  119. struct attr_stack;
  120. struct index_state;
  121. /*
  122. * Given a string, return the gitattribute object that
  123. * corresponds to it.
  124. */
  125. const struct git_attr *git_attr(const char *);
  126. /* Internal use */
  127. extern const char git_attr__true[];
  128. extern const char git_attr__false[];
  129. /**
  130. * Attribute Values
  131. * ----------------
  132. *
  133. * An attribute for a path can be in one of four states: Set, Unset, Unspecified
  134. * or set to a string, and `.value` member of `struct attr_check_item` records
  135. * it. The three macros check these, if none of them returns true, `.value`
  136. * member points at a string value of the attribute for the path.
  137. */
  138. /* Returns true if the attribute is Set for the path. */
  139. #define ATTR_TRUE(v) ((v) == git_attr__true)
  140. /* Returns true if the attribute is Unset for the path. */
  141. #define ATTR_FALSE(v) ((v) == git_attr__false)
  142. /* Returns true if the attribute is Unspecified for the path. */
  143. #define ATTR_UNSET(v) ((v) == NULL)
  144. /* This structure represents one attribute and its value. */
  145. struct attr_check_item {
  146. const struct git_attr *attr;
  147. const char *value;
  148. };
  149. /**
  150. * This structure represents a collection of `attr_check_item`. It is passed to
  151. * `git_check_attr()` function, specifying the attributes to check, and
  152. * receives their values.
  153. */
  154. struct attr_check {
  155. int nr;
  156. int alloc;
  157. struct attr_check_item *items;
  158. int all_attrs_nr;
  159. struct all_attrs_item *all_attrs;
  160. struct attr_stack *stack;
  161. };
  162. struct attr_check *attr_check_alloc(void);
  163. struct attr_check *attr_check_initl(const char *, ...);
  164. struct attr_check *attr_check_dup(const struct attr_check *check);
  165. struct attr_check_item *attr_check_append(struct attr_check *check,
  166. const struct git_attr *attr);
  167. void attr_check_reset(struct attr_check *check);
  168. void attr_check_clear(struct attr_check *check);
  169. void attr_check_free(struct attr_check *check);
  170. /*
  171. * Return the name of the attribute represented by the argument. The
  172. * return value is a pointer to a null-delimited string that is part
  173. * of the internal data structure; it should not be modified or freed.
  174. */
  175. const char *git_attr_name(const struct git_attr *);
  176. void git_check_attr(const struct index_state *istate,
  177. const char *path, struct attr_check *check);
  178. /*
  179. * Retrieve all attributes that apply to the specified path.
  180. * check holds the attributes and their values.
  181. */
  182. void git_all_attrs(const struct index_state *istate,
  183. const char *path, struct attr_check *check);
  184. enum git_attr_direction {
  185. GIT_ATTR_CHECKIN,
  186. GIT_ATTR_CHECKOUT,
  187. GIT_ATTR_INDEX
  188. };
  189. void git_attr_set_direction(enum git_attr_direction new_direction);
  190. void attr_start(void);
  191. #endif /* ATTR_H */