@matheusbsbs / matheus-bagliano / commits / d0bfd026a8
Fork 0

Add basic infrastructure to assign attributes to paths

This adds the basic infrastructure to assign attributes to paths, in a way similar to what the exclusion mechanism does based on $GIT_DIR/info/exclude and .gitignore files. An attribute is just a simple string that does not contain any whitespace. They can be specified in $GIT_DIR/info/attributes file, and .gitattributes file in each directory. Each line in these files defines a pattern matching rule. Similar to the exclusion mechanism, a later match overrides an earlier match in the same file, and entries from .gitattributes file in the same directory takes precedence over the ones from parent directories. Lines in $GIT_DIR/info/attributes file are used as the lowest precedence default rules. A line is either a comment (an empty line, or a line that begins with a '#'), or a rule, which is a whitespace separated list of tokens. The first token on the line is a shell glob pattern. The rest are names of attributes, each of which can optionally be prefixed with '!'. Such a line means "if a path matches this glob, this attribute is set (or unset -- if the attribute name is prefixed with '!'). For glob matching, the same "if the pattern does not have a slash in it, the basename of the path is matched with fnmatch(3) against the pattern, otherwise, the path is matched with the pattern with FNM_PATHNAME" rule as the exclusion mechanism is used. This does not define what an attribute means. Tying an attribute to various effects it has on git operation for paths that have it will be specified separately. Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 12, 2007 at 01:07 UTC d0bfd026a8241d544c339944976927b388d61a5e
8 files changed +453 -2
.gitignore
+1
@@ -16,6 +16,7 @@ git-blame
16 git-branch
17 git-bundle
18 git-cat-file
19 +git-check-attr
20 git-check-ref-format
21 git-checkout
22 git-checkout-index
Makefile
+3 -2
@@ -283,7 +283,7 @@ LIB_H = \
283 diff.h object.h pack.h pkt-line.h quote.h refs.h list-objects.h sideband.h \
284 run-command.h strbuf.h tag.h tree.h git-compat-util.h revision.h \
285 tree-walk.h log-tree.h dir.h path-list.h unpack-trees.h builtin.h \
286 - utf8.h reflog-walk.h patch-ids.h
286 + utf8.h reflog-walk.h attr.h
287
288 DIFF_OBJS = \
289 diff.o diff-lib.o diffcore-break.o diffcore-order.o \
@@ -305,7 +305,7 @@ LIB_OBJS = \
305 write_or_die.o trace.o list-objects.o grep.o match-trees.o \
306 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
307 color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
308 - convert.o
308 + convert.o attr.o
309
310 BUILTIN_OBJS = \
311 builtin-add.o \
@@ -316,6 +316,7 @@ BUILTIN_OBJS = \
316 builtin-branch.o \
317 builtin-bundle.o \
318 builtin-cat-file.o \
319 + builtin-check-attr.o \
320 builtin-checkout-index.o \
321 builtin-check-ref-format.o \
322 builtin-commit-tree.o \
attr.c new
+380
@@ -0,0 +1,380 @@
1 +#include "cache.h"
2 +#include "attr.h"
3 +
4 +/*
5 + * The basic design decision here is that we are not going to have
6 + * insanely large number of attributes.
7 + *
8 + * This is a randomly chosen prime.
9 + */
10 +#define HASHSIZE 257
11 +
12 +#ifndef DEBUG_ATTR
13 +#define DEBUG_ATTR 0
14 +#endif
15 +
16 +struct git_attr {
17 + struct git_attr *next;
18 + unsigned h;
19 + char name[FLEX_ARRAY];
20 +};
21 +
22 +static struct git_attr *(git_attr_hash[HASHSIZE]);
23 +
24 +static unsigned hash_name(const char *name, int namelen)
25 +{
26 + unsigned val = 0;
27 + unsigned char c;
28 +
29 + while (namelen--) {
30 + c = *name++;
31 + val = ((val << 7) | (val >> 22)) ^ c;
32 + }
33 + return val;
34 +}
35 +
36 +struct git_attr *git_attr(const char *name, int len)
37 +{
38 + unsigned hval = hash_name(name, len);
39 + unsigned pos = hval % HASHSIZE;
40 + struct git_attr *a;
41 +
42 + for (a = git_attr_hash[pos]; a; a = a->next) {
43 + if (a->h == hval &&
44 + !memcmp(a->name, name, len) && !a->name[len])
45 + return a;
46 + }
47 +
48 + a = xmalloc(sizeof(*a) + len + 1);
49 + memcpy(a->name, name, len);
50 + a->name[len] = 0;
51 + a->h = hval;
52 + a->next = git_attr_hash[pos];
53 + git_attr_hash[pos] = a;
54 + return a;
55 +}
56 +
57 +/*
58 + * .gitattributes file is one line per record, each of which is
59 + *
60 + * (1) glob pattern.
61 + * (2) whitespace
62 + * (3) whitespace separated list of attribute names, each of which
63 + * could be prefixed with '!' to mean "not set".
64 + */
65 +
66 +struct attr_state {
67 + int unset;
68 + struct git_attr *attr;
69 +};
70 +
71 +struct match_attr {
72 + char *pattern;
73 + unsigned num_attr;
74 + struct attr_state state[FLEX_ARRAY];
75 +};
76 +
77 +static const char blank[] = " \t\r\n";
78 +
79 +static struct match_attr *parse_attr_line(const char *line)
80 +{
81 + int namelen;
82 + int num_attr;
83 + const char *cp, *name;
84 + struct match_attr *res = res;
85 + int pass;
86 +
87 + cp = line + strspn(line, blank);
88 + if (!*cp || *cp == '#')
89 + return NULL;
90 + name = cp;
91 + namelen = strcspn(name, blank);
92 +
93 + for (pass = 0; pass < 2; pass++) {
94 + /* pass 0 counts and allocates, pass 1 fills */
95 + num_attr = 0;
96 + cp = name + namelen;
97 + cp = cp + strspn(cp, blank);
98 + while (*cp) {
99 + const char *ep;
100 + ep = cp + strcspn(cp, blank);
101 + if (pass) {
102 + struct attr_state *e;
103 +
104 + e = &(res->state[num_attr]);
105 + if (*cp == '!') {
106 + e->unset = 1;
107 + cp++;
108 + }
109 + e->attr = git_attr(cp, ep - cp);
110 + }
111 + num_attr++;
112 + cp = ep + strspn(ep, blank);
113 + }
114 + if (pass)
115 + break;
116 + res = xcalloc(1,
117 + sizeof(*res) +
118 + sizeof(struct attr_state) * num_attr +
119 + namelen + 1);
120 + res->pattern = (char*)&(res->state[num_attr]);
121 + memcpy(res->pattern, name, namelen);
122 + res->pattern[namelen] = 0;
123 + res->num_attr = num_attr;
124 + }
125 + return res;
126 +}
127 +
128 +/*
129 + * Like info/exclude and .gitignore, the attribute information can
130 + * come from many places.
131 + *
132 + * (1) .gitattribute file of the same directory;
133 + * (2) .gitattribute file of the parent directory if (1) does not have any match;
134 + * this goes recursively upwards, just like .gitignore
135 + * (3) perhaps $GIT_DIR/info/attributes, as the final fallback.
136 + *
137 + * In the same file, later entries override the earlier match, so in the
138 + * global list, we would have entries from info/attributes the earliest
139 + * (reading the file from top to bottom), .gitattribute of the root
140 + * directory (again, reading the file from top to bottom) down to the
141 + * current directory, and then scan the list backwards to find the first match.
142 + * This is exactly the same as what excluded() does in dir.c to deal with
143 + * .gitignore
144 + */
145 +
146 +static struct attr_stack {
147 + struct attr_stack *prev;
148 + char *origin;
149 + unsigned num_matches;
150 + struct match_attr **attrs;
151 +} *attr_stack;
152 +
153 +static void free_attr_elem(struct attr_stack *e)
154 +{
155 + int i;
156 + free(e->origin);
157 + for (i = 0; i < e->num_matches; i++)
158 + free(e->attrs[i]);
159 + free(e);
160 +}
161 +
162 +static const char *builtin_attr[] = {
163 + NULL,
164 +};
165 +
166 +static struct attr_stack *read_attr_from_array(const char **list)
167 +{
168 + struct attr_stack *res;
169 + const char *line;
170 +
171 + res = xcalloc(1, sizeof(*res));
172 + while ((line = *(list++)) != NULL) {
173 + struct match_attr *a = parse_attr_line(line);
174 + if (!a)
175 + continue;
176 + res->attrs = xrealloc(res->attrs, res->num_matches + 1);
177 + res->attrs[res->num_matches++] = a;
178 + }
179 + return res;
180 +}
181 +
182 +static struct attr_stack *read_attr_from_file(const char *path)
183 +{
184 + FILE *fp;
185 + struct attr_stack *res;
186 + char buf[2048];
187 +
188 + res = xcalloc(1, sizeof(*res));
189 + fp = fopen(path, "r");
190 + if (!fp)
191 + return res;
192 +
193 + while (fgets(buf, sizeof(buf), fp)) {
194 + struct match_attr *a = parse_attr_line(buf);
195 + if (!a)
196 + continue;
197 + res->attrs = xrealloc(res->attrs, res->num_matches + 1);
198 + res->attrs[res->num_matches++] = a;
199 + }
200 + fclose(fp);
201 + return res;
202 +}
203 +
204 +#if DEBUG_ATTR
205 +static void debug_info(const char *what, struct attr_stack *elem)
206 +{
207 + fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
208 +}
209 +#define debug_push(a) debug_info("push", (a))
210 +#define debug_pop(a) debug_info("pop", (a))
211 +#else
212 +#define debug_push(a) do { ; } while (0)
213 +#define debug_pop(a) do { ; } while (0)
214 +#endif
215 +
216 +static void prepare_attr_stack(const char *path, int dirlen)
217 +{
218 + struct attr_stack *elem, *info;
219 + int len;
220 + char pathbuf[PATH_MAX];
221 +
222 + /*
223 + * At the bottom of the attribute stack is the built-in
224 + * set of attribute definitions. Then, contents from
225 + * .gitattribute files from directories closer to the
226 + * root to the ones in deeper directories are pushed
227 + * to the stack. Finally, at the very top of the stack
228 + * we always keep the contents of $GIT_DIR/info/attributes.
229 + *
230 + * When checking, we use entries from near the top of the
231 + * stack, preferring $GIT_DIR/info/attributes, then
232 + * .gitattributes in deeper directories to shallower ones,
233 + * and finally use the built-in set as the default.
234 + */
235 + if (!attr_stack) {
236 + elem = read_attr_from_array(builtin_attr);
237 + elem->origin = NULL;
238 + elem->prev = attr_stack;
239 + attr_stack = elem;
240 +
241 + elem = read_attr_from_file(GITATTRIBUTES_FILE);
242 + elem->origin = strdup("");
243 + elem->prev = attr_stack;
244 + attr_stack = elem;
245 + debug_push(elem);
246 +
247 + elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE));
248 + elem->origin = NULL;
249 + elem->prev = attr_stack;
250 + attr_stack = elem;
251 + }
252 +
253 + /*
254 + * Pop the "info" one that is always at the top of the stack.
255 + */
256 + info = attr_stack;
257 + attr_stack = info->prev;
258 +
259 + /*
260 + * Pop the ones from directories that are not the prefix of
261 + * the path we are checking.
262 + */
263 + while (attr_stack && attr_stack->origin) {
264 + int namelen = strlen(attr_stack->origin);
265 +
266 + elem = attr_stack;
267 + if (namelen <= dirlen &&
268 + !strncmp(elem->origin, path, namelen))
269 + break;
270 +
271 + debug_pop(elem);
272 + attr_stack = elem->prev;
273 + free_attr_elem(elem);
274 + }
275 +
276 + /*
277 + * Read from parent directories and push them down
278 + */
279 + while (1) {
280 + char *cp;
281 +
282 + len = strlen(attr_stack->origin);
283 + if (dirlen <= len)
284 + break;
285 + memcpy(pathbuf, path, dirlen);
286 + memcpy(pathbuf + dirlen, "/", 2);
287 + cp = strchr(pathbuf + len + 1, '/');
288 + strcpy(cp + 1, GITATTRIBUTES_FILE);
289 + elem = read_attr_from_file(pathbuf);
290 + *cp = '\0';
291 + elem->origin = strdup(pathbuf);
292 + elem->prev = attr_stack;
293 + attr_stack = elem;
294 + debug_push(elem);
295 + }
296 +
297 + /*
298 + * Finally push the "info" one at the top of the stack.
299 + */
300 + info->prev = attr_stack;
301 + attr_stack = info;
302 +}
303 +
304 +static int path_matches(const char *pathname, int pathlen,
305 + const char *pattern,
306 + const char *base, int baselen)
307 +{
308 + if (!strchr(pattern, '/')) {
309 + /* match basename */
310 + const char *basename = strrchr(pathname, '/');
311 + basename = basename ? basename + 1 : pathname;
312 + return (fnmatch(pattern, basename, 0) == 0);
313 + }
314 + /*
315 + * match with FNM_PATHNAME; the pattern has base implicitly
316 + * in front of it.
317 + */
318 + if (*pattern == '/')
319 + pattern++;
320 + if (pathlen < baselen ||
321 + (baselen && pathname[baselen - 1] != '/') ||
322 + strncmp(pathname, base, baselen))
323 + return 0;
324 + return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
325 +}
326 +
327 +/*
328 + * I do not like this at all. Only because we allow individual
329 + * attribute to be set or unset incrementally by individual
330 + * lines in .gitattribute files, we need to do this triple
331 + * loop which looks quite wasteful.
332 + */
333 +static int fill(const char *path, int pathlen,
334 + struct attr_stack *stk, struct git_attr_check *check,
335 + int num, int rem)
336 +{
337 + int i, j, k;
338 + const char *base = stk->origin ? stk->origin : "";
339 +
340 + for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
341 + struct match_attr *a = stk->attrs[i];
342 + if (path_matches(path, pathlen,
343 + a->pattern, base, strlen(base))) {
344 + for (j = 0; j < a->num_attr; j++) {
345 + struct git_attr *attr = a->state[j].attr;
346 + int set = !a->state[j].unset;
347 + for (k = 0; k < num; k++) {
348 + if (0 <= check[k].isset ||
349 + check[k].attr != attr)
350 + continue;
351 + check[k].isset = set;
352 + rem--;
353 + }
354 + }
355 + }
356 + }
357 + return rem;
358 +}
359 +
360 +int git_checkattr(const char *path, int num, struct git_attr_check *check)
361 +{
362 + struct attr_stack *stk;
363 + const char *cp;
364 + int dirlen, pathlen, i, rem;
365 +
366 + for (i = 0; i < num; i++)
367 + check[i].isset = -1;
368 +
369 + pathlen = strlen(path);
370 + cp = strrchr(path, '/');
371 + if (!cp)
372 + dirlen = 0;
373 + else
374 + dirlen = cp - path;
375 + prepare_attr_stack(path, dirlen);
376 + rem = num;
377 + for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
378 + rem = fill(path, pathlen, stk, check, num, rem);
379 + return 0;
380 +}
attr.h new
+16
@@ -0,0 +1,16 @@
1 +#ifndef ATTR_H
2 +#define ATTR_H
3 +
4 +/* An attribute is a pointer to this opaque structure */
5 +struct git_attr;
6 +
7 +struct git_attr *git_attr(const char *, int);
8 +
9 +struct git_attr_check {
10 + struct git_attr *attr;
11 + int isset;
12 +};
13 +
14 +int git_checkattr(const char *path, int, struct git_attr_check *);
15 +
16 +#endif /* ATTR_H */
builtin-check-attr.c new
+49
@@ -0,0 +1,49 @@
1 +#include "builtin.h"
2 +#include "attr.h"
3 +#include "quote.h"
4 +
5 +static const char check_attr_usage[] =
6 +"git-check-attr attr... [--] pathname...";
7 +
8 +int cmd_check_attr(int argc, const char **argv, const char *prefix)
9 +{
10 + struct git_attr_check *check;
11 + int cnt, i, doubledash;
12 +
13 + doubledash = -1;
14 + for (i = 1; doubledash < 0 && i < argc; i++) {
15 + if (!strcmp(argv[i], "--"))
16 + doubledash = i;
17 + }
18 +
19 + /* If there is no double dash, we handle only one attribute */
20 + if (doubledash < 0) {
21 + cnt = 1;
22 + doubledash = 1;
23 + } else
24 + cnt = doubledash - 1;
25 + doubledash++;
26 +
27 + if (cnt <= 0 || argc < doubledash)
28 + usage(check_attr_usage);
29 + check = xcalloc(cnt, sizeof(*check));
30 + for (i = 0; i < cnt; i++) {
31 + const char *name;
32 + name = argv[i + 1];
33 + check[i].attr = git_attr(name, strlen(name));
34 + }
35 +
36 + for (i = doubledash; i < argc; i++) {
37 + int j;
38 + if (git_checkattr(argv[i], cnt, check))
39 + die("git_checkattr died");
40 + for (j = 0; j < cnt; j++) {
41 + write_name_quoted("", 0, argv[i], 1, stdout);
42 + printf(": %s: %s\n", argv[j+1],
43 + (check[j].isset < 0) ? "unspecified" :
44 + (check[j].isset == 0) ? "unset" :
45 + "set");
46 + }
47 + }
48 + return 0;
49 +}
builtin.h
+1
@@ -22,6 +22,7 @@ extern int cmd_branch(int argc, const char **argv, const char *prefix);
22 extern int cmd_bundle(int argc, const char **argv, const char *prefix);
23 extern int cmd_cat_file(int argc, const char **argv, const char *prefix);
24 extern int cmd_checkout_index(int argc, const char **argv, const char *prefix);
25 +extern int cmd_check_attr(int argc, const char **argv, const char *prefix);
26 extern int cmd_check_ref_format(int argc, const char **argv, const char *prefix);
27 extern int cmd_cherry(int argc, const char **argv, const char *prefix);
28 extern int cmd_cherry_pick(int argc, const char **argv, const char *prefix);
cache.h
+2
@@ -151,6 +151,8 @@ enum object_type {
151 #define CONFIG_ENVIRONMENT "GIT_CONFIG"
152 #define CONFIG_LOCAL_ENVIRONMENT "GIT_CONFIG_LOCAL"
153 #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH"
154 +#define GITATTRIBUTES_FILE ".gitattributes"
155 +#define INFOATTRIBUTES_FILE "info/attributes"
156
157 extern int is_bare_repository_cfg;
158 extern int is_bare_repository(void);
git.c
+1
@@ -234,6 +234,7 @@ static void handle_internal_command(int argc, const char **argv, char **envp)
234 { "cat-file", cmd_cat_file, RUN_SETUP },
235 { "checkout-index", cmd_checkout_index, RUN_SETUP },
236 { "check-ref-format", cmd_check_ref_format },
237 + { "check-attr", cmd_check_attr, RUN_SETUP | NOT_BARE },
238 { "cherry", cmd_cherry, RUN_SETUP },
239 { "cherry-pick", cmd_cherry_pick, RUN_SETUP | NOT_BARE },
240 { "commit-tree", cmd_commit_tree, RUN_SETUP },