Michael Haggerty | 86ab7f0 | 2011-08-12 23:43:04 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Handle git attributes. See gitattributes(5) for a description of |
| 3 | * the file syntax, and Documentation/technical/api-gitattributes.txt |
| 4 | * for a description of the API. |
| 5 | * |
| 6 | * One basic design decision here is that we are not going to support |
| 7 | * an insanely large number of attributes. |
| 8 | */ |
| 9 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 10 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 11 | #include "cache.h" |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 12 | #include "exec_cmd.h" |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 13 | #include "attr.h" |
Brandon Casey | 6eba621 | 2011-10-11 10:53:31 -0500 | [diff] [blame] | 14 | #include "dir.h" |
Junio C Hamano | 27547e5 | 2015-04-16 10:48:58 -0700 | [diff] [blame] | 15 | #include "utf8.h" |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 16 | |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 17 | const char git_attr__true[] = "(builtin)true"; |
| 18 | const char git_attr__false[] = "\0(builtin)false"; |
| 19 | static const char git_attr__unknown[] = "(builtin)unknown"; |
| 20 | #define ATTR__TRUE git_attr__true |
| 21 | #define ATTR__FALSE git_attr__false |
| 22 | #define ATTR__UNSET NULL |
| 23 | #define ATTR__UNKNOWN git_attr__unknown |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 24 | |
Michael Haggerty | 86ab7f0 | 2011-08-12 23:43:04 +0200 | [diff] [blame] | 25 | /* This is a randomly chosen prime. */ |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 26 | #define HASHSIZE 257 |
| 27 | |
| 28 | #ifndef DEBUG_ATTR |
| 29 | #define DEBUG_ATTR 0 |
| 30 | #endif |
| 31 | |
| 32 | struct git_attr { |
| 33 | struct git_attr *next; |
| 34 | unsigned h; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 35 | int attr_nr; |
Nguyễn Thái Ngọc Duy | fad32bc | 2014-12-28 06:39:47 +0700 | [diff] [blame] | 36 | int maybe_macro; |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 37 | int maybe_real; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 38 | char name[FLEX_ARRAY]; |
| 39 | }; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 40 | static int attr_nr; |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 41 | static int cannot_trust_maybe_real; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 42 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 43 | static struct git_attr_check *check_all_attr; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 44 | static struct git_attr *(git_attr_hash[HASHSIZE]); |
| 45 | |
Michael Haggerty | 352404a | 2011-08-04 06:36:17 +0200 | [diff] [blame] | 46 | char *git_attr_name(struct git_attr *attr) |
| 47 | { |
| 48 | return attr->name; |
| 49 | } |
| 50 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 51 | static unsigned hash_name(const char *name, int namelen) |
| 52 | { |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 53 | unsigned val = 0, c; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 54 | |
| 55 | while (namelen--) { |
| 56 | c = *name++; |
| 57 | val = ((val << 7) | (val >> 22)) ^ c; |
| 58 | } |
| 59 | return val; |
| 60 | } |
| 61 | |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 62 | static int invalid_attr_name(const char *name, int namelen) |
| 63 | { |
| 64 | /* |
Michael Haggerty | d42453a | 2011-08-04 06:36:13 +0200 | [diff] [blame] | 65 | * Attribute name cannot begin with '-' and must consist of |
| 66 | * characters from [-A-Za-z0-9_.]. |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 67 | */ |
Michael Haggerty | c0b13b2 | 2011-08-04 06:36:14 +0200 | [diff] [blame] | 68 | if (namelen <= 0 || *name == '-') |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 69 | return -1; |
| 70 | while (namelen--) { |
| 71 | char ch = *name++; |
| 72 | if (! (ch == '-' || ch == '.' || ch == '_' || |
| 73 | ('0' <= ch && ch <= '9') || |
| 74 | ('a' <= ch && ch <= 'z') || |
| 75 | ('A' <= ch && ch <= 'Z')) ) |
| 76 | return -1; |
| 77 | } |
| 78 | return 0; |
| 79 | } |
| 80 | |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 81 | static struct git_attr *git_attr_internal(const char *name, int len) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 82 | { |
| 83 | unsigned hval = hash_name(name, len); |
| 84 | unsigned pos = hval % HASHSIZE; |
| 85 | struct git_attr *a; |
| 86 | |
| 87 | for (a = git_attr_hash[pos]; a; a = a->next) { |
| 88 | if (a->h == hval && |
| 89 | !memcmp(a->name, name, len) && !a->name[len]) |
| 90 | return a; |
| 91 | } |
| 92 | |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 93 | if (invalid_attr_name(name, len)) |
| 94 | return NULL; |
| 95 | |
Jeff King | 96ffc06 | 2016-02-22 17:44:32 -0500 | [diff] [blame] | 96 | FLEX_ALLOC_MEM(a, name, name, len); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 97 | a->h = hval; |
| 98 | a->next = git_attr_hash[pos]; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 99 | a->attr_nr = attr_nr++; |
Nguyễn Thái Ngọc Duy | fad32bc | 2014-12-28 06:39:47 +0700 | [diff] [blame] | 100 | a->maybe_macro = 0; |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 101 | a->maybe_real = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 102 | git_attr_hash[pos] = a; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 103 | |
René Scharfe | 2756ca4 | 2014-09-16 20:56:57 +0200 | [diff] [blame] | 104 | REALLOC_ARRAY(check_all_attr, attr_nr); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 105 | check_all_attr[a->attr_nr].attr = a; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 106 | check_all_attr[a->attr_nr].value = ATTR__UNKNOWN; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 107 | return a; |
| 108 | } |
| 109 | |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 110 | struct git_attr *git_attr(const char *name) |
| 111 | { |
| 112 | return git_attr_internal(name, strlen(name)); |
| 113 | } |
| 114 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 115 | /* What does a matched pattern decide? */ |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 116 | struct attr_state { |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 117 | struct git_attr *attr; |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 118 | const char *setto; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 121 | struct pattern { |
| 122 | const char *pattern; |
| 123 | int patternlen; |
| 124 | int nowildcardlen; |
Saurav Sachidanand | f870899 | 2016-03-01 22:32:59 +0530 | [diff] [blame] | 125 | unsigned flags; /* EXC_FLAG_* */ |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 126 | }; |
| 127 | |
Michael Haggerty | ba845b7 | 2011-08-12 23:43:05 +0200 | [diff] [blame] | 128 | /* |
| 129 | * One rule, as from a .gitattributes file. |
| 130 | * |
| 131 | * If is_macro is true, then u.attr is a pointer to the git_attr being |
| 132 | * defined. |
| 133 | * |
| 134 | * If is_macro is false, then u.pattern points at the filename pattern |
| 135 | * to which the rule applies. (The memory pointed to is part of the |
| 136 | * memory block allocated for the match_attr instance.) |
| 137 | * |
| 138 | * In either case, num_attr is the number of attributes affected by |
| 139 | * this rule, and state is an array listing them. The attributes are |
| 140 | * listed as they appear in the file (macros unexpanded). |
| 141 | */ |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 142 | struct match_attr { |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 143 | union { |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 144 | struct pattern pat; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 145 | struct git_attr *attr; |
| 146 | } u; |
| 147 | char is_macro; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 148 | unsigned num_attr; |
| 149 | struct attr_state state[FLEX_ARRAY]; |
| 150 | }; |
| 151 | |
| 152 | static const char blank[] = " \t\r\n"; |
| 153 | |
Michael Haggerty | d175129 | 2011-08-12 23:43:07 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Parse a whitespace-delimited attribute state (i.e., "attr", |
| 156 | * "-attr", "!attr", or "attr=value") from the string starting at src. |
| 157 | * If e is not NULL, write the results to *e. Return a pointer to the |
| 158 | * remainder of the string (with leading whitespace removed), or NULL |
| 159 | * if there was an error. |
| 160 | */ |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 161 | static const char *parse_attr(const char *src, int lineno, const char *cp, |
Michael Haggerty | d175129 | 2011-08-12 23:43:07 +0200 | [diff] [blame] | 162 | struct attr_state *e) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 163 | { |
| 164 | const char *ep, *equals; |
| 165 | int len; |
| 166 | |
| 167 | ep = cp + strcspn(cp, blank); |
| 168 | equals = strchr(cp, '='); |
| 169 | if (equals && ep < equals) |
| 170 | equals = NULL; |
| 171 | if (equals) |
| 172 | len = equals - cp; |
| 173 | else |
| 174 | len = ep - cp; |
Michael Haggerty | d175129 | 2011-08-12 23:43:07 +0200 | [diff] [blame] | 175 | if (!e) { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 176 | if (*cp == '-' || *cp == '!') { |
| 177 | cp++; |
| 178 | len--; |
| 179 | } |
| 180 | if (invalid_attr_name(cp, len)) { |
| 181 | fprintf(stderr, |
| 182 | "%.*s is not a valid attribute name: %s:%d\n", |
| 183 | len, cp, src, lineno); |
| 184 | return NULL; |
| 185 | } |
| 186 | } else { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 187 | if (*cp == '-' || *cp == '!') { |
| 188 | e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; |
| 189 | cp++; |
| 190 | len--; |
| 191 | } |
| 192 | else if (!equals) |
| 193 | e->setto = ATTR__TRUE; |
| 194 | else { |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 195 | e->setto = xmemdupz(equals + 1, ep - equals - 1); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 196 | } |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 197 | e->attr = git_attr_internal(cp, len); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 198 | } |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 199 | return ep + strspn(ep, blank); |
| 200 | } |
| 201 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 202 | static struct match_attr *parse_attr_line(const char *line, const char *src, |
| 203 | int lineno, int macro_ok) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 204 | { |
| 205 | int namelen; |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 206 | int num_attr, i; |
Michael Haggerty | 85c4a0d | 2011-08-12 23:43:08 +0200 | [diff] [blame] | 207 | const char *cp, *name, *states; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 208 | struct match_attr *res = NULL; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 209 | int is_macro; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 210 | |
| 211 | cp = line + strspn(line, blank); |
| 212 | if (!*cp || *cp == '#') |
| 213 | return NULL; |
| 214 | name = cp; |
| 215 | namelen = strcspn(name, blank); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 216 | if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 217 | starts_with(name, ATTRIBUTE_MACRO_PREFIX)) { |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 218 | if (!macro_ok) { |
| 219 | fprintf(stderr, "%s not allowed: %s:%d\n", |
| 220 | name, src, lineno); |
| 221 | return NULL; |
| 222 | } |
| 223 | is_macro = 1; |
| 224 | name += strlen(ATTRIBUTE_MACRO_PREFIX); |
| 225 | name += strspn(name, blank); |
| 226 | namelen = strcspn(name, blank); |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 227 | if (invalid_attr_name(name, namelen)) { |
| 228 | fprintf(stderr, |
| 229 | "%.*s is not a valid attribute name: %s:%d\n", |
| 230 | namelen, name, src, lineno); |
| 231 | return NULL; |
| 232 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | is_macro = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 236 | |
Michael Haggerty | 85c4a0d | 2011-08-12 23:43:08 +0200 | [diff] [blame] | 237 | states = name + namelen; |
| 238 | states += strspn(states, blank); |
| 239 | |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 240 | /* First pass to count the attr_states */ |
| 241 | for (cp = states, num_attr = 0; *cp; num_attr++) { |
| 242 | cp = parse_attr(src, lineno, cp, NULL); |
| 243 | if (!cp) |
| 244 | return NULL; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 245 | } |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 246 | |
| 247 | res = xcalloc(1, |
| 248 | sizeof(*res) + |
| 249 | sizeof(struct attr_state) * num_attr + |
| 250 | (is_macro ? 0 : namelen + 1)); |
Nguyễn Thái Ngọc Duy | fad32bc | 2014-12-28 06:39:47 +0700 | [diff] [blame] | 251 | if (is_macro) { |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 252 | res->u.attr = git_attr_internal(name, namelen); |
Nguyễn Thái Ngọc Duy | fad32bc | 2014-12-28 06:39:47 +0700 | [diff] [blame] | 253 | res->u.attr->maybe_macro = 1; |
| 254 | } else { |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 255 | char *p = (char *)&(res->state[num_attr]); |
| 256 | memcpy(p, name, namelen); |
| 257 | res->u.pat.pattern = p; |
| 258 | parse_exclude_pattern(&res->u.pat.pattern, |
| 259 | &res->u.pat.patternlen, |
| 260 | &res->u.pat.flags, |
| 261 | &res->u.pat.nowildcardlen); |
Thomas Rast | 8b1bd02 | 2013-03-01 21:06:17 +0100 | [diff] [blame] | 262 | if (res->u.pat.flags & EXC_FLAG_NEGATIVE) { |
| 263 | warning(_("Negative patterns are ignored in git attributes\n" |
| 264 | "Use '\\!' for literal leading exclamation.")); |
| 265 | return NULL; |
| 266 | } |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 267 | } |
| 268 | res->is_macro = is_macro; |
| 269 | res->num_attr = num_attr; |
| 270 | |
| 271 | /* Second pass to fill the attr_states */ |
| 272 | for (cp = states, i = 0; *cp; i++) { |
| 273 | cp = parse_attr(src, lineno, cp, &(res->state[i])); |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 274 | if (!is_macro) |
| 275 | res->state[i].attr->maybe_real = 1; |
| 276 | if (res->state[i].attr->maybe_macro) |
| 277 | cannot_trust_maybe_real = 1; |
Michael Haggerty | d68e1c1 | 2011-08-12 23:43:10 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 280 | return res; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Like info/exclude and .gitignore, the attribute information can |
| 285 | * come from many places. |
| 286 | * |
| 287 | * (1) .gitattribute file of the same directory; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 288 | * (2) .gitattribute file of the parent directory if (1) does not have |
| 289 | * any match; this goes recursively upwards, just like .gitignore. |
| 290 | * (3) $GIT_DIR/info/attributes, which overrides both of the above. |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 291 | * |
| 292 | * In the same file, later entries override the earlier match, so in the |
| 293 | * global list, we would have entries from info/attributes the earliest |
| 294 | * (reading the file from top to bottom), .gitattribute of the root |
| 295 | * directory (again, reading the file from top to bottom) down to the |
| 296 | * current directory, and then scan the list backwards to find the first match. |
Adam Spiers | 6d24e7a | 2012-12-27 02:32:25 +0000 | [diff] [blame] | 297 | * This is exactly the same as what is_excluded() does in dir.c to deal with |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 298 | * .gitignore |
| 299 | */ |
| 300 | |
| 301 | static struct attr_stack { |
| 302 | struct attr_stack *prev; |
| 303 | char *origin; |
Nguyễn Thái Ngọc Duy | cd6a0b2 | 2012-10-05 11:41:01 +0700 | [diff] [blame] | 304 | size_t originlen; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 305 | unsigned num_matches; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 306 | unsigned alloc; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 307 | struct match_attr **attrs; |
| 308 | } *attr_stack; |
| 309 | |
| 310 | static void free_attr_elem(struct attr_stack *e) |
| 311 | { |
| 312 | int i; |
| 313 | free(e->origin); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 314 | for (i = 0; i < e->num_matches; i++) { |
| 315 | struct match_attr *a = e->attrs[i]; |
| 316 | int j; |
| 317 | for (j = 0; j < a->num_attr; j++) { |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 318 | const char *setto = a->state[j].setto; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 319 | if (setto == ATTR__TRUE || |
| 320 | setto == ATTR__FALSE || |
| 321 | setto == ATTR__UNSET || |
| 322 | setto == ATTR__UNKNOWN) |
| 323 | ; |
| 324 | else |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 325 | free((char *) setto); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 326 | } |
| 327 | free(a); |
| 328 | } |
Jeff King | 37475f9 | 2012-01-11 22:05:03 -0500 | [diff] [blame] | 329 | free(e->attrs); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 330 | free(e); |
| 331 | } |
| 332 | |
| 333 | static const char *builtin_attr[] = { |
Junio C Hamano | 155a4b7 | 2012-09-08 21:28:55 -0700 | [diff] [blame] | 334 | "[attr]binary -diff -merge -text", |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 335 | NULL, |
| 336 | }; |
| 337 | |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 338 | static void handle_attr_line(struct attr_stack *res, |
| 339 | const char *line, |
| 340 | const char *src, |
| 341 | int lineno, |
| 342 | int macro_ok) |
| 343 | { |
| 344 | struct match_attr *a; |
| 345 | |
| 346 | a = parse_attr_line(line, src, lineno, macro_ok); |
| 347 | if (!a) |
| 348 | return; |
Dmitry S. Dolzhenko | 3a7fa03 | 2014-03-04 02:31:59 +0400 | [diff] [blame] | 349 | ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 350 | res->attrs[res->num_matches++] = a; |
| 351 | } |
| 352 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 353 | static struct attr_stack *read_attr_from_array(const char **list) |
| 354 | { |
| 355 | struct attr_stack *res; |
| 356 | const char *line; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 357 | int lineno = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 358 | |
| 359 | res = xcalloc(1, sizeof(*res)); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 360 | while ((line = *(list++)) != NULL) |
| 361 | handle_attr_line(res, line, "[builtin]", ++lineno, 1); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 362 | return res; |
| 363 | } |
| 364 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 365 | static enum git_attr_direction direction; |
| 366 | static struct index_state *use_index; |
| 367 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 368 | static struct attr_stack *read_attr_from_file(const char *path, int macro_ok) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 369 | { |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 370 | FILE *fp = fopen(path, "r"); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 371 | struct attr_stack *res; |
| 372 | char buf[2048]; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 373 | int lineno = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 374 | |
Jeff King | 11e50b2 | 2012-08-21 02:31:52 -0400 | [diff] [blame] | 375 | if (!fp) { |
Junio C Hamano | 8e950da | 2012-09-13 12:40:39 -0700 | [diff] [blame] | 376 | if (errno != ENOENT && errno != ENOTDIR) |
Junio C Hamano | 55b38a4 | 2012-08-21 14:52:07 -0700 | [diff] [blame] | 377 | warn_on_inaccessible(path); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 378 | return NULL; |
Jeff King | 11e50b2 | 2012-08-21 02:31:52 -0400 | [diff] [blame] | 379 | } |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 380 | res = xcalloc(1, sizeof(*res)); |
Junio C Hamano | 27547e5 | 2015-04-16 10:48:58 -0700 | [diff] [blame] | 381 | while (fgets(buf, sizeof(buf), fp)) { |
| 382 | char *bufp = buf; |
| 383 | if (!lineno) |
| 384 | skip_utf8_bom(&bufp, strlen(bufp)); |
| 385 | handle_attr_line(res, bufp, path, ++lineno, macro_ok); |
| 386 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 387 | fclose(fp); |
| 388 | return res; |
| 389 | } |
| 390 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 391 | static struct attr_stack *read_attr_from_index(const char *path, int macro_ok) |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 392 | { |
| 393 | struct attr_stack *res; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 394 | char *buf, *sp; |
| 395 | int lineno = 0; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 396 | |
Lukas Fleischer | ff36682 | 2013-04-13 15:28:31 +0200 | [diff] [blame] | 397 | buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 398 | if (!buf) |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 399 | return NULL; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 400 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 401 | res = xcalloc(1, sizeof(*res)); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 402 | for (sp = buf; *sp; ) { |
| 403 | char *ep; |
| 404 | int more; |
| 405 | for (ep = sp; *ep && *ep != '\n'; ep++) |
| 406 | ; |
| 407 | more = (*ep == '\n'); |
| 408 | *ep = '\0'; |
| 409 | handle_attr_line(res, sp, path, ++lineno, macro_ok); |
| 410 | sp = ep + more; |
| 411 | } |
| 412 | free(buf); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 413 | return res; |
| 414 | } |
| 415 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 416 | static struct attr_stack *read_attr(const char *path, int macro_ok) |
| 417 | { |
| 418 | struct attr_stack *res; |
| 419 | |
| 420 | if (direction == GIT_ATTR_CHECKOUT) { |
| 421 | res = read_attr_from_index(path, macro_ok); |
| 422 | if (!res) |
| 423 | res = read_attr_from_file(path, macro_ok); |
| 424 | } |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 425 | else if (direction == GIT_ATTR_CHECKIN) { |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 426 | res = read_attr_from_file(path, macro_ok); |
| 427 | if (!res) |
| 428 | /* |
| 429 | * There is no checked out .gitattributes file there, but |
| 430 | * we might have it in the index. We allow operation in a |
| 431 | * sparsely checked out work tree, so read from it. |
| 432 | */ |
| 433 | res = read_attr_from_index(path, macro_ok); |
| 434 | } |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 435 | else |
| 436 | res = read_attr_from_index(path, macro_ok); |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 437 | if (!res) |
| 438 | res = xcalloc(1, sizeof(*res)); |
| 439 | return res; |
| 440 | } |
| 441 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 442 | #if DEBUG_ATTR |
| 443 | static void debug_info(const char *what, struct attr_stack *elem) |
| 444 | { |
| 445 | fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()"); |
| 446 | } |
Junio C Hamano | cf94ccd | 2008-02-07 00:02:08 -0800 | [diff] [blame] | 447 | static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 448 | { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 449 | const char *value = v; |
| 450 | |
| 451 | if (ATTR_TRUE(value)) |
| 452 | value = "set"; |
| 453 | else if (ATTR_FALSE(value)) |
| 454 | value = "unset"; |
| 455 | else if (ATTR_UNSET(value)) |
| 456 | value = "unspecified"; |
| 457 | |
| 458 | fprintf(stderr, "%s: %s => %s (%s)\n", |
| 459 | what, attr->name, (char *) value, match); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 460 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 461 | #define debug_push(a) debug_info("push", (a)) |
| 462 | #define debug_pop(a) debug_info("pop", (a)) |
| 463 | #else |
| 464 | #define debug_push(a) do { ; } while (0) |
| 465 | #define debug_pop(a) do { ; } while (0) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 466 | #define debug_set(a,b,c,d) do { ; } while (0) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 467 | #endif |
| 468 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 469 | static void drop_attr_stack(void) |
| 470 | { |
| 471 | while (attr_stack) { |
| 472 | struct attr_stack *elem = attr_stack; |
| 473 | attr_stack = elem->prev; |
| 474 | free_attr_elem(elem); |
| 475 | } |
| 476 | } |
| 477 | |
Ramsay Jones | c514772 | 2011-04-07 19:49:33 +0100 | [diff] [blame] | 478 | static const char *git_etc_gitattributes(void) |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 479 | { |
| 480 | static const char *system_wide; |
| 481 | if (!system_wide) |
| 482 | system_wide = system_path(ETC_GITATTRIBUTES); |
| 483 | return system_wide; |
| 484 | } |
| 485 | |
Ramsay Jones | c514772 | 2011-04-07 19:49:33 +0100 | [diff] [blame] | 486 | static int git_attr_system(void) |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 487 | { |
| 488 | return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); |
| 489 | } |
| 490 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 491 | static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE) |
| 492 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 493 | static void bootstrap_attr_stack(void) |
| 494 | { |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 495 | struct attr_stack *elem; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 496 | |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 497 | if (attr_stack) |
| 498 | return; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 499 | |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 500 | elem = read_attr_from_array(builtin_attr); |
| 501 | elem->origin = NULL; |
| 502 | elem->prev = attr_stack; |
| 503 | attr_stack = elem; |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 504 | |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 505 | if (git_attr_system()) { |
| 506 | elem = read_attr_from_file(git_etc_gitattributes(), 1); |
| 507 | if (elem) { |
| 508 | elem->origin = NULL; |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 509 | elem->prev = attr_stack; |
| 510 | attr_stack = elem; |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 511 | } |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 512 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 513 | |
Paul Tan | 2527bbc | 2015-05-06 16:00:59 +0800 | [diff] [blame] | 514 | if (!git_attributes_file) |
| 515 | git_attributes_file = xdg_config_home("attributes"); |
Jeff King | f0c1c15 | 2012-07-24 07:53:57 -0400 | [diff] [blame] | 516 | if (git_attributes_file) { |
| 517 | elem = read_attr_from_file(git_attributes_file, 1); |
| 518 | if (elem) { |
| 519 | elem->origin = NULL; |
| 520 | elem->prev = attr_stack; |
| 521 | attr_stack = elem; |
| 522 | } |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
| 526 | elem = read_attr(GITATTRIBUTES_FILE, 1); |
Junio C Hamano | 6c65b5e | 2012-01-10 14:14:26 -0800 | [diff] [blame] | 527 | elem->origin = xstrdup(""); |
Nguyễn Thái Ngọc Duy | cd6a0b2 | 2012-10-05 11:41:01 +0700 | [diff] [blame] | 528 | elem->originlen = 0; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 529 | elem->prev = attr_stack; |
| 530 | attr_stack = elem; |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 531 | debug_push(elem); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 532 | } |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 533 | |
Jeff King | f932729 | 2015-08-10 05:38:57 -0400 | [diff] [blame] | 534 | elem = read_attr_from_file(git_path_info_attributes(), 1); |
Junio C Hamano | 909ca7b | 2012-01-10 12:27:37 -0800 | [diff] [blame] | 535 | if (!elem) |
| 536 | elem = xcalloc(1, sizeof(*elem)); |
| 537 | elem->origin = NULL; |
| 538 | elem->prev = attr_stack; |
| 539 | attr_stack = elem; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 542 | static void prepare_attr_stack(const char *path, int dirlen) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 543 | { |
| 544 | struct attr_stack *elem, *info; |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 545 | int len; |
Michael Haggerty | a872701 | 2011-08-04 06:36:19 +0200 | [diff] [blame] | 546 | const char *cp; |
| 547 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 548 | /* |
| 549 | * At the bottom of the attribute stack is the built-in |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 550 | * set of attribute definitions, followed by the contents |
| 551 | * of $(prefix)/etc/gitattributes and a file specified by |
| 552 | * core.attributesfile. Then, contents from |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 553 | * .gitattribute files from directories closer to the |
| 554 | * root to the ones in deeper directories are pushed |
| 555 | * to the stack. Finally, at the very top of the stack |
| 556 | * we always keep the contents of $GIT_DIR/info/attributes. |
| 557 | * |
| 558 | * When checking, we use entries from near the top of the |
| 559 | * stack, preferring $GIT_DIR/info/attributes, then |
| 560 | * .gitattributes in deeper directories to shallower ones, |
| 561 | * and finally use the built-in set as the default. |
| 562 | */ |
Michael Haggerty | 7373eab | 2011-08-04 06:36:22 +0200 | [diff] [blame] | 563 | bootstrap_attr_stack(); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 564 | |
| 565 | /* |
| 566 | * Pop the "info" one that is always at the top of the stack. |
| 567 | */ |
| 568 | info = attr_stack; |
| 569 | attr_stack = info->prev; |
| 570 | |
| 571 | /* |
| 572 | * Pop the ones from directories that are not the prefix of |
Junio C Hamano | c432ef9 | 2012-01-10 12:28:38 -0800 | [diff] [blame] | 573 | * the path we are checking. Break out of the loop when we see |
| 574 | * the root one (whose origin is an empty string "") or the builtin |
| 575 | * one (whose origin is NULL) without popping it. |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 576 | */ |
Jeff King | 77f7f82 | 2012-01-10 14:32:06 -0500 | [diff] [blame] | 577 | while (attr_stack->origin) { |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 578 | int namelen = strlen(attr_stack->origin); |
| 579 | |
| 580 | elem = attr_stack; |
| 581 | if (namelen <= dirlen && |
Jeff King | 1afca44 | 2012-01-10 13:08:21 -0500 | [diff] [blame] | 582 | !strncmp(elem->origin, path, namelen) && |
| 583 | (!namelen || path[namelen] == '/')) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 584 | break; |
| 585 | |
| 586 | debug_pop(elem); |
| 587 | attr_stack = elem->prev; |
| 588 | free_attr_elem(elem); |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Read from parent directories and push them down |
| 593 | */ |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 594 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
Junio C Hamano | c432ef9 | 2012-01-10 12:28:38 -0800 | [diff] [blame] | 595 | /* |
| 596 | * bootstrap_attr_stack() should have added, and the |
| 597 | * above loop should have stopped before popping, the |
| 598 | * root element whose attr_stack->origin is set to an |
| 599 | * empty string. |
| 600 | */ |
Brandon Casey | 97410b2 | 2011-10-06 13:22:21 -0500 | [diff] [blame] | 601 | struct strbuf pathbuf = STRBUF_INIT; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 602 | |
Junio C Hamano | c432ef9 | 2012-01-10 12:28:38 -0800 | [diff] [blame] | 603 | assert(attr_stack->origin); |
Brandon Casey | 97410b2 | 2011-10-06 13:22:21 -0500 | [diff] [blame] | 604 | while (1) { |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 605 | len = strlen(attr_stack->origin); |
| 606 | if (dirlen <= len) |
| 607 | break; |
Brandon Casey | 97410b2 | 2011-10-06 13:22:21 -0500 | [diff] [blame] | 608 | cp = memchr(path + len + 1, '/', dirlen - len - 1); |
| 609 | if (!cp) |
| 610 | cp = path + dirlen; |
| 611 | strbuf_add(&pathbuf, path, cp - path); |
Dmitry Potapov | f66cf96 | 2008-07-16 19:39:55 +0400 | [diff] [blame] | 612 | strbuf_addch(&pathbuf, '/'); |
Brandon Casey | 97410b2 | 2011-10-06 13:22:21 -0500 | [diff] [blame] | 613 | strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE); |
Dmitry Potapov | f66cf96 | 2008-07-16 19:39:55 +0400 | [diff] [blame] | 614 | elem = read_attr(pathbuf.buf, 0); |
Brandon Casey | 97410b2 | 2011-10-06 13:22:21 -0500 | [diff] [blame] | 615 | strbuf_setlen(&pathbuf, cp - path); |
Nguyễn Thái Ngọc Duy | cd6a0b2 | 2012-10-05 11:41:01 +0700 | [diff] [blame] | 616 | elem->origin = strbuf_detach(&pathbuf, &elem->originlen); |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 617 | elem->prev = attr_stack; |
| 618 | attr_stack = elem; |
| 619 | debug_push(elem); |
| 620 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 621 | |
Brandon Casey | 97410b2 | 2011-10-06 13:22:21 -0500 | [diff] [blame] | 622 | strbuf_release(&pathbuf); |
| 623 | } |
René Scharfe | d4c9856 | 2009-07-01 00:30:00 +0200 | [diff] [blame] | 624 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 625 | /* |
| 626 | * Finally push the "info" one at the top of the stack. |
| 627 | */ |
| 628 | info->prev = attr_stack; |
| 629 | attr_stack = info; |
| 630 | } |
| 631 | |
| 632 | static int path_matches(const char *pathname, int pathlen, |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 633 | int basename_offset, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 634 | const struct pattern *pat, |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 635 | const char *base, int baselen) |
| 636 | { |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 637 | const char *pattern = pat->pattern; |
| 638 | int prefix = pat->nowildcardlen; |
Junio C Hamano | dc09e9e | 2013-03-28 17:49:13 -0400 | [diff] [blame] | 639 | int isdir = (pathlen && pathname[pathlen - 1] == '/'); |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 640 | |
Junio C Hamano | dc09e9e | 2013-03-28 17:49:13 -0400 | [diff] [blame] | 641 | if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir) |
Jean-Noël AVILA | 94bc671 | 2012-12-08 21:04:39 +0100 | [diff] [blame] | 642 | return 0; |
| 643 | |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 644 | if (pat->flags & EXC_FLAG_NODIR) { |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 645 | return match_basename(pathname + basename_offset, |
Junio C Hamano | dc09e9e | 2013-03-28 17:49:13 -0400 | [diff] [blame] | 646 | pathlen - basename_offset - isdir, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 647 | pattern, prefix, |
| 648 | pat->patternlen, pat->flags); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 649 | } |
Junio C Hamano | dc09e9e | 2013-03-28 17:49:13 -0400 | [diff] [blame] | 650 | return match_pathname(pathname, pathlen - isdir, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 651 | base, baselen, |
| 652 | pattern, prefix, pat->patternlen, pat->flags); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 653 | } |
| 654 | |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 655 | static int macroexpand_one(int attr_nr, int rem); |
| 656 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 657 | static int fill_one(const char *what, struct match_attr *a, int rem) |
| 658 | { |
| 659 | struct git_attr_check *check = check_all_attr; |
| 660 | int i; |
| 661 | |
Henrik Grubbström | 969f9d7 | 2010-04-06 14:46:43 +0200 | [diff] [blame] | 662 | for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 663 | struct git_attr *attr = a->state[i].attr; |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 664 | const char **n = &(check[attr->attr_nr].value); |
| 665 | const char *v = a->state[i].setto; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 666 | |
| 667 | if (*n == ATTR__UNKNOWN) { |
Henrik Grubbström | 426c27b | 2010-04-06 14:46:42 +0200 | [diff] [blame] | 668 | debug_set(what, |
Nguyễn Thái Ngọc Duy | 712efb1 | 2013-01-15 09:49:26 +0700 | [diff] [blame] | 669 | a->is_macro ? a->u.attr->name : a->u.pat.pattern, |
Henrik Grubbström | 426c27b | 2010-04-06 14:46:42 +0200 | [diff] [blame] | 670 | attr, v); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 671 | *n = v; |
| 672 | rem--; |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 673 | rem = macroexpand_one(attr->attr_nr, rem); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | return rem; |
| 677 | } |
| 678 | |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 679 | static int fill(const char *path, int pathlen, int basename_offset, |
Nguyễn Thái Ngọc Duy | 4742d13 | 2012-10-05 11:41:02 +0700 | [diff] [blame] | 680 | struct attr_stack *stk, int rem) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 681 | { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 682 | int i; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 683 | const char *base = stk->origin ? stk->origin : ""; |
| 684 | |
| 685 | for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { |
| 686 | struct match_attr *a = stk->attrs[i]; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 687 | if (a->is_macro) |
| 688 | continue; |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 689 | if (path_matches(path, pathlen, basename_offset, |
Nguyễn Thái Ngọc Duy | 82dce99 | 2012-10-15 13:24:39 +0700 | [diff] [blame] | 690 | &a->u.pat, base, stk->originlen)) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 691 | rem = fill_one("fill", a, rem); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 692 | } |
| 693 | return rem; |
| 694 | } |
| 695 | |
Nguyễn Thái Ngọc Duy | aa7710e | 2014-12-28 06:39:46 +0700 | [diff] [blame] | 696 | static int macroexpand_one(int nr, int rem) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 697 | { |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 698 | struct attr_stack *stk; |
| 699 | struct match_attr *a = NULL; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 700 | int i; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 701 | |
Nguyễn Thái Ngọc Duy | fad32bc | 2014-12-28 06:39:47 +0700 | [diff] [blame] | 702 | if (check_all_attr[nr].value != ATTR__TRUE || |
| 703 | !check_all_attr[nr].attr->maybe_macro) |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 704 | return rem; |
| 705 | |
| 706 | for (stk = attr_stack; !a && stk; stk = stk->prev) |
| 707 | for (i = stk->num_matches - 1; !a && 0 <= i; i--) { |
| 708 | struct match_attr *ma = stk->attrs[i]; |
| 709 | if (!ma->is_macro) |
| 710 | continue; |
Nguyễn Thái Ngọc Duy | aa7710e | 2014-12-28 06:39:46 +0700 | [diff] [blame] | 711 | if (ma->u.attr->attr_nr == nr) |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 712 | a = ma; |
| 713 | } |
| 714 | |
| 715 | if (a) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 716 | rem = fill_one("expand", a, rem); |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 717 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 718 | return rem; |
| 719 | } |
| 720 | |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 721 | /* |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 722 | * Collect attributes for path into the array pointed to by |
| 723 | * check_all_attr. If num is non-zero, only attributes in check[] are |
| 724 | * collected. Otherwise all attributes are collected. |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 725 | */ |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 726 | static void collect_some_attrs(const char *path, int num, |
| 727 | struct git_attr_check *check) |
| 728 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 729 | { |
| 730 | struct attr_stack *stk; |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 731 | int i, pathlen, rem, dirlen; |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 732 | const char *cp, *last_slash = NULL; |
| 733 | int basename_offset; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 734 | |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 735 | for (cp = path; *cp; cp++) { |
| 736 | if (*cp == '/' && cp[1]) |
| 737 | last_slash = cp; |
| 738 | } |
| 739 | pathlen = cp - path; |
| 740 | if (last_slash) { |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 741 | basename_offset = last_slash + 1 - path; |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 742 | dirlen = last_slash - path; |
| 743 | } else { |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 744 | basename_offset = 0; |
Duy Nguyen | 9db9eec | 2013-01-16 13:02:38 +0700 | [diff] [blame] | 745 | dirlen = 0; |
| 746 | } |
| 747 | |
| 748 | prepare_attr_stack(path, dirlen); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 749 | for (i = 0; i < attr_nr; i++) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 750 | check_all_attr[i].value = ATTR__UNKNOWN; |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 751 | if (num && !cannot_trust_maybe_real) { |
| 752 | rem = 0; |
| 753 | for (i = 0; i < num; i++) { |
| 754 | if (!check[i].attr->maybe_real) { |
| 755 | struct git_attr_check *c; |
| 756 | c = check_all_attr + check[i].attr->attr_nr; |
| 757 | c->value = ATTR__UNSET; |
| 758 | rem++; |
| 759 | } |
| 760 | } |
| 761 | if (rem == num) |
| 762 | return; |
| 763 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 764 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 765 | rem = attr_nr; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 766 | for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) |
Junio C Hamano | bd2f371 | 2013-03-26 10:28:07 -0700 | [diff] [blame] | 767 | rem = fill(path, pathlen, basename_offset, stk, rem); |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Michael Haggerty | d932f4e | 2011-08-04 06:36:33 +0200 | [diff] [blame] | 770 | int git_check_attr(const char *path, int num, struct git_attr_check *check) |
Michael Haggerty | 2d72174 | 2011-08-04 06:36:20 +0200 | [diff] [blame] | 771 | { |
| 772 | int i; |
| 773 | |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 774 | collect_some_attrs(path, num, check); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 775 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 776 | for (i = 0; i < num; i++) { |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 777 | const char *value = check_all_attr[check[i].attr->attr_nr].value; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 778 | if (value == ATTR__UNKNOWN) |
| 779 | value = ATTR__UNSET; |
| 780 | check[i].value = value; |
| 781 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 782 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 783 | return 0; |
| 784 | } |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 785 | |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 786 | int git_all_attrs(const char *path, int *num, struct git_attr_check **check) |
| 787 | { |
| 788 | int i, count, j; |
| 789 | |
Nguyễn Thái Ngọc Duy | 06a604e | 2014-12-28 06:39:48 +0700 | [diff] [blame] | 790 | collect_some_attrs(path, 0, NULL); |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 791 | |
| 792 | /* Count the number of attributes that are set. */ |
| 793 | count = 0; |
| 794 | for (i = 0; i < attr_nr; i++) { |
| 795 | const char *value = check_all_attr[i].value; |
| 796 | if (value != ATTR__UNSET && value != ATTR__UNKNOWN) |
| 797 | ++count; |
| 798 | } |
| 799 | *num = count; |
Jeff King | b32fa95 | 2016-02-22 17:44:25 -0500 | [diff] [blame] | 800 | ALLOC_ARRAY(*check, count); |
Michael Haggerty | ee548df | 2011-08-04 06:36:23 +0200 | [diff] [blame] | 801 | j = 0; |
| 802 | for (i = 0; i < attr_nr; i++) { |
| 803 | const char *value = check_all_attr[i].value; |
| 804 | if (value != ATTR__UNSET && value != ATTR__UNKNOWN) { |
| 805 | (*check)[j].attr = check_all_attr[i].attr; |
| 806 | (*check)[j].value = value; |
| 807 | ++j; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 814 | void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate) |
| 815 | { |
| 816 | enum git_attr_direction old = direction; |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 817 | |
| 818 | if (is_bare_repository() && new != GIT_ATTR_INDEX) |
| 819 | die("BUG: non-INDEX attr direction in a bare repo"); |
| 820 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 821 | direction = new; |
| 822 | if (new != old) |
| 823 | drop_attr_stack(); |
| 824 | use_index = istate; |
| 825 | } |