Setup and Config
Getting and Creating Projects
Basic Snapshotting
Branching and Merging
Sharing and Updating Projects
Inspection and Comparison
Patching
Debugging
External Systems
Server Admin
Guides
- gitattributes
- Command-line interface conventions
- Everyday Git
- Frequently Asked Questions (FAQ)
- Glossary
- Hooks
- gitignore
- gitmodules
- Revisions
- Submodules
- Tutorial
- Workflows
- All guides...
Administration
Plumbing Commands
- 2.19.1 → 2.24.4 no changes
-
2.19.0
2018-09-10
- 2.14.6 → 2.18.5 no changes
-
2.13.7
2018-05-22
- 2.1.4 → 2.12.5 no changes
-
2.0.5
2014-12-17
gitattributes mechanism gives a uniform way to associate various attributes to set of paths.
Data Structure
structgit_attr-
An attribute is an opaque object that is identified by its name. Pass the name to
git_attr() function to obtain the object of this type. The internal representation of this structure is of no interest to the calling programs. The name of the attribute can be retrieved by callinggit_attr_name(). structattr_check_item-
This structure represents one attribute and its value.
structattr_check-
This structure represents a collection of
attr_check_item. It is passed togit_check_attr() function, specifying the attributes to check, and receives their values.
Attribute Values
An attribute for a path can be in one of four states: Set, Unset,
Unspecified or set to a string, and .value member of struct
attr_check_item records it. There are three macros to check these:
If none of the above returns true, .value member points at a string
value of the attribute for the path.
Querying Specific Attributes
-
Prepare
structattr_checkusing attr_check_initl() function, enumerating the names of attributes whose values you are interested in, terminated with a NULL pointer. Alternatively, an emptystructattr_checkcan be prepared by callingattr_check_alloc() function and then attributes you want to ask about can be added to it withattr_check_append() function. -
Call
git_check_attr() to check the attributes for the path. -
Inspect
attr_checkstructure to see how each of the attribute in the array is defined for the path.
Example
To see how attributes "crlf" and "ident" are set for different paths.
-
Prepare a
structattr_checkwith two elements (because we are checking two attributes):
static struct attr_check *check;
static void setup_check(void)
{
if (check)
return; /* already done */
check = attr_check_initl("crlf", "ident", NULL);
}
-
Call
git_check_attr() with the preparedstructattr_check:
const char *path; setup_check(); git_check_attr(path, check);
-
Act on
.valuemember of the result, left in check->items[]:
const char *value = check->items[0].value;
if (ATTR_TRUE(value)) {
The attribute is Set, by listing only the name of the
attribute in the gitattributes file for the path.
} else if (ATTR_FALSE(value)) {
The attribute is Unset, by listing the name of the
attribute prefixed with a dash - for the path.
} else if (ATTR_UNSET(value)) {
The attribute is neither set nor unset for the path.
} else if (!strcmp(value, "input")) {
If none of ATTR_TRUE(), ATTR_FALSE(), or ATTR_UNSET() is
true, the value is a string set in the gitattributes
file for the path by saying "attr=value".
} else if (... other check using value as string ...) {
...
}
To see how attributes in argv[] are set for different paths, only the first step in the above would be different.
static struct attr_check *check;
static void setup_check(const char **argv)
{
check = attr_check_alloc();
while (*argv) {
struct git_attr *attr = git_attr(*argv);
attr_check_append(check, attr);
argv++;
}
}
Querying All Attributes
To get the values of all attributes associated with a file:
-
Prepare an empty
attr_checkstructure by callingattr_check_alloc(). -
Call
git_all_attrs(), which populates theattr_checkwith the attributes attached to the path. -
Iterate over the
attr_check.items[] array to examine the attribute names and values. The name of the attribute described by anattr_check.items[] object can be retrieved via git_attr_name(check->items[i].attr). (Please note that no items will be returned for unset attributes, soATTR_UNSET() will return false for all returnedattr_check.items[] objects.) -
Free the
attr_checkstruct by callingattr_check_free().