attr.c 13.5 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 40 41
	uint32_t flags,
	const char *path,
	git_vector *files);
42

43
static void release_attr_files(git_vector *files);
44 45

int git_attr_get(
46
	const char **value,
Linquize committed
47
	git_repository *repo,
48 49
	uint32_t flags,
	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

Russell Belfer committed
61 62
	assert(value && repo && name);

63 64
	*value = NULL;

65 66 67 68
	if (git_repository_is_bare(repo))
		dir_flag = GIT_DIR_FLAG_FALSE;

	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
69 70
		return -1;

71
	if ((error = collect_attr_files(repo, NULL, flags, pathname, &files)) < 0)
72
		goto cleanup;
73

74
	memset(&attr, 0, sizeof(attr));
75 76 77 78 79 80
	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) {
81 82 83
			size_t pos;

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

91
cleanup:
92
	release_attr_files(&files);
93
	git_attr_path__free(&path);
94 95 96 97 98 99 100 101 102 103

	return error;
}


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

104
int git_attr_get_many_with_session(
105
	const char **values,
Linquize committed
106
	git_repository *repo,
107
	git_attr_session *attr_session,
108 109
	uint32_t flags,
	const char *pathname,
Linquize committed
110
	size_t num_attr,
111
	const char **names)
112 113 114 115
{
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
116
	size_t i, j, k;
117 118 119 120
	git_attr_file *file;
	git_attr_rule *rule;
	attr_get_many_info *info = NULL;
	size_t num_found = 0;
121
	git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
122

Russell Belfer committed
123 124 125 126 127
	if (!num_attr)
		return 0;

	assert(values && repo && names);

128 129 130 131
	if (git_repository_is_bare(repo))
		dir_flag = GIT_DIR_FLAG_FALSE;

	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
132 133
		return -1;

134
	if ((error = collect_attr_files(repo, attr_session, flags, pathname, &files)) < 0)
135
		goto cleanup;
136

137
	info = git__calloc(num_attr, sizeof(attr_get_many_info));
138
	GIT_ERROR_CHECK_ALLOC(info);
139 140 141 142 143 144

	git_vector_foreach(&files, i, file) {

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

			for (k = 0; k < num_attr; k++) {
145
				size_t pos;
146 147 148 149 150 151 152 153 154

				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]);
				}

155
				if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) {
156 157 158 159 160 161 162 163 164 165 166
					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;
				}
			}
		}
	}

167 168 169 170 171
	for (k = 0; k < num_attr; k++) {
		if (!info[k].found)
			values[k] = NULL;
	}

172
cleanup:
173
	release_attr_files(&files);
174
	git_attr_path__free(&path);
175 176 177 178 179
	git__free(info);

	return error;
}

180 181 182 183 184 185 186 187 188 189 190
int git_attr_get_many(
	const char **values,
	git_repository *repo,
	uint32_t flags,
	const char *pathname,
	size_t num_attr,
	const char **names)
{
	return git_attr_get_many_with_session(
		values, repo, NULL, flags, pathname, num_attr, names);
}
191 192

int git_attr_foreach(
Linquize committed
193
	git_repository *repo,
194 195
	uint32_t flags,
	const char *pathname,
196 197 198 199 200 201
	int (*callback)(const char *name, const char *value, void *payload),
	void *payload)
{
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
202
	size_t i, j, k;
203 204 205
	git_attr_file *file;
	git_attr_rule *rule;
	git_attr_assignment *assign;
206
	git_strmap *seen = NULL;
207
	git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN;
208

Russell Belfer committed
209 210
	assert(repo && callback);

211 212 213 214
	if (git_repository_is_bare(repo))
		dir_flag = GIT_DIR_FLAG_FALSE;

	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo), dir_flag) < 0)
215 216
		return -1;

217
	if ((error = collect_attr_files(repo, NULL, flags, pathname, &files)) < 0 ||
218
	    (error = git_strmap_new(&seen)) < 0)
219
		goto cleanup;
220 221 222 223 224 225 226

	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 */
227
				if (git_strmap_exists(seen, assign->name))
228 229
					continue;

230
				if ((error = git_strmap_set(seen, assign->name, assign)) < 0)
231
					goto cleanup;
232

233 234
				error = callback(assign->name, assign->value, payload);
				if (error) {
235
					git_error_set_after_callback(error);
236
					goto cleanup;
237
				}
238 239 240 241 242
			}
		}
	}

cleanup:
243
	git_strmap_free(seen);
244
	release_attr_files(&files);
245
	git_attr_path__free(&path);
246 247 248 249

	return error;
}

250 251
static int preload_attr_file(
	git_repository *repo,
252
	git_attr_session *attr_session,
253 254
	git_attr_file_source source,
	const char *base,
255 256
	const char *file,
	bool allow_macros)
257 258 259 260 261 262
{
	int error;
	git_attr_file *preload = NULL;

	if (!file)
		return 0;
263 264
	if (!(error = git_attr_cache__get(&preload, repo, attr_session, source, base, file,
					  git_attr_file__parse_buffer, allow_macros)))
265 266 267 268 269
		git_attr_file__free(preload);

	return error;
}

270 271 272 273 274 275 276 277 278 279
static int system_attr_file(
	git_buf *out,
	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)
280
			git_error_clear();
281 282 283 284 285 286 287 288

		return error;
	}

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

		if (error == GIT_ENOTFOUND)
289
			git_error_clear();
290 291 292 293 294 295 296 297 298 299 300
		else if (error)
			return error;

		attr_session->init_sysdir = 1;
	}

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

	/* We can safely provide a git_buf with no allocation (asize == 0) to
	 * a consumer. This allows them to treat this as a regular `git_buf`,
301
	 * but their call to `git_buf_dispose` will not attempt to free it.
302
	 */
303 304
	git_buf_attach_notowned(
		out, attr_session->sysdir.ptr, attr_session->sysdir.size);
305 306 307
	return 0;
}

308 309 310 311
static int attr_setup(
	git_repository *repo,
	git_attr_session *attr_session,
	uint32_t flags)
312
{
313
	git_buf path = GIT_BUF_INIT;
314 315 316
	git_index *idx = NULL;
	const char *workdir;
	int error = 0;
317

318
	if (attr_session && attr_session->init_setup)
319 320
		return 0;

321 322 323
	if ((error = git_attr_cache__init(repo)) < 0)
		return error;

324 325 326
	/*
	 * Preload attribute files that could contain macros so the
	 * definitions will be available for later file parsing.
327 328
	 */

329 330
	if ((error = system_attr_file(&path, attr_session)) < 0 ||
	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
331
				       NULL, path.ptr, true)) < 0) {
332 333 334
		if (error != GIT_ENOTFOUND)
			goto out;
	}
335

336
	if ((error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
337
				       NULL, git_repository_attr_cache(repo)->cfg_attr_file, true)) < 0)
338
		goto out;
339

340
	git_buf_clear(&path); /* git_repository_item_path expects an empty buffer, because it uses git_buf_set */
341 342
	if ((error = git_repository_item_path(&path, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
343
				       path.ptr, GIT_ATTR_FILE_INREPO, true)) < 0) {
344 345 346
		if (error != GIT_ENOTFOUND)
			goto out;
	}
347

348 349
	if ((workdir = git_repository_workdir(repo)) != NULL &&
	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_FILE,
350
				       workdir, GIT_ATTR_FILE, true)) < 0)
351
			goto out;
352 353

	if ((error = git_repository_index__weakptr(&idx, repo)) < 0 ||
354
	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_INDEX,
355
				       NULL, GIT_ATTR_FILE, true)) < 0)
356
			goto out;
357

358 359 360 361 362
	if ((flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0 &&
	    (error = preload_attr_file(repo, attr_session, GIT_ATTR_FILE__FROM_HEAD,
				       NULL, GIT_ATTR_FILE, true)) < 0)
		goto out;

363
	if (attr_session)
364
		attr_session->init_setup = 1;
365

366
out:
367
	git_buf_dispose(&path);
368

369 370 371
	return error;
}

372 373 374 375 376 377 378
int git_attr_add_macro(
	git_repository *repo,
	const char *name,
	const char *values)
{
	int error;
	git_attr_rule *macro = NULL;
379
	git_pool *pool;
380

Russell Belfer committed
381
	if ((error = git_attr_cache__init(repo)) < 0)
382
		return error;
383 384

	macro = git__calloc(1, sizeof(git_attr_rule));
385
	GIT_ERROR_CHECK_ALLOC(macro);
386

387 388 389
	pool = &git_repository_attr_cache(repo)->pool;

	macro->match.pattern = git_pool_strdup(pool, name);
390
	GIT_ERROR_CHECK_ALLOC(macro->match.pattern);
391 392 393 394

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

395
	error = git_attr_assignment__parse(repo, pool, &macro->assigns, &values);
396

397
	if (!error)
398 399
		error = git_attr_cache__insert_macro(repo, macro);

400
	if (error < 0)
401 402 403 404 405
		git_attr_rule__free(macro);

	return error;
}

406 407
typedef struct {
	git_repository *repo;
408
	git_attr_session *attr_session;
409 410 411
	uint32_t flags;
	const char *workdir;
	git_index *index;
412 413 414
	git_vector *files;
} attr_walk_up_info;

415
static int attr_decide_sources(
416
	uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs)
417 418 419 420 421 422
{
	int count = 0;

	switch (flags & 0x03) {
	case GIT_ATTR_CHECK_FILE_THEN_INDEX:
		if (has_wd)
423
			srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
424
		if (has_index)
425
			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
426 427 428
		break;
	case GIT_ATTR_CHECK_INDEX_THEN_FILE:
		if (has_index)
429
			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
430
		if (has_wd)
431
			srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
432 433 434
		break;
	case GIT_ATTR_CHECK_INDEX_ONLY:
		if (has_index)
435
			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
436 437 438
		break;
	}

439 440 441
	if ((flags & GIT_ATTR_CHECK_INCLUDE_HEAD) != 0)
		srcs[count++] = GIT_ATTR_FILE__FROM_HEAD;

442 443 444
	return count;
}

445 446
static int push_attr_file(
	git_repository *repo,
447
	git_attr_session *attr_session,
448
	git_vector *list,
449
	git_attr_file_source source,
450
	const char *base,
451 452
	const char *filename,
	bool allow_macros)
453 454 455 456
{
	int error = 0;
	git_attr_file *file = NULL;

457
	error = git_attr_cache__get(&file, repo, attr_session,
458
		source, base, filename, git_attr_file__parse_buffer, allow_macros);
459

460 461 462 463 464 465 466
	if (error < 0)
		return error;

	if (file != NULL) {
		if ((error = git_vector_insert(list, file)) < 0)
			git_attr_file__free(file);
	}
467 468 469 470

	return error;
}

471
static int push_one_attr(void *ref, const char *path)
472 473
{
	attr_walk_up_info *info = (attr_walk_up_info *)ref;
474
	git_attr_file_source src[GIT_ATTR_FILE_NUM_SOURCES];
475 476
	int error = 0, n_src, i;
	bool allow_macros;
477

478
	n_src = attr_decide_sources(
479
		info->flags, info->workdir != NULL, info->index != NULL, src);
480
	allow_macros = info->workdir ? !strcmp(info->workdir, path) : false;
481 482

	for (i = 0; !error && i < n_src; ++i)
483 484
		error = push_attr_file(info->repo, info->attr_session, info->files,
				       src[i], path, GIT_ATTR_FILE, allow_macros);
485

486
	return error;
487 488
}

489 490 491 492 493 494 495 496 497 498 499 500
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);
}

501
static int collect_attr_files(
502
	git_repository *repo,
503
	git_attr_session *attr_session,
504 505 506
	uint32_t flags,
	const char *path,
	git_vector *files)
507
{
508
	int error = 0;
509
	git_buf dir = GIT_BUF_INIT, attrfile = GIT_BUF_INIT;
510
	const char *workdir = git_repository_workdir(repo);
511
	attr_walk_up_info info = { NULL };
512

513
	if ((error = attr_setup(repo, attr_session, flags)) < 0)
514
		return error;
515

516 517
	/* Resolve path in a non-bare repo */
	if (workdir != NULL)
518
		error = git_path_find_dir(&dir, path, workdir);
519 520
	else
		error = git_path_dirname_r(&dir, path);
521
	if (error < 0)
522 523 524 525 526 527 528 529 530
		goto cleanup;

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

531 532
	if ((error = git_repository_item_path(&attrfile, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 ||
	    (error = push_attr_file(repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
533
				    attrfile.ptr, GIT_ATTR_FILE_INREPO, true)) < 0) {
534 535 536
		if (error != GIT_ENOTFOUND)
			goto cleanup;
	}
537

538 539
	info.repo = repo;
	info.attr_session = attr_session;
540 541 542
	info.flags = flags;
	info.workdir = workdir;
	if (git_repository_index__weakptr(&info.index, repo) < 0)
543
		git_error_clear(); /* no error even if there is no index */
544
	info.files = files;
545

The rugged tests are fragile committed
546
	if (!strcmp(dir.ptr, "."))
547
		error = push_one_attr(&info, "");
The rugged tests are fragile committed
548
	else
549 550
		error = git_path_walk_up(&dir, workdir, push_one_attr, &info);

551
	if (error < 0)
552 553
		goto cleanup;

554
	if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
555 556
		error = push_attr_file(repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
				       NULL, git_repository_attr_cache(repo)->cfg_attr_file, true);
557 558
		if (error < 0)
			goto cleanup;
559 560
	}

561
	if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
562
		error = system_attr_file(&dir, attr_session);
563

564
		if (!error)
565 566
			error = push_attr_file(repo, attr_session, files, GIT_ATTR_FILE__FROM_FILE,
					       NULL, dir.ptr, true);
567
		else if (error == GIT_ENOTFOUND)
568 569
			error = 0;
	}
570 571

 cleanup:
572
	if (error < 0)
573
		release_attr_files(files);
574 575
	git_buf_dispose(&attrfile);
	git_buf_dispose(&dir);
576 577 578

	return error;
}