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>
Michael Haggerty committed
Aug 4, 2011 at 06:36 UTC
ee548df3005d976d4e6a78b3b4454fed812ea28c
3 files changed
+67
-14
Documentation/technical/api-gitattributes.txt
+30
-14
@@ -22,19 +22,6 @@ Data Structure
22
to `git_checkattr()` function, and receives the results.
23
24
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
-
25
Attribute Values
26
----------------
27
@@ -58,6 +45,19 @@ If none of the above returns true, `.value` member points at a string
45
value of the attribute for the path.
46
47
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
+
61
Example
62
-------
63
@@ -109,4 +109,20 @@ static void setup_check(void)
109
}
110
------------
111
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
+28
@@ -747,6 +747,34 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
747
return 0;
748
}
749
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
+
778
void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
779
{
780
enum git_attr_direction old = direction;
attr.h
+9
@@ -38,6 +38,15 @@ char *git_attr_name(struct git_attr *);
38
39
int git_checkattr(const char *path, int, struct git_attr_check *);
40
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
+
50
enum git_attr_direction {
51
GIT_ATTR_CHECKIN,
52
GIT_ATTR_CHECKOUT,