Update messages in preparation for i18n
[git/git.git] / convert.c
CommitLineData
a33e0b2a 1#define NO_THE_INDEX_COMPATIBILITY_MACROS
6c510bee 2#include "cache.h"
b2141fc1 3#include "config.h"
35ebfd6a 4#include "attr.h"
3fed15f5 5#include "run-command.h"
a2b665de 6#include "quote.h"
6424c2ad 7#include "sigchain.h"
edcc8581 8#include "pkt-line.h"
99605d62 9#include "sub-process.h"
107642fe 10#include "utf8.h"
35ebfd6a 11
6c510bee
LT
12/*
13 * convert.c - convert a file when checking it out and checking it in.
14 *
15 * This should use the pathname to decide on whether it wants to do some
16 * more interesting conversions (automatic gzip/unzip, general format
17 * conversions etc etc), but by default it just does automatic CRLF<->LF
942e7747 18 * translation when the "text" attribute or "auto_crlf" option is set.
6c510bee
LT
19 */
20
a7630bd4
TB
21/* Stat bits: When BIN is set, the txt bits are unset */
22#define CONVERT_STAT_BITS_TXT_LF 0x1
23#define CONVERT_STAT_BITS_TXT_CRLF 0x2
24#define CONVERT_STAT_BITS_BIN 0x4
25
c61dcff9 26enum crlf_action {
df747b81
TB
27 CRLF_UNDEFINED,
28 CRLF_BINARY,
fd6cce9e 29 CRLF_TEXT,
df747b81
TB
30 CRLF_TEXT_INPUT,
31 CRLF_TEXT_CRLF,
32 CRLF_AUTO,
33 CRLF_AUTO_INPUT,
34 CRLF_AUTO_CRLF
fd6cce9e 35};
163b9591 36
6c510bee 37struct text_stat {
28624193 38 /* NUL, CR, LF and CRLF counts */
6e336a53 39 unsigned nul, lonecr, lonelf, crlf;
6c510bee
LT
40
41 /* These are just approximations! */
42 unsigned printable, nonprintable;
43};
44
45static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
46{
47 unsigned long i;
48
49 memset(stats, 0, sizeof(*stats));
50
51 for (i = 0; i < size; i++) {
52 unsigned char c = buf[i];
53 if (c == '\r') {
6e336a53 54 if (i+1 < size && buf[i+1] == '\n') {
6c510bee 55 stats->crlf++;
6e336a53
TB
56 i++;
57 } else
58 stats->lonecr++;
6c510bee
LT
59 continue;
60 }
61 if (c == '\n') {
6e336a53 62 stats->lonelf++;
6c510bee
LT
63 continue;
64 }
65 if (c == 127)
66 /* DEL */
67 stats->nonprintable++;
68 else if (c < 32) {
69 switch (c) {
70 /* BS, HT, ESC and FF */
71 case '\b': case '\t': case '\033': case '\014':
72 stats->printable++;
73 break;
28624193
DP
74 case 0:
75 stats->nul++;
76 /* fall through */
6c510bee
LT
77 default:
78 stats->nonprintable++;
79 }
80 }
81 else
82 stats->printable++;
83 }
f9dd4bf4
DK
84
85 /* If file ends with EOF then don't count this EOF as non-printable. */
86 if (size >= 1 && buf[size-1] == '\032')
87 stats->nonprintable--;
6c510bee
LT
88}
89
90/*
91 * The same heuristics as diff.c::mmfile_is_binary()
a7630bd4 92 * We treat files with bare CR as binary
6c510bee 93 */
a7630bd4 94static int convert_is_binary(unsigned long size, const struct text_stat *stats)
6c510bee 95{
6e336a53 96 if (stats->lonecr)
a7630bd4 97 return 1;
28624193
DP
98 if (stats->nul)
99 return 1;
6c510bee
LT
100 if ((stats->printable >> 7) < stats->nonprintable)
101 return 1;
6c510bee
LT
102 return 0;
103}
104
a7630bd4
TB
105static unsigned int gather_convert_stats(const char *data, unsigned long size)
106{
107 struct text_stat stats;
6e336a53 108 int ret = 0;
a7630bd4
TB
109 if (!data || !size)
110 return 0;
111 gather_stats(data, size, &stats);
112 if (convert_is_binary(size, &stats))
6e336a53
TB
113 ret |= CONVERT_STAT_BITS_BIN;
114 if (stats.crlf)
115 ret |= CONVERT_STAT_BITS_TXT_CRLF;
116 if (stats.lonelf)
117 ret |= CONVERT_STAT_BITS_TXT_LF;
118
119 return ret;
a7630bd4
TB
120}
121
122static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
123{
124 unsigned int convert_stats = gather_convert_stats(data, size);
125
126 if (convert_stats & CONVERT_STAT_BITS_BIN)
127 return "-text";
128 switch (convert_stats) {
129 case CONVERT_STAT_BITS_TXT_LF:
130 return "lf";
131 case CONVERT_STAT_BITS_TXT_CRLF:
132 return "crlf";
133 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
134 return "mixed";
135 default:
136 return "none";
137 }
138}
139
a7609c54
BW
140const char *get_cached_convert_stats_ascii(const struct index_state *istate,
141 const char *path)
a7630bd4
TB
142{
143 const char *ret;
144 unsigned long sz;
a7609c54 145 void *data = read_blob_data_from_index(istate, path, &sz);
a7630bd4
TB
146 ret = gather_convert_stats_ascii(data, sz);
147 free(data);
148 return ret;
149}
150
151const char *get_wt_convert_stats_ascii(const char *path)
152{
153 const char *ret = "";
154 struct strbuf sb = STRBUF_INIT;
155 if (strbuf_read_file(&sb, path, 0) >= 0)
156 ret = gather_convert_stats_ascii(sb.buf, sb.len);
157 strbuf_release(&sb);
158 return ret;
159}
160
4b4024f5
TB
161static int text_eol_is_crlf(void)
162{
163 if (auto_crlf == AUTO_CRLF_TRUE)
164 return 1;
165 else if (auto_crlf == AUTO_CRLF_INPUT)
166 return 0;
167 if (core_eol == EOL_CRLF)
168 return 1;
169 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
170 return 1;
171 return 0;
172}
173
c61dcff9 174static enum eol output_eol(enum crlf_action crlf_action)
f217f0e8 175{
c61dcff9 176 switch (crlf_action) {
942e7747
EB
177 case CRLF_BINARY:
178 return EOL_UNSET;
df747b81 179 case CRLF_TEXT_CRLF:
942e7747 180 return EOL_CRLF;
df747b81 181 case CRLF_TEXT_INPUT:
942e7747 182 return EOL_LF;
df747b81
TB
183 case CRLF_UNDEFINED:
184 case CRLF_AUTO_CRLF:
65237284 185 return EOL_CRLF;
df747b81 186 case CRLF_AUTO_INPUT:
65237284 187 return EOL_LF;
942e7747
EB
188 case CRLF_TEXT:
189 case CRLF_AUTO:
df747b81 190 /* fall through */
4b4024f5 191 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
942e7747 192 }
1a07e59c 193 warning("illegal crlf_action %d", (int)crlf_action);
ec70f52f 194 return core_eol;
942e7747
EB
195}
196
8462ff43 197static void check_global_conv_flags_eol(const char *path, enum crlf_action crlf_action,
a0ad53c1 198 struct text_stat *old_stats, struct text_stat *new_stats,
8462ff43 199 int conv_flags)
21e5ad50 200{
a0ad53c1 201 if (old_stats->crlf && !new_stats->crlf ) {
21e5ad50 202 /*
a0ad53c1 203 * CRLFs would not be restored by checkout
21e5ad50 204 */
8462ff43 205 if (conv_flags & CONV_EOL_RNDTRP_DIE)
1a07e59c 206 die(_("CRLF would be replaced by LF in %s"), path);
8462ff43 207 else if (conv_flags & CONV_EOL_RNDTRP_WARN)
87cb7845
VA
208 warning(_("CRLF will be replaced by LF in %s.\n"
209 "The file will have its original line"
210 " endings in your working directory."), path);
a0ad53c1 211 } else if (old_stats->lonelf && !new_stats->lonelf ) {
21e5ad50 212 /*
a0ad53c1 213 * CRLFs would be added by checkout
21e5ad50 214 */
8462ff43
TB
215 if (conv_flags & CONV_EOL_RNDTRP_DIE)
216 die(_("LF would be replaced by CRLF in %s"), path);
217 else if (conv_flags & CONV_EOL_RNDTRP_WARN)
87cb7845
VA
218 warning(_("LF will be replaced by CRLF in %s.\n"
219 "The file will have its original line"
220 " endings in your working directory."), path);
21e5ad50
SP
221 }
222}
223
86ff70a0 224static int has_crlf_in_index(const struct index_state *istate, const char *path)
c4805393 225{
c4805393 226 unsigned long sz;
c4805393 227 void *data;
86ff70a0
TB
228 const char *crp;
229 int has_crlf = 0;
c4805393 230
49a6d31f 231 data = read_blob_data_from_index(istate, path, &sz);
4982fd78 232 if (!data)
c4805393 233 return 0;
86ff70a0
TB
234
235 crp = memchr(data, '\r', sz);
236 if (crp) {
237 unsigned int ret_stats;
238 ret_stats = gather_convert_stats(data, sz);
239 if (!(ret_stats & CONVERT_STAT_BITS_BIN) &&
240 (ret_stats & CONVERT_STAT_BITS_TXT_CRLF))
241 has_crlf = 1;
242 }
c4805393 243 free(data);
86ff70a0 244 return has_crlf;
c4805393
FAG
245}
246
a0ad53c1
TB
247static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
248 enum crlf_action crlf_action)
249{
250 if (output_eol(crlf_action) != EOL_CRLF)
251 return 0;
252 /* No "naked" LF? Nothing to convert, regardless. */
253 if (!stats->lonelf)
254 return 0;
255
256 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
257 /* If we have any CR or CRLF line endings, we do not touch it */
258 /* This is the new safer autocrlf-handling */
259 if (stats->lonecr || stats->crlf)
260 return 0;
261
262 if (convert_is_binary(len, stats))
263 return 0;
264 }
265 return 1;
266
267}
268
7a17918c
LS
269static int validate_encoding(const char *path, const char *enc,
270 const char *data, size_t len, int die_on_error)
271{
272 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
273 if (istarts_with(enc, "UTF")) {
274 /*
275 * Check for detectable errors in UTF encodings
276 */
277 if (has_prohibited_utf_bom(enc, data, len)) {
278 const char *error_msg = _(
279 "BOM is prohibited in '%s' if encoded as %s");
280 /*
281 * This advice is shown for UTF-??BE and UTF-??LE encodings.
282 * We cut off the last two characters of the encoding name
283 * to generate the encoding name suitable for BOMs.
284 */
285 const char *advise_msg = _(
286 "The file '%s' contains a byte order "
287 "mark (BOM). Please use UTF-%s as "
288 "working-tree-encoding.");
289 const char *stripped = NULL;
290 char *upper = xstrdup_toupper(enc);
291 upper[strlen(upper)-2] = '\0';
292 if (!skip_prefix(upper, "UTF-", &stripped))
293 skip_prefix(stripped, "UTF", &stripped);
294 advise(advise_msg, path, stripped);
295 free(upper);
296 if (die_on_error)
297 die(error_msg, path, enc);
298 else {
299 return error(error_msg, path, enc);
300 }
301
302 } else if (is_missing_required_utf_bom(enc, data, len)) {
303 const char *error_msg = _(
304 "BOM is required in '%s' if encoded as %s");
305 const char *advise_msg = _(
306 "The file '%s' is missing a byte order "
307 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
308 "(depending on the byte order) as "
309 "working-tree-encoding.");
310 const char *stripped = NULL;
311 char *upper = xstrdup_toupper(enc);
312 if (!skip_prefix(upper, "UTF-", &stripped))
313 skip_prefix(stripped, "UTF", &stripped);
314 advise(advise_msg, path, stripped, stripped);
315 free(upper);
316 if (die_on_error)
317 die(error_msg, path, enc);
318 else {
319 return error(error_msg, path, enc);
320 }
321 }
322
323 }
324 return 0;
325}
326
541d059c
LS
327static void trace_encoding(const char *context, const char *path,
328 const char *encoding, const char *buf, size_t len)
329{
330 static struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING);
331 struct strbuf trace = STRBUF_INIT;
332 int i;
333
334 strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding);
335 for (i = 0; i < len && buf; ++i) {
336 strbuf_addf(
337 &trace,"| \e[2m%2i:\e[0m %2x \e[2m%c\e[0m%c",
338 i,
339 (unsigned char) buf[i],
340 (buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '),
341 ((i+1) % 8 && (i+1) < len ? ' ' : '\n')
342 );
343 }
344 strbuf_addchars(&trace, '\n', 1);
345
346 trace_strbuf(&coe, &trace);
347 strbuf_release(&trace);
348}
349
e92d6225
LS
350static int check_roundtrip(const char *enc_name)
351{
352 /*
353 * check_roundtrip_encoding contains a string of comma and/or
354 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
355 * Search for the given encoding in that string.
356 */
357 const char *found = strcasestr(check_roundtrip_encoding, enc_name);
358 const char *next;
359 int len;
360 if (!found)
361 return 0;
362 next = found + strlen(enc_name);
363 len = strlen(check_roundtrip_encoding);
364 return (found && (
365 /*
366 * check that the found encoding is at the
367 * beginning of check_roundtrip_encoding or
368 * that it is prefixed with a space or comma
369 */
370 found == check_roundtrip_encoding || (
371 (isspace(found[-1]) || found[-1] == ',')
372 )
373 ) && (
374 /*
375 * check that the found encoding is at the
376 * end of check_roundtrip_encoding or
377 * that it is suffixed with a space or comma
378 */
379 next == check_roundtrip_encoding + len || (
380 next < check_roundtrip_encoding + len &&
381 (isspace(next[0]) || next[0] == ',')
382 )
383 ));
384}
385
107642fe
LS
386static const char *default_encoding = "UTF-8";
387
388static int encode_to_git(const char *path, const char *src, size_t src_len,
389 struct strbuf *buf, const char *enc, int conv_flags)
390{
391 char *dst;
392 int dst_len;
393 int die_on_error = conv_flags & CONV_WRITE_OBJECT;
394
395 /*
396 * No encoding is specified or there is nothing to encode.
397 * Tell the caller that the content was not modified.
398 */
399 if (!enc || (src && !src_len))
400 return 0;
401
402 /*
403 * Looks like we got called from "would_convert_to_git()".
404 * This means Git wants to know if it would encode (= modify!)
405 * the content. Let's answer with "yes", since an encoding was
406 * specified.
407 */
408 if (!buf && !src)
409 return 1;
410
7a17918c
LS
411 if (validate_encoding(path, enc, src, src_len, die_on_error))
412 return 0;
413
541d059c 414 trace_encoding("source", path, enc, src, src_len);
107642fe
LS
415 dst = reencode_string_len(src, src_len, default_encoding, enc,
416 &dst_len);
417 if (!dst) {
418 /*
419 * We could add the blob "as-is" to Git. However, on checkout
420 * we would try to reencode to the original encoding. This
421 * would fail and we would leave the user with a messed-up
422 * working tree. Let's try to avoid this by screaming loud.
423 */
424 const char* msg = _("failed to encode '%s' from %s to %s");
425 if (die_on_error)
426 die(msg, path, enc, default_encoding);
427 else {
428 error(msg, path, enc, default_encoding);
429 return 0;
430 }
431 }
541d059c 432 trace_encoding("destination", path, default_encoding, dst, dst_len);
107642fe 433
e92d6225
LS
434 /*
435 * UTF supports lossless conversion round tripping [1] and conversions
436 * between UTF and other encodings are mostly round trip safe as
437 * Unicode aims to be a superset of all other character encodings.
438 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
439 * trip issues [2]. Check the round trip conversion for all encodings
440 * listed in core.checkRoundtripEncoding.
441 *
442 * The round trip check is only performed if content is written to Git.
443 * This ensures that no information is lost during conversion to/from
444 * the internal UTF-8 representation.
445 *
446 * Please note, the code below is not tested because I was not able to
447 * generate a faulty round trip without an iconv error. Iconv errors
448 * are already caught above.
449 *
450 * [1] http://unicode.org/faq/utf_bom.html#gen2
451 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
452 */
453 if (die_on_error && check_roundtrip(enc)) {
454 char *re_src;
455 int re_src_len;
456
457 re_src = reencode_string_len(dst, dst_len,
458 enc, default_encoding,
459 &re_src_len);
460
461 trace_printf("Checking roundtrip encoding for %s...\n", enc);
462 trace_encoding("reencoded source", path, enc,
463 re_src, re_src_len);
464
465 if (!re_src || src_len != re_src_len ||
466 memcmp(src, re_src, src_len)) {
467 const char* msg = _("encoding '%s' from %s to %s and "
468 "back is not the same");
469 die(msg, path, enc, default_encoding);
470 }
471
472 free(re_src);
473 }
474
107642fe
LS
475 strbuf_attach(buf, dst, dst_len, dst_len + 1);
476 return 1;
477}
478
479static int encode_to_worktree(const char *path, const char *src, size_t src_len,
480 struct strbuf *buf, const char *enc)
481{
482 char *dst;
483 int dst_len;
484
485 /*
486 * No encoding is specified or there is nothing to encode.
487 * Tell the caller that the content was not modified.
488 */
489 if (!enc || (src && !src_len))
490 return 0;
491
492 dst = reencode_string_len(src, src_len, enc, default_encoding,
493 &dst_len);
494 if (!dst) {
495 error("failed to encode '%s' from %s to %s",
1a07e59c 496 path, default_encoding, enc);
107642fe
LS
497 return 0;
498 }
499
500 strbuf_attach(buf, dst, dst_len, dst_len + 1);
501 return 1;
502}
503
49a6d31f
BW
504static int crlf_to_git(const struct index_state *istate,
505 const char *path, const char *src, size_t len,
c61dcff9 506 struct strbuf *buf,
8462ff43 507 enum crlf_action crlf_action, int conv_flags)
6c510bee 508{
6c510bee 509 struct text_stat stats;
5ecd293d 510 char *dst;
a0ad53c1 511 int convert_crlf_into_lf;
6c510bee 512
c61dcff9 513 if (crlf_action == CRLF_BINARY ||
4c3b57b9 514 (src && !len))
5ecd293d 515 return 0;
6c510bee 516
4c3b57b9
JK
517 /*
518 * If we are doing a dry-run and have no source buffer, there is
519 * nothing to analyze; we must assume we would convert.
520 */
521 if (!buf && !src)
522 return 1;
523
5ecd293d 524 gather_stats(src, len, &stats);
a0ad53c1
TB
525 /* Optimization: No CRLF? Nothing to convert, regardless. */
526 convert_crlf_into_lf = !!stats.crlf;
6c510bee 527
df747b81 528 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
a7630bd4 529 if (convert_is_binary(len, &stats))
5ecd293d 530 return 0;
65237284 531 /*
1c25d2d8
TB
532 * If the file in the index has any CR in it, do not
533 * convert. This is the new safer autocrlf handling,
534 * unless we want to renormalize in a merge or
535 * cherry-pick.
65237284 536 */
8462ff43 537 if ((!(conv_flags & CONV_EOL_RENORMALIZE)) &&
86ff70a0 538 has_crlf_in_index(istate, path))
a0ad53c1 539 convert_crlf_into_lf = 0;
201ac8ef 540 }
8462ff43
TB
541 if (((conv_flags & CONV_EOL_RNDTRP_WARN) ||
542 ((conv_flags & CONV_EOL_RNDTRP_DIE) && len))) {
a0ad53c1
TB
543 struct text_stat new_stats;
544 memcpy(&new_stats, &stats, sizeof(new_stats));
545 /* simulate "git add" */
546 if (convert_crlf_into_lf) {
547 new_stats.lonelf += new_stats.crlf;
548 new_stats.crlf = 0;
549 }
550 /* simulate "git checkout" */
551 if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) {
552 new_stats.crlf += new_stats.lonelf;
553 new_stats.lonelf = 0;
554 }
8462ff43 555 check_global_conv_flags_eol(path, crlf_action, &stats, &new_stats, conv_flags);
a0ad53c1
TB
556 }
557 if (!convert_crlf_into_lf)
21e5ad50
SP
558 return 0;
559
92ac3197
JK
560 /*
561 * At this point all of our source analysis is done, and we are sure we
562 * would convert. If we are in dry-run mode, we can give an answer.
563 */
564 if (!buf)
565 return 1;
566
90d16ec0
PH
567 /* only grow if not in place */
568 if (strbuf_avail(buf) + buf->len < len)
569 strbuf_grow(buf, len - buf->len);
5ecd293d 570 dst = buf->buf;
df747b81 571 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
163b9591
JH
572 /*
573 * If we guessed, we already know we rejected a file with
574 * lone CR, and we can strip a CR without looking at what
575 * follow it.
576 */
201ac8ef 577 do {
ac78e548 578 unsigned char c = *src++;
201ac8ef 579 if (c != '\r')
ac78e548 580 *dst++ = c;
5ecd293d 581 } while (--len);
201ac8ef
JH
582 } else {
583 do {
ac78e548 584 unsigned char c = *src++;
5ecd293d 585 if (! (c == '\r' && (1 < len && *src == '\n')))
ac78e548 586 *dst++ = c;
5ecd293d 587 } while (--len);
201ac8ef 588 }
5ecd293d
PH
589 strbuf_setlen(buf, dst - buf->buf);
590 return 1;
6c510bee
LT
591}
592
5ecd293d 593static int crlf_to_worktree(const char *path, const char *src, size_t len,
c61dcff9 594 struct strbuf *buf, enum crlf_action crlf_action)
6c510bee 595{
5ecd293d 596 char *to_free = NULL;
6c510bee 597 struct text_stat stats;
6c510bee 598
c61dcff9 599 if (!len || output_eol(crlf_action) != EOL_CRLF)
5ecd293d 600 return 0;
6c510bee 601
5ecd293d 602 gather_stats(src, len, &stats);
a0ad53c1 603 if (!will_convert_lf_to_crlf(len, &stats, crlf_action))
5ecd293d 604 return 0;
6c510bee 605
5ecd293d
PH
606 /* are we "faking" in place editing ? */
607 if (src == buf->buf)
b315c5c0 608 to_free = strbuf_detach(buf, NULL);
5ecd293d 609
6e336a53 610 strbuf_grow(buf, len + stats.lonelf);
5ecd293d
PH
611 for (;;) {
612 const char *nl = memchr(src, '\n', len);
613 if (!nl)
614 break;
615 if (nl > src && nl[-1] == '\r') {
616 strbuf_add(buf, src, nl + 1 - src);
617 } else {
618 strbuf_add(buf, src, nl - src);
619 strbuf_addstr(buf, "\r\n");
620 }
621 len -= nl + 1 - src;
622 src = nl + 1;
623 }
624 strbuf_add(buf, src, len);
625
626 free(to_free);
627 return 1;
6c510bee 628}
35ebfd6a 629
546bb582
JS
630struct filter_params {
631 const char *src;
632 unsigned long size;
9035d75a 633 int fd;
546bb582 634 const char *cmd;
a2b665de 635 const char *path;
546bb582
JS
636};
637
9035d75a 638static int filter_buffer_or_fd(int in, int out, void *data)
aa4ed402
JH
639{
640 /*
641 * Spawn cmd and feed the buffer contents through its stdin.
642 */
d3180279 643 struct child_process child_process = CHILD_PROCESS_INIT;
546bb582 644 struct filter_params *params = (struct filter_params *)data;
aa4ed402 645 int write_err, status;
66dbfd55
GV
646 const char *argv[] = { NULL, NULL };
647
a2b665de
PW
648 /* apply % substitution to cmd */
649 struct strbuf cmd = STRBUF_INIT;
650 struct strbuf path = STRBUF_INIT;
651 struct strbuf_expand_dict_entry dict[] = {
652 { "f", NULL, },
653 { NULL, NULL, },
654 };
655
656 /* quote the path to preserve spaces, etc. */
657 sq_quote_buf(&path, params->path);
658 dict[0].value = path.buf;
659
660 /* expand all %f with the quoted path */
661 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
662 strbuf_release(&path);
663
664 argv[0] = cmd.buf;
aa4ed402 665
dc1bfdcd 666 child_process.argv = argv;
ac0ba18d 667 child_process.use_shell = 1;
dc1bfdcd 668 child_process.in = -1;
ae6a5609 669 child_process.out = out;
aa4ed402 670
f31f1d39
RS
671 if (start_command(&child_process)) {
672 strbuf_release(&cmd);
255f04d6 673 return error("cannot fork to run external filter '%s'", params->cmd);
f31f1d39 674 }
aa4ed402 675
6424c2ad
JB
676 sigchain_push(SIGPIPE, SIG_IGN);
677
9035d75a 678 if (params->src) {
0c4dd67a
JH
679 write_err = (write_in_full(child_process.in,
680 params->src, params->size) < 0);
681 if (errno == EPIPE)
682 write_err = 0;
9035d75a
SP
683 } else {
684 write_err = copy_fd(params->fd, child_process.in);
0c4dd67a
JH
685 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
686 write_err = 0;
9035d75a
SP
687 }
688
dc1bfdcd 689 if (close(child_process.in))
aa4ed402
JH
690 write_err = 1;
691 if (write_err)
255f04d6 692 error("cannot feed the input to external filter '%s'", params->cmd);
aa4ed402 693
6424c2ad
JB
694 sigchain_pop(SIGPIPE);
695
aa4ed402
JH
696 status = finish_command(&child_process);
697 if (status)
255f04d6 698 error("external filter '%s' failed %d", params->cmd, status);
a2b665de
PW
699
700 strbuf_release(&cmd);
aa4ed402
JH
701 return (write_err || status);
702}
703
234fa07e 704static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
5ecd293d 705 struct strbuf *dst, const char *cmd)
aa4ed402
JH
706{
707 /*
708 * Create a pipeline to have the command filter the buffer's
709 * contents.
710 *
711 * (child --> cmd) --> us
712 */
b84be553 713 int err = 0;
f285a2d7 714 struct strbuf nbuf = STRBUF_INIT;
546bb582
JS
715 struct async async;
716 struct filter_params params;
aa4ed402 717
546bb582 718 memset(&async, 0, sizeof(async));
9035d75a 719 async.proc = filter_buffer_or_fd;
546bb582 720 async.data = &params;
ae6a5609 721 async.out = -1;
546bb582
JS
722 params.src = src;
723 params.size = len;
9035d75a 724 params.fd = fd;
546bb582 725 params.cmd = cmd;
a2b665de 726 params.path = path;
aa4ed402
JH
727
728 fflush(NULL);
546bb582
JS
729 if (start_async(&async))
730 return 0; /* error was already reported */
aa4ed402 731
546bb582 732 if (strbuf_read(&nbuf, async.out, len) < 0) {
b84be553 733 err = error("read from external filter '%s' failed", cmd);
aa4ed402 734 }
546bb582 735 if (close(async.out)) {
b84be553 736 err = error("read from external filter '%s' failed", cmd);
aa4ed402 737 }
546bb582 738 if (finish_async(&async)) {
b84be553 739 err = error("external filter '%s' failed", cmd);
aa4ed402
JH
740 }
741
b84be553 742 if (!err) {
90d16ec0 743 strbuf_swap(dst, &nbuf);
5ecd293d 744 }
90d16ec0 745 strbuf_release(&nbuf);
b84be553 746 return !err;
aa4ed402
JH
747}
748
234fa07e
LS
749#define CAP_CLEAN (1u<<0)
750#define CAP_SMUDGE (1u<<1)
2841e8f8 751#define CAP_DELAY (1u<<2)
234fa07e 752
1b0b46ee
BP
753struct cmd2process {
754 struct subprocess_entry subprocess; /* must be the first member! */
755 unsigned int supported_capabilities;
756};
757
f514d7d1
BP
758static int subprocess_map_initialized;
759static struct hashmap subprocess_map;
edcc8581 760
7ddb9b2c 761static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
edcc8581 762{
fa64a2fd
JT
763 static int versions[] = {2, 0};
764 static struct subprocess_capability capabilities[] = {
1514c8ed
LS
765 { "clean", CAP_CLEAN },
766 { "smudge", CAP_SMUDGE },
2841e8f8 767 { "delay", CAP_DELAY },
fa64a2fd 768 { NULL, 0 }
1514c8ed 769 };
fa64a2fd
JT
770 struct cmd2process *entry = (struct cmd2process *)subprocess;
771 return subprocess_handshake(subprocess, "git-filter", versions, NULL,
772 capabilities,
773 &entry->supported_capabilities);
a810ea99
BP
774}
775
9364fc29
LS
776static void handle_filter_error(const struct strbuf *filter_status,
777 struct cmd2process *entry,
778 const unsigned int wanted_capability) {
779 if (!strcmp(filter_status->buf, "error"))
780 ; /* The filter signaled a problem with the file. */
781 else if (!strcmp(filter_status->buf, "abort") && wanted_capability) {
782 /*
783 * The filter signaled a permanent problem. Don't try to filter
784 * files with the same command for the lifetime of the current
785 * Git process.
786 */
787 entry->supported_capabilities &= ~wanted_capability;
788 } else {
789 /*
790 * Something went wrong with the protocol filter.
791 * Force shutdown and restart if another blob requires filtering.
792 */
793 error("external filter '%s' failed", entry->subprocess.cmd);
794 subprocess_stop(&subprocess_map, &entry->subprocess);
795 free(entry);
796 }
797}
798
edcc8581
LS
799static int apply_multi_file_filter(const char *path, const char *src, size_t len,
800 int fd, struct strbuf *dst, const char *cmd,
2841e8f8
LS
801 const unsigned int wanted_capability,
802 struct delayed_checkout *dco)
edcc8581
LS
803{
804 int err;
2841e8f8 805 int can_delay = 0;
edcc8581
LS
806 struct cmd2process *entry;
807 struct child_process *process;
808 struct strbuf nbuf = STRBUF_INIT;
809 struct strbuf filter_status = STRBUF_INIT;
810 const char *filter_type;
811
f514d7d1
BP
812 if (!subprocess_map_initialized) {
813 subprocess_map_initialized = 1;
9ab42958 814 hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
edcc8581
LS
815 entry = NULL;
816 } else {
f514d7d1 817 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
edcc8581
LS
818 }
819
820 fflush(NULL);
821
822 if (!entry) {
7ddb9b2c
BP
823 entry = xmalloc(sizeof(*entry));
824 entry->supported_capabilities = 0;
825
f514d7d1 826 if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
7ddb9b2c 827 free(entry);
edcc8581 828 return 0;
7ddb9b2c 829 }
edcc8581 830 }
1b0b46ee 831 process = &entry->subprocess.process;
edcc8581 832
42b0a86c 833 if (!(entry->supported_capabilities & wanted_capability))
edcc8581
LS
834 return 0;
835
42b0a86c 836 if (wanted_capability & CAP_CLEAN)
edcc8581 837 filter_type = "clean";
42b0a86c 838 else if (wanted_capability & CAP_SMUDGE)
edcc8581
LS
839 filter_type = "smudge";
840 else
841 die("unexpected filter type");
842
843 sigchain_push(SIGPIPE, SIG_IGN);
844
845 assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
846 err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
847 if (err)
848 goto done;
849
850 err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
851 if (err) {
852 error("path name too long for external filter");
853 goto done;
854 }
855
856 err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
857 if (err)
858 goto done;
859
2841e8f8
LS
860 if ((entry->supported_capabilities & CAP_DELAY) &&
861 dco && dco->state == CE_CAN_DELAY) {
862 can_delay = 1;
863 err = packet_write_fmt_gently(process->in, "can-delay=1\n");
864 if (err)
865 goto done;
866 }
867
edcc8581
LS
868 err = packet_flush_gently(process->in);
869 if (err)
870 goto done;
871
872 if (fd >= 0)
873 err = write_packetized_from_fd(fd, process->in);
874 else
875 err = write_packetized_from_buf(src, len, process->in);
876 if (err)
877 goto done;
878
4f2a2e9f
BP
879 err = subprocess_read_status(process->out, &filter_status);
880 if (err)
881 goto done;
882
2841e8f8
LS
883 if (can_delay && !strcmp(filter_status.buf, "delayed")) {
884 string_list_insert(&dco->filters, cmd);
885 string_list_insert(&dco->paths, path);
886 } else {
887 /* The filter got the blob and wants to send us a response. */
888 err = strcmp(filter_status.buf, "success");
889 if (err)
890 goto done;
891
892 err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
893 if (err)
894 goto done;
895
896 err = subprocess_read_status(process->out, &filter_status);
897 if (err)
898 goto done;
899
900 err = strcmp(filter_status.buf, "success");
901 }
902
903done:
904 sigchain_pop(SIGPIPE);
905
906 if (err)
907 handle_filter_error(&filter_status, entry, wanted_capability);
908 else
909 strbuf_swap(dst, &nbuf);
910 strbuf_release(&nbuf);
911 return !err;
912}
913
914
915int async_query_available_blobs(const char *cmd, struct string_list *available_paths)
916{
917 int err;
918 char *line;
919 struct cmd2process *entry;
920 struct child_process *process;
921 struct strbuf filter_status = STRBUF_INIT;
922
923 assert(subprocess_map_initialized);
924 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
925 if (!entry) {
926 error("external filter '%s' is not available anymore although "
927 "not all paths have been filtered", cmd);
928 return 0;
929 }
930 process = &entry->subprocess.process;
931 sigchain_push(SIGPIPE, SIG_IGN);
932
933 err = packet_write_fmt_gently(
934 process->in, "command=list_available_blobs\n");
edcc8581
LS
935 if (err)
936 goto done;
937
2841e8f8 938 err = packet_flush_gently(process->in);
edcc8581
LS
939 if (err)
940 goto done;
941
2841e8f8
LS
942 while ((line = packet_read_line(process->out, NULL))) {
943 const char *path;
944 if (skip_prefix(line, "pathname=", &path))
945 string_list_insert(available_paths, xstrdup(path));
946 else
947 ; /* ignore unknown keys */
948 }
949
4f2a2e9f
BP
950 err = subprocess_read_status(process->out, &filter_status);
951 if (err)
952 goto done;
953
edcc8581
LS
954 err = strcmp(filter_status.buf, "success");
955
956done:
957 sigchain_pop(SIGPIPE);
958
9364fc29 959 if (err)
2841e8f8 960 handle_filter_error(&filter_status, entry, 0);
edcc8581 961 return !err;
aa4ed402
JH
962}
963
964static struct convert_driver {
965 const char *name;
966 struct convert_driver *next;
cd8be6c9
BH
967 const char *smudge;
968 const char *clean;
edcc8581 969 const char *process;
36daaaca 970 int required;
aa4ed402
JH
971} *user_convert, **user_convert_tail;
972
234fa07e
LS
973static int apply_filter(const char *path, const char *src, size_t len,
974 int fd, struct strbuf *dst, struct convert_driver *drv,
2841e8f8
LS
975 const unsigned int wanted_capability,
976 struct delayed_checkout *dco)
234fa07e
LS
977{
978 const char *cmd = NULL;
979
980 if (!drv)
981 return 0;
982
983 if (!dst)
984 return 1;
985
42b0a86c 986 if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean)
234fa07e 987 cmd = drv->clean;
42b0a86c 988 else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge)
234fa07e
LS
989 cmd = drv->smudge;
990
991 if (cmd && *cmd)
992 return apply_single_file_filter(path, src, len, fd, dst, cmd);
edcc8581 993 else if (drv->process && *drv->process)
2841e8f8
LS
994 return apply_multi_file_filter(path, src, len, fd, dst,
995 drv->process, wanted_capability, dco);
234fa07e
LS
996
997 return 0;
998}
999
ef90d6d4 1000static int read_convert_config(const char *var, const char *value, void *cb)
aa4ed402 1001{
d731f0ad 1002 const char *key, *name;
aa4ed402
JH
1003 int namelen;
1004 struct convert_driver *drv;
1005
1006 /*
1007 * External conversion drivers are configured using
1008 * "filter.<name>.variable".
1009 */
d731f0ad 1010 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
aa4ed402 1011 return 0;
aa4ed402
JH
1012 for (drv = user_convert; drv; drv = drv->next)
1013 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
1014 break;
1015 if (!drv) {
aa4ed402 1016 drv = xcalloc(1, sizeof(struct convert_driver));
182af834 1017 drv->name = xmemdupz(name, namelen);
aa4ed402
JH
1018 *user_convert_tail = drv;
1019 user_convert_tail = &(drv->next);
1020 }
1021
aa4ed402
JH
1022 /*
1023 * filter.<name>.smudge and filter.<name>.clean specifies
1024 * the command line:
1025 *
1026 * command-line
1027 *
1028 * The command-line will not be interpolated in any way.
1029 */
1030
d731f0ad 1031 if (!strcmp("smudge", key))
cd8be6c9
BH
1032 return git_config_string(&drv->smudge, var, value);
1033
d731f0ad 1034 if (!strcmp("clean", key))
cd8be6c9 1035 return git_config_string(&drv->clean, var, value);
aa4ed402 1036
edcc8581
LS
1037 if (!strcmp("process", key))
1038 return git_config_string(&drv->process, var, value);
1039
d731f0ad 1040 if (!strcmp("required", key)) {
36daaaca
JB
1041 drv->required = git_config_bool(var, value);
1042 return 0;
1043 }
1044
aa4ed402
JH
1045 return 0;
1046}
1047
3fed15f5
JH
1048static int count_ident(const char *cp, unsigned long size)
1049{
1050 /*
af9b54bb 1051 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
3fed15f5
JH
1052 */
1053 int cnt = 0;
1054 char ch;
1055
1056 while (size) {
1057 ch = *cp++;
1058 size--;
1059 if (ch != '$')
1060 continue;
af9b54bb 1061 if (size < 3)
3fed15f5 1062 break;
af9b54bb 1063 if (memcmp("Id", cp, 2))
3fed15f5 1064 continue;
af9b54bb
AP
1065 ch = cp[2];
1066 cp += 3;
1067 size -= 3;
3fed15f5 1068 if (ch == '$')
af9b54bb 1069 cnt++; /* $Id$ */
3fed15f5
JH
1070 if (ch != ':')
1071 continue;
1072
1073 /*
af9b54bb 1074 * "$Id: ... "; scan up to the closing dollar sign and discard.
3fed15f5
JH
1075 */
1076 while (size) {
1077 ch = *cp++;
1078 size--;
1079 if (ch == '$') {
1080 cnt++;
1081 break;
1082 }
a9f3049f
HG
1083 if (ch == '\n')
1084 break;
3fed15f5
JH
1085 }
1086 }
1087 return cnt;
1088}
1089
5ecd293d
PH
1090static int ident_to_git(const char *path, const char *src, size_t len,
1091 struct strbuf *buf, int ident)
3fed15f5 1092{
5ecd293d 1093 char *dst, *dollar;
3fed15f5 1094
4c3b57b9 1095 if (!ident || (src && !count_ident(src, len)))
5ecd293d
PH
1096 return 0;
1097
92ac3197
JK
1098 if (!buf)
1099 return 1;
1100
90d16ec0
PH
1101 /* only grow if not in place */
1102 if (strbuf_avail(buf) + buf->len < len)
1103 strbuf_grow(buf, len - buf->len);
5ecd293d
PH
1104 dst = buf->buf;
1105 for (;;) {
1106 dollar = memchr(src, '$', len);
1107 if (!dollar)
1108 break;
77321184 1109 memmove(dst, src, dollar + 1 - src);
5ecd293d
PH
1110 dst += dollar + 1 - src;
1111 len -= dollar + 1 - src;
1112 src = dollar + 1;
1113
1114 if (len > 3 && !memcmp(src, "Id:", 3)) {
1115 dollar = memchr(src + 3, '$', len - 3);
1116 if (!dollar)
1117 break;
a9f3049f
HG
1118 if (memchr(src + 3, '\n', dollar - src - 3)) {
1119 /* Line break before the next dollar. */
1120 continue;
1121 }
1122
af9b54bb
AP
1123 memcpy(dst, "Id$", 3);
1124 dst += 3;
5ecd293d
PH
1125 len -= dollar + 1 - src;
1126 src = dollar + 1;
3fed15f5
JH
1127 }
1128 }
77321184 1129 memmove(dst, src, len);
5ecd293d
PH
1130 strbuf_setlen(buf, dst + len - buf->buf);
1131 return 1;
3fed15f5
JH
1132}
1133
5ecd293d
PH
1134static int ident_to_worktree(const char *path, const char *src, size_t len,
1135 struct strbuf *buf, int ident)
3fed15f5 1136{
f070facc 1137 struct object_id oid;
07814d90 1138 char *to_free = NULL, *dollar, *spc;
5ecd293d 1139 int cnt;
3fed15f5
JH
1140
1141 if (!ident)
5ecd293d 1142 return 0;
3fed15f5 1143
5ecd293d 1144 cnt = count_ident(src, len);
3fed15f5 1145 if (!cnt)
5ecd293d 1146 return 0;
3fed15f5 1147
5ecd293d
PH
1148 /* are we "faking" in place editing ? */
1149 if (src == buf->buf)
b315c5c0 1150 to_free = strbuf_detach(buf, NULL);
f070facc 1151 hash_object_file(src, len, "blob", &oid);
3fed15f5 1152
1a750441 1153 strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3));
5ecd293d
PH
1154 for (;;) {
1155 /* step 1: run to the next '$' */
1156 dollar = memchr(src, '$', len);
1157 if (!dollar)
1158 break;
1159 strbuf_add(buf, src, dollar + 1 - src);
1160 len -= dollar + 1 - src;
1161 src = dollar + 1;
c23290d5 1162
5ecd293d
PH
1163 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1164 if (len < 3 || memcmp("Id", src, 2))
3fed15f5
JH
1165 continue;
1166
5ecd293d
PH
1167 /* step 3: skip over Id$ or Id:xxxxx$ */
1168 if (src[2] == '$') {
1169 src += 3;
1170 len -= 3;
1171 } else if (src[2] == ':') {
1172 /*
1173 * It's possible that an expanded Id has crept its way into the
07814d90
HG
1174 * repository, we cope with that by stripping the expansion out.
1175 * This is probably not a good idea, since it will cause changes
1176 * on checkout, which won't go away by stash, but let's keep it
1177 * for git-style ids.
5ecd293d
PH
1178 */
1179 dollar = memchr(src + 3, '$', len - 3);
1180 if (!dollar) {
1181 /* incomplete keyword, no more '$', so just quit the loop */
1182 break;
1183 }
c23290d5 1184
a9f3049f
HG
1185 if (memchr(src + 3, '\n', dollar - src - 3)) {
1186 /* Line break before the next dollar. */
1187 continue;
1188 }
1189
07814d90
HG
1190 spc = memchr(src + 4, ' ', dollar - src - 4);
1191 if (spc && spc < dollar-1) {
1192 /* There are spaces in unexpected places.
1193 * This is probably an id from some other
1194 * versioning system. Keep it for now.
1195 */
1196 continue;
1197 }
1198
5ecd293d
PH
1199 len -= dollar + 1 - src;
1200 src = dollar + 1;
1201 } else {
1202 /* it wasn't a "Id$" or "Id:xxxx$" */
1203 continue;
1204 }
c23290d5 1205
5ecd293d
PH
1206 /* step 4: substitute */
1207 strbuf_addstr(buf, "Id: ");
f070facc 1208 strbuf_addstr(buf, oid_to_hex(&oid));
5ecd293d 1209 strbuf_addstr(buf, " $");
3fed15f5 1210 }
5ecd293d 1211 strbuf_add(buf, src, len);
3fed15f5 1212
5ecd293d
PH
1213 free(to_free);
1214 return 1;
35ebfd6a
JH
1215}
1216
107642fe
LS
1217static const char *git_path_check_encoding(struct attr_check_item *check)
1218{
1219 const char *value = check->value;
1220
1221 if (ATTR_UNSET(value) || !strlen(value))
1222 return NULL;
1223
1224 if (ATTR_TRUE(value) || ATTR_FALSE(value)) {
1225 die(_("true/false are no valid working-tree-encodings"));
1226 }
1227
1228 /* Don't encode to the default encoding */
1229 if (same_encoding(value, default_encoding))
1230 return NULL;
1231
1232 return value;
1233}
1234
7bd18054 1235static enum crlf_action git_path_check_crlf(struct attr_check_item *check)
35ebfd6a 1236{
6073ee85
JH
1237 const char *value = check->value;
1238
1239 if (ATTR_TRUE(value))
1240 return CRLF_TEXT;
1241 else if (ATTR_FALSE(value))
1242 return CRLF_BINARY;
1243 else if (ATTR_UNSET(value))
1244 ;
1245 else if (!strcmp(value, "input"))
df747b81 1246 return CRLF_TEXT_INPUT;
fd6cce9e
EB
1247 else if (!strcmp(value, "auto"))
1248 return CRLF_AUTO;
df747b81 1249 return CRLF_UNDEFINED;
35ebfd6a
JH
1250}
1251
7bd18054 1252static enum eol git_path_check_eol(struct attr_check_item *check)
fd6cce9e
EB
1253{
1254 const char *value = check->value;
1255
1256 if (ATTR_UNSET(value))
1257 ;
1258 else if (!strcmp(value, "lf"))
1259 return EOL_LF;
1260 else if (!strcmp(value, "crlf"))
1261 return EOL_CRLF;
1262 return EOL_UNSET;
1263}
1264
7bd18054 1265static struct convert_driver *git_path_check_convert(struct attr_check_item *check)
aa4ed402
JH
1266{
1267 const char *value = check->value;
1268 struct convert_driver *drv;
1269
1270 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
1271 return NULL;
1272 for (drv = user_convert; drv; drv = drv->next)
1273 if (!strcmp(value, drv->name))
1274 return drv;
1275 return NULL;
1276}
1277
7bd18054 1278static int git_path_check_ident(struct attr_check_item *check)
3fed15f5
JH
1279{
1280 const char *value = check->value;
1281
1282 return !!ATTR_TRUE(value);
1283}
1284
3bfba20d
JH
1285struct conv_attrs {
1286 struct convert_driver *drv;
bb211b4d
TB
1287 enum crlf_action attr_action; /* What attr says */
1288 enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
3bfba20d 1289 int ident;
107642fe 1290 const char *working_tree_encoding; /* Supported encoding or default encoding if NULL */
3bfba20d
JH
1291};
1292
3bfba20d 1293static void convert_attrs(struct conv_attrs *ca, const char *path)
83295964 1294{
2aef63d3 1295 static struct attr_check *check;
83295964 1296
2aef63d3
JH
1297 if (!check) {
1298 check = attr_check_initl("crlf", "ident", "filter",
107642fe
LS
1299 "eol", "text", "working-tree-encoding",
1300 NULL);
83295964
JH
1301 user_convert_tail = &user_convert;
1302 git_config(read_convert_config, NULL);
1303 }
3bfba20d 1304
2aef63d3
JH
1305 if (!git_check_attr(path, check)) {
1306 struct attr_check_item *ccheck = check->items;
bb211b4d 1307 ca->crlf_action = git_path_check_crlf(ccheck + 4);
df747b81 1308 if (ca->crlf_action == CRLF_UNDEFINED)
92cce135
TB
1309 ca->crlf_action = git_path_check_crlf(ccheck + 0);
1310 ca->ident = git_path_check_ident(ccheck + 1);
1311 ca->drv = git_path_check_convert(ccheck + 2);
817a0c79
TB
1312 if (ca->crlf_action != CRLF_BINARY) {
1313 enum eol eol_attr = git_path_check_eol(ccheck + 3);
65237284
TB
1314 if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
1315 ca->crlf_action = CRLF_AUTO_INPUT;
1316 else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
1317 ca->crlf_action = CRLF_AUTO_CRLF;
1318 else if (eol_attr == EOL_LF)
817a0c79
TB
1319 ca->crlf_action = CRLF_TEXT_INPUT;
1320 else if (eol_attr == EOL_CRLF)
1321 ca->crlf_action = CRLF_TEXT_CRLF;
1322 }
107642fe 1323 ca->working_tree_encoding = git_path_check_encoding(ccheck + 5);
3bfba20d
JH
1324 } else {
1325 ca->drv = NULL;
df747b81 1326 ca->crlf_action = CRLF_UNDEFINED;
3bfba20d
JH
1327 ca->ident = 0;
1328 }
5c94c93d
1329
1330 /* Save attr and make a decision for action */
1331 ca->attr_action = ca->crlf_action;
df747b81
TB
1332 if (ca->crlf_action == CRLF_TEXT)
1333 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
1334 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
1335 ca->crlf_action = CRLF_BINARY;
1336 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
1337 ca->crlf_action = CRLF_AUTO_CRLF;
1338 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
1339 ca->crlf_action = CRLF_AUTO_INPUT;
83295964
JH
1340}
1341
9035d75a
SP
1342int would_convert_to_git_filter_fd(const char *path)
1343{
1344 struct conv_attrs ca;
1345
1346 convert_attrs(&ca, path);
1347 if (!ca.drv)
1348 return 0;
1349
1350 /*
1351 * Apply a filter to an fd only if the filter is required to succeed.
1352 * We must die if the filter fails, because the original data before
1353 * filtering is not available.
1354 */
1355 if (!ca.drv->required)
1356 return 0;
1357
2841e8f8 1358 return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL);
9035d75a
SP
1359}
1360
a7630bd4
TB
1361const char *get_convert_attr_ascii(const char *path)
1362{
1363 struct conv_attrs ca;
a7630bd4
TB
1364
1365 convert_attrs(&ca, path);
bb211b4d 1366 switch (ca.attr_action) {
df747b81 1367 case CRLF_UNDEFINED:
a7630bd4
TB
1368 return "";
1369 case CRLF_BINARY:
1370 return "-text";
1371 case CRLF_TEXT:
1372 return "text";
df747b81 1373 case CRLF_TEXT_INPUT:
a7630bd4 1374 return "text eol=lf";
df747b81
TB
1375 case CRLF_TEXT_CRLF:
1376 return "text eol=crlf";
a7630bd4
TB
1377 case CRLF_AUTO:
1378 return "text=auto";
df747b81 1379 case CRLF_AUTO_CRLF:
65237284 1380 return "text=auto eol=crlf";
df747b81 1381 case CRLF_AUTO_INPUT:
65237284 1382 return "text=auto eol=lf";
a7630bd4
TB
1383 }
1384 return "";
1385}
1386
82b474e0
BW
1387int convert_to_git(const struct index_state *istate,
1388 const char *path, const char *src, size_t len,
8462ff43 1389 struct strbuf *dst, int conv_flags)
35ebfd6a 1390{
3bfba20d 1391 int ret = 0;
3bfba20d 1392 struct conv_attrs ca;
6073ee85 1393
3bfba20d 1394 convert_attrs(&ca, path);
3fed15f5 1395
2841e8f8 1396 ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL);
234fa07e 1397 if (!ret && ca.drv && ca.drv->required)
36daaaca
JB
1398 die("%s: clean filter '%s' failed", path, ca.drv->name);
1399
92ac3197 1400 if (ret && dst) {
5ecd293d
PH
1401 src = dst->buf;
1402 len = dst->len;
aa4ed402 1403 }
107642fe
LS
1404
1405 ret |= encode_to_git(path, src, len, dst, ca.working_tree_encoding, conv_flags);
1406 if (ret && dst) {
1407 src = dst->buf;
1408 len = dst->len;
1409 }
1410
8462ff43
TB
1411 if (!(conv_flags & CONV_EOL_KEEP_CRLF)) {
1412 ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, conv_flags);
2fea9de6
TB
1413 if (ret && dst) {
1414 src = dst->buf;
1415 len = dst->len;
1416 }
6073ee85 1417 }
3bfba20d 1418 return ret | ident_to_git(path, src, len, dst, ca.ident);
35ebfd6a
JH
1419}
1420
d6c41c20
BW
1421void convert_to_git_filter_fd(const struct index_state *istate,
1422 const char *path, int fd, struct strbuf *dst,
8462ff43 1423 int conv_flags)
9035d75a
SP
1424{
1425 struct conv_attrs ca;
1426 convert_attrs(&ca, path);
1427
1428 assert(ca.drv);
edcc8581 1429 assert(ca.drv->clean || ca.drv->process);
9035d75a 1430
2841e8f8 1431 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL))
9035d75a
SP
1432 die("%s: clean filter '%s' failed", path, ca.drv->name);
1433
107642fe 1434 encode_to_git(path, dst->buf, dst->len, dst, ca.working_tree_encoding, conv_flags);
8462ff43 1435 crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags);
9035d75a
SP
1436 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
1437}
1438
43dd2332
EB
1439static int convert_to_working_tree_internal(const char *path, const char *src,
1440 size_t len, struct strbuf *dst,
2841e8f8 1441 int normalizing, struct delayed_checkout *dco)
35ebfd6a 1442{
36daaaca 1443 int ret = 0, ret_filter = 0;
3bfba20d 1444 struct conv_attrs ca;
6073ee85 1445
3bfba20d 1446 convert_attrs(&ca, path);
3fed15f5 1447
3bfba20d 1448 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
5ecd293d
PH
1449 if (ret) {
1450 src = dst->buf;
1451 len = dst->len;
3fed15f5 1452 }
43dd2332
EB
1453 /*
1454 * CRLF conversion can be skipped if normalizing, unless there
edcc8581
LS
1455 * is a smudge or process filter (even if the process filter doesn't
1456 * support smudge). The filters might expect CRLFs.
43dd2332 1457 */
edcc8581 1458 if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
3bfba20d 1459 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
43dd2332
EB
1460 if (ret) {
1461 src = dst->buf;
1462 len = dst->len;
1463 }
aa4ed402 1464 }
36daaaca 1465
107642fe
LS
1466 ret |= encode_to_worktree(path, src, len, dst, ca.working_tree_encoding);
1467 if (ret) {
1468 src = dst->buf;
1469 len = dst->len;
1470 }
1471
2841e8f8
LS
1472 ret_filter = apply_filter(
1473 path, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco);
234fa07e 1474 if (!ret_filter && ca.drv && ca.drv->required)
36daaaca
JB
1475 die("%s: smudge filter %s failed", path, ca.drv->name);
1476
1477 return ret | ret_filter;
35ebfd6a 1478}
f217f0e8 1479
2841e8f8
LS
1480int async_convert_to_working_tree(const char *path, const char *src,
1481 size_t len, struct strbuf *dst,
1482 void *dco)
1483{
1484 return convert_to_working_tree_internal(path, src, len, dst, 0, dco);
1485}
1486
43dd2332
EB
1487int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
1488{
2841e8f8 1489 return convert_to_working_tree_internal(path, src, len, dst, 0, NULL);
43dd2332
EB
1490}
1491
a33e0b2a
BW
1492int renormalize_buffer(const struct index_state *istate, const char *path,
1493 const char *src, size_t len, struct strbuf *dst)
f217f0e8 1494{
2841e8f8 1495 int ret = convert_to_working_tree_internal(path, src, len, dst, 1, NULL);
f217f0e8
EB
1496 if (ret) {
1497 src = dst->buf;
1498 len = dst->len;
1499 }
8462ff43 1500 return ret | convert_to_git(istate, path, src, len, dst, CONV_EOL_RENORMALIZE);
f217f0e8 1501}
dd8e9121 1502
b6691092
JH
1503/*****************************************************************
1504 *
749f763d 1505 * Streaming conversion support
b6691092
JH
1506 *
1507 *****************************************************************/
1508
1509typedef int (*filter_fn)(struct stream_filter *,
1510 const char *input, size_t *isize_p,
1511 char *output, size_t *osize_p);
1512typedef void (*free_fn)(struct stream_filter *);
1513
1514struct stream_filter_vtbl {
1515 filter_fn filter;
1516 free_fn free;
1517};
1518
1519struct stream_filter {
1520 struct stream_filter_vtbl *vtbl;
1521};
1522
1523static int null_filter_fn(struct stream_filter *filter,
1524 const char *input, size_t *isize_p,
1525 char *output, size_t *osize_p)
1526{
4ae66704
JH
1527 size_t count;
1528
1529 if (!input)
1530 return 0; /* we do not keep any states */
1531 count = *isize_p;
b6691092
JH
1532 if (*osize_p < count)
1533 count = *osize_p;
1534 if (count) {
1535 memmove(output, input, count);
1536 *isize_p -= count;
1537 *osize_p -= count;
1538 }
1539 return 0;
1540}
1541
1542static void null_free_fn(struct stream_filter *filter)
1543{
1544 ; /* nothing -- null instances are shared */
1545}
1546
1547static struct stream_filter_vtbl null_vtbl = {
1548 null_filter_fn,
1549 null_free_fn,
1550};
1551
1552static struct stream_filter null_filter_singleton = {
1553 &null_vtbl,
1554};
1555
1556int is_null_stream_filter(struct stream_filter *filter)
1557{
1558 return filter == &null_filter_singleton;
1559}
1560
b84c7839
JH
1561
1562/*
1563 * LF-to-CRLF filter
1564 */
284e3d28
CMN
1565
1566struct lf_to_crlf_filter {
1567 struct stream_filter filter;
8496f568
JH
1568 unsigned has_held:1;
1569 char held;
284e3d28
CMN
1570};
1571
e322ee38
JH
1572static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1573 const char *input, size_t *isize_p,
1574 char *output, size_t *osize_p)
1575{
284e3d28
CMN
1576 size_t count, o = 0;
1577 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1578
8496f568
JH
1579 /*
1580 * We may be holding onto the CR to see if it is followed by a
1581 * LF, in which case we would need to go to the main loop.
1582 * Otherwise, just emit it to the output stream.
1583 */
1584 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1585 output[o++] = lf_to_crlf->held;
1586 lf_to_crlf->has_held = 0;
284e3d28 1587 }
e322ee38 1588
87afe9a5
JH
1589 /* We are told to drain */
1590 if (!input) {
1591 *osize_p -= o;
1592 return 0;
1593 }
e322ee38 1594
e322ee38 1595 count = *isize_p;
8496f568 1596 if (count || lf_to_crlf->has_held) {
284e3d28 1597 size_t i;
8496f568
JH
1598 int was_cr = 0;
1599
1600 if (lf_to_crlf->has_held) {
1601 was_cr = 1;
1602 lf_to_crlf->has_held = 0;
1603 }
1604
284e3d28 1605 for (i = 0; o < *osize_p && i < count; i++) {
e322ee38 1606 char ch = input[i];
8496f568 1607
e322ee38 1608 if (ch == '\n') {
284e3d28 1609 output[o++] = '\r';
8496f568
JH
1610 } else if (was_cr) {
1611 /*
1612 * Previous round saw CR and it is not followed
1613 * by a LF; emit the CR before processing the
1614 * current character.
1615 */
1616 output[o++] = '\r';
e322ee38 1617 }
8496f568
JH
1618
1619 /*
1620 * We may have consumed the last output slot,
1621 * in which case we need to break out of this
1622 * loop; hold the current character before
1623 * returning.
1624 */
1625 if (*osize_p <= o) {
1626 lf_to_crlf->has_held = 1;
1627 lf_to_crlf->held = ch;
1628 continue; /* break but increment i */
1629 }
1630
1631 if (ch == '\r') {
1632 was_cr = 1;
1633 continue;
1634 }
1635
1636 was_cr = 0;
e322ee38
JH
1637 output[o++] = ch;
1638 }
1639
1640 *osize_p -= o;
1641 *isize_p -= i;
8496f568
JH
1642
1643 if (!lf_to_crlf->has_held && was_cr) {
1644 lf_to_crlf->has_held = 1;
1645 lf_to_crlf->held = '\r';
1646 }
e322ee38
JH
1647 }
1648 return 0;
1649}
1650
284e3d28
CMN
1651static void lf_to_crlf_free_fn(struct stream_filter *filter)
1652{
1653 free(filter);
1654}
1655
e322ee38
JH
1656static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1657 lf_to_crlf_filter_fn,
284e3d28 1658 lf_to_crlf_free_fn,
e322ee38
JH
1659};
1660
284e3d28
CMN
1661static struct stream_filter *lf_to_crlf_filter(void)
1662{
87afe9a5 1663 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
e322ee38 1664
284e3d28 1665 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
284e3d28
CMN
1666 return (struct stream_filter *)lf_to_crlf;
1667}
b84c7839 1668
a265a7f9
JH
1669/*
1670 * Cascade filter
1671 */
1672#define FILTER_BUFFER 1024
1673struct cascade_filter {
1674 struct stream_filter filter;
1675 struct stream_filter *one;
1676 struct stream_filter *two;
1677 char buf[FILTER_BUFFER];
1678 int end, ptr;
1679};
1680
1681static int cascade_filter_fn(struct stream_filter *filter,
1682 const char *input, size_t *isize_p,
1683 char *output, size_t *osize_p)
1684{
1685 struct cascade_filter *cas = (struct cascade_filter *) filter;
1686 size_t filled = 0;
1687 size_t sz = *osize_p;
1688 size_t to_feed, remaining;
1689
1690 /*
1691 * input -- (one) --> buf -- (two) --> output
1692 */
1693 while (filled < sz) {
1694 remaining = sz - filled;
1695
1696 /* do we already have something to feed two with? */
1697 if (cas->ptr < cas->end) {
1698 to_feed = cas->end - cas->ptr;
1699 if (stream_filter(cas->two,
1700 cas->buf + cas->ptr, &to_feed,
1701 output + filled, &remaining))
1702 return -1;
1703 cas->ptr += (cas->end - cas->ptr) - to_feed;
1704 filled = sz - remaining;
1705 continue;
1706 }
1707
1708 /* feed one from upstream and have it emit into our buffer */
1709 to_feed = input ? *isize_p : 0;
1710 if (input && !to_feed)
1711 break;
1712 remaining = sizeof(cas->buf);
1713 if (stream_filter(cas->one,
1714 input, &to_feed,
1715 cas->buf, &remaining))
1716 return -1;
1717 cas->end = sizeof(cas->buf) - remaining;
1718 cas->ptr = 0;
1719 if (input) {
1720 size_t fed = *isize_p - to_feed;
1721 *isize_p -= fed;
1722 input += fed;
1723 }
1724
1725 /* do we know that we drained one completely? */
1726 if (input || cas->end)
1727 continue;
1728
1729 /* tell two to drain; we have nothing more to give it */
1730 to_feed = 0;
1731 remaining = sz - filled;
1732 if (stream_filter(cas->two,
1733 NULL, &to_feed,
1734 output + filled, &remaining))
1735 return -1;
1736 if (remaining == (sz - filled))
1737 break; /* completely drained two */
1738 filled = sz - remaining;
1739 }
1740 *osize_p -= filled;
1741 return 0;
1742}
1743
1744static void cascade_free_fn(struct stream_filter *filter)
1745{
1746 struct cascade_filter *cas = (struct cascade_filter *)filter;
1747 free_stream_filter(cas->one);
1748 free_stream_filter(cas->two);
1749 free(filter);
1750}
1751
1752static struct stream_filter_vtbl cascade_vtbl = {
1753 cascade_filter_fn,
1754 cascade_free_fn,
1755};
1756
1757static struct stream_filter *cascade_filter(struct stream_filter *one,
1758 struct stream_filter *two)
1759{
1760 struct cascade_filter *cascade;
1761
1762 if (!one || is_null_stream_filter(one))
1763 return two;
1764 if (!two || is_null_stream_filter(two))
1765 return one;
1766
1767 cascade = xmalloc(sizeof(*cascade));
1768 cascade->one = one;
1769 cascade->two = two;
1770 cascade->end = cascade->ptr = 0;
1771 cascade->filter.vtbl = &cascade_vtbl;
1772 return (struct stream_filter *)cascade;
1773}
1774
b84c7839
JH
1775/*
1776 * ident filter
1777 */
1778#define IDENT_DRAINING (-1)
1779#define IDENT_SKIPPING (-2)
1780struct ident_filter {
1781 struct stream_filter filter;
1782 struct strbuf left;
1783 int state;
1a750441 1784 char ident[GIT_MAX_HEXSZ + 5]; /* ": x40 $" */
b84c7839
JH
1785};
1786
1787static int is_foreign_ident(const char *str)
1788{
1789 int i;
1790
ae021d87 1791 if (!skip_prefix(str, "$Id: ", &str))
b84c7839 1792 return 0;
ae021d87 1793 for (i = 0; str[i]; i++) {
b84c7839
JH
1794 if (isspace(str[i]) && str[i+1] != '$')
1795 return 1;
1796 }
1797 return 0;
1798}
1799
1800static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1801{
1802 size_t to_drain = ident->left.len;
1803
1804 if (*osize_p < to_drain)
1805 to_drain = *osize_p;
1806 if (to_drain) {
1807 memcpy(*output_p, ident->left.buf, to_drain);
1808 strbuf_remove(&ident->left, 0, to_drain);
1809 *output_p += to_drain;
1810 *osize_p -= to_drain;
1811 }
1812 if (!ident->left.len)
1813 ident->state = 0;
1814}
1815
1816static int ident_filter_fn(struct stream_filter *filter,
1817 const char *input, size_t *isize_p,
1818 char *output, size_t *osize_p)
1819{
1820 struct ident_filter *ident = (struct ident_filter *)filter;
1821 static const char head[] = "$Id";
1822
1823 if (!input) {
1824 /* drain upon eof */
1825 switch (ident->state) {
1826 default:
1827 strbuf_add(&ident->left, head, ident->state);
1cf01a34 1828 /* fallthrough */
b84c7839 1829 case IDENT_SKIPPING:
1cf01a34 1830 /* fallthrough */
b84c7839
JH
1831 case IDENT_DRAINING:
1832 ident_drain(ident, &output, osize_p);
1833 }
1834 return 0;
1835 }
1836
1837 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1838 int ch;
1839
1840 if (ident->state == IDENT_DRAINING) {
1841 ident_drain(ident, &output, osize_p);
1842 if (!*osize_p)
1843 break;
1844 continue;
1845 }
1846
1847 ch = *(input++);
1848 (*isize_p)--;
1849
1850 if (ident->state == IDENT_SKIPPING) {
1851 /*
1852 * Skipping until '$' or LF, but keeping them
1853 * in case it is a foreign ident.
1854 */
1855 strbuf_addch(&ident->left, ch);
1856 if (ch != '\n' && ch != '$')
1857 continue;
1858 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1859 strbuf_setlen(&ident->left, sizeof(head) - 1);
1860 strbuf_addstr(&ident->left, ident->ident);
1861 }
1862 ident->state = IDENT_DRAINING;
1863 continue;
1864 }
1865
1866 if (ident->state < sizeof(head) &&
1867 head[ident->state] == ch) {
1868 ident->state++;
1869 continue;
1870 }
1871
1872 if (ident->state)
1873 strbuf_add(&ident->left, head, ident->state);
1874 if (ident->state == sizeof(head) - 1) {
1875 if (ch != ':' && ch != '$') {
1876 strbuf_addch(&ident->left, ch);
1877 ident->state = 0;
1878 continue;
1879 }
1880
1881 if (ch == ':') {
1882 strbuf_addch(&ident->left, ch);
1883 ident->state = IDENT_SKIPPING;
1884 } else {
1885 strbuf_addstr(&ident->left, ident->ident);
1886 ident->state = IDENT_DRAINING;
1887 }
1888 continue;
1889 }
1890
1891 strbuf_addch(&ident->left, ch);
1892 ident->state = IDENT_DRAINING;
1893 }
1894 return 0;
1895}
1896
1897static void ident_free_fn(struct stream_filter *filter)
1898{
1899 struct ident_filter *ident = (struct ident_filter *)filter;
1900 strbuf_release(&ident->left);
1901 free(filter);
1902}
1903
1904static struct stream_filter_vtbl ident_vtbl = {
1905 ident_filter_fn,
1906 ident_free_fn,
1907};
1908
1a750441 1909static struct stream_filter *ident_filter(const struct object_id *oid)
b84c7839
JH
1910{
1911 struct ident_filter *ident = xmalloc(sizeof(*ident));
1912
5096d490 1913 xsnprintf(ident->ident, sizeof(ident->ident),
1a750441 1914 ": %s $", oid_to_hex(oid));
b84c7839
JH
1915 strbuf_init(&ident->left, 0);
1916 ident->filter.vtbl = &ident_vtbl;
1917 ident->state = 0;
1918 return (struct stream_filter *)ident;
1919}
1920
dd8e9121 1921/*
b6691092
JH
1922 * Return an appropriately constructed filter for the path, or NULL if
1923 * the contents cannot be filtered without reading the whole thing
1924 * in-core.
1925 *
1926 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1927 * large binary blob you would want us not to slurp into the memory!
dd8e9121 1928 */
1a750441 1929struct stream_filter *get_stream_filter(const char *path, const struct object_id *oid)
dd8e9121
JH
1930{
1931 struct conv_attrs ca;
b84c7839 1932 struct stream_filter *filter = NULL;
dd8e9121
JH
1933
1934 convert_attrs(&ca, path);
edcc8581 1935 if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
caa47adc
TB
1936 return NULL;
1937
107642fe
LS
1938 if (ca.working_tree_encoding)
1939 return NULL;
1940
caa47adc
TB
1941 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1942 return NULL;
b84c7839
JH
1943
1944 if (ca.ident)
1a750441 1945 filter = ident_filter(oid);
dd8e9121 1946
caa47adc 1947 if (output_eol(ca.crlf_action) == EOL_CRLF)
284e3d28 1948 filter = cascade_filter(filter, lf_to_crlf_filter());
caa47adc
TB
1949 else
1950 filter = cascade_filter(filter, &null_filter_singleton);
e322ee38 1951
b84c7839 1952 return filter;
b6691092
JH
1953}
1954
1955void free_stream_filter(struct stream_filter *filter)
1956{
1957 filter->vtbl->free(filter);
1958}
1959
1960int stream_filter(struct stream_filter *filter,
1961 const char *input, size_t *isize_p,
1962 char *output, size_t *osize_p)
1963{
1964 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);
dd8e9121 1965}