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

attribute macro support

This adds "attribute macros" (for lack of better name). So far, we have low-level attributes such as crlf and diff, which are defined in operational terms --- setting or unsetting them on a particular path directly affects what is done to the path. For example, in order to decline diffs or crlf conversions on a binary blob, no diffs on PostScript files, and treat all other files normally, you would have something like these: * diff crlf *.ps !diff proprietary.o !diff !crlf That is fine as the operation goes, but gets unwieldy rather rapidly, when we start adding more low-level attributes that are defined in operational terms. A near-term example of such an attribute would be 'merge-3way' which would control if git should attempt the usual 3-way file-level merge internally, or leave merging to a specialized external program of user's choice. When it is added, we do _not_ want to force the users to update the above to: * diff crlf merge-3way *.ps !diff proprietary.o !diff !crlf !merge-3way The way this patch solves this issue is to realize that the attributes the user is assigning to paths are not defined in terms of operations but in terms of what they are. All of the three low-level attributes usually make sense for most of the files that sane SCM users have git operate on (these files are typically called "text'). Only a few cases, such as binary blob, need exception to decline the "usual treatment given to text files" -- and people mark them as "binary". So this allows the $GIT_DIR/info/alternates and .gitattributes at the toplevel of the project to also specify attributes that assigns other attributes. The syntax is '[attr]' followed by an attribute name followed by a list of attribute names: [attr] binary !diff !crlf !merge-3way When "binary" attribute is set to a path, if the path has not got diff/crlf/merge-3way attribute set or unset by other rules, this rule unsets the three low-level attributes. It is expected that the user level .gitattributes will be expressed mostly in terms of attributes based on what the files are, and the above sample would become like this: (built-in attribute configuration) [attr] binary !diff !crlf !merge-3way * diff crlf merge-3way (project specific .gitattributes) proprietary.o binary (user preference $GIT_DIR/info/attributes) *.ps !diff There are a few caveats. * As described above, you can define these macros only in $GIT_DIR/info/attributes and toplevel .gitattributes. * There is no attempt to detect circular definition of macro attributes, and definitions are evaluated from bottom to top as usual to fill in other attributes that have not yet got values. The following would work as expected: [attr] text diff crlf [attr] ps text !diff *.ps ps while this would most likely not (I haven't tried): [attr] ps text !diff [attr] text diff crlf *.ps ps * When a macro says "[attr] A B !C", saying that a path does not have attribute A does not let you tell anything about attributes B or C. That is, given this: [attr] text diff crlf [attr] ps text !diff *.txt !ps path hello.txt, which would match "*.txt" pattern, would have "ps" attribute set to zero, but that does not make text attribute of hello.txt set to false (nor diff attribute set to true). Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Apr 14, 2007 at 08:54 UTC f48fd68887a03756658a46486a5dd1301c5a655f
2 files changed +132 -48
attr.c
+131 -48
@@ -16,9 +16,12 @@
16 struct git_attr {
17 struct git_attr *next;
18 unsigned h;
19 + int attr_nr;
20 char name[FLEX_ARRAY];
21 };
22 +static int attr_nr;
23
24 +static struct git_attr_check *check_all_attr;
25 static struct git_attr *(git_attr_hash[HASHSIZE]);
26
27 static unsigned hash_name(const char *name, int namelen)
@@ -50,7 +53,12 @@ struct git_attr *git_attr(const char *name, int len)
53 a->name[len] = 0;
54 a->h = hval;
55 a->next = git_attr_hash[pos];
56 + a->attr_nr = attr_nr++;
57 git_attr_hash[pos] = a;
58 +
59 + check_all_attr = xrealloc(check_all_attr,
60 + sizeof(*check_all_attr) * attr_nr);
61 + check_all_attr[a->attr_nr].attr = a;
62 return a;
63 }
64
@@ -69,26 +77,46 @@ struct attr_state {
77 };
78
79 struct match_attr {
72 - char *pattern;
80 + union {
81 + char *pattern;
82 + struct git_attr *attr;
83 + } u;
84 + char is_macro;
85 unsigned num_attr;
86 struct attr_state state[FLEX_ARRAY];
87 };
88
89 static const char blank[] = " \t\r\n";
90
79 -static struct match_attr *parse_attr_line(const char *line)
91 +static struct match_attr *parse_attr_line(const char *line, const char *src,
92 + int lineno, int macro_ok)
93 {
94 int namelen;
95 int num_attr;
96 const char *cp, *name;
97 struct match_attr *res = res;
98 int pass;
99 + int is_macro;
100
101 cp = line + strspn(line, blank);
102 if (!*cp || *cp == '#')
103 return NULL;
104 name = cp;
105 namelen = strcspn(name, blank);
106 + if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
107 + !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
108 + if (!macro_ok) {
109 + fprintf(stderr, "%s not allowed: %s:%d\n",
110 + name, src, lineno);
111 + return NULL;
112 + }
113 + is_macro = 1;
114 + name += strlen(ATTRIBUTE_MACRO_PREFIX);
115 + name += strspn(name, blank);
116 + namelen = strcspn(name, blank);
117 + }
118 + else
119 + is_macro = 0;
120
121 for (pass = 0; pass < 2; pass++) {
122 /* pass 0 counts and allocates, pass 1 fills */
@@ -113,13 +141,19 @@ static struct match_attr *parse_attr_line(const char *line)
141 }
142 if (pass)
143 break;
144 +
145 res = xcalloc(1,
146 sizeof(*res) +
147 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;
148 + (is_macro ? 0 : namelen + 1));
149 + if (is_macro)
150 + res->u.attr = git_attr(name, namelen);
151 + else {
152 + res->u.pattern = (char*)&(res->state[num_attr]);
153 + memcpy(res->u.pattern, name, namelen);
154 + res->u.pattern[namelen] = 0;
155 + }
156 + res->is_macro = is_macro;
157 res->num_attr = num_attr;
158 }
159 return res;
@@ -167,10 +201,13 @@ static struct attr_stack *read_attr_from_array(const char **list)
201 {
202 struct attr_stack *res;
203 const char *line;
204 + int lineno = 0;
205
206 res = xcalloc(1, sizeof(*res));
207 while ((line = *(list++)) != NULL) {
173 - struct match_attr *a = parse_attr_line(line);
208 + struct match_attr *a;
209 +
210 + a = parse_attr_line(line, "[builtin]", ++lineno, 1);
211 if (!a)
212 continue;
213 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
@@ -179,11 +216,12 @@ static struct attr_stack *read_attr_from_array(const char **list)
216 return res;
217 }
218
182 -static struct attr_stack *read_attr_from_file(const char *path)
219 +static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
220 {
221 FILE *fp;
222 struct attr_stack *res;
223 char buf[2048];
224 + int lineno = 0;
225
226 res = xcalloc(1, sizeof(*res));
227 fp = fopen(path, "r");
@@ -191,7 +229,9 @@ static struct attr_stack *read_attr_from_file(const char *path)
229 return res;
230
231 while (fgets(buf, sizeof(buf), fp)) {
194 - struct match_attr *a = parse_attr_line(buf);
232 + struct match_attr *a;
233 +
234 + a = parse_attr_line(buf, path, ++lineno, macro_ok);
235 if (!a)
236 continue;
237 res->attrs = xrealloc(res->attrs, res->num_matches + 1);
@@ -206,13 +246,42 @@ static void debug_info(const char *what, struct attr_stack *elem)
246 {
247 fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
248 }
249 +static void debug_set(const char *what, const char *match, struct git_attr *attr, int set)
250 +{
251 + fprintf(stderr, "%s: %s => %d (%s)\n",
252 + what, attr->name, set, match);
253 +}
254 #define debug_push(a) debug_info("push", (a))
255 #define debug_pop(a) debug_info("pop", (a))
256 #else
257 #define debug_push(a) do { ; } while (0)
258 #define debug_pop(a) do { ; } while (0)
259 +#define debug_set(a,b,c,d) do { ; } while (0)
260 #endif
261
262 +static void bootstrap_attr_stack(void)
263 +{
264 + if (!attr_stack) {
265 + struct attr_stack *elem;
266 +
267 + elem = read_attr_from_array(builtin_attr);
268 + elem->origin = NULL;
269 + elem->prev = attr_stack;
270 + attr_stack = elem;
271 +
272 + elem = read_attr_from_file(GITATTRIBUTES_FILE, 1);
273 + elem->origin = strdup("");
274 + elem->prev = attr_stack;
275 + attr_stack = elem;
276 + debug_push(elem);
277 +
278 + elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
279 + elem->origin = NULL;
280 + elem->prev = attr_stack;
281 + attr_stack = elem;
282 + }
283 +}
284 +
285 static void prepare_attr_stack(const char *path, int dirlen)
286 {
287 struct attr_stack *elem, *info;
@@ -232,23 +301,8 @@ static void prepare_attr_stack(const char *path, int dirlen)
301 * .gitattributes in deeper directories to shallower ones,
302 * and finally use the built-in set as the default.
303 */
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 - }
304 + if (!attr_stack)
305 + bootstrap_attr_stack();
306
307 /*
308 * Pop the "info" one that is always at the top of the stack.
@@ -286,7 +340,7 @@ static void prepare_attr_stack(const char *path, int dirlen)
340 memcpy(pathbuf + dirlen, "/", 2);
341 cp = strchr(pathbuf + len + 1, '/');
342 strcpy(cp + 1, GITATTRIBUTES_FILE);
289 - elem = read_attr_from_file(pathbuf);
343 + elem = read_attr_from_file(pathbuf, 0);
344 *cp = '\0';
345 elem->origin = strdup(pathbuf);
346 elem->prev = attr_stack;
@@ -324,31 +378,26 @@ static int path_matches(const char *pathname, int pathlen,
378 return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
379 }
380
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)
381 +static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
382 {
337 - int i, j, k;
383 const char *base = stk->origin ? stk->origin : "";
384 + int i, j;
385 + struct git_attr_check *check = check_all_attr;
386
387 for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
388 struct match_attr *a = stk->attrs[i];
389 + if (a->is_macro)
390 + continue;
391 if (path_matches(path, pathlen,
343 - a->pattern, base, strlen(base))) {
344 - for (j = 0; j < a->num_attr; j++) {
392 + a->u.pattern, base, strlen(base))) {
393 + for (j = 0; 0 < rem && j < a->num_attr; j++) {
394 struct git_attr *attr = a->state[j].attr;
395 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;
396 + int *n = &(check[attr->attr_nr].isset);
397 +
398 + if (*n < 0) {
399 + debug_set("fill", a->u.pattern, attr, set);
400 + *n = set;
401 rem--;
402 }
403 }
@@ -357,14 +406,41 @@ static int fill(const char *path, int pathlen,
406 return rem;
407 }
408
409 +static int macroexpand(struct attr_stack *stk, int rem)
410 +{
411 + int i, j;
412 + struct git_attr_check *check = check_all_attr;
413 +
414 + for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
415 + struct match_attr *a = stk->attrs[i];
416 + if (!a->is_macro)
417 + continue;
418 + if (check[a->u.attr->attr_nr].isset < 0)
419 + continue;
420 + for (j = 0; 0 < rem && j < a->num_attr; j++) {
421 + struct git_attr *attr = a->state[j].attr;
422 + int set = !a->state[j].unset;
423 + int *n = &(check[attr->attr_nr].isset);
424 +
425 + if (*n < 0) {
426 + debug_set("expand", a->u.attr->name, attr, set);
427 + *n = set;
428 + rem--;
429 + }
430 + }
431 + }
432 + return rem;
433 +}
434 +
435 int git_checkattr(const char *path, int num, struct git_attr_check *check)
436 {
437 struct attr_stack *stk;
438 const char *cp;
439 int dirlen, pathlen, i, rem;
440
366 - for (i = 0; i < num; i++)
367 - check[i].isset = -1;
441 + bootstrap_attr_stack();
442 + for (i = 0; i < attr_nr; i++)
443 + check_all_attr[i].isset = -1;
444
445 pathlen = strlen(path);
446 cp = strrchr(path, '/');
@@ -373,8 +449,15 @@ int git_checkattr(const char *path, int num, struct git_attr_check *check)
449 else
450 dirlen = cp - path;
451 prepare_attr_stack(path, dirlen);
376 - rem = num;
452 + rem = attr_nr;
453 + for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
454 + rem = fill(path, pathlen, stk, rem);
455 +
456 for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
378 - rem = fill(path, pathlen, stk, check, num, rem);
457 + rem = macroexpand(stk, rem);
458 +
459 + for (i = 0; i < num; i++)
460 + check[i].isset = check_all_attr[check[i].attr->attr_nr].isset;
461 +
462 return 0;
463 }
cache.h
+1
@@ -153,6 +153,7 @@ enum object_type {
153 #define EXEC_PATH_ENVIRONMENT "GIT_EXEC_PATH"
154 #define GITATTRIBUTES_FILE ".gitattributes"
155 #define INFOATTRIBUTES_FILE "info/attributes"
156 +#define ATTRIBUTE_MACRO_PREFIX "[attr]"
157
158 extern int is_bare_repository_cfg;
159 extern int is_bare_repository(void);