]> git.ipfire.org Git - thirdparty/git.git/blame - convert.c
convert: give saner names to crlf/eol variables, types and functions
[thirdparty/git.git] / convert.c
CommitLineData
6c510bee 1#include "cache.h"
35ebfd6a 2#include "attr.h"
3fed15f5 3#include "run-command.h"
a2b665de 4#include "quote.h"
35ebfd6a 5
6c510bee
LT
6/*
7 * convert.c - convert a file when checking it out and checking it in.
8 *
9 * This should use the pathname to decide on whether it wants to do some
10 * more interesting conversions (automatic gzip/unzip, general format
11 * conversions etc etc), but by default it just does automatic CRLF<->LF
942e7747 12 * translation when the "text" attribute or "auto_crlf" option is set.
6c510bee
LT
13 */
14
c61dcff9 15enum crlf_action {
fd6cce9e
EB
16 CRLF_GUESS = -1,
17 CRLF_BINARY = 0,
18 CRLF_TEXT,
19 CRLF_INPUT,
20 CRLF_CRLF,
c9b6782a 21 CRLF_AUTO
fd6cce9e 22};
163b9591 23
6c510bee 24struct text_stat {
28624193
DP
25 /* NUL, CR, LF and CRLF counts */
26 unsigned nul, cr, lf, crlf;
6c510bee
LT
27
28 /* These are just approximations! */
29 unsigned printable, nonprintable;
30};
31
32static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
33{
34 unsigned long i;
35
36 memset(stats, 0, sizeof(*stats));
37
38 for (i = 0; i < size; i++) {
39 unsigned char c = buf[i];
40 if (c == '\r') {
41 stats->cr++;
42 if (i+1 < size && buf[i+1] == '\n')
43 stats->crlf++;
44 continue;
45 }
46 if (c == '\n') {
47 stats->lf++;
48 continue;
49 }
50 if (c == 127)
51 /* DEL */
52 stats->nonprintable++;
53 else if (c < 32) {
54 switch (c) {
55 /* BS, HT, ESC and FF */
56 case '\b': case '\t': case '\033': case '\014':
57 stats->printable++;
58 break;
28624193
DP
59 case 0:
60 stats->nul++;
61 /* fall through */
6c510bee
LT
62 default:
63 stats->nonprintable++;
64 }
65 }
66 else
67 stats->printable++;
68 }
f9dd4bf4
DK
69
70 /* If file ends with EOF then don't count this EOF as non-printable. */
71 if (size >= 1 && buf[size-1] == '\032')
72 stats->nonprintable--;
6c510bee
LT
73}
74
75/*
76 * The same heuristics as diff.c::mmfile_is_binary()
77 */
78static int is_binary(unsigned long size, struct text_stat *stats)
79{
80
28624193
DP
81 if (stats->nul)
82 return 1;
6c510bee
LT
83 if ((stats->printable >> 7) < stats->nonprintable)
84 return 1;
85 /*
86 * Other heuristics? Average line length might be relevant,
87 * as might LF vs CR vs CRLF counts..
88 *
89 * NOTE! It might be normal to have a low ratio of CRLF to LF
90 * (somebody starts with a LF-only file and edits it with an editor
91 * that adds CRLF only to lines that are added..). But do we
92 * want to support CR-only? Probably not.
93 */
94 return 0;
95}
96
c61dcff9 97static enum eol output_eol(enum crlf_action crlf_action)
f217f0e8 98{
c61dcff9 99 switch (crlf_action) {
942e7747
EB
100 case CRLF_BINARY:
101 return EOL_UNSET;
102 case CRLF_CRLF:
103 return EOL_CRLF;
104 case CRLF_INPUT:
105 return EOL_LF;
106 case CRLF_GUESS:
107 if (!auto_crlf)
108 return EOL_UNSET;
109 /* fall through */
110 case CRLF_TEXT:
111 case CRLF_AUTO:
112 if (auto_crlf == AUTO_CRLF_TRUE)
113 return EOL_CRLF;
114 else if (auto_crlf == AUTO_CRLF_INPUT)
115 return EOL_LF;
ec70f52f 116 else if (core_eol == EOL_UNSET)
942e7747
EB
117 return EOL_NATIVE;
118 }
ec70f52f 119 return core_eol;
942e7747
EB
120}
121
c61dcff9 122static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
21e5ad50
SP
123 struct text_stat *stats, enum safe_crlf checksafe)
124{
125 if (!checksafe)
126 return;
127
c61dcff9 128 if (output_eol(crlf_action) == EOL_LF) {
21e5ad50
SP
129 /*
130 * CRLFs would not be restored by checkout:
131 * check if we'd remove CRLFs
132 */
133 if (stats->crlf) {
134 if (checksafe == SAFE_CRLF_WARN)
942e7747 135 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
21e5ad50
SP
136 else /* i.e. SAFE_CRLF_FAIL */
137 die("CRLF would be replaced by LF in %s.", path);
138 }
c61dcff9 139 } else if (output_eol(crlf_action) == EOL_CRLF) {
21e5ad50
SP
140 /*
141 * CRLFs would be added by checkout:
142 * check if we have "naked" LFs
143 */
144 if (stats->lf != stats->crlf) {
145 if (checksafe == SAFE_CRLF_WARN)
942e7747 146 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
21e5ad50
SP
147 else /* i.e. SAFE_CRLF_FAIL */
148 die("LF would be replaced by CRLF in %s", path);
149 }
150 }
151}
152
c4805393
FAG
153static int has_cr_in_index(const char *path)
154{
155 int pos, len;
156 unsigned long sz;
157 enum object_type type;
158 void *data;
159 int has_cr;
160 struct index_state *istate = &the_index;
161
162 len = strlen(path);
163 pos = index_name_pos(istate, path, len);
164 if (pos < 0) {
165 /*
166 * We might be in the middle of a merge, in which
167 * case we would read stage #2 (ours).
168 */
169 int i;
170 for (i = -pos - 1;
171 (pos < 0 && i < istate->cache_nr &&
172 !strcmp(istate->cache[i]->name, path));
173 i++)
174 if (ce_stage(istate->cache[i]) == 2)
175 pos = i;
176 }
177 if (pos < 0)
178 return 0;
179 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
180 if (!data || type != OBJ_BLOB) {
181 free(data);
182 return 0;
183 }
184
185 has_cr = memchr(data, '\r', sz) != NULL;
186 free(data);
187 return has_cr;
188}
189
5ecd293d 190static int crlf_to_git(const char *path, const char *src, size_t len,
c61dcff9
JH
191 struct strbuf *buf,
192 enum crlf_action crlf_action, enum safe_crlf checksafe)
6c510bee 193{
6c510bee 194 struct text_stat stats;
5ecd293d 195 char *dst;
6c510bee 196
c61dcff9
JH
197 if (crlf_action == CRLF_BINARY ||
198 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) || !len)
5ecd293d 199 return 0;
6c510bee 200
5ecd293d 201 gather_stats(src, len, &stats);
6c510bee 202
c61dcff9 203 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
201ac8ef
JH
204 /*
205 * We're currently not going to even try to convert stuff
206 * that has bare CR characters. Does anybody do that crazy
207 * stuff?
208 */
209 if (stats.cr != stats.crlf)
5ecd293d 210 return 0;
201ac8ef
JH
211
212 /*
213 * And add some heuristics for binary vs text, of course...
214 */
5ecd293d
PH
215 if (is_binary(len, &stats))
216 return 0;
c4805393 217
c61dcff9 218 if (crlf_action == CRLF_GUESS) {
fd6cce9e
EB
219 /*
220 * If the file in the index has any CR in it, do not convert.
221 * This is the new safer autocrlf handling.
222 */
223 if (has_cr_in_index(path))
224 return 0;
225 }
201ac8ef 226 }
6c510bee 227
c61dcff9 228 check_safe_crlf(path, crlf_action, &stats, checksafe);
21e5ad50
SP
229
230 /* Optimization: No CR? Nothing to convert, regardless. */
231 if (!stats.cr)
232 return 0;
233
90d16ec0
PH
234 /* only grow if not in place */
235 if (strbuf_avail(buf) + buf->len < len)
236 strbuf_grow(buf, len - buf->len);
5ecd293d 237 dst = buf->buf;
c61dcff9 238 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
163b9591
JH
239 /*
240 * If we guessed, we already know we rejected a file with
241 * lone CR, and we can strip a CR without looking at what
242 * follow it.
243 */
201ac8ef 244 do {
ac78e548 245 unsigned char c = *src++;
201ac8ef 246 if (c != '\r')
ac78e548 247 *dst++ = c;
5ecd293d 248 } while (--len);
201ac8ef
JH
249 } else {
250 do {
ac78e548 251 unsigned char c = *src++;
5ecd293d 252 if (! (c == '\r' && (1 < len && *src == '\n')))
ac78e548 253 *dst++ = c;
5ecd293d 254 } while (--len);
201ac8ef 255 }
5ecd293d
PH
256 strbuf_setlen(buf, dst - buf->buf);
257 return 1;
6c510bee
LT
258}
259
5ecd293d 260static int crlf_to_worktree(const char *path, const char *src, size_t len,
c61dcff9 261 struct strbuf *buf, enum crlf_action crlf_action)
6c510bee 262{
5ecd293d 263 char *to_free = NULL;
6c510bee 264 struct text_stat stats;
6c510bee 265
c61dcff9 266 if (!len || output_eol(crlf_action) != EOL_CRLF)
5ecd293d 267 return 0;
6c510bee 268
5ecd293d 269 gather_stats(src, len, &stats);
6c510bee
LT
270
271 /* No LF? Nothing to convert, regardless. */
272 if (!stats.lf)
5ecd293d 273 return 0;
6c510bee
LT
274
275 /* Was it already in CRLF format? */
276 if (stats.lf == stats.crlf)
5ecd293d 277 return 0;
6c510bee 278
c61dcff9
JH
279 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
280 if (crlf_action == CRLF_GUESS) {
fd6cce9e
EB
281 /* If we have any CR or CRLF line endings, we do not touch it */
282 /* This is the new safer autocrlf-handling */
283 if (stats.cr > 0 || stats.crlf > 0)
284 return 0;
285 }
c4805393 286
201ac8ef
JH
287 /* If we have any bare CR characters, we're not going to touch it */
288 if (stats.cr != stats.crlf)
5ecd293d 289 return 0;
6c510bee 290
5ecd293d
PH
291 if (is_binary(len, &stats))
292 return 0;
201ac8ef 293 }
6c510bee 294
5ecd293d
PH
295 /* are we "faking" in place editing ? */
296 if (src == buf->buf)
b315c5c0 297 to_free = strbuf_detach(buf, NULL);
5ecd293d
PH
298
299 strbuf_grow(buf, len + stats.lf - stats.crlf);
300 for (;;) {
301 const char *nl = memchr(src, '\n', len);
302 if (!nl)
303 break;
304 if (nl > src && nl[-1] == '\r') {
305 strbuf_add(buf, src, nl + 1 - src);
306 } else {
307 strbuf_add(buf, src, nl - src);
308 strbuf_addstr(buf, "\r\n");
309 }
310 len -= nl + 1 - src;
311 src = nl + 1;
312 }
313 strbuf_add(buf, src, len);
314
315 free(to_free);
316 return 1;
6c510bee 317}
35ebfd6a 318
546bb582
JS
319struct filter_params {
320 const char *src;
321 unsigned long size;
322 const char *cmd;
a2b665de 323 const char *path;
546bb582
JS
324};
325
ae6a5609 326static int filter_buffer(int in, int out, void *data)
aa4ed402
JH
327{
328 /*
329 * Spawn cmd and feed the buffer contents through its stdin.
330 */
331 struct child_process child_process;
546bb582 332 struct filter_params *params = (struct filter_params *)data;
aa4ed402 333 int write_err, status;
66dbfd55
GV
334 const char *argv[] = { NULL, NULL };
335
a2b665de
PW
336 /* apply % substitution to cmd */
337 struct strbuf cmd = STRBUF_INIT;
338 struct strbuf path = STRBUF_INIT;
339 struct strbuf_expand_dict_entry dict[] = {
340 { "f", NULL, },
341 { NULL, NULL, },
342 };
343
344 /* quote the path to preserve spaces, etc. */
345 sq_quote_buf(&path, params->path);
346 dict[0].value = path.buf;
347
348 /* expand all %f with the quoted path */
349 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
350 strbuf_release(&path);
351
352 argv[0] = cmd.buf;
aa4ed402
JH
353
354 memset(&child_process, 0, sizeof(child_process));
dc1bfdcd 355 child_process.argv = argv;
ac0ba18d 356 child_process.use_shell = 1;
dc1bfdcd 357 child_process.in = -1;
ae6a5609 358 child_process.out = out;
aa4ed402 359
dc1bfdcd 360 if (start_command(&child_process))
546bb582 361 return error("cannot fork to run external filter %s", params->cmd);
aa4ed402 362
546bb582 363 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
dc1bfdcd 364 if (close(child_process.in))
aa4ed402
JH
365 write_err = 1;
366 if (write_err)
546bb582 367 error("cannot feed the input to external filter %s", params->cmd);
aa4ed402
JH
368
369 status = finish_command(&child_process);
370 if (status)
5709e036 371 error("external filter %s failed %d", params->cmd, status);
a2b665de
PW
372
373 strbuf_release(&cmd);
aa4ed402
JH
374 return (write_err || status);
375}
376
5ecd293d
PH
377static int apply_filter(const char *path, const char *src, size_t len,
378 struct strbuf *dst, const char *cmd)
aa4ed402
JH
379{
380 /*
381 * Create a pipeline to have the command filter the buffer's
382 * contents.
383 *
384 * (child --> cmd) --> us
385 */
546bb582 386 int ret = 1;
f285a2d7 387 struct strbuf nbuf = STRBUF_INIT;
546bb582
JS
388 struct async async;
389 struct filter_params params;
aa4ed402
JH
390
391 if (!cmd)
5ecd293d 392 return 0;
aa4ed402 393
546bb582
JS
394 memset(&async, 0, sizeof(async));
395 async.proc = filter_buffer;
396 async.data = &params;
ae6a5609 397 async.out = -1;
546bb582
JS
398 params.src = src;
399 params.size = len;
400 params.cmd = cmd;
a2b665de 401 params.path = path;
aa4ed402
JH
402
403 fflush(NULL);
546bb582
JS
404 if (start_async(&async))
405 return 0; /* error was already reported */
aa4ed402 406
546bb582 407 if (strbuf_read(&nbuf, async.out, len) < 0) {
5ecd293d
PH
408 error("read from external filter %s failed", cmd);
409 ret = 0;
aa4ed402 410 }
546bb582 411 if (close(async.out)) {
90d16ec0 412 error("read from external filter %s failed", cmd);
5ecd293d 413 ret = 0;
aa4ed402 414 }
546bb582
JS
415 if (finish_async(&async)) {
416 error("external filter %s failed", cmd);
5ecd293d 417 ret = 0;
aa4ed402
JH
418 }
419
5ecd293d 420 if (ret) {
90d16ec0 421 strbuf_swap(dst, &nbuf);
5ecd293d 422 }
90d16ec0 423 strbuf_release(&nbuf);
5ecd293d 424 return ret;
aa4ed402
JH
425}
426
427static struct convert_driver {
428 const char *name;
429 struct convert_driver *next;
cd8be6c9
BH
430 const char *smudge;
431 const char *clean;
aa4ed402
JH
432} *user_convert, **user_convert_tail;
433
ef90d6d4 434static int read_convert_config(const char *var, const char *value, void *cb)
aa4ed402
JH
435{
436 const char *ep, *name;
437 int namelen;
438 struct convert_driver *drv;
439
440 /*
441 * External conversion drivers are configured using
442 * "filter.<name>.variable".
443 */
444 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
445 return 0;
446 name = var + 7;
447 namelen = ep - name;
448 for (drv = user_convert; drv; drv = drv->next)
449 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
450 break;
451 if (!drv) {
aa4ed402 452 drv = xcalloc(1, sizeof(struct convert_driver));
182af834 453 drv->name = xmemdupz(name, namelen);
aa4ed402
JH
454 *user_convert_tail = drv;
455 user_convert_tail = &(drv->next);
456 }
457
458 ep++;
459
460 /*
461 * filter.<name>.smudge and filter.<name>.clean specifies
462 * the command line:
463 *
464 * command-line
465 *
466 * The command-line will not be interpolated in any way.
467 */
468
cd8be6c9
BH
469 if (!strcmp("smudge", ep))
470 return git_config_string(&drv->smudge, var, value);
471
472 if (!strcmp("clean", ep))
473 return git_config_string(&drv->clean, var, value);
aa4ed402 474
aa4ed402
JH
475 return 0;
476}
477
6073ee85 478static void setup_convert_check(struct git_attr_check *check)
35ebfd6a 479{
5ec3e670 480 static struct git_attr *attr_text;
35ebfd6a 481 static struct git_attr *attr_crlf;
fd6cce9e 482 static struct git_attr *attr_eol;
3fed15f5 483 static struct git_attr *attr_ident;
aa4ed402 484 static struct git_attr *attr_filter;
35ebfd6a 485
5ec3e670
EB
486 if (!attr_text) {
487 attr_text = git_attr("text");
7fb0eaa2 488 attr_crlf = git_attr("crlf");
fd6cce9e 489 attr_eol = git_attr("eol");
7fb0eaa2
JH
490 attr_ident = git_attr("ident");
491 attr_filter = git_attr("filter");
aa4ed402 492 user_convert_tail = &user_convert;
ef90d6d4 493 git_config(read_convert_config, NULL);
3fed15f5
JH
494 }
495 check[0].attr = attr_crlf;
496 check[1].attr = attr_ident;
aa4ed402 497 check[2].attr = attr_filter;
fd6cce9e 498 check[3].attr = attr_eol;
5ec3e670 499 check[4].attr = attr_text;
3fed15f5
JH
500}
501
502static int count_ident(const char *cp, unsigned long size)
503{
504 /*
af9b54bb 505 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
3fed15f5
JH
506 */
507 int cnt = 0;
508 char ch;
509
510 while (size) {
511 ch = *cp++;
512 size--;
513 if (ch != '$')
514 continue;
af9b54bb 515 if (size < 3)
3fed15f5 516 break;
af9b54bb 517 if (memcmp("Id", cp, 2))
3fed15f5 518 continue;
af9b54bb
AP
519 ch = cp[2];
520 cp += 3;
521 size -= 3;
3fed15f5 522 if (ch == '$')
af9b54bb 523 cnt++; /* $Id$ */
3fed15f5
JH
524 if (ch != ':')
525 continue;
526
527 /*
af9b54bb 528 * "$Id: ... "; scan up to the closing dollar sign and discard.
3fed15f5
JH
529 */
530 while (size) {
531 ch = *cp++;
532 size--;
533 if (ch == '$') {
534 cnt++;
535 break;
536 }
a9f3049f
HG
537 if (ch == '\n')
538 break;
3fed15f5
JH
539 }
540 }
541 return cnt;
542}
543
5ecd293d
PH
544static int ident_to_git(const char *path, const char *src, size_t len,
545 struct strbuf *buf, int ident)
3fed15f5 546{
5ecd293d 547 char *dst, *dollar;
3fed15f5 548
5ecd293d
PH
549 if (!ident || !count_ident(src, len))
550 return 0;
551
90d16ec0
PH
552 /* only grow if not in place */
553 if (strbuf_avail(buf) + buf->len < len)
554 strbuf_grow(buf, len - buf->len);
5ecd293d
PH
555 dst = buf->buf;
556 for (;;) {
557 dollar = memchr(src, '$', len);
558 if (!dollar)
559 break;
560 memcpy(dst, src, dollar + 1 - src);
561 dst += dollar + 1 - src;
562 len -= dollar + 1 - src;
563 src = dollar + 1;
564
565 if (len > 3 && !memcmp(src, "Id:", 3)) {
566 dollar = memchr(src + 3, '$', len - 3);
567 if (!dollar)
568 break;
a9f3049f
HG
569 if (memchr(src + 3, '\n', dollar - src - 3)) {
570 /* Line break before the next dollar. */
571 continue;
572 }
573
af9b54bb
AP
574 memcpy(dst, "Id$", 3);
575 dst += 3;
5ecd293d
PH
576 len -= dollar + 1 - src;
577 src = dollar + 1;
3fed15f5
JH
578 }
579 }
5ecd293d
PH
580 memcpy(dst, src, len);
581 strbuf_setlen(buf, dst + len - buf->buf);
582 return 1;
3fed15f5
JH
583}
584
5ecd293d
PH
585static int ident_to_worktree(const char *path, const char *src, size_t len,
586 struct strbuf *buf, int ident)
3fed15f5 587{
3fed15f5 588 unsigned char sha1[20];
07814d90 589 char *to_free = NULL, *dollar, *spc;
5ecd293d 590 int cnt;
3fed15f5
JH
591
592 if (!ident)
5ecd293d 593 return 0;
3fed15f5 594
5ecd293d 595 cnt = count_ident(src, len);
3fed15f5 596 if (!cnt)
5ecd293d 597 return 0;
3fed15f5 598
5ecd293d
PH
599 /* are we "faking" in place editing ? */
600 if (src == buf->buf)
b315c5c0 601 to_free = strbuf_detach(buf, NULL);
5ecd293d 602 hash_sha1_file(src, len, "blob", sha1);
3fed15f5 603
5ecd293d
PH
604 strbuf_grow(buf, len + cnt * 43);
605 for (;;) {
606 /* step 1: run to the next '$' */
607 dollar = memchr(src, '$', len);
608 if (!dollar)
609 break;
610 strbuf_add(buf, src, dollar + 1 - src);
611 len -= dollar + 1 - src;
612 src = dollar + 1;
c23290d5 613
5ecd293d
PH
614 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
615 if (len < 3 || memcmp("Id", src, 2))
3fed15f5
JH
616 continue;
617
5ecd293d
PH
618 /* step 3: skip over Id$ or Id:xxxxx$ */
619 if (src[2] == '$') {
620 src += 3;
621 len -= 3;
622 } else if (src[2] == ':') {
623 /*
624 * It's possible that an expanded Id has crept its way into the
07814d90
HG
625 * repository, we cope with that by stripping the expansion out.
626 * This is probably not a good idea, since it will cause changes
627 * on checkout, which won't go away by stash, but let's keep it
628 * for git-style ids.
5ecd293d
PH
629 */
630 dollar = memchr(src + 3, '$', len - 3);
631 if (!dollar) {
632 /* incomplete keyword, no more '$', so just quit the loop */
633 break;
634 }
c23290d5 635
a9f3049f
HG
636 if (memchr(src + 3, '\n', dollar - src - 3)) {
637 /* Line break before the next dollar. */
638 continue;
639 }
640
07814d90
HG
641 spc = memchr(src + 4, ' ', dollar - src - 4);
642 if (spc && spc < dollar-1) {
643 /* There are spaces in unexpected places.
644 * This is probably an id from some other
645 * versioning system. Keep it for now.
646 */
647 continue;
648 }
649
5ecd293d
PH
650 len -= dollar + 1 - src;
651 src = dollar + 1;
652 } else {
653 /* it wasn't a "Id$" or "Id:xxxx$" */
654 continue;
655 }
c23290d5 656
5ecd293d
PH
657 /* step 4: substitute */
658 strbuf_addstr(buf, "Id: ");
659 strbuf_add(buf, sha1_to_hex(sha1), 40);
660 strbuf_addstr(buf, " $");
3fed15f5 661 }
5ecd293d 662 strbuf_add(buf, src, len);
3fed15f5 663
5ecd293d
PH
664 free(to_free);
665 return 1;
35ebfd6a
JH
666}
667
6073ee85 668static int git_path_check_crlf(const char *path, struct git_attr_check *check)
35ebfd6a 669{
6073ee85
JH
670 const char *value = check->value;
671
672 if (ATTR_TRUE(value))
673 return CRLF_TEXT;
674 else if (ATTR_FALSE(value))
675 return CRLF_BINARY;
676 else if (ATTR_UNSET(value))
677 ;
678 else if (!strcmp(value, "input"))
679 return CRLF_INPUT;
fd6cce9e
EB
680 else if (!strcmp(value, "auto"))
681 return CRLF_AUTO;
163b9591 682 return CRLF_GUESS;
35ebfd6a
JH
683}
684
fd6cce9e
EB
685static int git_path_check_eol(const char *path, struct git_attr_check *check)
686{
687 const char *value = check->value;
688
689 if (ATTR_UNSET(value))
690 ;
691 else if (!strcmp(value, "lf"))
692 return EOL_LF;
693 else if (!strcmp(value, "crlf"))
694 return EOL_CRLF;
695 return EOL_UNSET;
696}
697
aa4ed402
JH
698static struct convert_driver *git_path_check_convert(const char *path,
699 struct git_attr_check *check)
700{
701 const char *value = check->value;
702 struct convert_driver *drv;
703
704 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
705 return NULL;
706 for (drv = user_convert; drv; drv = drv->next)
707 if (!strcmp(value, drv->name))
708 return drv;
709 return NULL;
710}
711
3fed15f5
JH
712static int git_path_check_ident(const char *path, struct git_attr_check *check)
713{
714 const char *value = check->value;
715
716 return !!ATTR_TRUE(value);
717}
718
c61dcff9 719static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol eol_attr)
f217f0e8 720{
5ec3e670 721 if (text_attr == CRLF_BINARY)
fd6cce9e
EB
722 return CRLF_BINARY;
723 if (eol_attr == EOL_LF)
724 return CRLF_INPUT;
725 if (eol_attr == EOL_CRLF)
726 return CRLF_CRLF;
5ec3e670 727 return text_attr;
fd6cce9e
EB
728}
729
21e5ad50
SP
730int convert_to_git(const char *path, const char *src, size_t len,
731 struct strbuf *dst, enum safe_crlf checksafe)
35ebfd6a 732{
5ec3e670 733 struct git_attr_check check[5];
c61dcff9 734 enum crlf_action crlf_action = CRLF_GUESS;
942e7747 735 enum eol eol_attr = EOL_UNSET;
5ecd293d 736 int ident = 0, ret = 0;
cd8be6c9 737 const char *filter = NULL;
6073ee85
JH
738
739 setup_convert_check(check);
3fed15f5 740 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 741 struct convert_driver *drv;
c61dcff9
JH
742 crlf_action = git_path_check_crlf(path, check + 4);
743 if (crlf_action == CRLF_GUESS)
744 crlf_action = git_path_check_crlf(path, check + 0);
3fed15f5 745 ident = git_path_check_ident(path, check + 1);
aa4ed402 746 drv = git_path_check_convert(path, check + 2);
942e7747 747 eol_attr = git_path_check_eol(path, check + 3);
aa4ed402
JH
748 if (drv && drv->clean)
749 filter = drv->clean;
3fed15f5
JH
750 }
751
5ecd293d
PH
752 ret |= apply_filter(path, src, len, dst, filter);
753 if (ret) {
754 src = dst->buf;
755 len = dst->len;
aa4ed402 756 }
c61dcff9
JH
757 crlf_action = input_crlf_action(crlf_action, eol_attr);
758 ret |= crlf_to_git(path, src, len, dst, crlf_action, checksafe);
5ecd293d
PH
759 if (ret) {
760 src = dst->buf;
761 len = dst->len;
6073ee85 762 }
5ecd293d 763 return ret | ident_to_git(path, src, len, dst, ident);
35ebfd6a
JH
764}
765
43dd2332
EB
766static int convert_to_working_tree_internal(const char *path, const char *src,
767 size_t len, struct strbuf *dst,
768 int normalizing)
35ebfd6a 769{
5ec3e670 770 struct git_attr_check check[5];
c61dcff9 771 enum crlf_action crlf_action = CRLF_GUESS;
942e7747 772 enum eol eol_attr = EOL_UNSET;
5ecd293d 773 int ident = 0, ret = 0;
cd8be6c9 774 const char *filter = NULL;
6073ee85
JH
775
776 setup_convert_check(check);
3fed15f5 777 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 778 struct convert_driver *drv;
c61dcff9
JH
779 crlf_action = git_path_check_crlf(path, check + 4);
780 if (crlf_action == CRLF_GUESS)
781 crlf_action = git_path_check_crlf(path, check + 0);
3fed15f5 782 ident = git_path_check_ident(path, check + 1);
aa4ed402 783 drv = git_path_check_convert(path, check + 2);
942e7747 784 eol_attr = git_path_check_eol(path, check + 3);
aa4ed402
JH
785 if (drv && drv->smudge)
786 filter = drv->smudge;
6073ee85 787 }
3fed15f5 788
5ecd293d
PH
789 ret |= ident_to_worktree(path, src, len, dst, ident);
790 if (ret) {
791 src = dst->buf;
792 len = dst->len;
3fed15f5 793 }
43dd2332
EB
794 /*
795 * CRLF conversion can be skipped if normalizing, unless there
796 * is a smudge filter. The filter might expect CRLFs.
797 */
798 if (filter || !normalizing) {
c61dcff9
JH
799 crlf_action = input_crlf_action(crlf_action, eol_attr);
800 ret |= crlf_to_worktree(path, src, len, dst, crlf_action);
43dd2332
EB
801 if (ret) {
802 src = dst->buf;
803 len = dst->len;
804 }
aa4ed402 805 }
5ecd293d 806 return ret | apply_filter(path, src, len, dst, filter);
35ebfd6a 807}
f217f0e8 808
43dd2332
EB
809int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
810{
811 return convert_to_working_tree_internal(path, src, len, dst, 0);
812}
813
f217f0e8
EB
814int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
815{
43dd2332 816 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
f217f0e8
EB
817 if (ret) {
818 src = dst->buf;
819 len = dst->len;
820 }
821 return ret | convert_to_git(path, src, len, dst, 0);
822}