git

Форк
0
/
attr.c 
1383 строки · 34.6 Кб
1
/*
2
 * Handle git attributes.  See gitattributes(5) for a description of
3
 * the file syntax, and attr.h for a description of the API.
4
 *
5
 * One basic design decision here is that we are not going to support
6
 * an insanely large number of attributes.
7
 */
8

9
#define USE_THE_REPOSITORY_VARIABLE
10

11
#include "git-compat-util.h"
12
#include "config.h"
13
#include "environment.h"
14
#include "exec-cmd.h"
15
#include "attr.h"
16
#include "dir.h"
17
#include "gettext.h"
18
#include "path.h"
19
#include "utf8.h"
20
#include "quote.h"
21
#include "read-cache-ll.h"
22
#include "refs.h"
23
#include "revision.h"
24
#include "object-store-ll.h"
25
#include "setup.h"
26
#include "thread-utils.h"
27
#include "tree-walk.h"
28
#include "object-name.h"
29

30
char *git_attr_tree;
31

32
const char git_attr__true[] = "(builtin)true";
33
const char git_attr__false[] = "\0(builtin)false";
34
static const char git_attr__unknown[] = "(builtin)unknown";
35
#define ATTR__TRUE git_attr__true
36
#define ATTR__FALSE git_attr__false
37
#define ATTR__UNSET NULL
38
#define ATTR__UNKNOWN git_attr__unknown
39

40
struct git_attr {
41
	unsigned int attr_nr; /* unique attribute number */
42
	char name[FLEX_ARRAY]; /* attribute name */
43
};
44

45
const char *git_attr_name(const struct git_attr *attr)
46
{
47
	return attr->name;
48
}
49

50
struct attr_hashmap {
51
	struct hashmap map;
52
	pthread_mutex_t mutex;
53
};
54

55
static inline void hashmap_lock(struct attr_hashmap *map)
56
{
57
	pthread_mutex_lock(&map->mutex);
58
}
59

60
static inline void hashmap_unlock(struct attr_hashmap *map)
61
{
62
	pthread_mutex_unlock(&map->mutex);
63
}
64

65
/* The container for objects stored in "struct attr_hashmap" */
66
struct attr_hash_entry {
67
	struct hashmap_entry ent;
68
	const char *key; /* the key; memory should be owned by value */
69
	size_t keylen; /* length of the key */
70
	void *value; /* the stored value */
71
};
72

73
/* attr_hashmap comparison function */
74
static int attr_hash_entry_cmp(const void *cmp_data UNUSED,
75
			       const struct hashmap_entry *eptr,
76
			       const struct hashmap_entry *entry_or_key,
77
			       const void *keydata UNUSED)
78
{
79
	const struct attr_hash_entry *a, *b;
80

81
	a = container_of(eptr, const struct attr_hash_entry, ent);
82
	b = container_of(entry_or_key, const struct attr_hash_entry, ent);
83
	return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
84
}
85

86
/*
87
 * The global dictionary of all interned attributes.  This
88
 * is a singleton object which is shared between threads.
89
 * Access to this dictionary must be surrounded with a mutex.
90
 */
91
static struct attr_hashmap g_attr_hashmap = {
92
	.map = HASHMAP_INIT(attr_hash_entry_cmp, NULL),
93
};
94

95
/*
96
 * Retrieve the 'value' stored in a hashmap given the provided 'key'.
97
 * If there is no matching entry, return NULL.
98
 */
99
static void *attr_hashmap_get(struct attr_hashmap *map,
100
			      const char *key, size_t keylen)
101
{
102
	struct attr_hash_entry k;
103
	struct attr_hash_entry *e;
104

105
	hashmap_entry_init(&k.ent, memhash(key, keylen));
106
	k.key = key;
107
	k.keylen = keylen;
108
	e = hashmap_get_entry(&map->map, &k, ent, NULL);
109

110
	return e ? e->value : NULL;
111
}
112

113
/* Add 'value' to a hashmap based on the provided 'key'. */
114
static void attr_hashmap_add(struct attr_hashmap *map,
115
			     const char *key, size_t keylen,
116
			     void *value)
117
{
118
	struct attr_hash_entry *e;
119

120
	e = xmalloc(sizeof(struct attr_hash_entry));
121
	hashmap_entry_init(&e->ent, memhash(key, keylen));
122
	e->key = key;
123
	e->keylen = keylen;
124
	e->value = value;
125

126
	hashmap_add(&map->map, &e->ent);
127
}
128

129
struct all_attrs_item {
130
	const struct git_attr *attr;
131
	const char *value;
132
	/*
133
	 * If 'macro' is non-NULL, indicates that 'attr' is a macro based on
134
	 * the current attribute stack and contains a pointer to the match_attr
135
	 * definition of the macro
136
	 */
137
	const struct match_attr *macro;
138
};
139

140
/*
141
 * Reallocate and reinitialize the array of all attributes (which is used in
142
 * the attribute collection process) in 'check' based on the global dictionary
143
 * of attributes.
144
 */
145
static void all_attrs_init(struct attr_hashmap *map, struct attr_check *check)
146
{
147
	int i;
148
	unsigned int size;
149

150
	hashmap_lock(map);
151

152
	size = hashmap_get_size(&map->map);
153
	if (size < check->all_attrs_nr)
154
		BUG("interned attributes shouldn't be deleted");
155

156
	/*
157
	 * If the number of attributes in the global dictionary has increased
158
	 * (or this attr_check instance doesn't have an initialized all_attrs
159
	 * field), reallocate the provided attr_check instance's all_attrs
160
	 * field and fill each entry with its corresponding git_attr.
161
	 */
162
	if (size != check->all_attrs_nr) {
163
		struct attr_hash_entry *e;
164
		struct hashmap_iter iter;
165

166
		REALLOC_ARRAY(check->all_attrs, size);
167
		check->all_attrs_nr = size;
168

169
		hashmap_for_each_entry(&map->map, &iter, e,
170
					ent /* member name */) {
171
			const struct git_attr *a = e->value;
172
			check->all_attrs[a->attr_nr].attr = a;
173
		}
174
	}
175

176
	hashmap_unlock(map);
177

178
	/*
179
	 * Re-initialize every entry in check->all_attrs.
180
	 * This re-initialization can live outside of the locked region since
181
	 * the attribute dictionary is no longer being accessed.
182
	 */
183
	for (i = 0; i < check->all_attrs_nr; i++) {
184
		check->all_attrs[i].value = ATTR__UNKNOWN;
185
		check->all_attrs[i].macro = NULL;
186
	}
187
}
188

189
/*
190
 * Atribute name cannot begin with "builtin_" which
191
 * is a reserved namespace for built in attributes values.
192
 */
193
static int attr_name_reserved(const char *name)
194
{
195
	return starts_with(name, "builtin_");
196
}
197

198
static int attr_name_valid(const char *name, size_t namelen)
199
{
200
	/*
201
	 * Attribute name cannot begin with '-' and must consist of
202
	 * characters from [-A-Za-z0-9_.].
203
	 */
204
	if (namelen <= 0 || *name == '-')
205
		return 0;
206
	while (namelen--) {
207
		char ch = *name++;
208
		if (! (ch == '-' || ch == '.' || ch == '_' ||
209
		       ('0' <= ch && ch <= '9') ||
210
		       ('a' <= ch && ch <= 'z') ||
211
		       ('A' <= ch && ch <= 'Z')) )
212
			return 0;
213
	}
214
	return 1;
215
}
216

217
static void report_invalid_attr(const char *name, size_t len,
218
				const char *src, int lineno)
219
{
220
	struct strbuf err = STRBUF_INIT;
221
	strbuf_addf(&err, _("%.*s is not a valid attribute name"),
222
		    (int) len, name);
223
	fprintf(stderr, "%s: %s:%d\n", err.buf, src, lineno);
224
	strbuf_release(&err);
225
}
226

227
/*
228
 * Given a 'name', lookup and return the corresponding attribute in the global
229
 * dictionary.  If no entry is found, create a new attribute and store it in
230
 * the dictionary.
231
 */
232
static const struct git_attr *git_attr_internal(const char *name, size_t namelen)
233
{
234
	struct git_attr *a;
235

236
	if (!attr_name_valid(name, namelen))
237
		return NULL;
238

239
	hashmap_lock(&g_attr_hashmap);
240

241
	a = attr_hashmap_get(&g_attr_hashmap, name, namelen);
242

243
	if (!a) {
244
		FLEX_ALLOC_MEM(a, name, name, namelen);
245
		a->attr_nr = hashmap_get_size(&g_attr_hashmap.map);
246

247
		attr_hashmap_add(&g_attr_hashmap, a->name, namelen, a);
248
		if (a->attr_nr != hashmap_get_size(&g_attr_hashmap.map) - 1)
249
			die(_("unable to add additional attribute"));
250
	}
251

252
	hashmap_unlock(&g_attr_hashmap);
253

254
	return a;
255
}
256

257
const struct git_attr *git_attr(const char *name)
258
{
259
	return git_attr_internal(name, strlen(name));
260
}
261

262
/* What does a matched pattern decide? */
263
struct attr_state {
264
	const struct git_attr *attr;
265
	const char *setto;
266
};
267

268
struct pattern {
269
	const char *pattern;
270
	int patternlen;
271
	int nowildcardlen;
272
	unsigned flags;		/* PATTERN_FLAG_* */
273
};
274

275
/*
276
 * One rule, as from a .gitattributes file.
277
 *
278
 * If is_macro is true, then u.attr is a pointer to the git_attr being
279
 * defined.
280
 *
281
 * If is_macro is false, then u.pat is the filename pattern to which the
282
 * rule applies.
283
 *
284
 * In either case, num_attr is the number of attributes affected by
285
 * this rule, and state is an array listing them.  The attributes are
286
 * listed as they appear in the file (macros unexpanded).
287
 */
288
struct match_attr {
289
	union {
290
		struct pattern pat;
291
		const struct git_attr *attr;
292
	} u;
293
	char is_macro;
294
	size_t num_attr;
295
	struct attr_state state[FLEX_ARRAY];
296
};
297

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

300
/* Flags usable in read_attr() and parse_attr_line() family of functions. */
301
#define READ_ATTR_MACRO_OK (1<<0)
302
#define READ_ATTR_NOFOLLOW (1<<1)
303

304
/*
305
 * Parse a whitespace-delimited attribute state (i.e., "attr",
306
 * "-attr", "!attr", or "attr=value") from the string starting at src.
307
 * If e is not NULL, write the results to *e.  Return a pointer to the
308
 * remainder of the string (with leading whitespace removed), or NULL
309
 * if there was an error.
310
 */
311
static const char *parse_attr(const char *src, int lineno, const char *cp,
312
			      struct attr_state *e)
313
{
314
	const char *ep, *equals;
315
	size_t len;
316

317
	ep = cp + strcspn(cp, blank);
318
	equals = strchr(cp, '=');
319
	if (equals && ep < equals)
320
		equals = NULL;
321
	if (equals)
322
		len = equals - cp;
323
	else
324
		len = ep - cp;
325
	if (!e) {
326
		if (*cp == '-' || *cp == '!') {
327
			cp++;
328
			len--;
329
		}
330
		if (!attr_name_valid(cp, len) || attr_name_reserved(cp)) {
331
			report_invalid_attr(cp, len, src, lineno);
332
			return NULL;
333
		}
334
	} else {
335
		/*
336
		 * As this function is always called twice, once with
337
		 * e == NULL in the first pass and then e != NULL in
338
		 * the second pass, no need for attr_name_valid()
339
		 * check here.
340
		 */
341
		if (*cp == '-' || *cp == '!') {
342
			e->setto = (*cp == '-') ? ATTR__FALSE : ATTR__UNSET;
343
			cp++;
344
			len--;
345
		}
346
		else if (!equals)
347
			e->setto = ATTR__TRUE;
348
		else {
349
			e->setto = xmemdupz(equals + 1, ep - equals - 1);
350
		}
351
		e->attr = git_attr_internal(cp, len);
352
	}
353
	return ep + strspn(ep, blank);
354
}
355

356
static struct match_attr *parse_attr_line(const char *line, const char *src,
357
					  int lineno, unsigned flags)
358
{
359
	size_t namelen, num_attr, i;
360
	const char *cp, *name, *states;
361
	struct match_attr *res = NULL;
362
	int is_macro;
363
	struct strbuf pattern = STRBUF_INIT;
364

365
	cp = line + strspn(line, blank);
366
	if (!*cp || *cp == '#')
367
		return NULL;
368
	name = cp;
369

370
	if (strlen(line) >= ATTR_MAX_LINE_LENGTH) {
371
		warning(_("ignoring overly long attributes line %d"), lineno);
372
		return NULL;
373
	}
374

375
	if (*cp == '"' && !unquote_c_style(&pattern, name, &states)) {
376
		name = pattern.buf;
377
		namelen = pattern.len;
378
	} else {
379
		namelen = strcspn(name, blank);
380
		states = name + namelen;
381
	}
382

383
	if (strlen(ATTRIBUTE_MACRO_PREFIX) < namelen &&
384
	    starts_with(name, ATTRIBUTE_MACRO_PREFIX)) {
385
		if (!(flags & READ_ATTR_MACRO_OK)) {
386
			fprintf_ln(stderr, _("%s not allowed: %s:%d"),
387
				   name, src, lineno);
388
			goto fail_return;
389
		}
390
		is_macro = 1;
391
		name += strlen(ATTRIBUTE_MACRO_PREFIX);
392
		name += strspn(name, blank);
393
		namelen = strcspn(name, blank);
394
		if (!attr_name_valid(name, namelen) || attr_name_reserved(name)) {
395
			report_invalid_attr(name, namelen, src, lineno);
396
			goto fail_return;
397
		}
398
	}
399
	else
400
		is_macro = 0;
401

402
	states += strspn(states, blank);
403

404
	/* First pass to count the attr_states */
405
	for (cp = states, num_attr = 0; *cp; num_attr++) {
406
		cp = parse_attr(src, lineno, cp, NULL);
407
		if (!cp)
408
			goto fail_return;
409
	}
410

411
	res = xcalloc(1, st_add3(sizeof(*res),
412
				 st_mult(sizeof(struct attr_state), num_attr),
413
				 is_macro ? 0 : namelen + 1));
414
	if (is_macro) {
415
		res->u.attr = git_attr_internal(name, namelen);
416
	} else {
417
		char *p = (char *)&(res->state[num_attr]);
418
		memcpy(p, name, namelen);
419
		res->u.pat.pattern = p;
420
		parse_path_pattern(&res->u.pat.pattern,
421
				      &res->u.pat.patternlen,
422
				      &res->u.pat.flags,
423
				      &res->u.pat.nowildcardlen);
424
		if (res->u.pat.flags & PATTERN_FLAG_NEGATIVE) {
425
			warning(_("Negative patterns are ignored in git attributes\n"
426
				  "Use '\\!' for literal leading exclamation."));
427
			goto fail_return;
428
		}
429
	}
430
	res->is_macro = is_macro;
431
	res->num_attr = num_attr;
432

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

438
	strbuf_release(&pattern);
439
	return res;
440

441
fail_return:
442
	strbuf_release(&pattern);
443
	free(res);
444
	return NULL;
445
}
446

447
/*
448
 * Like info/exclude and .gitignore, the attribute information can
449
 * come from many places.
450
 *
451
 * (1) .gitattributes file of the same directory;
452
 * (2) .gitattributes file of the parent directory if (1) does not have
453
 *      any match; this goes recursively upwards, just like .gitignore.
454
 * (3) $GIT_DIR/info/attributes, which overrides both of the above.
455
 *
456
 * In the same file, later entries override the earlier match, so in the
457
 * global list, we would have entries from info/attributes the earliest
458
 * (reading the file from top to bottom), .gitattributes of the root
459
 * directory (again, reading the file from top to bottom) down to the
460
 * current directory, and then scan the list backwards to find the first match.
461
 * This is exactly the same as what is_excluded() does in dir.c to deal with
462
 * .gitignore file and info/excludes file as a fallback.
463
 */
464

465
struct attr_stack {
466
	struct attr_stack *prev;
467
	char *origin;
468
	size_t originlen;
469
	unsigned num_matches;
470
	unsigned alloc;
471
	struct match_attr **attrs;
472
};
473

474
static void attr_stack_free(struct attr_stack *e)
475
{
476
	unsigned i;
477
	free(e->origin);
478
	for (i = 0; i < e->num_matches; i++) {
479
		struct match_attr *a = e->attrs[i];
480
		size_t j;
481

482
		for (j = 0; j < a->num_attr; j++) {
483
			const char *setto = a->state[j].setto;
484
			if (setto == ATTR__TRUE ||
485
			    setto == ATTR__FALSE ||
486
			    setto == ATTR__UNSET ||
487
			    setto == ATTR__UNKNOWN)
488
				;
489
			else
490
				free((char *) setto);
491
		}
492
		free(a);
493
	}
494
	free(e->attrs);
495
	free(e);
496
}
497

498
static void drop_attr_stack(struct attr_stack **stack)
499
{
500
	while (*stack) {
501
		struct attr_stack *elem = *stack;
502
		*stack = elem->prev;
503
		attr_stack_free(elem);
504
	}
505
}
506

507
/* List of all attr_check structs; access should be surrounded by mutex */
508
static struct check_vector {
509
	size_t nr;
510
	size_t alloc;
511
	struct attr_check **checks;
512
	pthread_mutex_t mutex;
513
} check_vector;
514

515
static inline void vector_lock(void)
516
{
517
	pthread_mutex_lock(&check_vector.mutex);
518
}
519

520
static inline void vector_unlock(void)
521
{
522
	pthread_mutex_unlock(&check_vector.mutex);
523
}
524

525
static void check_vector_add(struct attr_check *c)
526
{
527
	vector_lock();
528

529
	ALLOC_GROW(check_vector.checks,
530
		   check_vector.nr + 1,
531
		   check_vector.alloc);
532
	check_vector.checks[check_vector.nr++] = c;
533

534
	vector_unlock();
535
}
536

537
static void check_vector_remove(struct attr_check *check)
538
{
539
	int i;
540

541
	vector_lock();
542

543
	/* Find entry */
544
	for (i = 0; i < check_vector.nr; i++)
545
		if (check_vector.checks[i] == check)
546
			break;
547

548
	if (i >= check_vector.nr)
549
		BUG("no entry found");
550

551
	/* shift entries over */
552
	for (; i < check_vector.nr - 1; i++)
553
		check_vector.checks[i] = check_vector.checks[i + 1];
554

555
	check_vector.nr--;
556

557
	vector_unlock();
558
}
559

560
/* Iterate through all attr_check instances and drop their stacks */
561
static void drop_all_attr_stacks(void)
562
{
563
	int i;
564

565
	vector_lock();
566

567
	for (i = 0; i < check_vector.nr; i++) {
568
		drop_attr_stack(&check_vector.checks[i]->stack);
569
	}
570

571
	vector_unlock();
572
}
573

574
struct attr_check *attr_check_alloc(void)
575
{
576
	struct attr_check *c = xcalloc(1, sizeof(struct attr_check));
577

578
	/* save pointer to the check struct */
579
	check_vector_add(c);
580

581
	return c;
582
}
583

584
struct attr_check *attr_check_initl(const char *one, ...)
585
{
586
	struct attr_check *check;
587
	int cnt;
588
	va_list params;
589
	const char *param;
590

591
	va_start(params, one);
592
	for (cnt = 1; (param = va_arg(params, const char *)) != NULL; cnt++)
593
		;
594
	va_end(params);
595

596
	check = attr_check_alloc();
597
	check->nr = cnt;
598
	check->alloc = cnt;
599
	CALLOC_ARRAY(check->items, cnt);
600

601
	check->items[0].attr = git_attr(one);
602
	va_start(params, one);
603
	for (cnt = 1; cnt < check->nr; cnt++) {
604
		const struct git_attr *attr;
605
		param = va_arg(params, const char *);
606
		if (!param)
607
			BUG("counted %d != ended at %d",
608
			    check->nr, cnt);
609
		attr = git_attr(param);
610
		if (!attr)
611
			BUG("%s: not a valid attribute name", param);
612
		check->items[cnt].attr = attr;
613
	}
614
	va_end(params);
615
	return check;
616
}
617

618
struct attr_check *attr_check_dup(const struct attr_check *check)
619
{
620
	struct attr_check *ret;
621

622
	if (!check)
623
		return NULL;
624

625
	ret = attr_check_alloc();
626

627
	ret->nr = check->nr;
628
	ret->alloc = check->alloc;
629
	DUP_ARRAY(ret->items, check->items, ret->nr);
630

631
	return ret;
632
}
633

634
struct attr_check_item *attr_check_append(struct attr_check *check,
635
					  const struct git_attr *attr)
636
{
637
	struct attr_check_item *item;
638

639
	ALLOC_GROW(check->items, check->nr + 1, check->alloc);
640
	item = &check->items[check->nr++];
641
	item->attr = attr;
642
	return item;
643
}
644

645
void attr_check_reset(struct attr_check *check)
646
{
647
	check->nr = 0;
648
}
649

650
void attr_check_clear(struct attr_check *check)
651
{
652
	FREE_AND_NULL(check->items);
653
	check->alloc = 0;
654
	check->nr = 0;
655

656
	FREE_AND_NULL(check->all_attrs);
657
	check->all_attrs_nr = 0;
658

659
	drop_attr_stack(&check->stack);
660
}
661

662
void attr_check_free(struct attr_check *check)
663
{
664
	if (check) {
665
		/* Remove check from the check vector */
666
		check_vector_remove(check);
667

668
		attr_check_clear(check);
669
		free(check);
670
	}
671
}
672

673
static const char *builtin_attr[] = {
674
	"[attr]binary -diff -merge -text",
675
	NULL,
676
};
677

678
static void handle_attr_line(struct attr_stack *res,
679
			     const char *line,
680
			     const char *src,
681
			     int lineno,
682
			     unsigned flags)
683
{
684
	struct match_attr *a;
685

686
	a = parse_attr_line(line, src, lineno, flags);
687
	if (!a)
688
		return;
689
	ALLOC_GROW_BY(res->attrs, res->num_matches, 1, res->alloc);
690
	res->attrs[res->num_matches - 1] = a;
691
}
692

693
static struct attr_stack *read_attr_from_array(const char **list)
694
{
695
	struct attr_stack *res;
696
	const char *line;
697
	int lineno = 0;
698

699
	CALLOC_ARRAY(res, 1);
700
	while ((line = *(list++)) != NULL)
701
		handle_attr_line(res, line, "[builtin]", ++lineno,
702
				 READ_ATTR_MACRO_OK);
703
	return res;
704
}
705

706
/*
707
 * Callers into the attribute system assume there is a single, system-wide
708
 * global state where attributes are read from and when the state is flipped by
709
 * calling git_attr_set_direction(), the stack frames that have been
710
 * constructed need to be discarded so that subsequent calls into the
711
 * attribute system will lazily read from the right place.  Since changing
712
 * direction causes a global paradigm shift, it should not ever be called while
713
 * another thread could potentially be calling into the attribute system.
714
 */
715
static enum git_attr_direction direction;
716

717
void git_attr_set_direction(enum git_attr_direction new_direction)
718
{
719
	if (is_bare_repository() && new_direction != GIT_ATTR_INDEX)
720
		BUG("non-INDEX attr direction in a bare repo");
721

722
	if (new_direction != direction)
723
		drop_all_attr_stacks();
724

725
	direction = new_direction;
726
}
727

728
static struct attr_stack *read_attr_from_file(const char *path, unsigned flags)
729
{
730
	struct strbuf buf = STRBUF_INIT;
731
	int fd;
732
	FILE *fp;
733
	struct attr_stack *res;
734
	int lineno = 0;
735
	struct stat st;
736

737
	if (flags & READ_ATTR_NOFOLLOW)
738
		fd = open_nofollow(path, O_RDONLY);
739
	else
740
		fd = open(path, O_RDONLY);
741

742
	if (fd < 0) {
743
		warn_on_fopen_errors(path);
744
		return NULL;
745
	}
746
	fp = xfdopen(fd, "r");
747
	if (fstat(fd, &st)) {
748
		warning_errno(_("cannot fstat gitattributes file '%s'"), path);
749
		fclose(fp);
750
		return NULL;
751
	}
752
	if (st.st_size >= ATTR_MAX_FILE_SIZE) {
753
		warning(_("ignoring overly large gitattributes file '%s'"), path);
754
		fclose(fp);
755
		return NULL;
756
	}
757

758
	CALLOC_ARRAY(res, 1);
759
	while (strbuf_getline(&buf, fp) != EOF) {
760
		if (!lineno && starts_with(buf.buf, utf8_bom))
761
			strbuf_remove(&buf, 0, strlen(utf8_bom));
762
		handle_attr_line(res, buf.buf, path, ++lineno, flags);
763
	}
764

765
	fclose(fp);
766
	strbuf_release(&buf);
767
	return res;
768
}
769

770
static struct attr_stack *read_attr_from_buf(char *buf, size_t length,
771
					     const char *path, unsigned flags)
772
{
773
	struct attr_stack *res;
774
	char *sp;
775
	int lineno = 0;
776

777
	if (!buf)
778
		return NULL;
779
	if (length >= ATTR_MAX_FILE_SIZE) {
780
		warning(_("ignoring overly large gitattributes blob '%s'"), path);
781
		free(buf);
782
		return NULL;
783
	}
784

785
	CALLOC_ARRAY(res, 1);
786
	for (sp = buf; *sp;) {
787
		char *ep;
788
		int more;
789

790
		ep = strchrnul(sp, '\n');
791
		more = (*ep == '\n');
792
		*ep = '\0';
793
		handle_attr_line(res, sp, path, ++lineno, flags);
794
		sp = ep + more;
795
	}
796
	free(buf);
797

798
	return res;
799
}
800

801
static struct attr_stack *read_attr_from_blob(struct index_state *istate,
802
					      const struct object_id *tree_oid,
803
					      const char *path, unsigned flags)
804
{
805
	struct object_id oid;
806
	unsigned long sz;
807
	enum object_type type;
808
	void *buf;
809
	unsigned short mode;
810

811
	if (!tree_oid)
812
		return NULL;
813

814
	if (get_tree_entry(istate->repo, tree_oid, path, &oid, &mode))
815
		return NULL;
816

817
	buf = repo_read_object_file(istate->repo, &oid, &type, &sz);
818
	if (!buf || type != OBJ_BLOB) {
819
		free(buf);
820
		return NULL;
821
	}
822

823
	return read_attr_from_buf(buf, sz, path, flags);
824
}
825

826
static struct attr_stack *read_attr_from_index(struct index_state *istate,
827
					       const char *path, unsigned flags)
828
{
829
	struct attr_stack *stack = NULL;
830
	char *buf;
831
	unsigned long size;
832
	int sparse_dir_pos = -1;
833

834
	if (!istate)
835
		return NULL;
836

837
	/*
838
	 * When handling sparse-checkouts, .gitattributes files
839
	 * may reside within a sparse directory. We distinguish
840
	 * whether a path exists directly in the index or not by
841
	 * evaluating if 'pos' is negative.
842
	 * If 'pos' is negative, the path is not directly present
843
	 * in the index and is likely within a sparse directory.
844
	 * For paths not in the index, The absolute value of 'pos'
845
	 * minus 1 gives us the position where the path would be
846
	 * inserted in lexicographic order within the index.
847
	 * We then subtract another 1 from this value
848
	 * (sparse_dir_pos = -pos - 2) to find the position of the
849
	 * last index entry which is lexicographically smaller than
850
	 * the path. This would be the sparse directory containing
851
	 * the path. By identifying the sparse directory containing
852
	 * the path, we can correctly read the attributes specified
853
	 * in the .gitattributes file from the tree object of the
854
	 * sparse directory.
855
	 */
856
	if (!path_in_cone_mode_sparse_checkout(path, istate)) {
857
		int pos = index_name_pos_sparse(istate, path, strlen(path));
858

859
		if (pos < 0)
860
			sparse_dir_pos = -pos - 2;
861
	}
862

863
	if (sparse_dir_pos >= 0 &&
864
	    S_ISSPARSEDIR(istate->cache[sparse_dir_pos]->ce_mode) &&
865
	    !strncmp(istate->cache[sparse_dir_pos]->name, path, ce_namelen(istate->cache[sparse_dir_pos]))) {
866
		const char *relative_path = path + ce_namelen(istate->cache[sparse_dir_pos]);
867
		stack = read_attr_from_blob(istate, &istate->cache[sparse_dir_pos]->oid, relative_path, flags);
868
	} else {
869
		buf = read_blob_data_from_index(istate, path, &size);
870
		if (buf)
871
			stack = read_attr_from_buf(buf, size, path, flags);
872
	}
873
	return stack;
874
}
875

876
static struct attr_stack *read_attr(struct index_state *istate,
877
				    const struct object_id *tree_oid,
878
				    const char *path, unsigned flags)
879
{
880
	struct attr_stack *res = NULL;
881

882
	if (direction == GIT_ATTR_INDEX) {
883
		res = read_attr_from_index(istate, path, flags);
884
	} else if (tree_oid) {
885
		res = read_attr_from_blob(istate, tree_oid, path, flags);
886
	} else if (!is_bare_repository()) {
887
		if (direction == GIT_ATTR_CHECKOUT) {
888
			res = read_attr_from_index(istate, path, flags);
889
			if (!res)
890
				res = read_attr_from_file(path, flags);
891
		} else if (direction == GIT_ATTR_CHECKIN) {
892
			res = read_attr_from_file(path, flags);
893
			if (!res)
894
				/*
895
				 * There is no checked out .gitattributes file
896
				 * there, but we might have it in the index.
897
				 * We allow operation in a sparsely checked out
898
				 * work tree, so read from it.
899
				 */
900
				res = read_attr_from_index(istate, path, flags);
901
		}
902
	}
903

904
	if (!res)
905
		CALLOC_ARRAY(res, 1);
906
	return res;
907
}
908

909
const char *git_attr_system_file(void)
910
{
911
	static const char *system_wide;
912
	if (!system_wide)
913
		system_wide = system_path(ETC_GITATTRIBUTES);
914
	return system_wide;
915
}
916

917
const char *git_attr_global_file(void)
918
{
919
	if (!git_attributes_file)
920
		git_attributes_file = xdg_config_home("attributes");
921

922
	return git_attributes_file;
923
}
924

925
int git_attr_system_is_enabled(void)
926
{
927
	return !git_env_bool("GIT_ATTR_NOSYSTEM", 0);
928
}
929

930
static GIT_PATH_FUNC(git_path_info_attributes, INFOATTRIBUTES_FILE)
931

932
static void push_stack(struct attr_stack **attr_stack_p,
933
		       struct attr_stack *elem, char *origin, size_t originlen)
934
{
935
	if (elem) {
936
		elem->origin = origin;
937
		if (origin)
938
			elem->originlen = originlen;
939
		elem->prev = *attr_stack_p;
940
		*attr_stack_p = elem;
941
	}
942
}
943

944
static void bootstrap_attr_stack(struct index_state *istate,
945
				 const struct object_id *tree_oid,
946
				 struct attr_stack **stack)
947
{
948
	struct attr_stack *e;
949
	unsigned flags = READ_ATTR_MACRO_OK;
950

951
	if (*stack)
952
		return;
953

954
	/* builtin frame */
955
	e = read_attr_from_array(builtin_attr);
956
	push_stack(stack, e, NULL, 0);
957

958
	/* system-wide frame */
959
	if (git_attr_system_is_enabled()) {
960
		e = read_attr_from_file(git_attr_system_file(), flags);
961
		push_stack(stack, e, NULL, 0);
962
	}
963

964
	/* home directory */
965
	if (git_attr_global_file()) {
966
		e = read_attr_from_file(git_attr_global_file(), flags);
967
		push_stack(stack, e, NULL, 0);
968
	}
969

970
	/* root directory */
971
	e = read_attr(istate, tree_oid, GITATTRIBUTES_FILE, flags | READ_ATTR_NOFOLLOW);
972
	push_stack(stack, e, xstrdup(""), 0);
973

974
	/* info frame */
975
	if (startup_info->have_repository)
976
		e = read_attr_from_file(git_path_info_attributes(), flags);
977
	else
978
		e = NULL;
979
	if (!e)
980
		CALLOC_ARRAY(e, 1);
981
	push_stack(stack, e, NULL, 0);
982
}
983

984
static void prepare_attr_stack(struct index_state *istate,
985
			       const struct object_id *tree_oid,
986
			       const char *path, int dirlen,
987
			       struct attr_stack **stack)
988
{
989
	struct attr_stack *info;
990
	struct strbuf pathbuf = STRBUF_INIT;
991

992
	/*
993
	 * At the bottom of the attribute stack is the built-in
994
	 * set of attribute definitions, followed by the contents
995
	 * of $(prefix)/etc/gitattributes and a file specified by
996
	 * core.attributesfile.  Then, contents from
997
	 * .gitattributes files from directories closer to the
998
	 * root to the ones in deeper directories are pushed
999
	 * to the stack.  Finally, at the very top of the stack
1000
	 * we always keep the contents of $GIT_DIR/info/attributes.
1001
	 *
1002
	 * When checking, we use entries from near the top of the
1003
	 * stack, preferring $GIT_DIR/info/attributes, then
1004
	 * .gitattributes in deeper directories to shallower ones,
1005
	 * and finally use the built-in set as the default.
1006
	 */
1007
	bootstrap_attr_stack(istate, tree_oid, stack);
1008

1009
	/*
1010
	 * Pop the "info" one that is always at the top of the stack.
1011
	 */
1012
	info = *stack;
1013
	*stack = info->prev;
1014

1015
	/*
1016
	 * Pop the ones from directories that are not the prefix of
1017
	 * the path we are checking. Break out of the loop when we see
1018
	 * the root one (whose origin is an empty string "") or the builtin
1019
	 * one (whose origin is NULL) without popping it.
1020
	 */
1021
	while ((*stack)->origin) {
1022
		int namelen = (*stack)->originlen;
1023
		struct attr_stack *elem;
1024

1025
		elem = *stack;
1026
		if (namelen <= dirlen &&
1027
		    !strncmp(elem->origin, path, namelen) &&
1028
		    (!namelen || path[namelen] == '/'))
1029
			break;
1030

1031
		*stack = elem->prev;
1032
		attr_stack_free(elem);
1033
	}
1034

1035
	/*
1036
	 * bootstrap_attr_stack() should have added, and the
1037
	 * above loop should have stopped before popping, the
1038
	 * root element whose attr_stack->origin is set to an
1039
	 * empty string.
1040
	 */
1041
	assert((*stack)->origin);
1042

1043
	strbuf_addstr(&pathbuf, (*stack)->origin);
1044
	/* Build up to the directory 'path' is in */
1045
	while (pathbuf.len < dirlen) {
1046
		size_t len = pathbuf.len;
1047
		struct attr_stack *next;
1048
		char *origin;
1049

1050
		/* Skip path-separator */
1051
		if (len < dirlen && is_dir_sep(path[len]))
1052
			len++;
1053
		/* Find the end of the next component */
1054
		while (len < dirlen && !is_dir_sep(path[len]))
1055
			len++;
1056

1057
		if (pathbuf.len > 0)
1058
			strbuf_addch(&pathbuf, '/');
1059
		strbuf_add(&pathbuf, path + pathbuf.len, (len - pathbuf.len));
1060
		strbuf_addf(&pathbuf, "/%s", GITATTRIBUTES_FILE);
1061

1062
		next = read_attr(istate, tree_oid, pathbuf.buf, READ_ATTR_NOFOLLOW);
1063

1064
		/* reset the pathbuf to not include "/.gitattributes" */
1065
		strbuf_setlen(&pathbuf, len);
1066

1067
		origin = xstrdup(pathbuf.buf);
1068
		push_stack(stack, next, origin, len);
1069
	}
1070

1071
	/*
1072
	 * Finally push the "info" one at the top of the stack.
1073
	 */
1074
	push_stack(stack, info, NULL, 0);
1075

1076
	strbuf_release(&pathbuf);
1077
}
1078

1079
static int path_matches(const char *pathname, int pathlen,
1080
			int basename_offset,
1081
			const struct pattern *pat,
1082
			const char *base, int baselen)
1083
{
1084
	const char *pattern = pat->pattern;
1085
	int prefix = pat->nowildcardlen;
1086
	int isdir = (pathlen && pathname[pathlen - 1] == '/');
1087

1088
	if ((pat->flags & PATTERN_FLAG_MUSTBEDIR) && !isdir)
1089
		return 0;
1090

1091
	if (pat->flags & PATTERN_FLAG_NODIR) {
1092
		return match_basename(pathname + basename_offset,
1093
				      pathlen - basename_offset - isdir,
1094
				      pattern, prefix,
1095
				      pat->patternlen, pat->flags);
1096
	}
1097
	return match_pathname(pathname, pathlen - isdir,
1098
			      base, baselen,
1099
			      pattern, prefix, pat->patternlen);
1100
}
1101

1102
static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem);
1103

1104
static int fill_one(struct all_attrs_item *all_attrs,
1105
		    const struct match_attr *a, int rem)
1106
{
1107
	size_t i;
1108

1109
	for (i = a->num_attr; rem > 0 && i > 0; i--) {
1110
		const struct git_attr *attr = a->state[i - 1].attr;
1111
		const char **n = &(all_attrs[attr->attr_nr].value);
1112
		const char *v = a->state[i - 1].setto;
1113

1114
		if (*n == ATTR__UNKNOWN) {
1115
			*n = v;
1116
			rem--;
1117
			rem = macroexpand_one(all_attrs, attr->attr_nr, rem);
1118
		}
1119
	}
1120
	return rem;
1121
}
1122

1123
static int fill(const char *path, int pathlen, int basename_offset,
1124
		const struct attr_stack *stack,
1125
		struct all_attrs_item *all_attrs, int rem)
1126
{
1127
	for (; rem > 0 && stack; stack = stack->prev) {
1128
		unsigned i;
1129
		const char *base = stack->origin ? stack->origin : "";
1130

1131
		for (i = stack->num_matches; 0 < rem && 0 < i; i--) {
1132
			const struct match_attr *a = stack->attrs[i - 1];
1133
			if (a->is_macro)
1134
				continue;
1135
			if (path_matches(path, pathlen, basename_offset,
1136
					 &a->u.pat, base, stack->originlen))
1137
				rem = fill_one(all_attrs, a, rem);
1138
		}
1139
	}
1140

1141
	return rem;
1142
}
1143

1144
static int macroexpand_one(struct all_attrs_item *all_attrs, int nr, int rem)
1145
{
1146
	const struct all_attrs_item *item = &all_attrs[nr];
1147

1148
	if (item->macro && item->value == ATTR__TRUE)
1149
		return fill_one(all_attrs, item->macro, rem);
1150
	else
1151
		return rem;
1152
}
1153

1154
/*
1155
 * Marks the attributes which are macros based on the attribute stack.
1156
 * This prevents having to search through the attribute stack each time
1157
 * a macro needs to be expanded during the fill stage.
1158
 */
1159
static void determine_macros(struct all_attrs_item *all_attrs,
1160
			     const struct attr_stack *stack)
1161
{
1162
	for (; stack; stack = stack->prev) {
1163
		unsigned i;
1164
		for (i = stack->num_matches; i > 0; i--) {
1165
			const struct match_attr *ma = stack->attrs[i - 1];
1166
			if (ma->is_macro) {
1167
				unsigned int n = ma->u.attr->attr_nr;
1168
				if (!all_attrs[n].macro) {
1169
					all_attrs[n].macro = ma;
1170
				}
1171
			}
1172
		}
1173
	}
1174
}
1175

1176
/*
1177
 * Collect attributes for path into the array pointed to by check->all_attrs.
1178
 * If check->check_nr is non-zero, only attributes in check[] are collected.
1179
 * Otherwise all attributes are collected.
1180
 */
1181
static void collect_some_attrs(struct index_state *istate,
1182
			       const struct object_id *tree_oid,
1183
			       const char *path, struct attr_check *check)
1184
{
1185
	int pathlen, rem, dirlen;
1186
	const char *cp, *last_slash = NULL;
1187
	int basename_offset;
1188

1189
	for (cp = path; *cp; cp++) {
1190
		if (*cp == '/' && cp[1])
1191
			last_slash = cp;
1192
	}
1193
	pathlen = cp - path;
1194
	if (last_slash) {
1195
		basename_offset = last_slash + 1 - path;
1196
		dirlen = last_slash - path;
1197
	} else {
1198
		basename_offset = 0;
1199
		dirlen = 0;
1200
	}
1201

1202
	prepare_attr_stack(istate, tree_oid, path, dirlen, &check->stack);
1203
	all_attrs_init(&g_attr_hashmap, check);
1204
	determine_macros(check->all_attrs, check->stack);
1205

1206
	rem = check->all_attrs_nr;
1207
	fill(path, pathlen, basename_offset, check->stack, check->all_attrs, rem);
1208
}
1209

1210
static const char *default_attr_source_tree_object_name;
1211

1212
void set_git_attr_source(const char *tree_object_name)
1213
{
1214
	default_attr_source_tree_object_name = xstrdup(tree_object_name);
1215
}
1216

1217
static int compute_default_attr_source(struct object_id *attr_source)
1218
{
1219
	int ignore_bad_attr_tree = 0;
1220

1221
	if (!default_attr_source_tree_object_name)
1222
		default_attr_source_tree_object_name = getenv(GIT_ATTR_SOURCE_ENVIRONMENT);
1223

1224
	if (!default_attr_source_tree_object_name && git_attr_tree) {
1225
		default_attr_source_tree_object_name = git_attr_tree;
1226
		ignore_bad_attr_tree = 1;
1227
	}
1228

1229
	if (!default_attr_source_tree_object_name)
1230
		return 0;
1231

1232
	if (!startup_info->have_repository) {
1233
		if (!ignore_bad_attr_tree)
1234
			die(_("cannot use --attr-source or GIT_ATTR_SOURCE without repo"));
1235
		return 0;
1236
	}
1237

1238
	if (repo_get_oid_treeish(the_repository,
1239
				 default_attr_source_tree_object_name,
1240
				 attr_source)) {
1241
		if (!ignore_bad_attr_tree)
1242
			die(_("bad --attr-source or GIT_ATTR_SOURCE"));
1243
		return 0;
1244
	}
1245

1246
	return 1;
1247
}
1248

1249
static struct object_id *default_attr_source(void)
1250
{
1251
	static struct object_id attr_source;
1252
	static int has_attr_source = -1;
1253

1254
	if (has_attr_source < 0)
1255
		has_attr_source = compute_default_attr_source(&attr_source);
1256
	if (!has_attr_source)
1257
		return NULL;
1258
	return &attr_source;
1259
}
1260

1261
static const char *interned_mode_string(unsigned int mode)
1262
{
1263
	static struct {
1264
		unsigned int val;
1265
		char str[7];
1266
	} mode_string[] = {
1267
		{ .val = 0040000 },
1268
		{ .val = 0100644 },
1269
		{ .val = 0100755 },
1270
		{ .val = 0120000 },
1271
		{ .val = 0160000 },
1272
	};
1273
	int i;
1274

1275
	for (i = 0; i < ARRAY_SIZE(mode_string); i++) {
1276
		if (mode_string[i].val != mode)
1277
			continue;
1278
		if (!*mode_string[i].str)
1279
			snprintf(mode_string[i].str, sizeof(mode_string[i].str),
1280
				 "%06o", mode);
1281
		return mode_string[i].str;
1282
	}
1283
	BUG("Unsupported mode 0%o", mode);
1284
}
1285

1286
static const char *builtin_object_mode_attr(struct index_state *istate, const char *path)
1287
{
1288
	unsigned int mode;
1289

1290
	if (direction == GIT_ATTR_CHECKIN) {
1291
		struct object_id oid;
1292
		struct stat st;
1293
		if (lstat(path, &st))
1294
			die_errno(_("unable to stat '%s'"), path);
1295
		mode = canon_mode(st.st_mode);
1296
		if (S_ISDIR(mode)) {
1297
			/*
1298
			 *`path` is either a directory or it is a submodule,
1299
			 * in which case it is already indexed as submodule
1300
			 * or it does not exist in the index yet and we need to
1301
			 * check if we can resolve to a ref.
1302
			*/
1303
			int pos = index_name_pos(istate, path, strlen(path));
1304
			if (pos >= 0) {
1305
				 if (S_ISGITLINK(istate->cache[pos]->ce_mode))
1306
					 mode = istate->cache[pos]->ce_mode;
1307
			} else if (repo_resolve_gitlink_ref(the_repository, path,
1308
							    "HEAD", &oid) == 0) {
1309
				mode = S_IFGITLINK;
1310
			}
1311
		}
1312
	} else {
1313
		/*
1314
		 * For GIT_ATTR_CHECKOUT and GIT_ATTR_INDEX we only check
1315
		 * for mode in the index.
1316
		 */
1317
		int pos = index_name_pos(istate, path, strlen(path));
1318
		if (pos >= 0)
1319
			mode = istate->cache[pos]->ce_mode;
1320
		else
1321
			return ATTR__UNSET;
1322
	}
1323

1324
	return interned_mode_string(mode);
1325
}
1326

1327

1328
static const char *compute_builtin_attr(struct index_state *istate,
1329
					  const char *path,
1330
					  const struct git_attr *attr) {
1331
	static const struct git_attr *object_mode_attr;
1332

1333
	if (!object_mode_attr)
1334
		object_mode_attr = git_attr("builtin_objectmode");
1335

1336
	if (attr == object_mode_attr)
1337
		return builtin_object_mode_attr(istate, path);
1338
	return ATTR__UNSET;
1339
}
1340

1341
void git_check_attr(struct index_state *istate,
1342
		    const char *path,
1343
		    struct attr_check *check)
1344
{
1345
	int i;
1346
	const struct object_id *tree_oid = default_attr_source();
1347

1348
	collect_some_attrs(istate, tree_oid, path, check);
1349

1350
	for (i = 0; i < check->nr; i++) {
1351
		unsigned int n = check->items[i].attr->attr_nr;
1352
		const char *value = check->all_attrs[n].value;
1353
		if (value == ATTR__UNKNOWN)
1354
			value = compute_builtin_attr(istate, path, check->all_attrs[n].attr);
1355
		check->items[i].value = value;
1356
	}
1357
}
1358

1359
void git_all_attrs(struct index_state *istate,
1360
		   const char *path, struct attr_check *check)
1361
{
1362
	int i;
1363
	const struct object_id *tree_oid = default_attr_source();
1364

1365
	attr_check_reset(check);
1366
	collect_some_attrs(istate, tree_oid, path, check);
1367

1368
	for (i = 0; i < check->all_attrs_nr; i++) {
1369
		const char *name = check->all_attrs[i].attr->name;
1370
		const char *value = check->all_attrs[i].value;
1371
		struct attr_check_item *item;
1372
		if (value == ATTR__UNSET || value == ATTR__UNKNOWN)
1373
			continue;
1374
		item = attr_check_append(check, git_attr(name));
1375
		item->value = value;
1376
	}
1377
}
1378

1379
void attr_start(void)
1380
{
1381
	pthread_mutex_init(&g_attr_hashmap.mutex, NULL);
1382
	pthread_mutex_init(&check_vector.mutex, NULL);
1383
}
1384

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.