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