c0e7893b5fa9ff0ffa1d22c6f2195334913bc3e3
   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
  10#define NO_THE_INDEX_COMPATIBILITY_MACROS
  11#include "cache.h"
  12#include "exec_cmd.h"
  13#include "attr.h"
  14#include "dir.h"
  15#include "utf8.h"
  16#include "quote.h"
  17
  18const char git_attr__true[] = "(builtin)true";
  19const char git_attr__false[] = "\0(builtin)false";
  20static const char git_attr__unknown[] = "(builtin)unknown";
  21#define ATTR__TRUE git_attr__true
  22#define ATTR__FALSE git_attr__false
  23#define ATTR__UNSET NULL
  24#define ATTR__UNKNOWN git_attr__unknown
  25
  26/* This is a randomly chosen prime. */
  27#define HASHSIZE 257
  28
  29#ifndef DEBUG_ATTR
  30#define DEBUG_ATTR 0
  31#endif
  32
  33/*
  34 * NEEDSWORK: the global dictionary of the interned attributes
  35 * must stay a singleton even after we become thread-ready.
  36 * Access to these must be surrounded with mutex when it happens.
  37 */
  38struct git_attr {
  39        struct git_attr *next;
  40        unsigned h;
  41        int attr_nr;
  42        int maybe_macro;
  43        int maybe_real;
  44        char name[FLEX_ARRAY];
  45};
  46static int attr_nr;
  47static struct git_attr *(git_attr_hash[HASHSIZE]);
  48
  49/*
  50 * NEEDSWORK: maybe-real, maybe-macro are not property of
  51 * an attribute, as it depends on what .gitattributes are
  52 * read.  Once we introduce per git_attr_check attr_stack
  53 * and check_all_attr, the optimization based on them will
  54 * become unnecessary and can go away.  So is this variable.
  55 */
  56static int cannot_trust_maybe_real;
  57
  58/* NEEDSWORK: This will become per git_attr_check */
  59static struct attr_check_item *check_all_attr;
  60
  61const char *git_attr_name(const struct git_attr *attr)
  62{
  63        return attr->name;
  64}
  65
  66static unsigned hash_name(const char *name, int namelen)
  67{
  68        unsigned val = 0, c;
  69
  70        while (namelen--) {
  71                c = *name++;
  72                val = ((val << 7) | (val >> 22)) ^ c;
  73        }
  74        return val;
  75}
  76
  77static int invalid_attr_name(const char *name, int namelen)
  78{
  79        /*
  80         * Attribute name cannot begin with '-' and must consist of
  81         * characters from [-A-Za-z0-9_.].
  82         */
  83        if (namelen <= 0 || *name == '-')
  84                return -1;
  85        while (namelen--) {
  86                char ch = *name++;
  87                if (! (ch == '-' || ch == '.' || ch == '_' ||
  88                       ('0' <= ch && ch <= '9') ||
  89                       ('a' <= ch && ch <= 'z') ||
  90                       ('A' <= ch && ch <= 'Z')) )
  91                        return -1;
  92        }
  93        return 0;
  94}
  95
  96static struct git_attr *git_attr_internal(const char *name, int len)
  97{
  98        unsigned hval = hash_name(name, len);
  99        unsigned pos = hval % HASHSIZE;
 100        struct git_attr *a;
 101
 102        for (a = git_attr_hash[pos]; a; a = a->next) {
 103                if (a->h == hval &&
 104                    !memcmp(a->name, name, len) && !a->name[len])
 105                        return a;
 106        }
 107
 108        if (invalid_attr_name(name, len))
 109                return NULL;
 110
 111        FLEX_ALLOC_MEM(a, name, name, len);
 112        a->h = hval;
 113        a->next = git_attr_hash[pos];
 114        a->attr_nr = attr_nr++;
 115        a->maybe_macro = 0;
 116        a->maybe_real = 0;
 117        git_attr_hash[pos] = a;
 118
 119        /*
 120         * NEEDSWORK: per git_attr_check check_all_attr
 121         * will be initialized a lot more lazily, not
 122         * like this, and not here.
 123         */
 124        REALLOC_ARRAY(check_all_attr, attr_nr);
 125        check_all_attr[a->attr_nr].attr = a;
 126        check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
 127        return a;
 128}
 129
 130struct git_attr *git_attr(const char *name)
 131{
 132        return git_attr_internal(name, strlen(name));
 133}
 134
 135/* What does a matched pattern decide? */
 136struct attr_state {
 137        struct git_attr *attr;
 138        const char *setto;
 139};
 140
 141struct pattern {
 142        const char *pattern;
 143        int patternlen;
 144        int nowildcardlen;
 145        unsigned flags;         /* EXC_FLAG_* */
 146};
 147
 148/*
 149 * One rule, as from a .gitattributes file.
 150 *
 151 * If is_macro is true, then u.attr is a pointer to the git_attr being
 152 * defined.
 153 *
 154 * If is_macro is false, then u.pat is the filename pattern to which the
 155 * rule applies.
 156 *
 157 * In either case, num_attr is the number of attributes affected by
 158 * this rule, and state is an array listing them.  The attributes are
 159 * listed as they appear in the file (macros unexpanded).
 160 */
 161struct match_attr {
 162        union {
 163                struct pattern pat;
 164                struct git_attr *attr;
 165        } u;
 166        char is_macro;
 167        unsigned num_attr;
 168        struct attr_state state[FLEX_ARRAY];
 169};
 170
 171static const char blank[] = " \t\r\n";
 172
 173/*
 174 * Parse a whitespace-delimited attribute state (i.e., "attr",
 175 * "-attr", "!attr", or "attr=value") from the string starting at src.
 176 * If e is not NULL, write the results to *e.  Return a pointer to the
 177 * remainder of the string (with leading whitespace removed), or NULL
 178 * if there was an error.
 179 */
 180static const char *parse_attr(const char *src, int lineno, const char *cp,
 181                              struct attr_state *e)
 182{
 183        const char *ep, *equals;
 184        int len;
 185
 186        ep = cp + strcspn(cp, blank);
 187        equals = strchr(cp, '=');
 188        if (equals && ep < equals)
 189                equals = NULL;
 190        if (equals)
 191                len = equals - cp;
 192        else
 193                len = ep - cp;
 194        if (!e) {
 195                if (*cp == '-' || *cp == '!') {
 196                        cp++;
 197                        len--;
 198                }
 199                if (invalid_attr_name(cp, len)) {
 200                        fprintf(stderr,
 201                                "%.*s is not a valid attribute name: %s:%d\n",
 202                                len, cp, src, lineno);
 203                        return NULL;
 204                }
 205        } else {
 206                /*
 207                 * As this function is always called twice, once with
 208                 * e == NULL in the first pass and then e != NULL in
 209                 * the second pass, no need for invalid_attr_name()
 210                 * check here.
 211                 */
 212                if (*cp == '-' || *cp == '!') {
 213                        e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
 214                        cp++;
 215                        len--;
 216                }
 217                else if (!equals)
 218                        e->setto = ATTR__TRUE;
 219                else {
 220                        e->setto = xmemdupz(equals + 1, ep - equals - 1);
 221                }
 222                e->attr = git_attr_internal(cp, len);
 223        }
 224        return ep + strspn(ep, blank);
 225}
 226
 227static struct match_attr *parse_attr_line(const char *line, const char *src,
 228                                          int lineno, int macro_ok)
 229{
 230        int namelen;
 231        int num_attr, i;
 232        const char *cp, *name, *states;
 233        struct match_attr *res = NULL;
 234        int is_macro;
 235        struct strbuf pattern = STRBUF_INIT;
 236
 237        cp = line + strspn(line, blank);
 238        if (!*cp || *cp == '#')
 239                return NULL;
 240        name = cp;
 241
 242        if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) {
 243                name = pattern.buf;
 244                namelen = pattern.len;
 245        } else {
 246                namelen = strcspn(name, blank);
 247                states = name + namelen;
 248        }
 249
 250        if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
 251            starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
 252                if (!macro_ok) {
 253                        fprintf(stderr, "%s not allowed: %s:%d\n",
 254                                name, src, lineno);
 255                        goto fail_return;
 256                }
 257                is_macro = 1;
 258                name += strlen(ATTRIBUTE_MACRO_PREFIX);
 259                name += strspn(name, blank);
 260                namelen = strcspn(name, blank);
 261                if (invalid_attr_name(name, namelen)) {
 262                        fprintf(stderr,
 263                                "%.*s is not a valid attribute name: %s:%d\n",
 264                                namelen, name, src, lineno);
 265                        goto fail_return;
 266                }
 267        }
 268        else
 269                is_macro = 0;
 270
 271        states += strspn(states, blank);
 272
 273        /* First pass to count the attr_states */
 274        for (cp = states, num_attr = 0; *cp; num_attr++) {
 275                cp = parse_attr(src, lineno, cp, NULL);
 276                if (!cp)
 277                        goto fail_return;
 278        }
 279
 280        res = xcalloc(1,
 281                      sizeof(*res) +
 282                      sizeof(struct attr_state) * num_attr +
 283                      (is_macro ? 0 : namelen + 1));
 284        if (is_macro) {
 285                res->u.attr = git_attr_internal(name, namelen);
 286                res->u.attr->maybe_macro = 1;
 287        } else {
 288                char *p = (char *)&(res->state[num_attr]);
 289                memcpy(p, name, namelen);
 290                res->u.pat.pattern = p;
 291                parse_exclude_pattern(&res->u.pat.pattern,
 292                                      &res->u.pat.patternlen,
 293                                      &res->u.pat.flags,
 294                                      &res->u.pat.nowildcardlen);
 295                if (res->u.pat.flags & EXC_FLAG_NEGATIVE) {
 296                        warning(_("Negative patterns are ignored in git attributes\n"
 297                                  "Use '\\!' for literal leading exclamation."));
 298                        goto fail_return;
 299                }
 300        }
 301        res->is_macro = is_macro;
 302        res->num_attr = num_attr;
 303
 304        /* Second pass to fill the attr_states */
 305        for (cp = states, i = 0; *cp; i++) {
 306                cp = parse_attr(src, lineno, cp, &(res->state[i]));
 307                if (!is_macro)
 308                        res->state[i].attr->maybe_real = 1;
 309                if (res->state[i].attr->maybe_macro)
 310                        cannot_trust_maybe_real = 1;
 311        }
 312
 313        strbuf_release(&pattern);
 314        return res;
 315
 316fail_return:
 317        strbuf_release(&pattern);
 318        free(res);
 319        return NULL;
 320}
 321
 322/*
 323 * Like info/exclude and .gitignore, the attribute information can
 324 * come from many places.
 325 *
 326 * (1) .gitattribute file of the same directory;
 327 * (2) .gitattribute file of the parent directory if (1) does not have
 328 *      any match; this goes recursively upwards, just like .gitignore.
 329 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
 330 *
 331 * In the same file, later entries override the earlier match, so in the
 332 * global list, we would have entries from info/attributes the earliest
 333 * (reading the file from top to bottom), .gitattribute of the root
 334 * directory (again, reading the file from top to bottom) down to the
 335 * current directory, and then scan the list backwards to find the first match.
 336 * This is exactly the same as what is_excluded() does in dir.c to deal with
 337 * .gitignore file and info/excludes file as a fallback.
 338 */
 339
 340/* NEEDSWORK: This will become per git_attr_check */
 341static struct attr_stack {
 342        struct attr_stack *prev;
 343        char *origin;
 344        size_t originlen;
 345        unsigned num_matches;
 346        unsigned alloc;
 347        struct match_attr **attrs;
 348} *attr_stack;
 349
 350static void free_attr_elem(struct attr_stack *e)
 351{
 352        int i;
 353        free(e->origin);
 354        for (i = 0; i < e->num_matches; i++) {
 355                struct match_attr *a = e->attrs[i];
 356                int j;
 357                for (j = 0; j < a->num_attr; j++) {
 358                        const char *setto = a->state[j].setto;
 359                        if (setto == ATTR__TRUE ||
 360                            setto == ATTR__FALSE ||
 361                            setto == ATTR__UNSET ||
 362                            setto == ATTR__UNKNOWN)
 363                                ;
 364                        else
 365                                free((char *) setto);
 366                }
 367                free(a);
 368        }
 369        free(e->attrs);
 370        free(e);
 371}
 372
 373struct attr_check *attr_check_alloc(void)
 374{
 375        return xcalloc(1, sizeof(struct attr_check));
 376}
 377
 378struct attr_check *attr_check_initl(const char *one, ...)
 379{
 380        struct attr_check *check;
 381        int cnt;
 382        va_list params;
 383        const char *param;
 384
 385        va_start(params, one);
 386        for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++)
 387                ;
 388        va_end(params);
 389
 390        check = attr_check_alloc();
 391        check->nr = cnt;
 392        check->alloc = cnt;
 393        check->items = xcalloc(cnt, sizeof(struct attr_check_item));
 394
 395        check->items[0].attr = git_attr(one);
 396        va_start(params, one);
 397        for (cnt = 1; cnt < check->nr; cnt++) {
 398                const struct git_attr *attr;
 399                param = va_arg(params, const char *);
 400                if (!param)
 401                        die("BUG: counted %d != ended at %d",
 402                            check->nr, cnt);
 403                attr = git_attr(param);
 404                if (!attr)
 405                        die("BUG: %s: not a valid attribute name", param);
 406                check->items[cnt].attr = attr;
 407        }
 408        va_end(params);
 409        return check;
 410}
 411
 412struct attr_check_item *attr_check_append(struct attr_check *check,
 413                                          const struct git_attr *attr)
 414{
 415        struct attr_check_item *item;
 416
 417        ALLOC_GROW(check->items, check->nr + 1, check->alloc);
 418        item = &check->items[check->nr++];
 419        item->attr = attr;
 420        return item;
 421}
 422
 423void attr_check_reset(struct attr_check *check)
 424{
 425        check->nr = 0;
 426}
 427
 428void attr_check_clear(struct attr_check *check)
 429{
 430        free(check->items);
 431        check->items = NULL;
 432        check->alloc = 0;
 433        check->nr = 0;
 434}
 435
 436void attr_check_free(struct attr_check *check)
 437{
 438        attr_check_clear(check);
 439        free(check);
 440}
 441
 442static const char *builtin_attr[] = {
 443        "[attr]binary -diff -merge -text",
 444        NULL,
 445};
 446
 447static void handle_attr_line(struct attr_stack *res,
 448                             const char *line,
 449                             const char *src,
 450                             int lineno,
 451                             int macro_ok)
 452{
 453        struct match_attr *a;
 454
 455        a = parse_attr_line(line, src, lineno, macro_ok);
 456        if (!a)
 457                return;
 458        ALLOC_GROW(res->attrs, res->num_matches + 1, res->alloc);
 459        res->attrs[res->num_matches++] = a;
 460}
 461
 462static struct attr_stack *read_attr_from_array(const char **list)
 463{
 464        struct attr_stack *res;
 465        const char *line;
 466        int lineno = 0;
 467
 468        res = xcalloc(1, sizeof(*res));
 469        while ((line = *(list++)) != NULL)
 470                handle_attr_line(res, line, "[builtin]", ++lineno, 1);
 471        return res;
 472}
 473
 474/*
 475 * NEEDSWORK: these two are tricky.  The callers assume there is a
 476 * single, system-wide global state "where we read attributes from?"
 477 * and when the state is flipped by calling git_attr_set_direction(),
 478 * attr_stack is discarded so that subsequent attr_check will lazily
 479 * read from the right place.  And they do not know or care who called
 480 * by them uses the attribute subsystem, hence have no knowledge of
 481 * existing git_attr_check instances or future ones that will be
 482 * created).
 483 *
 484 * Probably we need a thread_local that holds these two variables,
 485 * and a list of git_attr_check instances (which need to be maintained
 486 * by hooking into git_attr_check_alloc(), git_attr_check_initl(), and
 487 * git_attr_check_clear().  Then git_attr_set_direction() updates the
 488 * fields in that thread_local for these two variables, iterate over
 489 * all the active git_attr_check instances and discard the attr_stack
 490 * they hold.  Yuck, but it sounds doable.
 491 */
 492static enum git_attr_direction direction;
 493static struct index_state *use_index;
 494
 495static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
 496{
 497        FILE *fp = fopen(path, "r");
 498        struct attr_stack *res;
 499        char buf[2048];
 500        int lineno = 0;
 501
 502        if (!fp) {
 503                if (errno != ENOENT && errno != ENOTDIR)
 504                        warn_on_inaccessible(path);
 505                return NULL;
 506        }
 507        res = xcalloc(1, sizeof(*res));
 508        while (fgets(buf, sizeof(buf), fp)) {
 509                char *bufp = buf;
 510                if (!lineno)
 511                        skip_utf8_bom(&bufp, strlen(bufp));
 512                handle_attr_line(res, bufp, path, ++lineno, macro_ok);
 513        }
 514        fclose(fp);
 515        return res;
 516}
 517
 518static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
 519{
 520        struct attr_stack *res;
 521        char *buf, *sp;
 522        int lineno = 0;
 523
 524        buf = read_blob_data_from_index(use_index ? use_index : &the_index, path, NULL);
 525        if (!buf)
 526                return NULL;
 527
 528        res = xcalloc(1, sizeof(*res));
 529        for (sp = buf; *sp; ) {
 530                char *ep;
 531                int more;
 532
 533                ep = strchrnul(sp, '\n');
 534                more = (*ep == '\n');
 535                *ep = '\0';
 536                handle_attr_line(res, sp, path, ++lineno, macro_ok);
 537                sp = ep + more;
 538        }
 539        free(buf);
 540        return res;
 541}
 542
 543static struct attr_stack *read_attr(const char *path, int macro_ok)
 544{
 545        struct attr_stack *res;
 546
 547        if (direction == GIT_ATTR_CHECKOUT) {
 548                res = read_attr_from_index(path, macro_ok);
 549                if (!res)
 550                        res = read_attr_from_file(path, macro_ok);
 551        }
 552        else if (direction == GIT_ATTR_CHECKIN) {
 553                res = read_attr_from_file(path, macro_ok);
 554                if (!res)
 555                        /*
 556                         * There is no checked out .gitattributes file there, but
 557                         * we might have it in the index.  We allow operation in a
 558                         * sparsely checked out work tree, so read from it.
 559                         */
 560                        res = read_attr_from_index(path, macro_ok);
 561        }
 562        else
 563                res = read_attr_from_index(path, macro_ok);
 564        if (!res)
 565                res = xcalloc(1, sizeof(*res));
 566        return res;
 567}
 568
 569#if DEBUG_ATTR
 570static void debug_info(const char *what, struct attr_stack *elem)
 571{
 572        fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
 573}
 574static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
 575{
 576        const char *value = v;
 577
 578        if (ATTR_TRUE(value))
 579                value = "set";
 580        else if (ATTR_FALSE(value))
 581                value = "unset";
 582        else if (ATTR_UNSET(value))
 583                value = "unspecified";
 584
 585        fprintf(stderr, "%s: %s => %s (%s)\n",
 586                what, attr->name, (char *) value, match);
 587}
 588#define debug_push(a) debug_info("push", (a))
 589#define debug_pop(a) debug_info("pop", (a))
 590#else
 591#define debug_push(a) do { ; } while (0)
 592#define debug_pop(a) do { ; } while (0)
 593#define debug_set(a,b,c,d) do { ; } while (0)
 594#endif /* DEBUG_ATTR */
 595
 596static void drop_attr_stack(void)
 597{
 598        while (attr_stack) {
 599                struct attr_stack *elem = attr_stack;
 600                attr_stack = elem->prev;
 601                free_attr_elem(elem);
 602        }
 603}
 604
 605static const char *git_etc_gitattributes(void)
 606{
 607        static const char *system_wide;
 608        if (!system_wide)
 609                system_wide = system_path(ETC_GITATTRIBUTES);
 610        return system_wide;
 611}
 612
 613static int git_attr_system(void)
 614{
 615        return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
 616}
 617
 618static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
 619
 620static void push_stack(struct attr_stack **attr_stack_p,
 621                       struct attr_stack *elem, char *origin, size_t originlen)
 622{
 623        if (elem) {
 624                elem->origin = origin;
 625                if (origin)
 626                        elem->originlen = originlen;
 627                elem->prev = *attr_stack_p;
 628                *attr_stack_p = elem;
 629        }
 630}
 631
 632static void bootstrap_attr_stack(void)
 633{
 634        struct attr_stack *elem;
 635
 636        if (attr_stack)
 637                return;
 638
 639        push_stack(&attr_stack, read_attr_from_array(builtin_attr), NULL, 0);
 640
 641        if (git_attr_system())
 642                push_stack(&attr_stack,
 643                           read_attr_from_file(git_etc_gitattributes(), 1),
 644                           NULL, 0);
 645
 646        if (!git_attributes_file)
 647                git_attributes_file = xdg_config_home("attributes");
 648        if (git_attributes_file)
 649                push_stack(&attr_stack,
 650                           read_attr_from_file(git_attributes_file, 1),
 651                           NULL, 0);
 652
 653        if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
 654                elem = read_attr(GITATTRIBUTES_FILE, 1);
 655                push_stack(&attr_stack, elem, xstrdup(""), 0);
 656                debug_push(elem);
 657        }
 658
 659        if (startup_info->have_repository)
 660                elem = read_attr_from_file(git_path_info_attributes(), 1);
 661        else
 662                elem = NULL;
 663
 664        if (!elem)
 665                elem = xcalloc(1, sizeof(*elem));
 666        push_stack(&attr_stack, elem, NULL, 0);
 667}
 668
 669static void prepare_attr_stack(const char *path, int dirlen)
 670{
 671        struct attr_stack *elem, *info;
 672        const char *cp;
 673
 674        /*
 675         * At the bottom of the attribute stack is the built-in
 676         * set of attribute definitions, followed by the contents
 677         * of $(prefix)/etc/gitattributes and a file specified by
 678         * core.attributesfile.  Then, contents from
 679         * .gitattribute files from directories closer to the
 680         * root to the ones in deeper directories are pushed
 681         * to the stack.  Finally, at the very top of the stack
 682         * we always keep the contents of $GIT_DIR/info/attributes.
 683         *
 684         * When checking, we use entries from near the top of the
 685         * stack, preferring $GIT_DIR/info/attributes, then
 686         * .gitattributes in deeper directories to shallower ones,
 687         * and finally use the built-in set as the default.
 688         */
 689        bootstrap_attr_stack();
 690
 691        /*
 692         * Pop the "info" one that is always at the top of the stack.
 693         */
 694        info = attr_stack;
 695        attr_stack = info->prev;
 696
 697        /*
 698         * Pop the ones from directories that are not the prefix of
 699         * the path we are checking. Break out of the loop when we see
 700         * the root one (whose origin is an empty string "") or the builtin
 701         * one (whose origin is NULL) without popping it.
 702         */
 703        while (attr_stack->origin) {
 704                int namelen = strlen(attr_stack->origin);
 705
 706                elem = attr_stack;
 707                if (namelen <= dirlen &&
 708                    !strncmp(elem->origin, path, namelen) &&
 709                    (!namelen || path[namelen] == '/'))
 710                        break;
 711
 712                debug_pop(elem);
 713                attr_stack = elem->prev;
 714                free_attr_elem(elem);
 715        }
 716
 717        /*
 718         * Read from parent directories and push them down
 719         */
 720        if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
 721                /*
 722                 * bootstrap_attr_stack() should have added, and the
 723                 * above loop should have stopped before popping, the
 724                 * root element whose attr_stack->origin is set to an
 725                 * empty string.
 726                 */
 727                struct strbuf pathbuf = STRBUF_INIT;
 728
 729                assert(attr_stack->origin);
 730                while (1) {
 731                        size_t len = strlen(attr_stack->origin);
 732                        char *origin;
 733
 734                        if (dirlen <= len)
 735                                break;
 736                        cp = memchr(path + len + 1, '/', dirlen - len - 1);
 737                        if (!cp)
 738                                cp = path + dirlen;
 739                        strbuf_addf(&pathbuf,
 740                                    "%.*s/%s", (int)(cp - path), path,
 741                                    GITATTRIBUTES_FILE);
 742                        elem = read_attr(pathbuf.buf, 0);
 743                        strbuf_setlen(&pathbuf, cp - path);
 744                        origin = strbuf_detach(&pathbuf, &len);
 745                        push_stack(&attr_stack, elem, origin, len);
 746                        debug_push(elem);
 747                }
 748
 749                strbuf_release(&pathbuf);
 750        }
 751
 752        /*
 753         * Finally push the "info" one at the top of the stack.
 754         */
 755        push_stack(&attr_stack, info, NULL, 0);
 756}
 757
 758static int path_matches(const char *pathname, int pathlen,
 759                        int basename_offset,
 760                        const struct pattern *pat,
 761                        const char *base, int baselen)
 762{
 763        const char *pattern = pat->pattern;
 764        int prefix = pat->nowildcardlen;
 765        int isdir = (pathlen && pathname[pathlen - 1] == '/');
 766
 767        if ((pat->flags & EXC_FLAG_MUSTBEDIR) && !isdir)
 768                return 0;
 769
 770        if (pat->flags & EXC_FLAG_NODIR) {
 771                return match_basename(pathname + basename_offset,
 772                                      pathlen - basename_offset - isdir,
 773                                      pattern, prefix,
 774                                      pat->patternlen, pat->flags);
 775        }
 776        return match_pathname(pathname, pathlen - isdir,
 777                              base, baselen,
 778                              pattern, prefix, pat->patternlen, pat->flags);
 779}
 780
 781static int macroexpand_one(int attr_nr, int rem);
 782
 783static int fill_one(const char *what, struct match_attr *a, int rem)
 784{
 785        struct attr_check_item *check = check_all_attr;
 786        int i;
 787
 788        for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
 789                struct git_attr *attr = a->state[i].attr;
 790                const char **n = &(check[attr->attr_nr].value);
 791                const char *v = a->state[i].setto;
 792
 793                if (*n == ATTR__UNKNOWN) {
 794                        debug_set(what,
 795                                  a->is_macro ? a->u.attr->name : a->u.pat.pattern,
 796                                  attr, v);
 797                        *n = v;
 798                        rem--;
 799                        rem = macroexpand_one(attr->attr_nr, rem);
 800                }
 801        }
 802        return rem;
 803}
 804
 805static int fill(const char *path, int pathlen, int basename_offset,
 806                struct attr_stack *stk, int rem)
 807{
 808        int i;
 809        const char *base = stk->origin ? stk->origin : "";
 810
 811        for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
 812                struct match_attr *a = stk->attrs[i];
 813                if (a->is_macro)
 814                        continue;
 815                if (path_matches(path, pathlen, basename_offset,
 816                                 &a->u.pat, base, stk->originlen))
 817                        rem = fill_one("fill", a, rem);
 818        }
 819        return rem;
 820}
 821
 822static int macroexpand_one(int nr, int rem)
 823{
 824        struct attr_stack *stk;
 825        int i;
 826
 827        if (check_all_attr[nr].value != ATTR__TRUE ||
 828            !check_all_attr[nr].attr->maybe_macro)
 829                return rem;
 830
 831        for (stk = attr_stack; stk; stk = stk->prev) {
 832                for (i = stk->num_matches - 1; 0 <= i; i--) {
 833                        struct match_attr *ma = stk->attrs[i];
 834                        if (!ma->is_macro)
 835                                continue;
 836                        if (ma->u.attr->attr_nr == nr)
 837                                return fill_one("expand", ma, rem);
 838                }
 839        }
 840
 841        return rem;
 842}
 843
 844/*
 845 * Collect attributes for path into the array pointed to by
 846 * check_all_attr. If num is non-zero, only attributes in check[] are
 847 * collected. Otherwise all attributes are collected.
 848 */
 849static void collect_some_attrs(const char *path, int num,
 850                               struct attr_check_item *check)
 851
 852{
 853        struct attr_stack *stk;
 854        int i, pathlen, rem, dirlen;
 855        const char *cp, *last_slash = NULL;
 856        int basename_offset;
 857
 858        for (cp = path; *cp; cp++) {
 859                if (*cp == '/' && cp[1])
 860                        last_slash = cp;
 861        }
 862        pathlen = cp - path;
 863        if (last_slash) {
 864                basename_offset = last_slash + 1 - path;
 865                dirlen = last_slash - path;
 866        } else {
 867                basename_offset = 0;
 868                dirlen = 0;
 869        }
 870
 871        prepare_attr_stack(path, dirlen);
 872        for (i = 0; i < attr_nr; i++)
 873                check_all_attr[i].value = ATTR__UNKNOWN;
 874        if (num && !cannot_trust_maybe_real) {
 875                rem = 0;
 876                for (i = 0; i < num; i++) {
 877                        if (!check[i].attr->maybe_real) {
 878                                struct attr_check_item *c;
 879                                c = check_all_attr + check[i].attr->attr_nr;
 880                                c->value = ATTR__UNSET;
 881                                rem++;
 882                        }
 883                }
 884                if (rem == num)
 885                        return;
 886        }
 887
 888        rem = attr_nr;
 889        for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
 890                rem = fill(path, pathlen, basename_offset, stk, rem);
 891}
 892
 893static int git_check_attrs(const char *path, int num,
 894                           struct attr_check_item *check)
 895{
 896        int i;
 897
 898        collect_some_attrs(path, num, check);
 899
 900        for (i = 0; i < num; i++) {
 901                const char *value = check_all_attr[check[i].attr->attr_nr].value;
 902                if (value == ATTR__UNKNOWN)
 903                        value = ATTR__UNSET;
 904                check[i].value = value;
 905        }
 906
 907        return 0;
 908}
 909
 910void git_all_attrs(const char *path, struct attr_check *check)
 911{
 912        int i;
 913
 914        attr_check_reset(check);
 915        collect_some_attrs(path, check->nr, check->items);
 916
 917        for (i = 0; i < attr_nr; i++) {
 918                const char *name = check_all_attr[i].attr->name;
 919                const char *value = check_all_attr[i].value;
 920                struct attr_check_item *item;
 921                if (value == ATTR__UNSET || value == ATTR__UNKNOWN)
 922                        continue;
 923                item = attr_check_append(check, git_attr(name));
 924                item->value = value;
 925        }
 926}
 927
 928int git_check_attr(const char *path, struct attr_check *check)
 929{
 930        return git_check_attrs(path, check->nr, check->items);
 931}
 932
 933void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
 934{
 935        enum git_attr_direction old = direction;
 936
 937        if (is_bare_repository() && new != GIT_ATTR_INDEX)
 938                die("BUG: non-INDEX attr direction in a bare repo");
 939
 940        direction = new;
 941        if (new != old)
 942                drop_attr_stack();
 943        use_index = istate;
 944}