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