line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
/* |
2
|
|
|
|
|
|
|
* Copyright (C) the libgit2 contributors. All rights reserved. |
3
|
|
|
|
|
|
|
* |
4
|
|
|
|
|
|
|
* This file is part of libgit2, distributed under the GNU GPL v2 with |
5
|
|
|
|
|
|
|
* a Linking Exception. For full terms see the included COPYING file. |
6
|
|
|
|
|
|
|
*/ |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "ignore.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "git2/ignore.h" |
11
|
|
|
|
|
|
|
#include "common.h" |
12
|
|
|
|
|
|
|
#include "attrcache.h" |
13
|
|
|
|
|
|
|
#include "path.h" |
14
|
|
|
|
|
|
|
#include "config.h" |
15
|
|
|
|
|
|
|
#include "wildmatch.h" |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#define GIT_IGNORE_INTERNAL "[internal]exclude" |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
#define GIT_IGNORE_DEFAULT_RULES ".\n..\n.git\n" |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
/** |
22
|
|
|
|
|
|
|
* A negative ignore pattern can negate a positive one without |
23
|
|
|
|
|
|
|
* wildcards if it is a basename only and equals the basename of |
24
|
|
|
|
|
|
|
* the positive pattern. Thus |
25
|
|
|
|
|
|
|
* |
26
|
|
|
|
|
|
|
* foo/bar |
27
|
|
|
|
|
|
|
* !bar |
28
|
|
|
|
|
|
|
* |
29
|
|
|
|
|
|
|
* would result in foo/bar being unignored again while |
30
|
|
|
|
|
|
|
* |
31
|
|
|
|
|
|
|
* moo/foo/bar |
32
|
|
|
|
|
|
|
* !foo/bar |
33
|
|
|
|
|
|
|
* |
34
|
|
|
|
|
|
|
* would do nothing. The reverse also holds true: a positive |
35
|
|
|
|
|
|
|
* basename pattern can be negated by unignoring the basename in |
36
|
|
|
|
|
|
|
* subdirectories. Thus |
37
|
|
|
|
|
|
|
* |
38
|
|
|
|
|
|
|
* bar |
39
|
|
|
|
|
|
|
* !foo/bar |
40
|
|
|
|
|
|
|
* |
41
|
|
|
|
|
|
|
* would result in foo/bar being unignored again. As with the |
42
|
|
|
|
|
|
|
* first case, |
43
|
|
|
|
|
|
|
* |
44
|
|
|
|
|
|
|
* foo/bar |
45
|
|
|
|
|
|
|
* !moo/foo/bar |
46
|
|
|
|
|
|
|
* |
47
|
|
|
|
|
|
|
* would do nothing, again. |
48
|
|
|
|
|
|
|
*/ |
49
|
0
|
|
|
|
|
|
static int does_negate_pattern(git_attr_fnmatch *rule, git_attr_fnmatch *neg) |
50
|
|
|
|
|
|
|
{ |
51
|
|
|
|
|
|
|
int (*cmp)(const char *, const char *, size_t); |
52
|
|
|
|
|
|
|
git_attr_fnmatch *longer, *shorter; |
53
|
|
|
|
|
|
|
char *p; |
54
|
|
|
|
|
|
|
|
55
|
0
|
0
|
|
|
|
|
if ((rule->flags & GIT_ATTR_FNMATCH_NEGATIVE) != 0 |
56
|
0
|
0
|
|
|
|
|
|| (neg->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) |
57
|
0
|
|
|
|
|
|
return false; |
58
|
|
|
|
|
|
|
|
59
|
0
|
0
|
|
|
|
|
if (neg->flags & GIT_ATTR_FNMATCH_ICASE) |
60
|
0
|
|
|
|
|
|
cmp = git__strncasecmp; |
61
|
|
|
|
|
|
|
else |
62
|
0
|
|
|
|
|
|
cmp = git__strncmp; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
/* If lengths match we need to have an exact match */ |
65
|
0
|
0
|
|
|
|
|
if (rule->length == neg->length) { |
66
|
0
|
|
|
|
|
|
return cmp(rule->pattern, neg->pattern, rule->length) == 0; |
67
|
0
|
0
|
|
|
|
|
} else if (rule->length < neg->length) { |
68
|
0
|
|
|
|
|
|
shorter = rule; |
69
|
0
|
|
|
|
|
|
longer = neg; |
70
|
|
|
|
|
|
|
} else { |
71
|
0
|
|
|
|
|
|
shorter = neg; |
72
|
0
|
|
|
|
|
|
longer = rule; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
/* Otherwise, we need to check if the shorter |
76
|
|
|
|
|
|
|
* rule is a basename only (that is, it contains |
77
|
|
|
|
|
|
|
* no path separator) and, if so, if it |
78
|
|
|
|
|
|
|
* matches the tail of the longer rule */ |
79
|
0
|
|
|
|
|
|
p = longer->pattern + longer->length - shorter->length; |
80
|
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if (p[-1] != '/') |
82
|
0
|
|
|
|
|
|
return false; |
83
|
0
|
0
|
|
|
|
|
if (memchr(shorter->pattern, '/', shorter->length) != NULL) |
84
|
0
|
|
|
|
|
|
return false; |
85
|
|
|
|
|
|
|
|
86
|
0
|
|
|
|
|
|
return cmp(p, shorter->pattern, shorter->length) == 0; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
/** |
90
|
|
|
|
|
|
|
* A negative ignore can only unignore a file which is given explicitly before, thus |
91
|
|
|
|
|
|
|
* |
92
|
|
|
|
|
|
|
* foo |
93
|
|
|
|
|
|
|
* !foo/bar |
94
|
|
|
|
|
|
|
* |
95
|
|
|
|
|
|
|
* does not unignore 'foo/bar' as it's not in the list. However |
96
|
|
|
|
|
|
|
* |
97
|
|
|
|
|
|
|
* foo/ |
98
|
|
|
|
|
|
|
* !foo/bar |
99
|
|
|
|
|
|
|
* |
100
|
|
|
|
|
|
|
* does unignore 'foo/bar', as it is contained within the 'foo/' rule. |
101
|
|
|
|
|
|
|
*/ |
102
|
0
|
|
|
|
|
|
static int does_negate_rule(int *out, git_vector *rules, git_attr_fnmatch *match) |
103
|
|
|
|
|
|
|
{ |
104
|
0
|
|
|
|
|
|
int error = 0, wildmatch_flags; |
105
|
|
|
|
|
|
|
size_t i; |
106
|
|
|
|
|
|
|
git_attr_fnmatch *rule; |
107
|
|
|
|
|
|
|
char *path; |
108
|
0
|
|
|
|
|
|
git_buf buf = GIT_BUF_INIT; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
*out = 0; |
111
|
|
|
|
|
|
|
|
112
|
0
|
|
|
|
|
|
wildmatch_flags = WM_PATHNAME; |
113
|
0
|
0
|
|
|
|
|
if (match->flags & GIT_ATTR_FNMATCH_ICASE) |
114
|
0
|
|
|
|
|
|
wildmatch_flags |= WM_CASEFOLD; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
/* path of the file relative to the workdir, so we match the rules in subdirs */ |
117
|
0
|
0
|
|
|
|
|
if (match->containing_dir) { |
118
|
0
|
|
|
|
|
|
git_buf_puts(&buf, match->containing_dir); |
119
|
|
|
|
|
|
|
} |
120
|
0
|
0
|
|
|
|
|
if (git_buf_puts(&buf, match->pattern) < 0) |
121
|
0
|
|
|
|
|
|
return -1; |
122
|
|
|
|
|
|
|
|
123
|
0
|
|
|
|
|
|
path = git_buf_detach(&buf); |
124
|
|
|
|
|
|
|
|
125
|
0
|
0
|
|
|
|
|
git_vector_foreach(rules, i, rule) { |
126
|
0
|
0
|
|
|
|
|
if (!(rule->flags & GIT_ATTR_FNMATCH_HASWILD)) { |
127
|
0
|
0
|
|
|
|
|
if (does_negate_pattern(rule, match)) { |
128
|
0
|
|
|
|
|
|
error = 0; |
129
|
0
|
|
|
|
|
|
*out = 1; |
130
|
0
|
|
|
|
|
|
goto out; |
131
|
|
|
|
|
|
|
} |
132
|
|
|
|
|
|
|
else |
133
|
0
|
|
|
|
|
|
continue; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
0
|
|
|
|
|
|
git_buf_clear(&buf); |
137
|
0
|
0
|
|
|
|
|
if (rule->containing_dir) |
138
|
0
|
|
|
|
|
|
git_buf_puts(&buf, rule->containing_dir); |
139
|
0
|
|
|
|
|
|
git_buf_puts(&buf, rule->pattern); |
140
|
|
|
|
|
|
|
|
141
|
0
|
0
|
|
|
|
|
if (git_buf_oom(&buf)) |
142
|
0
|
|
|
|
|
|
goto out; |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
/* if we found a match, we want to keep this rule */ |
145
|
0
|
0
|
|
|
|
|
if ((wildmatch(git_buf_cstr(&buf), path, wildmatch_flags)) == WM_MATCH) { |
146
|
0
|
|
|
|
|
|
*out = 1; |
147
|
0
|
|
|
|
|
|
error = 0; |
148
|
0
|
|
|
|
|
|
goto out; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
error = 0; |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
out: |
155
|
0
|
|
|
|
|
|
git__free(path); |
156
|
0
|
|
|
|
|
|
git_buf_dispose(&buf); |
157
|
0
|
|
|
|
|
|
return error; |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
373
|
|
|
|
|
|
static int parse_ignore_file( |
161
|
|
|
|
|
|
|
git_repository *repo, git_attr_file *attrs, const char *data, bool allow_macros) |
162
|
|
|
|
|
|
|
{ |
163
|
373
|
|
|
|
|
|
int error = 0; |
164
|
373
|
|
|
|
|
|
int ignore_case = false; |
165
|
373
|
|
|
|
|
|
const char *scan = data, *context = NULL; |
166
|
373
|
|
|
|
|
|
git_attr_fnmatch *match = NULL; |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
GIT_UNUSED(allow_macros); |
169
|
|
|
|
|
|
|
|
170
|
373
|
50
|
|
|
|
|
if (git_repository__configmap_lookup(&ignore_case, repo, GIT_CONFIGMAP_IGNORECASE) < 0) |
171
|
0
|
|
|
|
|
|
git_error_clear(); |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
/* if subdir file path, convert context for file paths */ |
174
|
746
|
|
|
|
|
|
if (attrs->entry && |
175
|
744
|
100
|
|
|
|
|
git_path_root(attrs->entry->path) < 0 && |
176
|
371
|
|
|
|
|
|
!git__suffixcmp(attrs->entry->path, "/" GIT_IGNORE_FILE)) |
177
|
176
|
|
|
|
|
|
context = attrs->entry->path; |
178
|
|
|
|
|
|
|
|
179
|
373
|
50
|
|
|
|
|
if (git_mutex_lock(&attrs->lock) < 0) { |
180
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock ignore file"); |
181
|
0
|
|
|
|
|
|
return -1; |
182
|
|
|
|
|
|
|
} |
183
|
|
|
|
|
|
|
|
184
|
445
|
50
|
|
|
|
|
while (!error && *scan) { |
|
|
100
|
|
|
|
|
|
185
|
72
|
|
|
|
|
|
int valid_rule = 1; |
186
|
|
|
|
|
|
|
|
187
|
72
|
100
|
|
|
|
|
if (!match && !(match = git__calloc(1, sizeof(*match)))) { |
|
|
50
|
|
|
|
|
|
188
|
0
|
|
|
|
|
|
error = -1; |
189
|
0
|
|
|
|
|
|
break; |
190
|
|
|
|
|
|
|
} |
191
|
|
|
|
|
|
|
|
192
|
72
|
|
|
|
|
|
match->flags = |
193
|
|
|
|
|
|
|
GIT_ATTR_FNMATCH_ALLOWSPACE | GIT_ATTR_FNMATCH_ALLOWNEG; |
194
|
|
|
|
|
|
|
|
195
|
72
|
100
|
|
|
|
|
if (!(error = git_attr_fnmatch__parse( |
196
|
|
|
|
|
|
|
match, &attrs->pool, context, &scan))) |
197
|
|
|
|
|
|
|
{ |
198
|
44
|
|
|
|
|
|
match->flags |= GIT_ATTR_FNMATCH_IGNORE; |
199
|
|
|
|
|
|
|
|
200
|
44
|
50
|
|
|
|
|
if (ignore_case) |
201
|
0
|
|
|
|
|
|
match->flags |= GIT_ATTR_FNMATCH_ICASE; |
202
|
|
|
|
|
|
|
|
203
|
44
|
|
|
|
|
|
scan = git__next_line(scan); |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
/* |
206
|
|
|
|
|
|
|
* If a negative match doesn't actually do anything, |
207
|
|
|
|
|
|
|
* throw it away. As we cannot always verify whether a |
208
|
|
|
|
|
|
|
* rule containing wildcards negates another rule, we |
209
|
|
|
|
|
|
|
* do not optimize away these rules, though. |
210
|
|
|
|
|
|
|
* */ |
211
|
44
|
50
|
|
|
|
|
if (match->flags & GIT_ATTR_FNMATCH_NEGATIVE |
212
|
0
|
0
|
|
|
|
|
&& !(match->flags & GIT_ATTR_FNMATCH_HASWILD)) |
213
|
0
|
|
|
|
|
|
error = does_negate_rule(&valid_rule, &attrs->rules, match); |
214
|
|
|
|
|
|
|
|
215
|
44
|
50
|
|
|
|
|
if (!error && valid_rule) |
|
|
50
|
|
|
|
|
|
216
|
44
|
|
|
|
|
|
error = git_vector_insert(&attrs->rules, match); |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
72
|
100
|
|
|
|
|
if (error != 0 || !valid_rule) { |
|
|
50
|
|
|
|
|
|
220
|
28
|
|
|
|
|
|
match->pattern = NULL; |
221
|
|
|
|
|
|
|
|
222
|
56
|
50
|
|
|
|
|
if (error == GIT_ENOTFOUND) |
223
|
28
|
|
|
|
|
|
error = 0; |
224
|
|
|
|
|
|
|
} else { |
225
|
72
|
|
|
|
|
|
match = NULL; /* vector now "owns" the match */ |
226
|
|
|
|
|
|
|
} |
227
|
|
|
|
|
|
|
} |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
git_mutex_unlock(&attrs->lock); |
230
|
373
|
|
|
|
|
|
git__free(match); |
231
|
|
|
|
|
|
|
|
232
|
373
|
|
|
|
|
|
return error; |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
548
|
|
|
|
|
|
static int push_ignore_file( |
236
|
|
|
|
|
|
|
git_ignores *ignores, |
237
|
|
|
|
|
|
|
git_vector *which_list, |
238
|
|
|
|
|
|
|
const char *base, |
239
|
|
|
|
|
|
|
const char *filename) |
240
|
|
|
|
|
|
|
{ |
241
|
548
|
|
|
|
|
|
int error = 0; |
242
|
548
|
|
|
|
|
|
git_attr_file *file = NULL; |
243
|
|
|
|
|
|
|
|
244
|
548
|
|
|
|
|
|
error = git_attr_cache__get(&file, ignores->repo, NULL, GIT_ATTR_FILE__FROM_FILE, |
245
|
|
|
|
|
|
|
base, filename, parse_ignore_file, false); |
246
|
548
|
50
|
|
|
|
|
if (error < 0) |
247
|
0
|
|
|
|
|
|
return error; |
248
|
|
|
|
|
|
|
|
249
|
548
|
50
|
|
|
|
|
if (file != NULL) { |
250
|
548
|
50
|
|
|
|
|
if ((error = git_vector_insert(which_list, file)) < 0) |
251
|
0
|
|
|
|
|
|
git_attr_file__free(file); |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
|
254
|
548
|
|
|
|
|
|
return error; |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
|
257
|
186
|
|
|
|
|
|
static int push_one_ignore(void *payload, const char *path) |
258
|
|
|
|
|
|
|
{ |
259
|
186
|
|
|
|
|
|
git_ignores *ign = payload; |
260
|
186
|
|
|
|
|
|
ign->depth++; |
261
|
186
|
|
|
|
|
|
return push_ignore_file(ign, &ign->ign_path, path, GIT_IGNORE_FILE); |
262
|
|
|
|
|
|
|
} |
263
|
|
|
|
|
|
|
|
264
|
188
|
|
|
|
|
|
static int get_internal_ignores(git_attr_file **out, git_repository *repo) |
265
|
|
|
|
|
|
|
{ |
266
|
|
|
|
|
|
|
int error; |
267
|
|
|
|
|
|
|
|
268
|
188
|
50
|
|
|
|
|
if ((error = git_attr_cache__init(repo)) < 0) |
269
|
0
|
|
|
|
|
|
return error; |
270
|
|
|
|
|
|
|
|
271
|
188
|
|
|
|
|
|
error = git_attr_cache__get(out, repo, NULL, GIT_ATTR_FILE__IN_MEMORY, NULL, |
272
|
|
|
|
|
|
|
GIT_IGNORE_INTERNAL, NULL, false); |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
/* if internal rules list is empty, insert default rules */ |
275
|
188
|
50
|
|
|
|
|
if (!error && !(*out)->rules.length) |
|
|
100
|
|
|
|
|
|
276
|
14
|
|
|
|
|
|
error = parse_ignore_file(repo, *out, GIT_IGNORE_DEFAULT_RULES, false); |
277
|
|
|
|
|
|
|
|
278
|
188
|
|
|
|
|
|
return error; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
186
|
|
|
|
|
|
int git_ignore__for_path( |
282
|
|
|
|
|
|
|
git_repository *repo, |
283
|
|
|
|
|
|
|
const char *path, |
284
|
|
|
|
|
|
|
git_ignores *ignores) |
285
|
|
|
|
|
|
|
{ |
286
|
186
|
|
|
|
|
|
int error = 0; |
287
|
186
|
|
|
|
|
|
const char *workdir = git_repository_workdir(repo); |
288
|
186
|
|
|
|
|
|
git_buf infopath = GIT_BUF_INIT; |
289
|
|
|
|
|
|
|
|
290
|
186
|
50
|
|
|
|
|
assert(repo && ignores && path); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
291
|
|
|
|
|
|
|
|
292
|
186
|
|
|
|
|
|
memset(ignores, 0, sizeof(*ignores)); |
293
|
186
|
|
|
|
|
|
ignores->repo = repo; |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
/* Read the ignore_case flag */ |
296
|
186
|
50
|
|
|
|
|
if ((error = git_repository__configmap_lookup( |
297
|
|
|
|
|
|
|
&ignores->ignore_case, repo, GIT_CONFIGMAP_IGNORECASE)) < 0) |
298
|
0
|
|
|
|
|
|
goto cleanup; |
299
|
|
|
|
|
|
|
|
300
|
186
|
50
|
|
|
|
|
if ((error = git_attr_cache__init(repo)) < 0) |
301
|
0
|
|
|
|
|
|
goto cleanup; |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
/* given a unrooted path in a non-bare repo, resolve it */ |
304
|
372
|
50
|
|
|
|
|
if (workdir && git_path_root(path) < 0) { |
|
|
50
|
|
|
|
|
|
305
|
186
|
|
|
|
|
|
git_buf local = GIT_BUF_INIT; |
306
|
|
|
|
|
|
|
|
307
|
186
|
50
|
|
|
|
|
if ((error = git_path_dirname_r(&local, path)) < 0 || |
|
|
50
|
|
|
|
|
|
308
|
186
|
50
|
|
|
|
|
(error = git_path_resolve_relative(&local, 0)) < 0 || |
309
|
186
|
|
|
|
|
|
(error = git_path_to_dir(&local)) < 0 || |
310
|
186
|
|
|
|
|
|
(error = git_buf_joinpath(&ignores->dir, workdir, local.ptr)) < 0) |
311
|
|
|
|
|
|
|
{;} /* Nothing, we just want to stop on the first error */ |
312
|
186
|
|
|
|
|
|
git_buf_dispose(&local); |
313
|
|
|
|
|
|
|
} else { |
314
|
0
|
|
|
|
|
|
error = git_buf_joinpath(&ignores->dir, path, ""); |
315
|
|
|
|
|
|
|
} |
316
|
186
|
50
|
|
|
|
|
if (error < 0) |
317
|
0
|
|
|
|
|
|
goto cleanup; |
318
|
|
|
|
|
|
|
|
319
|
186
|
50
|
|
|
|
|
if (workdir && !git__prefixcmp(ignores->dir.ptr, workdir)) |
|
|
50
|
|
|
|
|
|
320
|
186
|
|
|
|
|
|
ignores->dir_root = strlen(workdir); |
321
|
|
|
|
|
|
|
|
322
|
|
|
|
|
|
|
/* set up internals */ |
323
|
186
|
50
|
|
|
|
|
if ((error = get_internal_ignores(&ignores->ign_internal, repo)) < 0) |
324
|
0
|
|
|
|
|
|
goto cleanup; |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
/* load .gitignore up the path */ |
327
|
186
|
50
|
|
|
|
|
if (workdir != NULL) { |
328
|
186
|
|
|
|
|
|
error = git_path_walk_up( |
329
|
|
|
|
|
|
|
&ignores->dir, workdir, push_one_ignore, ignores); |
330
|
186
|
50
|
|
|
|
|
if (error < 0) |
331
|
0
|
|
|
|
|
|
goto cleanup; |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
/* load .git/info/exclude if possible */ |
335
|
186
|
50
|
|
|
|
|
if ((error = git_repository_item_path(&infopath, repo, GIT_REPOSITORY_ITEM_INFO)) < 0 || |
|
|
50
|
|
|
|
|
|
336
|
186
|
|
|
|
|
|
(error = push_ignore_file(ignores, &ignores->ign_global, infopath.ptr, GIT_IGNORE_FILE_INREPO)) < 0) { |
337
|
0
|
0
|
|
|
|
|
if (error != GIT_ENOTFOUND) |
338
|
0
|
|
|
|
|
|
goto cleanup; |
339
|
0
|
|
|
|
|
|
error = 0; |
340
|
|
|
|
|
|
|
} |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
/* load core.excludesfile */ |
343
|
186
|
50
|
|
|
|
|
if (git_repository_attr_cache(repo)->cfg_excl_file != NULL) |
344
|
0
|
|
|
|
|
|
error = push_ignore_file( |
345
|
|
|
|
|
|
|
ignores, &ignores->ign_global, NULL, |
346
|
0
|
|
|
|
|
|
git_repository_attr_cache(repo)->cfg_excl_file); |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
cleanup: |
349
|
186
|
|
|
|
|
|
git_buf_dispose(&infopath); |
350
|
186
|
50
|
|
|
|
|
if (error < 0) |
351
|
0
|
|
|
|
|
|
git_ignore__free(ignores); |
352
|
|
|
|
|
|
|
|
353
|
186
|
|
|
|
|
|
return error; |
354
|
|
|
|
|
|
|
} |
355
|
|
|
|
|
|
|
|
356
|
176
|
|
|
|
|
|
int git_ignore__push_dir(git_ignores *ign, const char *dir) |
357
|
|
|
|
|
|
|
{ |
358
|
176
|
50
|
|
|
|
|
if (git_buf_joinpath(&ign->dir, ign->dir.ptr, dir) < 0) |
359
|
0
|
|
|
|
|
|
return -1; |
360
|
|
|
|
|
|
|
|
361
|
176
|
|
|
|
|
|
ign->depth++; |
362
|
|
|
|
|
|
|
|
363
|
176
|
|
|
|
|
|
return push_ignore_file( |
364
|
176
|
|
|
|
|
|
ign, &ign->ign_path, ign->dir.ptr, GIT_IGNORE_FILE); |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
|
367
|
359
|
|
|
|
|
|
int git_ignore__pop_dir(git_ignores *ign) |
368
|
|
|
|
|
|
|
{ |
369
|
359
|
50
|
|
|
|
|
if (ign->ign_path.length > 0) { |
370
|
359
|
|
|
|
|
|
git_attr_file *file = git_vector_last(&ign->ign_path); |
371
|
359
|
|
|
|
|
|
const char *start = file->entry->path, *end; |
372
|
|
|
|
|
|
|
|
373
|
|
|
|
|
|
|
/* - ign->dir looks something like "/home/user/a/b/" (or "a/b/c/d/") |
374
|
|
|
|
|
|
|
* - file->path looks something like "a/b/.gitignore |
375
|
|
|
|
|
|
|
* |
376
|
|
|
|
|
|
|
* We are popping the last directory off ign->dir. We also want |
377
|
|
|
|
|
|
|
* to remove the file from the vector if the popped directory |
378
|
|
|
|
|
|
|
* matches the ignore path. We need to test if the "a/b" part of |
379
|
|
|
|
|
|
|
* the file key matches the path we are about to pop. |
380
|
|
|
|
|
|
|
*/ |
381
|
|
|
|
|
|
|
|
382
|
359
|
100
|
|
|
|
|
if ((end = strrchr(start, '/')) != NULL) { |
383
|
176
|
|
|
|
|
|
size_t dirlen = (end - start) + 1; |
384
|
176
|
|
|
|
|
|
const char *relpath = ign->dir.ptr + ign->dir_root; |
385
|
176
|
|
|
|
|
|
size_t pathlen = ign->dir.size - ign->dir_root; |
386
|
|
|
|
|
|
|
|
387
|
176
|
50
|
|
|
|
|
if (pathlen == dirlen && !memcmp(relpath, start, dirlen)) { |
|
|
50
|
|
|
|
|
|
388
|
176
|
|
|
|
|
|
git_vector_pop(&ign->ign_path); |
389
|
176
|
|
|
|
|
|
git_attr_file__free(file); |
390
|
|
|
|
|
|
|
} |
391
|
|
|
|
|
|
|
} |
392
|
|
|
|
|
|
|
} |
393
|
|
|
|
|
|
|
|
394
|
359
|
100
|
|
|
|
|
if (--ign->depth > 0) { |
395
|
176
|
|
|
|
|
|
git_buf_rtruncate_at_char(&ign->dir, '/'); |
396
|
176
|
|
|
|
|
|
git_path_to_dir(&ign->dir); |
397
|
|
|
|
|
|
|
} |
398
|
|
|
|
|
|
|
|
399
|
359
|
|
|
|
|
|
return 0; |
400
|
|
|
|
|
|
|
} |
401
|
|
|
|
|
|
|
|
402
|
218
|
|
|
|
|
|
void git_ignore__free(git_ignores *ignores) |
403
|
|
|
|
|
|
|
{ |
404
|
|
|
|
|
|
|
unsigned int i; |
405
|
|
|
|
|
|
|
git_attr_file *file; |
406
|
|
|
|
|
|
|
|
407
|
218
|
|
|
|
|
|
git_attr_file__free(ignores->ign_internal); |
408
|
|
|
|
|
|
|
|
409
|
404
|
100
|
|
|
|
|
git_vector_foreach(&ignores->ign_path, i, file) { |
410
|
186
|
|
|
|
|
|
git_attr_file__free(file); |
411
|
186
|
|
|
|
|
|
ignores->ign_path.contents[i] = NULL; |
412
|
|
|
|
|
|
|
} |
413
|
218
|
|
|
|
|
|
git_vector_free(&ignores->ign_path); |
414
|
|
|
|
|
|
|
|
415
|
404
|
100
|
|
|
|
|
git_vector_foreach(&ignores->ign_global, i, file) { |
416
|
186
|
|
|
|
|
|
git_attr_file__free(file); |
417
|
186
|
|
|
|
|
|
ignores->ign_global.contents[i] = NULL; |
418
|
|
|
|
|
|
|
} |
419
|
218
|
|
|
|
|
|
git_vector_free(&ignores->ign_global); |
420
|
|
|
|
|
|
|
|
421
|
218
|
|
|
|
|
|
git_buf_dispose(&ignores->dir); |
422
|
218
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
|
424
|
2689
|
|
|
|
|
|
static bool ignore_lookup_in_rules( |
425
|
|
|
|
|
|
|
int *ignored, git_attr_file *file, git_attr_path *path) |
426
|
|
|
|
|
|
|
{ |
427
|
|
|
|
|
|
|
size_t j; |
428
|
|
|
|
|
|
|
git_attr_fnmatch *match; |
429
|
|
|
|
|
|
|
|
430
|
5021
|
100
|
|
|
|
|
git_vector_rforeach(&file->rules, j, match) { |
431
|
2343
|
50
|
|
|
|
|
if (match->flags & GIT_ATTR_FNMATCH_DIRECTORY && |
|
|
0
|
|
|
|
|
|
432
|
0
|
|
|
|
|
|
path->is_dir == GIT_DIR_FLAG_FALSE) |
433
|
0
|
|
|
|
|
|
continue; |
434
|
2343
|
100
|
|
|
|
|
if (git_attr_fnmatch__match(match, path)) { |
435
|
22
|
|
|
|
|
|
*ignored = ((match->flags & GIT_ATTR_FNMATCH_NEGATIVE) == 0) ? |
436
|
11
|
|
|
|
|
|
GIT_IGNORE_TRUE : GIT_IGNORE_FALSE; |
437
|
11
|
|
|
|
|
|
return true; |
438
|
|
|
|
|
|
|
} |
439
|
|
|
|
|
|
|
} |
440
|
|
|
|
|
|
|
|
441
|
2678
|
|
|
|
|
|
return false; |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
|
444
|
764
|
|
|
|
|
|
int git_ignore__lookup( |
445
|
|
|
|
|
|
|
int *out, git_ignores *ignores, const char *pathname, git_dir_flag dir_flag) |
446
|
|
|
|
|
|
|
{ |
447
|
|
|
|
|
|
|
size_t i; |
448
|
|
|
|
|
|
|
git_attr_file *file; |
449
|
|
|
|
|
|
|
git_attr_path path; |
450
|
|
|
|
|
|
|
|
451
|
764
|
|
|
|
|
|
*out = GIT_IGNORE_NOTFOUND; |
452
|
|
|
|
|
|
|
|
453
|
764
|
50
|
|
|
|
|
if (git_attr_path__init( |
454
|
764
|
|
|
|
|
|
&path, pathname, git_repository_workdir(ignores->repo), dir_flag) < 0) |
455
|
0
|
|
|
|
|
|
return -1; |
456
|
|
|
|
|
|
|
|
457
|
|
|
|
|
|
|
/* first process builtins - success means path was found */ |
458
|
764
|
100
|
|
|
|
|
if (ignore_lookup_in_rules(out, ignores->ign_internal, &path)) |
459
|
10
|
|
|
|
|
|
goto cleanup; |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
/* next process files in the path. |
462
|
|
|
|
|
|
|
* this process has to process ignores in reverse order |
463
|
|
|
|
|
|
|
* to ensure correct prioritization of rules |
464
|
|
|
|
|
|
|
*/ |
465
|
1918
|
100
|
|
|
|
|
git_vector_rforeach(&ignores->ign_path, i, file) { |
466
|
1164
|
50
|
|
|
|
|
if (ignore_lookup_in_rules(out, file, &path)) |
467
|
0
|
|
|
|
|
|
goto cleanup; |
468
|
|
|
|
|
|
|
} |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
/* last process global ignores */ |
471
|
1508
|
100
|
|
|
|
|
git_vector_foreach(&ignores->ign_global, i, file) { |
472
|
754
|
50
|
|
|
|
|
if (ignore_lookup_in_rules(out, file, &path)) |
473
|
0
|
|
|
|
|
|
goto cleanup; |
474
|
|
|
|
|
|
|
} |
475
|
|
|
|
|
|
|
|
476
|
|
|
|
|
|
|
cleanup: |
477
|
764
|
|
|
|
|
|
git_attr_path__free(&path); |
478
|
764
|
|
|
|
|
|
return 0; |
479
|
|
|
|
|
|
|
} |
480
|
|
|
|
|
|
|
|
481
|
2
|
|
|
|
|
|
int git_ignore_add_rule(git_repository *repo, const char *rules) |
482
|
|
|
|
|
|
|
{ |
483
|
|
|
|
|
|
|
int error; |
484
|
2
|
|
|
|
|
|
git_attr_file *ign_internal = NULL; |
485
|
|
|
|
|
|
|
|
486
|
2
|
50
|
|
|
|
|
if ((error = get_internal_ignores(&ign_internal, repo)) < 0) |
487
|
0
|
|
|
|
|
|
return error; |
488
|
|
|
|
|
|
|
|
489
|
2
|
|
|
|
|
|
error = parse_ignore_file(repo, ign_internal, rules, false); |
490
|
2
|
|
|
|
|
|
git_attr_file__free(ign_internal); |
491
|
|
|
|
|
|
|
|
492
|
2
|
|
|
|
|
|
return error; |
493
|
|
|
|
|
|
|
} |
494
|
|
|
|
|
|
|
|
495
|
0
|
|
|
|
|
|
int git_ignore_clear_internal_rules(git_repository *repo) |
496
|
|
|
|
|
|
|
{ |
497
|
|
|
|
|
|
|
int error; |
498
|
|
|
|
|
|
|
git_attr_file *ign_internal; |
499
|
|
|
|
|
|
|
|
500
|
0
|
0
|
|
|
|
|
if ((error = get_internal_ignores(&ign_internal, repo)) < 0) |
501
|
0
|
|
|
|
|
|
return error; |
502
|
|
|
|
|
|
|
|
503
|
0
|
0
|
|
|
|
|
if (!(error = git_attr_file__clear_rules(ign_internal, true))) |
504
|
0
|
|
|
|
|
|
error = parse_ignore_file( |
505
|
|
|
|
|
|
|
repo, ign_internal, GIT_IGNORE_DEFAULT_RULES, false); |
506
|
|
|
|
|
|
|
|
507
|
0
|
|
|
|
|
|
git_attr_file__free(ign_internal); |
508
|
0
|
|
|
|
|
|
return error; |
509
|
|
|
|
|
|
|
} |
510
|
|
|
|
|
|
|
|
511
|
3
|
|
|
|
|
|
int git_ignore_path_is_ignored( |
512
|
|
|
|
|
|
|
int *ignored, |
513
|
|
|
|
|
|
|
git_repository *repo, |
514
|
|
|
|
|
|
|
const char *pathname) |
515
|
|
|
|
|
|
|
{ |
516
|
|
|
|
|
|
|
int error; |
517
|
|
|
|
|
|
|
const char *workdir; |
518
|
|
|
|
|
|
|
git_attr_path path; |
519
|
|
|
|
|
|
|
git_ignores ignores; |
520
|
|
|
|
|
|
|
unsigned int i; |
521
|
|
|
|
|
|
|
git_attr_file *file; |
522
|
3
|
|
|
|
|
|
git_dir_flag dir_flag = GIT_DIR_FLAG_UNKNOWN; |
523
|
|
|
|
|
|
|
|
524
|
3
|
50
|
|
|
|
|
assert(repo && ignored && pathname); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
525
|
|
|
|
|
|
|
|
526
|
3
|
|
|
|
|
|
workdir = git_repository_workdir(repo); |
527
|
|
|
|
|
|
|
|
528
|
3
|
|
|
|
|
|
memset(&path, 0, sizeof(path)); |
529
|
3
|
|
|
|
|
|
memset(&ignores, 0, sizeof(ignores)); |
530
|
|
|
|
|
|
|
|
531
|
3
|
50
|
|
|
|
|
if (!git__suffixcmp(pathname, "/")) |
532
|
0
|
|
|
|
|
|
dir_flag = GIT_DIR_FLAG_TRUE; |
533
|
3
|
50
|
|
|
|
|
else if (git_repository_is_bare(repo)) |
534
|
0
|
|
|
|
|
|
dir_flag = GIT_DIR_FLAG_FALSE; |
535
|
|
|
|
|
|
|
|
536
|
3
|
50
|
|
|
|
|
if ((error = git_attr_path__init(&path, pathname, workdir, dir_flag)) < 0 || |
|
|
50
|
|
|
|
|
|
537
|
3
|
|
|
|
|
|
(error = git_ignore__for_path(repo, path.path, &ignores)) < 0) |
538
|
|
|
|
|
|
|
goto cleanup; |
539
|
|
|
|
|
|
|
|
540
|
|
|
|
|
|
|
while (1) { |
541
|
|
|
|
|
|
|
/* first process builtins - success means path was found */ |
542
|
3
|
100
|
|
|
|
|
if (ignore_lookup_in_rules(ignored, ignores.ign_internal, &path)) |
543
|
1
|
|
|
|
|
|
goto cleanup; |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
/* next process files in the path */ |
546
|
4
|
100
|
|
|
|
|
git_vector_foreach(&ignores.ign_path, i, file) { |
547
|
2
|
50
|
|
|
|
|
if (ignore_lookup_in_rules(ignored, file, &path)) |
548
|
0
|
|
|
|
|
|
goto cleanup; |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
|
|
|
|
|
|
/* last process global ignores */ |
552
|
4
|
100
|
|
|
|
|
git_vector_foreach(&ignores.ign_global, i, file) { |
553
|
2
|
50
|
|
|
|
|
if (ignore_lookup_in_rules(ignored, file, &path)) |
554
|
0
|
|
|
|
|
|
goto cleanup; |
555
|
|
|
|
|
|
|
} |
556
|
|
|
|
|
|
|
|
557
|
|
|
|
|
|
|
/* move up one directory */ |
558
|
2
|
50
|
|
|
|
|
if (path.basename == path.path) |
559
|
2
|
|
|
|
|
|
break; |
560
|
0
|
|
|
|
|
|
path.basename[-1] = '\0'; |
561
|
0
|
0
|
|
|
|
|
while (path.basename > path.path && *path.basename != '/') |
|
|
0
|
|
|
|
|
|
562
|
0
|
|
|
|
|
|
path.basename--; |
563
|
0
|
0
|
|
|
|
|
if (path.basename > path.path) |
564
|
0
|
|
|
|
|
|
path.basename++; |
565
|
0
|
|
|
|
|
|
path.is_dir = 1; |
566
|
|
|
|
|
|
|
|
567
|
0
|
0
|
|
|
|
|
if ((error = git_ignore__pop_dir(&ignores)) < 0) |
568
|
0
|
|
|
|
|
|
break; |
569
|
0
|
|
|
|
|
|
} |
570
|
|
|
|
|
|
|
|
571
|
2
|
|
|
|
|
|
*ignored = 0; |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
cleanup: |
574
|
3
|
|
|
|
|
|
git_attr_path__free(&path); |
575
|
3
|
|
|
|
|
|
git_ignore__free(&ignores); |
576
|
3
|
|
|
|
|
|
return error; |
577
|
|
|
|
|
|
|
} |
578
|
|
|
|
|
|
|
|
579
|
0
|
|
|
|
|
|
int git_ignore__check_pathspec_for_exact_ignores( |
580
|
|
|
|
|
|
|
git_repository *repo, |
581
|
|
|
|
|
|
|
git_vector *vspec, |
582
|
|
|
|
|
|
|
bool no_fnmatch) |
583
|
|
|
|
|
|
|
{ |
584
|
0
|
|
|
|
|
|
int error = 0; |
585
|
|
|
|
|
|
|
size_t i; |
586
|
|
|
|
|
|
|
git_attr_fnmatch *match; |
587
|
|
|
|
|
|
|
int ignored; |
588
|
0
|
|
|
|
|
|
git_buf path = GIT_BUF_INIT; |
589
|
|
|
|
|
|
|
const char *wd, *filename; |
590
|
|
|
|
|
|
|
git_index *idx; |
591
|
|
|
|
|
|
|
|
592
|
0
|
0
|
|
|
|
|
if ((error = git_repository__ensure_not_bare( |
593
|
0
|
0
|
|
|
|
|
repo, "validate pathspec")) < 0 || |
594
|
|
|
|
|
|
|
(error = git_repository_index(&idx, repo)) < 0) |
595
|
0
|
|
|
|
|
|
return error; |
596
|
|
|
|
|
|
|
|
597
|
0
|
|
|
|
|
|
wd = git_repository_workdir(repo); |
598
|
|
|
|
|
|
|
|
599
|
0
|
0
|
|
|
|
|
git_vector_foreach(vspec, i, match) { |
600
|
|
|
|
|
|
|
/* skip wildcard matches (if they are being used) */ |
601
|
0
|
0
|
|
|
|
|
if ((match->flags & GIT_ATTR_FNMATCH_HASWILD) != 0 && |
|
|
0
|
|
|
|
|
|
602
|
0
|
|
|
|
|
|
!no_fnmatch) |
603
|
0
|
|
|
|
|
|
continue; |
604
|
|
|
|
|
|
|
|
605
|
0
|
|
|
|
|
|
filename = match->pattern; |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
/* if file is already in the index, it's fine */ |
608
|
0
|
0
|
|
|
|
|
if (git_index_get_bypath(idx, filename, 0) != NULL) |
609
|
0
|
|
|
|
|
|
continue; |
610
|
|
|
|
|
|
|
|
611
|
0
|
0
|
|
|
|
|
if ((error = git_buf_joinpath(&path, wd, filename)) < 0) |
612
|
0
|
|
|
|
|
|
break; |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
/* is there a file on disk that matches this exactly? */ |
615
|
0
|
0
|
|
|
|
|
if (!git_path_isfile(path.ptr)) |
616
|
0
|
|
|
|
|
|
continue; |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
/* is that file ignored? */ |
619
|
0
|
0
|
|
|
|
|
if ((error = git_ignore_path_is_ignored(&ignored, repo, filename)) < 0) |
620
|
0
|
|
|
|
|
|
break; |
621
|
|
|
|
|
|
|
|
622
|
0
|
0
|
|
|
|
|
if (ignored) { |
623
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "pathspec contains ignored file '%s'", |
624
|
|
|
|
|
|
|
filename); |
625
|
0
|
|
|
|
|
|
error = GIT_EINVALIDSPEC; |
626
|
0
|
|
|
|
|
|
break; |
627
|
|
|
|
|
|
|
} |
628
|
|
|
|
|
|
|
} |
629
|
|
|
|
|
|
|
|
630
|
0
|
|
|
|
|
|
git_index_free(idx); |
631
|
0
|
|
|
|
|
|
git_buf_dispose(&path); |
632
|
|
|
|
|
|
|
|
633
|
0
|
|
|
|
|
|
return error; |
634
|
|
|
|
|
|
|
} |
635
|
|
|
|
|
|
|
|