attr.c 8.71 KB
Newer Older
1
#include "common.h"
2
#include "repository.h"
3
#include "sysdir.h"
4
#include "config.h"
5
#include "attr_file.h"
6
#include "ignore.h"
7
#include "git2/oid.h"
8 9
#include <ctype.h>

10
GIT__USE_STRMAP;
11

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
const char *git_attr__true  = "[internal]__TRUE__";
const char *git_attr__false = "[internal]__FALSE__";
const char *git_attr__unset = "[internal]__UNSET__";

git_attr_t git_attr_value(const char *attr)
{
	if (attr == NULL || attr == git_attr__unset)
		return GIT_ATTR_UNSPECIFIED_T;

	if (attr == git_attr__true)
		return GIT_ATTR_TRUE_T;

	if (attr == git_attr__false)
		return GIT_ATTR_FALSE_T;

	return GIT_ATTR_VALUE_T;
}

30
static int collect_attr_files(
31 32 33 34
	git_repository *repo,
	uint32_t flags,
	const char *path,
	git_vector *files);
35

36
static void release_attr_files(git_vector *files);
37 38

int git_attr_get(
39
	const char **value,
Linquize committed
40
	git_repository *repo,
41 42
	uint32_t flags,
	const char *pathname,
43
	const char *name)
44 45 46 47
{
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
48
	size_t i, j;
49 50 51 52
	git_attr_file *file;
	git_attr_name attr;
	git_attr_rule *rule;

Russell Belfer committed
53 54
	assert(value && repo && name);

55 56
	*value = NULL;

57 58 59
	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
		return -1;

60
	if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
61
		goto cleanup;
62

63
	memset(&attr, 0, sizeof(attr));
64 65 66 67 68 69
	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) {
70 71 72
			size_t pos;

			if (!git_vector_bsearch(&pos, &rule->assigns, &attr)) {
73 74
				*value = ((git_attr_assignment *)git_vector_get(
							  &rule->assigns, pos))->value;
75
				goto cleanup;
76 77 78 79
			}
		}
	}

80
cleanup:
81
	release_attr_files(&files);
82
	git_attr_path__free(&path);
83 84 85 86 87 88 89 90 91 92 93

	return error;
}


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

int git_attr_get_many(
94
	const char **values,
Linquize committed
95
	git_repository *repo,
96 97
	uint32_t flags,
	const char *pathname,
Linquize committed
98
	size_t num_attr,
99
	const char **names)
100 101 102 103
{
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
104
	size_t i, j, k;
105 106 107 108 109
	git_attr_file *file;
	git_attr_rule *rule;
	attr_get_many_info *info = NULL;
	size_t num_found = 0;

Russell Belfer committed
110 111 112 113 114
	if (!num_attr)
		return 0;

	assert(values && repo && names);

115 116 117
	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
		return -1;

118
	if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0)
119
		goto cleanup;
120

121 122
	info = git__calloc(num_attr, sizeof(attr_get_many_info));
	GITERR_CHECK_ALLOC(info);
123 124 125 126 127 128

	git_vector_foreach(&files, i, file) {

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

			for (k = 0; k < num_attr; k++) {
129
				size_t pos;
130 131 132 133 134 135 136 137 138

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

139
				if (!git_vector_bsearch(&pos, &rule->assigns, &info[k].name)) {
140 141 142 143 144 145 146 147 148 149 150
					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;
				}
			}
		}
	}

151 152 153 154 155
	for (k = 0; k < num_attr; k++) {
		if (!info[k].found)
			values[k] = NULL;
	}

156
cleanup:
157
	release_attr_files(&files);
158
	git_attr_path__free(&path);
159 160 161 162 163 164 165
	git__free(info);

	return error;
}


int git_attr_foreach(
Linquize committed
166
	git_repository *repo,
167 168
	uint32_t flags,
	const char *pathname,
169 170 171 172 173 174
	int (*callback)(const char *name, const char *value, void *payload),
	void *payload)
{
	int error;
	git_attr_path path;
	git_vector files = GIT_VECTOR_INIT;
175
	size_t i, j, k;
176 177 178
	git_attr_file *file;
	git_attr_rule *rule;
	git_attr_assignment *assign;
179
	git_strmap *seen = NULL;
180

Russell Belfer committed
181 182
	assert(repo && callback);

183 184 185
	if (git_attr_path__init(&path, pathname, git_repository_workdir(repo)) < 0)
		return -1;

186 187
	if ((error = collect_attr_files(repo, flags, pathname, &files)) < 0 ||
		(error = git_strmap_alloc(&seen)) < 0)
188
		goto cleanup;
189 190 191 192 193 194 195

	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 */
196
				if (git_strmap_exists(seen, assign->name))
197 198
					continue;

199
				git_strmap_insert(seen, assign->name, assign, error);
200 201
				if (error < 0)
					goto cleanup;
202

203 204
				error = callback(assign->name, assign->value, payload);
				if (error) {
205
					giterr_set_after_callback(error);
206
					goto cleanup;
207
				}
208 209 210 211 212
			}
		}
	}

cleanup:
213
	git_strmap_free(seen);
214
	release_attr_files(&files);
215
	git_attr_path__free(&path);
216 217 218 219

	return error;
}

220 221 222 223 224 225 226
int git_attr_add_macro(
	git_repository *repo,
	const char *name,
	const char *values)
{
	int error;
	git_attr_rule *macro = NULL;
227
	git_pool *pool;
228

229 230
	if (git_attr_cache__init(repo) < 0)
		return -1;
231 232

	macro = git__calloc(1, sizeof(git_attr_rule));
233
	GITERR_CHECK_ALLOC(macro);
234

235 236 237
	pool = &git_repository_attr_cache(repo)->pool;

	macro->match.pattern = git_pool_strdup(pool, name);
238
	GITERR_CHECK_ALLOC(macro->match.pattern);
239 240 241 242

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

243
	error = git_attr_assignment__parse(repo, pool, &macro->assigns, &values);
244

245
	if (!error)
246 247
		error = git_attr_cache__insert_macro(repo, macro);

248
	if (error < 0)
249 250 251 252 253
		git_attr_rule__free(macro);

	return error;
}

254 255
typedef struct {
	git_repository *repo;
256 257 258
	uint32_t flags;
	const char *workdir;
	git_index *index;
259 260 261
	git_vector *files;
} attr_walk_up_info;

262
static int attr_decide_sources(
263
	uint32_t flags, bool has_wd, bool has_index, git_attr_file_source *srcs)
264 265 266 267 268 269
{
	int count = 0;

	switch (flags & 0x03) {
	case GIT_ATTR_CHECK_FILE_THEN_INDEX:
		if (has_wd)
270
			srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
271
		if (has_index)
272
			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
273 274 275
		break;
	case GIT_ATTR_CHECK_INDEX_THEN_FILE:
		if (has_index)
276
			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
277
		if (has_wd)
278
			srcs[count++] = GIT_ATTR_FILE__FROM_FILE;
279 280 281
		break;
	case GIT_ATTR_CHECK_INDEX_ONLY:
		if (has_index)
282
			srcs[count++] = GIT_ATTR_FILE__FROM_INDEX;
283 284 285 286 287 288
		break;
	}

	return count;
}

289 290 291
static int push_attr_file(
	git_repository *repo,
	git_vector *list,
292
	git_attr_file_source source,
293 294 295 296 297 298
	const char *base,
	const char *filename)
{
	int error = 0;
	git_attr_file *file = NULL;

299
	error = git_attr_cache__get(
300
		&file, repo, source, base, filename, git_attr_file__parse_buffer);
301 302 303 304 305 306 307
	if (error < 0)
		return error;

	if (file != NULL) {
		if ((error = git_vector_insert(list, file)) < 0)
			git_attr_file__free(file);
	}
308 309 310 311

	return error;
}

312 313
static int push_one_attr(void *ref, git_buf *path)
{
314
	int error = 0, n_src, i;
315
	attr_walk_up_info *info = (attr_walk_up_info *)ref;
316
	git_attr_file_source src[2];
317

318
	n_src = attr_decide_sources(
319 320 321
		info->flags, info->workdir != NULL, info->index != NULL, src);

	for (i = 0; !error && i < n_src; ++i)
322 323
		error = push_attr_file(
			info->repo, info->files, src[i], path->ptr, GIT_ATTR_FILE);
324

325
	return error;
326 327
}

328 329 330 331 332 333 334 335 336 337 338 339
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);
}

340
static int collect_attr_files(
341 342 343 344
	git_repository *repo,
	uint32_t flags,
	const char *path,
	git_vector *files)
345
{
346
	int error;
347 348
	git_buf dir = GIT_BUF_INIT;
	const char *workdir = git_repository_workdir(repo);
349
	attr_walk_up_info info = { NULL };
350

351 352
	if ((error = git_attr_cache__init(repo)) < 0)
		return error;
353

354 355
	/* Resolve path in a non-bare repo */
	if (workdir != NULL)
356 357
		error = git_path_find_dir(&dir, path, workdir);
	else
358
		error = git_path_dirname_r(&dir, path);
359
	if (error < 0)
360 361 362 363 364 365 366 367 368
		goto cleanup;

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

369
	error = push_attr_file(
370
		repo, files, GIT_ATTR_FILE__FROM_FILE,
371
		git_repository_path(repo), GIT_ATTR_FILE_INREPO);
372
	if (error < 0)
373 374
		goto cleanup;

375 376 377 378 379
	info.repo  = repo;
	info.flags = flags;
	info.workdir = workdir;
	if (git_repository_index__weakptr(&info.index, repo) < 0)
		giterr_clear(); /* no error even if there is no index */
380
	info.files = files;
381

382
	error = git_path_walk_up(&dir, workdir, push_one_attr, &info);
383
	if (error < 0)
384 385
		goto cleanup;

386
	if (git_repository_attr_cache(repo)->cfg_attr_file != NULL) {
387
		error = push_attr_file(
388
			repo, files, GIT_ATTR_FILE__FROM_FILE,
389
			NULL, git_repository_attr_cache(repo)->cfg_attr_file);
390 391
		if (error < 0)
			goto cleanup;
392 393
	}

394
	if ((flags & GIT_ATTR_CHECK_NO_SYSTEM) == 0) {
395
		error = git_sysdir_find_system_file(&dir, GIT_ATTR_FILE_SYSTEM);
396
		if (!error)
397
			error = push_attr_file(
398
				repo, files, GIT_ATTR_FILE__FROM_FILE, NULL, dir.ptr);
399 400
		else if (error == GIT_ENOTFOUND) {
			giterr_clear();
401
			error = 0;
402
		}
403
	}
404 405

 cleanup:
406
	if (error < 0)
407
		release_attr_files(files);
408 409 410 411
	git_buf_free(&dir);

	return error;
}