deps/libgit2/src/libgit2/crlf.c

filter: Statementstmt 65.5% 122 / 186 Branchbran 43.5% 67 / 154 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 55.5% 189 / 340 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« deps/libgit2/src/libgit2/status.c ↑ Summary deps/libgit2/src/libgit2/diff.c »
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 "common.h"
9  
10 #include "git2/attr.h"
11 #include "git2/blob.h"
12 #include "git2/index.h"
13 #include "git2/sys/filter.h"
14  
15 #include "buf.h"
16 #include "futils.h"
17 #include "hash.h"
18 #include "filter.h"
19 #include "repository.h"
20  
21 typedef enum {
22 GIT_CRLF_UNDEFINED,
23 GIT_CRLF_BINARY,
24 GIT_CRLF_TEXT,
25 GIT_CRLF_TEXT_INPUT,
26 GIT_CRLF_TEXT_CRLF,
27 GIT_CRLF_AUTO,
28 GIT_CRLF_AUTO_INPUT,
29 GIT_CRLF_AUTO_CRLF
30 } git_crlf_t;
31  
32 struct crlf_attrs {
33 int attr_action; /* the .gitattributes setting */
34 int crlf_action; /* the core.autocrlf setting */
35  
36 int auto_crlf;
37 int safe_crlf;
38 int core_eol;
39 };
40  
41 struct crlf_filter {
42 git_filter f;
43 };
44  
45 538 static git_crlf_t check_crlf(const char *value)
46 {
47 538 ▶ if (GIT_ATTR_IS_TRUE(value))
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_ATTR_IS_TRUE(value)) 0 538 538
48 0 return GIT_CRLF_TEXT;
49 538 ▶ else if (GIT_ATTR_IS_FALSE(value))
Branch: 1 branch
BranchTrueFalseTotal
else if (GIT_ATTR_IS_FALSE(value)) 0 538 538
50 0 return GIT_CRLF_BINARY;
51 538 ▶ else if (GIT_ATTR_IS_UNSPECIFIED(value))
Branch: 1 branch
BranchTrueFalseTotal
else if (GIT_ATTR_IS_UNSPECIFIED(value)) 0 538 538
52 ;
53 0 ▶ else if (strcmp(value, "input") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(value, "input") == 0) 0 0 0
54 0 return GIT_CRLF_TEXT_INPUT;
55 0 ▶ else if (strcmp(value, "auto") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(value, "auto") == 0) 0 0 0
56 0 return GIT_CRLF_AUTO;
57  
58 538 return GIT_CRLF_UNDEFINED;
59 }
60  
61 269 static git_configmap_value check_eol(const char *value)
62 {
63 269 ▶ if (GIT_ATTR_IS_UNSPECIFIED(value))
Branch: 1 branch
BranchTrueFalseTotal
if (GIT_ATTR_IS_UNSPECIFIED(value)) 0 269 269
64 ;
65 0 ▶ else if (strcmp(value, "lf") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(value, "lf") == 0) 0 0 0
66 0 return GIT_EOL_LF;
67 0 ▶ else if (strcmp(value, "crlf") == 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(value, "crlf") == 0) 0 0 0
68 0 return GIT_EOL_CRLF;
69  
70 269 return GIT_EOL_UNSET;
71 }
72  
73 57 static int has_cr_in_index(const git_filter_source *src)
74 {
75 57 git_repository *repo = git_filter_source_repo(src);
76 57 const char *path = git_filter_source_path(src);
77 git_index *index;
78 const git_index_entry *entry;
79 git_blob *blob;
80 const void *blobcontent;
81 git_object_size_t blobsize;
82 bool found_cr;
83  
84 57 ▶ if (!path)
Branch: 1 branch
BranchTrueFalseTotal
if (!path) 0 57 57
85 0 return false;
86  
87 57 ▶ if (git_repository_index__weakptr(&index, repo) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (git_repository_index__weakptr(&index, repo) < 0) { 0 57 57
88 0 git_error_clear();
89 0 return false;
90 }
91  
92 68 if (!(entry = git_index_get_bypath(index, path, 0)) &&
93 11 !(entry = git_index_get_bypath(index, path, 1)))
94 10 return false;
95  
96 47 ▶ if (!S_ISREG(entry->mode)) /* don't crlf filter non-blobs */
Branch: 1 branch
BranchTrueFalseTotal
if (!S_ISREG(entry->mode)) /* don't crlf filter non-blobs */ 0 47 47
97 0 return true;
98  
99 47 ▶ if (git_blob_lookup(&blob, repo, &entry->id) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (git_blob_lookup(&blob, repo, &entry->id) < 0) 0 47 47
100 0 return false;
101  
102 47 blobcontent = git_blob_rawcontent(blob);
103 47 blobsize = git_blob_rawsize(blob);
104 47 ▶ if (!git__is_sizet(blobsize))
Branch: 1 branch
BranchTrueFalseTotal
if (!git__is_sizet(blobsize)) 0 47 47
105 0 blobsize = (size_t)-1;
106  
107 47 ▶ found_cr = (blobcontent != NULL &&
Branch: 1 branch
BranchTrueFalseTotal
found_cr = (blobcontent != NULL && 47 0 47
108 94 ▶ blobsize > 0 &&
Branch: 1 branch
BranchTrueFalseTotal
blobsize > 0 && 47 0 47
109 47 ▶ memchr(blobcontent, '\r', (size_t)blobsize) != NULL);
Branch: 1 branch
BranchTrueFalseTotal
memchr(blobcontent, '\r', (size_t)blobsize) != NULL); 0 47 47
110  
111 47 git_blob_free(blob);
112 47 return found_cr;
113 }
114  
115 0 static int text_eol_is_crlf(struct crlf_attrs *ca)
116 {
117 0 ▶ if (ca->auto_crlf == GIT_AUTO_CRLF_TRUE)
Branch: 1 branch
BranchTrueFalseTotal
if (ca->auto_crlf == GIT_AUTO_CRLF_TRUE) 0 0 0
118 0 return 1;
119 0 ▶ else if (ca->auto_crlf == GIT_AUTO_CRLF_INPUT)
Branch: 1 branch
BranchTrueFalseTotal
else if (ca->auto_crlf == GIT_AUTO_CRLF_INPUT) 0 0 0
120 0 return 0;
121  
122 0 ▶ if (ca->core_eol == GIT_EOL_CRLF)
Branch: 1 branch
BranchTrueFalseTotal
if (ca->core_eol == GIT_EOL_CRLF) 0 0 0
123 0 return 1;
124 if (ca->core_eol == GIT_EOL_UNSET && GIT_EOL_NATIVE == GIT_EOL_CRLF)
125 return 1;
126  
127 0 return 0;
128 }
129  
130 16 static git_configmap_value output_eol(struct crlf_attrs *ca)
131 {
132 16 switch (ca->crlf_action) {
133 0 case GIT_CRLF_BINARY:
134 0 return GIT_EOL_UNSET;
135 0 case GIT_CRLF_TEXT_CRLF:
136 0 return GIT_EOL_CRLF;
137 0 case GIT_CRLF_TEXT_INPUT:
138 0 return GIT_EOL_LF;
139 6 case GIT_CRLF_UNDEFINED:
140 case GIT_CRLF_AUTO_CRLF:
141 6 return GIT_EOL_CRLF;
142 10 case GIT_CRLF_AUTO_INPUT:
143 10 return GIT_EOL_LF;
144 0 case GIT_CRLF_TEXT:
145 case GIT_CRLF_AUTO:
146 0 ▶ return text_eol_is_crlf(ca) ? GIT_EOL_CRLF : GIT_EOL_LF;
Branch: 1 branch
BranchTrueFalseTotal
return text_eol_is_crlf(ca) ? GIT_EOL_CRLF : GIT_EOL_LF; 0 0 0
147 }
148  
149 /* TODO: warn when available */
150 0 return ca->core_eol;
151 }
152  
153 57 GIT_INLINE(int) check_safecrlf(
154 struct crlf_attrs *ca,
155 const git_filter_source *src,
156 git_str_text_stats *stats)
157 {
158 57 const char *filename = git_filter_source_path(src);
159  
160 57 ▶ if (!ca->safe_crlf)
Branch: 1 branch
BranchTrueFalseTotal
if (!ca->safe_crlf) 57 0 57
161 57 return 0;
162  
163 0 ▶ if (output_eol(ca) == GIT_EOL_LF) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_eol(ca) == GIT_EOL_LF) { 0 0 0
164 /*
165 * CRLFs would not be restored by checkout:
166 * check if we'd remove CRLFs
167 */
168 0 ▶ if (stats->crlf) {
Branch: 1 branch
BranchTrueFalseTotal
if (stats->crlf) { 0 0 0
169 0 ▶ if (ca->safe_crlf == GIT_SAFE_CRLF_WARN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ca->safe_crlf == GIT_SAFE_CRLF_WARN) { 0 0 0
170 /* TODO: issue a warning when available */
171 } else {
172 0 ▶ if (filename && *filename)
Branch: 2 branches
BranchTrueFalseTotal
if (filename && *filename) 0 0 0
if (filename && *filename) 0 0 0
173 0 git_error_set(
174 GIT_ERROR_FILTER, "CRLF would be replaced by LF in '%s'",
175 filename);
176 else
177 0 git_error_set(
178 GIT_ERROR_FILTER, "CRLF would be replaced by LF");
179  
180 0 return -1;
181 }
182 }
183 0 ▶ } else if (output_eol(ca) == GIT_EOL_CRLF) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (output_eol(ca) == GIT_EOL_CRLF) { 0 0 0
184 /*
185 * CRLFs would be added by checkout:
186 * check if we have "naked" LFs
187 */
188 0 ▶ if (stats->crlf != stats->lf) {
Branch: 1 branch
BranchTrueFalseTotal
if (stats->crlf != stats->lf) { 0 0 0
189 0 ▶ if (ca->safe_crlf == GIT_SAFE_CRLF_WARN) {
Branch: 1 branch
BranchTrueFalseTotal
if (ca->safe_crlf == GIT_SAFE_CRLF_WARN) { 0 0 0
190 /* TODO: issue a warning when available */
191 } else {
192 0 ▶ if (filename && *filename)
Branch: 2 branches
BranchTrueFalseTotal
if (filename && *filename) 0 0 0
if (filename && *filename) 0 0 0
193 0 git_error_set(
194 GIT_ERROR_FILTER, "LF would be replaced by CRLF in '%s'",
195 filename);
196 else
197 0 git_error_set(
198 GIT_ERROR_FILTER, "LF would be replaced by CRLF");
199  
200 0 return -1;
201 }
202 }
203 }
204  
205 0 return 0;
206 }
207  
208 59 static int crlf_apply_to_odb(
209 struct crlf_attrs *ca,
210 git_str *to,
211 const git_str *from,
212 const git_filter_source *src)
213 {
214 git_str_text_stats stats;
215 bool is_binary;
216 int error;
217  
218 /* Binary attribute? Empty file? Nothing to do */
219 59 ▶ if (ca->crlf_action == GIT_CRLF_BINARY || from->size == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_BINARY || from->size == 0) 59 0 59
if (ca->crlf_action == GIT_CRLF_BINARY || from->size == 0) 2 57 59
220 2 return GIT_PASSTHROUGH;
221  
222 57 is_binary = git_str_gather_text_stats(&stats, from, false);
223  
224 /* Heuristics to see if we can skip the conversion.
225 * Straight from Core Git.
226 */
227 57 ▶ if (ca->crlf_action == GIT_CRLF_AUTO ||
Branch: 1 branch
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_AUTO || 57 0 57
228 57 ▶ ca->crlf_action == GIT_CRLF_AUTO_INPUT ||
Branch: 1 branch
BranchTrueFalseTotal
ca->crlf_action == GIT_CRLF_AUTO_INPUT || 12 45 57
229 12 ▶ ca->crlf_action == GIT_CRLF_AUTO_CRLF) {
Branch: 1 branch
BranchTrueFalseTotal
ca->crlf_action == GIT_CRLF_AUTO_CRLF) { 12 0 12
230  
231 57 ▶ if (is_binary)
Branch: 1 branch
BranchTrueFalseTotal
if (is_binary) 0 57 57
232 0 return GIT_PASSTHROUGH;
233  
234 /*
235 * If the file in the index has any CR in it, do not convert.
236 * This is the new safer autocrlf handling.
237 */
238 57 ▶ if (has_cr_in_index(src))
Branch: 1 branch
BranchTrueFalseTotal
if (has_cr_in_index(src)) 0 57 57
239 0 return GIT_PASSTHROUGH;
240 }
241  
242 57 ▶ if ((error = check_safecrlf(ca, src, &stats)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = check_safecrlf(ca, src, &stats)) < 0) 0 57 57
243 0 return error;
244  
245 /* If there are no CR characters to filter out, then just pass */
246 57 ▶ if (!stats.crlf)
Branch: 1 branch
BranchTrueFalseTotal
if (!stats.crlf) 54 3 57
247 54 return GIT_PASSTHROUGH;
248  
249 /* Actually drop the carriage returns */
250 3 return git_str_crlf_to_lf(to, from);
251 }
252  
253 16 static int crlf_apply_to_workdir(
254 struct crlf_attrs *ca,
255 git_str *to,
256 const git_str *from)
257 {
258 git_str_text_stats stats;
259 bool is_binary;
260  
261 /* Empty file? Nothing to do. */
262 16 ▶ if (git_str_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF)
Branch: 2 branches
BranchTrueFalseTotal
if (git_str_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF) 16 0 16
if (git_str_len(from) == 0 || output_eol(ca) != GIT_EOL_CRLF) 10 6 16
263 10 return GIT_PASSTHROUGH;
264  
265 6 is_binary = git_str_gather_text_stats(&stats, from, false);
266  
267 /* If there are no LFs, or all LFs are part of a CRLF, nothing to do */
268 6 ▶ if (stats.lf == 0 || stats.lf == stats.crlf)
Branch: 2 branches
BranchTrueFalseTotal
if (stats.lf == 0 || stats.lf == stats.crlf) 3 3 6
if (stats.lf == 0 || stats.lf == stats.crlf) 0 3 3
269 3 return GIT_PASSTHROUGH;
270  
271 3 ▶ if (ca->crlf_action == GIT_CRLF_AUTO ||
Branch: 1 branch
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_AUTO || 3 0 3
272 3 ▶ ca->crlf_action == GIT_CRLF_AUTO_INPUT ||
Branch: 1 branch
BranchTrueFalseTotal
ca->crlf_action == GIT_CRLF_AUTO_INPUT || 3 0 3
273 3 ▶ ca->crlf_action == GIT_CRLF_AUTO_CRLF) {
Branch: 1 branch
BranchTrueFalseTotal
ca->crlf_action == GIT_CRLF_AUTO_CRLF) { 3 0 3
274  
275 /* If we have any existing CR or CRLF line endings, do nothing */
276 3 ▶ if (stats.cr > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (stats.cr > 0) 0 3 3
277 0 return GIT_PASSTHROUGH;
278  
279 /* Don't filter binary files */
280 3 ▶ if (is_binary)
Branch: 1 branch
BranchTrueFalseTotal
if (is_binary) 0 3 3
281 0 return GIT_PASSTHROUGH;
282 }
283  
284 3 return git_str_lf_to_crlf(to, from);
285 }
286  
287 269 static int convert_attrs(
288 struct crlf_attrs *ca,
289 const char **attr_values,
290 const git_filter_source *src)
291 {
292 int error;
293  
294 269 memset(ca, 0, sizeof(struct crlf_attrs));
295  
296 269 ▶ if ((error = git_repository__configmap_lookup(&ca->auto_crlf,
Branch: 1 branch
BranchTrueFalseTotal
if ((error = git_repository__configmap_lookup(&ca->auto_crlf, 269 0 269
297 269 ▶ git_filter_source_repo(src), GIT_CONFIGMAP_AUTO_CRLF)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_filter_source_repo(src), GIT_CONFIGMAP_AUTO_CRLF)) < 0 || 269 0 269
298 269 (error = git_repository__configmap_lookup(&ca->safe_crlf,
299 269 ▶ git_filter_source_repo(src), GIT_CONFIGMAP_SAFE_CRLF)) < 0 ||
Branch: 1 branch
BranchTrueFalseTotal
git_filter_source_repo(src), GIT_CONFIGMAP_SAFE_CRLF)) < 0 || 0 269 269
300 269 (error = git_repository__configmap_lookup(&ca->core_eol,
301 git_filter_source_repo(src), GIT_CONFIGMAP_EOL)) < 0)
302 0 return error;
303  
304 /* downgrade FAIL to WARN if ALLOW_UNSAFE option is used */
305 269 ▶ if ((git_filter_source_flags(src) & GIT_FILTER_ALLOW_UNSAFE) &&
Branch: 1 branch
BranchTrueFalseTotal
if ((git_filter_source_flags(src) & GIT_FILTER_ALLOW_UNSAFE) && 169 100 269
306 169 ▶ ca->safe_crlf == GIT_SAFE_CRLF_FAIL)
Branch: 1 branch
BranchTrueFalseTotal
ca->safe_crlf == GIT_SAFE_CRLF_FAIL) 0 169 169
307 0 ca->safe_crlf = GIT_SAFE_CRLF_WARN;
308  
309 269 ▶ if (attr_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (attr_values) { 269 0 269
310 /* load the text attribute */
311 269 ca->crlf_action = check_crlf(attr_values[2]); /* text */
312  
313 269 ▶ if (ca->crlf_action == GIT_CRLF_UNDEFINED)
Branch: 1 branch
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_UNDEFINED) 269 0 269
314 269 ca->crlf_action = check_crlf(attr_values[0]); /* crlf */
315  
316 269 ▶ if (ca->crlf_action != GIT_CRLF_BINARY) {
Branch: 1 branch
BranchTrueFalseTotal
if (ca->crlf_action != GIT_CRLF_BINARY) { 269 0 269
317 /* load the eol attribute */
318 269 int eol_attr = check_eol(attr_values[1]);
319  
320 269 ▶ if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_LF)
Branch: 2 branches
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_LF) 0 269 269
if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_LF) 0 0 0
321 0 ca->crlf_action = GIT_CRLF_AUTO_INPUT;
322 269 ▶ else if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_CRLF)
Branch: 2 branches
BranchTrueFalseTotal
else if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_CRLF) 0 269 269
else if (ca->crlf_action == GIT_CRLF_AUTO && eol_attr == GIT_EOL_CRLF) 0 0 0
323 0 ca->crlf_action = GIT_CRLF_AUTO_CRLF;
324 269 ▶ else if (eol_attr == GIT_EOL_LF)
Branch: 1 branch
BranchTrueFalseTotal
else if (eol_attr == GIT_EOL_LF) 0 269 269
325 0 ca->crlf_action = GIT_CRLF_TEXT_INPUT;
326 269 ▶ else if (eol_attr == GIT_EOL_CRLF)
Branch: 1 branch
BranchTrueFalseTotal
else if (eol_attr == GIT_EOL_CRLF) 0 269 269
327 0 ca->crlf_action = GIT_CRLF_TEXT_CRLF;
328 }
329  
330 269 ca->attr_action = ca->crlf_action;
331 } else {
332 0 ca->crlf_action = GIT_CRLF_UNDEFINED;
333 }
334  
335 269 ▶ if (ca->crlf_action == GIT_CRLF_TEXT)
Branch: 1 branch
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_TEXT) 0 269 269
336 0 ▶ ca->crlf_action = text_eol_is_crlf(ca) ? GIT_CRLF_TEXT_CRLF : GIT_CRLF_TEXT_INPUT;
Branch: 1 branch
BranchTrueFalseTotal
ca->crlf_action = text_eol_is_crlf(ca) ? GIT_CRLF_TEXT_CRLF : GIT_CRLF_TEXT_INPUT; 0 0 0
337 269 ▶ if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_FALSE)
Branch: 2 branches
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_FALSE) 269 0 269
if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_FALSE) 198 71 269
338 198 ca->crlf_action = GIT_CRLF_BINARY;
339 269 ▶ if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_TRUE)
Branch: 2 branches
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_TRUE) 71 198 269
if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_TRUE) 16 55 71
340 16 ca->crlf_action = GIT_CRLF_AUTO_CRLF;
341 269 ▶ if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_INPUT)
Branch: 2 branches
BranchTrueFalseTotal
if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_INPUT) 55 214 269
if (ca->crlf_action == GIT_CRLF_UNDEFINED && ca->auto_crlf == GIT_AUTO_CRLF_INPUT) 55 0 55
342 55 ca->crlf_action = GIT_CRLF_AUTO_INPUT;
343  
344 269 return 0;
345 }
346  
347 269 static int crlf_check(
348 git_filter *self,
349 void **payload, /* points to NULL ptr on entry, may be set */
350 const git_filter_source *src,
351 const char **attr_values)
352 {
353 struct crlf_attrs ca;
354  
355 269 GIT_UNUSED(self);
356  
357 269 convert_attrs(&ca, attr_values, src);
358  
359 269 ▶ if (ca.crlf_action == GIT_CRLF_BINARY)
Branch: 1 branch
BranchTrueFalseTotal
if (ca.crlf_action == GIT_CRLF_BINARY) 198 71 269
360 198 return GIT_PASSTHROUGH;
361  
362 71 *payload = git__malloc(sizeof(ca));
363 71 ▶ GIT_ERROR_CHECK_ALLOC(*payload);
Branch: 1 branch
BranchTrueFalseTotal
GIT_ERROR_CHECK_ALLOC(*payload); 0 71 71
364 71 memcpy(*payload, &ca, sizeof(ca));
365  
366 71 return 0;
367 }
368  
369 75 static int crlf_apply(
370 git_filter *self,
371 void **payload, /* may be read and/or set */
372 git_str *to,
373 const git_str *from,
374 const git_filter_source *src)
375 {
376 75 int error = 0;
377  
378 /* initialize payload in case `check` was bypassed */
379 75 ▶ if (!*payload) {
Branch: 1 branch
BranchTrueFalseTotal
if (!*payload) { 0 75 75
380 0 ▶ if ((error = crlf_check(self, payload, src, NULL)) < 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((error = crlf_check(self, payload, src, NULL)) < 0) 0 0 0
381 0 return error;
382 }
383  
384 75 ▶ if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
Branch: 1 branch
BranchTrueFalseTotal
if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE) 16 59 75
385 16 error = crlf_apply_to_workdir(*payload, to, from);
386 else
387 59 error = crlf_apply_to_odb(*payload, to, from, src);
388  
389 75 return error;
390 }
391  
392 75 static int crlf_stream(
393 git_writestream **out,
394 git_filter *self,
395 void **payload,
396 const git_filter_source *src,
397 git_writestream *next)
398 {
399 75 return git_filter_buffered_stream_new(out,
400 self, crlf_apply, NULL, payload, src, next);
401 }
402  
403 71 static void crlf_cleanup(
404 git_filter *self,
405 void *payload)
406 {
407 71 GIT_UNUSED(self);
408 71 git__free(payload);
409 71 }
410  
411 87 git_filter *git_crlf_filter_new(void)
412 {
413 87 struct crlf_filter *f = git__calloc(1, sizeof(struct crlf_filter));
414 87 ▶ if (f == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (f == NULL) 0 87 87
415 0 return NULL;
416  
417 87 f->f.version = GIT_FILTER_VERSION;
418 87 f->f.attributes = "crlf eol text";
419 87 f->f.initialize = NULL;
420 87 f->f.shutdown = git_filter_free;
421 87 f->f.check = crlf_check;
422 87 f->f.stream = crlf_stream;
423 87 f->f.cleanup = crlf_cleanup;
424  
425 87 return (git_filter *)f;
426 }
« deps/libgit2/src/libgit2/status.c ↑ Summary deps/libgit2/src/libgit2/diff.c »