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