Skip to content

Commit 1295c21

Browse files
committed
attr: retire git_check_attrs() API
Since nobody uses the old API, make it file-scope static, and update the documentation to describe the new API. Signed-off-by: Junio C Hamano <[email protected]> Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Brandon Williams <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2aef63d commit 1295c21

File tree

3 files changed

+58
-32
lines changed

3 files changed

+58
-32
lines changed

Documentation/technical/api-gitattributes.txt

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@ Data Structure
1616
of no interest to the calling programs. The name of the
1717
attribute can be retrieved by calling `git_attr_name()`.
1818

19-
`struct git_attr_check`::
19+
`struct attr_check_item`::
2020

21-
This structure represents a set of attributes to check in a call
22-
to `git_check_attr()` function, and receives the results.
21+
This structure represents one attribute and its value.
22+
23+
`struct attr_check`::
24+
25+
This structure represents a collection of `attr_check_item`.
26+
It is passed to `git_check_attr()` function, specifying the
27+
attributes to check, and receives their values.
2328

2429

2530
Attribute Values
2631
----------------
2732

2833
An attribute for a path can be in one of four states: Set, Unset,
2934
Unspecified or set to a string, and `.value` member of `struct
30-
git_attr_check` records it. There are three macros to check these:
35+
attr_check_item` records it. There are three macros to check these:
3136

3237
`ATTR_TRUE()`::
3338

@@ -48,49 +53,51 @@ value of the attribute for the path.
4853
Querying Specific Attributes
4954
----------------------------
5055

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.
56+
* Prepare `struct attr_check` using attr_check_initl()
57+
function, enumerating the names of attributes whose values you are
58+
interested in, terminated with a NULL pointer. Alternatively, an
59+
empty `struct attr_check` can be prepared by calling
60+
`attr_check_alloc()` function and then attributes you want to
61+
ask about can be added to it with `attr_check_append()`
62+
function.
5463

5564
* Call `git_check_attr()` to check the attributes for the path.
5665

57-
* Inspect `git_attr_check` structure to see how each of the attribute in
58-
the array is defined for the path.
66+
* Inspect `attr_check` structure to see how each of the
67+
attribute in the array is defined for the path.
5968

6069

6170
Example
6271
-------
6372

64-
To see how attributes "crlf" and "indent" are set for different paths.
73+
To see how attributes "crlf" and "ident" are set for different paths.
6574

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()`:
75+
. Prepare a `struct attr_check` with two elements (because
76+
we are checking two attributes):
6977

7078
------------
71-
static struct git_attr_check check[2];
79+
static struct attr_check *check;
7280
static void setup_check(void)
7381
{
74-
if (check[0].attr)
82+
if (check)
7583
return; /* already done */
76-
check[0].attr = git_attr("crlf");
77-
check[1].attr = git_attr("ident");
84+
check = attr_check_initl("crlf", "ident", NULL);
7885
}
7986
------------
8087

81-
. Call `git_check_attr()` with the prepared array of `struct git_attr_check`:
88+
. Call `git_check_attr()` with the prepared `struct attr_check`:
8289

8390
------------
8491
const char *path;
8592

8693
setup_check();
87-
git_check_attr(path, ARRAY_SIZE(check), check);
94+
git_check_attr(path, check);
8895
------------
8996

90-
. Act on `.value` member of the result, left in `check[]`:
97+
. Act on `.value` member of the result, left in `check->items[]`:
9198

9299
------------
93-
const char *value = check[0].value;
100+
const char *value = check->items[0].value;
94101

95102
if (ATTR_TRUE(value)) {
96103
The attribute is Set, by listing only the name of the
@@ -109,20 +116,39 @@ static void setup_check(void)
109116
}
110117
------------
111118

119+
To see how attributes in argv[] are set for different paths, only
120+
the first step in the above would be different.
121+
122+
------------
123+
static struct attr_check *check;
124+
static void setup_check(const char **argv)
125+
{
126+
check = attr_check_alloc();
127+
while (*argv) {
128+
struct git_attr *attr = git_attr(*argv);
129+
attr_check_append(check, attr);
130+
argv++;
131+
}
132+
}
133+
------------
134+
112135

113136
Querying All Attributes
114137
-----------------------
115138

116139
To get the values of all attributes associated with a file:
117140

118-
* Call `git_all_attrs()`, which returns an array of `git_attr_check`
119-
structures.
141+
* Prepare an empty `attr_check` structure by calling
142+
`attr_check_alloc()`.
143+
144+
* Call `git_all_attrs()`, which populates the `attr_check`
145+
with the attributes attached to the path.
120146

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.)
147+
* Iterate over the `attr_check.items[]` array to examine
148+
the attribute names and values. The name of the attribute
149+
described by a `attr_check.items[]` object can be retrieved via
150+
`git_attr_name(check->items[i].attr)`. (Please note that no items
151+
will be returned for unset attributes, so `ATTR_UNSET()` will return
152+
false for all returned `attr_check.items[]` objects.)
127153

128-
* Free the `git_array_check` array.
154+
* Free the `attr_check` struct by calling `attr_check_free()`.

attr.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,8 @@ static void collect_some_attrs(const char *path, int num,
890890
rem = fill(path, pathlen, basename_offset, stk, rem);
891891
}
892892

893-
int git_check_attrs(const char *path, int num, struct attr_check_item *check)
893+
static int git_check_attrs(const char *path, int num,
894+
struct attr_check_item *check)
894895
{
895896
int i;
896897

attr.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ extern void attr_check_free(struct attr_check *check);
5252
*/
5353
extern const char *git_attr_name(const struct git_attr *);
5454

55-
int git_check_attrs(const char *path, int, struct attr_check_item *);
5655
extern int git_check_attr(const char *path, struct attr_check *check);
5756

5857
/*

0 commit comments

Comments
 (0)