attr.c 16.3 KB
Newer Older
1 2 3 4 5 6 7
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

8 9
#include "attr.h"

10
#include "repository.h"
11
#include "sysdir.h"
12
#include "config.h"
13
#include "attr_file.h"
14
#include "ignore.h"
15
#include "git2/oid.h"
16 17
#include <ctype.h>

18 19 20 21
const char *git_attr__true  = "[internal]__TRUE__";
const char *git_attr__false = "[internal]__FALSE__";
const char *git_attr__unset = "[internal]__UNSET__";

22
git_attr_value_t git_attr_value(const char *attr)
23 24
{
	if (attr == NULL || attr == git_attr__unset)
25
		return GIT_ATTR_VALUE_UNSPECIFIED;
26 27

	if (attr == git_attr__true)
28
		return GIT_ATTR_VALUE_TRUE;
29 30

	if (attr == git_attr__false)
31
		return GIT_ATTR_VALUE_FALSE;
32

33
	return GIT_ATTR_VALUE_STRING;
34 35
}

36
static int collect_attr_files(
37
	git_repository *repo,
38
	git_attr_session *attr_session,
39
	git_attr_options *opts,
40 41
	const char *path,
	git_vector *files);
42

43
static void release_attr_files(git_vector *files);
44

45
int git_attr_get_ext(
46
	const char **value,
Linquize committed
47
	git_repository *repo,
48
	git_attr_options *opts,
49
	const char *pathname,
50
	const char *name)
51 52 53 54
{
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
55
	size_t i, j;
56 57 58
	git_attr_file *file;
	git_attr_name attr;
	git_attr_rule *rule;
59
	git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
60

Edward Thomson committed
61 62 63
	GIT_ASSERT_ARG(value);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(name);
64
	GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options");
Russell Belfer committed
65

66 67
	*value = NULL;

68 69 70
	if (git_repository_is_bare(repo))
		dir_flag = GIT_DIR_FLAG_FALSE;

71
	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
72 73
		return -1;

74
	if ((error = collect_attr_files(repo, NULL, opts, pathname, &files)) < 0)
75
		goto cleanup;
76

77
	memset(&attr, 0, sizeof(attr));
78 79 80 81 82 83
	attr.name = name;
	attr.name_hash = git_attr_file__name_hash(name);

	git_vector_foreach(&files, i, file) {

		git_attr_file__foreach_matching_rule(file, &path, j, rule) {
84 85 86
			size_t pos;

			if (!git_vector_bsearch(&pos, &rule->assigns, &attr)) {
87 88
				*value = ((git_attr_assignment *)git_vector_get(
							  &rule->assigns, pos))->value;
89
				goto cleanup;
90 91 92 93
			}
		}
	}

94
cleanup:
95
	release_attr_files(&files);
96
	git_attr_path__free(&path);
97 98 99 100

	return error;
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114
int git_attr_get(
	const char **value,
	git_repository *repo,
	uint32_t flags,
	const char *pathname,
	const char *name)
{
	git_attr_options opts = GIT_ATTR_OPTIONS_INIT;

	opts.flags = flags;

	return git_attr_get_ext(value, repo, &opts, pathname, name);
}

115 116 117 118 119 120

typedef struct {
	git_attr_name name;
	git_attr_assignment *found;
} attr_get_many_info;

121
int git_attr_get_many_with_session(
122
	const char **values,
Linquize committed
123
	git_repository *repo,
124
	git_attr_session *attr_session,
125
	git_attr_options *opts,
126
	const char *pathname,
Linquize committed
127
	size_t num_attr,
128
	const char **names)
129 130 131 132
{
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
133
	size_t i, j, k;
134 135 136 137
	git_attr_file *file;
	git_attr_rule *rule;
	attr_get_many_info *info = NULL;
	size_t num_found = 0;
138
	git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
139

Russell Belfer committed
140 141 142
	if (!num_attr)
		return 0;

Edward Thomson committed
143 144 145 146
	GIT_ASSERT_ARG(values);
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(pathname);
	GIT_ASSERT_ARG(names);
147
	GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options");
Russell Belfer committed
148

149 150 151
	if (git_repository_is_bare(repo))
		dir_flag = GIT_DIR_FLAG_FALSE;

152
	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
153 154
		return -1;

155
	if ((error = collect_attr_files(repo, attr_session, opts, pathname, &files)) < 0)
156
		goto cleanup;
157

158
	info = git__calloc(num_attr, sizeof(attr_get_many_info));
159
	GIT_ERROR_CHECK_ALLOC(info);
160 161 162 163 164 165

	git_vector_foreach(&files, i, file) {

		git_attr_file__foreach_matching_rule(file, &path, j, rule) {

			for (k = 0; k < num_attr; k++) {
166
				size_t pos;
167 168 169 170 171 172 173 174 175

				if (info[k].found != NULL) /* already found assignment */
					continue;

				if (!info[k].name.name) {
					info[k].name.name = names[k];
					info[k].name.name_hash = git_attr_file__name_hash(names[k]);
				}

176
				if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) {
177 178 179 180 181 182 183 184 185 186 187
					info[k].found = (git_attr_assignment *)
						git_vector_get(&rule->assigns, pos);
					values[k] = info[k].found->value;

					if (++num_found == num_attr)
						goto cleanup;
				}
			}
		}
	}

188 189 190 191 192
	for (k = 0; k < num_attr; k++) {
		if (!info[k].found)
			values[k] = NULL;
	}

193
cleanup:
194
	release_attr_files(&files);
195
	git_attr_path__free(&path);
196 197 198 199 200
	git__free(info);

	return error;
}

201 202 203 204 205 206 207 208
int git_attr_get_many(
	const char **values,
	git_repository *repo,
	uint32_t flags,
	const char *pathname,
	size_t num_attr,
	const char **names)
{
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
	git_attr_options opts = GIT_ATTR_OPTIONS_INIT;

	opts.flags = flags;

	return git_attr_get_many_with_session(
		values, repo, NULL, &opts, pathname, num_attr, names);
}

int git_attr_get_many_ext(
	const char **values,
	git_repository *repo,
	git_attr_options *opts,
	const char *pathname,
	size_t num_attr,
	const char **names)
{
225
	return git_attr_get_many_with_session(
226
		values, repo, NULL, opts, pathname, num_attr, names);
227
}
228 229

int git_attr_foreach(
Linquize committed
230
	git_repository *repo,
231 232
	uint32_t flags,
	const char *pathname,
233 234 235
	int (*callback)(const char *name, const char *value, void *payload),
	void *payload)
{
236 237 238 239 240 241 242 243 244 245 246 247 248 249
	git_attr_options opts = GIT_ATTR_OPTIONS_INIT;

	opts.flags = flags;

	return git_attr_foreach_ext(repo, &opts, pathname, callback, payload);
}

int git_attr_foreach_ext(
	git_repository *repo,
	git_attr_options *opts,
	const char *pathname,
	int (*callback)(const char *name, const char *value, void *payload),
	void *payload)
{
250 251 252
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
253
	size_t i, j, k;
254 255 256
	git_attr_file *file;
	git_attr_rule *rule;
	git_attr_assignment *assign;
257
	git_strmap *seen = NULL;
258
	git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
259

Edward Thomson committed
260 261
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(callback);
262
	GIT_ERROR_CHECK_VERSION(opts, GIT_ATTR_OPTIONS_VERSION, "git_attr_options");
Russell Belfer committed
263

264 265 266
	if (git_repository_is_bare(repo))
		dir_flag = GIT_DIR_FLAG_FALSE;

267
	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
268 269
		return -1;

270
	if ((error = collect_attr_files(repo, NULL, opts, pathname, &files)) < 0 ||
271
	    (error = git_strmap_new(&seen)) < 0)
272
		goto cleanup;
273 274 275 276 277 278 279

	git_vector_foreach(&files, i, file) {

		git_attr_file__foreach_matching_rule(file, &path, j, rule) {

			git_vector_foreach(&rule->assigns, k, assign) {
				/* skip if higher priority assignment was already seen */
280
				if (git_strmap_exists(seen, assign->name))
281 282
					continue;

283
				if ((error = git_strmap_set(seen, assign->name, assign)) < 0)
284
					goto cleanup;
285

286 287
				error = callback(assign->name, assign->value, payload);
				if (error) {
288
					git_error_set_after_callback(error);
289
					goto cleanup;
290
				}
291 292 293 294 295
			}
		}
	}

cleanup:
296
	git_strmap_free(seen);
297
	release_attr_files(&files);
298
	git_attr_path__free(&path);
299 300 301 302

	return error;
}

303
static int preload_attr_source(
304
	git_repository *repo,
305
	git_attr_session *attr_session,
306
	git_attr_file_source *source)
307 308 309 310
{
	int error;
	git_attr_file *preload = NULL;

311
	if (!source)
312
		return 0;
313 314 315 316 317

	error = git_attr_cache__get(&preload, repo, attr_session, source,
	                            git_attr_file__parse_buffer, true);

	if (!error)
318 319 320 321 322
		git_attr_file__free(preload);

	return error;
}

323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
GIT_INLINE(int) preload_attr_file(
	git_repository *repo,
	git_attr_session *attr_session,
	const char *base,
	const char *filename)
{
	git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE };

	if (!filename)
		return 0;

	source.base = base;
	source.filename = filename;

	return preload_attr_source(repo, attr_session, &source);
}

340
static int system_attr_file(
341
	git_str *out,
342 343 344 345 346 347 348 349
	git_attr_session *attr_session)
{
	int error;

	if (!attr_session) {
		error = git_sysdir_find_system_file(out, GIT_ATTR_FILE_SYSTEM);

		if (error == GIT_ENOTFOUND)
350
			git_error_clear();
351 352 353 354 355 356 357 358

		return error;
	}

	if (!attr_session->init_sysdir) {
		error = git_sysdir_find_system_file(&attr_session->sysdir, GIT_ATTR_FILE_SYSTEM);

		if (error == GIT_ENOTFOUND)
359
			git_error_clear();
360 361 362 363 364 365 366 367 368
		else if (error)
			return error;

		attr_session->init_sysdir = 1;
	}

	if (attr_session->sysdir.size == 0)
		return GIT_ENOTFOUND;

369 370 371
	/* We can safely provide a git_str with no allocation (asize == 0) to
	 * a consumer. This allows them to treat this as a regular `git_str`,
	 * but their call to `git_str_dispose` will not attempt to free it.
372
	 */
373
	git_str_attach_notowned(
374
		out, attr_session->sysdir.ptr, attr_session->sysdir.size);
375 376 377
	return 0;
}

378 379 380
static int attr_setup(
	git_repository *repo,
	git_attr_session *attr_session,
381
	git_attr_options *opts)
382
{
383
	git_str system = GIT_STR_INIT, info = GIT_STR_INIT;
384
	git_attr_file_source index_source = { GIT_ATTR_FILE_SOURCE_INDEX, NULL, GIT_ATTR_FILE, NULL };
385
	git_attr_file_source head_source = { GIT_ATTR_FILE_SOURCE_HEAD, NULL, GIT_ATTR_FILE, NULL };
386
	git_attr_file_source commit_source = { GIT_ATTR_FILE_SOURCE_COMMIT, NULL, GIT_ATTR_FILE, NULL };
387 388 389
	git_index *idx = NULL;
	const char *workdir;
	int error = 0;
390

391
	if (attr_session && attr_session->init_setup)
392 393
		return 0;

394 395 396
	if ((error = git_attr_cache__init(repo)) < 0)
		return error;

397 398 399
	/*
	 * Preload attribute files that could contain macros so the
	 * definitions will be available for later file parsing.
400 401
	 */

402 403
	if ((error = system_attr_file(&system, attr_session)) < 0 ||
	    (error = preload_attr_file(repo, attr_session, NULL, system.ptr)) < 0) {
404 405
		if (error != GIT_ENOTFOUND)
			goto out;
406 407

		error = 0;
408
	}
409

410 411
	if ((error = preload_attr_file(repo, attr_session, NULL,
	                               git_repository_attr_cache(repo)->cfg_attr_file)) < 0)
412
		goto out;
413

414
	if ((error = git_repository__item_path(&info, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
415
	    (error = preload_attr_file(repo, attr_session, info.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
416 417
		if (error != GIT_ENOTFOUND)
			goto out;
418 419

		error = 0;
420
	}
421

422
	if ((workdir = git_repository_workdir(repo)) != NULL &&
423
	    (error = preload_attr_file(repo, attr_session, workdir, GIT_ATTR_FILE)) < 0)
424
			goto out;
425 426

	if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
427
	    (error = preload_attr_source(repo, attr_session, &index_source)) < 0)
428
			goto out;
429

430
	if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0) &&
431
	    (error = preload_attr_source(repo, attr_session, &head_source)) < 0)
432 433
		goto out;

434
	if ((opts && (opts->flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0)) {
435 436 437 438 439 440
#ifndef GIT_DEPRECATE_HARD
		if (opts->commit_id)
			commit_source.commit_id = opts->commit_id;
		else
#endif
		commit_source.commit_id = &opts->attr_commit_id;
441 442 443 444 445

		if ((error = preload_attr_source(repo, attr_session, &commit_source)) < 0)
			goto out;
	}

446
	if (attr_session)
447
		attr_session->init_setup = 1;
448

449
out:
450 451
	git_str_dispose(&system);
	git_str_dispose(&info);
452

453 454 455
	return error;
}

456 457 458 459 460 461 462
int git_attr_add_macro(
	git_repository *repo,
	const char *name,
	const char *values)
{
	int error;
	git_attr_rule *macro = NULL;
463
	git_pool *pool;
464

Edward Thomson committed
465 466 467
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(name);

Russell Belfer committed
468
	if ((error = git_attr_cache__init(repo)) < 0)
469
		return error;
470 471

	macro = git__calloc(1, sizeof(git_attr_rule));
472
	GIT_ERROR_CHECK_ALLOC(macro);
473

474 475 476
	pool = &git_repository_attr_cache(repo)->pool;

	macro->match.pattern = git_pool_strdup(pool, name);
477
	GIT_ERROR_CHECK_ALLOC(macro->match.pattern);
478 479 480 481

	macro->match.length = strlen(macro->match.pattern);
	macro->match.flags = GIT_ATTR_FNMATCH_MACRO;

482
	error = git_attr_assignment__parse(repo, pool, &macro->assigns, &values);
483

484
	if (!error)
485 486
		error = git_attr_cache__insert_macro(repo, macro);

487
	if (error < 0)
488 489 490 491 492
		git_attr_rule__free(macro);

	return error;
}

493 494
typedef struct {
	git_repository *repo;
495
	git_attr_session *attr_session;
496
	git_attr_options *opts;
497 498
	const char *workdir;
	git_index *index;
499 500 501
	git_vector *files;
} attr_walk_up_info;

502
static int attr_decide_sources(
503 504 505 506
	uint32_t flags,
	bool has_wd,
	bool has_index,
	git_attr_file_source_t *srcs)
507 508 509 510 511 512
{
	int count = 0;

	switch (flags & 0x03) {
	case GIT_ATTR_CHECK_FILE_THEN_INDEX:
		if (has_wd)
513
			srcs[count++] = GIT_ATTR_FILE_SOURCE_FILE;
514
		if (has_index)
515
			srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX;
516 517 518
		break;
	case GIT_ATTR_CHECK_INDEX_THEN_FILE:
		if (has_index)
519
			srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX;
520
		if (has_wd)
521
			srcs[count++] = GIT_ATTR_FILE_SOURCE_FILE;
522 523 524
		break;
	case GIT_ATTR_CHECK_INDEX_ONLY:
		if (has_index)
525
			srcs[count++] = GIT_ATTR_FILE_SOURCE_INDEX;
526 527 528
		break;
	}

529 530 531 532
	if ((flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0)
		srcs[count++] = GIT_ATTR_FILE_SOURCE_HEAD;

	if ((flags & GIT_ATTR_CHECK_INCLUDE_COMMIT) != 0)
533
		srcs[count++] = GIT_ATTR_FILE_SOURCE_COMMIT;
534

535 536 537
	return count;
}

538
static int push_attr_source(
539
	git_repository *repo,
540
	git_attr_session *attr_session,
541
	git_vector *list,
542
	git_attr_file_source *source,
543
	bool allow_macros)
544 545 546 547
{
	int error = 0;
	git_attr_file *file = NULL;

548
	error = git_attr_cache__get(&file, repo, attr_session,
549
	                            source,
550 551
	                            git_attr_file__parse_buffer,
	                            allow_macros);
552

553 554 555 556 557 558 559
	if (error < 0)
		return error;

	if (file != NULL) {
		if ((error = git_vector_insert(list, file)) < 0)
			git_attr_file__free(file);
	}
560 561 562 563

	return error;
}

564 565 566 567 568 569 570 571 572 573 574
GIT_INLINE(int) push_attr_file(
	git_repository *repo,
	git_attr_session *attr_session,
	git_vector *list,
	const char *base,
	const char *filename)
{
	git_attr_file_source source = { GIT_ATTR_FILE_SOURCE_FILE, base, filename };
	return push_attr_source(repo, attr_session, list, &source, true);
}

575
static int push_one_attr(void *ref, const char *path)
576 577
{
	attr_walk_up_info *info = (attr_walk_up_info *)ref;
578
	git_attr_file_source_t src[GIT_ATTR_FILE_NUM_SOURCES];
579 580
	int error = 0, n_src, i;
	bool allow_macros;
581

582 583 584 585 586
	n_src = attr_decide_sources(info->opts ? info->opts->flags : 0,
	                            info->workdir != NULL,
	                            info->index != NULL,
	                            src);

587
	allow_macros = info->workdir ? !strcmp(info->workdir, path) : false;
588

589 590 591
	for (i = 0; !error && i < n_src; ++i) {
		git_attr_file_source source = { src[i], path, GIT_ATTR_FILE };

592 593 594 595 596 597 598 599
		if (src[i] == GIT_ATTR_FILE_SOURCE_COMMIT && info->opts) {
#ifndef GIT_DEPRECATE_HARD
			if (info->opts->commit_id)
				source.commit_id = info->opts->commit_id;
			else
#endif
			source.commit_id = &info->opts->attr_commit_id;
		}
600

601 602 603
		error = push_attr_source(info->repo, info->attr_session, info->files,
		                       &source, allow_macros);
	}
604

605
	return error;
606 607
}

608 609 610 611 612 613 614 615 616 617 618 619
static void release_attr_files(git_vector *files)
{
	size_t i;
	git_attr_file *file;

	git_vector_foreach(files, i, file) {
		git_attr_file__free(file);
		files->contents[i] = NULL;
	}
	git_vector_free(files);
}

620
static int collect_attr_files(
621
	git_repository *repo,
622
	git_attr_session *attr_session,
623
	git_attr_options *opts,
624 625
	const char *path,
	git_vector *files)
626
{
627
	int error = 0;
628
	git_str dir = GIT_STR_INIT, attrfile = GIT_STR_INIT;
629
	const char *workdir = git_repository_workdir(repo);
630
	attr_walk_up_info info = { NULL };
631

632
	GIT_ASSERT(!git_fs_path_is_absolute(path));
633

634
	if ((error = attr_setup(repo, attr_session, opts)) < 0)
635
		return error;
636

637
	/* Resolve path in a non-bare repo */
638 639
	if (workdir != NULL) {
		if (!(error = git_repository_workdir_path(&dir, repo, path)))
640
			error = git_fs_path_find_dir(&dir);
641 642
	}
	else {
643
		error = git_fs_path_dirname_r(&dir, path);
644 645
	}

646
	if (error < 0)
647 648 649 650 651 652 653 654 655
		goto cleanup;

	/* in precendence order highest to lowest:
	 * - $GIT_DIR/info/attributes
	 * - path components with .gitattributes
	 * - config core.attributesfile
	 * - $GIT_PREFIX/etc/gitattributes
	 */

656
	if ((error = git_repository__item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
657
	    (error = push_attr_file(repo, attr_session, files, attrfile.ptr, GIT_ATTR_FILE_INREPO)) < 0) {
658 659 660
		if (error != GIT_ENOTFOUND)
			goto cleanup;
	}
661

662 663
	info.repo = repo;
	info.attr_session = attr_session;
664
	info.opts = opts;
665 666
	info.workdir = workdir;
	if (git_repository_index__weakptr(&info.index, repo) < 0)
667
		git_error_clear(); /* no error even if there is no index */
668
	info.files = files;
669

The rugged tests are fragile committed
670
	if (!strcmp(dir.ptr, "."))
671
		error = push_one_attr(&info, "");
The rugged tests are fragile committed
672
	else
673
		error = git_fs_path_walk_up(&dir, workdir, push_one_attr, &info);
674

675
	if (error < 0)
676 677
		goto cleanup;

678
	if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
679
		error = push_attr_file(repo, attr_session, files, NULL, git_repository_attr_cache(repo)->cfg_attr_file);
680 681
		if (error < 0)
			goto cleanup;
682 683
	}

684
	if (!opts || (opts->flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
685
		error = system_attr_file(&dir, attr_session);
686

687
		if (!error)
688
			error = push_attr_file(repo, attr_session, files, NULL, dir.ptr);
689
		else if (error == GIT_ENOTFOUND)
690 691
			error = 0;
	}
692 693

 cleanup:
694
	if (error < 0)
695
		release_attr_files(files);
696 697
	git_str_dispose(&attrfile);
	git_str_dispose(&dir);
698 699 700

	return error;
}