ignore.c 4.69 KB
Newer Older
1 2 3 4 5 6 7 8
#include "ignore.h"
#include "path.h"

#define GIT_IGNORE_INTERNAL		"[internal]exclude"
#define GIT_IGNORE_FILE_INREPO	"info/exclude"
#define GIT_IGNORE_FILE			".gitignore"

static int load_ignore_file(
9
	git_repository *repo, const char *path, git_attr_file *ignores)
10
{
11
	int error;
12
	git_buf fbuf = GIT_BUF_INIT;
13 14
	git_attr_fnmatch *match = NULL;
	const char *scan = NULL;
15
	char *context = NULL;
16

17 18 19 20
	if (ignores->path == NULL) {
		if (git_attr_file__set_path(repo, path, ignores) < 0)
			return -1;
	}
21

22 23 24
	if (git__suffixcmp(ignores->path, GIT_IGNORE_FILE) == 0) {
		context = git__strndup(ignores->path,
			strlen(ignores->path) - strlen(GIT_IGNORE_FILE));
25
		GITERR_CHECK_ALLOC(context);
26
	}
27

28
	error = git_futils_readbuffer(&fbuf, path);
29

30
	scan = fbuf.ptr;
31

32 33 34 35
	while (!error && *scan) {
		if (!match) {
			match = git__calloc(1, sizeof(*match));
			GITERR_CHECK_ALLOC(match);
36 37
		}

38
		if (!(error = git_attr_fnmatch__parse(match, context, &scan))) {
39 40 41 42 43
			match->flags = match->flags | GIT_ATTR_FNMATCH_IGNORE;
			scan = git__next_line(scan);
			error = git_vector_insert(&ignores->rules, match);
		}

44
		if (error != 0) {
45 46 47 48
			git__free(match->pattern);
			match->pattern = NULL;

			if (error == GIT_ENOTFOUND)
49
				error = 0;
50 51 52 53 54
		} else {
			match = NULL; /* vector now "owns" the match */
		}
	}

55
	git_buf_free(&fbuf);
56
	git__free(match);
57
	git__free(context);
58 59 60 61 62 63 64

	return error;
}

#define push_ignore(R,S,B,F) \
	git_attr_cache__push_file((R),(S),(B),(F),load_ignore_file)

65 66
static int push_one_ignore(void *ref, git_buf *path)
{
67
	git_ignores *ign = (git_ignores *)ref;
68
	return push_ignore(ign->repo, &ign->ign_path, path->ptr, GIT_IGNORE_FILE);
69 70
}

71
int git_ignore__for_path(git_repository *repo, const char *path, git_ignores *ignores)
72
{
73
	int error = 0;
74
	const char *workdir = git_repository_workdir(repo);
75 76

	assert(ignores);
77

78 79 80 81
	ignores->repo = repo;
	git_buf_init(&ignores->dir, 0);
	ignores->ign_internal = NULL;

82 83 84
	if ((error = git_vector_init(&ignores->ign_path, 8, NULL)) < 0 ||
		(error = git_vector_init(&ignores->ign_global, 2, NULL)) < 0 ||
		(error = git_attr_cache__init(repo)) < 0)
85 86
		goto cleanup;

87 88
	/* translate path into directory within workdir */
	if ((error = git_path_find_dir(&ignores->dir, path, workdir)) < 0)
89 90
		goto cleanup;

91 92 93
	/* set up internals */
	error = git_attr_cache__lookup_or_create_file(
		repo, GIT_IGNORE_INTERNAL, NULL, NULL, &ignores->ign_internal);
94
	if (error < 0)
95 96 97
		goto cleanup;

	/* load .gitignore up the path */
98
	error = git_path_walk_up(&ignores->dir, workdir, push_one_ignore, ignores);
99
	if (error < 0)
100 101 102
		goto cleanup;

	/* load .git/info/exclude */
103
	error = push_ignore(repo, &ignores->ign_global,
104
		git_repository_path(repo), GIT_IGNORE_FILE_INREPO);
105
	if (error < 0)
106 107 108
		goto cleanup;

	/* load core.excludesfile */
109 110 111
	if (git_repository_attr_cache(repo)->cfg_excl_file != NULL)
		error = push_ignore(repo, &ignores->ign_global, NULL,
			git_repository_attr_cache(repo)->cfg_excl_file);
112 113

cleanup:
114
	if (error < 0)
115
		git_ignore__free(ignores);
116

117 118 119 120 121
	return error;
}

int git_ignore__push_dir(git_ignores *ign, const char *dir)
{
122 123 124 125
	if (git_buf_joinpath(&ign->dir, ign->dir.ptr, dir) < 0)
		return -1;
	else
		return push_ignore(
126
			ign->repo, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE);
127 128
}

129 130 131 132 133
int git_ignore__pop_dir(git_ignores *ign)
{
	if (ign->ign_path.length > 0) {
		git_attr_file *file = git_vector_last(&ign->ign_path);
		if (git__suffixcmp(ign->dir.ptr, file->path) == 0)
134
			git_vector_pop(&ign->ign_path);
135 136
		git_buf_rtruncate_at_char(&ign->dir, '/');
	}
137
	return 0;
138 139
}

140
void git_ignore__free(git_ignores *ignores)
141
{
142 143 144 145 146 147
	/* don't need to free ignores->ign_internal since it is in cache */
	git_vector_free(&ignores->ign_path);
	git_vector_free(&ignores->ign_global);
	git_buf_free(&ignores->dir);
}

148
static bool ignore_lookup_in_rules(
149 150 151 152 153 154
	git_vector *rules, git_attr_path *path, int *ignored)
{
	unsigned int j;
	git_attr_fnmatch *match;

	git_vector_rforeach(rules, j, match) {
155
		if (git_attr_fnmatch__match(match, path)) {
156
			*ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0);
157
			return true;
158 159 160
		}
	}

161
	return false;
162 163
}

164
int git_ignore__lookup(git_ignores *ignores, const char *pathname, int *ignored)
165
{
166
	unsigned int i;
167 168 169
	git_attr_file *file;
	git_attr_path path;

170 171 172
	if (git_attr_path__init(
		&path, pathname, git_repository_workdir(ignores->repo)) < 0)
		return -1;
173

174 175 176 177
	/* first process builtins - success means path was found */
	if (ignore_lookup_in_rules(
			&ignores->ign_internal->rules, &path, ignored))
		return 0;
178

179 180
	/* next process files in the path */
	git_vector_foreach(&ignores->ign_path, i, file) {
181 182
		if (ignore_lookup_in_rules(&file->rules, &path, ignored))
			return 0;
183 184
	}

185 186
	/* last process global ignores */
	git_vector_foreach(&ignores->ign_global, i, file) {
187 188
		if (ignore_lookup_in_rules(&file->rules, &path, ignored))
			return 0;
189 190 191
	}

	*ignored = 0;
192
	return 0;
193
}