Skip to content

Commit ee548df

Browse files
mhaggergitster
authored andcommitted
Allow querying all attributes on a file
Add a function, git_all_attrs(), that reports on all attributes that are set on a path. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7373eab commit ee548df

File tree

3 files changed

+67
-14
lines changed

3 files changed

+67
-14
lines changed

Documentation/technical/api-gitattributes.txt

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,6 @@ Data Structure
2222
to `git_checkattr()` function, and receives the results.
2323

2424

25-
Calling Sequence
26-
----------------
27-
28-
* Prepare an array of `struct git_attr_check` to define the list of
29-
attributes you would want to check. To populate this array, you would
30-
need to define necessary attributes by calling `git_attr()` function.
31-
32-
* Call git_checkattr() to check the attributes for the path.
33-
34-
* Inspect `git_attr_check` structure to see how each of the attribute in
35-
the array is defined for the path.
36-
37-
3825
Attribute Values
3926
----------------
4027

@@ -58,6 +45,19 @@ If none of the above returns true, `.value` member points at a string
5845
value of the attribute for the path.
5946

6047

48+
Querying Specific Attributes
49+
----------------------------
50+
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.
54+
55+
* Call `git_checkattr()` to check the attributes for the path.
56+
57+
* Inspect `git_attr_check` structure to see how each of the attribute in
58+
the array is defined for the path.
59+
60+
6161
Example
6262
-------
6363

@@ -109,4 +109,20 @@ static void setup_check(void)
109109
}
110110
------------
111111

112-
(JC)
112+
113+
Querying All Attributes
114+
-----------------------
115+
116+
To get the values of all attributes associated with a file:
117+
118+
* Call `git_all_attrs()`, which returns an array of `git_attr_check`
119+
structures.
120+
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.)
127+
128+
* Free the `git_array_check` array.

attr.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,34 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
747747
return 0;
748748
}
749749

750+
int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
751+
{
752+
int i, count, j;
753+
754+
collect_all_attrs(path);
755+
756+
/* Count the number of attributes that are set. */
757+
count = 0;
758+
for (i = 0; i < attr_nr; i++) {
759+
const char *value = check_all_attr[i].value;
760+
if (value != ATTR__UNSET && value != ATTR__UNKNOWN)
761+
++count;
762+
}
763+
*num = count;
764+
*check = xmalloc(sizeof(**check) * count);
765+
j = 0;
766+
for (i = 0; i < attr_nr; i++) {
767+
const char *value = check_all_attr[i].value;
768+
if (value != ATTR__UNSET && value != ATTR__UNKNOWN) {
769+
(*check)[j].attr = check_all_attr[i].attr;
770+
(*check)[j].value = value;
771+
++j;
772+
}
773+
}
774+
775+
return 0;
776+
}
777+
750778
void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
751779
{
752780
enum git_attr_direction old = direction;

attr.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ char *git_attr_name(struct git_attr *);
3838

3939
int git_checkattr(const char *path, int, struct git_attr_check *);
4040

41+
/*
42+
* Retrieve all attributes that apply to the specified path. *num
43+
* will be set the the number of attributes on the path; **check will
44+
* be set to point at a newly-allocated array of git_attr_check
45+
* objects describing the attributes and their values. *check must be
46+
* free()ed by the caller.
47+
*/
48+
int git_all_attrs(const char *path, int *num, struct git_attr_check **check);
49+
4150
enum git_attr_direction {
4251
GIT_ATTR_CHECKIN,
4352
GIT_ATTR_CHECKOUT,

0 commit comments

Comments
 (0)