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