| 1 | /* |
| 2 | * Handle git attributes. See gitattributes(5) for a description of |
| 3 | * the file syntax, and attr.h for a description of the API. |
| 4 | * |
| 5 | * One basic design decision here is that we are not going to support |
| 6 | * an insanely large number of attributes. |
| 7 | */ |
| 8 | |
| 9 | #define USE_THE_REPOSITORY_VARIABLE |
| 10 | #define DISABLE_SIGN_COMPARE_WARNINGS |
| 11 | |
| 12 | #include "git-compat-util.h" |
| 13 | #include "config.h" |
| 14 | #include "environment.h" |
| 15 | #include "exec-cmd.h" |
| 16 | #include "attr.h" |
| 17 | #include "dir.h" |
| 18 | #include "gettext.h" |
| 19 | #include "path.h" |
| 20 | #include "utf8.h" |
| 21 | #include "quote.h" |
| 22 | #include "read-cache-ll.h" |
| 23 | #include "refs.h" |
| 24 | #include "revision.h" |
| 25 | #include "odb.h" |
| 26 | #include "setup.h" |
| 27 | #include "thread-utils.h" |
| 28 | #include "tree-walk.h" |
| 29 | #include "object-name.h" |
| 30 | |
| 31 | char *git_attr_tree; |
| 32 | |
| 33 | const char git_attr__true[] = "(builtin)true"; |
| 34 | const char git_attr__false[] = "\0(builtin)false"; |
| 35 | static const char git_attr__unknown[] = "(builtin)unknown"; |
| 36 | #define ATTR__TRUE git_attr__true |
| 37 | #define ATTR__FALSE git_attr__false |
| 38 | #define ATTR__UNSET NULL |
| 39 | #define ATTR__UNKNOWN git_attr__unknown |
| 40 | |
| 41 | struct git_attr { |
| 42 | unsigned int attr_nr; /* unique attribute number */ |
| 43 | char name[FLEX_ARRAY]; /* attribute name */ |
| 44 | }; |
| 45 | |
| 46 | const char *git_attr_name(const struct git_attr *attr) |
| 47 | { |
| 48 | return attr->name; |
| 49 | } |
| 50 | |
| 51 | struct attr_hashmap { |
| 52 | struct hashmap map; |
| 53 | pthread_mutex_t mutex; |
| 54 | }; |
| 55 | |
| 56 | static inline void hashmap_lock(struct attr_hashmap *map) |
| 57 | { |
| 58 | pthread_mutex_lock(&map->mutex); |
| 59 | } |
| 60 | |
| 61 | static inline void hashmap_unlock(struct attr_hashmap *map) |
| 62 | { |
| 63 | pthread_mutex_unlock(&map->mutex); |
| 64 | } |
| 65 | |
| 66 | /* The container for objects stored in "struct attr_hashmap" */ |
| 67 | struct attr_hash_entry { |
| 68 | struct hashmap_entry ent; |
| 69 | const char *key; /* the key; memory should be owned by value */ |
| 70 | size_t keylen; /* length of the key */ |
| 71 | void *value; /* the stored value */ |
| 72 | }; |
| 73 | |
| 74 | /* attr_hashmap comparison function */ |
| 75 | static int attr_hash_entry_cmp(const void *cmp_data UNUSED, |
| 76 | const struct hashmap_entry *eptr, |
| 77 | const struct hashmap_entry *entry_or_key, |
| 78 | const void *keydata UNUSED) |
| 79 | { |
| 80 | const struct attr_hash_entry *a, *b; |
| 81 | |
| 82 | a = container_of(eptr, const struct attr_hash_entry, ent); |
| 83 | b = container_of(entry_or_key, const struct attr_hash_entry, ent); |
| 84 | return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen); |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * The global dictionary of all interned attributes. This |
| 89 | * is a singleton object which is shared between threads. |
| 90 | * Access to this dictionary must be surrounded with a mutex. |
| 91 | */ |
| 92 | static struct attr_hashmap g_attr_hashmap = { |
| 93 | .map = HASHMAP_INIT(attr_hash_entry_cmp, NULL), |
| 94 | }; |
| 95 | |
| 96 | /* |
| 97 | * Retrieve the 'value' stored in a hashmap given the provided 'key'. |
| 98 | * If there is no matching entry, return NULL. |
| 99 | */ |
| 100 | static void *attr_hashmap_get(struct attr_hashmap *map, |
| 101 | const char *key, size_t keylen) |
| 102 | { |
| 103 | struct attr_hash_entry k; |
| 104 | struct attr_hash_entry *e; |
| 105 | |
| 106 | hashmap_entry_init(&k.ent, memhash(key, keylen)); |
| 107 | k.key = key; |
| 108 | k.keylen = keylen; |
| 109 | e = hashmap_get_entry(&map->map, &k, ent, NULL); |
| 110 | |
| 111 | return e ? e->value : NULL; |
| 112 | } |
| 113 | |
| 114 | /* Add 'value' to a hashmap based on the provided 'key'. */ |
| 115 | static void attr_hashmap_add(struct attr_hashmap *map, |
| 116 | const char *key, size_t keylen, |
| 117 | void *value) |
| 118 | { |
| 119 | struct attr_hash_entry *e; |
| 120 | |
| 121 | e = xmalloc(sizeof(struct attr_hash_entry)); |
| 122 | hashmap_entry_init(&e->ent, memhash(key, keylen)); |
| 123 | e->key = key; |
| 124 | e->keylen = keylen; |
| 125 | e->value = value; |
| 126 | |
| 127 | hashmap_add(&map->map, &e->ent); |
| 128 | } |
| 129 | |
| 130 | struct all_attrs_item { |
| 131 | const struct git_attr *attr; |
| 132 | const char *value; |
| 133 | /* |
| 134 | * If 'macro' is non-NULL, indicates that 'attr' is a macro based on |
| 135 | * the current attribute stack and contains a pointer to the match_attr |
| 136 | * definition of the macro |
| 137 | */ |
| 138 | const struct match_attr *macro; |
| 139 | }; |
| 140 | |
| 141 | /* |
| 142 | * Reallocate and reinitialize the array of all attributes (which is used in |
| 143 | * the attribute collection process) in 'check' based on the global dictionary |
| 144 | * of attributes. |
| 145 | */ |
| 146 | static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check) |
| 147 | { |
| 148 | int i; |
| 149 | unsigned int size; |
| 150 | |
| 151 | hashmap_lock(map); |
| 152 | |
| 153 | size = hashmap_get_size(&map->map); |
| 154 | if (size < check->all_attrs_nr) |
| 155 | BUG("interned attributes shouldn't be deleted"); |
| 156 | |
| 157 | /* |
| 158 | * If the number of attributes in the global dictionary has increased |
| 159 | * (or this attr_check instance doesn't have an initialized all_attrs |
| 160 | * field), reallocate the provided attr_check instance's all_attrs |
| 161 | * field and fill each entry with its corresponding git_attr. |
| 162 | */ |
| 163 | if (size != check->all_attrs_nr) { |
| 164 | struct attr_hash_entry *e; |
| 165 | struct hashmap_iter iter; |
| 166 | |
| 167 | REALLOC_ARRAY(check->all_attrs, size); |
| 168 | check->all_attrs_nr = size; |
| 169 | |
| 170 | hashmap_for_each_entry(&map->map, &iter, e, |
| 171 | ent /* member name */) { |
| 172 | const struct git_attr *a = e->value; |
| 173 | check->all_attrs[a->attr_nr].attr = a; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | hashmap_unlock(map); |
| 178 | |
| 179 | /* |
| 180 | * Re-initialize every entry in check->all_attrs. |
| 181 | * This re-initialization can live outside of the locked region since |
| 182 | * the attribute dictionary is no longer being accessed. |
| 183 | */ |
| 184 | for (i = 0; i < check->all_attrs_nr; i++) { |
| 185 | check->all_attrs[i].value = ATTR__UNKNOWN; |
| 186 | check->all_attrs[i].macro = NULL; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | * Attribute name cannot begin with "builtin_" which |
| 192 | * is a reserved namespace for built in attributes values. |
| 193 | */ |
| 194 | static int attr_name_reserved(const char *name) |
| 195 | { |
| 196 | return starts_with(name, "builtin_"); |
| 197 | } |
| 198 | |
| 199 | static int attr_name_valid(const char *name, size_t namelen) |
| 200 | { |
| 201 | /* |
| 202 | * Attribute name cannot begin with '-' and must consist of |
| 203 | * characters from [-A-Za-z0-9_.]. |
| 204 | */ |
| 205 | if (namelen <= 0 || *name == '-') |
| 206 | return 0; |
| 207 | while (namelen--) { |
| 208 | char ch = *name++; |
| 209 | if (! (ch == '-' || ch == '.' || ch == '_' || |
| 210 | ('0' <= ch && ch <= '9') || |
| 211 | ('a' <= ch && ch <= 'z') || |
| 212 | ('A' <= ch && ch <= 'Z')) ) |
| 213 | return 0; |
| 214 | } |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | static void report_invalid_attr(const char *name, size_t len, |
| 219 | const char *src, int lineno) |
| 220 | { |
| 221 | struct strbuf err = STRBUF_INIT; |
| 222 | strbuf_addf(&err, _("%.*s is not a valid attribute name"), |
| 223 | (int) len, name); |
| 224 | fprintf(stderr, "%s: %s:%d\n", err.buf, src, lineno); |
| 225 | strbuf_release(&err); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Given a 'name', lookup and return the corresponding attribute in the global |
| 230 | * dictionary. If no entry is found, create a new attribute and store it in |
| 231 | * the dictionary. |
| 232 | */ |
| 233 | static const struct git_attr *git_attr_internal(const char *name, size_t namelen) |
| 234 | { |
| 235 | struct git_attr *a; |
| 236 | |
| 237 | if (!attr_name_valid(name, namelen)) |
| 238 | return NULL; |
| 239 | |
| 240 | hashmap_lock(&g_attr_hashmap); |
| 241 | |
| 242 | a = attr_hashmap_get(&g_attr_hashmap, name, namelen); |
| 243 | |
| 244 | if (!a) { |
| 245 | FLEX_ALLOC_MEM(a, name, name, namelen); |
| 246 | a->attr_nr = hashmap_get_size(&g_attr_hashmap.map); |
| 247 | |
| 248 | attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a); |
| 249 | if (a->attr_nr != hashmap_get_size(&g_attr_hashmap.map) - 1) |
| 250 | die(_("unable to add additional attribute")); |
| 251 | } |
| 252 | |
| 253 | hashmap_unlock(&g_attr_hashmap); |
| 254 | |
| 255 | return a; |
| 256 | } |
| 257 | |
| 258 | const struct git_attr *git_attr(const char *name) |
| 259 | { |
| 260 | return git_attr_internal(name, strlen(name)); |
| 261 | } |
| 262 | |
| 263 | static const char blank[] = " \t\r\n"; |
| 264 | |
| 265 | /* Flags usable in read_attr() and parse_attr_line() family of functions. */ |
| 266 | #define READ_ATTR_MACRO_OK (1<<0) |
| 267 | #define READ_ATTR_NOFOLLOW (1<<1) |
| 268 | |
| 269 | /* |
| 270 | * Parse a whitespace-delimited attribute state (i.e., "attr", |
| 271 | * "-attr", "!attr", or "attr=value") from the string starting at src. |
| 272 | * If e is not NULL, write the results to *e. Return a pointer to the |
| 273 | * remainder of the string (with leading whitespace removed), or NULL |
| 274 | * if there was an error. |
| 275 | */ |
| 276 | static const char *parse_attr(const char *src, int lineno, const char *cp, |
| 277 | struct attr_state *e) |
| 278 | { |
| 279 | const char *ep, *equals; |
| 280 | size_t len; |
| 281 | |
| 282 | ep = cp + strcspn(cp, blank); |
| 283 | equals = strchr(cp, '='); |
| 284 | if (equals && ep < equals) |
| 285 | equals = NULL; |
| 286 | if (equals) |
| 287 | len = equals - cp; |
| 288 | else |
| 289 | len = ep - cp; |
| 290 | if (!e) { |
| 291 | if (*cp == '-' || *cp == '!') { |
| 292 | cp++; |
| 293 | len--; |
| 294 | } |
| 295 | if (!attr_name_valid(cp, len) || attr_name_reserved(cp)) { |
| 296 | report_invalid_attr(cp, len, src, lineno); |
| 297 | return NULL; |
| 298 | } |
| 299 | } else { |
| 300 | /* |
| 301 | * As this function is always called twice, once with |
| 302 | * e == NULL in the first pass and then e != NULL in |
| 303 | * the second pass, no need for attr_name_valid() |
| 304 | * check here. |
| 305 | */ |
| 306 | if (*cp == '-' || *cp == '!') { |
| 307 | e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET; |
| 308 | cp++; |
| 309 | len--; |
| 310 | } |
| 311 | else if (!equals) |
| 312 | e->setto = ATTR__TRUE; |
| 313 | else { |
| 314 | e->setto = xmemdupz(equals + 1, ep - equals - 1); |
| 315 | } |
| 316 | e->attr = git_attr_internal(cp, len); |
| 317 | } |
| 318 | return ep + strspn(ep, blank); |
| 319 | } |
| 320 | |
| 321 | struct match_attr *parse_attr_line(const char *line, const char *src, |
| 322 | int lineno, unsigned flags) |
| 323 | { |
| 324 | size_t namelen, num_attr, i; |
| 325 | const char *cp, *name, *states; |
| 326 | struct match_attr *res = NULL; |
| 327 | int is_macro; |
| 328 | struct strbuf pattern = STRBUF_INIT; |
| 329 | |
| 330 | cp = line + strspn(line, blank); |
| 331 | if (!*cp || *cp == '#') |
| 332 | return NULL; |
| 333 | name = cp; |
| 334 | |
| 335 | if (strlen(line) >= ATTR_MAX_LINE_LENGTH) { |
| 336 | warning(_("ignoring overly long attributes line %d"), lineno); |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) { |
| 341 | name = pattern.buf; |
| 342 | namelen = pattern.len; |
| 343 | } else { |
| 344 | namelen = strcspn(name, blank); |
| 345 | states = name + namelen; |
| 346 | } |
| 347 | |
| 348 | if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen && |
| 349 | starts_with(name, ATTRIBUTE_MACRO_PREFIX)) { |
| 350 | if (!(flags & READ_ATTR_MACRO_OK)) { |
| 351 | fprintf_ln(stderr, _("%s not allowed: %s:%d"), |
| 352 | name, src, lineno); |
| 353 | goto fail_return; |
| 354 | } |
| 355 | is_macro = 1; |
| 356 | name += strlen(ATTRIBUTE_MACRO_PREFIX); |
| 357 | name += strspn(name, blank); |
| 358 | namelen = strcspn(name, blank); |
| 359 | if (!attr_name_valid(name, namelen) || attr_name_reserved(name)) { |
| 360 | report_invalid_attr(name, namelen, src, lineno); |
| 361 | goto fail_return; |
| 362 | } |
| 363 | } |
| 364 | else |
| 365 | is_macro = 0; |
| 366 | |
| 367 | states += strspn(states, blank); |
| 368 | |
| 369 | /* First pass to count the attr_states */ |
| 370 | for (cp = states, num_attr = 0; *cp; num_attr++) { |
| 371 | cp = parse_attr(src, lineno, cp, NULL); |
| 372 | if (!cp) |
| 373 | goto fail_return; |
| 374 | } |
| 375 | |
| 376 | res = xcalloc(1, st_add3(sizeof(*res), |
| 377 | st_mult(sizeof(struct attr_state), num_attr), |
| 378 | is_macro ? 0 : namelen + 1)); |
| 379 | if (is_macro) { |
| 380 | res->u.attr = git_attr_internal(name, namelen); |
| 381 | } else { |
| 382 | char *p = (char *)&(res->state[num_attr]); |
| 383 | memcpy(p, name, namelen); |
| 384 | res->u.pat.pattern = p; |
| 385 | parse_path_pattern(&res->u.pat.pattern, |
| 386 | &res->u.pat.patternlen, |
| 387 | &res->u.pat.flags, |
| 388 | &res->u.pat.nowildcardlen); |
| 389 | if (res->u.pat.flags & PATTERN_FLAG_NEGATIVE) { |
| 390 | warning(_("Negative patterns are ignored in git attributes\n" |
| 391 | "Use '\\!' for literal leading exclamation.")); |
| 392 | goto fail_return; |
| 393 | } |
| 394 | } |
| 395 | res->is_macro = is_macro; |
| 396 | res->num_attr = num_attr; |
| 397 | |
| 398 | /* Second pass to fill the attr_states */ |
| 399 | for (cp = states, i = 0; *cp; i++) { |
| 400 | cp = parse_attr(src, lineno, cp, &(res->state[i])); |
| 401 | } |
| 402 | |
| 403 | strbuf_release(&pattern); |
| 404 | return res; |
| 405 | |
| 406 | fail_return: |
| 407 | strbuf_release(&pattern); |
| 408 | free(res); |
| 409 | return NULL; |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Like info/exclude and .gitignore, the attribute information can |
| 414 | * come from many places. |
| 415 | * |
| 416 | * (1) .gitattributes file of the same directory; |
| 417 | * (2) .gitattributes file of the parent directory if (1) does not have |
| 418 | * any match; this goes recursively upwards, just like .gitignore. |
| 419 | * (3) $GIT_DIR/info/attributes, which overrides both of the above. |
| 420 | * |
| 421 | * In the same file, later entries override the earlier match, so in the |
| 422 | * global list, we would have entries from info/attributes the earliest |
| 423 | * (reading the file from top to bottom), .gitattributes of the root |
| 424 | * directory (again, reading the file from top to bottom) down to the |
| 425 | * current directory, and then scan the list backwards to find the first match. |
| 426 | * This is exactly the same as what is_excluded() does in dir.c to deal with |
| 427 | * .gitignore file and info/excludes file as a fallback. |
| 428 | */ |
| 429 | |
| 430 | struct attr_stack { |
| 431 | struct attr_stack *prev; |
| 432 | char *origin; |
| 433 | size_t originlen; |
| 434 | unsigned num_matches; |
| 435 | unsigned alloc; |
| 436 | struct match_attr **attrs; |
| 437 | }; |
| 438 | |
| 439 | static void attr_stack_free(struct attr_stack *e) |
| 440 | { |
| 441 | unsigned i; |
| 442 | free(e->origin); |
| 443 | for (i = 0; i < e->num_matches; i++) { |
| 444 | struct match_attr *a = e->attrs[i]; |
| 445 | size_t j; |
| 446 | |
| 447 | for (j = 0; j < a->num_attr; j++) { |
| 448 | const char *setto = a->state[j].setto; |
| 449 | if (setto == ATTR__TRUE || |
| 450 | setto == ATTR__FALSE || |
| 451 | setto == ATTR__UNSET || |
| 452 | setto == ATTR__UNKNOWN) |
| 453 | ; |
| 454 | else |
| 455 | free((char *) setto); |
| 456 | } |
| 457 | free(a); |
| 458 | } |
| 459 | free(e->attrs); |
| 460 | free(e); |
| 461 | } |
| 462 | |
| 463 | static void drop_attr_stack(struct attr_stack **stack) |
| 464 | { |
| 465 | while (*stack) { |
| 466 | struct attr_stack *elem = *stack; |
| 467 | *stack = elem->prev; |
| 468 | attr_stack_free(elem); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | /* List of all attr_check structs; access should be surrounded by mutex */ |
| 473 | static struct check_vector { |
| 474 | size_t nr; |
| 475 | size_t alloc; |
| 476 | struct attr_check **checks; |
| 477 | pthread_mutex_t mutex; |
| 478 | } check_vector; |
| 479 | |
| 480 | static inline void vector_lock(void) |
| 481 | { |
| 482 | pthread_mutex_lock(&check_vector.mutex); |
| 483 | } |
| 484 | |
| 485 | static inline void vector_unlock(void) |
| 486 | { |
| 487 | pthread_mutex_unlock(&check_vector.mutex); |
| 488 | } |
| 489 | |
| 490 | static void check_vector_add(struct attr_check *c) |
| 491 | { |
| 492 | vector_lock(); |
| 493 | |
| 494 | ALLOC_GROW(check_vector.checks, |
| 495 | check_vector.nr + 1, |
| 496 | check_vector.alloc); |
| 497 | check_vector.checks[check_vector.nr++] = c; |
| 498 | |
| 499 | vector_unlock(); |
| 500 | } |
| 501 | |
| 502 | static void check_vector_remove(struct attr_check *check) |
| 503 | { |
| 504 | int i; |
| 505 | |
| 506 | vector_lock(); |
| 507 | |
| 508 | /* Find entry */ |
| 509 | for (i = 0; i < check_vector.nr; i++) |
| 510 | if (check_vector.checks[i] == check) |
| 511 | break; |
| 512 | |
| 513 | if (i >= check_vector.nr) |
| 514 | BUG("no entry found"); |
| 515 | |
| 516 | /* shift entries over */ |
| 517 | for (; i < check_vector.nr - 1; i++) |
| 518 | check_vector.checks[i] = check_vector.checks[i + 1]; |
| 519 | |
| 520 | check_vector.nr--; |
| 521 | |
| 522 | vector_unlock(); |
| 523 | } |
| 524 | |
| 525 | /* Iterate through all attr_check instances and drop their stacks */ |
| 526 | static void drop_all_attr_stacks(void) |
| 527 | { |
| 528 | int i; |
| 529 | |
| 530 | vector_lock(); |
| 531 | |
| 532 | for (i = 0; i < check_vector.nr; i++) { |
| 533 | drop_attr_stack(&check_vector.checks[i]->stack); |
| 534 | } |
| 535 | |
| 536 | vector_unlock(); |
| 537 | } |
| 538 | |
| 539 | struct attr_check *attr_check_alloc(void) |
| 540 | { |
| 541 | struct attr_check *c = xcalloc(1, sizeof(struct attr_check)); |
| 542 | |
| 543 | /* save pointer to the check struct */ |
| 544 | check_vector_add(c); |
| 545 | |
| 546 | return c; |
| 547 | } |
| 548 | |
| 549 | struct attr_check *attr_check_initl(const char *one, ...) |
| 550 | { |
| 551 | struct attr_check *check; |
| 552 | int cnt; |
| 553 | va_list params; |
| 554 | const char *param; |
| 555 | |
| 556 | va_start(params, one); |
| 557 | for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++) |
| 558 | ; |
| 559 | va_end(params); |
| 560 | |
| 561 | check = attr_check_alloc(); |
| 562 | check->nr = cnt; |
| 563 | check->alloc = cnt; |
| 564 | CALLOC_ARRAY(check->items, cnt); |
| 565 | |
| 566 | check->items[0].attr = git_attr(one); |
| 567 | va_start(params, one); |
| 568 | for (cnt = 1; cnt < check->nr; cnt++) { |
| 569 | const struct git_attr *attr; |
| 570 | param = va_arg(params, const char *); |
| 571 | if (!param) |
| 572 | BUG("counted %d != ended at %d", |
| 573 | check->nr, cnt); |
| 574 | attr = git_attr(param); |
| 575 | if (!attr) |
| 576 | BUG("%s: not a valid attribute name", param); |
| 577 | check->items[cnt].attr = attr; |
| 578 | } |
| 579 | va_end(params); |
| 580 | return check; |
| 581 | } |
| 582 | |
| 583 | struct attr_check *attr_check_dup(const struct attr_check *check) |
| 584 | { |
| 585 | struct attr_check *ret; |
| 586 | |
| 587 | if (!check) |
| 588 | return NULL; |
| 589 | |
| 590 | ret = attr_check_alloc(); |
| 591 | |
| 592 | ret->nr = check->nr; |
| 593 | ret->alloc = check->alloc; |
| 594 | DUP_ARRAY(ret->items, check->items, ret->nr); |
| 595 | |
| 596 | return ret; |
| 597 | } |
| 598 | |
| 599 | struct attr_check_item *attr_check_append(struct attr_check *check, |
| 600 | const struct git_attr *attr) |
| 601 | { |
| 602 | struct attr_check_item *item; |
| 603 | |
| 604 | ALLOC_GROW(check->items, check->nr + 1, check->alloc); |
| 605 | item = &check->items[check->nr++]; |
| 606 | item->attr = attr; |
| 607 | return item; |
| 608 | } |
| 609 | |
| 610 | void attr_check_reset(struct attr_check *check) |
| 611 | { |
| 612 | check->nr = 0; |
| 613 | } |
| 614 | |
| 615 | void attr_check_clear(struct attr_check *check) |
| 616 | { |
| 617 | FREE_AND_NULL(check->items); |
| 618 | check->alloc = 0; |
| 619 | check->nr = 0; |
| 620 | |
| 621 | FREE_AND_NULL(check->all_attrs); |
| 622 | check->all_attrs_nr = 0; |
| 623 | |
| 624 | drop_attr_stack(&check->stack); |
| 625 | } |
| 626 | |
| 627 | void attr_check_free(struct attr_check *check) |
| 628 | { |
| 629 | if (check) { |
| 630 | /* Remove check from the check vector */ |
| 631 | check_vector_remove(check); |
| 632 | |
| 633 | attr_check_clear(check); |
| 634 | free(check); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | static const char *builtin_attr[] = { |
| 639 | "[attr]binary -diff -merge -text", |
| 640 | NULL, |
| 641 | }; |
| 642 | |
| 643 | static void handle_attr_line(struct attr_stack *res, |
| 644 | const char *line, |
| 645 | const char *src, |
| 646 | int lineno, |
| 647 | unsigned flags) |
| 648 | { |
| 649 | struct match_attr *a; |
| 650 | |
| 651 | a = parse_attr_line(line, src, lineno, flags); |
| 652 | if (!a) |
| 653 | return; |
| 654 | ALLOC_GROW_BY(res->attrs, res->num_matches, 1, res->alloc); |
| 655 | res->attrs[res->num_matches - 1] = a; |
| 656 | } |
| 657 | |
| 658 | static struct attr_stack *read_attr_from_array(const char **list) |
| 659 | { |
| 660 | struct attr_stack *res; |
| 661 | const char *line; |
| 662 | int lineno = 0; |
| 663 | |
| 664 | CALLOC_ARRAY(res, 1); |
| 665 | while ((line = *(list++)) != NULL) |
| 666 | handle_attr_line(res, line, "[builtin]", ++lineno, |
| 667 | READ_ATTR_MACRO_OK); |
| 668 | return res; |
| 669 | } |
| 670 | |
| 671 | /* |
| 672 | * Callers into the attribute system assume there is a single, system-wide |
| 673 | * global state where attributes are read from and when the state is flipped by |
| 674 | * calling git_attr_set_direction(), the stack frames that have been |
| 675 | * constructed need to be discarded so that subsequent calls into the |
| 676 | * attribute system will lazily read from the right place. Since changing |
| 677 | * direction causes a global paradigm shift, it should not ever be called while |
| 678 | * another thread could potentially be calling into the attribute system. |
| 679 | */ |
| 680 | static enum git_attr_direction direction; |
| 681 | |
| 682 | void git_attr_set_direction(enum git_attr_direction new_direction) |
| 683 | { |
| 684 | if (is_bare_repository(the_repository) && new_direction != GIT_ATTR_INDEX) |
| 685 | BUG("non-INDEX attr direction in a bare repo"); |
| 686 | |
| 687 | if (new_direction != direction) |
| 688 | drop_all_attr_stacks(); |
| 689 | |
| 690 | direction = new_direction; |
| 691 | } |
| 692 | |
| 693 | static struct attr_stack *read_attr_from_file(const char *path, unsigned flags) |
| 694 | { |
| 695 | struct strbuf buf = STRBUF_INIT; |
| 696 | int fd; |
| 697 | FILE *fp; |
| 698 | struct attr_stack *res; |
| 699 | int lineno = 0; |
| 700 | struct stat st; |
| 701 | |
| 702 | if (flags & READ_ATTR_NOFOLLOW) |
| 703 | fd = open_nofollow(path, O_RDONLY); |
| 704 | else |
| 705 | fd = open(path, O_RDONLY); |
| 706 | |
| 707 | if (fd < 0) { |
| 708 | warn_on_fopen_errors(path); |
| 709 | return NULL; |
| 710 | } |
| 711 | fp = xfdopen(fd, "r"); |
| 712 | if (fstat(fd, &st)) { |
| 713 | warning_errno(_("cannot fstat gitattributes file '%s'"), path); |
| 714 | fclose(fp); |
| 715 | return NULL; |
| 716 | } |
| 717 | if (st.st_size >= ATTR_MAX_FILE_SIZE) { |
| 718 | warning(_("ignoring overly large gitattributes file '%s'"), path); |
| 719 | fclose(fp); |
| 720 | return NULL; |
| 721 | } |
| 722 | |
| 723 | CALLOC_ARRAY(res, 1); |
| 724 | while (strbuf_getline(&buf, fp) != EOF) { |
| 725 | if (!lineno && starts_with(buf.buf, utf8_bom)) |
| 726 | strbuf_remove(&buf, 0, strlen(utf8_bom)); |
| 727 | handle_attr_line(res, buf.buf, path, ++lineno, flags); |
| 728 | } |
| 729 | |
| 730 | fclose(fp); |
| 731 | strbuf_release(&buf); |
| 732 | return res; |
| 733 | } |
| 734 | |
| 735 | static struct attr_stack *read_attr_from_buf(char *buf, size_t length, |
| 736 | const char *path, unsigned flags) |
| 737 | { |
| 738 | struct attr_stack *res; |
| 739 | char *sp; |
| 740 | int lineno = 0; |
| 741 | |
| 742 | if (!buf) |
| 743 | return NULL; |
| 744 | if (length >= ATTR_MAX_FILE_SIZE) { |
| 745 | warning(_("ignoring overly large gitattributes blob '%s'"), path); |
| 746 | free(buf); |
| 747 | return NULL; |
| 748 | } |
| 749 | |
| 750 | CALLOC_ARRAY(res, 1); |
| 751 | for (sp = buf; *sp;) { |
| 752 | char *ep; |
| 753 | int more; |
| 754 | |
| 755 | ep = strchrnul(sp, '\n'); |
| 756 | more = (*ep == '\n'); |
| 757 | *ep = '\0'; |
| 758 | handle_attr_line(res, sp, path, ++lineno, flags); |
| 759 | sp = ep + more; |
| 760 | } |
| 761 | free(buf); |
| 762 | |
| 763 | return res; |
| 764 | } |
| 765 | |
| 766 | static struct attr_stack *read_attr_from_blob(struct index_state *istate, |
| 767 | const struct object_id *tree_oid, |
| 768 | const char *path, unsigned flags) |
| 769 | { |
| 770 | struct object_id oid; |
| 771 | size_t sz; |
| 772 | enum object_type type; |
| 773 | void *buf; |
| 774 | unsigned short mode; |
| 775 | |
| 776 | if (!tree_oid) |
| 777 | return NULL; |
| 778 | |
| 779 | if (get_tree_entry(istate->repo, tree_oid, path, &oid, &mode)) |
| 780 | return NULL; |
| 781 | |
| 782 | buf = odb_read_object(istate->repo->objects, &oid, &type, &sz); |
| 783 | if (!buf || type != OBJ_BLOB) { |
| 784 | free(buf); |
| 785 | return NULL; |
| 786 | } |
| 787 | |
| 788 | return read_attr_from_buf(buf, sz, path, flags); |
| 789 | } |
| 790 | |
| 791 | static struct attr_stack *read_attr_from_index(struct index_state *istate, |
| 792 | const char *path, unsigned flags) |
| 793 | { |
| 794 | struct attr_stack *stack = NULL; |
| 795 | char *buf; |
| 796 | unsigned long size; |
| 797 | int sparse_dir_pos = -1; |
| 798 | |
| 799 | if (!istate) |
| 800 | return NULL; |
| 801 | |
| 802 | /* |
| 803 | * When handling sparse-checkouts, .gitattributes files |
| 804 | * may reside within a sparse directory. We distinguish |
| 805 | * whether a path exists directly in the index or not by |
| 806 | * evaluating if 'pos' is negative. |
| 807 | * If 'pos' is negative, the path is not directly present |
| 808 | * in the index and is likely within a sparse directory. |
| 809 | * For paths not in the index, The absolute value of 'pos' |
| 810 | * minus 1 gives us the position where the path would be |
| 811 | * inserted in lexicographic order within the index. |
| 812 | * We then subtract another 1 from this value |
| 813 | * (sparse_dir_pos = -pos - 2) to find the position of the |
| 814 | * last index entry which is lexicographically smaller than |
| 815 | * the path. This would be the sparse directory containing |
| 816 | * the path. By identifying the sparse directory containing |
| 817 | * the path, we can correctly read the attributes specified |
| 818 | * in the .gitattributes file from the tree object of the |
| 819 | * sparse directory. |
| 820 | */ |
| 821 | if (!path_in_cone_mode_sparse_checkout(path, istate)) { |
| 822 | int pos = index_name_pos_sparse(istate, path, strlen(path)); |
| 823 | |
| 824 | if (pos < 0) |
| 825 | sparse_dir_pos = -pos - 2; |
| 826 | } |
| 827 | |
| 828 | if (sparse_dir_pos >= 0 && |
| 829 | S_ISSPARSEDIR(istate->cache[sparse_dir_pos]->ce_mode) && |
| 830 | !strncmp(istate->cache[sparse_dir_pos]->name, path, ce_namelen(istate->cache[sparse_dir_pos]))) { |
| 831 | const char *relative_path = path + ce_namelen(istate->cache[sparse_dir_pos]); |
| 832 | stack = read_attr_from_blob(istate, &istate->cache[sparse_dir_pos]->oid, relative_path, flags); |
| 833 | } else { |
| 834 | buf = read_blob_data_from_index(istate, path, &size); |
| 835 | if (buf) |
| 836 | stack = read_attr_from_buf(buf, size, path, flags); |
| 837 | } |
| 838 | return stack; |
| 839 | } |
| 840 | |
| 841 | static struct attr_stack *read_attr(struct index_state *istate, |
| 842 | const struct object_id *tree_oid, |
| 843 | const char *path, unsigned flags) |
| 844 | { |
| 845 | struct attr_stack *res = NULL; |
| 846 | |
| 847 | if (direction == GIT_ATTR_INDEX) { |
| 848 | res = read_attr_from_index(istate, path, flags); |
| 849 | } else if (tree_oid) { |
| 850 | res = read_attr_from_blob(istate, tree_oid, path, flags); |
| 851 | } else if (!is_bare_repository(the_repository)) { |
| 852 | if (direction == GIT_ATTR_CHECKOUT) { |
| 853 | res = read_attr_from_index(istate, path, flags); |
| 854 | if (!res) |
| 855 | res = read_attr_from_file(path, flags); |
| 856 | } else if (direction == GIT_ATTR_CHECKIN) { |
| 857 | res = read_attr_from_file(path, flags); |
| 858 | if (!res) |
| 859 | /* |
| 860 | * There is no checked out .gitattributes file |
| 861 | * there, but we might have it in the index. |
| 862 | * We allow operation in a sparsely checked out |
| 863 | * work tree, so read from it. |
| 864 | */ |
| 865 | res = read_attr_from_index(istate, path, flags); |
| 866 | } |
| 867 | } |
| 868 | |
| 869 | if (!res) |
| 870 | CALLOC_ARRAY(res, 1); |
| 871 | return res; |
| 872 | } |
| 873 | |
| 874 | const char *git_attr_system_file(void) |
| 875 | { |
| 876 | static const char *system_wide; |
| 877 | if (!system_wide) |
| 878 | system_wide = system_path(ETC_GITATTRIBUTES); |
| 879 | return system_wide; |
| 880 | } |
| 881 | |
| 882 | const char *git_attr_global_file(void) |
| 883 | { |
| 884 | struct repo_config_values *cfg = repo_config_values(the_repository); |
| 885 | if (!cfg->attributes_file) |
| 886 | cfg->attributes_file = xdg_config_home("attributes"); |
| 887 | |
| 888 | return cfg->attributes_file; |
| 889 | } |
| 890 | |
| 891 | int git_attr_system_is_enabled(void) |
| 892 | { |
| 893 | return !git_env_bool("GIT_ATTR_NOSYSTEM", 0); |
| 894 | } |
| 895 | |
| 896 | static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE) |
| 897 | |
| 898 | static void push_stack(struct attr_stack **attr_stack_p, |
| 899 | struct attr_stack *elem, char *origin, size_t originlen) |
| 900 | { |
| 901 | if (elem) { |
| 902 | elem->origin = origin; |
| 903 | if (origin) |
| 904 | elem->originlen = originlen; |
| 905 | elem->prev = *attr_stack_p; |
| 906 | *attr_stack_p = elem; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | static void bootstrap_attr_stack(struct index_state *istate, |
| 911 | const struct object_id *tree_oid, |
| 912 | struct attr_stack **stack) |
| 913 | { |
| 914 | struct attr_stack *e; |
| 915 | unsigned flags = READ_ATTR_MACRO_OK; |
| 916 | |
| 917 | if (*stack) |
| 918 | return; |
| 919 | |
| 920 | /* builtin frame */ |
| 921 | e = read_attr_from_array(builtin_attr); |
| 922 | push_stack(stack, e, NULL, 0); |
| 923 | |
| 924 | /* system-wide frame */ |
| 925 | if (git_attr_system_is_enabled()) { |
| 926 | e = read_attr_from_file(git_attr_system_file(), flags); |
| 927 | push_stack(stack, e, NULL, 0); |
| 928 | } |
| 929 | |
| 930 | /* home directory */ |
| 931 | if (git_attr_global_file()) { |
| 932 | e = read_attr_from_file(git_attr_global_file(), flags); |
| 933 | push_stack(stack, e, NULL, 0); |
| 934 | } |
| 935 | |
| 936 | /* root directory */ |
| 937 | e = read_attr(istate, tree_oid, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW); |
| 938 | push_stack(stack, e, xstrdup(""), 0); |
| 939 | |
| 940 | /* info frame */ |
| 941 | if (startup_info->have_repository) |
| 942 | e = read_attr_from_file(git_path_info_attributes(), flags); |
| 943 | else |
| 944 | e = NULL; |
| 945 | if (!e) |
| 946 | CALLOC_ARRAY(e, 1); |
| 947 | push_stack(stack, e, NULL, 0); |
| 948 | } |
| 949 | |
| 950 | static void prepare_attr_stack(struct index_state *istate, |
| 951 | const struct object_id *tree_oid, |
| 952 | const char *path, int dirlen, |
| 953 | struct attr_stack **stack) |
| 954 | { |
| 955 | struct attr_stack *info; |
| 956 | struct strbuf pathbuf = STRBUF_INIT; |
| 957 | |
| 958 | /* |
| 959 | * At the bottom of the attribute stack is the built-in |
| 960 | * set of attribute definitions, followed by the contents |
| 961 | * of $(prefix)/etc/gitattributes and a file specified by |
| 962 | * core.attributesfile. Then, contents from |
| 963 | * .gitattributes files from directories closer to the |
| 964 | * root to the ones in deeper directories are pushed |
| 965 | * to the stack. Finally, at the very top of the stack |
| 966 | * we always keep the contents of $GIT_DIR/info/attributes. |
| 967 | * |
| 968 | * When checking, we use entries from near the top of the |
| 969 | * stack, preferring $GIT_DIR/info/attributes, then |
| 970 | * .gitattributes in deeper directories to shallower ones, |
| 971 | * and finally use the built-in set as the default. |
| 972 | */ |
| 973 | bootstrap_attr_stack(istate, tree_oid, stack); |
| 974 | |
| 975 | /* |
| 976 | * Pop the "info" one that is always at the top of the stack. |
| 977 | */ |
| 978 | info = *stack; |
| 979 | *stack = info->prev; |
| 980 | |
| 981 | /* |
| 982 | * Pop the ones from directories that are not the prefix of |
| 983 | * the path we are checking. Break out of the loop when we see |
| 984 | * the root one (whose origin is an empty string "") or the builtin |
| 985 | * one (whose origin is NULL) without popping it. |
| 986 | */ |
| 987 | while ((*stack)->origin) { |
| 988 | int namelen = (*stack)->originlen; |
| 989 | struct attr_stack *elem; |
| 990 | |
| 991 | elem = *stack; |
| 992 | if (namelen <= dirlen && |
| 993 | !strncmp(elem->origin, path, namelen) && |
| 994 | (!namelen || path[namelen] == '/')) |
| 995 | break; |
| 996 | |
| 997 | *stack = elem->prev; |
| 998 | attr_stack_free(elem); |
| 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * bootstrap_attr_stack() should have added, and the |
| 1003 | * above loop should have stopped before popping, the |
| 1004 | * root element whose attr_stack->origin is set to an |
| 1005 | * empty string. |
| 1006 | */ |
| 1007 | assert((*stack)->origin); |
| 1008 | |
| 1009 | strbuf_addstr(&pathbuf, (*stack)->origin); |
| 1010 | /* Build up to the directory 'path' is in */ |
| 1011 | while (pathbuf.len < dirlen) { |
| 1012 | size_t len = pathbuf.len; |
| 1013 | struct attr_stack *next; |
| 1014 | char *origin; |
| 1015 | |
| 1016 | /* Skip path-separator */ |
| 1017 | if (len < dirlen && is_dir_sep(path[len])) |
| 1018 | len++; |
| 1019 | /* Find the end of the next component */ |
| 1020 | while (len < dirlen && !is_dir_sep(path[len])) |
| 1021 | len++; |
| 1022 | |
| 1023 | if (pathbuf.len > 0) |
| 1024 | strbuf_addch(&pathbuf, '/'); |
| 1025 | strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len)); |
| 1026 | strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE); |
| 1027 | |
| 1028 | next = read_attr(istate, tree_oid, pathbuf.buf, READ_ATTR_NOFOLLOW); |
| 1029 | |
| 1030 | /* reset the pathbuf to not include "/.gitattributes" */ |
| 1031 | strbuf_setlen(&pathbuf, len); |
| 1032 | |
| 1033 | origin = xstrdup(pathbuf.buf); |
| 1034 | push_stack(stack, next, origin, len); |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Finally push the "info" one at the top of the stack. |
| 1039 | */ |
| 1040 | push_stack(stack, info, NULL, 0); |
| 1041 | |
| 1042 | strbuf_release(&pathbuf); |
| 1043 | } |
| 1044 | |
| 1045 | static int path_matches(const char *pathname, int pathlen, |
| 1046 | int basename_offset, |
| 1047 | const struct pattern *pat, |
| 1048 | const char *base, int baselen) |
| 1049 | { |
| 1050 | const char *pattern = pat->pattern; |
| 1051 | int prefix = pat->nowildcardlen; |
| 1052 | int isdir = (pathlen && pathname[pathlen - 1] == '/'); |
| 1053 | |
| 1054 | if ((pat->flags & PATTERN_FLAG_MUSTBEDIR) && !isdir) |
| 1055 | return 0; |
| 1056 | |
| 1057 | if (pat->flags & PATTERN_FLAG_NODIR) { |
| 1058 | return match_basename(pathname + basename_offset, |
| 1059 | pathlen - basename_offset - isdir, |
| 1060 | pattern, prefix, |
| 1061 | pat->patternlen, pat->flags); |
| 1062 | } |
| 1063 | return match_pathname(pathname, pathlen - isdir, |
| 1064 | base, baselen, |
| 1065 | pattern, prefix, pat->patternlen); |
| 1066 | } |
| 1067 | |
| 1068 | struct attr_state_queue { |
| 1069 | const struct attr_state **items; |
| 1070 | size_t alloc, nr; |
| 1071 | }; |
| 1072 | |
| 1073 | static void attr_state_queue_push(struct attr_state_queue *t, |
| 1074 | const struct match_attr *a) |
| 1075 | { |
| 1076 | for (size_t i = 0; i < a->num_attr; i++) { |
| 1077 | ALLOC_GROW(t->items, t->nr + 1, t->alloc); |
| 1078 | t->items[t->nr++] = &a->state[i]; |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | static const struct attr_state *attr_state_queue_pop(struct attr_state_queue *t) |
| 1083 | { |
| 1084 | return t->nr ? t->items[--t->nr] : NULL; |
| 1085 | } |
| 1086 | |
| 1087 | static void attr_state_queue_release(struct attr_state_queue *t) |
| 1088 | { |
| 1089 | free(t->items); |
| 1090 | } |
| 1091 | |
| 1092 | static int fill_one(struct all_attrs_item *all_attrs, |
| 1093 | const struct match_attr *a, int rem) |
| 1094 | { |
| 1095 | struct attr_state_queue todo = { 0 }; |
| 1096 | const struct attr_state *state; |
| 1097 | |
| 1098 | attr_state_queue_push(&todo, a); |
| 1099 | while (rem > 0 && (state = attr_state_queue_pop(&todo))) { |
| 1100 | const struct git_attr *attr = state->attr; |
| 1101 | const char **n = &(all_attrs[attr->attr_nr].value); |
| 1102 | const char *v = state->setto; |
| 1103 | |
| 1104 | if (*n == ATTR__UNKNOWN) { |
| 1105 | const struct all_attrs_item *item = |
| 1106 | &all_attrs[attr->attr_nr]; |
| 1107 | *n = v; |
| 1108 | rem--; |
| 1109 | if (item->macro && item->value == ATTR__TRUE) |
| 1110 | attr_state_queue_push(&todo, item->macro); |
| 1111 | } |
| 1112 | } |
| 1113 | attr_state_queue_release(&todo); |
| 1114 | return rem; |
| 1115 | } |
| 1116 | |
| 1117 | static int fill(const char *path, int pathlen, int basename_offset, |
| 1118 | const struct attr_stack *stack, |
| 1119 | struct all_attrs_item *all_attrs, int rem) |
| 1120 | { |
| 1121 | for (; rem > 0 && stack; stack = stack->prev) { |
| 1122 | unsigned i; |
| 1123 | const char *base = stack->origin ? stack->origin : ""; |
| 1124 | |
| 1125 | for (i = stack->num_matches; 0 < rem && 0 < i; i--) { |
| 1126 | const struct match_attr *a = stack->attrs[i - 1]; |
| 1127 | if (a->is_macro) |
| 1128 | continue; |
| 1129 | if (path_matches(path, pathlen, basename_offset, |
| 1130 | &a->u.pat, base, stack->originlen)) |
| 1131 | rem = fill_one(all_attrs, a, rem); |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | return rem; |
| 1136 | } |
| 1137 | |
| 1138 | /* |
| 1139 | * Marks the attributes which are macros based on the attribute stack. |
| 1140 | * This prevents having to search through the attribute stack each time |
| 1141 | * a macro needs to be expanded during the fill stage. |
| 1142 | */ |
| 1143 | static void determine_macros(struct all_attrs_item *all_attrs, |
| 1144 | const struct attr_stack *stack) |
| 1145 | { |
| 1146 | for (; stack; stack = stack->prev) { |
| 1147 | unsigned i; |
| 1148 | for (i = stack->num_matches; i > 0; i--) { |
| 1149 | const struct match_attr *ma = stack->attrs[i - 1]; |
| 1150 | if (ma->is_macro) { |
| 1151 | unsigned int n = ma->u.attr->attr_nr; |
| 1152 | if (!all_attrs[n].macro) { |
| 1153 | all_attrs[n].macro = ma; |
| 1154 | } |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | } |
| 1159 | |
| 1160 | /* |
| 1161 | * Collect attributes for path into the array pointed to by check->all_attrs. |
| 1162 | * If check->check_nr is non-zero, only attributes in check[] are collected. |
| 1163 | * Otherwise all attributes are collected. |
| 1164 | */ |
| 1165 | static void collect_some_attrs(struct index_state *istate, |
| 1166 | const struct object_id *tree_oid, |
| 1167 | const char *path, struct attr_check *check) |
| 1168 | { |
| 1169 | int pathlen, rem, dirlen; |
| 1170 | const char *cp, *last_slash = NULL; |
| 1171 | int basename_offset; |
| 1172 | |
| 1173 | for (cp = path; *cp; cp++) { |
| 1174 | if (*cp == '/' && cp[1]) |
| 1175 | last_slash = cp; |
| 1176 | } |
| 1177 | pathlen = cp - path; |
| 1178 | if (last_slash) { |
| 1179 | basename_offset = last_slash + 1 - path; |
| 1180 | dirlen = last_slash - path; |
| 1181 | } else { |
| 1182 | basename_offset = 0; |
| 1183 | dirlen = 0; |
| 1184 | } |
| 1185 | |
| 1186 | prepare_attr_stack(istate, tree_oid, path, dirlen, &check->stack); |
| 1187 | all_attrs_init(&g_attr_hashmap, check); |
| 1188 | determine_macros(check->all_attrs, check->stack); |
| 1189 | |
| 1190 | rem = check->all_attrs_nr; |
| 1191 | fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem); |
| 1192 | } |
| 1193 | |
| 1194 | static const char *default_attr_source_tree_object_name; |
| 1195 | |
| 1196 | void set_git_attr_source(const char *tree_object_name) |
| 1197 | { |
| 1198 | default_attr_source_tree_object_name = xstrdup(tree_object_name); |
| 1199 | } |
| 1200 | |
| 1201 | static int compute_default_attr_source(struct object_id *attr_source) |
| 1202 | { |
| 1203 | int ignore_bad_attr_tree = 0; |
| 1204 | |
| 1205 | if (!default_attr_source_tree_object_name) |
| 1206 | default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT); |
| 1207 | |
| 1208 | if (!default_attr_source_tree_object_name && git_attr_tree) { |
| 1209 | default_attr_source_tree_object_name = git_attr_tree; |
| 1210 | ignore_bad_attr_tree = 1; |
| 1211 | } |
| 1212 | |
| 1213 | if (!default_attr_source_tree_object_name) |
| 1214 | return 0; |
| 1215 | |
| 1216 | if (!startup_info->have_repository) { |
| 1217 | if (!ignore_bad_attr_tree) |
| 1218 | die(_("cannot use --attr-source or GIT_ATTR_SOURCE without repo")); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | if (repo_get_oid_treeish(the_repository, |
| 1223 | default_attr_source_tree_object_name, |
| 1224 | attr_source)) { |
| 1225 | if (!ignore_bad_attr_tree) |
| 1226 | die(_("bad --attr-source or GIT_ATTR_SOURCE")); |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | return 1; |
| 1231 | } |
| 1232 | |
| 1233 | static struct object_id *default_attr_source(void) |
| 1234 | { |
| 1235 | static struct object_id attr_source; |
| 1236 | static int has_attr_source = -1; |
| 1237 | |
| 1238 | if (has_attr_source < 0) |
| 1239 | has_attr_source = compute_default_attr_source(&attr_source); |
| 1240 | if (!has_attr_source) |
| 1241 | return NULL; |
| 1242 | return &attr_source; |
| 1243 | } |
| 1244 | |
| 1245 | static const char *interned_mode_string(unsigned int mode) |
| 1246 | { |
| 1247 | static struct { |
| 1248 | unsigned int val; |
| 1249 | char str[7]; |
| 1250 | } mode_string[] = { |
| 1251 | { .val = 0040000 }, |
| 1252 | { .val = 0100644 }, |
| 1253 | { .val = 0100755 }, |
| 1254 | { .val = 0120000 }, |
| 1255 | { .val = 0160000 }, |
| 1256 | }; |
| 1257 | int i; |
| 1258 | |
| 1259 | for (i = 0; i < ARRAY_SIZE(mode_string); i++) { |
| 1260 | if (mode_string[i].val != mode) |
| 1261 | continue; |
| 1262 | if (!*mode_string[i].str) |
| 1263 | snprintf(mode_string[i].str, sizeof(mode_string[i].str), |
| 1264 | "%06o", mode); |
| 1265 | return mode_string[i].str; |
| 1266 | } |
| 1267 | BUG("Unsupported mode 0%o", mode); |
| 1268 | } |
| 1269 | |
| 1270 | static const char *builtin_object_mode_attr(struct index_state *istate, const char *path) |
| 1271 | { |
| 1272 | unsigned int mode; |
| 1273 | |
| 1274 | if (direction == GIT_ATTR_CHECKIN) { |
| 1275 | struct object_id oid; |
| 1276 | struct stat st; |
| 1277 | if (lstat(path, &st)) |
| 1278 | die_errno(_("unable to stat '%s'"), path); |
| 1279 | mode = canon_mode(st.st_mode); |
| 1280 | if (S_ISDIR(mode)) { |
| 1281 | /* |
| 1282 | *`path` is either a directory or it is a submodule, |
| 1283 | * in which case it is already indexed as submodule |
| 1284 | * or it does not exist in the index yet and we need to |
| 1285 | * check if we can resolve to a ref. |
| 1286 | */ |
| 1287 | int pos = index_name_pos(istate, path, strlen(path)); |
| 1288 | if (pos >= 0) { |
| 1289 | if (S_ISGITLINK(istate->cache[pos]->ce_mode)) |
| 1290 | mode = istate->cache[pos]->ce_mode; |
| 1291 | } else if (repo_resolve_gitlink_ref(the_repository, path, |
| 1292 | "HEAD", &oid) == 0) { |
| 1293 | mode = S_IFGITLINK; |
| 1294 | } |
| 1295 | } |
| 1296 | } else { |
| 1297 | /* |
| 1298 | * For GIT_ATTR_CHECKOUT and GIT_ATTR_INDEX we only check |
| 1299 | * for mode in the index. |
| 1300 | */ |
| 1301 | int pos = index_name_pos(istate, path, strlen(path)); |
| 1302 | if (pos >= 0) |
| 1303 | mode = istate->cache[pos]->ce_mode; |
| 1304 | else |
| 1305 | return ATTR__UNSET; |
| 1306 | } |
| 1307 | |
| 1308 | return interned_mode_string(mode); |
| 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | static const char *compute_builtin_attr(struct index_state *istate, |
| 1313 | const char *path, |
| 1314 | const struct git_attr *attr) { |
| 1315 | static const struct git_attr *object_mode_attr; |
| 1316 | |
| 1317 | if (!object_mode_attr) |
| 1318 | object_mode_attr = git_attr("builtin_objectmode"); |
| 1319 | |
| 1320 | if (attr == object_mode_attr) |
| 1321 | return builtin_object_mode_attr(istate, path); |
| 1322 | return ATTR__UNSET; |
| 1323 | } |
| 1324 | |
| 1325 | void git_check_attr(struct index_state *istate, |
| 1326 | const char *path, |
| 1327 | struct attr_check *check) |
| 1328 | { |
| 1329 | int i; |
| 1330 | const struct object_id *tree_oid = default_attr_source(); |
| 1331 | |
| 1332 | collect_some_attrs(istate, tree_oid, path, check); |
| 1333 | |
| 1334 | for (i = 0; i < check->nr; i++) { |
| 1335 | unsigned int n = check->items[i].attr->attr_nr; |
| 1336 | const char *value = check->all_attrs[n].value; |
| 1337 | if (value == ATTR__UNKNOWN) |
| 1338 | value = compute_builtin_attr(istate, path, check->all_attrs[n].attr); |
| 1339 | check->items[i].value = value; |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | void git_all_attrs(struct index_state *istate, |
| 1344 | const char *path, struct attr_check *check) |
| 1345 | { |
| 1346 | int i; |
| 1347 | const struct object_id *tree_oid = default_attr_source(); |
| 1348 | |
| 1349 | attr_check_reset(check); |
| 1350 | collect_some_attrs(istate, tree_oid, path, check); |
| 1351 | |
| 1352 | for (i = 0; i < check->all_attrs_nr; i++) { |
| 1353 | const char *name = check->all_attrs[i].attr->name; |
| 1354 | const char *value = check->all_attrs[i].value; |
| 1355 | struct attr_check_item *item; |
| 1356 | if (value == ATTR__UNSET || value == ATTR__UNKNOWN) |
| 1357 | continue; |
| 1358 | item = attr_check_append(check, git_attr(name)); |
| 1359 | item->value = value; |
| 1360 | } |
| 1361 | } |
| 1362 | |
| 1363 | void attr_start(void) |
| 1364 | { |
| 1365 | pthread_mutex_init(&g_attr_hashmap.mutex, NULL); |
| 1366 | pthread_mutex_init(&check_vector.mutex, NULL); |
| 1367 | } |