attr.c 12.8 KB
Newer Older
1 2 3
#include "cache.h"
#include "attr.h"

4 5
#define ATTR__UNKNOWN	((void *) -2)

6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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 已提交
21
	int attr_nr;
22 23
	char name[FLEX_ARRAY];
};
J
Junio C Hamano 已提交
24
static int attr_nr;
25

J
Junio C Hamano 已提交
26
static struct git_attr_check *check_all_attr;
27 28 29 30 31 32 33 34 35 36 37 38 39 40
static struct git_attr *(git_attr_hash[HASHSIZE]);

static unsigned hash_name(const char *name, int namelen)
{
	unsigned val = 0;
	unsigned char c;

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

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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;
}

62 63 64 65 66 67 68 69 70 71 72 73
struct git_attr *git_attr(const char *name, int len)
{
	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;
	}

74 75 76
	if (invalid_attr_name(name, len))
		return NULL;

77 78 79 80 81
	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 已提交
82
	a->attr_nr = attr_nr++;
83
	git_attr_hash[pos] = a;
J
Junio C Hamano 已提交
84 85 86 87

	check_all_attr = xrealloc(check_all_attr,
				  sizeof(*check_all_attr) * attr_nr);
	check_all_attr[a->attr_nr].attr = a;
88
	check_all_attr[a->attr_nr].value = ATTR__UNKNOWN;
89 90 91 92 93 94 95 96 97
	return a;
}

/*
 * .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
98 99
 *     could be prefixed with '-' to mean "set to false", '!' to mean
 *     "unset".
100 101
 */

102
/* What does a matched pattern decide? */
103 104
struct attr_state {
	struct git_attr *attr;
105
	void *setto;
106 107 108
};

struct match_attr {
J
Junio C Hamano 已提交
109 110 111 112 113
	union {
		char *pattern;
		struct git_attr *attr;
	} u;
	char is_macro;
114 115 116 117 118 119
	unsigned num_attr;
	struct attr_state state[FLEX_ARRAY];
};

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

120 121 122 123 124 125 126 127 128 129 130 131 132 133 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
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 {
			char *value;
			int vallen = ep - equals;
			value = xmalloc(vallen);
			memcpy(value, equals+1, vallen-1);
			value[vallen-1] = 0;
			e->setto = value;
		}
		e->attr = git_attr(cp, len);
	}
	(*num_attr)++;
	return ep + strspn(ep, blank);
}

J
Junio C Hamano 已提交
170 171
static struct match_attr *parse_attr_line(const char *line, const char *src,
					  int lineno, int macro_ok)
172 173 174 175
{
	int namelen;
	int num_attr;
	const char *cp, *name;
176
	struct match_attr *res = NULL;
177
	int pass;
J
Junio C Hamano 已提交
178
	int is_macro;
179 180 181 182 183 184

	cp = line + strspn(line, blank);
	if (!*cp || *cp == '#')
		return NULL;
	name = cp;
	namelen = strcspn(name, blank);
J
Junio C Hamano 已提交
185 186 187 188 189 190 191 192 193 194 195
	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);
196 197 198 199 200 201
		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 已提交
202 203 204
	}
	else
		is_macro = 0;
205 206 207 208 209 210

	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);
211 212
		while (*cp)
			cp = parse_attr(src, lineno, cp, &num_attr, res);
213 214 215 216 217
		if (pass)
			break;
		res = xcalloc(1,
			      sizeof(*res) +
			      sizeof(struct attr_state) * num_attr +
J
Junio C Hamano 已提交
218
			      (is_macro ? 0 : namelen + 1));
219
		if (is_macro)
J
Junio C Hamano 已提交
220 221 222 223 224 225 226
			res->u.attr = git_attr(name, namelen);
		else {
			res->u.pattern = (char*)&(res->state[num_attr]);
			memcpy(res->u.pattern, name, namelen);
			res->u.pattern[namelen] = 0;
		}
		res->is_macro = is_macro;
227 228 229 230 231 232 233 234 235 236
		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;
237 238 239
 * (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.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
 *
 * 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;
	struct match_attr **attrs;
} *attr_stack;

static void free_attr_elem(struct attr_stack *e)
{
	int i;
	free(e->origin);
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
	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++) {
			void *setto = a->state[j].setto;
			if (setto == ATTR__TRUE ||
			    setto == ATTR__FALSE ||
			    setto == ATTR__UNSET ||
			    setto == ATTR__UNKNOWN)
				;
			else
				free(setto);
		}
		free(a);
	}
276 277 278 279
	free(e);
}

static const char *builtin_attr[] = {
280
	"[attr]binary -diff -crlf",
281 282 283 284 285 286 287
	NULL,
};

static struct attr_stack *read_attr_from_array(const char **list)
{
	struct attr_stack *res;
	const char *line;
J
Junio C Hamano 已提交
288
	int lineno = 0;
289 290 291

	res = xcalloc(1, sizeof(*res));
	while ((line = *(list++)) != NULL) {
J
Junio C Hamano 已提交
292 293 294
		struct match_attr *a;

		a = parse_attr_line(line, "[builtin]", ++lineno, 1);
295 296 297 298 299 300 301 302
		if (!a)
			continue;
		res->attrs = xrealloc(res->attrs, res->num_matches + 1);
		res->attrs[res->num_matches++] = a;
	}
	return res;
}

J
Junio C Hamano 已提交
303
static struct attr_stack *read_attr_from_file(const char *path, int macro_ok)
304 305 306 307
{
	FILE *fp;
	struct attr_stack *res;
	char buf[2048];
J
Junio C Hamano 已提交
308
	int lineno = 0;
309 310 311 312 313 314 315

	res = xcalloc(1, sizeof(*res));
	fp = fopen(path, "r");
	if (!fp)
		return res;

	while (fgets(buf, sizeof(buf), fp)) {
J
Junio C Hamano 已提交
316 317 318
		struct match_attr *a;

		a = parse_attr_line(buf, path, ++lineno, macro_ok);
319 320 321 322 323 324 325 326 327 328 329 330 331 332
		if (!a)
			continue;
		res->attrs = xrealloc(res->attrs, res->num_matches + 1);
		res->attrs[res->num_matches++] = a;
	}
	fclose(fp);
	return res;
}

#if DEBUG_ATTR
static void debug_info(const char *what, struct attr_stack *elem)
{
	fprintf(stderr, "%s: %s\n", what, elem->origin ? elem->origin : "()");
}
333
static void debug_set(const char *what, const char *match, struct git_attr *attr, void *v)
J
Junio C Hamano 已提交
334
{
335 336 337 338 339 340 341 342 343 344 345
	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 已提交
346
}
347 348 349 350 351
#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 已提交
352
#define debug_set(a,b,c,d) do { ; } while (0)
353 354
#endif

J
Junio C Hamano 已提交
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
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;

		elem = read_attr_from_file(GITATTRIBUTES_FILE, 1);
		elem->origin = strdup("");
		elem->prev = attr_stack;
		attr_stack = elem;
		debug_push(elem);

		elem = read_attr_from_file(git_path(INFOATTRIBUTES_FILE), 1);
		elem->origin = NULL;
		elem->prev = attr_stack;
		attr_stack = elem;
	}
}

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
static void prepare_attr_stack(const char *path, int dirlen)
{
	struct attr_stack *elem, *info;
	int len;
	char pathbuf[PATH_MAX];

	/*
	 * At the bottom of the attribute stack is the built-in
	 * set of attribute definitions.  Then, contents from
	 * .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 已提交
397 398
	if (!attr_stack)
		bootstrap_attr_stack();
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435

	/*
	 * 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
	 */
	while (1) {
		char *cp;

		len = strlen(attr_stack->origin);
		if (dirlen <= len)
			break;
		memcpy(pathbuf, path, dirlen);
		memcpy(pathbuf + dirlen, "/", 2);
		cp = strchr(pathbuf + len + 1, '/');
		strcpy(cp + 1, GITATTRIBUTES_FILE);
J
Junio C Hamano 已提交
436
		elem = read_attr_from_file(pathbuf, 0);
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
		*cp = '\0';
		elem->origin = strdup(pathbuf);
		elem->prev = attr_stack;
		attr_stack = elem;
		debug_push(elem);
	}

	/*
	 * 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 ||
	    (baselen && pathname[baselen - 1] != '/') ||
	    strncmp(pathname, base, baselen))
		return 0;
	return fnmatch(pattern, pathname + baselen, FNM_PATHNAME) == 0;
}

474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
static int fill_one(const char *what, struct match_attr *a, int rem)
{
	struct git_attr_check *check = check_all_attr;
	int i;

	for (i = 0; 0 < rem && i < a->num_attr; i++) {
		struct git_attr *attr = a->state[i].attr;
		void **n = &(check[attr->attr_nr].value);
		void *v = a->state[i].setto;

		if (*n == ATTR__UNKNOWN) {
			debug_set(what, a->u.pattern, attr, v);
			*n = v;
			rem--;
		}
	}
	return rem;
}

J
Junio C Hamano 已提交
493
static int fill(const char *path, int pathlen, struct attr_stack *stk, int rem)
494
{
495
	int i;
496 497 498 499
	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 已提交
500 501
		if (a->is_macro)
			continue;
502
		if (path_matches(path, pathlen,
503 504
				 a->u.pattern, base, strlen(base)))
			rem = fill_one("fill", a, rem);
505 506 507 508
	}
	return rem;
}

J
Junio C Hamano 已提交
509 510
static int macroexpand(struct attr_stack *stk, int rem)
{
511
	int i;
J
Junio C Hamano 已提交
512 513 514 515 516 517
	struct git_attr_check *check = check_all_attr;

	for (i = stk->num_matches - 1; 0 < rem && 0 <= i; i--) {
		struct match_attr *a = stk->attrs[i];
		if (!a->is_macro)
			continue;
518
		if (check[a->u.attr->attr_nr].value != ATTR__TRUE)
J
Junio C Hamano 已提交
519
			continue;
520
		rem = fill_one("expand", a, rem);
J
Junio C Hamano 已提交
521 522 523 524
	}
	return rem;
}

525 526 527 528 529 530
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 已提交
531 532
	bootstrap_attr_stack();
	for (i = 0; i < attr_nr; i++)
533
		check_all_attr[i].value = ATTR__UNKNOWN;
534 535 536 537 538 539 540 541

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

546
	for (stk = attr_stack; 0 < rem && stk; stk = stk->prev)
J
Junio C Hamano 已提交
547 548
		rem = macroexpand(stk, rem);

549 550 551 552 553 554
	for (i = 0; i < num; i++) {
		void *value = check_all_attr[check[i].attr->attr_nr].value;
		if (value == ATTR__UNKNOWN)
			value = ATTR__UNSET;
		check[i].value = value;
	}
J
Junio C Hamano 已提交
555

556 557
	return 0;
}