attr.c 17.2 KB
Newer Older
1
#define NO_THE_INDEX_COMPATIBILITY_MACROS
2
#include "cache.h"
3
#include "exec_cmd.h"
4 5
#include "attr.h"

6 7 8 9 10 11 12
const char git_attr__true[] = "(builtin)true";
const char git_attr__false[] = "\0(builtin)false";
static const char git_attr__unknown[] = "(builtin)unknown";
#define ATTR__TRUE git_attr__true
#define ATTR__FALSE git_attr__false
#define ATTR__UNSET NULL
#define ATTR__UNKNOWN git_attr__unknown
13

14 15
static const char *attributes_file;

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * The basic design decision here is that we are not going to have
 * insanely large number of attributes.
 *
 * This is a randomly chosen prime.
 */
#define HASHSIZE 257

#ifndef DEBUG_ATTR
#define DEBUG_ATTR 0
#endif

struct git_attr {
	struct git_attr *next;
	unsigned h;
J
Junio C Hamano 已提交
31
	int attr_nr;
32 33
	char name[FLEX_ARRAY];
};
J
Junio C Hamano 已提交
34
static int attr_nr;
35

J
Junio C Hamano 已提交
36
static struct git_attr_check *check_all_attr;
37 38
static struct git_attr *(git_attr_hash[HASHSIZE]);

39 40 41 42 43
char *git_attr_name(struct git_attr *attr)
{
	return attr->name;
}

44 45
static unsigned hash_name(const char *name, int namelen)
{
46
	unsigned val = 0, c;
47 48 49 50 51 52 53 54

	while (namelen--) {
		c = *name++;
		val = ((val << 7) | (val >> 22)) ^ c;
	}
	return val;
}

55 56 57
static int invalid_attr_name(const char *name, int namelen)
{
	/*
58 59
	 * Attribute name cannot begin with '-' and must consist of
	 * characters from [-A-Za-z0-9_.].
60
	 */
61
	if (namelen <= 0 || *name == '-')
62 63 64 65 66 67 68 69 70 71 72 73
		return -1;
	while (namelen--) {
		char ch = *name++;
		if (! (ch == '-' || ch == '.' || ch == '_' ||
		       ('0' <= ch && ch <= '9') ||
		       ('a' <= ch && ch <= 'z') ||
		       ('A' <= ch && ch <= 'Z')) )
			return -1;
	}
	return 0;
}

74
static struct git_attr *git_attr_internal(const char *name, int len)
75 76 77 78 79 80 81 82 83 84 85
{
	unsigned hval = hash_name(name, len);
	unsigned pos = hval % HASHSIZE;
	struct git_attr *a;

	for (a = git_attr_hash[pos]; a; a = a->next) {
		if (a->h == hval &&
		    !memcmp(a->name, name, len) && !a->name[len])
			return a;
	}

86 87 88
	if (invalid_attr_name(name, len))
		return NULL;

89 90 91 92 93
	a = xmalloc(sizeof(*a) + len + 1);
	memcpy(a->name, name, len);
	a->name[len] = 0;
	a->h = hval;
	a->next = git_attr_hash[pos];
J
Junio C Hamano 已提交
94
	a->attr_nr = attr_nr++;
95
	git_attr_hash[pos] = a;
J
Junio C Hamano 已提交
96 97 98 99

	check_all_attr = xrealloc(check_all_attr,
				  sizeof(*check_all_attr) * attr_nr);
	check_all_attr[a->attr_nr].attr = a;
100
	check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
101 102 103
	return a;
}

104 105 106 107 108
struct git_attr *git_attr(const char *name)
{
	return git_attr_internal(name, strlen(name));
}

109 110 111 112 113 114
/*
 * .gitattributes file is one line per record, each of which is
 *
 * (1) glob pattern.
 * (2) whitespace
 * (3) whitespace separated list of attribute names, each of which
115 116
 *     could be prefixed with '-' to mean "set to false", '!' to mean
 *     "unset".
117 118
 */

119
/* What does a matched pattern decide? */
120 121
struct attr_state {
	struct git_attr *attr;
122
	const char *setto;
123 124 125
};

struct match_attr {
J
Junio C Hamano 已提交
126 127 128 129 130
	union {
		char *pattern;
		struct git_attr *attr;
	} u;
	char is_macro;
131 132 133 134 135 136
	unsigned num_attr;
	struct attr_state state[FLEX_ARRAY];
};

static const char blank[] = " \t\r\n";

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
static const char *parse_attr(const char *src, int lineno, const char *cp,
			      int *num_attr, struct match_attr *res)
{
	const char *ep, *equals;
	int len;

	ep = cp + strcspn(cp, blank);
	equals = strchr(cp, '=');
	if (equals && ep < equals)
		equals = NULL;
	if (equals)
		len = equals - cp;
	else
		len = ep - cp;
	if (!res) {
		if (*cp == '-' || *cp == '!') {
			cp++;
			len--;
		}
		if (invalid_attr_name(cp, len)) {
			fprintf(stderr,
				"%.*s is not a valid attribute name: %s:%d\n",
				len, cp, src, lineno);
			return NULL;
		}
	} else {
		struct attr_state *e;

		e = &(res->state[*num_attr]);
		if (*cp == '-' || *cp == '!') {
			e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
			cp++;
			len--;
		}
		else if (!equals)
			e->setto = ATTR__TRUE;
		else {
P
Pierre Habouzit 已提交
174
			e->setto = xmemdupz(equals + 1, ep - equals - 1);
175
		}
176
		e->attr = git_attr_internal(cp, len);
177 178 179 180 181
	}
	(*num_attr)++;
	return ep + strspn(ep, blank);
}

J
Junio C Hamano 已提交
182 183
static struct match_attr *parse_attr_line(const char *line, const char *src,
					  int lineno, int macro_ok)
184 185 186 187
{
	int namelen;
	int num_attr;
	const char *cp, *name;
188
	struct match_attr *res = NULL;
189
	int pass;
J
Junio C Hamano 已提交
190
	int is_macro;
191 192 193 194 195 196

	cp = line + strspn(line, blank);
	if (!*cp || *cp == '#')
		return NULL;
	name = cp;
	namelen = strcspn(name, blank);
J
Junio C Hamano 已提交
197 198 199 200 201 202 203 204 205 206 207
	if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
	    !prefixcmp(name, ATTRIBUTE_MACRO_PREFIX)) {
		if (!macro_ok) {
			fprintf(stderr, "%s not allowed: %s:%d\n",
				name, src, lineno);
			return NULL;
		}
		is_macro = 1;
		name += strlen(ATTRIBUTE_MACRO_PREFIX);
		name += strspn(name, blank);
		namelen = strcspn(name, blank);
208 209 210 211 212 213
		if (invalid_attr_name(name, namelen)) {
			fprintf(stderr,
				"%.*s is not a valid attribute name: %s:%d\n",
				namelen, name, src, lineno);
			return NULL;
		}
J
Junio C Hamano 已提交
214 215 216
	}
	else
		is_macro = 0;
217 218 219 220 221 222

	for (pass = 0; pass < 2; pass++) {
		/* pass 0 counts and allocates, pass 1 fills */
		num_attr = 0;
		cp = name + namelen;
		cp = cp + strspn(cp, blank);
223
		while (*cp) {
224
			cp = parse_attr(src, lineno, cp, &num_attr, res);
225 226 227
			if (!cp)
				return NULL;
		}
228 229 230 231 232
		if (pass)
			break;
		res = xcalloc(1,
			      sizeof(*res) +
			      sizeof(struct attr_state) * num_attr +
J
Junio C Hamano 已提交
233
			      (is_macro ? 0 : namelen + 1));
234
		if (is_macro)
235
			res->u.attr = git_attr_internal(name, namelen);
J
Junio C Hamano 已提交
236
		else {
237
			res->u.pattern = (char *)&(res->state[num_attr]);
J
Junio C Hamano 已提交
238 239 240 241
			memcpy(res->u.pattern, name, namelen);
			res->u.pattern[namelen] = 0;
		}
		res->is_macro = is_macro;
242 243 244 245 246 247 248 249 250 251
		res->num_attr = num_attr;
	}
	return res;
}

/*
 * Like info/exclude and .gitignore, the attribute information can
 * come from many places.
 *
 * (1) .gitattribute file of the same directory;
252 253 254
 * (2) .gitattribute file of the parent directory if (1) does not have
 *      any match; this goes recursively upwards, just like .gitignore.
 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
255 256 257 258 259 260 261 262 263 264 265 266 267 268
 *
 * In the same file, later entries override the earlier match, so in the
 * global list, we would have entries from info/attributes the earliest
 * (reading the file from top to bottom), .gitattribute of the root
 * directory (again, reading the file from top to bottom) down to the
 * current directory, and then scan the list backwards to find the first match.
 * This is exactly the same as what excluded() does in dir.c to deal with
 * .gitignore
 */

static struct attr_stack {
	struct attr_stack *prev;
	char *origin;
	unsigned num_matches;
J
Junio C Hamano 已提交
269
	unsigned alloc;
270 271 272 273 274 275 276
	struct match_attr **attrs;
} *attr_stack;

static void free_attr_elem(struct attr_stack *e)
{
	int i;
	free(e->origin);
277 278 279 280
	for (i = 0; i < e->num_matches; i++) {
		struct match_attr *a = e->attrs[i];
		int j;
		for (j = 0; j < a->num_attr; j++) {
281
			const char *setto = a->state[j].setto;
282 283 284 285 286 287
			if (setto == ATTR__TRUE ||
			    setto == ATTR__FALSE ||
			    setto == ATTR__UNSET ||
			    setto == ATTR__UNKNOWN)
				;
			else
288
				free((char *) setto);
289 290 291
		}
		free(a);
	}
292 293 294 295
	free(e);
}

static const char *builtin_attr[] = {
296
	"[attr]binary -diff -text",
297 298 299
	NULL,
};

J
Junio C Hamano 已提交
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
static void handle_attr_line(struct attr_stack *res,
			     const char *line,
			     const char *src,
			     int lineno,
			     int macro_ok)
{
	struct match_attr *a;

	a = parse_attr_line(line, src, lineno, macro_ok);
	if (!a)
		return;
	if (res->alloc <= res->num_matches) {
		res->alloc = alloc_nr(res->num_matches);
		res->attrs = xrealloc(res->attrs,
				      sizeof(struct match_attr *) *
				      res->alloc);
	}
	res->attrs[res->num_matches++] = a;
}

320 321 322 323
static struct attr_stack *read_attr_from_array(const char **list)
{
	struct attr_stack *res;
	const char *line;
J
Junio C Hamano 已提交
324
	int lineno = 0;
325 326

	res = xcalloc(1, sizeof(*res));
J
Junio C Hamano 已提交
327 328
	while ((line = *(list++)) != NULL)
		handle_attr_line(res, line, "[builtin]", ++lineno, 1);
329 330 331
	return res;
}

332 333 334
static enum git_attr_direction direction;
static struct index_state *use_index;

J
Junio C Hamano 已提交
335
static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
336
{
J
Junio C Hamano 已提交
337
	FILE *fp = fopen(path, "r");
338 339
	struct attr_stack *res;
	char buf[2048];
J
Junio C Hamano 已提交
340
	int lineno = 0;
341 342

	if (!fp)
J
Junio C Hamano 已提交
343 344 345 346 347 348 349
		return NULL;
	res = xcalloc(1, sizeof(*res));
	while (fgets(buf, sizeof(buf), fp))
		handle_attr_line(res, buf, path, ++lineno, macro_ok);
	fclose(fp);
	return res;
}
350

351 352 353 354 355 356
static void *read_index_data(const char *path)
{
	int pos, len;
	unsigned long sz;
	enum object_type type;
	void *data;
357
	struct index_state *istate = use_index ? use_index : &the_index;
358 359

	len = strlen(path);
360
	pos = index_name_pos(istate, path, len);
361 362 363 364 365 366 367
	if (pos < 0) {
		/*
		 * We might be in the middle of a merge, in which
		 * case we would read stage #2 (ours).
		 */
		int i;
		for (i = -pos - 1;
368 369
		     (pos < 0 && i < istate->cache_nr &&
		      !strcmp(istate->cache[i]->name, path));
370
		     i++)
371
			if (ce_stage(istate->cache[i]) == 2)
372 373 374 375
				pos = i;
	}
	if (pos < 0)
		return NULL;
376
	data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
377 378 379 380 381 382 383
	if (!data || type != OBJ_BLOB) {
		free(data);
		return NULL;
	}
	return data;
}

384
static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
J
Junio C Hamano 已提交
385 386
{
	struct attr_stack *res;
387 388
	char *buf, *sp;
	int lineno = 0;
J
Junio C Hamano 已提交
389

390 391
	buf = read_index_data(path);
	if (!buf)
392
		return NULL;
393

394
	res = xcalloc(1, sizeof(*res));
395 396 397 398 399 400 401 402 403 404 405
	for (sp = buf; *sp; ) {
		char *ep;
		int more;
		for (ep = sp; *ep && *ep != '\n'; ep++)
			;
		more = (*ep == '\n');
		*ep = '\0';
		handle_attr_line(res, sp, path, ++lineno, macro_ok);
		sp = ep + more;
	}
	free(buf);
406 407 408
	return res;
}

409 410 411 412 413 414 415 416 417
static struct attr_stack *read_attr(const char *path, int macro_ok)
{
	struct attr_stack *res;

	if (direction == GIT_ATTR_CHECKOUT) {
		res = read_attr_from_index(path, macro_ok);
		if (!res)
			res = read_attr_from_file(path, macro_ok);
	}
418
	else if (direction == GIT_ATTR_CHECKIN) {
419 420 421 422 423 424 425 426 427
		res = read_attr_from_file(path, macro_ok);
		if (!res)
			/*
			 * There is no checked out .gitattributes file there, but
			 * we might have it in the index.  We allow operation in a
			 * sparsely checked out work tree, so read from it.
			 */
			res = read_attr_from_index(path, macro_ok);
	}
428 429
	else
		res = read_attr_from_index(path, macro_ok);
430 431 432 433 434
	if (!res)
		res = xcalloc(1, sizeof(*res));
	return res;
}

435 436 437 438 439
#if DEBUG_ATTR
static void debug_info(const char *what, struct attr_stack *elem)
{
	fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
}
440
static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
J
Junio C Hamano 已提交
441
{
442 443 444 445 446 447 448 449 450 451 452
	const char *value = v;

	if (ATTR_TRUE(value))
		value = "set";
	else if (ATTR_FALSE(value))
		value = "unset";
	else if (ATTR_UNSET(value))
		value = "unspecified";

	fprintf(stderr, "%s: %s => %s (%s)\n",
		what, attr->name, (char *) value, match);
J
Junio C Hamano 已提交
453
}
454 455 456 457 458
#define debug_push(a) debug_info("push", (a))
#define debug_pop(a) debug_info("pop", (a))
#else
#define debug_push(a) do { ; } while (0)
#define debug_pop(a) do { ; } while (0)
J
Junio C Hamano 已提交
459
#define debug_set(a,b,c,d) do { ; } while (0)
460 461
#endif

462 463 464 465 466 467 468 469 470
static void drop_attr_stack(void)
{
	while (attr_stack) {
		struct attr_stack *elem = attr_stack;
		attr_stack = elem->prev;
		free_attr_elem(elem);
	}
}

471
static const char *git_etc_gitattributes(void)
472 473 474 475 476 477 478
{
	static const char *system_wide;
	if (!system_wide)
		system_wide = system_path(ETC_GITATTRIBUTES);
	return system_wide;
}

479
static int git_attr_system(void)
480 481 482 483 484 485 486 487 488 489 490 491
{
	return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
}

static int git_attr_config(const char *var, const char *value, void *dummy)
{
	if (!strcmp(var, "core.attributesfile"))
		return git_config_pathname(&attributes_file, var, value);

	return 0;
}

J
Junio C Hamano 已提交
492 493 494 495 496 497 498 499 500 501
static void bootstrap_attr_stack(void)
{
	if (!attr_stack) {
		struct attr_stack *elem;

		elem = read_attr_from_array(builtin_attr);
		elem->origin = NULL;
		elem->prev = attr_stack;
		attr_stack = elem;

502 503 504 505 506 507 508 509 510 511
		if (git_attr_system()) {
			elem = read_attr_from_file(git_etc_gitattributes(), 1);
			if (elem) {
				elem->origin = NULL;
				elem->prev = attr_stack;
				attr_stack = elem;
			}
		}

		git_config(git_attr_config, NULL);
512
		if (attributes_file) {
513 514 515 516 517 518 519 520
			elem = read_attr_from_file(attributes_file, 1);
			if (elem) {
				elem->origin = NULL;
				elem->prev = attr_stack;
				attr_stack = elem;
			}
		}

521
		if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
522 523 524 525 526 527
			elem = read_attr(GITATTRIBUTES_FILE, 1);
			elem->origin = strdup("");
			elem->prev = attr_stack;
			attr_stack = elem;
			debug_push(elem);
		}
J
Junio C Hamano 已提交
528 529

		elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
J
Junio C Hamano 已提交
530 531
		if (!elem)
			elem = xcalloc(1, sizeof(*elem));
J
Junio C Hamano 已提交
532 533 534 535 536 537
		elem->origin = NULL;
		elem->prev = attr_stack;
		attr_stack = elem;
	}
}

538 539 540 541
static void prepare_attr_stack(const char *path, int dirlen)
{
	struct attr_stack *elem, *info;
	int len;
542 543 544
	struct strbuf pathbuf;

	strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
545 546 547

	/*
	 * At the bottom of the attribute stack is the built-in
548 549 550
	 * set of attribute definitions, followed by the contents
	 * of $(prefix)/etc/gitattributes and a file specified by
	 * core.attributesfile.  Then, contents from
551 552 553 554 555 556 557 558 559 560
	 * .gitattribute files from directories closer to the
	 * root to the ones in deeper directories are pushed
	 * to the stack.  Finally, at the very top of the stack
	 * we always keep the contents of $GIT_DIR/info/attributes.
	 *
	 * When checking, we use entries from near the top of the
	 * stack, preferring $GIT_DIR/info/attributes, then
	 * .gitattributes in deeper directories to shallower ones,
	 * and finally use the built-in set as the default.
	 */
J
Junio C Hamano 已提交
561 562
	if (!attr_stack)
		bootstrap_attr_stack();
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589

	/*
	 * Pop the "info" one that is always at the top of the stack.
	 */
	info = attr_stack;
	attr_stack = info->prev;

	/*
	 * Pop the ones from directories that are not the prefix of
	 * the path we are checking.
	 */
	while (attr_stack && attr_stack->origin) {
		int namelen = strlen(attr_stack->origin);

		elem = attr_stack;
		if (namelen <= dirlen &&
		    !strncmp(elem->origin, path, namelen))
			break;

		debug_pop(elem);
		attr_stack = elem->prev;
		free_attr_elem(elem);
	}

	/*
	 * Read from parent directories and push them down
	 */
590
	if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
591 592 593 594 595 596
		while (1) {
			char *cp;

			len = strlen(attr_stack->origin);
			if (dirlen <= len)
				break;
597 598 599 600
			strbuf_reset(&pathbuf);
			strbuf_add(&pathbuf, path, dirlen);
			strbuf_addch(&pathbuf, '/');
			cp = strchr(pathbuf.buf + len + 1, '/');
601
			strcpy(cp + 1, GITATTRIBUTES_FILE);
602
			elem = read_attr(pathbuf.buf, 0);
603
			*cp = '\0';
604
			elem->origin = strdup(pathbuf.buf);
605 606 607 608
			elem->prev = attr_stack;
			attr_stack = elem;
			debug_push(elem);
		}
609 610
	}

R
René Scharfe 已提交
611 612
	strbuf_release(&pathbuf);

613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	/*
	 * Finally push the "info" one at the top of the stack.
	 */
	info->prev = attr_stack;
	attr_stack = info;
}

static int path_matches(const char *pathname, int pathlen,
			const char *pattern,
			const char *base, int baselen)
{
	if (!strchr(pattern, '/')) {
		/* match basename */
		const char *basename = strrchr(pathname, '/');
		basename = basename ? basename + 1 : pathname;
		return (fnmatch(pattern, basename, 0) == 0);
	}
	/*
	 * match with FNM_PATHNAME; the pattern has base implicitly
	 * in front of it.
	 */
	if (*pattern == '/')
		pattern++;
	if (pathlen < baselen ||
637
	    (baselen && pathname[baselen] != '/') ||
638 639
	    strncmp(pathname, base, baselen))
		return 0;
640 641 642
	if (baselen != 0)
		baselen++;
	return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
643 644
}

645 646
static int macroexpand_one(int attr_nr, int rem);

647 648 649 650 651
static int fill_one(const char *what, struct match_attr *a, int rem)
{
	struct git_attr_check *check = check_all_attr;
	int i;

652
	for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
653
		struct git_attr *attr = a->state[i].attr;
654 655
		const char **n = &(check[attr->attr_nr].value);
		const char *v = a->state[i].setto;
656 657

		if (*n == ATTR__UNKNOWN) {
658 659 660
			debug_set(what,
				  a->is_macro ? a->u.attr->name : a->u.pattern,
				  attr, v);
661 662
			*n = v;
			rem--;
663
			rem = macroexpand_one(attr->attr_nr, rem);
664 665 666 667 668
		}
	}
	return rem;
}

J
Junio C Hamano 已提交
669
static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
670
{
671
	int i;
672 673 674 675
	const char *base = stk->origin ? stk->origin : "";

	for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
		struct match_attr *a = stk->attrs[i];
J
Junio C Hamano 已提交
676 677
		if (a->is_macro)
			continue;
678
		if (path_matches(path, pathlen,
679 680
				 a->u.pattern, base, strlen(base)))
			rem = fill_one("fill", a, rem);
681 682 683 684
	}
	return rem;
}

685
static int macroexpand_one(int attr_nr, int rem)
J
Junio C Hamano 已提交
686
{
687 688
	struct attr_stack *stk;
	struct match_attr *a = NULL;
689
	int i;
J
Junio C Hamano 已提交
690

691 692 693 694 695 696 697 698 699 700 701 702 703
	if (check_all_attr[attr_nr].value != ATTR__TRUE)
		return rem;

	for (stk = attr_stack; !a && stk; stk = stk->prev)
		for (i = stk->num_matches - 1; !a && 0 <= i; i--) {
			struct match_attr *ma = stk->attrs[i];
			if (!ma->is_macro)
				continue;
			if (ma->u.attr->attr_nr == attr_nr)
				a = ma;
		}

	if (a)
704
		rem = fill_one("expand", a, rem);
705

J
Junio C Hamano 已提交
706 707 708
	return rem;
}

709 710 711 712 713 714
int git_checkattr(const char *path, int num, struct git_attr_check *check)
{
	struct attr_stack *stk;
	const char *cp;
	int dirlen, pathlen, i, rem;

J
Junio C Hamano 已提交
715 716
	bootstrap_attr_stack();
	for (i = 0; i < attr_nr; i++)
717
		check_all_attr[i].value = ATTR__UNKNOWN;
718 719 720 721 722 723 724 725

	pathlen = strlen(path);
	cp = strrchr(path, '/');
	if (!cp)
		dirlen = 0;
	else
		dirlen = cp - path;
	prepare_attr_stack(path, dirlen);
J
Junio C Hamano 已提交
726 727 728 729
	rem = attr_nr;
	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
		rem = fill(path, pathlen, stk, rem);

730
	for (i = 0; i < num; i++) {
731
		const char *value = check_all_attr[check[i].attr->attr_nr].value;
732 733 734 735
		if (value == ATTR__UNKNOWN)
			value = ATTR__UNSET;
		check[i].value = value;
	}
J
Junio C Hamano 已提交
736

737 738
	return 0;
}
739 740 741 742

void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
{
	enum git_attr_direction old = direction;
743 744 745 746

	if (is_bare_repository() && new != GIT_ATTR_INDEX)
		die("BUG: non-INDEX attr direction in a bare repo");

747 748 749 750 751
	direction = new;
	if (new != old)
		drop_attr_stack();
	use_index = istate;
}