]> git.ipfire.org Git - thirdparty/git.git/blame - convert.c
compat/inet_ntop: fix off-by-one in inet_ntop4
[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"
6424c2ad 5#include "sigchain.h"
35ebfd6a 6
6c510bee
LT
7/*
8 * convert.c - convert a file when checking it out and checking it in.
9 *
10 * This should use the pathname to decide on whether it wants to do some
11 * more interesting conversions (automatic gzip/unzip, general format
12 * conversions etc etc), but by default it just does automatic CRLF<->LF
942e7747 13 * translation when the "text" attribute or "auto_crlf" option is set.
6c510bee
LT
14 */
15
c61dcff9 16enum crlf_action {
fd6cce9e
EB
17 CRLF_GUESS = -1,
18 CRLF_BINARY = 0,
19 CRLF_TEXT,
20 CRLF_INPUT,
21 CRLF_CRLF,
c9b6782a 22 CRLF_AUTO
fd6cce9e 23};
163b9591 24
6c510bee 25struct text_stat {
28624193
DP
26 /* NUL, CR, LF and CRLF counts */
27 unsigned nul, cr, lf, crlf;
6c510bee
LT
28
29 /* These are just approximations! */
30 unsigned printable, nonprintable;
31};
32
33static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
34{
35 unsigned long i;
36
37 memset(stats, 0, sizeof(*stats));
38
39 for (i = 0; i < size; i++) {
40 unsigned char c = buf[i];
41 if (c == '\r') {
42 stats->cr++;
43 if (i+1 < size && buf[i+1] == '\n')
44 stats->crlf++;
45 continue;
46 }
47 if (c == '\n') {
48 stats->lf++;
49 continue;
50 }
51 if (c == 127)
52 /* DEL */
53 stats->nonprintable++;
54 else if (c < 32) {
55 switch (c) {
56 /* BS, HT, ESC and FF */
57 case '\b': case '\t': case '\033': case '\014':
58 stats->printable++;
59 break;
28624193
DP
60 case 0:
61 stats->nul++;
62 /* fall through */
6c510bee
LT
63 default:
64 stats->nonprintable++;
65 }
66 }
67 else
68 stats->printable++;
69 }
f9dd4bf4
DK
70
71 /* If file ends with EOF then don't count this EOF as non-printable. */
72 if (size >= 1 && buf[size-1] == '\032')
73 stats->nonprintable--;
6c510bee
LT
74}
75
76/*
77 * The same heuristics as diff.c::mmfile_is_binary()
78 */
79static int is_binary(unsigned long size, struct text_stat *stats)
80{
81
28624193
DP
82 if (stats->nul)
83 return 1;
6c510bee
LT
84 if ((stats->printable >> 7) < stats->nonprintable)
85 return 1;
86 /*
87 * Other heuristics? Average line length might be relevant,
88 * as might LF vs CR vs CRLF counts..
89 *
90 * NOTE! It might be normal to have a low ratio of CRLF to LF
91 * (somebody starts with a LF-only file and edits it with an editor
92 * that adds CRLF only to lines that are added..). But do we
93 * want to support CR-only? Probably not.
94 */
95 return 0;
96}
97
c61dcff9 98static enum eol output_eol(enum crlf_action crlf_action)
f217f0e8 99{
c61dcff9 100 switch (crlf_action) {
942e7747
EB
101 case CRLF_BINARY:
102 return EOL_UNSET;
103 case CRLF_CRLF:
104 return EOL_CRLF;
105 case CRLF_INPUT:
106 return EOL_LF;
107 case CRLF_GUESS:
108 if (!auto_crlf)
109 return EOL_UNSET;
110 /* fall through */
111 case CRLF_TEXT:
112 case CRLF_AUTO:
113 if (auto_crlf == AUTO_CRLF_TRUE)
114 return EOL_CRLF;
115 else if (auto_crlf == AUTO_CRLF_INPUT)
116 return EOL_LF;
ec70f52f 117 else if (core_eol == EOL_UNSET)
942e7747
EB
118 return EOL_NATIVE;
119 }
ec70f52f 120 return core_eol;
942e7747
EB
121}
122
c61dcff9 123static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
21e5ad50
SP
124 struct text_stat *stats, enum safe_crlf checksafe)
125{
126 if (!checksafe)
127 return;
128
c61dcff9 129 if (output_eol(crlf_action) == EOL_LF) {
21e5ad50
SP
130 /*
131 * CRLFs would not be restored by checkout:
132 * check if we'd remove CRLFs
133 */
134 if (stats->crlf) {
135 if (checksafe == SAFE_CRLF_WARN)
942e7747 136 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
21e5ad50
SP
137 else /* i.e. SAFE_CRLF_FAIL */
138 die("CRLF would be replaced by LF in %s.", path);
139 }
c61dcff9 140 } else if (output_eol(crlf_action) == EOL_CRLF) {
21e5ad50
SP
141 /*
142 * CRLFs would be added by checkout:
143 * check if we have "naked" LFs
144 */
145 if (stats->lf != stats->crlf) {
146 if (checksafe == SAFE_CRLF_WARN)
942e7747 147 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
21e5ad50
SP
148 else /* i.e. SAFE_CRLF_FAIL */
149 die("LF would be replaced by CRLF in %s", path);
150 }
151 }
152}
153
c4805393
FAG
154static int has_cr_in_index(const char *path)
155{
c4805393 156 unsigned long sz;
c4805393
FAG
157 void *data;
158 int has_cr;
c4805393 159
4982fd78
LF
160 data = read_blob_data_from_cache(path, &sz);
161 if (!data)
c4805393 162 return 0;
c4805393
FAG
163 has_cr = memchr(data, '\r', sz) != NULL;
164 free(data);
165 return has_cr;
166}
167
5ecd293d 168static int crlf_to_git(const char *path, const char *src, size_t len,
c61dcff9
JH
169 struct strbuf *buf,
170 enum crlf_action crlf_action, enum safe_crlf checksafe)
6c510bee 171{
6c510bee 172 struct text_stat stats;
5ecd293d 173 char *dst;
6c510bee 174
c61dcff9 175 if (crlf_action == CRLF_BINARY ||
4c3b57b9
JK
176 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) ||
177 (src && !len))
5ecd293d 178 return 0;
6c510bee 179
4c3b57b9
JK
180 /*
181 * If we are doing a dry-run and have no source buffer, there is
182 * nothing to analyze; we must assume we would convert.
183 */
184 if (!buf && !src)
185 return 1;
186
5ecd293d 187 gather_stats(src, len, &stats);
6c510bee 188
c61dcff9 189 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
201ac8ef
JH
190 /*
191 * We're currently not going to even try to convert stuff
192 * that has bare CR characters. Does anybody do that crazy
193 * stuff?
194 */
195 if (stats.cr != stats.crlf)
5ecd293d 196 return 0;
201ac8ef
JH
197
198 /*
199 * And add some heuristics for binary vs text, of course...
200 */
5ecd293d
PH
201 if (is_binary(len, &stats))
202 return 0;
c4805393 203
c61dcff9 204 if (crlf_action == CRLF_GUESS) {
fd6cce9e
EB
205 /*
206 * If the file in the index has any CR in it, do not convert.
207 * This is the new safer autocrlf handling.
208 */
209 if (has_cr_in_index(path))
210 return 0;
211 }
201ac8ef 212 }
6c510bee 213
c61dcff9 214 check_safe_crlf(path, crlf_action, &stats, checksafe);
21e5ad50
SP
215
216 /* Optimization: No CR? Nothing to convert, regardless. */
217 if (!stats.cr)
218 return 0;
219
92ac3197
JK
220 /*
221 * At this point all of our source analysis is done, and we are sure we
222 * would convert. If we are in dry-run mode, we can give an answer.
223 */
224 if (!buf)
225 return 1;
226
90d16ec0
PH
227 /* only grow if not in place */
228 if (strbuf_avail(buf) + buf->len < len)
229 strbuf_grow(buf, len - buf->len);
5ecd293d 230 dst = buf->buf;
c61dcff9 231 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
163b9591
JH
232 /*
233 * If we guessed, we already know we rejected a file with
234 * lone CR, and we can strip a CR without looking at what
235 * follow it.
236 */
201ac8ef 237 do {
ac78e548 238 unsigned char c = *src++;
201ac8ef 239 if (c != '\r')
ac78e548 240 *dst++ = c;
5ecd293d 241 } while (--len);
201ac8ef
JH
242 } else {
243 do {
ac78e548 244 unsigned char c = *src++;
5ecd293d 245 if (! (c == '\r' && (1 < len && *src == '\n')))
ac78e548 246 *dst++ = c;
5ecd293d 247 } while (--len);
201ac8ef 248 }
5ecd293d
PH
249 strbuf_setlen(buf, dst - buf->buf);
250 return 1;
6c510bee
LT
251}
252
5ecd293d 253static int crlf_to_worktree(const char *path, const char *src, size_t len,
c61dcff9 254 struct strbuf *buf, enum crlf_action crlf_action)
6c510bee 255{
5ecd293d 256 char *to_free = NULL;
6c510bee 257 struct text_stat stats;
6c510bee 258
c61dcff9 259 if (!len || output_eol(crlf_action) != EOL_CRLF)
5ecd293d 260 return 0;
6c510bee 261
5ecd293d 262 gather_stats(src, len, &stats);
6c510bee
LT
263
264 /* No LF? Nothing to convert, regardless. */
265 if (!stats.lf)
5ecd293d 266 return 0;
6c510bee
LT
267
268 /* Was it already in CRLF format? */
269 if (stats.lf == stats.crlf)
5ecd293d 270 return 0;
6c510bee 271
c61dcff9
JH
272 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
273 if (crlf_action == CRLF_GUESS) {
fd6cce9e
EB
274 /* If we have any CR or CRLF line endings, we do not touch it */
275 /* This is the new safer autocrlf-handling */
276 if (stats.cr > 0 || stats.crlf > 0)
277 return 0;
278 }
c4805393 279
201ac8ef
JH
280 /* If we have any bare CR characters, we're not going to touch it */
281 if (stats.cr != stats.crlf)
5ecd293d 282 return 0;
6c510bee 283
5ecd293d
PH
284 if (is_binary(len, &stats))
285 return 0;
201ac8ef 286 }
6c510bee 287
5ecd293d
PH
288 /* are we "faking" in place editing ? */
289 if (src == buf->buf)
b315c5c0 290 to_free = strbuf_detach(buf, NULL);
5ecd293d
PH
291
292 strbuf_grow(buf, len + stats.lf - stats.crlf);
293 for (;;) {
294 const char *nl = memchr(src, '\n', len);
295 if (!nl)
296 break;
297 if (nl > src && nl[-1] == '\r') {
298 strbuf_add(buf, src, nl + 1 - src);
299 } else {
300 strbuf_add(buf, src, nl - src);
301 strbuf_addstr(buf, "\r\n");
302 }
303 len -= nl + 1 - src;
304 src = nl + 1;
305 }
306 strbuf_add(buf, src, len);
307
308 free(to_free);
309 return 1;
6c510bee 310}
35ebfd6a 311
546bb582
JS
312struct filter_params {
313 const char *src;
314 unsigned long size;
9035d75a 315 int fd;
546bb582 316 const char *cmd;
a2b665de 317 const char *path;
546bb582
JS
318};
319
9035d75a 320static int filter_buffer_or_fd(int in, int out, void *data)
aa4ed402
JH
321{
322 /*
323 * Spawn cmd and feed the buffer contents through its stdin.
324 */
d3180279 325 struct child_process child_process = CHILD_PROCESS_INIT;
546bb582 326 struct filter_params *params = (struct filter_params *)data;
aa4ed402 327 int write_err, status;
66dbfd55
GV
328 const char *argv[] = { NULL, NULL };
329
a2b665de
PW
330 /* apply % substitution to cmd */
331 struct strbuf cmd = STRBUF_INIT;
332 struct strbuf path = STRBUF_INIT;
333 struct strbuf_expand_dict_entry dict[] = {
334 { "f", NULL, },
335 { NULL, NULL, },
336 };
337
338 /* quote the path to preserve spaces, etc. */
339 sq_quote_buf(&path, params->path);
340 dict[0].value = path.buf;
341
342 /* expand all %f with the quoted path */
343 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
344 strbuf_release(&path);
345
346 argv[0] = cmd.buf;
aa4ed402 347
dc1bfdcd 348 child_process.argv = argv;
ac0ba18d 349 child_process.use_shell = 1;
dc1bfdcd 350 child_process.in = -1;
ae6a5609 351 child_process.out = out;
aa4ed402 352
dc1bfdcd 353 if (start_command(&child_process))
546bb582 354 return error("cannot fork to run external filter %s", params->cmd);
aa4ed402 355
6424c2ad
JB
356 sigchain_push(SIGPIPE, SIG_IGN);
357
9035d75a 358 if (params->src) {
0c4dd67a
JH
359 write_err = (write_in_full(child_process.in,
360 params->src, params->size) < 0);
361 if (errno == EPIPE)
362 write_err = 0;
9035d75a
SP
363 } else {
364 write_err = copy_fd(params->fd, child_process.in);
0c4dd67a
JH
365 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
366 write_err = 0;
9035d75a
SP
367 }
368
dc1bfdcd 369 if (close(child_process.in))
aa4ed402
JH
370 write_err = 1;
371 if (write_err)
546bb582 372 error("cannot feed the input to external filter %s", params->cmd);
aa4ed402 373
6424c2ad
JB
374 sigchain_pop(SIGPIPE);
375
aa4ed402
JH
376 status = finish_command(&child_process);
377 if (status)
5709e036 378 error("external filter %s failed %d", params->cmd, status);
a2b665de
PW
379
380 strbuf_release(&cmd);
aa4ed402
JH
381 return (write_err || status);
382}
383
9035d75a 384static int apply_filter(const char *path, const char *src, size_t len, int fd,
5ecd293d 385 struct strbuf *dst, const char *cmd)
aa4ed402
JH
386{
387 /*
388 * Create a pipeline to have the command filter the buffer's
389 * contents.
390 *
391 * (child --> cmd) --> us
392 */
546bb582 393 int ret = 1;
f285a2d7 394 struct strbuf nbuf = STRBUF_INIT;
546bb582
JS
395 struct async async;
396 struct filter_params params;
aa4ed402
JH
397
398 if (!cmd)
5ecd293d 399 return 0;
aa4ed402 400
92ac3197
JK
401 if (!dst)
402 return 1;
403
546bb582 404 memset(&async, 0, sizeof(async));
9035d75a 405 async.proc = filter_buffer_or_fd;
546bb582 406 async.data = &params;
ae6a5609 407 async.out = -1;
546bb582
JS
408 params.src = src;
409 params.size = len;
9035d75a 410 params.fd = fd;
546bb582 411 params.cmd = cmd;
a2b665de 412 params.path = path;
aa4ed402
JH
413
414 fflush(NULL);
546bb582
JS
415 if (start_async(&async))
416 return 0; /* error was already reported */
aa4ed402 417
546bb582 418 if (strbuf_read(&nbuf, async.out, len) < 0) {
5ecd293d
PH
419 error("read from external filter %s failed", cmd);
420 ret = 0;
aa4ed402 421 }
546bb582 422 if (close(async.out)) {
90d16ec0 423 error("read from external filter %s failed", cmd);
5ecd293d 424 ret = 0;
aa4ed402 425 }
546bb582
JS
426 if (finish_async(&async)) {
427 error("external filter %s failed", cmd);
5ecd293d 428 ret = 0;
aa4ed402
JH
429 }
430
5ecd293d 431 if (ret) {
90d16ec0 432 strbuf_swap(dst, &nbuf);
5ecd293d 433 }
90d16ec0 434 strbuf_release(&nbuf);
5ecd293d 435 return ret;
aa4ed402
JH
436}
437
438static struct convert_driver {
439 const char *name;
440 struct convert_driver *next;
cd8be6c9
BH
441 const char *smudge;
442 const char *clean;
36daaaca 443 int required;
aa4ed402
JH
444} *user_convert, **user_convert_tail;
445
ef90d6d4 446static int read_convert_config(const char *var, const char *value, void *cb)
aa4ed402 447{
d731f0ad 448 const char *key, *name;
aa4ed402
JH
449 int namelen;
450 struct convert_driver *drv;
451
452 /*
453 * External conversion drivers are configured using
454 * "filter.<name>.variable".
455 */
d731f0ad 456 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
aa4ed402 457 return 0;
aa4ed402
JH
458 for (drv = user_convert; drv; drv = drv->next)
459 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
460 break;
461 if (!drv) {
aa4ed402 462 drv = xcalloc(1, sizeof(struct convert_driver));
182af834 463 drv->name = xmemdupz(name, namelen);
aa4ed402
JH
464 *user_convert_tail = drv;
465 user_convert_tail = &(drv->next);
466 }
467
aa4ed402
JH
468 /*
469 * filter.<name>.smudge and filter.<name>.clean specifies
470 * the command line:
471 *
472 * command-line
473 *
474 * The command-line will not be interpolated in any way.
475 */
476
d731f0ad 477 if (!strcmp("smudge", key))
cd8be6c9
BH
478 return git_config_string(&drv->smudge, var, value);
479
d731f0ad 480 if (!strcmp("clean", key))
cd8be6c9 481 return git_config_string(&drv->clean, var, value);
aa4ed402 482
d731f0ad 483 if (!strcmp("required", key)) {
36daaaca
JB
484 drv->required = git_config_bool(var, value);
485 return 0;
486 }
487
aa4ed402
JH
488 return 0;
489}
490
3fed15f5
JH
491static int count_ident(const char *cp, unsigned long size)
492{
493 /*
af9b54bb 494 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
3fed15f5
JH
495 */
496 int cnt = 0;
497 char ch;
498
499 while (size) {
500 ch = *cp++;
501 size--;
502 if (ch != '$')
503 continue;
af9b54bb 504 if (size < 3)
3fed15f5 505 break;
af9b54bb 506 if (memcmp("Id", cp, 2))
3fed15f5 507 continue;
af9b54bb
AP
508 ch = cp[2];
509 cp += 3;
510 size -= 3;
3fed15f5 511 if (ch == '$')
af9b54bb 512 cnt++; /* $Id$ */
3fed15f5
JH
513 if (ch != ':')
514 continue;
515
516 /*
af9b54bb 517 * "$Id: ... "; scan up to the closing dollar sign and discard.
3fed15f5
JH
518 */
519 while (size) {
520 ch = *cp++;
521 size--;
522 if (ch == '$') {
523 cnt++;
524 break;
525 }
a9f3049f
HG
526 if (ch == '\n')
527 break;
3fed15f5
JH
528 }
529 }
530 return cnt;
531}
532
5ecd293d
PH
533static int ident_to_git(const char *path, const char *src, size_t len,
534 struct strbuf *buf, int ident)
3fed15f5 535{
5ecd293d 536 char *dst, *dollar;
3fed15f5 537
4c3b57b9 538 if (!ident || (src && !count_ident(src, len)))
5ecd293d
PH
539 return 0;
540
92ac3197
JK
541 if (!buf)
542 return 1;
543
90d16ec0
PH
544 /* only grow if not in place */
545 if (strbuf_avail(buf) + buf->len < len)
546 strbuf_grow(buf, len - buf->len);
5ecd293d
PH
547 dst = buf->buf;
548 for (;;) {
549 dollar = memchr(src, '$', len);
550 if (!dollar)
551 break;
77321184 552 memmove(dst, src, dollar + 1 - src);
5ecd293d
PH
553 dst += dollar + 1 - src;
554 len -= dollar + 1 - src;
555 src = dollar + 1;
556
557 if (len > 3 && !memcmp(src, "Id:", 3)) {
558 dollar = memchr(src + 3, '$', len - 3);
559 if (!dollar)
560 break;
a9f3049f
HG
561 if (memchr(src + 3, '\n', dollar - src - 3)) {
562 /* Line break before the next dollar. */
563 continue;
564 }
565
af9b54bb
AP
566 memcpy(dst, "Id$", 3);
567 dst += 3;
5ecd293d
PH
568 len -= dollar + 1 - src;
569 src = dollar + 1;
3fed15f5
JH
570 }
571 }
77321184 572 memmove(dst, src, len);
5ecd293d
PH
573 strbuf_setlen(buf, dst + len - buf->buf);
574 return 1;
3fed15f5
JH
575}
576
5ecd293d
PH
577static int ident_to_worktree(const char *path, const char *src, size_t len,
578 struct strbuf *buf, int ident)
3fed15f5 579{
3fed15f5 580 unsigned char sha1[20];
07814d90 581 char *to_free = NULL, *dollar, *spc;
5ecd293d 582 int cnt;
3fed15f5
JH
583
584 if (!ident)
5ecd293d 585 return 0;
3fed15f5 586
5ecd293d 587 cnt = count_ident(src, len);
3fed15f5 588 if (!cnt)
5ecd293d 589 return 0;
3fed15f5 590
5ecd293d
PH
591 /* are we "faking" in place editing ? */
592 if (src == buf->buf)
b315c5c0 593 to_free = strbuf_detach(buf, NULL);
5ecd293d 594 hash_sha1_file(src, len, "blob", sha1);
3fed15f5 595
5ecd293d
PH
596 strbuf_grow(buf, len + cnt * 43);
597 for (;;) {
598 /* step 1: run to the next '$' */
599 dollar = memchr(src, '$', len);
600 if (!dollar)
601 break;
602 strbuf_add(buf, src, dollar + 1 - src);
603 len -= dollar + 1 - src;
604 src = dollar + 1;
c23290d5 605
5ecd293d
PH
606 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
607 if (len < 3 || memcmp("Id", src, 2))
3fed15f5
JH
608 continue;
609
5ecd293d
PH
610 /* step 3: skip over Id$ or Id:xxxxx$ */
611 if (src[2] == '$') {
612 src += 3;
613 len -= 3;
614 } else if (src[2] == ':') {
615 /*
616 * It's possible that an expanded Id has crept its way into the
07814d90
HG
617 * repository, we cope with that by stripping the expansion out.
618 * This is probably not a good idea, since it will cause changes
619 * on checkout, which won't go away by stash, but let's keep it
620 * for git-style ids.
5ecd293d
PH
621 */
622 dollar = memchr(src + 3, '$', len - 3);
623 if (!dollar) {
624 /* incomplete keyword, no more '$', so just quit the loop */
625 break;
626 }
c23290d5 627
a9f3049f
HG
628 if (memchr(src + 3, '\n', dollar - src - 3)) {
629 /* Line break before the next dollar. */
630 continue;
631 }
632
07814d90
HG
633 spc = memchr(src + 4, ' ', dollar - src - 4);
634 if (spc && spc < dollar-1) {
635 /* There are spaces in unexpected places.
636 * This is probably an id from some other
637 * versioning system. Keep it for now.
638 */
639 continue;
640 }
641
5ecd293d
PH
642 len -= dollar + 1 - src;
643 src = dollar + 1;
644 } else {
645 /* it wasn't a "Id$" or "Id:xxxx$" */
646 continue;
647 }
c23290d5 648
5ecd293d
PH
649 /* step 4: substitute */
650 strbuf_addstr(buf, "Id: ");
651 strbuf_add(buf, sha1_to_hex(sha1), 40);
652 strbuf_addstr(buf, " $");
3fed15f5 653 }
5ecd293d 654 strbuf_add(buf, src, len);
3fed15f5 655
5ecd293d
PH
656 free(to_free);
657 return 1;
35ebfd6a
JH
658}
659
7356b51e 660static enum crlf_action git_path_check_crlf(const char *path, struct git_attr_check *check)
35ebfd6a 661{
6073ee85
JH
662 const char *value = check->value;
663
664 if (ATTR_TRUE(value))
665 return CRLF_TEXT;
666 else if (ATTR_FALSE(value))
667 return CRLF_BINARY;
668 else if (ATTR_UNSET(value))
669 ;
670 else if (!strcmp(value, "input"))
671 return CRLF_INPUT;
fd6cce9e
EB
672 else if (!strcmp(value, "auto"))
673 return CRLF_AUTO;
163b9591 674 return CRLF_GUESS;
35ebfd6a
JH
675}
676
ef563de6 677static enum eol git_path_check_eol(const char *path, struct git_attr_check *check)
fd6cce9e
EB
678{
679 const char *value = check->value;
680
681 if (ATTR_UNSET(value))
682 ;
683 else if (!strcmp(value, "lf"))
684 return EOL_LF;
685 else if (!strcmp(value, "crlf"))
686 return EOL_CRLF;
687 return EOL_UNSET;
688}
689
aa4ed402
JH
690static struct convert_driver *git_path_check_convert(const char *path,
691 struct git_attr_check *check)
692{
693 const char *value = check->value;
694 struct convert_driver *drv;
695
696 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
697 return NULL;
698 for (drv = user_convert; drv; drv = drv->next)
699 if (!strcmp(value, drv->name))
700 return drv;
701 return NULL;
702}
703
3fed15f5
JH
704static int git_path_check_ident(const char *path, struct git_attr_check *check)
705{
706 const char *value = check->value;
707
708 return !!ATTR_TRUE(value);
709}
710
c61dcff9 711static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol eol_attr)
f217f0e8 712{
5ec3e670 713 if (text_attr == CRLF_BINARY)
fd6cce9e
EB
714 return CRLF_BINARY;
715 if (eol_attr == EOL_LF)
716 return CRLF_INPUT;
717 if (eol_attr == EOL_CRLF)
718 return CRLF_CRLF;
5ec3e670 719 return text_attr;
fd6cce9e
EB
720}
721
3bfba20d
JH
722struct conv_attrs {
723 struct convert_driver *drv;
724 enum crlf_action crlf_action;
725 enum eol eol_attr;
726 int ident;
727};
728
83295964
JH
729static const char *conv_attr_name[] = {
730 "crlf", "ident", "filter", "eol", "text",
731};
732#define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
733
3bfba20d 734static void convert_attrs(struct conv_attrs *ca, const char *path)
83295964
JH
735{
736 int i;
737 static struct git_attr_check ccheck[NUM_CONV_ATTRS];
738
739 if (!ccheck[0].attr) {
740 for (i = 0; i < NUM_CONV_ATTRS; i++)
741 ccheck[i].attr = git_attr(conv_attr_name[i]);
742 user_convert_tail = &user_convert;
743 git_config(read_convert_config, NULL);
744 }
3bfba20d 745
d932f4eb 746 if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
3bfba20d
JH
747 ca->crlf_action = git_path_check_crlf(path, ccheck + 4);
748 if (ca->crlf_action == CRLF_GUESS)
749 ca->crlf_action = git_path_check_crlf(path, ccheck + 0);
750 ca->ident = git_path_check_ident(path, ccheck + 1);
751 ca->drv = git_path_check_convert(path, ccheck + 2);
752 ca->eol_attr = git_path_check_eol(path, ccheck + 3);
753 } else {
754 ca->drv = NULL;
755 ca->crlf_action = CRLF_GUESS;
756 ca->eol_attr = EOL_UNSET;
757 ca->ident = 0;
758 }
83295964
JH
759}
760
9035d75a
SP
761int would_convert_to_git_filter_fd(const char *path)
762{
763 struct conv_attrs ca;
764
765 convert_attrs(&ca, path);
766 if (!ca.drv)
767 return 0;
768
769 /*
770 * Apply a filter to an fd only if the filter is required to succeed.
771 * We must die if the filter fails, because the original data before
772 * filtering is not available.
773 */
774 if (!ca.drv->required)
775 return 0;
776
777 return apply_filter(path, NULL, 0, -1, NULL, ca.drv->clean);
778}
779
21e5ad50
SP
780int convert_to_git(const char *path, const char *src, size_t len,
781 struct strbuf *dst, enum safe_crlf checksafe)
35ebfd6a 782{
3bfba20d 783 int ret = 0;
cd8be6c9 784 const char *filter = NULL;
36daaaca 785 int required = 0;
3bfba20d 786 struct conv_attrs ca;
6073ee85 787
3bfba20d 788 convert_attrs(&ca, path);
36daaaca 789 if (ca.drv) {
3bfba20d 790 filter = ca.drv->clean;
36daaaca
JB
791 required = ca.drv->required;
792 }
3fed15f5 793
9035d75a 794 ret |= apply_filter(path, src, len, -1, dst, filter);
36daaaca
JB
795 if (!ret && required)
796 die("%s: clean filter '%s' failed", path, ca.drv->name);
797
92ac3197 798 if (ret && dst) {
5ecd293d
PH
799 src = dst->buf;
800 len = dst->len;
aa4ed402 801 }
3bfba20d
JH
802 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
803 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
92ac3197 804 if (ret && dst) {
5ecd293d
PH
805 src = dst->buf;
806 len = dst->len;
6073ee85 807 }
3bfba20d 808 return ret | ident_to_git(path, src, len, dst, ca.ident);
35ebfd6a
JH
809}
810
9035d75a
SP
811void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
812 enum safe_crlf checksafe)
813{
814 struct conv_attrs ca;
815 convert_attrs(&ca, path);
816
817 assert(ca.drv);
818 assert(ca.drv->clean);
819
820 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))
821 die("%s: clean filter '%s' failed", path, ca.drv->name);
822
823 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
824 crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
825 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
826}
827
43dd2332
EB
828static int convert_to_working_tree_internal(const char *path, const char *src,
829 size_t len, struct strbuf *dst,
830 int normalizing)
35ebfd6a 831{
36daaaca 832 int ret = 0, ret_filter = 0;
cd8be6c9 833 const char *filter = NULL;
36daaaca 834 int required = 0;
3bfba20d 835 struct conv_attrs ca;
6073ee85 836
3bfba20d 837 convert_attrs(&ca, path);
36daaaca 838 if (ca.drv) {
3bfba20d 839 filter = ca.drv->smudge;
36daaaca
JB
840 required = ca.drv->required;
841 }
3fed15f5 842
3bfba20d 843 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
5ecd293d
PH
844 if (ret) {
845 src = dst->buf;
846 len = dst->len;
3fed15f5 847 }
43dd2332
EB
848 /*
849 * CRLF conversion can be skipped if normalizing, unless there
850 * is a smudge filter. The filter might expect CRLFs.
851 */
852 if (filter || !normalizing) {
3bfba20d
JH
853 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
854 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
43dd2332
EB
855 if (ret) {
856 src = dst->buf;
857 len = dst->len;
858 }
aa4ed402 859 }
36daaaca 860
9035d75a 861 ret_filter = apply_filter(path, src, len, -1, dst, filter);
36daaaca
JB
862 if (!ret_filter && required)
863 die("%s: smudge filter %s failed", path, ca.drv->name);
864
865 return ret | ret_filter;
35ebfd6a 866}
f217f0e8 867
43dd2332
EB
868int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
869{
870 return convert_to_working_tree_internal(path, src, len, dst, 0);
871}
872
f217f0e8
EB
873int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
874{
43dd2332 875 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
f217f0e8
EB
876 if (ret) {
877 src = dst->buf;
878 len = dst->len;
879 }
7356b51e 880 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_FALSE);
f217f0e8 881}
dd8e9121 882
b6691092
JH
883/*****************************************************************
884 *
749f763d 885 * Streaming conversion support
b6691092
JH
886 *
887 *****************************************************************/
888
889typedef int (*filter_fn)(struct stream_filter *,
890 const char *input, size_t *isize_p,
891 char *output, size_t *osize_p);
892typedef void (*free_fn)(struct stream_filter *);
893
894struct stream_filter_vtbl {
895 filter_fn filter;
896 free_fn free;
897};
898
899struct stream_filter {
900 struct stream_filter_vtbl *vtbl;
901};
902
903static int null_filter_fn(struct stream_filter *filter,
904 const char *input, size_t *isize_p,
905 char *output, size_t *osize_p)
906{
4ae66704
JH
907 size_t count;
908
909 if (!input)
910 return 0; /* we do not keep any states */
911 count = *isize_p;
b6691092
JH
912 if (*osize_p < count)
913 count = *osize_p;
914 if (count) {
915 memmove(output, input, count);
916 *isize_p -= count;
917 *osize_p -= count;
918 }
919 return 0;
920}
921
922static void null_free_fn(struct stream_filter *filter)
923{
924 ; /* nothing -- null instances are shared */
925}
926
927static struct stream_filter_vtbl null_vtbl = {
928 null_filter_fn,
929 null_free_fn,
930};
931
932static struct stream_filter null_filter_singleton = {
933 &null_vtbl,
934};
935
936int is_null_stream_filter(struct stream_filter *filter)
937{
938 return filter == &null_filter_singleton;
939}
940
b84c7839
JH
941
942/*
943 * LF-to-CRLF filter
944 */
284e3d28
CMN
945
946struct lf_to_crlf_filter {
947 struct stream_filter filter;
8496f568
JH
948 unsigned has_held:1;
949 char held;
284e3d28
CMN
950};
951
e322ee38
JH
952static int lf_to_crlf_filter_fn(struct stream_filter *filter,
953 const char *input, size_t *isize_p,
954 char *output, size_t *osize_p)
955{
284e3d28
CMN
956 size_t count, o = 0;
957 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
958
8496f568
JH
959 /*
960 * We may be holding onto the CR to see if it is followed by a
961 * LF, in which case we would need to go to the main loop.
962 * Otherwise, just emit it to the output stream.
963 */
964 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
965 output[o++] = lf_to_crlf->held;
966 lf_to_crlf->has_held = 0;
284e3d28 967 }
e322ee38 968
87afe9a5
JH
969 /* We are told to drain */
970 if (!input) {
971 *osize_p -= o;
972 return 0;
973 }
e322ee38 974
e322ee38 975 count = *isize_p;
8496f568 976 if (count || lf_to_crlf->has_held) {
284e3d28 977 size_t i;
8496f568
JH
978 int was_cr = 0;
979
980 if (lf_to_crlf->has_held) {
981 was_cr = 1;
982 lf_to_crlf->has_held = 0;
983 }
984
284e3d28 985 for (i = 0; o < *osize_p && i < count; i++) {
e322ee38 986 char ch = input[i];
8496f568 987
e322ee38 988 if (ch == '\n') {
284e3d28 989 output[o++] = '\r';
8496f568
JH
990 } else if (was_cr) {
991 /*
992 * Previous round saw CR and it is not followed
993 * by a LF; emit the CR before processing the
994 * current character.
995 */
996 output[o++] = '\r';
e322ee38 997 }
8496f568
JH
998
999 /*
1000 * We may have consumed the last output slot,
1001 * in which case we need to break out of this
1002 * loop; hold the current character before
1003 * returning.
1004 */
1005 if (*osize_p <= o) {
1006 lf_to_crlf->has_held = 1;
1007 lf_to_crlf->held = ch;
1008 continue; /* break but increment i */
1009 }
1010
1011 if (ch == '\r') {
1012 was_cr = 1;
1013 continue;
1014 }
1015
1016 was_cr = 0;
e322ee38
JH
1017 output[o++] = ch;
1018 }
1019
1020 *osize_p -= o;
1021 *isize_p -= i;
8496f568
JH
1022
1023 if (!lf_to_crlf->has_held && was_cr) {
1024 lf_to_crlf->has_held = 1;
1025 lf_to_crlf->held = '\r';
1026 }
e322ee38
JH
1027 }
1028 return 0;
1029}
1030
284e3d28
CMN
1031static void lf_to_crlf_free_fn(struct stream_filter *filter)
1032{
1033 free(filter);
1034}
1035
e322ee38
JH
1036static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1037 lf_to_crlf_filter_fn,
284e3d28 1038 lf_to_crlf_free_fn,
e322ee38
JH
1039};
1040
284e3d28
CMN
1041static struct stream_filter *lf_to_crlf_filter(void)
1042{
87afe9a5 1043 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
e322ee38 1044
284e3d28 1045 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
284e3d28
CMN
1046 return (struct stream_filter *)lf_to_crlf;
1047}
b84c7839 1048
a265a7f9
JH
1049/*
1050 * Cascade filter
1051 */
1052#define FILTER_BUFFER 1024
1053struct cascade_filter {
1054 struct stream_filter filter;
1055 struct stream_filter *one;
1056 struct stream_filter *two;
1057 char buf[FILTER_BUFFER];
1058 int end, ptr;
1059};
1060
1061static int cascade_filter_fn(struct stream_filter *filter,
1062 const char *input, size_t *isize_p,
1063 char *output, size_t *osize_p)
1064{
1065 struct cascade_filter *cas = (struct cascade_filter *) filter;
1066 size_t filled = 0;
1067 size_t sz = *osize_p;
1068 size_t to_feed, remaining;
1069
1070 /*
1071 * input -- (one) --> buf -- (two) --> output
1072 */
1073 while (filled < sz) {
1074 remaining = sz - filled;
1075
1076 /* do we already have something to feed two with? */
1077 if (cas->ptr < cas->end) {
1078 to_feed = cas->end - cas->ptr;
1079 if (stream_filter(cas->two,
1080 cas->buf + cas->ptr, &to_feed,
1081 output + filled, &remaining))
1082 return -1;
1083 cas->ptr += (cas->end - cas->ptr) - to_feed;
1084 filled = sz - remaining;
1085 continue;
1086 }
1087
1088 /* feed one from upstream and have it emit into our buffer */
1089 to_feed = input ? *isize_p : 0;
1090 if (input && !to_feed)
1091 break;
1092 remaining = sizeof(cas->buf);
1093 if (stream_filter(cas->one,
1094 input, &to_feed,
1095 cas->buf, &remaining))
1096 return -1;
1097 cas->end = sizeof(cas->buf) - remaining;
1098 cas->ptr = 0;
1099 if (input) {
1100 size_t fed = *isize_p - to_feed;
1101 *isize_p -= fed;
1102 input += fed;
1103 }
1104
1105 /* do we know that we drained one completely? */
1106 if (input || cas->end)
1107 continue;
1108
1109 /* tell two to drain; we have nothing more to give it */
1110 to_feed = 0;
1111 remaining = sz - filled;
1112 if (stream_filter(cas->two,
1113 NULL, &to_feed,
1114 output + filled, &remaining))
1115 return -1;
1116 if (remaining == (sz - filled))
1117 break; /* completely drained two */
1118 filled = sz - remaining;
1119 }
1120 *osize_p -= filled;
1121 return 0;
1122}
1123
1124static void cascade_free_fn(struct stream_filter *filter)
1125{
1126 struct cascade_filter *cas = (struct cascade_filter *)filter;
1127 free_stream_filter(cas->one);
1128 free_stream_filter(cas->two);
1129 free(filter);
1130}
1131
1132static struct stream_filter_vtbl cascade_vtbl = {
1133 cascade_filter_fn,
1134 cascade_free_fn,
1135};
1136
1137static struct stream_filter *cascade_filter(struct stream_filter *one,
1138 struct stream_filter *two)
1139{
1140 struct cascade_filter *cascade;
1141
1142 if (!one || is_null_stream_filter(one))
1143 return two;
1144 if (!two || is_null_stream_filter(two))
1145 return one;
1146
1147 cascade = xmalloc(sizeof(*cascade));
1148 cascade->one = one;
1149 cascade->two = two;
1150 cascade->end = cascade->ptr = 0;
1151 cascade->filter.vtbl = &cascade_vtbl;
1152 return (struct stream_filter *)cascade;
1153}
1154
b84c7839
JH
1155/*
1156 * ident filter
1157 */
1158#define IDENT_DRAINING (-1)
1159#define IDENT_SKIPPING (-2)
1160struct ident_filter {
1161 struct stream_filter filter;
1162 struct strbuf left;
1163 int state;
1164 char ident[45]; /* ": x40 $" */
1165};
1166
1167static int is_foreign_ident(const char *str)
1168{
1169 int i;
1170
ae021d87 1171 if (!skip_prefix(str, "$Id: ", &str))
b84c7839 1172 return 0;
ae021d87 1173 for (i = 0; str[i]; i++) {
b84c7839
JH
1174 if (isspace(str[i]) && str[i+1] != '$')
1175 return 1;
1176 }
1177 return 0;
1178}
1179
1180static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1181{
1182 size_t to_drain = ident->left.len;
1183
1184 if (*osize_p < to_drain)
1185 to_drain = *osize_p;
1186 if (to_drain) {
1187 memcpy(*output_p, ident->left.buf, to_drain);
1188 strbuf_remove(&ident->left, 0, to_drain);
1189 *output_p += to_drain;
1190 *osize_p -= to_drain;
1191 }
1192 if (!ident->left.len)
1193 ident->state = 0;
1194}
1195
1196static int ident_filter_fn(struct stream_filter *filter,
1197 const char *input, size_t *isize_p,
1198 char *output, size_t *osize_p)
1199{
1200 struct ident_filter *ident = (struct ident_filter *)filter;
1201 static const char head[] = "$Id";
1202
1203 if (!input) {
1204 /* drain upon eof */
1205 switch (ident->state) {
1206 default:
1207 strbuf_add(&ident->left, head, ident->state);
1208 case IDENT_SKIPPING:
1209 /* fallthru */
1210 case IDENT_DRAINING:
1211 ident_drain(ident, &output, osize_p);
1212 }
1213 return 0;
1214 }
1215
1216 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1217 int ch;
1218
1219 if (ident->state == IDENT_DRAINING) {
1220 ident_drain(ident, &output, osize_p);
1221 if (!*osize_p)
1222 break;
1223 continue;
1224 }
1225
1226 ch = *(input++);
1227 (*isize_p)--;
1228
1229 if (ident->state == IDENT_SKIPPING) {
1230 /*
1231 * Skipping until '$' or LF, but keeping them
1232 * in case it is a foreign ident.
1233 */
1234 strbuf_addch(&ident->left, ch);
1235 if (ch != '\n' && ch != '$')
1236 continue;
1237 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1238 strbuf_setlen(&ident->left, sizeof(head) - 1);
1239 strbuf_addstr(&ident->left, ident->ident);
1240 }
1241 ident->state = IDENT_DRAINING;
1242 continue;
1243 }
1244
1245 if (ident->state < sizeof(head) &&
1246 head[ident->state] == ch) {
1247 ident->state++;
1248 continue;
1249 }
1250
1251 if (ident->state)
1252 strbuf_add(&ident->left, head, ident->state);
1253 if (ident->state == sizeof(head) - 1) {
1254 if (ch != ':' && ch != '$') {
1255 strbuf_addch(&ident->left, ch);
1256 ident->state = 0;
1257 continue;
1258 }
1259
1260 if (ch == ':') {
1261 strbuf_addch(&ident->left, ch);
1262 ident->state = IDENT_SKIPPING;
1263 } else {
1264 strbuf_addstr(&ident->left, ident->ident);
1265 ident->state = IDENT_DRAINING;
1266 }
1267 continue;
1268 }
1269
1270 strbuf_addch(&ident->left, ch);
1271 ident->state = IDENT_DRAINING;
1272 }
1273 return 0;
1274}
1275
1276static void ident_free_fn(struct stream_filter *filter)
1277{
1278 struct ident_filter *ident = (struct ident_filter *)filter;
1279 strbuf_release(&ident->left);
1280 free(filter);
1281}
1282
1283static struct stream_filter_vtbl ident_vtbl = {
1284 ident_filter_fn,
1285 ident_free_fn,
1286};
1287
1288static struct stream_filter *ident_filter(const unsigned char *sha1)
1289{
1290 struct ident_filter *ident = xmalloc(sizeof(*ident));
1291
1292 sprintf(ident->ident, ": %s $", sha1_to_hex(sha1));
1293 strbuf_init(&ident->left, 0);
1294 ident->filter.vtbl = &ident_vtbl;
1295 ident->state = 0;
1296 return (struct stream_filter *)ident;
1297}
1298
dd8e9121 1299/*
b6691092
JH
1300 * Return an appropriately constructed filter for the path, or NULL if
1301 * the contents cannot be filtered without reading the whole thing
1302 * in-core.
1303 *
1304 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1305 * large binary blob you would want us not to slurp into the memory!
dd8e9121 1306 */
b6691092 1307struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
dd8e9121
JH
1308{
1309 struct conv_attrs ca;
1310 enum crlf_action crlf_action;
b84c7839 1311 struct stream_filter *filter = NULL;
dd8e9121
JH
1312
1313 convert_attrs(&ca, path);
1314
b84c7839
JH
1315 if (ca.drv && (ca.drv->smudge || ca.drv->clean))
1316 return filter;
1317
1318 if (ca.ident)
1319 filter = ident_filter(sha1);
dd8e9121
JH
1320
1321 crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
b84c7839 1322
b0d9c69f 1323 if ((crlf_action == CRLF_BINARY) || (crlf_action == CRLF_INPUT) ||
a265a7f9
JH
1324 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE))
1325 filter = cascade_filter(filter, &null_filter_singleton);
1326
1327 else if (output_eol(crlf_action) == EOL_CRLF &&
1328 !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS))
284e3d28 1329 filter = cascade_filter(filter, lf_to_crlf_filter());
e322ee38 1330
b84c7839 1331 return filter;
b6691092
JH
1332}
1333
1334void free_stream_filter(struct stream_filter *filter)
1335{
1336 filter->vtbl->free(filter);
1337}
1338
1339int stream_filter(struct stream_filter *filter,
1340 const char *input, size_t *isize_p,
1341 char *output, size_t *osize_p)
1342{
1343 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);
dd8e9121 1344}