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 <gitster@pobox.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Junio C Hamano
2017-01-27 18:01:58 -08:00
parent 2aef63d31c
commit 1295c21524
3 changed files with 58 additions and 32 deletions

View File

@ -16,10 +16,15 @@ Data Structure
of no interest to the calling programs. The name of the of no interest to the calling programs. The name of the
attribute can be retrieved by calling `git_attr_name()`. attribute can be retrieved by calling `git_attr_name()`.
`struct git_attr_check`:: `struct attr_check_item`::
This structure represents a set of attributes to check in a call This structure represents one attribute and its value.
to `git_check_attr()` function, and receives the results.
`struct attr_check`::
This structure represents a collection of `attr_check_item`.
It is passed to `git_check_attr()` function, specifying the
attributes to check, and receives their values.
Attribute Values Attribute Values
@ -27,7 +32,7 @@ Attribute Values
An attribute for a path can be in one of four states: Set, Unset, An attribute for a path can be in one of four states: Set, Unset,
Unspecified or set to a string, and `.value` member of `struct Unspecified or set to a string, and `.value` member of `struct
git_attr_check` records it. There are three macros to check these: attr_check_item` records it. There are three macros to check these:
`ATTR_TRUE()`:: `ATTR_TRUE()`::
@ -48,49 +53,51 @@ value of the attribute for the path.
Querying Specific Attributes Querying Specific Attributes
---------------------------- ----------------------------
* Prepare an array of `struct git_attr_check` to define the list of * Prepare `struct attr_check` using attr_check_initl()
attributes you would want to check. To populate this array, you would function, enumerating the names of attributes whose values you are
need to define necessary attributes by calling `git_attr()` function. interested in, terminated with a NULL pointer. Alternatively, an
empty `struct attr_check` can be prepared by calling
`attr_check_alloc()` function and then attributes you want to
ask about can be added to it with `attr_check_append()`
function.
* Call `git_check_attr()` to check the attributes for the path. * Call `git_check_attr()` to check the attributes for the path.
* Inspect `git_attr_check` structure to see how each of the attribute in * Inspect `attr_check` structure to see how each of the
the array is defined for the path. attribute in the array is defined for the path.
Example Example
------- -------
To see how attributes "crlf" and "indent" are set for different paths. To see how attributes "crlf" and "ident" are set for different paths.
. Prepare an array of `struct git_attr_check` with two elements (because . Prepare a `struct attr_check` with two elements (because
we are checking two attributes). Initialize their `attr` member with we are checking two attributes):
pointers to `struct git_attr` obtained by calling `git_attr()`:
------------ ------------
static struct git_attr_check check[2]; static struct attr_check *check;
static void setup_check(void) static void setup_check(void)
{ {
if (check[0].attr) if (check)
return; /* already done */ return; /* already done */
check[0].attr = git_attr("crlf"); check = attr_check_initl("crlf", "ident", NULL);
check[1].attr = git_attr("ident");
} }
------------ ------------
. Call `git_check_attr()` with the prepared array of `struct git_attr_check`: . Call `git_check_attr()` with the prepared `struct attr_check`:
------------ ------------
const char *path; const char *path;
setup_check(); setup_check();
git_check_attr(path, ARRAY_SIZE(check), check); git_check_attr(path, check);
------------ ------------
. Act on `.value` member of the result, left in `check[]`: . Act on `.value` member of the result, left in `check->items[]`:
------------ ------------
const char *value = check[0].value; const char *value = check->items[0].value;
if (ATTR_TRUE(value)) { if (ATTR_TRUE(value)) {
The attribute is Set, by listing only the name of the The attribute is Set, by listing only the name of the
@ -109,20 +116,39 @@ static void setup_check(void)
} }
------------ ------------
To see how attributes in argv[] are set for different paths, only
the first step in the above would be different.
------------
static struct attr_check *check;
static void setup_check(const char **argv)
{
check = attr_check_alloc();
while (*argv) {
struct git_attr *attr = git_attr(*argv);
attr_check_append(check, attr);
argv++;
}
}
------------
Querying All Attributes Querying All Attributes
----------------------- -----------------------
To get the values of all attributes associated with a file: To get the values of all attributes associated with a file:
* Call `git_all_attrs()`, which returns an array of `git_attr_check` * Prepare an empty `attr_check` structure by calling
structures. `attr_check_alloc()`.
* Iterate over the `git_attr_check` array to examine the attribute * Call `git_all_attrs()`, which populates the `attr_check`
names and values. The name of the attribute described by a with the attributes attached to the path.
`git_attr_check` object can be retrieved via
`git_attr_name(check[i].attr)`. (Please note that no items will be
returned for unset attributes, so `ATTR_UNSET()` will return false
for all returned `git_array_check` objects.)
* Free the `git_array_check` array. * Iterate over the `attr_check.items[]` array to examine
the attribute names and values. The name of the attribute
described by a `attr_check.items[]` object can be retrieved via
`git_attr_name(check->items[i].attr)`. (Please note that no items
will be returned for unset attributes, so `ATTR_UNSET()` will return
false for all returned `attr_check.items[]` objects.)
* Free the `attr_check` struct by calling `attr_check_free()`.

3
attr.c
View File

@ -890,7 +890,8 @@ static void collect_some_attrs(const char *path, int num,
rem = fill(path, pathlen, basename_offset, stk, rem); rem = fill(path, pathlen, basename_offset, stk, rem);
} }
int git_check_attrs(const char *path, int num, struct attr_check_item *check) static int git_check_attrs(const char *path, int num,
struct attr_check_item *check)
{ {
int i; int i;

1
attr.h
View File

@ -52,7 +52,6 @@ extern void attr_check_free(struct attr_check *check);
*/ */
extern const char *git_attr_name(const struct git_attr *); extern const char *git_attr_name(const struct git_attr *);
int git_check_attrs(const char *path, int, struct attr_check_item *);
extern int git_check_attr(const char *path, struct attr_check *check); extern int git_check_attr(const char *path, struct attr_check *check);
/* /*