@@ -8,6 +8,18 @@ attributes to set of paths.
8
8
Data Structure
9
9
--------------
10
10
11
+ extern struct git_attr *git_attr(const char *);
12
+
13
+ /*
14
+ * Return the name of the attribute represented by the argument. The
15
+ * return value is a pointer to a null-delimited string that is part
16
+ * of the internal data structure; it should not be modified or freed.
17
+ */
18
+ extern const char *git_attr_name(const struct git_attr *);
19
+
20
+ extern int attr_name_valid(const char *name, size_t namelen);
21
+ extern void invalid_attr_name_message(struct strbuf *, const char *, int);
22
+
11
23
`struct git_attr`::
12
24
13
25
An attribute is an opaque object that is identified by its name.
@@ -18,8 +30,15 @@ Data Structure
18
30
19
31
`struct git_attr_check`::
20
32
21
- This structure represents a set of attributes to check in a call
22
- to `git_check_attr()` function, and receives the results.
33
+ This structure represents a collection of `struct git_attrs`.
34
+ It is passed to `git_check_attr()` function, specifying the
35
+ attributes to check, and receives their values into a corresponding
36
+ `struct git_attr_result`.
37
+
38
+ `struct git_attr_result`::
39
+
40
+ This structure represents a collection of results to its
41
+ corresponding `struct git_attr_check`, that has the same order.
23
42
24
43
25
44
Attribute Values
@@ -48,49 +67,62 @@ value of the attribute for the path.
48
67
Querying Specific Attributes
49
68
----------------------------
50
69
51
- * Prepare an array of `struct git_attr_check` to define the list of
52
- attributes you would want to check. To populate this array, you would
53
- need to define necessary attributes by calling `git_attr()` function.
70
+ * Prepare `struct git_attr_check` using git_attr_check_initl()
71
+ function, enumerating the names of attributes whose values you are
72
+ interested in, terminated with a NULL pointer. Alternatively, an
73
+ empty `struct git_attr_check` as alloced by git_attr_check_alloc()
74
+ can be prepared by calling `git_attr_check_alloc()` function and
75
+ then attributes you want to ask about can be added to it with
76
+ `git_attr_check_append()` function.
77
+ git_attr_check_initl is thread safe, i.e. you can call it
78
+ from different threads at the same time; internally however only one
79
+ call at a time is processed. If the calls from different threads have
80
+ the same arguments, the returned `git_attr_check` may be the same.
54
81
55
- * Call `git_check_attr()` to check the attributes for the path.
82
+ * Call `git_check_attr()` to check the attributes for the path,
83
+ the returned `git_attr_result` contains the result.
56
84
57
- * Inspect `git_attr_check ` structure to see how each of the attribute in
58
- the array is defined for the path.
85
+ * Inspect the returned `git_attr_result ` structure to see how
86
+ each of the attribute in the array is defined for the path.
59
87
88
+ * Do not free the result as the memory is owned by the attr subsystem.
60
89
61
90
Example
62
91
-------
63
92
64
- To see how attributes "crlf" and "indent " are set for different paths.
93
+ To see how attributes "crlf" and "ident " are set for different paths.
65
94
66
- . Prepare an array of `struct git_attr_check` with two elements (because
67
- we are checking two attributes). Initialize their `attr` member with
68
- pointers to `struct git_attr` obtained by calling `git_attr()`:
95
+ . Prepare a `struct git_attr_check` with two elements (because
96
+ we are checking two attributes):
69
97
70
98
------------
71
- static struct git_attr_check check[2] ;
99
+ static struct git_attr_check * check;
72
100
static void setup_check(void)
73
101
{
74
- if (check[0].attr )
102
+ if (check)
75
103
return; /* already done */
76
- check[0].attr = git_attr("crlf");
77
- check[1].attr = git_attr("ident");
104
+ check = git_attr_check_initl("crlf", "ident", NULL);
78
105
}
79
106
------------
80
107
81
- . Call `git_check_attr()` with the prepared array of `struct git_attr_check`:
108
+ . Call `git_check_attr()` with the prepared `struct git_attr_check`:
82
109
83
110
------------
84
111
const char *path;
112
+ struct git_attr_result *result;
85
113
86
114
setup_check();
87
- git_check_attr(path, ARRAY_SIZE(check) , check);
115
+ result = git_check_attr(path, check);
88
116
------------
89
117
90
- . Act on `.value` member of the result, left in `check[]`:
118
+ The `result` must not be free'd as it is owned by the attr subsystem.
119
+ It is reused by the same thread, so a subsequent call to git_check_attr
120
+ in the same thread will overwrite the result.
121
+
122
+ . Act on `result->value[]`:
91
123
92
124
------------
93
- const char *value = check [0].value ;
125
+ const char *value = result->value [0];
94
126
95
127
if (ATTR_TRUE(value)) {
96
128
The attribute is Set, by listing only the name of the
@@ -109,20 +141,44 @@ static void setup_check(void)
109
141
}
110
142
------------
111
143
144
+ To see how attributes in argv[] are set for different paths, only
145
+ the first step in the above would be different.
146
+
147
+ ------------
148
+ static struct git_attr_check *check;
149
+ static void setup_check(const char **argv)
150
+ {
151
+ if (check)
152
+ return; /* already done */
153
+ check = git_attr_check_alloc();
154
+ while (*argv) {
155
+ struct git_attr *attr = git_attr(*argv);
156
+ git_attr_check_append(check, attr);
157
+ argv++;
158
+ }
159
+ }
160
+ ------------
161
+
112
162
113
163
Querying All Attributes
114
164
-----------------------
115
165
116
166
To get the values of all attributes associated with a file:
117
167
118
- * Call `git_all_attrs()`, which returns an array of `git_attr_check`
119
- structures.
168
+ * Setup a local variables on the stack for both the question
169
+ `struct git_attr_check` as well as the result `struct git_attr_result`.
170
+ Zero them out via their respective _INIT macro.
171
+
172
+ * Call `git_all_attrs()`
120
173
121
- * Iterate over the `git_attr_check` array to examine the attribute
122
- names and values. The name of the attribute described by a
123
- `git_attr_check` object can be retrieved via
124
- `git_attr_name(check[i].attr)`. (Please note that no items will be
125
- returned for unset attributes, so `ATTR_UNSET()` will return false
126
- for all returned `git_array_check` objects.)
174
+ * Iterate over the `git_attr_check.attr[]` array to examine the
175
+ attribute names. The name of the attribute described by a
176
+ `git_attr_check.attr[]` object can be retrieved via
177
+ `git_attr_name(check->attr[i])`. (Please note that no items
178
+ will be returned for unset attributes, so `ATTR_UNSET()` will return
179
+ false for all returned `git_array_check` objects.)
180
+ The respective value for an attribute can be found in the same
181
+ index position in of `git_attr_result`.
127
182
128
- * Free the `git_array_check` array.
183
+ * Clear the local variables by calling `git_attr_check_clear()` and
184
+ `git_attr_result_clear()`.
0 commit comments