Skip to content

Commit a04855b

Browse files
committed
Merge branch 'bw/attr'
The gitattributes machinery is being taught to work better in a multi-threaded environment. * bw/attr: (27 commits) attr: reformat git_attr_set_direction() function attr: push the bare repo check into read_attr() attr: store attribute stack in attr_check structure attr: tighten const correctness with git_attr and match_attr attr: remove maybe-real, maybe-macro from git_attr attr: eliminate global check_all_attr array attr: use hashmap for attribute dictionary attr: change validity check for attribute names to use positive logic attr: pass struct attr_check to collect_some_attrs attr: retire git_check_attrs() API attr: convert git_check_attrs() callers to use the new API attr: convert git_all_attrs() to use "struct attr_check" attr: (re)introduce git_check_attr() and struct attr_check attr: rename function and struct related to checking attributes attr.c: outline the future plans by heavily commenting Documentation: fix a typo attr.c: add push_stack() helper attr: support quoting pathname patterns in C style attr.c: plug small leak in parse_attr_line() attr.c: tighten constness around "git_attr" structure ...
2 parents fdeb89f + f0dd042 commit a04855b

File tree

14 files changed

+816
-444
lines changed

14 files changed

+816
-444
lines changed

Documentation/gitattributes.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ Each line in `gitattributes` file is of form:
2121
pattern attr1 attr2 ...
2222

2323
That is, a pattern followed by an attributes list,
24-
separated by whitespaces. When the pattern matches the
25-
path in question, the attributes listed on the line are given to
26-
the path.
24+
separated by whitespaces. Leading and trailing whitespaces are
25+
ignored. Lines that begin with '#' are ignored. Patterns
26+
that begin with a double quote are quoted in C style.
27+
When the pattern matches the path in question, the attributes
28+
listed on the line are given to the path.
2729

2830
Each attribute can be in one of these states for a given path:
2931

@@ -86,7 +88,7 @@ is either not set or empty, $HOME/.config/git/attributes is used instead.
8688
Attributes for all users on a system should be placed in the
8789
`$(prefix)/etc/gitattributes` file.
8890

89-
Sometimes you would need to override an setting of an attribute
91+
Sometimes you would need to override a setting of an attribute
9092
for a path to `Unspecified` state. This can be done by listing
9193
the name of the attribute prefixed with an exclamation point `!`.
9294

Documentation/technical/api-gitattributes.txt

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@ Data Structure
1616
of no interest to the calling programs. The name of the
1717
attribute can be retrieved by calling `git_attr_name()`.
1818

19-
`struct git_attr_check`::
19+
`struct attr_check_item`::
2020

21-
This structure represents a set of attributes to check in a call
22-
to `git_check_attr()` function, and receives the results.
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.
2328

2429

2530
Attribute Values
2631
----------------
2732

2833
An attribute for a path can be in one of four states: Set, Unset,
2934
Unspecified or set to a string, and `.value` member of `struct
30-
git_attr_check` records it. There are three macros to check these:
35+
attr_check_item` records it. There are three macros to check these:
3136

3237
`ATTR_TRUE()`::
3338

@@ -48,49 +53,51 @@ value of the attribute for the path.
4853
Querying Specific Attributes
4954
----------------------------
5055

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.
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.
5463

5564
* Call `git_check_attr()` to check the attributes for the path.
5665

57-
* Inspect `git_attr_check` structure to see how each of the attribute in
58-
the array is defined for the path.
66+
* Inspect `attr_check` structure to see how each of the
67+
attribute in the array is defined for the path.
5968

6069

6170
Example
6271
-------
6372

64-
To see how attributes "crlf" and "indent" are set for different paths.
73+
To see how attributes "crlf" and "ident" are set for different paths.
6574

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()`:
75+
. Prepare a `struct attr_check` with two elements (because
76+
we are checking two attributes):
6977

7078
------------
71-
static struct git_attr_check check[2];
79+
static struct attr_check *check;
7280
static void setup_check(void)
7381
{
74-
if (check[0].attr)
82+
if (check)
7583
return; /* already done */
76-
check[0].attr = git_attr("crlf");
77-
check[1].attr = git_attr("ident");
84+
check = attr_check_initl("crlf", "ident", NULL);
7885
}
7986
------------
8087

81-
. Call `git_check_attr()` with the prepared array of `struct git_attr_check`:
88+
. Call `git_check_attr()` with the prepared `struct attr_check`:
8289

8390
------------
8491
const char *path;
8592

8693
setup_check();
87-
git_check_attr(path, ARRAY_SIZE(check), check);
94+
git_check_attr(path, check);
8895
------------
8996

90-
. Act on `.value` member of the result, left in `check[]`:
97+
. Act on `.value` member of the result, left in `check->items[]`:
9198

9299
------------
93-
const char *value = check[0].value;
100+
const char *value = check->items[0].value;
94101

95102
if (ATTR_TRUE(value)) {
96103
The attribute is Set, by listing only the name of the
@@ -109,20 +116,39 @@ static void setup_check(void)
109116
}
110117
------------
111118

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+
112135

113136
Querying All Attributes
114137
-----------------------
115138

116139
To get the values of all attributes associated with a file:
117140

118-
* Call `git_all_attrs()`, which returns an array of `git_attr_check`
119-
structures.
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.
120146

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.)
147+
* Iterate over the `attr_check.items[]` array to examine
148+
the attribute names and values. The name of the attribute
149+
described by a `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.)
127153

128-
* Free the `git_array_check` array.
154+
* Free the `attr_check` struct by calling `attr_check_free()`.

archive.c

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,6 @@ void *sha1_file_to_archive(const struct archiver_args *args,
8787
return buffer;
8888
}
8989

90-
static void setup_archive_check(struct git_attr_check *check)
91-
{
92-
static struct git_attr *attr_export_ignore;
93-
static struct git_attr *attr_export_subst;
94-
95-
if (!attr_export_ignore) {
96-
attr_export_ignore = git_attr("export-ignore");
97-
attr_export_subst = git_attr("export-subst");
98-
}
99-
check[0].attr = attr_export_ignore;
100-
check[1].attr = attr_export_subst;
101-
}
102-
10390
struct directory {
10491
struct directory *up;
10592
struct object_id oid;
@@ -120,10 +107,10 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
120107
void *context)
121108
{
122109
static struct strbuf path = STRBUF_INIT;
110+
static struct attr_check *check;
123111
struct archiver_context *c = context;
124112
struct archiver_args *args = c->args;
125113
write_archive_entry_fn_t write_entry = c->write_entry;
126-
struct git_attr_check check[2];
127114
const char *path_without_prefix;
128115
int err;
129116

@@ -137,11 +124,12 @@ static int write_archive_entry(const unsigned char *sha1, const char *base,
137124
strbuf_addch(&path, '/');
138125
path_without_prefix = path.buf + args->baselen;
139126

140-
setup_archive_check(check);
141-
if (!git_check_attr(path_without_prefix, ARRAY_SIZE(check), check)) {
142-
if (ATTR_TRUE(check[0].value))
127+
if (!check)
128+
check = attr_check_initl("export-ignore", "export-subst", NULL);
129+
if (!git_check_attr(path_without_prefix, check)) {
130+
if (ATTR_TRUE(check->items[0].value))
143131
return 0;
144-
args->convert = ATTR_TRUE(check[1].value);
132+
args->convert = ATTR_TRUE(check->items[1].value);
145133
}
146134

147135
if (S_ISDIR(mode) || S_ISGITLINK(mode)) {

0 commit comments

Comments
 (0)