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