Code

ab3a84d2f716e7f43209b8fda10f8713facce008
[git.git] / Documentation / technical / api-gitattributes.txt
1 gitattributes API
2 =================
4 gitattributes mechanism gives a uniform way to associate various
5 attributes to set of paths.
8 Data Structure
9 --------------
11 `struct git_attr`::
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()`.
19 `struct git_attr_check`::
21         This structure represents a set of attributes to check in a call
22         to `git_checkattr()` function, and receives the results.
25 Calling Sequence
26 ----------------
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.
32 * Call git_checkattr() to check the attributes for the path.
34 * Inspect `git_attr_check` structure to see how each of the attribute in
35   the array is defined for the path.
38 Attribute Values
39 ----------------
41 An attribute for a path can be in one of four states: Set, Unset,
42 Unspecified or set to a string, and `.value` member of `struct
43 git_attr_check` records it.  There are three macros to check these:
45 `ATTR_TRUE()`::
47         Returns true if the attribute is Set for the path.
49 `ATTR_FALSE()`::
51         Returns true if the attribute is Unset for the path.
53 `ATTR_UNSET()`::
55         Returns true if the attribute is Unspecified for the path.
57 If none of the above returns true, `.value` member points at a string
58 value of the attribute for the path.
61 Example
62 -------
64 To see how attributes "crlf" and "indent" are set for different paths.
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()`:
70 ------------
71 static struct git_attr_check check[2];
72 static void setup_check(void)
73 {
74         if (check[0].attr)
75                 return; /* already done */
76         check[0].attr = git_attr("crlf");
77         check[1].attr = git_attr("ident");
78 }
79 ------------
81 . Call `git_checkattr()` with the prepared array of `struct git_attr_check`:
83 ------------
84         const char *path;
86         setup_check();
87         git_checkattr(path, ARRAY_SIZE(check), check);
88 ------------
90 . Act on `.value` member of the result, left in `check[]`:
92 ------------
93         const char *value = check[0].value;
95         if (ATTR_TRUE(value)) {
96                 The attribute is Set, by listing only the name of the
97                 attribute in the gitattributes file for the path.
98         } else if (ATTR_FALSE(value)) {
99                 The attribute is Unset, by listing the name of the
100                 attribute prefixed with a dash - for the path.
101         } else if (ATTR_UNSET(value)) {
102                 The attribute is not set nor unset for the path.
103         } else if (!strcmp(value, "input")) {
104                 If none of ATTR_TRUE(), ATTR_FALSE(), or ATTR_UNSET() is
105                 true, the value is a string set in the gitattributes
106                 file for the path by saying "attr=value".
107         } else if (... other check using value as string ...) {
108                 ...
109         }
110 ------------
112 (JC)