Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 1 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 2 | #include "cache.h" |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 3 | #include "exec_cmd.h" |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 4 | #include "attr.h" |
| 5 | |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 6 | const char git_attr__true[] = "(builtin)true"; |
| 7 | const char git_attr__false[] = "\0(builtin)false"; |
| 8 | static const char git_attr__unknown[] = "(builtin)unknown"; |
| 9 | #define ATTR__TRUE git_attr__true |
| 10 | #define ATTR__FALSE git_attr__false |
| 11 | #define ATTR__UNSET NULL |
| 12 | #define ATTR__UNKNOWN git_attr__unknown |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 13 | |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 14 | static const char *attributes_file; |
| 15 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 16 | /* |
| 17 | * The basic design decision here is that we are not going to have |
| 18 | * insanely large number of attributes. |
| 19 | * |
| 20 | * This is a randomly chosen prime. |
| 21 | */ |
| 22 | #define HASHSIZE 257 |
| 23 | |
| 24 | #ifndef DEBUG_ATTR |
| 25 | #define DEBUG_ATTR 0 |
| 26 | #endif |
| 27 | |
| 28 | struct git_attr { |
| 29 | struct git_attr *next; |
| 30 | unsigned h; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 31 | int attr_nr; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 32 | char name[FLEX_ARRAY]; |
| 33 | }; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 34 | static int attr_nr; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 35 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 36 | static struct git_attr_check *check_all_attr; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 37 | static struct git_attr *(git_attr_hash[HASHSIZE]); |
| 38 | |
| 39 | static unsigned hash_name(const char *name, int namelen) |
| 40 | { |
Linus Torvalds | 48fb7de | 2009-06-17 17:22:27 -0700 | [diff] [blame] | 41 | unsigned val = 0, c; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 42 | |
| 43 | while (namelen--) { |
| 44 | c = *name++; |
| 45 | val = ((val << 7) | (val >> 22)) ^ c; |
| 46 | } |
| 47 | return val; |
| 48 | } |
| 49 | |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 50 | static int invalid_attr_name(const char *name, int namelen) |
| 51 | { |
| 52 | /* |
| 53 | * Attribute name cannot begin with '-' and from |
| 54 | * [-A-Za-z0-9_.]. We'd specifically exclude '=' for now, |
| 55 | * as we might later want to allow non-binary value for |
| 56 | * attributes, e.g. "*.svg merge=special-merge-program-for-svg" |
| 57 | */ |
| 58 | if (*name == '-') |
| 59 | return -1; |
| 60 | while (namelen--) { |
| 61 | char ch = *name++; |
| 62 | if (! (ch == '-' || ch == '.' || ch == '_' || |
| 63 | ('0' <= ch && ch <= '9') || |
| 64 | ('a' <= ch && ch <= 'z') || |
| 65 | ('A' <= ch && ch <= 'Z')) ) |
| 66 | return -1; |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 71 | 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] | 72 | { |
| 73 | unsigned hval = hash_name(name, len); |
| 74 | unsigned pos = hval % HASHSIZE; |
| 75 | struct git_attr *a; |
| 76 | |
| 77 | for (a = git_attr_hash[pos]; a; a = a->next) { |
| 78 | if (a->h == hval && |
| 79 | !memcmp(a->name, name, len) && !a->name[len]) |
| 80 | return a; |
| 81 | } |
| 82 | |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 83 | if (invalid_attr_name(name, len)) |
| 84 | return NULL; |
| 85 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 86 | a = xmalloc(sizeof(*a) + len + 1); |
| 87 | memcpy(a->name, name, len); |
| 88 | a->name[len] = 0; |
| 89 | a->h = hval; |
| 90 | a->next = git_attr_hash[pos]; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 91 | a->attr_nr = attr_nr++; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 92 | git_attr_hash[pos] = a; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 93 | |
| 94 | check_all_attr = xrealloc(check_all_attr, |
| 95 | sizeof(*check_all_attr) * attr_nr); |
| 96 | check_all_attr[a->attr_nr].attr = a; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 97 | check_all_attr[a->attr_nr].value = ATTR__UNKNOWN; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 98 | return a; |
| 99 | } |
| 100 | |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 101 | struct git_attr *git_attr(const char *name) |
| 102 | { |
| 103 | return git_attr_internal(name, strlen(name)); |
| 104 | } |
| 105 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 106 | /* |
| 107 | * .gitattributes file is one line per record, each of which is |
| 108 | * |
| 109 | * (1) glob pattern. |
| 110 | * (2) whitespace |
| 111 | * (3) whitespace separated list of attribute names, each of which |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 112 | * could be prefixed with '-' to mean "set to false", '!' to mean |
| 113 | * "unset". |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 114 | */ |
| 115 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 116 | /* What does a matched pattern decide? */ |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 117 | struct attr_state { |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 118 | struct git_attr *attr; |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 119 | const char *setto; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | struct match_attr { |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 123 | union { |
| 124 | char *pattern; |
| 125 | struct git_attr *attr; |
| 126 | } u; |
| 127 | char is_macro; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 128 | unsigned num_attr; |
| 129 | struct attr_state state[FLEX_ARRAY]; |
| 130 | }; |
| 131 | |
| 132 | static const char blank[] = " \t\r\n"; |
| 133 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 134 | static const char *parse_attr(const char *src, int lineno, const char *cp, |
| 135 | int *num_attr, struct match_attr *res) |
| 136 | { |
| 137 | const char *ep, *equals; |
| 138 | int len; |
| 139 | |
| 140 | ep = cp + strcspn(cp, blank); |
| 141 | equals = strchr(cp, '='); |
| 142 | if (equals && ep < equals) |
| 143 | equals = NULL; |
| 144 | if (equals) |
| 145 | len = equals - cp; |
| 146 | else |
| 147 | len = ep - cp; |
| 148 | if (!res) { |
| 149 | if (*cp == '-' || *cp == '!') { |
| 150 | cp++; |
| 151 | len--; |
| 152 | } |
| 153 | if (invalid_attr_name(cp, len)) { |
| 154 | fprintf(stderr, |
| 155 | "%.*s is not a valid attribute name: %s:%d\n", |
| 156 | len, cp, src, lineno); |
| 157 | return NULL; |
| 158 | } |
| 159 | } else { |
| 160 | struct attr_state *e; |
| 161 | |
| 162 | e = &(res->state[*num_attr]); |
| 163 | if (*cp == '-' || *cp == '!') { |
| 164 | e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; |
| 165 | cp++; |
| 166 | len--; |
| 167 | } |
| 168 | else if (!equals) |
| 169 | e->setto = ATTR__TRUE; |
| 170 | else { |
Pierre Habouzit | 182af83 | 2007-09-16 00:32:36 +0200 | [diff] [blame] | 171 | e->setto = xmemdupz(equals + 1, ep - equals - 1); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 172 | } |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 173 | e->attr = git_attr_internal(cp, len); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 174 | } |
| 175 | (*num_attr)++; |
| 176 | return ep + strspn(ep, blank); |
| 177 | } |
| 178 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 179 | static struct match_attr *parse_attr_line(const char *line, const char *src, |
| 180 | int lineno, int macro_ok) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 181 | { |
| 182 | int namelen; |
| 183 | int num_attr; |
| 184 | const char *cp, *name; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 185 | struct match_attr *res = NULL; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 186 | int pass; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 187 | int is_macro; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 188 | |
| 189 | cp = line + strspn(line, blank); |
| 190 | if (!*cp || *cp == '#') |
| 191 | return NULL; |
| 192 | name = cp; |
| 193 | namelen = strcspn(name, blank); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 194 | if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && |
| 195 | !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) { |
| 196 | if (!macro_ok) { |
| 197 | fprintf(stderr, "%s not allowed: %s:%d\n", |
| 198 | name, src, lineno); |
| 199 | return NULL; |
| 200 | } |
| 201 | is_macro = 1; |
| 202 | name += strlen(ATTRIBUTE_MACRO_PREFIX); |
| 203 | name += strspn(name, blank); |
| 204 | namelen = strcspn(name, blank); |
Junio C Hamano | e4aee10 | 2007-04-15 14:56:09 -0700 | [diff] [blame] | 205 | if (invalid_attr_name(name, namelen)) { |
| 206 | fprintf(stderr, |
| 207 | "%.*s is not a valid attribute name: %s:%d\n", |
| 208 | namelen, name, src, lineno); |
| 209 | return NULL; |
| 210 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 211 | } |
| 212 | else |
| 213 | is_macro = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 214 | |
| 215 | for (pass = 0; pass < 2; pass++) { |
| 216 | /* pass 0 counts and allocates, pass 1 fills */ |
| 217 | num_attr = 0; |
| 218 | cp = name + namelen; |
| 219 | cp = cp + strspn(cp, blank); |
Steffen Prohaska | d7b0a09 | 2007-10-18 22:02:35 +0200 | [diff] [blame] | 220 | while (*cp) { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 221 | cp = parse_attr(src, lineno, cp, &num_attr, res); |
Steffen Prohaska | d7b0a09 | 2007-10-18 22:02:35 +0200 | [diff] [blame] | 222 | if (!cp) |
| 223 | return NULL; |
| 224 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 225 | if (pass) |
| 226 | break; |
| 227 | res = xcalloc(1, |
| 228 | sizeof(*res) + |
| 229 | sizeof(struct attr_state) * num_attr + |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 230 | (is_macro ? 0 : namelen + 1)); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 231 | if (is_macro) |
Junio C Hamano | 7fb0eaa | 2010-01-16 20:39:59 -0800 | [diff] [blame] | 232 | res->u.attr = git_attr_internal(name, namelen); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 233 | else { |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 234 | res->u.pattern = (char *)&(res->state[num_attr]); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 235 | memcpy(res->u.pattern, name, namelen); |
| 236 | res->u.pattern[namelen] = 0; |
| 237 | } |
| 238 | res->is_macro = is_macro; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 239 | res->num_attr = num_attr; |
| 240 | } |
| 241 | return res; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * Like info/exclude and .gitignore, the attribute information can |
| 246 | * come from many places. |
| 247 | * |
| 248 | * (1) .gitattribute file of the same directory; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 249 | * (2) .gitattribute file of the parent directory if (1) does not have |
| 250 | * any match; this goes recursively upwards, just like .gitignore. |
| 251 | * (3) $GIT_DIR/info/attributes, which overrides both of the above. |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 252 | * |
| 253 | * In the same file, later entries override the earlier match, so in the |
| 254 | * global list, we would have entries from info/attributes the earliest |
| 255 | * (reading the file from top to bottom), .gitattribute of the root |
| 256 | * directory (again, reading the file from top to bottom) down to the |
| 257 | * current directory, and then scan the list backwards to find the first match. |
| 258 | * This is exactly the same as what excluded() does in dir.c to deal with |
| 259 | * .gitignore |
| 260 | */ |
| 261 | |
| 262 | static struct attr_stack { |
| 263 | struct attr_stack *prev; |
| 264 | char *origin; |
| 265 | unsigned num_matches; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 266 | unsigned alloc; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 267 | struct match_attr **attrs; |
| 268 | } *attr_stack; |
| 269 | |
| 270 | static void free_attr_elem(struct attr_stack *e) |
| 271 | { |
| 272 | int i; |
| 273 | free(e->origin); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 274 | for (i = 0; i < e->num_matches; i++) { |
| 275 | struct match_attr *a = e->attrs[i]; |
| 276 | int j; |
| 277 | for (j = 0; j < a->num_attr; j++) { |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 278 | const char *setto = a->state[j].setto; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 279 | if (setto == ATTR__TRUE || |
| 280 | setto == ATTR__FALSE || |
| 281 | setto == ATTR__UNSET || |
| 282 | setto == ATTR__UNKNOWN) |
| 283 | ; |
| 284 | else |
Felipe Contreras | 4b25d09 | 2009-05-01 12:06:36 +0300 | [diff] [blame] | 285 | free((char *) setto); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 286 | } |
| 287 | free(a); |
| 288 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 289 | free(e); |
| 290 | } |
| 291 | |
| 292 | static const char *builtin_attr[] = { |
Eyvind Bernhardsen | 5ec3e67 | 2010-05-19 22:43:11 +0200 | [diff] [blame] | 293 | "[attr]binary -diff -text", |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 294 | NULL, |
| 295 | }; |
| 296 | |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 297 | static void handle_attr_line(struct attr_stack *res, |
| 298 | const char *line, |
| 299 | const char *src, |
| 300 | int lineno, |
| 301 | int macro_ok) |
| 302 | { |
| 303 | struct match_attr *a; |
| 304 | |
| 305 | a = parse_attr_line(line, src, lineno, macro_ok); |
| 306 | if (!a) |
| 307 | return; |
| 308 | if (res->alloc <= res->num_matches) { |
| 309 | res->alloc = alloc_nr(res->num_matches); |
| 310 | res->attrs = xrealloc(res->attrs, |
| 311 | sizeof(struct match_attr *) * |
| 312 | res->alloc); |
| 313 | } |
| 314 | res->attrs[res->num_matches++] = a; |
| 315 | } |
| 316 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 317 | static struct attr_stack *read_attr_from_array(const char **list) |
| 318 | { |
| 319 | struct attr_stack *res; |
| 320 | const char *line; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 321 | int lineno = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 322 | |
| 323 | res = xcalloc(1, sizeof(*res)); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 324 | while ((line = *(list++)) != NULL) |
| 325 | handle_attr_line(res, line, "[builtin]", ++lineno, 1); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 326 | return res; |
| 327 | } |
| 328 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 329 | static enum git_attr_direction direction; |
| 330 | static struct index_state *use_index; |
| 331 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 332 | 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] | 333 | { |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 334 | FILE *fp = fopen(path, "r"); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 335 | struct attr_stack *res; |
| 336 | char buf[2048]; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 337 | int lineno = 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 338 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 339 | if (!fp) |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 340 | return NULL; |
| 341 | res = xcalloc(1, sizeof(*res)); |
| 342 | while (fgets(buf, sizeof(buf), fp)) |
| 343 | handle_attr_line(res, buf, path, ++lineno, macro_ok); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 344 | fclose(fp); |
| 345 | return res; |
| 346 | } |
| 347 | |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 348 | static void *read_index_data(const char *path) |
| 349 | { |
| 350 | int pos, len; |
| 351 | unsigned long sz; |
| 352 | enum object_type type; |
| 353 | void *data; |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 354 | struct index_state *istate = use_index ? use_index : &the_index; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 355 | |
| 356 | len = strlen(path); |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 357 | pos = index_name_pos(istate, path, len); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 358 | if (pos < 0) { |
| 359 | /* |
| 360 | * We might be in the middle of a merge, in which |
| 361 | * case we would read stage #2 (ours). |
| 362 | */ |
| 363 | int i; |
| 364 | for (i = -pos - 1; |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 365 | (pos < 0 && i < istate->cache_nr && |
| 366 | !strcmp(istate->cache[i]->name, path)); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 367 | i++) |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 368 | if (ce_stage(istate->cache[i]) == 2) |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 369 | pos = i; |
| 370 | } |
| 371 | if (pos < 0) |
| 372 | return NULL; |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 373 | data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 374 | if (!data || type != OBJ_BLOB) { |
| 375 | free(data); |
| 376 | return NULL; |
| 377 | } |
| 378 | return data; |
| 379 | } |
| 380 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 381 | 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] | 382 | { |
| 383 | struct attr_stack *res; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 384 | char *buf, *sp; |
| 385 | int lineno = 0; |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 386 | |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 387 | buf = read_index_data(path); |
| 388 | if (!buf) |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 389 | return NULL; |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 390 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 391 | res = xcalloc(1, sizeof(*res)); |
Junio C Hamano | 1a9d7e9 | 2007-08-14 01:41:02 -0700 | [diff] [blame] | 392 | for (sp = buf; *sp; ) { |
| 393 | char *ep; |
| 394 | int more; |
| 395 | for (ep = sp; *ep && *ep != '\n'; ep++) |
| 396 | ; |
| 397 | more = (*ep == '\n'); |
| 398 | *ep = '\0'; |
| 399 | handle_attr_line(res, sp, path, ++lineno, macro_ok); |
| 400 | sp = ep + more; |
| 401 | } |
| 402 | free(buf); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 403 | return res; |
| 404 | } |
| 405 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 406 | static struct attr_stack *read_attr(const char *path, int macro_ok) |
| 407 | { |
| 408 | struct attr_stack *res; |
| 409 | |
| 410 | if (direction == GIT_ATTR_CHECKOUT) { |
| 411 | res = read_attr_from_index(path, macro_ok); |
| 412 | if (!res) |
| 413 | res = read_attr_from_file(path, macro_ok); |
| 414 | } |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 415 | else if (direction == GIT_ATTR_CHECKIN) { |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 416 | res = read_attr_from_file(path, macro_ok); |
| 417 | if (!res) |
| 418 | /* |
| 419 | * There is no checked out .gitattributes file there, but |
| 420 | * we might have it in the index. We allow operation in a |
| 421 | * sparsely checked out work tree, so read from it. |
| 422 | */ |
| 423 | res = read_attr_from_index(path, macro_ok); |
| 424 | } |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 425 | else |
| 426 | res = read_attr_from_index(path, macro_ok); |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 427 | if (!res) |
| 428 | res = xcalloc(1, sizeof(*res)); |
| 429 | return res; |
| 430 | } |
| 431 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 432 | #if DEBUG_ATTR |
| 433 | static void debug_info(const char *what, struct attr_stack *elem) |
| 434 | { |
| 435 | fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()"); |
| 436 | } |
Junio C Hamano | cf94ccd | 2008-02-07 00:02:08 -0800 | [diff] [blame] | 437 | 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] | 438 | { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 439 | const char *value = v; |
| 440 | |
| 441 | if (ATTR_TRUE(value)) |
| 442 | value = "set"; |
| 443 | else if (ATTR_FALSE(value)) |
| 444 | value = "unset"; |
| 445 | else if (ATTR_UNSET(value)) |
| 446 | value = "unspecified"; |
| 447 | |
| 448 | fprintf(stderr, "%s: %s => %s (%s)\n", |
| 449 | what, attr->name, (char *) value, match); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 450 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 451 | #define debug_push(a) debug_info("push", (a)) |
| 452 | #define debug_pop(a) debug_info("pop", (a)) |
| 453 | #else |
| 454 | #define debug_push(a) do { ; } while (0) |
| 455 | #define debug_pop(a) do { ; } while (0) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 456 | #define debug_set(a,b,c,d) do { ; } while (0) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 457 | #endif |
| 458 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 459 | static void drop_attr_stack(void) |
| 460 | { |
| 461 | while (attr_stack) { |
| 462 | struct attr_stack *elem = attr_stack; |
| 463 | attr_stack = elem->prev; |
| 464 | free_attr_elem(elem); |
| 465 | } |
| 466 | } |
| 467 | |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 468 | const char *git_etc_gitattributes(void) |
| 469 | { |
| 470 | static const char *system_wide; |
| 471 | if (!system_wide) |
| 472 | system_wide = system_path(ETC_GITATTRIBUTES); |
| 473 | return system_wide; |
| 474 | } |
| 475 | |
| 476 | int git_attr_system(void) |
| 477 | { |
| 478 | return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); |
| 479 | } |
| 480 | |
| 481 | int git_attr_global(void) |
| 482 | { |
| 483 | return !git_env_bool("GIT_ATTR_NOGLOBAL", 0); |
| 484 | } |
| 485 | |
| 486 | static int git_attr_config(const char *var, const char *value, void *dummy) |
| 487 | { |
| 488 | if (!strcmp(var, "core.attributesfile")) |
| 489 | return git_config_pathname(&attributes_file, var, value); |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 494 | static void bootstrap_attr_stack(void) |
| 495 | { |
| 496 | if (!attr_stack) { |
| 497 | struct attr_stack *elem; |
| 498 | |
| 499 | elem = read_attr_from_array(builtin_attr); |
| 500 | elem->origin = NULL; |
| 501 | elem->prev = attr_stack; |
| 502 | attr_stack = elem; |
| 503 | |
Petr Onderka | 6df42ab | 2010-09-01 00:42:43 +0200 | [diff] [blame] | 504 | if (git_attr_system()) { |
| 505 | elem = read_attr_from_file(git_etc_gitattributes(), 1); |
| 506 | if (elem) { |
| 507 | elem->origin = NULL; |
| 508 | elem->prev = attr_stack; |
| 509 | attr_stack = elem; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | git_config(git_attr_config, NULL); |
| 514 | if (git_attr_global() && attributes_file) { |
| 515 | elem = read_attr_from_file(attributes_file, 1); |
| 516 | if (elem) { |
| 517 | elem->origin = NULL; |
| 518 | elem->prev = attr_stack; |
| 519 | attr_stack = elem; |
| 520 | } |
| 521 | } |
| 522 | |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 523 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 524 | elem = read_attr(GITATTRIBUTES_FILE, 1); |
| 525 | elem->origin = strdup(""); |
| 526 | elem->prev = attr_stack; |
| 527 | attr_stack = elem; |
| 528 | debug_push(elem); |
| 529 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 530 | |
| 531 | elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1); |
Junio C Hamano | a441311 | 2007-08-14 01:40:45 -0700 | [diff] [blame] | 532 | if (!elem) |
| 533 | elem = xcalloc(1, sizeof(*elem)); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 534 | elem->origin = NULL; |
| 535 | elem->prev = attr_stack; |
| 536 | attr_stack = elem; |
| 537 | } |
| 538 | } |
| 539 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 540 | static void prepare_attr_stack(const char *path, int dirlen) |
| 541 | { |
| 542 | struct attr_stack *elem, *info; |
| 543 | int len; |
Dmitry Potapov | f66cf96 | 2008-07-16 19:39:55 +0400 | [diff] [blame] | 544 | struct strbuf pathbuf; |
| 545 | |
| 546 | strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE)); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 547 | |
| 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 | */ |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 563 | if (!attr_stack) |
| 564 | bootstrap_attr_stack(); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 565 | |
| 566 | /* |
| 567 | * Pop the "info" one that is always at the top of the stack. |
| 568 | */ |
| 569 | info = attr_stack; |
| 570 | attr_stack = info->prev; |
| 571 | |
| 572 | /* |
| 573 | * Pop the ones from directories that are not the prefix of |
| 574 | * the path we are checking. |
| 575 | */ |
| 576 | while (attr_stack && attr_stack->origin) { |
| 577 | int namelen = strlen(attr_stack->origin); |
| 578 | |
| 579 | elem = attr_stack; |
| 580 | if (namelen <= dirlen && |
| 581 | !strncmp(elem->origin, path, namelen)) |
| 582 | break; |
| 583 | |
| 584 | debug_pop(elem); |
| 585 | attr_stack = elem->prev; |
| 586 | free_attr_elem(elem); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Read from parent directories and push them down |
| 591 | */ |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 592 | if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 593 | while (1) { |
| 594 | char *cp; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 595 | |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 596 | len = strlen(attr_stack->origin); |
| 597 | if (dirlen <= len) |
| 598 | break; |
Dmitry Potapov | f66cf96 | 2008-07-16 19:39:55 +0400 | [diff] [blame] | 599 | strbuf_reset(&pathbuf); |
| 600 | strbuf_add(&pathbuf, path, dirlen); |
| 601 | strbuf_addch(&pathbuf, '/'); |
| 602 | cp = strchr(pathbuf.buf + len + 1, '/'); |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 603 | strcpy(cp + 1, GITATTRIBUTES_FILE); |
Dmitry Potapov | f66cf96 | 2008-07-16 19:39:55 +0400 | [diff] [blame] | 604 | elem = read_attr(pathbuf.buf, 0); |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 605 | *cp = '\0'; |
Dmitry Potapov | f66cf96 | 2008-07-16 19:39:55 +0400 | [diff] [blame] | 606 | elem->origin = strdup(pathbuf.buf); |
René Scharfe | 2d35d55 | 2008-06-08 17:16:11 +0200 | [diff] [blame] | 607 | elem->prev = attr_stack; |
| 608 | attr_stack = elem; |
| 609 | debug_push(elem); |
| 610 | } |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 611 | } |
| 612 | |
René Scharfe | d4c9856 | 2009-07-01 00:30:00 +0200 | [diff] [blame] | 613 | strbuf_release(&pathbuf); |
| 614 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 615 | /* |
| 616 | * Finally push the "info" one at the top of the stack. |
| 617 | */ |
| 618 | info->prev = attr_stack; |
| 619 | attr_stack = info; |
| 620 | } |
| 621 | |
| 622 | static int path_matches(const char *pathname, int pathlen, |
| 623 | const char *pattern, |
| 624 | const char *base, int baselen) |
| 625 | { |
| 626 | if (!strchr(pattern, '/')) { |
| 627 | /* match basename */ |
| 628 | const char *basename = strrchr(pathname, '/'); |
| 629 | basename = basename ? basename + 1 : pathname; |
| 630 | return (fnmatch(pattern, basename, 0) == 0); |
| 631 | } |
| 632 | /* |
| 633 | * match with FNM_PATHNAME; the pattern has base implicitly |
| 634 | * in front of it. |
| 635 | */ |
| 636 | if (*pattern == '/') |
| 637 | pattern++; |
| 638 | if (pathlen < baselen || |
Junio C Hamano | cf94ccd | 2008-02-07 00:02:08 -0800 | [diff] [blame] | 639 | (baselen && pathname[baselen] != '/') || |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 640 | strncmp(pathname, base, baselen)) |
| 641 | return 0; |
Matthew Ogilvie | 82881b3 | 2008-04-22 12:19:12 -0600 | [diff] [blame] | 642 | if (baselen != 0) |
| 643 | baselen++; |
| 644 | return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 647 | static int macroexpand_one(int attr_nr, int rem); |
| 648 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 649 | static int fill_one(const char *what, struct match_attr *a, int rem) |
| 650 | { |
| 651 | struct git_attr_check *check = check_all_attr; |
| 652 | int i; |
| 653 | |
Henrik Grubbström | 969f9d7 | 2010-04-06 14:46:43 +0200 | [diff] [blame] | 654 | for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 655 | struct git_attr *attr = a->state[i].attr; |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 656 | const char **n = &(check[attr->attr_nr].value); |
| 657 | const char *v = a->state[i].setto; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 658 | |
| 659 | if (*n == ATTR__UNKNOWN) { |
Henrik Grubbström | 426c27b | 2010-04-06 14:46:42 +0200 | [diff] [blame] | 660 | debug_set(what, |
| 661 | a->is_macro ? a->u.attr->name : a->u.pattern, |
| 662 | attr, v); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 663 | *n = v; |
| 664 | rem--; |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 665 | rem = macroexpand_one(attr->attr_nr, rem); |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | return rem; |
| 669 | } |
| 670 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 671 | static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem) |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 672 | { |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 673 | int i; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 674 | const char *base = stk->origin ? stk->origin : ""; |
| 675 | |
| 676 | for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) { |
| 677 | struct match_attr *a = stk->attrs[i]; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 678 | if (a->is_macro) |
| 679 | continue; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 680 | if (path_matches(path, pathlen, |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 681 | a->u.pattern, base, strlen(base))) |
| 682 | rem = fill_one("fill", a, rem); |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 683 | } |
| 684 | return rem; |
| 685 | } |
| 686 | |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 687 | static int macroexpand_one(int attr_nr, int rem) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 688 | { |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 689 | struct attr_stack *stk; |
| 690 | struct match_attr *a = NULL; |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 691 | int i; |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 692 | |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 693 | if (check_all_attr[attr_nr].value != ATTR__TRUE) |
| 694 | return rem; |
| 695 | |
| 696 | for (stk = attr_stack; !a && stk; stk = stk->prev) |
| 697 | for (i = stk->num_matches - 1; !a && 0 <= i; i--) { |
| 698 | struct match_attr *ma = stk->attrs[i]; |
| 699 | if (!ma->is_macro) |
| 700 | continue; |
| 701 | if (ma->u.attr->attr_nr == attr_nr) |
| 702 | a = ma; |
| 703 | } |
| 704 | |
| 705 | if (a) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 706 | rem = fill_one("expand", a, rem); |
Henrik Grubbström | ec775c4 | 2010-04-06 14:46:44 +0200 | [diff] [blame] | 707 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 708 | return rem; |
| 709 | } |
| 710 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 711 | int git_checkattr(const char *path, int num, struct git_attr_check *check) |
| 712 | { |
| 713 | struct attr_stack *stk; |
| 714 | const char *cp; |
| 715 | int dirlen, pathlen, i, rem; |
| 716 | |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 717 | bootstrap_attr_stack(); |
| 718 | for (i = 0; i < attr_nr; i++) |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 719 | check_all_attr[i].value = ATTR__UNKNOWN; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 720 | |
| 721 | pathlen = strlen(path); |
| 722 | cp = strrchr(path, '/'); |
| 723 | if (!cp) |
| 724 | dirlen = 0; |
| 725 | else |
| 726 | dirlen = cp - path; |
| 727 | prepare_attr_stack(path, dirlen); |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 728 | rem = attr_nr; |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 729 | for (stk = attr_stack; 0 < rem && stk; stk = stk->prev) |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 730 | rem = fill(path, pathlen, stk, rem); |
| 731 | |
Junio C Hamano | 515106f | 2007-04-16 21:33:31 -0700 | [diff] [blame] | 732 | for (i = 0; i < num; i++) { |
Junio C Hamano | a5e92ab | 2007-04-18 16:16:37 -0700 | [diff] [blame] | 733 | 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] | 734 | if (value == ATTR__UNKNOWN) |
| 735 | value = ATTR__UNSET; |
| 736 | check[i].value = value; |
| 737 | } |
Junio C Hamano | f48fd68 | 2007-04-14 08:54:37 -0700 | [diff] [blame] | 738 | |
Junio C Hamano | d0bfd02 | 2007-04-12 01:07:32 -0700 | [diff] [blame] | 739 | return 0; |
| 740 | } |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 741 | |
| 742 | void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate) |
| 743 | { |
| 744 | enum git_attr_direction old = direction; |
Nguyễn Thái Ngọc Duy | 4191e80 | 2009-04-18 00:17:58 +0200 | [diff] [blame] | 745 | |
| 746 | if (is_bare_repository() && new != GIT_ATTR_INDEX) |
| 747 | die("BUG: non-INDEX attr direction in a bare repo"); |
| 748 | |
Junio C Hamano | 06f33c1 | 2009-03-13 21:24:08 -0700 | [diff] [blame] | 749 | direction = new; |
| 750 | if (new != old) |
| 751 | drop_attr_stack(); |
| 752 | use_index = istate; |
| 753 | } |