enums: omit trailing comma for portability
[git/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
fd6cce9e
EB
15enum action {
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
f217f0e8
EB
97static enum eol determine_output_conversion(enum action action)
98{
942e7747
EB
99 switch (action) {
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;
116 else if (eol == EOL_UNSET)
117 return EOL_NATIVE;
118 }
119 return eol;
120}
121
fd6cce9e 122static void check_safe_crlf(const char *path, enum action action,
21e5ad50
SP
123 struct text_stat *stats, enum safe_crlf checksafe)
124{
125 if (!checksafe)
126 return;
127
942e7747 128 if (determine_output_conversion(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 }
942e7747 139 } else if (determine_output_conversion(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,
fd6cce9e 191 struct strbuf *buf, enum action action, enum safe_crlf checksafe)
6c510bee 192{
6c510bee 193 struct text_stat stats;
5ecd293d 194 char *dst;
6c510bee 195
fd6cce9e
EB
196 if (action == CRLF_BINARY ||
197 (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) || !len)
5ecd293d 198 return 0;
6c510bee 199
5ecd293d 200 gather_stats(src, len, &stats);
6c510bee 201
fd6cce9e 202 if (action == CRLF_AUTO || action == CRLF_GUESS) {
201ac8ef
JH
203 /*
204 * We're currently not going to even try to convert stuff
205 * that has bare CR characters. Does anybody do that crazy
206 * stuff?
207 */
208 if (stats.cr != stats.crlf)
5ecd293d 209 return 0;
201ac8ef
JH
210
211 /*
212 * And add some heuristics for binary vs text, of course...
213 */
5ecd293d
PH
214 if (is_binary(len, &stats))
215 return 0;
c4805393 216
fd6cce9e
EB
217 if (action == CRLF_GUESS) {
218 /*
219 * If the file in the index has any CR in it, do not convert.
220 * This is the new safer autocrlf handling.
221 */
222 if (has_cr_in_index(path))
223 return 0;
224 }
201ac8ef 225 }
6c510bee 226
21e5ad50
SP
227 check_safe_crlf(path, action, &stats, checksafe);
228
229 /* Optimization: No CR? Nothing to convert, regardless. */
230 if (!stats.cr)
231 return 0;
232
90d16ec0
PH
233 /* only grow if not in place */
234 if (strbuf_avail(buf) + buf->len < len)
235 strbuf_grow(buf, len - buf->len);
5ecd293d 236 dst = buf->buf;
fd6cce9e 237 if (action == CRLF_AUTO || action == CRLF_GUESS) {
163b9591
JH
238 /*
239 * If we guessed, we already know we rejected a file with
240 * lone CR, and we can strip a CR without looking at what
241 * follow it.
242 */
201ac8ef 243 do {
ac78e548 244 unsigned char c = *src++;
201ac8ef 245 if (c != '\r')
ac78e548 246 *dst++ = c;
5ecd293d 247 } while (--len);
201ac8ef
JH
248 } else {
249 do {
ac78e548 250 unsigned char c = *src++;
5ecd293d 251 if (! (c == '\r' && (1 < len && *src == '\n')))
ac78e548 252 *dst++ = c;
5ecd293d 253 } while (--len);
201ac8ef 254 }
5ecd293d
PH
255 strbuf_setlen(buf, dst - buf->buf);
256 return 1;
6c510bee
LT
257}
258
5ecd293d 259static int crlf_to_worktree(const char *path, const char *src, size_t len,
fd6cce9e 260 struct strbuf *buf, enum action action)
6c510bee 261{
5ecd293d 262 char *to_free = NULL;
6c510bee 263 struct text_stat stats;
6c510bee 264
942e7747 265 if (!len || determine_output_conversion(action) != EOL_CRLF)
5ecd293d 266 return 0;
6c510bee 267
5ecd293d 268 gather_stats(src, len, &stats);
6c510bee
LT
269
270 /* No LF? Nothing to convert, regardless. */
271 if (!stats.lf)
5ecd293d 272 return 0;
6c510bee
LT
273
274 /* Was it already in CRLF format? */
275 if (stats.lf == stats.crlf)
5ecd293d 276 return 0;
6c510bee 277
fd6cce9e
EB
278 if (action == CRLF_AUTO || action == CRLF_GUESS) {
279 if (action == CRLF_GUESS) {
280 /* If we have any CR or CRLF line endings, we do not touch it */
281 /* This is the new safer autocrlf-handling */
282 if (stats.cr > 0 || stats.crlf > 0)
283 return 0;
284 }
c4805393 285
201ac8ef
JH
286 /* If we have any bare CR characters, we're not going to touch it */
287 if (stats.cr != stats.crlf)
5ecd293d 288 return 0;
6c510bee 289
5ecd293d
PH
290 if (is_binary(len, &stats))
291 return 0;
201ac8ef 292 }
6c510bee 293
5ecd293d
PH
294 /* are we "faking" in place editing ? */
295 if (src == buf->buf)
b315c5c0 296 to_free = strbuf_detach(buf, NULL);
5ecd293d
PH
297
298 strbuf_grow(buf, len + stats.lf - stats.crlf);
299 for (;;) {
300 const char *nl = memchr(src, '\n', len);
301 if (!nl)
302 break;
303 if (nl > src && nl[-1] == '\r') {
304 strbuf_add(buf, src, nl + 1 - src);
305 } else {
306 strbuf_add(buf, src, nl - src);
307 strbuf_addstr(buf, "\r\n");
308 }
309 len -= nl + 1 - src;
310 src = nl + 1;
311 }
312 strbuf_add(buf, src, len);
313
314 free(to_free);
315 return 1;
6c510bee 316}
35ebfd6a 317
546bb582
JS
318struct filter_params {
319 const char *src;
320 unsigned long size;
321 const char *cmd;
a2b665de 322 const char *path;
546bb582
JS
323};
324
ae6a5609 325static int filter_buffer(int in, int out, void *data)
aa4ed402
JH
326{
327 /*
328 * Spawn cmd and feed the buffer contents through its stdin.
329 */
330 struct child_process child_process;
546bb582 331 struct filter_params *params = (struct filter_params *)data;
aa4ed402 332 int write_err, status;
66dbfd55
GV
333 const char *argv[] = { NULL, NULL };
334
a2b665de
PW
335 /* apply % substitution to cmd */
336 struct strbuf cmd = STRBUF_INIT;
337 struct strbuf path = STRBUF_INIT;
338 struct strbuf_expand_dict_entry dict[] = {
339 { "f", NULL, },
340 { NULL, NULL, },
341 };
342
343 /* quote the path to preserve spaces, etc. */
344 sq_quote_buf(&path, params->path);
345 dict[0].value = path.buf;
346
347 /* expand all %f with the quoted path */
348 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
349 strbuf_release(&path);
350
351 argv[0] = cmd.buf;
aa4ed402
JH
352
353 memset(&child_process, 0, sizeof(child_process));
dc1bfdcd 354 child_process.argv = argv;
ac0ba18d 355 child_process.use_shell = 1;
dc1bfdcd 356 child_process.in = -1;
ae6a5609 357 child_process.out = out;
aa4ed402 358
dc1bfdcd 359 if (start_command(&child_process))
546bb582 360 return error("cannot fork to run external filter %s", params->cmd);
aa4ed402 361
546bb582 362 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
dc1bfdcd 363 if (close(child_process.in))
aa4ed402
JH
364 write_err = 1;
365 if (write_err)
546bb582 366 error("cannot feed the input to external filter %s", params->cmd);
aa4ed402
JH
367
368 status = finish_command(&child_process);
369 if (status)
5709e036 370 error("external filter %s failed %d", params->cmd, status);
a2b665de
PW
371
372 strbuf_release(&cmd);
aa4ed402
JH
373 return (write_err || status);
374}
375
5ecd293d
PH
376static int apply_filter(const char *path, const char *src, size_t len,
377 struct strbuf *dst, const char *cmd)
aa4ed402
JH
378{
379 /*
380 * Create a pipeline to have the command filter the buffer's
381 * contents.
382 *
383 * (child --> cmd) --> us
384 */
546bb582 385 int ret = 1;
f285a2d7 386 struct strbuf nbuf = STRBUF_INIT;
546bb582
JS
387 struct async async;
388 struct filter_params params;
aa4ed402
JH
389
390 if (!cmd)
5ecd293d 391 return 0;
aa4ed402 392
546bb582
JS
393 memset(&async, 0, sizeof(async));
394 async.proc = filter_buffer;
395 async.data = &params;
ae6a5609 396 async.out = -1;
546bb582
JS
397 params.src = src;
398 params.size = len;
399 params.cmd = cmd;
a2b665de 400 params.path = path;
aa4ed402
JH
401
402 fflush(NULL);
546bb582
JS
403 if (start_async(&async))
404 return 0; /* error was already reported */
aa4ed402 405
546bb582 406 if (strbuf_read(&nbuf, async.out, len) < 0) {
5ecd293d
PH
407 error("read from external filter %s failed", cmd);
408 ret = 0;
aa4ed402 409 }
546bb582 410 if (close(async.out)) {
90d16ec0 411 error("read from external filter %s failed", cmd);
5ecd293d 412 ret = 0;
aa4ed402 413 }
546bb582
JS
414 if (finish_async(&async)) {
415 error("external filter %s failed", cmd);
5ecd293d 416 ret = 0;
aa4ed402
JH
417 }
418
5ecd293d 419 if (ret) {
90d16ec0 420 strbuf_swap(dst, &nbuf);
5ecd293d 421 }
90d16ec0 422 strbuf_release(&nbuf);
5ecd293d 423 return ret;
aa4ed402
JH
424}
425
426static struct convert_driver {
427 const char *name;
428 struct convert_driver *next;
cd8be6c9
BH
429 const char *smudge;
430 const char *clean;
aa4ed402
JH
431} *user_convert, **user_convert_tail;
432
ef90d6d4 433static int read_convert_config(const char *var, const char *value, void *cb)
aa4ed402
JH
434{
435 const char *ep, *name;
436 int namelen;
437 struct convert_driver *drv;
438
439 /*
440 * External conversion drivers are configured using
441 * "filter.<name>.variable".
442 */
443 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
444 return 0;
445 name = var + 7;
446 namelen = ep - name;
447 for (drv = user_convert; drv; drv = drv->next)
448 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
449 break;
450 if (!drv) {
aa4ed402 451 drv = xcalloc(1, sizeof(struct convert_driver));
182af834 452 drv->name = xmemdupz(name, namelen);
aa4ed402
JH
453 *user_convert_tail = drv;
454 user_convert_tail = &(drv->next);
455 }
456
457 ep++;
458
459 /*
460 * filter.<name>.smudge and filter.<name>.clean specifies
461 * the command line:
462 *
463 * command-line
464 *
465 * The command-line will not be interpolated in any way.
466 */
467
cd8be6c9
BH
468 if (!strcmp("smudge", ep))
469 return git_config_string(&drv->smudge, var, value);
470
471 if (!strcmp("clean", ep))
472 return git_config_string(&drv->clean, var, value);
aa4ed402 473
aa4ed402
JH
474 return 0;
475}
476
6073ee85 477static void setup_convert_check(struct git_attr_check *check)
35ebfd6a 478{
5ec3e670 479 static struct git_attr *attr_text;
35ebfd6a 480 static struct git_attr *attr_crlf;
fd6cce9e 481 static struct git_attr *attr_eol;
3fed15f5 482 static struct git_attr *attr_ident;
aa4ed402 483 static struct git_attr *attr_filter;
35ebfd6a 484
5ec3e670
EB
485 if (!attr_text) {
486 attr_text = git_attr("text");
7fb0eaa2 487 attr_crlf = git_attr("crlf");
fd6cce9e 488 attr_eol = git_attr("eol");
7fb0eaa2
JH
489 attr_ident = git_attr("ident");
490 attr_filter = git_attr("filter");
aa4ed402 491 user_convert_tail = &user_convert;
ef90d6d4 492 git_config(read_convert_config, NULL);
3fed15f5
JH
493 }
494 check[0].attr = attr_crlf;
495 check[1].attr = attr_ident;
aa4ed402 496 check[2].attr = attr_filter;
fd6cce9e 497 check[3].attr = attr_eol;
5ec3e670 498 check[4].attr = attr_text;
3fed15f5
JH
499}
500
501static int count_ident(const char *cp, unsigned long size)
502{
503 /*
af9b54bb 504 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
3fed15f5
JH
505 */
506 int cnt = 0;
507 char ch;
508
509 while (size) {
510 ch = *cp++;
511 size--;
512 if (ch != '$')
513 continue;
af9b54bb 514 if (size < 3)
3fed15f5 515 break;
af9b54bb 516 if (memcmp("Id", cp, 2))
3fed15f5 517 continue;
af9b54bb
AP
518 ch = cp[2];
519 cp += 3;
520 size -= 3;
3fed15f5 521 if (ch == '$')
af9b54bb 522 cnt++; /* $Id$ */
3fed15f5
JH
523 if (ch != ':')
524 continue;
525
526 /*
af9b54bb 527 * "$Id: ... "; scan up to the closing dollar sign and discard.
3fed15f5
JH
528 */
529 while (size) {
530 ch = *cp++;
531 size--;
532 if (ch == '$') {
533 cnt++;
534 break;
535 }
a9f3049f
HG
536 if (ch == '\n')
537 break;
3fed15f5
JH
538 }
539 }
540 return cnt;
541}
542
5ecd293d
PH
543static int ident_to_git(const char *path, const char *src, size_t len,
544 struct strbuf *buf, int ident)
3fed15f5 545{
5ecd293d 546 char *dst, *dollar;
3fed15f5 547
5ecd293d
PH
548 if (!ident || !count_ident(src, len))
549 return 0;
550
90d16ec0
PH
551 /* only grow if not in place */
552 if (strbuf_avail(buf) + buf->len < len)
553 strbuf_grow(buf, len - buf->len);
5ecd293d
PH
554 dst = buf->buf;
555 for (;;) {
556 dollar = memchr(src, '$', len);
557 if (!dollar)
558 break;
559 memcpy(dst, src, dollar + 1 - src);
560 dst += dollar + 1 - src;
561 len -= dollar + 1 - src;
562 src = dollar + 1;
563
564 if (len > 3 && !memcmp(src, "Id:", 3)) {
565 dollar = memchr(src + 3, '$', len - 3);
566 if (!dollar)
567 break;
a9f3049f
HG
568 if (memchr(src + 3, '\n', dollar - src - 3)) {
569 /* Line break before the next dollar. */
570 continue;
571 }
572
af9b54bb
AP
573 memcpy(dst, "Id$", 3);
574 dst += 3;
5ecd293d
PH
575 len -= dollar + 1 - src;
576 src = dollar + 1;
3fed15f5
JH
577 }
578 }
5ecd293d
PH
579 memcpy(dst, src, len);
580 strbuf_setlen(buf, dst + len - buf->buf);
581 return 1;
3fed15f5
JH
582}
583
5ecd293d
PH
584static int ident_to_worktree(const char *path, const char *src, size_t len,
585 struct strbuf *buf, int ident)
3fed15f5 586{
3fed15f5 587 unsigned char sha1[20];
07814d90 588 char *to_free = NULL, *dollar, *spc;
5ecd293d 589 int cnt;
3fed15f5
JH
590
591 if (!ident)
5ecd293d 592 return 0;
3fed15f5 593
5ecd293d 594 cnt = count_ident(src, len);
3fed15f5 595 if (!cnt)
5ecd293d 596 return 0;
3fed15f5 597
5ecd293d
PH
598 /* are we "faking" in place editing ? */
599 if (src == buf->buf)
b315c5c0 600 to_free = strbuf_detach(buf, NULL);
5ecd293d 601 hash_sha1_file(src, len, "blob", sha1);
3fed15f5 602
5ecd293d
PH
603 strbuf_grow(buf, len + cnt * 43);
604 for (;;) {
605 /* step 1: run to the next '$' */
606 dollar = memchr(src, '$', len);
607 if (!dollar)
608 break;
609 strbuf_add(buf, src, dollar + 1 - src);
610 len -= dollar + 1 - src;
611 src = dollar + 1;
c23290d5 612
5ecd293d
PH
613 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
614 if (len < 3 || memcmp("Id", src, 2))
3fed15f5
JH
615 continue;
616
5ecd293d
PH
617 /* step 3: skip over Id$ or Id:xxxxx$ */
618 if (src[2] == '$') {
619 src += 3;
620 len -= 3;
621 } else if (src[2] == ':') {
622 /*
623 * It's possible that an expanded Id has crept its way into the
07814d90
HG
624 * repository, we cope with that by stripping the expansion out.
625 * This is probably not a good idea, since it will cause changes
626 * on checkout, which won't go away by stash, but let's keep it
627 * for git-style ids.
5ecd293d
PH
628 */
629 dollar = memchr(src + 3, '$', len - 3);
630 if (!dollar) {
631 /* incomplete keyword, no more '$', so just quit the loop */
632 break;
633 }
c23290d5 634
a9f3049f
HG
635 if (memchr(src + 3, '\n', dollar - src - 3)) {
636 /* Line break before the next dollar. */
637 continue;
638 }
639
07814d90
HG
640 spc = memchr(src + 4, ' ', dollar - src - 4);
641 if (spc && spc < dollar-1) {
642 /* There are spaces in unexpected places.
643 * This is probably an id from some other
644 * versioning system. Keep it for now.
645 */
646 continue;
647 }
648
5ecd293d
PH
649 len -= dollar + 1 - src;
650 src = dollar + 1;
651 } else {
652 /* it wasn't a "Id$" or "Id:xxxx$" */
653 continue;
654 }
c23290d5 655
5ecd293d
PH
656 /* step 4: substitute */
657 strbuf_addstr(buf, "Id: ");
658 strbuf_add(buf, sha1_to_hex(sha1), 40);
659 strbuf_addstr(buf, " $");
3fed15f5 660 }
5ecd293d 661 strbuf_add(buf, src, len);
3fed15f5 662
5ecd293d
PH
663 free(to_free);
664 return 1;
35ebfd6a
JH
665}
666
6073ee85 667static int git_path_check_crlf(const char *path, struct git_attr_check *check)
35ebfd6a 668{
6073ee85
JH
669 const char *value = check->value;
670
671 if (ATTR_TRUE(value))
672 return CRLF_TEXT;
673 else if (ATTR_FALSE(value))
674 return CRLF_BINARY;
675 else if (ATTR_UNSET(value))
676 ;
677 else if (!strcmp(value, "input"))
678 return CRLF_INPUT;
fd6cce9e
EB
679 else if (!strcmp(value, "auto"))
680 return CRLF_AUTO;
163b9591 681 return CRLF_GUESS;
35ebfd6a
JH
682}
683
fd6cce9e
EB
684static int git_path_check_eol(const char *path, struct git_attr_check *check)
685{
686 const char *value = check->value;
687
688 if (ATTR_UNSET(value))
689 ;
690 else if (!strcmp(value, "lf"))
691 return EOL_LF;
692 else if (!strcmp(value, "crlf"))
693 return EOL_CRLF;
694 return EOL_UNSET;
695}
696
aa4ed402
JH
697static struct convert_driver *git_path_check_convert(const char *path,
698 struct git_attr_check *check)
699{
700 const char *value = check->value;
701 struct convert_driver *drv;
702
703 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
704 return NULL;
705 for (drv = user_convert; drv; drv = drv->next)
706 if (!strcmp(value, drv->name))
707 return drv;
708 return NULL;
709}
710
3fed15f5
JH
711static int git_path_check_ident(const char *path, struct git_attr_check *check)
712{
713 const char *value = check->value;
714
715 return !!ATTR_TRUE(value);
716}
717
f217f0e8
EB
718static enum action determine_action(enum action text_attr, enum eol eol_attr)
719{
5ec3e670 720 if (text_attr == CRLF_BINARY)
fd6cce9e
EB
721 return CRLF_BINARY;
722 if (eol_attr == EOL_LF)
723 return CRLF_INPUT;
724 if (eol_attr == EOL_CRLF)
725 return CRLF_CRLF;
5ec3e670 726 return text_attr;
fd6cce9e
EB
727}
728
21e5ad50
SP
729int convert_to_git(const char *path, const char *src, size_t len,
730 struct strbuf *dst, enum safe_crlf checksafe)
35ebfd6a 731{
5ec3e670 732 struct git_attr_check check[5];
fd6cce9e 733 enum action action = CRLF_GUESS;
942e7747 734 enum eol eol_attr = EOL_UNSET;
5ecd293d 735 int ident = 0, ret = 0;
cd8be6c9 736 const char *filter = NULL;
6073ee85
JH
737
738 setup_convert_check(check);
3fed15f5 739 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 740 struct convert_driver *drv;
5ec3e670
EB
741 action = git_path_check_crlf(path, check + 4);
742 if (action == CRLF_GUESS)
743 action = git_path_check_crlf(path, check + 0);
3fed15f5 744 ident = git_path_check_ident(path, check + 1);
aa4ed402 745 drv = git_path_check_convert(path, check + 2);
942e7747 746 eol_attr = git_path_check_eol(path, check + 3);
aa4ed402
JH
747 if (drv && drv->clean)
748 filter = drv->clean;
3fed15f5
JH
749 }
750
5ecd293d
PH
751 ret |= apply_filter(path, src, len, dst, filter);
752 if (ret) {
753 src = dst->buf;
754 len = dst->len;
aa4ed402 755 }
942e7747 756 action = determine_action(action, eol_attr);
fd6cce9e 757 ret |= crlf_to_git(path, src, len, dst, action, checksafe);
5ecd293d
PH
758 if (ret) {
759 src = dst->buf;
760 len = dst->len;
6073ee85 761 }
5ecd293d 762 return ret | ident_to_git(path, src, len, dst, ident);
35ebfd6a
JH
763}
764
43dd2332
EB
765static int convert_to_working_tree_internal(const char *path, const char *src,
766 size_t len, struct strbuf *dst,
767 int normalizing)
35ebfd6a 768{
5ec3e670 769 struct git_attr_check check[5];
fd6cce9e 770 enum action action = CRLF_GUESS;
942e7747 771 enum eol eol_attr = EOL_UNSET;
5ecd293d 772 int ident = 0, ret = 0;
cd8be6c9 773 const char *filter = NULL;
6073ee85
JH
774
775 setup_convert_check(check);
3fed15f5 776 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 777 struct convert_driver *drv;
5ec3e670
EB
778 action = git_path_check_crlf(path, check + 4);
779 if (action == CRLF_GUESS)
780 action = git_path_check_crlf(path, check + 0);
3fed15f5 781 ident = git_path_check_ident(path, check + 1);
aa4ed402 782 drv = git_path_check_convert(path, check + 2);
942e7747 783 eol_attr = git_path_check_eol(path, check + 3);
aa4ed402
JH
784 if (drv && drv->smudge)
785 filter = drv->smudge;
6073ee85 786 }
3fed15f5 787
5ecd293d
PH
788 ret |= ident_to_worktree(path, src, len, dst, ident);
789 if (ret) {
790 src = dst->buf;
791 len = dst->len;
3fed15f5 792 }
43dd2332
EB
793 /*
794 * CRLF conversion can be skipped if normalizing, unless there
795 * is a smudge filter. The filter might expect CRLFs.
796 */
797 if (filter || !normalizing) {
798 action = determine_action(action, eol_attr);
799 ret |= crlf_to_worktree(path, src, len, dst, action);
800 if (ret) {
801 src = dst->buf;
802 len = dst->len;
803 }
aa4ed402 804 }
5ecd293d 805 return ret | apply_filter(path, src, len, dst, filter);
35ebfd6a 806}
f217f0e8 807
43dd2332
EB
808int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
809{
810 return convert_to_working_tree_internal(path, src, len, dst, 0);
811}
812
f217f0e8
EB
813int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
814{
43dd2332 815 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
f217f0e8
EB
816 if (ret) {
817 src = dst->buf;
818 len = dst->len;
819 }
820 return ret | convert_to_git(path, src, len, dst, 0);
821}