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