attr.c 19.2 KB
Newer Older
M
Michael Haggerty 已提交
1 2 3 4 5 6 7 8 9
/*
 * Handle git attributes.  See gitattributes(5) for a description of
 * the file syntax, and Documentation/technical/api-gitattributes.txt
 * for a description of the API.
 *
 * One basic design decision here is that we are not going to support
 * an insanely large number of attributes.
 */

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

16 17 18 19 20 21 22
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
23

M
Michael Haggerty 已提交
24
/* This is a randomly chosen prime. */
25 26 27 28 29 30 31 32 33
#define HASHSIZE 257

#ifndef DEBUG_ATTR
#define DEBUG_ATTR 0
#endif

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

J
Junio C Hamano 已提交
39
static struct git_attr_check *check_all_attr;
40 41
static struct git_attr *(git_attr_hash[HASHSIZE]);

42 43 44 45 46
char *git_attr_name(struct git_attr *attr)
{
	return attr->name;
}

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

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

58 59 60
static int invalid_attr_name(const char *name, int namelen)
{
	/*
61 62
	 * Attribute name cannot begin with '-' and must consist of
	 * characters from [-A-Za-z0-9_.].
63
	 */
64
	if (namelen <= 0 || *name == '-')
65 66 67 68 69 70 71 72 73 74 75 76
		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;
}

77
static struct git_attr *git_attr_internal(const char *name, int len)
78 79 80 81 82 83 84 85 86 87 88
{
	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;
	}

89 90 91
	if (invalid_attr_name(name, len))
		return NULL;

92 93 94 95 96
	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 已提交
97
	a->attr_nr = attr_nr++;
98
	git_attr_hash[pos] = a;
J
Junio C Hamano 已提交
99 100 101 102

	check_all_attr = xrealloc(check_all_attr,
				  sizeof(*check_all_attr) * attr_nr);
	check_all_attr[a->attr_nr].attr = a;
103
	check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
104 105 106
	return a;
}

107 108 109 110 111
struct git_attr *git_attr(const char *name)
{
	return git_attr_internal(name, strlen(name));
}

112
/* What does a matched pattern decide? */
113 114
struct attr_state {
	struct git_attr *attr;
115
	const char *setto;
116 117
};

M
Michael Haggerty 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131
/*
 * One rule, as from a .gitattributes file.
 *
 * If is_macro is true, then u.attr is a pointer to the git_attr being
 * defined.
 *
 * If is_macro is false, then u.pattern points at the filename pattern
 * to which the rule applies.  (The memory pointed to is part of the
 * memory block allocated for the match_attr instance.)
 *
 * In either case, num_attr is the number of attributes affected by
 * this rule, and state is an array listing them.  The attributes are
 * listed as they appear in the file (macros unexpanded).
 */
132
struct match_attr {
J
Junio C Hamano 已提交
133 134 135 136 137
	union {
		char *pattern;
		struct git_attr *attr;
	} u;
	char is_macro;
138 139 140 141 142 143
	unsigned num_attr;
	struct attr_state state[FLEX_ARRAY];
};

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

144 145 146 147 148 149 150
/*
 * Parse a whitespace-delimited attribute state (i.e., "attr",
 * "-attr", "!attr", or "attr=value") from the string starting at src.
 * If e is not NULL, write the results to *e.  Return a pointer to the
 * remainder of the string (with leading whitespace removed), or NULL
 * if there was an error.
 */
151
static const char *parse_attr(const char *src, int lineno, const char *cp,
152
			      struct attr_state *e)
153 154 155 156 157 158 159 160 161 162 163 164
{
	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;
165
	if (!e) {
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
		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 {
		if (*cp == '-' || *cp == '!') {
			e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
			cp++;
			len--;
		}
		else if (!equals)
			e->setto = ATTR__TRUE;
		else {
P
Pierre Habouzit 已提交
185
			e->setto = xmemdupz(equals + 1, ep - equals - 1);
186
		}
187
		e->attr = git_attr_internal(cp, len);
188 189 190 191
	}
	return ep + strspn(ep, blank);
}

J
Junio C Hamano 已提交
192 193
static struct match_attr *parse_attr_line(const char *line, const char *src,
					  int lineno, int macro_ok)
194 195
{
	int namelen;
M
Michael Haggerty 已提交
196
	int num_attr, i;
197
	const char *cp, *name, *states;
198
	struct match_attr *res = NULL;
J
Junio C Hamano 已提交
199
	int is_macro;
200 201 202 203 204 205

	cp = line + strspn(line, blank);
	if (!*cp || *cp == '#')
		return NULL;
	name = cp;
	namelen = strcspn(name, blank);
J
Junio C Hamano 已提交
206 207 208 209 210 211 212 213 214 215 216
	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);
217 218 219 220 221 222
		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 已提交
223 224 225
	}
	else
		is_macro = 0;
226

227 228 229
	states = name + namelen;
	states += strspn(states, blank);

M
Michael Haggerty 已提交
230 231 232 233 234
	/* First pass to count the attr_states */
	for (cp = states, num_attr = 0; *cp; num_attr++) {
		cp = parse_attr(src, lineno, cp, NULL);
		if (!cp)
			return NULL;
235
	}
M
Michael Haggerty 已提交
236 237 238 239 240 241 242 243 244 245 246

	res = xcalloc(1,
		      sizeof(*res) +
		      sizeof(struct attr_state) * num_attr +
		      (is_macro ? 0 : namelen + 1));
	if (is_macro)
		res->u.attr = git_attr_internal(name, namelen);
	else {
		res->u.pattern = (char *)&(res->state[num_attr]);
		memcpy(res->u.pattern, name, namelen);
		res->u.pattern[namelen] = 0;
247
	}
M
Michael Haggerty 已提交
248 249 250 251 252 253 254 255
	res->is_macro = is_macro;
	res->num_attr = num_attr;

	/* Second pass to fill the attr_states */
	for (cp = states, i = 0; *cp; i++) {
		cp = parse_attr(src, lineno, cp, &(res->state[i]));
	}

256 257 258 259 260 261 262 263
	return res;
}

/*
 * Like info/exclude and .gitignore, the attribute information can
 * come from many places.
 *
 * (1) .gitattribute file of the same directory;
264 265 266
 * (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.
267 268 269 270 271 272 273 274 275 276 277 278 279
 *
 * 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;
280
	size_t originlen;
281
	unsigned num_matches;
J
Junio C Hamano 已提交
282
	unsigned alloc;
283 284 285 286 287 288 289
	struct match_attr **attrs;
} *attr_stack;

static void free_attr_elem(struct attr_stack *e)
{
	int i;
	free(e->origin);
290 291 292 293
	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++) {
294
			const char *setto = a->state[j].setto;
295 296 297 298 299 300
			if (setto == ATTR__TRUE ||
			    setto == ATTR__FALSE ||
			    setto == ATTR__UNSET ||
			    setto == ATTR__UNKNOWN)
				;
			else
301
				free((char *) setto);
302 303 304
		}
		free(a);
	}
J
Jeff King 已提交
305
	free(e->attrs);
306 307 308 309
	free(e);
}

static const char *builtin_attr[] = {
310
	"[attr]binary -diff -merge -text",
311 312 313
	NULL,
};

J
Junio C Hamano 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
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;
}

334 335 336 337
static struct attr_stack *read_attr_from_array(const char **list)
{
	struct attr_stack *res;
	const char *line;
J
Junio C Hamano 已提交
338
	int lineno = 0;
339 340

	res = xcalloc(1, sizeof(*res));
J
Junio C Hamano 已提交
341 342
	while ((line = *(list++)) != NULL)
		handle_attr_line(res, line, "[builtin]", ++lineno, 1);
343 344 345
	return res;
}

346 347 348
static enum git_attr_direction direction;
static struct index_state *use_index;

J
Junio C Hamano 已提交
349
static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
350
{
J
Junio C Hamano 已提交
351
	FILE *fp = fopen(path, "r");
352 353
	struct attr_stack *res;
	char buf[2048];
J
Junio C Hamano 已提交
354
	int lineno = 0;
355

356
	if (!fp) {
357
		if (errno != ENOENT && errno != ENOTDIR)
358
			warn_on_inaccessible(path);
J
Junio C Hamano 已提交
359
		return NULL;
360
	}
J
Junio C Hamano 已提交
361 362 363 364 365 366
	res = xcalloc(1, sizeof(*res));
	while (fgets(buf, sizeof(buf), fp))
		handle_attr_line(res, buf, path, ++lineno, macro_ok);
	fclose(fp);
	return res;
}
367

368 369 370 371 372 373
static void *read_index_data(const char *path)
{
	int pos, len;
	unsigned long sz;
	enum object_type type;
	void *data;
374
	struct index_state *istate = use_index ? use_index : &the_index;
375 376

	len = strlen(path);
377
	pos = index_name_pos(istate, path, len);
378 379 380 381 382 383 384
	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;
385 386
		     (pos < 0 && i < istate->cache_nr &&
		      !strcmp(istate->cache[i]->name, path));
387
		     i++)
388
			if (ce_stage(istate->cache[i]) == 2)
389 390 391 392
				pos = i;
	}
	if (pos < 0)
		return NULL;
393
	data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
394 395 396 397 398 399 400
	if (!data || type != OBJ_BLOB) {
		free(data);
		return NULL;
	}
	return data;
}

401
static struct attr_stack *read_attr_from_index(const char *path, int macro_ok)
J
Junio C Hamano 已提交
402 403
{
	struct attr_stack *res;
404 405
	char *buf, *sp;
	int lineno = 0;
J
Junio C Hamano 已提交
406

407 408
	buf = read_index_data(path);
	if (!buf)
409
		return NULL;
410

411
	res = xcalloc(1, sizeof(*res));
412 413 414 415 416 417 418 419 420 421 422
	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);
423 424 425
	return res;
}

426 427 428 429 430 431 432 433 434
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);
	}
435
	else if (direction == GIT_ATTR_CHECKIN) {
436 437 438 439 440 441 442 443 444
		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);
	}
445 446
	else
		res = read_attr_from_index(path, macro_ok);
447 448 449 450 451
	if (!res)
		res = xcalloc(1, sizeof(*res));
	return res;
}

452 453 454 455 456
#if DEBUG_ATTR
static void debug_info(const char *what, struct attr_stack *elem)
{
	fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
}
457
static void debug_set(const char *what, const char *match, struct git_attr *attr, const void *v)
J
Junio C Hamano 已提交
458
{
459 460 461 462 463 464 465 466 467 468 469
	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 已提交
470
}
471 472 473 474 475
#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 已提交
476
#define debug_set(a,b,c,d) do { ; } while (0)
477 478
#endif

479 480 481 482 483 484 485 486 487
static void drop_attr_stack(void)
{
	while (attr_stack) {
		struct attr_stack *elem = attr_stack;
		attr_stack = elem->prev;
		free_attr_elem(elem);
	}
}

488
static const char *git_etc_gitattributes(void)
489 490 491 492 493 494 495
{
	static const char *system_wide;
	if (!system_wide)
		system_wide = system_path(ETC_GITATTRIBUTES);
	return system_wide;
}

496
static int git_attr_system(void)
497 498 499 500
{
	return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
}

J
Junio C Hamano 已提交
501 502
static void bootstrap_attr_stack(void)
{
503
	struct attr_stack *elem;
504
	char *xdg_attributes_file;
J
Junio C Hamano 已提交
505

506 507
	if (attr_stack)
		return;
J
Junio C Hamano 已提交
508

509 510 511 512
	elem = read_attr_from_array(builtin_attr);
	elem->origin = NULL;
	elem->prev = attr_stack;
	attr_stack = elem;
513

514 515 516 517 518 519
	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;
520
		}
521
	}
522

523 524 525 526
	if (!git_attributes_file) {
		home_config_paths(NULL, &xdg_attributes_file, "attributes");
		git_attributes_file = xdg_attributes_file;
	}
527 528 529 530 531 532 533
	if (git_attributes_file) {
		elem = read_attr_from_file(git_attributes_file, 1);
		if (elem) {
			elem->origin = NULL;
			elem->prev = attr_stack;
			attr_stack = elem;
		}
534
	}
J
Junio C Hamano 已提交
535

536 537
	if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
		elem = read_attr(GITATTRIBUTES_FILE, 1);
538
		elem->origin = xstrdup("");
539
		elem->originlen = 0;
J
Junio C Hamano 已提交
540 541
		elem->prev = attr_stack;
		attr_stack = elem;
542
		debug_push(elem);
J
Junio C Hamano 已提交
543
	}
544 545 546 547 548 549 550

	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 已提交
551 552
}

553
static void prepare_attr_stack(const char *path)
554 555
{
	struct attr_stack *elem, *info;
556 557 558 559 560 561 562 563
	int dirlen, len;
	const char *cp;

	cp = strrchr(path, '/');
	if (!cp)
		dirlen = 0;
	else
		dirlen = cp - path;
564

565 566
	/*
	 * At the bottom of the attribute stack is the built-in
567 568 569
	 * set of attribute definitions, followed by the contents
	 * of $(prefix)/etc/gitattributes and a file specified by
	 * core.attributesfile.  Then, contents from
570 571 572 573 574 575 576 577 578 579
	 * .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.
	 */
M
Michael Haggerty 已提交
580
	bootstrap_attr_stack();
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
590 591 592
	 * 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.
593
	 */
594
	while (attr_stack->origin) {
595 596 597 598
		int namelen = strlen(attr_stack->origin);

		elem = attr_stack;
		if (namelen <= dirlen &&
599 600
		    !strncmp(elem->origin, path, namelen) &&
		    (!namelen || path[namelen] == '/'))
601 602 603 604 605 606 607 608 609 610
			break;

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

	/*
	 * Read from parent directories and push them down
	 */
611
	if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
612 613 614 615 616 617
		/*
		 * 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.
		 */
618
		struct strbuf pathbuf = STRBUF_INIT;
619

620
		assert(attr_stack->origin);
621
		while (1) {
622 623 624
			len = strlen(attr_stack->origin);
			if (dirlen <= len)
				break;
625 626 627 628
			cp = memchr(path + len + 1, '/', dirlen - len - 1);
			if (!cp)
				cp = path + dirlen;
			strbuf_add(&pathbuf, path, cp - path);
629
			strbuf_addch(&pathbuf, '/');
630
			strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE);
631
			elem = read_attr(pathbuf.buf, 0);
632
			strbuf_setlen(&pathbuf, cp - path);
633
			elem->origin = strbuf_detach(&pathbuf, &elem->originlen);
634 635 636 637
			elem->prev = attr_stack;
			attr_stack = elem;
			debug_push(elem);
		}
638

639 640
		strbuf_release(&pathbuf);
	}
R
René Scharfe 已提交
641

642 643 644 645 646 647 648 649
	/*
	 * 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,
650
			const char *basename,
651 652 653 654
			const char *pattern,
			const char *base, int baselen)
{
	if (!strchr(pattern, '/')) {
655
		return (fnmatch_icase(pattern, basename, 0) == 0);
656 657 658 659 660 661 662 663
	}
	/*
	 * match with FNM_PATHNAME; the pattern has base implicitly
	 * in front of it.
	 */
	if (*pattern == '/')
		pattern++;
	if (pathlen < baselen ||
664
	    (baselen && pathname[baselen] != '/') ||
665 666
	    strncmp(pathname, base, baselen))
		return 0;
667 668
	if (baselen != 0)
		baselen++;
669
	return fnmatch_icase(pattern, pathname + baselen, FNM_PATHNAME) == 0;
670 671
}

672 673
static int macroexpand_one(int attr_nr, int rem);

674 675 676 677 678
static int fill_one(const char *what, struct match_attr *a, int rem)
{
	struct git_attr_check *check = check_all_attr;
	int i;

679
	for (i = a->num_attr - 1; 0 < rem && 0 <= i; i--) {
680
		struct git_attr *attr = a->state[i].attr;
681 682
		const char **n = &(check[attr->attr_nr].value);
		const char *v = a->state[i].setto;
683 684

		if (*n == ATTR__UNKNOWN) {
685 686 687
			debug_set(what,
				  a->is_macro ? a->u.attr->name : a->u.pattern,
				  attr, v);
688 689
			*n = v;
			rem--;
690
			rem = macroexpand_one(attr->attr_nr, rem);
691 692 693 694 695
		}
	}
	return rem;
}

696 697
static int fill(const char *path, int pathlen, const char *basename,
		struct attr_stack *stk, int rem)
698
{
699
	int i;
700 701 702 703
	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 已提交
704 705
		if (a->is_macro)
			continue;
706
		if (path_matches(path, pathlen, basename,
707
				 a->u.pattern, base, stk->originlen))
708
			rem = fill_one("fill", a, rem);
709 710 711 712
	}
	return rem;
}

713
static int macroexpand_one(int attr_nr, int rem)
J
Junio C Hamano 已提交
714
{
715 716
	struct attr_stack *stk;
	struct match_attr *a = NULL;
717
	int i;
J
Junio C Hamano 已提交
718

719 720 721 722 723 724 725 726 727 728 729 730 731
	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)
732
		rem = fill_one("expand", a, rem);
733

J
Junio C Hamano 已提交
734 735 736
	return rem;
}

737 738 739 740 741
/*
 * Collect all attributes for path into the array pointed to by
 * check_all_attr.
 */
static void collect_all_attrs(const char *path)
742 743
{
	struct attr_stack *stk;
744
	int i, pathlen, rem;
745
	const char *basename;
746

747
	prepare_attr_stack(path);
J
Junio C Hamano 已提交
748
	for (i = 0; i < attr_nr; i++)
749
		check_all_attr[i].value = ATTR__UNKNOWN;
750

751 752 753
	basename = strrchr(path, '/');
	basename = basename ? basename + 1 : path;

754
	pathlen = strlen(path);
J
Junio C Hamano 已提交
755 756
	rem = attr_nr;
	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
757
		rem = fill(path, pathlen, basename, stk, rem);
758 759
}

760
int git_check_attr(const char *path, int num, struct git_attr_check *check)
761 762 763 764
{
	int i;

	collect_all_attrs(path);
J
Junio C Hamano 已提交
765

766
	for (i = 0; i < num; i++) {
767
		const char *value = check_all_attr[check[i].attr->attr_nr].value;
768 769 770 771
		if (value == ATTR__UNKNOWN)
			value = ATTR__UNSET;
		check[i].value = value;
	}
J
Junio C Hamano 已提交
772

773 774
	return 0;
}
775

776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
int git_all_attrs(const char *path, int *num, struct git_attr_check **check)
{
	int i, count, j;

	collect_all_attrs(path);

	/* Count the number of attributes that are set. */
	count = 0;
	for (i = 0; i < attr_nr; i++) {
		const char *value = check_all_attr[i].value;
		if (value != ATTR__UNSET && value != ATTR__UNKNOWN)
			++count;
	}
	*num = count;
	*check = xmalloc(sizeof(**check) * count);
	j = 0;
	for (i = 0; i < attr_nr; i++) {
		const char *value = check_all_attr[i].value;
		if (value != ATTR__UNSET && value != ATTR__UNKNOWN) {
			(*check)[j].attr = check_all_attr[i].attr;
			(*check)[j].value = value;
			++j;
		}
	}

	return 0;
}

804 805 806
void git_attr_set_direction(enum git_attr_direction new, struct index_state *istate)
{
	enum git_attr_direction old = direction;
807 808 809 810

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

811 812 813 814 815
	direction = new;
	if (new != old)
		drop_attr_stack();
	use_index = istate;
}