attr.c 17.6 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 39 40
static struct git_attr *(git_attr_hash[HASHSIZE]);

static unsigned hash_name(const char *name, int namelen)
{
41
	unsigned val = 0, c;
42 43 44 45 46 47 48 49

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

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
static int invalid_attr_name(const char *name, int namelen)
{
	/*
	 * Attribute name cannot begin with '-' and from
	 * [-A-Za-z0-9_.].  We'd specifically exclude '=' for now,
	 * as we might later want to allow non-binary value for
	 * attributes, e.g. "*.svg	merge=special-merge-program-for-svg"
	 */
	if (*name == '-')
		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;
}

71
static struct git_attr *git_attr_internal(const char *name, int len)
72 73 74 75 76 77 78 79 80 81 82
{
	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;
	}

83 84 85
	if (invalid_attr_name(name, len))
		return NULL;

86 87 88 89 90
	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 已提交
91
	a->attr_nr = attr_nr++;
92
	git_attr_hash[pos] = a;
J
Junio C Hamano 已提交
93 94 95 96

	check_all_attr = xrealloc(check_all_attr,
				  sizeof(*check_all_attr) * attr_nr);
	check_all_attr[a->attr_nr].attr = a;
97
	check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
98 99 100
	return a;
}

101 102 103 104 105
struct git_attr *git_attr(const char *name)
{
	return git_attr_internal(name, strlen(name));
}

106 107 108 109 110 111
/*
 * .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
112 113
 *     could be prefixed with '-' to mean "set to false", '!' to mean
 *     "unset".
114 115
 */

116
/* What does a matched pattern decide? */
117 118
struct attr_state {
	struct git_attr *attr;
119
	const char *setto;
120 121 122
};

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

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

134 135 136 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
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 已提交
171
			e->setto = xmemdupz(equals + 1, ep - equals - 1);
172
		}
173
		e->attr = git_attr_internal(cp, len);
174 175 176 177 178
	}
	(*num_attr)++;
	return ep + strspn(ep, blank);
}

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

	cp = line + strspn(line, blank);
	if (!*cp || *cp == '#')
		return NULL;
	name = cp;
	namelen = strcspn(name, blank);
J
Junio C Hamano 已提交
194 195 196 197 198 199 200 201 202 203 204
	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);
205 206 207 208 209 210
		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 已提交
211 212 213
	}
	else
		is_macro = 0;
214 215 216 217 218 219

	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);
220
		while (*cp) {
221
			cp = parse_attr(src, lineno, cp, &num_attr, res);
222 223 224
			if (!cp)
				return NULL;
		}
225 226 227 228 229
		if (pass)
			break;
		res = xcalloc(1,
			      sizeof(*res) +
			      sizeof(struct attr_state) * num_attr +
J
Junio C Hamano 已提交
230
			      (is_macro ? 0 : namelen + 1));
231
		if (is_macro)
232
			res->u.attr = git_attr_internal(name, namelen);
J
Junio C Hamano 已提交
233
		else {
234
			res->u.pattern = (char *)&(res->state[num_attr]);
J
Junio C Hamano 已提交
235 236 237 238
			memcpy(res->u.pattern, name, namelen);
			res->u.pattern[namelen] = 0;
		}
		res->is_macro = is_macro;
239 240 241 242 243 244 245 246 247 248
		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;
249 250 251
 * (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.
252 253 254 255 256 257 258 259 260 261 262 263 264 265
 *
 * 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 已提交
266
	unsigned alloc;
267 268 269 270 271 272 273
	struct match_attr **attrs;
} *attr_stack;

static void free_attr_elem(struct attr_stack *e)
{
	int i;
	free(e->origin);
274 275 276 277
	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++) {
278
			const char *setto = a->state[j].setto;
279 280 281 282 283 284
			if (setto == ATTR__TRUE ||
			    setto == ATTR__FALSE ||
			    setto == ATTR__UNSET ||
			    setto == ATTR__UNKNOWN)
				;
			else
285
				free((char *) setto);
286 287 288
		}
		free(a);
	}
289 290 291 292
	free(e);
}

static const char *builtin_attr[] = {
293
	"[attr]binary -diff -text",
294 295 296
	NULL,
};

J
Junio C Hamano 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
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;
}

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

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

329 330 331
static enum git_attr_direction direction;
static struct index_state *use_index;

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

	if (!fp)
J
Junio C Hamano 已提交
340 341 342 343 344 345 346
		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;
}
347

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

	len = strlen(path);
357
	pos = index_name_pos(istate, path, len);
358 359 360 361 362 363 364
	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;
365 366
		     (pos < 0 && i < istate->cache_nr &&
		      !strcmp(istate->cache[i]->name, path));
367
		     i++)
368
			if (ce_stage(istate->cache[i]) == 2)
369 370 371 372
				pos = i;
	}
	if (pos < 0)
		return NULL;
373
	data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
374 375 376 377 378 379 380
	if (!data || type != OBJ_BLOB) {
		free(data);
		return NULL;
	}
	return data;
}

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

387 388
	buf = read_index_data(path);
	if (!buf)
389
		return NULL;
390

391
	res = xcalloc(1, sizeof(*res));
392 393 394 395 396 397 398 399 400 401 402
	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);
403 404 405
	return res;
}

406 407 408 409 410 411 412 413 414
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);
	}
415
	else if (direction == GIT_ATTR_CHECKIN) {
416 417 418 419 420 421 422 423 424
		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);
	}
425 426
	else
		res = read_attr_from_index(path, macro_ok);
427 428 429 430 431
	if (!res)
		res = xcalloc(1, sizeof(*res));
	return res;
}

432 433 434 435 436
#if DEBUG_ATTR
static void debug_info(const char *what, struct attr_stack *elem)
{
	fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
}
437
static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
J
Junio C Hamano 已提交
438
{
439 440 441 442 443 444 445 446 447 448 449
	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 已提交
450
}
451 452 453 454 455
#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 已提交
456
#define debug_set(a,b,c,d) do { ; } while (0)
457 458
#endif

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

468
static const char *git_etc_gitattributes(void)
469 470 471 472 473 474 475
{
	static const char *system_wide;
	if (!system_wide)
		system_wide = system_path(ETC_GITATTRIBUTES);
	return system_wide;
}

476
static int git_attr_system(void)
477 478 479 480 481 482 483 484 485 486 487 488
{
	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 已提交
489 490
static void bootstrap_attr_stack(void)
{
491
	struct attr_stack *elem;
J
Junio C Hamano 已提交
492

493 494
	if (attr_stack)
		return;
J
Junio C Hamano 已提交
495

496 497 498 499
	elem = read_attr_from_array(builtin_attr);
	elem->origin = NULL;
	elem->prev = attr_stack;
	attr_stack = elem;
500

501 502 503 504 505 506
	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;
507
		}
508
	}
509

510 511 512 513 514
	git_config(git_attr_config, NULL);
	if (attributes_file) {
		elem = read_attr_from_file(attributes_file, 1);
		if (elem) {
			elem->origin = NULL;
515 516 517
			elem->prev = attr_stack;
			attr_stack = elem;
		}
518
	}
J
Junio C Hamano 已提交
519

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

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

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

	strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
543 544 545

	/*
	 * At the bottom of the attribute stack is the built-in
546 547 548
	 * set of attribute definitions, followed by the contents
	 * of $(prefix)/etc/gitattributes and a file specified by
	 * core.attributesfile.  Then, contents from
549 550 551 552 553 554 555 556 557 558
	 * .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 已提交
559 560
	if (!attr_stack)
		bootstrap_attr_stack();
561 562 563 564 565 566 567 568 569

	/*
	 * 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
570 571 572
	 * the path we are checking. Break out of the loop when we see
	 * the root one (whose origin is an empty string "") or the builtin
	 * one (whose origin is NULL) without popping it.
573
	 */
574
	while (attr_stack->origin) {
575 576 577 578
		int namelen = strlen(attr_stack->origin);

		elem = attr_stack;
		if (namelen <= dirlen &&
579 580
		    !strncmp(elem->origin, path, namelen) &&
		    (!namelen || path[namelen] == '/'))
581 582 583 584 585 586 587 588 589 590
			break;

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

	/*
	 * Read from parent directories and push them down
	 */
591
	if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
592 593 594 595 596 597 598
		/*
		 * bootstrap_attr_stack() should have added, and the
		 * above loop should have stopped before popping, the
		 * root element whose attr_stack->origin is set to an
		 * empty string.
		 */
		assert(attr_stack->origin);
599 600 601 602 603 604
		while (1) {
			char *cp;

			len = strlen(attr_stack->origin);
			if (dirlen <= len)
				break;
605 606 607 608
			strbuf_reset(&pathbuf);
			strbuf_add(&pathbuf, path, dirlen);
			strbuf_addch(&pathbuf, '/');
			cp = strchr(pathbuf.buf + len + 1, '/');
609
			strcpy(cp + 1, GITATTRIBUTES_FILE);
610
			elem = read_attr(pathbuf.buf, 0);
611
			*cp = '\0';
612
			elem->origin = strdup(pathbuf.buf);
613 614 615 616
			elem->prev = attr_stack;
			attr_stack = elem;
			debug_push(elem);
		}
617 618
	}

R
René Scharfe 已提交
619 620
	strbuf_release(&pathbuf);

621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
	/*
	 * 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 ||
645
	    (baselen && pathname[baselen] != '/') ||
646 647
	    strncmp(pathname, base, baselen))
		return 0;
648 649 650
	if (baselen != 0)
		baselen++;
	return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
651 652
}

653 654
static int macroexpand_one(int attr_nr, int rem);

655 656 657 658 659
static int fill_one(const char *what, struct match_attr *a, int rem)
{
	struct git_attr_check *check = check_all_attr;
	int i;

660
	for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
661
		struct git_attr *attr = a->state[i].attr;
662 663
		const char **n = &(check[attr->attr_nr].value);
		const char *v = a->state[i].setto;
664 665

		if (*n == ATTR__UNKNOWN) {
666 667 668
			debug_set(what,
				  a->is_macro ? a->u.attr->name : a->u.pattern,
				  attr, v);
669 670
			*n = v;
			rem--;
671
			rem = macroexpand_one(attr->attr_nr, rem);
672 673 674 675 676
		}
	}
	return rem;
}

J
Junio C Hamano 已提交
677
static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
678
{
679
	int i;
680 681 682 683
	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 已提交
684 685
		if (a->is_macro)
			continue;
686
		if (path_matches(path, pathlen,
687 688
				 a->u.pattern, base, strlen(base)))
			rem = fill_one("fill", a, rem);
689 690 691 692
	}
	return rem;
}

693
static int macroexpand_one(int attr_nr, int rem)
J
Junio C Hamano 已提交
694
{
695 696
	struct attr_stack *stk;
	struct match_attr *a = NULL;
697
	int i;
J
Junio C Hamano 已提交
698

699 700 701 702 703 704 705 706 707 708 709 710 711
	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)
712
		rem = fill_one("expand", a, rem);
713

J
Junio C Hamano 已提交
714 715 716
	return rem;
}

717 718 719 720 721 722
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 已提交
723 724
	bootstrap_attr_stack();
	for (i = 0; i < attr_nr; i++)
725
		check_all_attr[i].value = ATTR__UNKNOWN;
726 727 728 729 730 731 732 733

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

738
	for (i = 0; i < num; i++) {
739
		const char *value = check_all_attr[check[i].attr->attr_nr].value;
740 741 742 743
		if (value == ATTR__UNKNOWN)
			value = ATTR__UNSET;
		check[i].value = value;
	}
J
Junio C Hamano 已提交
744

745 746
	return 0;
}
747 748 749 750

void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
{
	enum git_attr_direction old = direction;
751 752 753 754

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

755 756 757 758 759
	direction = new;
	if (new != old)
		drop_attr_stack();
	use_index = istate;
}