blob: 9d97eaa9dee99eef7e66072c4c51cfff3000bba3 [file] [log] [blame]
Junio C Hamano3dac5042007-12-15 08:40:541gitattributes API
2=================
3
4gitattributes mechanism gives a uniform way to associate various
5attributes to set of paths.
6
7
8Data Structure
9--------------
10
11`struct git_attr`::
12
13An attribute is an opaque object that is identified by its name.
14Pass the name and its length to `git_attr()` function to obtain
15the object of this type. The internal representation of this
16structure is of no interest to the calling programs.
17
18`struct git_attr_check`::
19
20This structure represents a set of attributes to check in a call
21to `git_checkattr()` function, and receives the results.
22
23
24Calling Sequence
25----------------
26
27* Prepare an array of `struct git_attr_check` to define the list of
28 attributes you would want to check. To populate this array, you would
29 need to define necessary attributes by calling `git_attr()` function.
30
31* Call git_checkattr() to check the attributes for the path.
32
33* Inspect `git_attr_check` structure to see how each of the attribute in
34 the array is defined for the path.
35
36
37Attribute Values
38----------------
39
40An attribute for a path can be in one of four states: Set, Unset,
41Unspecified or set to a string, and `.value` member of `struct
42git_attr_check` records it. There are three macros to check these:
43
44`ATTR_TRUE()`::
45
46Returns true if the attribute is Set for the path.
47
48`ATTR_FALSE()`::
49
50Returns true if the attribute is Unset for the path.
51
52`ATTR_UNSET()`::
53
54Returns true if the attribute is Unspecified for the path.
55
56If none of the above returns true, `.value` member points at a string
57value of the attribute for the path.
58
59
60Example
61-------
62
63To see how attributes "crlf" and "indent" are set for different paths.
64
65. Prepare an array of `struct git_attr_check` with two elements (because
66 we are checking two attributes). Initialize their `attr` member with
67 pointers to `struct git_attr` obtained by calling `git_attr()`:
68
69------------
70static struct git_attr_check check[2];
71static void setup_check(void)
72{
73if (check[0].attr)
74return; /* already done */
75check[0].attr = git_attr("crlf", 4);
76check[1].attr = git_attr("ident", 5);
77}
78------------
79
80. Call `git_checkattr()` with the prepared array of `struct git_attr_check`:
81
82------------
83const char *path;
84
85setup_check();
86git_checkattr(path, ARRAY_SIZE(check), check);
87------------
88
89. Act on `.value` member of the result, left in `check[]`:
90
91------------
92const char *value = check[0].value;
93
94if (ATTR_TRUE(value)) {
95The attribute is Set, by listing only the name of the
96attribute in the gitattributes file for the path.
97} else if (ATTR_FALSE(value)) {
98The attribute is Unset, by listing the name of the
99attribute prefixed with a dash - for the path.
100} else if (ATTR_UNSET(value)) {
101The attribute is not set nor unset for the path.
102} else if (!strcmp(value, "input")) {
103If none of ATTR_TRUE(), ATTR_FALSE(), or ATTR_UNSET() is
104true, the value is a string set in the gitattributes
105file for the path by saying "attr=value".
106} else if (... other check using value as string ...) {
107...
108}
109------------
110
111(JC)