]> git.proxmox.com Git - libgit2.git/blob - src/crlf.c
Introduce core.safecrlf handling
[libgit2.git] / src / crlf.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 "git2/attr.h"
9 #include "git2/blob.h"
10 #include "git2/index.h"
11 #include "git2/sys/filter.h"
12
13 #include "common.h"
14 #include "fileops.h"
15 #include "hash.h"
16 #include "filter.h"
17 #include "buf_text.h"
18 #include "repository.h"
19
20 struct crlf_attrs {
21 int crlf_action;
22 int eol;
23 int auto_crlf;
24 int safe_crlf;
25 };
26
27 struct crlf_filter {
28 git_filter f;
29 };
30
31 static int check_crlf(const char *value)
32 {
33 if (GIT_ATTR_TRUE(value))
34 return GIT_CRLF_TEXT;
35
36 if (GIT_ATTR_FALSE(value))
37 return GIT_CRLF_BINARY;
38
39 if (GIT_ATTR_UNSPECIFIED(value))
40 return GIT_CRLF_GUESS;
41
42 if (strcmp(value, "input") == 0)
43 return GIT_CRLF_INPUT;
44
45 if (strcmp(value, "auto") == 0)
46 return GIT_CRLF_AUTO;
47
48 return GIT_CRLF_GUESS;
49 }
50
51 static int check_eol(const char *value)
52 {
53 if (GIT_ATTR_UNSPECIFIED(value))
54 return GIT_EOL_UNSET;
55
56 if (strcmp(value, "lf") == 0)
57 return GIT_EOL_LF;
58
59 if (strcmp(value, "crlf") == 0)
60 return GIT_EOL_CRLF;
61
62 return GIT_EOL_UNSET;
63 }
64
65 static int crlf_input_action(struct crlf_attrs *ca)
66 {
67 if (ca->crlf_action == GIT_CRLF_BINARY)
68 return GIT_CRLF_BINARY;
69
70 if (ca->eol == GIT_EOL_LF)
71 return GIT_CRLF_INPUT;
72
73 if (ca->eol == GIT_EOL_CRLF)
74 return GIT_CRLF_CRLF;
75
76 return ca->crlf_action;
77 }
78
79 static int has_cr_in_index(const git_filter_source *src)
80 {
81 git_repository *repo = git_filter_source_repo(src);
82 const char *path = git_filter_source_path(src);
83 git_index *index;
84 const git_index_entry *entry;
85 git_blob *blob;
86 const void *blobcontent;
87 git_off_t blobsize;
88 bool found_cr;
89
90 if (!path)
91 return false;
92
93 if (git_repository_index__weakptr(&index, repo) < 0) {
94 giterr_clear();
95 return false;
96 }
97
98 if (!(entry = git_index_get_bypath(index, path, 0)) &&
99 !(entry = git_index_get_bypath(index, path, 1)))
100 return false;
101
102 if (!S_ISREG(entry->mode)) /* don't crlf filter non-blobs */
103 return true;
104
105 if (git_blob_lookup(&blob, repo, &entry->id) < 0)
106 return false;
107
108 blobcontent = git_blob_rawcontent(blob);
109 blobsize = git_blob_rawsize(blob);
110 if (!git__is_sizet(blobsize))
111 blobsize = (size_t)-1;
112
113 found_cr = (blobcontent != NULL &&
114 blobsize > 0 &&
115 memchr(blobcontent, '\r', (size_t)blobsize) != NULL);
116
117 git_blob_free(blob);
118 return found_cr;
119 }
120
121 static int crlf_apply_to_odb(
122 struct crlf_attrs *ca,
123 git_buf *to,
124 const git_buf *from,
125 const git_filter_source *src)
126 {
127 git_buf safe = GIT_BUF_INIT;
128 int error = 0;
129
130 /* Empty file? Nothing to do */
131 if (!git_buf_len(from))
132 return 0;
133
134 /* Heuristics to see if we can skip the conversion.
135 * Straight from Core Git.
136 */
137 if (ca->crlf_action == GIT_CRLF_AUTO || ca->crlf_action == GIT_CRLF_GUESS) {
138 git_buf_text_stats stats;
139
140 /* Check heuristics for binary vs text - returns true if binary */
141 if (git_buf_text_gather_stats(&stats, from, false))
142 return GIT_PASSTHROUGH;
143
144 /*
145 * We're currently not going to even try to convert stuff
146 * that has bare CR characters. Does anybody do that crazy
147 * stuff?
148 */
149 if (stats.cr != stats.crlf)
150 return GIT_PASSTHROUGH;
151
152 if (ca->crlf_action == GIT_CRLF_GUESS) {
153 /*
154 * If the file in the index has any CR in it, do not convert.
155 * This is the new safer autocrlf handling.
156 */
157 if (has_cr_in_index(src))
158 return GIT_PASSTHROUGH;
159 }
160
161 if (!stats.cr && !ca->safe_crlf)
162 return GIT_PASSTHROUGH;
163 }
164
165 /* Actually drop the carriage returns */
166 if ((error = git_buf_text_crlf_to_lf(to, from)) < 0)
167 return error;
168
169 /* If safecrlf is enabled, sanity-check the result. */
170 if (ca->safe_crlf) {
171 if ((error = git_buf_grow(&safe, max(from->size, to->size))) < 0 ||
172 (error = git_buf_text_lf_to_crlf(&safe, to)) < 0)
173 goto done;
174
175 if (git_buf_cmp(from, &safe) != 0) {
176 giterr_set(GITERR_FILTER, "LF would be replaced by CRLF in '%s'",
177 git_filter_source_path(src));
178 error = -1;
179 }
180 }
181
182 done:
183 git_buf_free(&safe);
184
185 return error;
186 }
187
188 static const char *line_ending(struct crlf_attrs *ca)
189 {
190 switch (ca->crlf_action) {
191 case GIT_CRLF_BINARY:
192 case GIT_CRLF_INPUT:
193 return "\n";
194
195 case GIT_CRLF_CRLF:
196 return "\r\n";
197
198 case GIT_CRLF_AUTO:
199 case GIT_CRLF_TEXT:
200 case GIT_CRLF_GUESS:
201 break;
202
203 default:
204 goto line_ending_error;
205 }
206
207 switch (ca->eol) {
208 case GIT_EOL_UNSET:
209 return GIT_EOL_NATIVE == GIT_EOL_CRLF ? "\r\n" : "\n";
210
211 case GIT_EOL_CRLF:
212 return "\r\n";
213
214 case GIT_EOL_LF:
215 return "\n";
216
217 default:
218 goto line_ending_error;
219 }
220
221 line_ending_error:
222 giterr_set(GITERR_INVALID, "Invalid input to line ending filter");
223 return NULL;
224 }
225
226 static int crlf_apply_to_workdir(
227 struct crlf_attrs *ca, git_buf *to, const git_buf *from)
228 {
229 const char *workdir_ending = NULL;
230
231 /* Empty file? Nothing to do. */
232 if (git_buf_len(from) == 0)
233 return 0;
234
235 /* Don't filter binary files */
236 if (git_buf_text_is_binary(from))
237 return GIT_PASSTHROUGH;
238
239 /* Determine proper line ending */
240 workdir_ending = line_ending(ca);
241 if (!workdir_ending)
242 return -1;
243
244 /* only LF->CRLF conversion is supported, do nothing on LF platforms */
245 if (strcmp(workdir_ending, "\r\n") != 0)
246 return GIT_PASSTHROUGH;
247
248 return git_buf_text_lf_to_crlf(to, from);
249 }
250
251 static int crlf_check(
252 git_filter *self,
253 void **payload, /* points to NULL ptr on entry, may be set */
254 const git_filter_source *src,
255 const char **attr_values)
256 {
257 int error;
258 struct crlf_attrs ca;
259
260 GIT_UNUSED(self);
261
262 if (!attr_values) {
263 ca.crlf_action = GIT_CRLF_GUESS;
264 ca.eol = GIT_EOL_UNSET;
265 } else {
266 ca.crlf_action = check_crlf(attr_values[2]); /* text */
267 if (ca.crlf_action == GIT_CRLF_GUESS)
268 ca.crlf_action = check_crlf(attr_values[0]); /* clrf */
269 ca.eol = check_eol(attr_values[1]); /* eol */
270 }
271 ca.auto_crlf = GIT_AUTO_CRLF_DEFAULT;
272
273 /*
274 * Use the core Git logic to see if we should perform CRLF for this file
275 * based on its attributes & the value of `core.autocrlf`
276 */
277 ca.crlf_action = crlf_input_action(&ca);
278
279 if (ca.crlf_action == GIT_CRLF_BINARY)
280 return GIT_PASSTHROUGH;
281
282 if (ca.crlf_action == GIT_CRLF_GUESS ||
283 (ca.crlf_action == GIT_CRLF_AUTO &&
284 git_filter_source_mode(src) == GIT_FILTER_SMUDGE)) {
285 error = git_repository__cvar(
286 &ca.auto_crlf, git_filter_source_repo(src), GIT_CVAR_AUTO_CRLF);
287 if (error < 0)
288 return error;
289
290 if (ca.auto_crlf == GIT_AUTO_CRLF_FALSE)
291 return GIT_PASSTHROUGH;
292
293 if (ca.auto_crlf == GIT_AUTO_CRLF_INPUT &&
294 git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
295 return GIT_PASSTHROUGH;
296 }
297
298 if (git_filter_source_mode(src) == GIT_FILTER_CLEAN) {
299 error = git_repository__cvar(
300 &ca.safe_crlf, git_filter_source_repo(src), GIT_CVAR_SAFE_CRLF);
301 if (error < 0)
302 return error;
303 }
304
305 *payload = git__malloc(sizeof(ca));
306 GITERR_CHECK_ALLOC(*payload);
307 memcpy(*payload, &ca, sizeof(ca));
308
309 return 0;
310 }
311
312 static int crlf_apply(
313 git_filter *self,
314 void **payload, /* may be read and/or set */
315 git_buf *to,
316 const git_buf *from,
317 const git_filter_source *src)
318 {
319 /* initialize payload in case `check` was bypassed */
320 if (!*payload) {
321 int error = crlf_check(self, payload, src, NULL);
322 if (error < 0 && error != GIT_PASSTHROUGH)
323 return error;
324 }
325
326 if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
327 return crlf_apply_to_workdir(*payload, to, from);
328 else
329 return crlf_apply_to_odb(*payload, to, from, src);
330 }
331
332 static void crlf_cleanup(
333 git_filter *self,
334 void *payload)
335 {
336 GIT_UNUSED(self);
337 git__free(payload);
338 }
339
340 git_filter *git_crlf_filter_new(void)
341 {
342 struct crlf_filter *f = git__calloc(1, sizeof(struct crlf_filter));
343
344 f->f.version = GIT_FILTER_VERSION;
345 f->f.attributes = "crlf eol text";
346 f->f.initialize = NULL;
347 f->f.shutdown = git_filter_free;
348 f->f.check = crlf_check;
349 f->f.apply = crlf_apply;
350 f->f.cleanup = crlf_cleanup;
351
352 return (git_filter *)f;
353 }