blob: 3fb40cced08359b0fa93f6da155917e62668558e [file] [log] [blame]
Junio C Hamanod0bfd022007-04-12 01:07:32 -07001#ifndef ATTR_H
2#define ATTR_H
3
Heba Waly3a1b3412019-11-17 21:04:47 +00004/**
5 * gitattributes mechanism gives a uniform way to associate various attributes
6 * to set of paths.
7 *
8 *
9 * Querying Specific Attributes
10 * ----------------------------
11 *
12 * - Prepare `struct attr_check` using attr_check_initl() function, enumerating
13 * the names of attributes whose values you are interested in, terminated with
14 * a NULL pointer. Alternatively, an empty `struct attr_check` can be
15 * prepared by calling `attr_check_alloc()` function and then attributes you
16 * want to ask about can be added to it with `attr_check_append()` function.
17 *
18 * - Call `git_check_attr()` to check the attributes for the path.
19 *
20 * - Inspect `attr_check` structure to see how each of the attribute in the
21 * array is defined for the path.
22 *
23 *
24 * Example
25 * -------
26 *
27 * To see how attributes "crlf" and "ident" are set for different paths.
28 *
29 * - Prepare a `struct attr_check` with two elements (because we are checking
30 * two attributes):
31 *
32 * ------------
33 * static struct attr_check *check;
34 * static void setup_check(void)
35 * {
36 * if (check)
37 * return; // already done
38 * check = attr_check_initl("crlf", "ident", NULL);
39 * }
40 * ------------
41 *
42 * - Call `git_check_attr()` with the prepared `struct attr_check`:
43 *
44 * ------------
45 * const char *path;
46 *
47 * setup_check();
48 * git_check_attr(path, check);
49 * ------------
50 *
51 * - Act on `.value` member of the result, left in `check->items[]`:
52 *
53 * ------------
54 * const char *value = check->items[0].value;
55 *
56 * if (ATTR_TRUE(value)) {
57 * The attribute is Set, by listing only the name of the
58 * attribute in the gitattributes file for the path.
59 * } else if (ATTR_FALSE(value)) {
60 * The attribute is Unset, by listing the name of the
61 * attribute prefixed with a dash - for the path.
62 * } else if (ATTR_UNSET(value)) {
63 * The attribute is neither set nor unset for the path.
64 * } else if (!strcmp(value, "input")) {
65 * If none of ATTR_TRUE(), ATTR_FALSE(), or ATTR_UNSET() is
66 * true, the value is a string set in the gitattributes
67 * file for the path by saying "attr=value".
68 * } else if (... other check using value as string ...) {
69 * ...
70 * }
71 * ------------
72 *
73 * To see how attributes in argv[] are set for different paths, only
74 * the first step in the above would be different.
75 *
76 * ------------
77 * static struct attr_check *check;
78 * static void setup_check(const char **argv)
79 * {
80 * check = attr_check_alloc();
81 * while (*argv) {
82 * struct git_attr *attr = git_attr(*argv);
83 * attr_check_append(check, attr);
84 * argv++;
85 * }
86 * }
87 * ------------
88 *
89 *
90 * Querying All Attributes
91 * -----------------------
92 *
93 * To get the values of all attributes associated with a file:
94 *
95 * - Prepare an empty `attr_check` structure by calling `attr_check_alloc()`.
96 *
97 * - Call `git_all_attrs()`, which populates the `attr_check` with the
98 * attributes attached to the path.
99 *
100 * - Iterate over the `attr_check.items[]` array to examine the attribute
101 * names and values. The name of the attribute described by an
102 * `attr_check.items[]` object can be retrieved via
103 * `git_attr_name(check->items[i].attr)`. (Please note that no items will be
104 * returned for unset attributes, so `ATTR_UNSET()` will return false for all
105 * returned `attr_check.items[]` objects.)
106 *
107 * - Free the `attr_check` struct by calling `attr_check_free()`.
108 */
109
Nguyễn Thái Ngọc Duy7a400a22018-08-13 18:14:20 +0200110struct index_state;
111
Heba Waly3a1b3412019-11-17 21:04:47 +0000112/**
113 * An attribute is an opaque object that is identified by its name. Pass the
114 * name to `git_attr()` function to obtain the object of this type.
115 * The internal representation of this structure is of no interest to the
116 * calling programs. The name of the attribute can be retrieved by calling
117 * `git_attr_name()`.
118 */
Junio C Hamanod0bfd022007-04-12 01:07:32 -0700119struct git_attr;
120
Brandon Williamsdc81cf32017-01-27 18:02:05 -0800121/* opaque structures used internally for attribute collection */
Brandon Williams685b2922017-01-27 18:02:02 -0800122struct all_attrs_item;
Brandon Williamsdc81cf32017-01-27 18:02:05 -0800123struct attr_stack;
Brandon Williams685b2922017-01-27 18:02:02 -0800124
Junio C Hamanoa5e92ab2007-04-18 16:16:37 -0700125/*
126 * Given a string, return the gitattribute object that
127 * corresponds to it.
128 */
Brandon Williamse810e062017-01-27 18:02:04 -0800129const struct git_attr *git_attr(const char *);
Junio C Hamanod0bfd022007-04-12 01:07:32 -0700130
Junio C Hamano515106f2007-04-16 21:33:31 -0700131/* Internal use */
Junio C Hamanoa5e92ab2007-04-18 16:16:37 -0700132extern const char git_attr__true[];
133extern const char git_attr__false[];
Junio C Hamano515106f2007-04-16 21:33:31 -0700134
Heba Waly3a1b3412019-11-17 21:04:47 +0000135/**
136 * Attribute Values
137 * ----------------
138 *
139 * An attribute for a path can be in one of four states: Set, Unset, Unspecified
140 * or set to a string, and `.value` member of `struct attr_check_item` records
141 * it. The three macros check these, if none of them returns true, `.value`
142 * member points at a string value of the attribute for the path.
143 */
144
145/* Returns true if the attribute is Set for the path. */
Junio C Hamanoa5e92ab2007-04-18 16:16:37 -0700146#define ATTR_TRUE(v) ((v) == git_attr__true)
Heba Waly3a1b3412019-11-17 21:04:47 +0000147
148/* Returns true if the attribute is Unset for the path. */
Junio C Hamanoa5e92ab2007-04-18 16:16:37 -0700149#define ATTR_FALSE(v) ((v) == git_attr__false)
Heba Waly3a1b3412019-11-17 21:04:47 +0000150
151/* Returns true if the attribute is Unspecified for the path. */
Junio C Hamanoa5e92ab2007-04-18 16:16:37 -0700152#define ATTR_UNSET(v) ((v) == NULL)
Junio C Hamano515106f2007-04-16 21:33:31 -0700153
Heba Waly3a1b3412019-11-17 21:04:47 +0000154/* This structure represents one attribute and its value. */
Junio C Hamano7bd18052017-01-27 18:01:54 -0800155struct attr_check_item {
Junio C Hamanoec4d77a2017-01-27 18:01:48 -0800156 const struct git_attr *attr;
Junio C Hamanoa5e92ab2007-04-18 16:16:37 -0700157 const char *value;
Junio C Hamanod0bfd022007-04-12 01:07:32 -0700158};
159
Heba Waly3a1b3412019-11-17 21:04:47 +0000160/**
161 * This structure represents a collection of `attr_check_item`. It is passed to
162 * `git_check_attr()` function, specifying the attributes to check, and
163 * receives their values.
164 */
Junio C Hamano37293762017-01-30 10:05:20 -0800165struct attr_check {
166 int nr;
167 int alloc;
168 struct attr_check_item *items;
Brandon Williams685b2922017-01-27 18:02:02 -0800169 int all_attrs_nr;
170 struct all_attrs_item *all_attrs;
Brandon Williamsdc81cf32017-01-27 18:02:05 -0800171 struct attr_stack *stack;
Junio C Hamano37293762017-01-30 10:05:20 -0800172};
173
Nguyễn Thái Ngọc Duyc30f2e22018-06-30 11:20:21 +0200174struct attr_check *attr_check_alloc(void);
175struct attr_check *attr_check_initl(const char *, ...);
176struct attr_check *attr_check_dup(const struct attr_check *check);
Junio C Hamano37293762017-01-30 10:05:20 -0800177
Nguyễn Thái Ngọc Duyc30f2e22018-06-30 11:20:21 +0200178struct attr_check_item *attr_check_append(struct attr_check *check,
179 const struct git_attr *attr);
Junio C Hamano37293762017-01-30 10:05:20 -0800180
Nguyễn Thái Ngọc Duyc30f2e22018-06-30 11:20:21 +0200181void attr_check_reset(struct attr_check *check);
182void attr_check_clear(struct attr_check *check);
183void attr_check_free(struct attr_check *check);
Junio C Hamano37293762017-01-30 10:05:20 -0800184
Michael Haggerty352404a2011-08-04 06:36:17 +0200185/*
186 * Return the name of the attribute represented by the argument. The
187 * return value is a pointer to a null-delimited string that is part
188 * of the internal data structure; it should not be modified or freed.
189 */
Nguyễn Thái Ngọc Duyc30f2e22018-06-30 11:20:21 +0200190const char *git_attr_name(const struct git_attr *);
Michael Haggerty352404a2011-08-04 06:36:17 +0200191
Derrick Stolee847a9e52021-04-01 01:49:39 +0000192void git_check_attr(struct index_state *istate,
Torsten Bögershausend64324c2018-09-12 21:32:02 +0200193 const char *path, struct attr_check *check);
Junio C Hamanod0bfd022007-04-12 01:07:32 -0700194
Michael Haggertyee548df2011-08-04 06:36:23 +0200195/*
Junio C Hamano7f864112017-01-30 10:06:08 -0800196 * Retrieve all attributes that apply to the specified path.
197 * check holds the attributes and their values.
Michael Haggertyee548df2011-08-04 06:36:23 +0200198 */
Derrick Stolee847a9e52021-04-01 01:49:39 +0000199void git_all_attrs(struct index_state *istate,
Nguyễn Thái Ngọc Duy7a400a22018-08-13 18:14:20 +0200200 const char *path, struct attr_check *check);
Michael Haggertyee548df2011-08-04 06:36:23 +0200201
Junio C Hamano06f33c12009-03-13 21:24:08 -0700202enum git_attr_direction {
203 GIT_ATTR_CHECKIN,
Nguyễn Thái Ngọc Duy4191e802009-04-18 00:17:58 +0200204 GIT_ATTR_CHECKOUT,
Gary V. Vaughan4b055482010-05-14 09:31:35 +0000205 GIT_ATTR_INDEX
Junio C Hamano06f33c12009-03-13 21:24:08 -0700206};
Nguyễn Thái Ngọc Duyc4500e22018-08-13 18:14:33 +0200207void git_attr_set_direction(enum git_attr_direction new_direction);
Junio C Hamano06f33c12009-03-13 21:24:08 -0700208
Nguyễn Thái Ngọc Duyc30f2e22018-06-30 11:20:21 +0200209void attr_start(void);
Brandon Williams1a600b72017-01-27 18:02:01 -0800210
Junio C Hamanod0bfd022007-04-12 01:07:32 -0700211#endif /* ATTR_H */