@matheusbsbs / matheus-bagliano / commits / 3a1b3415d9
Fork 0

attr: move doc to attr.h

Move the documentation from Documentation/technical/api-gitattributes.txt to attr.h as it's easier for the developers to find the usage information beside the code instead of looking for it in another doc file. Also documentation/technical/api-gitattributes.txt is removed because the information it has is now redundant and it'll be hard to keep it up to date and synchronized with the documentation in the header file. Signed-off-by: Heba Waly <heba.waly@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Heba Waly committed Nov 17, 2019 at 21:04 UTC 3a1b3415d9330cbe3d3cce125b971dff96d0aa4c
3 files changed +135 -163
Documentation/technical/api-gitattributes.txt deleted
-154
@@ -1,154 +0,0 @@
1 -gitattributes API
2 -=================
3 -
4 -gitattributes mechanism gives a uniform way to associate various
5 -attributes to set of paths.
6 -
7 -
8 -Data Structure
9 ---------------
10 -
11 -`struct git_attr`::
12 -
13 - An attribute is an opaque object that is identified by its name.
14 - Pass the name to `git_attr()` function to obtain the object of
15 - this type. The internal representation of this structure is
16 - of no interest to the calling programs. The name of the
17 - attribute can be retrieved by calling `git_attr_name()`.
18 -
19 -`struct attr_check_item`::
20 -
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.
28 -
29 -
30 -Attribute Values
31 -----------------
32 -
33 -An attribute for a path can be in one of four states: Set, Unset,
34 -Unspecified or set to a string, and `.value` member of `struct
35 -attr_check_item` records it. There are three macros to check these:
36 -
37 -`ATTR_TRUE()`::
38 -
39 - Returns true if the attribute is Set for the path.
40 -
41 -`ATTR_FALSE()`::
42 -
43 - Returns true if the attribute is Unset for the path.
44 -
45 -`ATTR_UNSET()`::
46 -
47 - Returns true if the attribute is Unspecified for the path.
48 -
49 -If none of the above returns true, `.value` member points at a string
50 -value of the attribute for the path.
51 -
52 -
53 -Querying Specific Attributes
54 -----------------------------
55 -
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.
63 -
64 -* Call `git_check_attr()` to check the attributes for the path.
65 -
66 -* Inspect `attr_check` structure to see how each of the
67 - attribute in the array is defined for the path.
68 -
69 -
70 -Example
71 --------
72 -
73 -To see how attributes "crlf" and "ident" are set for different paths.
74 -
75 -. Prepare a `struct attr_check` with two elements (because
76 - we are checking two attributes):
77 -
78 -------------
79 -static struct attr_check *check;
80 -static void setup_check(void)
81 -{
82 - if (check)
83 - return; /* already done */
84 - check = attr_check_initl("crlf", "ident", NULL);
85 -}
86 -------------
87 -
88 -. Call `git_check_attr()` with the prepared `struct attr_check`:
89 -
90 -------------
91 - const char *path;
92 -
93 - setup_check();
94 - git_check_attr(path, check);
95 -------------
96 -
97 -. Act on `.value` member of the result, left in `check->items[]`:
98 -
99 -------------
100 - const char *value = check->items[0].value;
101 -
102 - if (ATTR_TRUE(value)) {
103 - The attribute is Set, by listing only the name of the
104 - attribute in the gitattributes file for the path.
105 - } else if (ATTR_FALSE(value)) {
106 - The attribute is Unset, by listing the name of the
107 - attribute prefixed with a dash - for the path.
108 - } else if (ATTR_UNSET(value)) {
109 - The attribute is neither set nor unset for the path.
110 - } else if (!strcmp(value, "input")) {
111 - If none of ATTR_TRUE(), ATTR_FALSE(), or ATTR_UNSET() is
112 - true, the value is a string set in the gitattributes
113 - file for the path by saying "attr=value".
114 - } else if (... other check using value as string ...) {
115 - ...
116 - }
117 -------------
118 -
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 -
135 -
136 -Querying All Attributes
137 ------------------------
138 -
139 -To get the values of all attributes associated with a file:
140 -
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.
146 -
147 -* Iterate over the `attr_check.items[]` array to examine
148 - the attribute names and values. The name of the attribute
149 - described by an `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.)
153 -
154 -* Free the `attr_check` struct by calling `attr_check_free()`.
attr.c
+1 -2
@@ -1,7 +1,6 @@
1 /*
2 * Handle git attributes. See gitattributes(5) for a description of
3 - * the file syntax, and Documentation/technical/api-gitattributes.txt
4 - * for a description of the API.
3 + * the file syntax, and attr.h for a description of the API.
4 *
5 * One basic design decision here is that we are not going to support
6 * an insanely large number of attributes.
attr.h
+134 -7
@@ -1,9 +1,121 @@
1 #ifndef ATTR_H
2 #define ATTR_H
3
4 +/**
5 + * gitattributes mechanism gives a uniform way to associate various attributes
6 + * to set of paths.
7 + *
8 + *
9 + * Querying Specific Attributes
10 + * ----------------------------
11 + *
12 + * - Prepare `struct attr_check` using attr_check_initl() function, enumerating
13 + * the names of attributes whose values you are interested in, terminated with
14 + * a NULL pointer. Alternatively, an empty `struct attr_check` can be
15 + * prepared by calling `attr_check_alloc()` function and then attributes you
16 + * want to ask about can be added to it with `attr_check_append()` function.
17 + *
18 + * - Call `git_check_attr()` to check the attributes for the path.
19 + *
20 + * - Inspect `attr_check` structure to see how each of the attribute in the
21 + * array is defined for the path.
22 + *
23 + *
24 + * Example
25 + * -------
26 + *
27 + * To see how attributes "crlf" and "ident" are set for different paths.
28 + *
29 + * - Prepare a `struct attr_check` with two elements (because we are checking
30 + * two attributes):
31 + *
32 + * ------------
33 + * static struct attr_check *check;
34 + * static void setup_check(void)
35 + * {
36 + * if (check)
37 + * return; // already done
38 + * check = attr_check_initl("crlf", "ident", NULL);
39 + * }
40 + * ------------
41 + *
42 + * - Call `git_check_attr()` with the prepared `struct attr_check`:
43 + *
44 + * ------------
45 + * const char *path;
46 + *
47 + * setup_check();
48 + * git_check_attr(path, check);
49 + * ------------
50 + *
51 + * - Act on `.value` member of the result, left in `check->items[]`:
52 + *
53 + * ------------
54 + * const char *value = check->items[0].value;
55 + *
56 + * if (ATTR_TRUE(value)) {
57 + * The attribute is Set, by listing only the name of the
58 + * attribute in the gitattributes file for the path.
59 + * } else if (ATTR_FALSE(value)) {
60 + * The attribute is Unset, by listing the name of the
61 + * attribute prefixed with a dash - for the path.
62 + * } else if (ATTR_UNSET(value)) {
63 + * The attribute is neither set nor unset for the path.
64 + * } else if (!strcmp(value, "input")) {
65 + * If none of ATTR_TRUE(), ATTR_FALSE(), or ATTR_UNSET() is
66 + * true, the value is a string set in the gitattributes
67 + * file for the path by saying "attr=value".
68 + * } else if (... other check using value as string ...) {
69 + * ...
70 + * }
71 + * ------------
72 + *
73 + * To see how attributes in argv[] are set for different paths, only
74 + * the first step in the above would be different.
75 + *
76 + * ------------
77 + * static struct attr_check *check;
78 + * static void setup_check(const char **argv)
79 + * {
80 + * check = attr_check_alloc();
81 + * while (*argv) {
82 + * struct git_attr *attr = git_attr(*argv);
83 + * attr_check_append(check, attr);
84 + * argv++;
85 + * }
86 + * }
87 + * ------------
88 + *
89 + *
90 + * Querying All Attributes
91 + * -----------------------
92 + *
93 + * To get the values of all attributes associated with a file:
94 + *
95 + * - Prepare an empty `attr_check` structure by calling `attr_check_alloc()`.
96 + *
97 + * - Call `git_all_attrs()`, which populates the `attr_check` with the
98 + * attributes attached to the path.
99 + *
100 + * - Iterate over the `attr_check.items[]` array to examine the attribute
101 + * names and values. The name of the attribute described by an
102 + * `attr_check.items[]` object can be retrieved via
103 + * `git_attr_name(check->items[i].attr)`. (Please note that no items will be
104 + * returned for unset attributes, so `ATTR_UNSET()` will return false for all
105 + * returned `attr_check.items[]` objects.)
106 + *
107 + * - Free the `attr_check` struct by calling `attr_check_free()`.
108 + */
109 +
110 struct index_state;
111
6 -/* An attribute is a pointer to this opaque structure */
112 +/**
113 + * An attribute is an opaque object that is identified by its name. Pass the
114 + * name to `git_attr()` function to obtain the object of this type.
115 + * The internal representation of this structure is of no interest to the
116 + * calling programs. The name of the attribute can be retrieved by calling
117 + * `git_attr_name()`.
118 + */
119 struct git_attr;
120
121 /* opaque structures used internally for attribute collection */
@@ -21,21 +133,36 @@ const struct git_attr *git_attr(const char *);
133 extern const char git_attr__true[];
134 extern const char git_attr__false[];
135
24 -/* For public to check git_attr_check results */
136 +/**
137 + * Attribute Values
138 + * ----------------
139 + *
140 + * An attribute for a path can be in one of four states: Set, Unset, Unspecified
141 + * or set to a string, and `.value` member of `struct attr_check_item` records
142 + * it. The three macros check these, if none of them returns true, `.value`
143 + * member points at a string value of the attribute for the path.
144 + */
145 +
146 +/* Returns true if the attribute is Set for the path. */
147 #define ATTR_TRUE(v) ((v) == git_attr__true)
148 +
149 +/* Returns true if the attribute is Unset for the path. */
150 #define ATTR_FALSE(v) ((v) == git_attr__false)
151 +
152 +/* Returns true if the attribute is Unspecified for the path. */
153 #define ATTR_UNSET(v) ((v) == NULL)
154
29 -/*
30 - * Send one or more git_attr_check to git_check_attrs(), and
31 - * each 'value' member tells what its value is.
32 - * Unset one is returned as NULL.
33 - */
155 +/* This structure represents one attribute and its value. */
156 struct attr_check_item {
157 const struct git_attr *attr;
158 const char *value;
159 };
160
161 +/**
162 + * This structure represents a collection of `attr_check_item`. It is passed to
163 + * `git_check_attr()` function, specifying the attributes to check, and
164 + * receives their values.
165 + */
166 struct attr_check {
167 int nr;
168 int alloc;