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 "filter.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "common.h" |
11
|
|
|
|
|
|
|
#include "futils.h" |
12
|
|
|
|
|
|
|
#include "hash.h" |
13
|
|
|
|
|
|
|
#include "repository.h" |
14
|
|
|
|
|
|
|
#include "global.h" |
15
|
|
|
|
|
|
|
#include "git2/sys/filter.h" |
16
|
|
|
|
|
|
|
#include "git2/config.h" |
17
|
|
|
|
|
|
|
#include "blob.h" |
18
|
|
|
|
|
|
|
#include "attr_file.h" |
19
|
|
|
|
|
|
|
#include "array.h" |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
struct git_filter_source { |
22
|
|
|
|
|
|
|
git_repository *repo; |
23
|
|
|
|
|
|
|
const char *path; |
24
|
|
|
|
|
|
|
git_oid oid; /* zero if unknown (which is likely) */ |
25
|
|
|
|
|
|
|
uint16_t filemode; /* zero if unknown */ |
26
|
|
|
|
|
|
|
git_filter_mode_t mode; |
27
|
|
|
|
|
|
|
uint32_t flags; |
28
|
|
|
|
|
|
|
}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
typedef struct { |
31
|
|
|
|
|
|
|
const char *filter_name; |
32
|
|
|
|
|
|
|
git_filter *filter; |
33
|
|
|
|
|
|
|
void *payload; |
34
|
|
|
|
|
|
|
} git_filter_entry; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
struct git_filter_list { |
37
|
|
|
|
|
|
|
git_array_t(git_filter_entry) filters; |
38
|
|
|
|
|
|
|
git_filter_source source; |
39
|
|
|
|
|
|
|
git_buf *temp_buf; |
40
|
|
|
|
|
|
|
char path[GIT_FLEX_ARRAY]; |
41
|
|
|
|
|
|
|
}; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
typedef struct { |
44
|
|
|
|
|
|
|
char *filter_name; |
45
|
|
|
|
|
|
|
git_filter *filter; |
46
|
|
|
|
|
|
|
int priority; |
47
|
|
|
|
|
|
|
int initialized; |
48
|
|
|
|
|
|
|
size_t nattrs, nmatches; |
49
|
|
|
|
|
|
|
char *attrdata; |
50
|
|
|
|
|
|
|
const char *attrs[GIT_FLEX_ARRAY]; |
51
|
|
|
|
|
|
|
} git_filter_def; |
52
|
|
|
|
|
|
|
|
53
|
94
|
|
|
|
|
|
static int filter_def_priority_cmp(const void *a, const void *b) |
54
|
|
|
|
|
|
|
{ |
55
|
94
|
|
|
|
|
|
int pa = ((const git_filter_def *)a)->priority; |
56
|
94
|
|
|
|
|
|
int pb = ((const git_filter_def *)b)->priority; |
57
|
94
|
100
|
|
|
|
|
return (pa < pb) ? -1 : (pa > pb) ? 1 : 0; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
struct git_filter_registry { |
61
|
|
|
|
|
|
|
git_rwlock lock; |
62
|
|
|
|
|
|
|
git_vector filters; |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
static struct git_filter_registry filter_registry; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
static void git_filter_global_shutdown(void); |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
176
|
|
|
|
|
|
static int filter_def_scan_attrs( |
71
|
|
|
|
|
|
|
git_buf *attrs, size_t *nattr, size_t *nmatch, const char *attr_str) |
72
|
|
|
|
|
|
|
{ |
73
|
176
|
|
|
|
|
|
const char *start, *scan = attr_str; |
74
|
|
|
|
|
|
|
int has_eq; |
75
|
|
|
|
|
|
|
|
76
|
176
|
|
|
|
|
|
*nattr = *nmatch = 0; |
77
|
|
|
|
|
|
|
|
78
|
176
|
50
|
|
|
|
|
if (!scan) |
79
|
0
|
|
|
|
|
|
return 0; |
80
|
|
|
|
|
|
|
|
81
|
524
|
100
|
|
|
|
|
while (*scan) { |
82
|
520
|
100
|
|
|
|
|
while (git__isspace(*scan)) scan++; |
83
|
|
|
|
|
|
|
|
84
|
1826
|
100
|
|
|
|
|
for (start = scan, has_eq = 0; *scan && !git__isspace(*scan); ++scan) { |
|
|
100
|
|
|
|
|
|
85
|
1478
|
50
|
|
|
|
|
if (*scan == '=') |
86
|
0
|
|
|
|
|
|
has_eq = 1; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
348
|
50
|
|
|
|
|
if (scan > start) { |
90
|
348
|
|
|
|
|
|
(*nattr)++; |
91
|
348
|
50
|
|
|
|
|
if (has_eq || *start == '-' || *start == '+' || *start == '!') |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
92
|
86
|
|
|
|
|
|
(*nmatch)++; |
93
|
|
|
|
|
|
|
|
94
|
348
|
50
|
|
|
|
|
if (has_eq) |
95
|
0
|
|
|
|
|
|
git_buf_putc(attrs, '='); |
96
|
348
|
|
|
|
|
|
git_buf_put(attrs, start, scan - start); |
97
|
348
|
|
|
|
|
|
git_buf_putc(attrs, '\0'); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
176
|
|
|
|
|
|
return 0; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
176
|
|
|
|
|
|
static void filter_def_set_attrs(git_filter_def *fdef) |
105
|
|
|
|
|
|
|
{ |
106
|
176
|
|
|
|
|
|
char *scan = fdef->attrdata; |
107
|
|
|
|
|
|
|
size_t i; |
108
|
|
|
|
|
|
|
|
109
|
524
|
100
|
|
|
|
|
for (i = 0; i < fdef->nattrs; ++i) { |
110
|
|
|
|
|
|
|
const char *name, *value; |
111
|
|
|
|
|
|
|
|
112
|
348
|
|
|
|
|
|
switch (*scan) { |
113
|
|
|
|
|
|
|
case '=': |
114
|
0
|
|
|
|
|
|
name = scan + 1; |
115
|
0
|
0
|
|
|
|
|
for (scan++; *scan != '='; scan++) /* find '=' */; |
116
|
0
|
|
|
|
|
|
*scan++ = '\0'; |
117
|
0
|
|
|
|
|
|
value = scan; |
118
|
0
|
|
|
|
|
|
break; |
119
|
|
|
|
|
|
|
case '-': |
120
|
0
|
|
|
|
|
|
name = scan + 1; value = git_attr__false; break; |
121
|
|
|
|
|
|
|
case '+': |
122
|
86
|
|
|
|
|
|
name = scan + 1; value = git_attr__true; break; |
123
|
|
|
|
|
|
|
case '!': |
124
|
0
|
|
|
|
|
|
name = scan + 1; value = git_attr__unset; break; |
125
|
|
|
|
|
|
|
default: |
126
|
262
|
|
|
|
|
|
name = scan; value = NULL; break; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
348
|
|
|
|
|
|
fdef->attrs[i] = name; |
130
|
348
|
|
|
|
|
|
fdef->attrs[i + fdef->nattrs] = value; |
131
|
|
|
|
|
|
|
|
132
|
348
|
|
|
|
|
|
scan += strlen(scan) + 1; |
133
|
|
|
|
|
|
|
} |
134
|
176
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
29
|
|
|
|
|
|
static int filter_def_name_key_check(const void *key, const void *fdef) |
137
|
|
|
|
|
|
|
{ |
138
|
29
|
|
|
|
|
|
const char *name = |
139
|
29
|
50
|
|
|
|
|
fdef ? ((const git_filter_def *)fdef)->filter_name : NULL; |
140
|
29
|
50
|
|
|
|
|
return name ? git__strcmp(key, name) : -1; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
static int filter_def_filter_key_check(const void *key, const void *fdef) |
144
|
|
|
|
|
|
|
{ |
145
|
0
|
0
|
|
|
|
|
const void *filter = fdef ? ((const git_filter_def *)fdef)->filter : NULL; |
146
|
0
|
0
|
|
|
|
|
return (key == filter) ? 0 : -1; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
/* Note: callers must lock the registry before calling this function */ |
150
|
176
|
|
|
|
|
|
static int filter_registry_insert( |
151
|
|
|
|
|
|
|
const char *name, git_filter *filter, int priority) |
152
|
|
|
|
|
|
|
{ |
153
|
|
|
|
|
|
|
git_filter_def *fdef; |
154
|
176
|
|
|
|
|
|
size_t nattr = 0, nmatch = 0, alloc_len; |
155
|
176
|
|
|
|
|
|
git_buf attrs = GIT_BUF_INIT; |
156
|
|
|
|
|
|
|
|
157
|
176
|
50
|
|
|
|
|
if (filter_def_scan_attrs(&attrs, &nattr, &nmatch, filter->attributes) < 0) |
158
|
0
|
|
|
|
|
|
return -1; |
159
|
|
|
|
|
|
|
|
160
|
176
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, nattr, 2); |
|
|
50
|
|
|
|
|
|
161
|
176
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_len, alloc_len, sizeof(char *)); |
|
|
50
|
|
|
|
|
|
162
|
176
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloc_len, alloc_len, sizeof(git_filter_def)); |
|
|
50
|
|
|
|
|
|
163
|
|
|
|
|
|
|
|
164
|
176
|
|
|
|
|
|
fdef = git__calloc(1, alloc_len); |
165
|
176
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(fdef); |
166
|
|
|
|
|
|
|
|
167
|
176
|
|
|
|
|
|
fdef->filter_name = git__strdup(name); |
168
|
176
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(fdef->filter_name); |
169
|
|
|
|
|
|
|
|
170
|
176
|
|
|
|
|
|
fdef->filter = filter; |
171
|
176
|
|
|
|
|
|
fdef->priority = priority; |
172
|
176
|
|
|
|
|
|
fdef->nattrs = nattr; |
173
|
176
|
|
|
|
|
|
fdef->nmatches = nmatch; |
174
|
176
|
|
|
|
|
|
fdef->attrdata = git_buf_detach(&attrs); |
175
|
|
|
|
|
|
|
|
176
|
176
|
|
|
|
|
|
filter_def_set_attrs(fdef); |
177
|
|
|
|
|
|
|
|
178
|
176
|
50
|
|
|
|
|
if (git_vector_insert(&filter_registry.filters, fdef) < 0) { |
179
|
0
|
|
|
|
|
|
git__free(fdef->filter_name); |
180
|
0
|
|
|
|
|
|
git__free(fdef->attrdata); |
181
|
0
|
|
|
|
|
|
git__free(fdef); |
182
|
0
|
|
|
|
|
|
return -1; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
176
|
|
|
|
|
|
git_vector_sort(&filter_registry.filters); |
186
|
176
|
|
|
|
|
|
return 0; |
187
|
|
|
|
|
|
|
} |
188
|
|
|
|
|
|
|
|
189
|
86
|
|
|
|
|
|
int git_filter_global_init(void) |
190
|
|
|
|
|
|
|
{ |
191
|
86
|
|
|
|
|
|
git_filter *crlf = NULL, *ident = NULL; |
192
|
86
|
|
|
|
|
|
int error = 0; |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
if (git_rwlock_init(&filter_registry.lock) < 0) |
195
|
|
|
|
|
|
|
return -1; |
196
|
|
|
|
|
|
|
|
197
|
86
|
50
|
|
|
|
|
if ((error = git_vector_init(&filter_registry.filters, 2, |
198
|
|
|
|
|
|
|
filter_def_priority_cmp)) < 0) |
199
|
0
|
|
|
|
|
|
goto done; |
200
|
|
|
|
|
|
|
|
201
|
172
|
|
|
|
|
|
if ((crlf = git_crlf_filter_new()) == NULL || |
202
|
86
|
|
|
|
|
|
filter_registry_insert( |
203
|
86
|
50
|
|
|
|
|
GIT_FILTER_CRLF, crlf, GIT_FILTER_CRLF_PRIORITY) < 0 || |
204
|
86
|
50
|
|
|
|
|
(ident = git_ident_filter_new()) == NULL || |
205
|
86
|
|
|
|
|
|
filter_registry_insert( |
206
|
|
|
|
|
|
|
GIT_FILTER_IDENT, ident, GIT_FILTER_IDENT_PRIORITY) < 0) |
207
|
0
|
|
|
|
|
|
error = -1; |
208
|
|
|
|
|
|
|
|
209
|
86
|
|
|
|
|
|
git__on_shutdown(git_filter_global_shutdown); |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
done: |
212
|
86
|
50
|
|
|
|
|
if (error) { |
213
|
0
|
|
|
|
|
|
git_filter_free(crlf); |
214
|
0
|
|
|
|
|
|
git_filter_free(ident); |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
86
|
|
|
|
|
|
return error; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
0
|
|
|
|
|
|
static void git_filter_global_shutdown(void) |
221
|
|
|
|
|
|
|
{ |
222
|
|
|
|
|
|
|
size_t pos; |
223
|
|
|
|
|
|
|
git_filter_def *fdef; |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
if (git_rwlock_wrlock(&filter_registry.lock) < 0) |
226
|
|
|
|
|
|
|
return; |
227
|
|
|
|
|
|
|
|
228
|
0
|
0
|
|
|
|
|
git_vector_foreach(&filter_registry.filters, pos, fdef) { |
229
|
0
|
0
|
|
|
|
|
if (fdef->filter && fdef->filter->shutdown) { |
|
|
0
|
|
|
|
|
|
230
|
0
|
|
|
|
|
|
fdef->filter->shutdown(fdef->filter); |
231
|
0
|
|
|
|
|
|
fdef->initialized = false; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
0
|
|
|
|
|
|
git__free(fdef->filter_name); |
235
|
0
|
|
|
|
|
|
git__free(fdef->attrdata); |
236
|
0
|
|
|
|
|
|
git__free(fdef); |
237
|
|
|
|
|
|
|
} |
238
|
|
|
|
|
|
|
|
239
|
0
|
|
|
|
|
|
git_vector_free(&filter_registry.filters); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
git_rwlock_wrunlock(&filter_registry.lock); |
242
|
|
|
|
|
|
|
git_rwlock_free(&filter_registry.lock); |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
/* Note: callers must lock the registry before calling this function */ |
246
|
12
|
|
|
|
|
|
static int filter_registry_find(size_t *pos, const char *name) |
247
|
|
|
|
|
|
|
{ |
248
|
12
|
|
|
|
|
|
return git_vector_search2( |
249
|
|
|
|
|
|
|
pos, &filter_registry.filters, filter_def_name_key_check, name); |
250
|
|
|
|
|
|
|
} |
251
|
|
|
|
|
|
|
|
252
|
|
|
|
|
|
|
/* Note: callers must lock the registry before calling this function */ |
253
|
8
|
|
|
|
|
|
static git_filter_def *filter_registry_lookup(size_t *pos, const char *name) |
254
|
|
|
|
|
|
|
{ |
255
|
8
|
|
|
|
|
|
git_filter_def *fdef = NULL; |
256
|
|
|
|
|
|
|
|
257
|
8
|
100
|
|
|
|
|
if (!filter_registry_find(pos, name)) |
258
|
5
|
|
|
|
|
|
fdef = git_vector_get(&filter_registry.filters, *pos); |
259
|
|
|
|
|
|
|
|
260
|
8
|
|
|
|
|
|
return fdef; |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
|
264
|
4
|
|
|
|
|
|
int git_filter_register( |
265
|
|
|
|
|
|
|
const char *name, git_filter *filter, int priority) |
266
|
|
|
|
|
|
|
{ |
267
|
|
|
|
|
|
|
int error; |
268
|
|
|
|
|
|
|
|
269
|
4
|
50
|
|
|
|
|
assert(name && filter); |
|
|
50
|
|
|
|
|
|
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
if (git_rwlock_wrlock(&filter_registry.lock) < 0) { |
272
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); |
273
|
|
|
|
|
|
|
return -1; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
4
|
50
|
|
|
|
|
if (!filter_registry_find(NULL, name)) { |
277
|
0
|
|
|
|
|
|
git_error_set( |
278
|
|
|
|
|
|
|
GIT_ERROR_FILTER, "attempt to reregister existing filter '%s'", name); |
279
|
0
|
|
|
|
|
|
error = GIT_EEXISTS; |
280
|
0
|
|
|
|
|
|
goto done; |
281
|
|
|
|
|
|
|
} |
282
|
|
|
|
|
|
|
|
283
|
4
|
|
|
|
|
|
error = filter_registry_insert(name, filter, priority); |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
done: |
286
|
|
|
|
|
|
|
git_rwlock_wrunlock(&filter_registry.lock); |
287
|
4
|
|
|
|
|
|
return error; |
288
|
|
|
|
|
|
|
} |
289
|
|
|
|
|
|
|
|
290
|
4
|
|
|
|
|
|
int git_filter_unregister(const char *name) |
291
|
|
|
|
|
|
|
{ |
292
|
|
|
|
|
|
|
size_t pos; |
293
|
|
|
|
|
|
|
git_filter_def *fdef; |
294
|
4
|
|
|
|
|
|
int error = 0; |
295
|
|
|
|
|
|
|
|
296
|
4
|
50
|
|
|
|
|
assert(name); |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
/* cannot unregister default filters */ |
299
|
4
|
50
|
|
|
|
|
if (!strcmp(GIT_FILTER_CRLF, name) || !strcmp(GIT_FILTER_IDENT, name)) { |
|
|
50
|
|
|
|
|
|
300
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_FILTER, "cannot unregister filter '%s'", name); |
301
|
0
|
|
|
|
|
|
return -1; |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
if (git_rwlock_wrlock(&filter_registry.lock) < 0) { |
305
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); |
306
|
|
|
|
|
|
|
return -1; |
307
|
|
|
|
|
|
|
} |
308
|
|
|
|
|
|
|
|
309
|
4
|
50
|
|
|
|
|
if ((fdef = filter_registry_lookup(&pos, name)) == NULL) { |
310
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_FILTER, "cannot find filter '%s' to unregister", name); |
311
|
0
|
|
|
|
|
|
error = GIT_ENOTFOUND; |
312
|
0
|
|
|
|
|
|
goto done; |
313
|
|
|
|
|
|
|
} |
314
|
|
|
|
|
|
|
|
315
|
4
|
|
|
|
|
|
git_vector_remove(&filter_registry.filters, pos); |
316
|
|
|
|
|
|
|
|
317
|
4
|
50
|
|
|
|
|
if (fdef->initialized && fdef->filter && fdef->filter->shutdown) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
318
|
2
|
|
|
|
|
|
fdef->filter->shutdown(fdef->filter); |
319
|
2
|
|
|
|
|
|
fdef->initialized = false; |
320
|
|
|
|
|
|
|
} |
321
|
|
|
|
|
|
|
|
322
|
4
|
|
|
|
|
|
git__free(fdef->filter_name); |
323
|
4
|
|
|
|
|
|
git__free(fdef->attrdata); |
324
|
4
|
|
|
|
|
|
git__free(fdef); |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
done: |
327
|
|
|
|
|
|
|
git_rwlock_wrunlock(&filter_registry.lock); |
328
|
4
|
|
|
|
|
|
return error; |
329
|
|
|
|
|
|
|
} |
330
|
|
|
|
|
|
|
|
331
|
18
|
|
|
|
|
|
static int filter_initialize(git_filter_def *fdef) |
332
|
|
|
|
|
|
|
{ |
333
|
18
|
|
|
|
|
|
int error = 0; |
334
|
|
|
|
|
|
|
|
335
|
18
|
50
|
|
|
|
|
if (!fdef->initialized && fdef->filter && fdef->filter->initialize) { |
|
|
50
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
336
|
1
|
50
|
|
|
|
|
if ((error = fdef->filter->initialize(fdef->filter)) < 0) |
337
|
0
|
|
|
|
|
|
return error; |
338
|
|
|
|
|
|
|
} |
339
|
|
|
|
|
|
|
|
340
|
18
|
|
|
|
|
|
fdef->initialized = true; |
341
|
18
|
|
|
|
|
|
return 0; |
342
|
|
|
|
|
|
|
} |
343
|
|
|
|
|
|
|
|
344
|
4
|
|
|
|
|
|
git_filter *git_filter_lookup(const char *name) |
345
|
|
|
|
|
|
|
{ |
346
|
|
|
|
|
|
|
size_t pos; |
347
|
|
|
|
|
|
|
git_filter_def *fdef; |
348
|
4
|
|
|
|
|
|
git_filter *filter = NULL; |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
if (git_rwlock_rdlock(&filter_registry.lock) < 0) { |
351
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); |
352
|
|
|
|
|
|
|
return NULL; |
353
|
|
|
|
|
|
|
} |
354
|
|
|
|
|
|
|
|
355
|
4
|
100
|
|
|
|
|
if ((fdef = filter_registry_lookup(&pos, name)) == NULL || |
|
|
50
|
|
|
|
|
|
356
|
0
|
0
|
|
|
|
|
(!fdef->initialized && filter_initialize(fdef) < 0)) |
357
|
|
|
|
|
|
|
goto done; |
358
|
|
|
|
|
|
|
|
359
|
1
|
|
|
|
|
|
filter = fdef->filter; |
360
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
done: |
362
|
|
|
|
|
|
|
git_rwlock_rdunlock(&filter_registry.lock); |
363
|
4
|
|
|
|
|
|
return filter; |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
|
366
|
0
|
|
|
|
|
|
void git_filter_free(git_filter *filter) |
367
|
|
|
|
|
|
|
{ |
368
|
0
|
|
|
|
|
|
git__free(filter); |
369
|
0
|
|
|
|
|
|
} |
370
|
|
|
|
|
|
|
|
371
|
729
|
|
|
|
|
|
git_repository *git_filter_source_repo(const git_filter_source *src) |
372
|
|
|
|
|
|
|
{ |
373
|
729
|
|
|
|
|
|
return src->repo; |
374
|
|
|
|
|
|
|
} |
375
|
|
|
|
|
|
|
|
376
|
92
|
|
|
|
|
|
const char *git_filter_source_path(const git_filter_source *src) |
377
|
|
|
|
|
|
|
{ |
378
|
92
|
|
|
|
|
|
return src->path; |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
|
381
|
1
|
|
|
|
|
|
uint16_t git_filter_source_filemode(const git_filter_source *src) |
382
|
|
|
|
|
|
|
{ |
383
|
1
|
|
|
|
|
|
return src->filemode; |
384
|
|
|
|
|
|
|
} |
385
|
|
|
|
|
|
|
|
386
|
1
|
|
|
|
|
|
const git_oid *git_filter_source_id(const git_filter_source *src) |
387
|
|
|
|
|
|
|
{ |
388
|
1
|
50
|
|
|
|
|
return git_oid_is_zero(&src->oid) ? NULL : &src->oid; |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
|
391
|
66
|
|
|
|
|
|
git_filter_mode_t git_filter_source_mode(const git_filter_source *src) |
392
|
|
|
|
|
|
|
{ |
393
|
66
|
|
|
|
|
|
return src->mode; |
394
|
|
|
|
|
|
|
} |
395
|
|
|
|
|
|
|
|
396
|
228
|
|
|
|
|
|
uint32_t git_filter_source_flags(const git_filter_source *src) |
397
|
|
|
|
|
|
|
{ |
398
|
228
|
|
|
|
|
|
return src->flags; |
399
|
|
|
|
|
|
|
} |
400
|
|
|
|
|
|
|
|
401
|
59
|
|
|
|
|
|
static int filter_list_new( |
402
|
|
|
|
|
|
|
git_filter_list **out, const git_filter_source *src) |
403
|
|
|
|
|
|
|
{ |
404
|
59
|
|
|
|
|
|
git_filter_list *fl = NULL; |
405
|
59
|
50
|
|
|
|
|
size_t pathlen = src->path ? strlen(src->path) : 0, alloclen; |
406
|
|
|
|
|
|
|
|
407
|
59
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_filter_list), pathlen); |
|
|
50
|
|
|
|
|
|
408
|
59
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); |
|
|
50
|
|
|
|
|
|
409
|
|
|
|
|
|
|
|
410
|
59
|
|
|
|
|
|
fl = git__calloc(1, alloclen); |
411
|
59
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(fl); |
412
|
|
|
|
|
|
|
|
413
|
59
|
50
|
|
|
|
|
if (src->path) |
414
|
59
|
|
|
|
|
|
memcpy(fl->path, src->path, pathlen); |
415
|
59
|
|
|
|
|
|
fl->source.repo = src->repo; |
416
|
59
|
|
|
|
|
|
fl->source.path = fl->path; |
417
|
59
|
|
|
|
|
|
fl->source.mode = src->mode; |
418
|
59
|
|
|
|
|
|
fl->source.flags = src->flags; |
419
|
|
|
|
|
|
|
|
420
|
59
|
|
|
|
|
|
*out = fl; |
421
|
59
|
|
|
|
|
|
return 0; |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
|
424
|
461
|
|
|
|
|
|
static int filter_list_check_attributes( |
425
|
|
|
|
|
|
|
const char ***out, |
426
|
|
|
|
|
|
|
git_repository *repo, |
427
|
|
|
|
|
|
|
git_attr_session *attr_session, |
428
|
|
|
|
|
|
|
git_filter_def *fdef, |
429
|
|
|
|
|
|
|
const git_filter_source *src) |
430
|
|
|
|
|
|
|
{ |
431
|
461
|
|
|
|
|
|
const char **strs = git__calloc(fdef->nattrs, sizeof(const char *)); |
432
|
461
|
|
|
|
|
|
uint32_t flags = 0; |
433
|
|
|
|
|
|
|
size_t i; |
434
|
|
|
|
|
|
|
int error; |
435
|
|
|
|
|
|
|
|
436
|
461
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(strs); |
437
|
|
|
|
|
|
|
|
438
|
461
|
50
|
|
|
|
|
if ((src->flags & GIT_FILTER_NO_SYSTEM_ATTRIBUTES) != 0) |
439
|
0
|
|
|
|
|
|
flags |= GIT_ATTR_CHECK_NO_SYSTEM; |
440
|
|
|
|
|
|
|
|
441
|
461
|
50
|
|
|
|
|
if ((src->flags & GIT_FILTER_ATTRIBUTES_FROM_HEAD) != 0) |
442
|
0
|
|
|
|
|
|
flags |= GIT_ATTR_CHECK_INCLUDE_HEAD; |
443
|
|
|
|
|
|
|
|
444
|
461
|
|
|
|
|
|
error = git_attr_get_many_with_session( |
445
|
461
|
|
|
|
|
|
strs, repo, attr_session, flags, src->path, fdef->nattrs, fdef->attrs); |
446
|
|
|
|
|
|
|
|
447
|
|
|
|
|
|
|
/* if no values were found but no matches are needed, it's okay! */ |
448
|
461
|
50
|
|
|
|
|
if (error == GIT_ENOTFOUND && !fdef->nmatches) { |
|
|
0
|
|
|
|
|
|
449
|
0
|
|
|
|
|
|
git_error_clear(); |
450
|
0
|
|
|
|
|
|
git__free((void *)strs); |
451
|
0
|
|
|
|
|
|
return 0; |
452
|
|
|
|
|
|
|
} |
453
|
|
|
|
|
|
|
|
454
|
1378
|
100
|
|
|
|
|
for (i = 0; !error && i < fdef->nattrs; ++i) { |
|
|
100
|
|
|
|
|
|
455
|
917
|
|
|
|
|
|
const char *want = fdef->attrs[fdef->nattrs + i]; |
456
|
|
|
|
|
|
|
git_attr_value_t want_type, found_type; |
457
|
|
|
|
|
|
|
|
458
|
917
|
100
|
|
|
|
|
if (!want) |
459
|
689
|
|
|
|
|
|
continue; |
460
|
|
|
|
|
|
|
|
461
|
228
|
|
|
|
|
|
want_type = git_attr_value(want); |
462
|
228
|
|
|
|
|
|
found_type = git_attr_value(strs[i]); |
463
|
|
|
|
|
|
|
|
464
|
228
|
50
|
|
|
|
|
if (want_type != found_type) |
465
|
228
|
|
|
|
|
|
error = GIT_ENOTFOUND; |
466
|
0
|
0
|
|
|
|
|
else if (want_type == GIT_ATTR_VALUE_STRING && |
|
|
0
|
|
|
|
|
|
467
|
0
|
0
|
|
|
|
|
strcmp(want, strs[i]) && |
468
|
0
|
|
|
|
|
|
strcmp(want, "*")) |
469
|
0
|
|
|
|
|
|
error = GIT_ENOTFOUND; |
470
|
|
|
|
|
|
|
} |
471
|
|
|
|
|
|
|
|
472
|
461
|
100
|
|
|
|
|
if (error) |
473
|
228
|
|
|
|
|
|
git__free((void *)strs); |
474
|
|
|
|
|
|
|
else |
475
|
233
|
|
|
|
|
|
*out = strs; |
476
|
|
|
|
|
|
|
|
477
|
461
|
|
|
|
|
|
return error; |
478
|
|
|
|
|
|
|
} |
479
|
|
|
|
|
|
|
|
480
|
0
|
|
|
|
|
|
int git_filter_list_new( |
481
|
|
|
|
|
|
|
git_filter_list **out, |
482
|
|
|
|
|
|
|
git_repository *repo, |
483
|
|
|
|
|
|
|
git_filter_mode_t mode, |
484
|
|
|
|
|
|
|
uint32_t flags) |
485
|
|
|
|
|
|
|
{ |
486
|
0
|
|
|
|
|
|
git_filter_source src = { 0 }; |
487
|
0
|
|
|
|
|
|
src.repo = repo; |
488
|
0
|
|
|
|
|
|
src.path = NULL; |
489
|
0
|
|
|
|
|
|
src.mode = mode; |
490
|
0
|
|
|
|
|
|
src.flags = flags; |
491
|
0
|
|
|
|
|
|
return filter_list_new(out, &src); |
492
|
|
|
|
|
|
|
} |
493
|
|
|
|
|
|
|
|
494
|
228
|
|
|
|
|
|
int git_filter_list__load_ext( |
495
|
|
|
|
|
|
|
git_filter_list **filters, |
496
|
|
|
|
|
|
|
git_repository *repo, |
497
|
|
|
|
|
|
|
git_blob *blob, /* can be NULL */ |
498
|
|
|
|
|
|
|
const char *path, |
499
|
|
|
|
|
|
|
git_filter_mode_t mode, |
500
|
|
|
|
|
|
|
git_filter_options *filter_opts) |
501
|
|
|
|
|
|
|
{ |
502
|
228
|
|
|
|
|
|
int error = 0; |
503
|
228
|
|
|
|
|
|
git_filter_list *fl = NULL; |
504
|
228
|
|
|
|
|
|
git_filter_source src = { 0 }; |
505
|
|
|
|
|
|
|
git_filter_entry *fe; |
506
|
|
|
|
|
|
|
size_t idx; |
507
|
|
|
|
|
|
|
git_filter_def *fdef; |
508
|
|
|
|
|
|
|
|
509
|
|
|
|
|
|
|
if (git_rwlock_rdlock(&filter_registry.lock) < 0) { |
510
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); |
511
|
|
|
|
|
|
|
return -1; |
512
|
|
|
|
|
|
|
} |
513
|
|
|
|
|
|
|
|
514
|
228
|
|
|
|
|
|
src.repo = repo; |
515
|
228
|
|
|
|
|
|
src.path = path; |
516
|
228
|
|
|
|
|
|
src.mode = mode; |
517
|
228
|
|
|
|
|
|
src.flags = filter_opts->flags; |
518
|
|
|
|
|
|
|
|
519
|
228
|
100
|
|
|
|
|
if (blob) |
520
|
31
|
|
|
|
|
|
git_oid_cpy(&src.oid, git_blob_id(blob)); |
521
|
|
|
|
|
|
|
|
522
|
689
|
100
|
|
|
|
|
git_vector_foreach(&filter_registry.filters, idx, fdef) { |
523
|
461
|
|
|
|
|
|
const char **values = NULL; |
524
|
461
|
|
|
|
|
|
void *payload = NULL; |
525
|
|
|
|
|
|
|
|
526
|
461
|
50
|
|
|
|
|
if (!fdef || !fdef->filter) |
|
|
50
|
|
|
|
|
|
527
|
228
|
|
|
|
|
|
continue; |
528
|
|
|
|
|
|
|
|
529
|
461
|
50
|
|
|
|
|
if (fdef->nattrs > 0) { |
530
|
461
|
|
|
|
|
|
error = filter_list_check_attributes( |
531
|
|
|
|
|
|
|
&values, repo, filter_opts->attr_session, fdef, &src); |
532
|
|
|
|
|
|
|
|
533
|
461
|
100
|
|
|
|
|
if (error == GIT_ENOTFOUND) { |
534
|
228
|
|
|
|
|
|
error = 0; |
535
|
228
|
|
|
|
|
|
continue; |
536
|
233
|
50
|
|
|
|
|
} else if (error < 0) |
537
|
0
|
|
|
|
|
|
break; |
538
|
|
|
|
|
|
|
} |
539
|
|
|
|
|
|
|
|
540
|
233
|
100
|
|
|
|
|
if (!fdef->initialized && (error = filter_initialize(fdef)) < 0) |
|
|
50
|
|
|
|
|
|
541
|
0
|
|
|
|
|
|
break; |
542
|
|
|
|
|
|
|
|
543
|
233
|
100
|
|
|
|
|
if (fdef->filter->check) |
544
|
231
|
|
|
|
|
|
error = fdef->filter->check( |
545
|
|
|
|
|
|
|
fdef->filter, &payload, &src, values); |
546
|
|
|
|
|
|
|
|
547
|
233
|
|
|
|
|
|
git__free((void *)values); |
548
|
|
|
|
|
|
|
|
549
|
233
|
100
|
|
|
|
|
if (error == GIT_PASSTHROUGH) |
550
|
170
|
|
|
|
|
|
error = 0; |
551
|
63
|
50
|
|
|
|
|
else if (error < 0) |
552
|
0
|
|
|
|
|
|
break; |
553
|
|
|
|
|
|
|
else { |
554
|
63
|
100
|
|
|
|
|
if (!fl) { |
555
|
59
|
50
|
|
|
|
|
if ((error = filter_list_new(&fl, &src)) < 0) |
556
|
0
|
|
|
|
|
|
break; |
557
|
|
|
|
|
|
|
|
558
|
59
|
|
|
|
|
|
fl->temp_buf = filter_opts->temp_buf; |
559
|
|
|
|
|
|
|
} |
560
|
|
|
|
|
|
|
|
561
|
63
|
100
|
|
|
|
|
fe = git_array_alloc(fl->filters); |
|
|
50
|
|
|
|
|
|
562
|
63
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(fe); |
563
|
|
|
|
|
|
|
|
564
|
63
|
|
|
|
|
|
fe->filter = fdef->filter; |
565
|
63
|
|
|
|
|
|
fe->filter_name = fdef->filter_name; |
566
|
233
|
|
|
|
|
|
fe->payload = payload; |
567
|
|
|
|
|
|
|
} |
568
|
|
|
|
|
|
|
} |
569
|
|
|
|
|
|
|
|
570
|
|
|
|
|
|
|
git_rwlock_rdunlock(&filter_registry.lock); |
571
|
|
|
|
|
|
|
|
572
|
228
|
50
|
|
|
|
|
if (error && fl != NULL) { |
|
|
0
|
|
|
|
|
|
573
|
0
|
|
|
|
|
|
git_array_clear(fl->filters); |
574
|
0
|
|
|
|
|
|
git__free(fl); |
575
|
0
|
|
|
|
|
|
fl = NULL; |
576
|
|
|
|
|
|
|
} |
577
|
|
|
|
|
|
|
|
578
|
228
|
|
|
|
|
|
*filters = fl; |
579
|
228
|
|
|
|
|
|
return error; |
580
|
|
|
|
|
|
|
} |
581
|
|
|
|
|
|
|
|
582
|
194
|
|
|
|
|
|
int git_filter_list_load( |
583
|
|
|
|
|
|
|
git_filter_list **filters, |
584
|
|
|
|
|
|
|
git_repository *repo, |
585
|
|
|
|
|
|
|
git_blob *blob, /* can be NULL */ |
586
|
|
|
|
|
|
|
const char *path, |
587
|
|
|
|
|
|
|
git_filter_mode_t mode, |
588
|
|
|
|
|
|
|
uint32_t flags) |
589
|
|
|
|
|
|
|
{ |
590
|
194
|
|
|
|
|
|
git_filter_options filter_opts = GIT_FILTER_OPTIONS_INIT; |
591
|
|
|
|
|
|
|
|
592
|
194
|
|
|
|
|
|
filter_opts.flags = flags; |
593
|
|
|
|
|
|
|
|
594
|
194
|
|
|
|
|
|
return git_filter_list__load_ext( |
595
|
|
|
|
|
|
|
filters, repo, blob, path, mode, &filter_opts); |
596
|
|
|
|
|
|
|
} |
597
|
|
|
|
|
|
|
|
598
|
181
|
|
|
|
|
|
void git_filter_list_free(git_filter_list *fl) |
599
|
|
|
|
|
|
|
{ |
600
|
|
|
|
|
|
|
uint32_t i; |
601
|
|
|
|
|
|
|
|
602
|
181
|
100
|
|
|
|
|
if (!fl) |
603
|
122
|
|
|
|
|
|
return; |
604
|
|
|
|
|
|
|
|
605
|
122
|
100
|
|
|
|
|
for (i = 0; i < git_array_size(fl->filters); ++i) { |
606
|
63
|
50
|
|
|
|
|
git_filter_entry *fe = git_array_get(fl->filters, i); |
607
|
63
|
100
|
|
|
|
|
if (fe->filter->cleanup) |
608
|
60
|
|
|
|
|
|
fe->filter->cleanup(fe->filter, fe->payload); |
609
|
|
|
|
|
|
|
} |
610
|
|
|
|
|
|
|
|
611
|
59
|
|
|
|
|
|
git_array_clear(fl->filters); |
612
|
59
|
|
|
|
|
|
git__free(fl); |
613
|
|
|
|
|
|
|
} |
614
|
|
|
|
|
|
|
|
615
|
0
|
|
|
|
|
|
int git_filter_list_contains( |
616
|
|
|
|
|
|
|
git_filter_list *fl, |
617
|
|
|
|
|
|
|
const char *name) |
618
|
|
|
|
|
|
|
{ |
619
|
|
|
|
|
|
|
size_t i; |
620
|
|
|
|
|
|
|
|
621
|
0
|
0
|
|
|
|
|
assert(name); |
622
|
|
|
|
|
|
|
|
623
|
0
|
0
|
|
|
|
|
if (!fl) |
624
|
0
|
|
|
|
|
|
return 0; |
625
|
|
|
|
|
|
|
|
626
|
0
|
0
|
|
|
|
|
for (i = 0; i < fl->filters.size; i++) { |
627
|
0
|
0
|
|
|
|
|
if (strcmp(fl->filters.ptr[i].filter_name, name) == 0) |
628
|
0
|
|
|
|
|
|
return 1; |
629
|
|
|
|
|
|
|
} |
630
|
|
|
|
|
|
|
|
631
|
0
|
|
|
|
|
|
return 0; |
632
|
|
|
|
|
|
|
} |
633
|
|
|
|
|
|
|
|
634
|
0
|
|
|
|
|
|
int git_filter_list_push( |
635
|
|
|
|
|
|
|
git_filter_list *fl, git_filter *filter, void *payload) |
636
|
|
|
|
|
|
|
{ |
637
|
0
|
|
|
|
|
|
int error = 0; |
638
|
|
|
|
|
|
|
size_t pos; |
639
|
0
|
|
|
|
|
|
git_filter_def *fdef = NULL; |
640
|
|
|
|
|
|
|
git_filter_entry *fe; |
641
|
|
|
|
|
|
|
|
642
|
0
|
0
|
|
|
|
|
assert(fl && filter); |
|
|
0
|
|
|
|
|
|
643
|
|
|
|
|
|
|
|
644
|
|
|
|
|
|
|
if (git_rwlock_rdlock(&filter_registry.lock) < 0) { |
645
|
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "failed to lock filter registry"); |
646
|
|
|
|
|
|
|
return -1; |
647
|
|
|
|
|
|
|
} |
648
|
|
|
|
|
|
|
|
649
|
0
|
0
|
|
|
|
|
if (git_vector_search2( |
650
|
|
|
|
|
|
|
&pos, &filter_registry.filters, |
651
|
|
|
|
|
|
|
filter_def_filter_key_check, filter) == 0) |
652
|
0
|
|
|
|
|
|
fdef = git_vector_get(&filter_registry.filters, pos); |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
git_rwlock_rdunlock(&filter_registry.lock); |
655
|
|
|
|
|
|
|
|
656
|
0
|
0
|
|
|
|
|
if (fdef == NULL) { |
657
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_FILTER, "cannot use an unregistered filter"); |
658
|
0
|
|
|
|
|
|
return -1; |
659
|
|
|
|
|
|
|
} |
660
|
|
|
|
|
|
|
|
661
|
0
|
0
|
|
|
|
|
if (!fdef->initialized && (error = filter_initialize(fdef)) < 0) |
|
|
0
|
|
|
|
|
|
662
|
0
|
|
|
|
|
|
return error; |
663
|
|
|
|
|
|
|
|
664
|
0
|
0
|
|
|
|
|
fe = git_array_alloc(fl->filters); |
|
|
0
|
|
|
|
|
|
665
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(fe); |
666
|
0
|
|
|
|
|
|
fe->filter = filter; |
667
|
0
|
|
|
|
|
|
fe->payload = payload; |
668
|
|
|
|
|
|
|
|
669
|
0
|
|
|
|
|
|
return 0; |
670
|
|
|
|
|
|
|
} |
671
|
|
|
|
|
|
|
|
672
|
0
|
|
|
|
|
|
size_t git_filter_list_length(const git_filter_list *fl) |
673
|
|
|
|
|
|
|
{ |
674
|
0
|
0
|
|
|
|
|
return fl ? git_array_size(fl->filters) : 0; |
675
|
|
|
|
|
|
|
} |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
struct buf_stream { |
678
|
|
|
|
|
|
|
git_writestream parent; |
679
|
|
|
|
|
|
|
git_buf *target; |
680
|
|
|
|
|
|
|
bool complete; |
681
|
|
|
|
|
|
|
}; |
682
|
|
|
|
|
|
|
|
683
|
51
|
|
|
|
|
|
static int buf_stream_write( |
684
|
|
|
|
|
|
|
git_writestream *s, const char *buffer, size_t len) |
685
|
|
|
|
|
|
|
{ |
686
|
51
|
|
|
|
|
|
struct buf_stream *buf_stream = (struct buf_stream *)s; |
687
|
51
|
50
|
|
|
|
|
assert(buf_stream); |
688
|
|
|
|
|
|
|
|
689
|
51
|
50
|
|
|
|
|
assert(buf_stream->complete == 0); |
690
|
|
|
|
|
|
|
|
691
|
51
|
|
|
|
|
|
return git_buf_put(buf_stream->target, buffer, len); |
692
|
|
|
|
|
|
|
} |
693
|
|
|
|
|
|
|
|
694
|
51
|
|
|
|
|
|
static int buf_stream_close(git_writestream *s) |
695
|
|
|
|
|
|
|
{ |
696
|
51
|
|
|
|
|
|
struct buf_stream *buf_stream = (struct buf_stream *)s; |
697
|
51
|
50
|
|
|
|
|
assert(buf_stream); |
698
|
|
|
|
|
|
|
|
699
|
51
|
50
|
|
|
|
|
assert(buf_stream->complete == 0); |
700
|
51
|
|
|
|
|
|
buf_stream->complete = 1; |
701
|
|
|
|
|
|
|
|
702
|
51
|
|
|
|
|
|
return 0; |
703
|
|
|
|
|
|
|
} |
704
|
|
|
|
|
|
|
|
705
|
0
|
|
|
|
|
|
static void buf_stream_free(git_writestream *s) |
706
|
|
|
|
|
|
|
{ |
707
|
|
|
|
|
|
|
GIT_UNUSED(s); |
708
|
0
|
|
|
|
|
|
} |
709
|
|
|
|
|
|
|
|
710
|
51
|
|
|
|
|
|
static void buf_stream_init(struct buf_stream *writer, git_buf *target) |
711
|
|
|
|
|
|
|
{ |
712
|
51
|
|
|
|
|
|
memset(writer, 0, sizeof(struct buf_stream)); |
713
|
|
|
|
|
|
|
|
714
|
51
|
|
|
|
|
|
writer->parent.write = buf_stream_write; |
715
|
51
|
|
|
|
|
|
writer->parent.close = buf_stream_close; |
716
|
51
|
|
|
|
|
|
writer->parent.free = buf_stream_free; |
717
|
51
|
|
|
|
|
|
writer->target = target; |
718
|
|
|
|
|
|
|
|
719
|
51
|
|
|
|
|
|
git_buf_clear(target); |
720
|
51
|
|
|
|
|
|
} |
721
|
|
|
|
|
|
|
|
722
|
32
|
|
|
|
|
|
int git_filter_list_apply_to_data( |
723
|
|
|
|
|
|
|
git_buf *tgt, git_filter_list *filters, git_buf *src) |
724
|
|
|
|
|
|
|
{ |
725
|
|
|
|
|
|
|
struct buf_stream writer; |
726
|
|
|
|
|
|
|
int error; |
727
|
|
|
|
|
|
|
|
728
|
32
|
|
|
|
|
|
git_buf_sanitize(tgt); |
729
|
32
|
|
|
|
|
|
git_buf_sanitize(src); |
730
|
|
|
|
|
|
|
|
731
|
32
|
100
|
|
|
|
|
if (!filters) { |
732
|
2
|
|
|
|
|
|
git_buf_attach_notowned(tgt, src->ptr, src->size); |
733
|
2
|
|
|
|
|
|
return 0; |
734
|
|
|
|
|
|
|
} |
735
|
|
|
|
|
|
|
|
736
|
30
|
|
|
|
|
|
buf_stream_init(&writer, tgt); |
737
|
|
|
|
|
|
|
|
738
|
30
|
50
|
|
|
|
|
if ((error = git_filter_list_stream_data(filters, src, |
739
|
|
|
|
|
|
|
&writer.parent)) < 0) |
740
|
0
|
|
|
|
|
|
return error; |
741
|
|
|
|
|
|
|
|
742
|
30
|
50
|
|
|
|
|
assert(writer.complete); |
743
|
32
|
|
|
|
|
|
return error; |
744
|
|
|
|
|
|
|
} |
745
|
|
|
|
|
|
|
|
746
|
19
|
|
|
|
|
|
int git_filter_list_apply_to_file( |
747
|
|
|
|
|
|
|
git_buf *out, |
748
|
|
|
|
|
|
|
git_filter_list *filters, |
749
|
|
|
|
|
|
|
git_repository *repo, |
750
|
|
|
|
|
|
|
const char *path) |
751
|
|
|
|
|
|
|
{ |
752
|
|
|
|
|
|
|
struct buf_stream writer; |
753
|
|
|
|
|
|
|
int error; |
754
|
|
|
|
|
|
|
|
755
|
19
|
|
|
|
|
|
buf_stream_init(&writer, out); |
756
|
|
|
|
|
|
|
|
757
|
19
|
100
|
|
|
|
|
if ((error = git_filter_list_stream_file( |
758
|
|
|
|
|
|
|
filters, repo, path, &writer.parent)) < 0) |
759
|
2
|
|
|
|
|
|
return error; |
760
|
|
|
|
|
|
|
|
761
|
17
|
50
|
|
|
|
|
assert(writer.complete); |
762
|
19
|
|
|
|
|
|
return error; |
763
|
|
|
|
|
|
|
} |
764
|
|
|
|
|
|
|
|
765
|
33
|
|
|
|
|
|
static int buf_from_blob(git_buf *out, git_blob *blob) |
766
|
|
|
|
|
|
|
{ |
767
|
33
|
|
|
|
|
|
git_object_size_t rawsize = git_blob_rawsize(blob); |
768
|
|
|
|
|
|
|
|
769
|
33
|
50
|
|
|
|
|
if (!git__is_sizet(rawsize)) { |
770
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OS, "blob is too large to filter"); |
771
|
0
|
|
|
|
|
|
return -1; |
772
|
|
|
|
|
|
|
} |
773
|
|
|
|
|
|
|
|
774
|
33
|
|
|
|
|
|
git_buf_attach_notowned(out, git_blob_rawcontent(blob), (size_t)rawsize); |
775
|
33
|
|
|
|
|
|
return 0; |
776
|
|
|
|
|
|
|
} |
777
|
|
|
|
|
|
|
|
778
|
2
|
|
|
|
|
|
int git_filter_list_apply_to_blob( |
779
|
|
|
|
|
|
|
git_buf *out, |
780
|
|
|
|
|
|
|
git_filter_list *filters, |
781
|
|
|
|
|
|
|
git_blob *blob) |
782
|
|
|
|
|
|
|
{ |
783
|
|
|
|
|
|
|
struct buf_stream writer; |
784
|
|
|
|
|
|
|
int error; |
785
|
|
|
|
|
|
|
|
786
|
2
|
|
|
|
|
|
buf_stream_init(&writer, out); |
787
|
|
|
|
|
|
|
|
788
|
2
|
50
|
|
|
|
|
if ((error = git_filter_list_stream_blob( |
789
|
|
|
|
|
|
|
filters, blob, &writer.parent)) < 0) |
790
|
0
|
|
|
|
|
|
return error; |
791
|
|
|
|
|
|
|
|
792
|
2
|
50
|
|
|
|
|
assert(writer.complete); |
793
|
2
|
|
|
|
|
|
return error; |
794
|
|
|
|
|
|
|
} |
795
|
|
|
|
|
|
|
|
796
|
|
|
|
|
|
|
struct proxy_stream { |
797
|
|
|
|
|
|
|
git_writestream parent; |
798
|
|
|
|
|
|
|
git_filter *filter; |
799
|
|
|
|
|
|
|
const git_filter_source *source; |
800
|
|
|
|
|
|
|
void **payload; |
801
|
|
|
|
|
|
|
git_buf input; |
802
|
|
|
|
|
|
|
git_buf temp_buf; |
803
|
|
|
|
|
|
|
git_buf *output; |
804
|
|
|
|
|
|
|
git_writestream *target; |
805
|
|
|
|
|
|
|
}; |
806
|
|
|
|
|
|
|
|
807
|
65
|
|
|
|
|
|
static int proxy_stream_write( |
808
|
|
|
|
|
|
|
git_writestream *s, const char *buffer, size_t len) |
809
|
|
|
|
|
|
|
{ |
810
|
65
|
|
|
|
|
|
struct proxy_stream *proxy_stream = (struct proxy_stream *)s; |
811
|
65
|
50
|
|
|
|
|
assert(proxy_stream); |
812
|
|
|
|
|
|
|
|
813
|
65
|
|
|
|
|
|
return git_buf_put(&proxy_stream->input, buffer, len); |
814
|
|
|
|
|
|
|
} |
815
|
|
|
|
|
|
|
|
816
|
67
|
|
|
|
|
|
static int proxy_stream_close(git_writestream *s) |
817
|
|
|
|
|
|
|
{ |
818
|
67
|
|
|
|
|
|
struct proxy_stream *proxy_stream = (struct proxy_stream *)s; |
819
|
|
|
|
|
|
|
git_buf *writebuf; |
820
|
67
|
|
|
|
|
|
git_error_state error_state = {0}; |
821
|
|
|
|
|
|
|
int error; |
822
|
|
|
|
|
|
|
|
823
|
67
|
50
|
|
|
|
|
assert(proxy_stream); |
824
|
|
|
|
|
|
|
|
825
|
67
|
|
|
|
|
|
error = proxy_stream->filter->apply( |
826
|
|
|
|
|
|
|
proxy_stream->filter, |
827
|
|
|
|
|
|
|
proxy_stream->payload, |
828
|
|
|
|
|
|
|
proxy_stream->output, |
829
|
67
|
|
|
|
|
|
&proxy_stream->input, |
830
|
|
|
|
|
|
|
proxy_stream->source); |
831
|
|
|
|
|
|
|
|
832
|
67
|
100
|
|
|
|
|
if (error == GIT_PASSTHROUGH) { |
833
|
57
|
|
|
|
|
|
writebuf = &proxy_stream->input; |
834
|
10
|
100
|
|
|
|
|
} else if (error == 0) { |
835
|
8
|
|
|
|
|
|
git_buf_sanitize(proxy_stream->output); |
836
|
8
|
|
|
|
|
|
writebuf = proxy_stream->output; |
837
|
|
|
|
|
|
|
} else { |
838
|
|
|
|
|
|
|
/* close stream before erroring out taking care |
839
|
|
|
|
|
|
|
* to preserve the original error */ |
840
|
2
|
|
|
|
|
|
git_error_state_capture(&error_state, error); |
841
|
2
|
|
|
|
|
|
proxy_stream->target->close(proxy_stream->target); |
842
|
2
|
|
|
|
|
|
git_error_state_restore(&error_state); |
843
|
2
|
|
|
|
|
|
return error; |
844
|
|
|
|
|
|
|
} |
845
|
|
|
|
|
|
|
|
846
|
65
|
50
|
|
|
|
|
if ((error = proxy_stream->target->write( |
847
|
65
|
|
|
|
|
|
proxy_stream->target, writebuf->ptr, writebuf->size)) == 0) |
848
|
65
|
|
|
|
|
|
error = proxy_stream->target->close(proxy_stream->target); |
849
|
|
|
|
|
|
|
|
850
|
67
|
|
|
|
|
|
return error; |
851
|
|
|
|
|
|
|
} |
852
|
|
|
|
|
|
|
|
853
|
67
|
|
|
|
|
|
static void proxy_stream_free(git_writestream *s) |
854
|
|
|
|
|
|
|
{ |
855
|
67
|
|
|
|
|
|
struct proxy_stream *proxy_stream = (struct proxy_stream *)s; |
856
|
67
|
50
|
|
|
|
|
assert(proxy_stream); |
857
|
|
|
|
|
|
|
|
858
|
67
|
|
|
|
|
|
git_buf_dispose(&proxy_stream->input); |
859
|
67
|
|
|
|
|
|
git_buf_dispose(&proxy_stream->temp_buf); |
860
|
67
|
|
|
|
|
|
git__free(proxy_stream); |
861
|
67
|
|
|
|
|
|
} |
862
|
|
|
|
|
|
|
|
863
|
67
|
|
|
|
|
|
static int proxy_stream_init( |
864
|
|
|
|
|
|
|
git_writestream **out, |
865
|
|
|
|
|
|
|
git_filter *filter, |
866
|
|
|
|
|
|
|
git_buf *temp_buf, |
867
|
|
|
|
|
|
|
void **payload, |
868
|
|
|
|
|
|
|
const git_filter_source *source, |
869
|
|
|
|
|
|
|
git_writestream *target) |
870
|
|
|
|
|
|
|
{ |
871
|
67
|
|
|
|
|
|
struct proxy_stream *proxy_stream = git__calloc(1, sizeof(struct proxy_stream)); |
872
|
67
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(proxy_stream); |
873
|
|
|
|
|
|
|
|
874
|
67
|
|
|
|
|
|
proxy_stream->parent.write = proxy_stream_write; |
875
|
67
|
|
|
|
|
|
proxy_stream->parent.close = proxy_stream_close; |
876
|
67
|
|
|
|
|
|
proxy_stream->parent.free = proxy_stream_free; |
877
|
67
|
|
|
|
|
|
proxy_stream->filter = filter; |
878
|
67
|
|
|
|
|
|
proxy_stream->payload = payload; |
879
|
67
|
|
|
|
|
|
proxy_stream->source = source; |
880
|
67
|
|
|
|
|
|
proxy_stream->target = target; |
881
|
67
|
100
|
|
|
|
|
proxy_stream->output = temp_buf ? temp_buf : &proxy_stream->temp_buf; |
882
|
|
|
|
|
|
|
|
883
|
67
|
100
|
|
|
|
|
if (temp_buf) |
884
|
14
|
|
|
|
|
|
git_buf_clear(temp_buf); |
885
|
|
|
|
|
|
|
|
886
|
67
|
|
|
|
|
|
*out = (git_writestream *)proxy_stream; |
887
|
67
|
|
|
|
|
|
return 0; |
888
|
|
|
|
|
|
|
} |
889
|
|
|
|
|
|
|
|
890
|
82
|
|
|
|
|
|
static int stream_list_init( |
891
|
|
|
|
|
|
|
git_writestream **out, |
892
|
|
|
|
|
|
|
git_vector *streams, |
893
|
|
|
|
|
|
|
git_filter_list *filters, |
894
|
|
|
|
|
|
|
git_writestream *target) |
895
|
|
|
|
|
|
|
{ |
896
|
82
|
|
|
|
|
|
git_writestream *last_stream = target; |
897
|
|
|
|
|
|
|
size_t i; |
898
|
82
|
|
|
|
|
|
int error = 0; |
899
|
|
|
|
|
|
|
|
900
|
82
|
|
|
|
|
|
*out = NULL; |
901
|
|
|
|
|
|
|
|
902
|
82
|
100
|
|
|
|
|
if (!filters) { |
903
|
19
|
|
|
|
|
|
*out = target; |
904
|
19
|
|
|
|
|
|
return 0; |
905
|
|
|
|
|
|
|
} |
906
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
/* Create filters last to first to get the chaining direction */ |
908
|
130
|
100
|
|
|
|
|
for (i = 0; i < git_array_size(filters->filters); ++i) { |
909
|
134
|
|
|
|
|
|
size_t filter_idx = (filters->source.mode == GIT_FILTER_TO_WORKTREE) ? |
910
|
67
|
100
|
|
|
|
|
git_array_size(filters->filters) - 1 - i : i; |
911
|
67
|
50
|
|
|
|
|
git_filter_entry *fe = git_array_get(filters->filters, filter_idx); |
912
|
|
|
|
|
|
|
git_writestream *filter_stream; |
913
|
|
|
|
|
|
|
|
914
|
67
|
50
|
|
|
|
|
assert(fe->filter->stream || fe->filter->apply); |
|
|
50
|
|
|
|
|
|
915
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
/* If necessary, create a stream that proxies the traditional |
917
|
|
|
|
|
|
|
* application. |
918
|
|
|
|
|
|
|
*/ |
919
|
67
|
50
|
|
|
|
|
if (fe->filter->stream) |
920
|
0
|
|
|
|
|
|
error = fe->filter->stream(&filter_stream, fe->filter, |
921
|
0
|
|
|
|
|
|
&fe->payload, &filters->source, last_stream); |
922
|
|
|
|
|
|
|
else |
923
|
|
|
|
|
|
|
/* Create a stream that proxies the one-shot apply */ |
924
|
67
|
|
|
|
|
|
error = proxy_stream_init(&filter_stream, fe->filter, |
925
|
67
|
|
|
|
|
|
filters->temp_buf, &fe->payload, &filters->source, |
926
|
|
|
|
|
|
|
last_stream); |
927
|
|
|
|
|
|
|
|
928
|
67
|
50
|
|
|
|
|
if (error < 0) |
929
|
0
|
|
|
|
|
|
goto out; |
930
|
|
|
|
|
|
|
|
931
|
67
|
|
|
|
|
|
git_vector_insert(streams, filter_stream); |
932
|
67
|
|
|
|
|
|
last_stream = filter_stream; |
933
|
|
|
|
|
|
|
} |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
out: |
936
|
63
|
50
|
|
|
|
|
if (error) |
937
|
0
|
|
|
|
|
|
last_stream->close(last_stream); |
938
|
|
|
|
|
|
|
else |
939
|
63
|
|
|
|
|
|
*out = last_stream; |
940
|
|
|
|
|
|
|
|
941
|
63
|
|
|
|
|
|
return error; |
942
|
|
|
|
|
|
|
} |
943
|
|
|
|
|
|
|
|
944
|
82
|
|
|
|
|
|
void stream_list_free(git_vector *streams) |
945
|
|
|
|
|
|
|
{ |
946
|
|
|
|
|
|
|
git_writestream *stream; |
947
|
|
|
|
|
|
|
size_t i; |
948
|
|
|
|
|
|
|
|
949
|
149
|
100
|
|
|
|
|
git_vector_foreach(streams, i, stream) |
950
|
67
|
|
|
|
|
|
stream->free(stream); |
951
|
82
|
|
|
|
|
|
git_vector_free(streams); |
952
|
82
|
|
|
|
|
|
} |
953
|
|
|
|
|
|
|
|
954
|
19
|
|
|
|
|
|
int git_filter_list_stream_file( |
955
|
|
|
|
|
|
|
git_filter_list *filters, |
956
|
|
|
|
|
|
|
git_repository *repo, |
957
|
|
|
|
|
|
|
const char *path, |
958
|
|
|
|
|
|
|
git_writestream *target) |
959
|
|
|
|
|
|
|
{ |
960
|
|
|
|
|
|
|
char buf[FILTERIO_BUFSIZE]; |
961
|
19
|
|
|
|
|
|
git_buf abspath = GIT_BUF_INIT; |
962
|
19
|
100
|
|
|
|
|
const char *base = repo ? git_repository_workdir(repo) : NULL; |
963
|
19
|
|
|
|
|
|
git_vector filter_streams = GIT_VECTOR_INIT; |
964
|
|
|
|
|
|
|
git_writestream *stream_start; |
965
|
|
|
|
|
|
|
ssize_t readlen; |
966
|
19
|
|
|
|
|
|
int fd = -1, error, initialized = 0; |
967
|
|
|
|
|
|
|
|
968
|
19
|
50
|
|
|
|
|
if ((error = stream_list_init( |
969
|
19
|
50
|
|
|
|
|
&stream_start, &filter_streams, filters, target)) < 0 || |
970
|
|
|
|
|
|
|
(error = git_path_join_unrooted(&abspath, path, base, NULL)) < 0) |
971
|
|
|
|
|
|
|
goto done; |
972
|
19
|
|
|
|
|
|
initialized = 1; |
973
|
|
|
|
|
|
|
|
974
|
19
|
50
|
|
|
|
|
if ((fd = git_futils_open_ro(abspath.ptr)) < 0) { |
975
|
0
|
|
|
|
|
|
error = fd; |
976
|
0
|
|
|
|
|
|
goto done; |
977
|
|
|
|
|
|
|
} |
978
|
|
|
|
|
|
|
|
979
|
38
|
100
|
|
|
|
|
while ((readlen = p_read(fd, buf, sizeof(buf))) > 0) { |
980
|
19
|
50
|
|
|
|
|
if ((error = stream_start->write(stream_start, buf, readlen)) < 0) |
981
|
0
|
|
|
|
|
|
goto done; |
982
|
|
|
|
|
|
|
} |
983
|
|
|
|
|
|
|
|
984
|
19
|
50
|
|
|
|
|
if (readlen < 0) |
985
|
0
|
|
|
|
|
|
error = -1; |
986
|
|
|
|
|
|
|
|
987
|
|
|
|
|
|
|
done: |
988
|
19
|
50
|
|
|
|
|
if (initialized) |
989
|
19
|
|
|
|
|
|
error |= stream_start->close(stream_start); |
990
|
|
|
|
|
|
|
|
991
|
19
|
50
|
|
|
|
|
if (fd >= 0) |
992
|
19
|
|
|
|
|
|
p_close(fd); |
993
|
19
|
|
|
|
|
|
stream_list_free(&filter_streams); |
994
|
19
|
|
|
|
|
|
git_buf_dispose(&abspath); |
995
|
19
|
|
|
|
|
|
return error; |
996
|
|
|
|
|
|
|
} |
997
|
|
|
|
|
|
|
|
998
|
63
|
|
|
|
|
|
int git_filter_list_stream_data( |
999
|
|
|
|
|
|
|
git_filter_list *filters, |
1000
|
|
|
|
|
|
|
git_buf *data, |
1001
|
|
|
|
|
|
|
git_writestream *target) |
1002
|
|
|
|
|
|
|
{ |
1003
|
63
|
|
|
|
|
|
git_vector filter_streams = GIT_VECTOR_INIT; |
1004
|
|
|
|
|
|
|
git_writestream *stream_start; |
1005
|
63
|
|
|
|
|
|
int error, initialized = 0; |
1006
|
|
|
|
|
|
|
|
1007
|
63
|
|
|
|
|
|
git_buf_sanitize(data); |
1008
|
|
|
|
|
|
|
|
1009
|
63
|
50
|
|
|
|
|
if ((error = stream_list_init(&stream_start, &filter_streams, filters, target)) < 0) |
1010
|
0
|
|
|
|
|
|
goto out; |
1011
|
63
|
|
|
|
|
|
initialized = 1; |
1012
|
|
|
|
|
|
|
|
1013
|
63
|
50
|
|
|
|
|
if ((error = stream_start->write( |
1014
|
63
|
|
|
|
|
|
stream_start, data->ptr, data->size)) < 0) |
1015
|
0
|
|
|
|
|
|
goto out; |
1016
|
|
|
|
|
|
|
|
1017
|
|
|
|
|
|
|
out: |
1018
|
63
|
50
|
|
|
|
|
if (initialized) |
1019
|
63
|
|
|
|
|
|
error |= stream_start->close(stream_start); |
1020
|
|
|
|
|
|
|
|
1021
|
63
|
|
|
|
|
|
stream_list_free(&filter_streams); |
1022
|
63
|
|
|
|
|
|
return error; |
1023
|
|
|
|
|
|
|
} |
1024
|
|
|
|
|
|
|
|
1025
|
33
|
|
|
|
|
|
int git_filter_list_stream_blob( |
1026
|
|
|
|
|
|
|
git_filter_list *filters, |
1027
|
|
|
|
|
|
|
git_blob *blob, |
1028
|
|
|
|
|
|
|
git_writestream *target) |
1029
|
|
|
|
|
|
|
{ |
1030
|
33
|
|
|
|
|
|
git_buf in = GIT_BUF_INIT; |
1031
|
|
|
|
|
|
|
|
1032
|
33
|
50
|
|
|
|
|
if (buf_from_blob(&in, blob) < 0) |
1033
|
0
|
|
|
|
|
|
return -1; |
1034
|
|
|
|
|
|
|
|
1035
|
33
|
100
|
|
|
|
|
if (filters) |
1036
|
14
|
|
|
|
|
|
git_oid_cpy(&filters->source.oid, git_blob_id(blob)); |
1037
|
|
|
|
|
|
|
|
1038
|
33
|
|
|
|
|
|
return git_filter_list_stream_data(filters, &in, target); |
1039
|
|
|
|
|
|
|
} |
1040
|
|
|
|
|
|
|
|
1041
|
0
|
|
|
|
|
|
int git_filter_init(git_filter *filter, unsigned int version) |
1042
|
|
|
|
|
|
|
{ |
1043
|
0
|
0
|
|
|
|
|
GIT_INIT_STRUCTURE_FROM_TEMPLATE(filter, version, git_filter, GIT_FILTER_INIT); |
1044
|
0
|
|
|
|
|
|
return 0; |
1045
|
|
|
|
|
|
|
} |