help: release strbuf on error return in exec_man_man()
[git/debian.git] / convert.c
blob4a0ed8d3cb1976f2af819ce39f4df7199748922e
1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "config.h"
4 #include "attr.h"
5 #include "run-command.h"
6 #include "quote.h"
7 #include "sigchain.h"
8 #include "pkt-line.h"
9 #include "sub-process.h"
12 * convert.c - convert a file when checking it out and checking it in.
14 * This should use the pathname to decide on whether it wants to do some
15 * more interesting conversions (automatic gzip/unzip, general format
16 * conversions etc etc), but by default it just does automatic CRLF<->LF
17 * translation when the "text" attribute or "auto_crlf" option is set.
20 /* Stat bits: When BIN is set, the txt bits are unset */
21 #define CONVERT_STAT_BITS_TXT_LF 0x1
22 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
23 #define CONVERT_STAT_BITS_BIN 0x4
25 enum crlf_action {
26 CRLF_UNDEFINED,
27 CRLF_BINARY,
28 CRLF_TEXT,
29 CRLF_TEXT_INPUT,
30 CRLF_TEXT_CRLF,
31 CRLF_AUTO,
32 CRLF_AUTO_INPUT,
33 CRLF_AUTO_CRLF
36 struct text_stat {
37 /* NUL, CR, LF and CRLF counts */
38 unsigned nul, lonecr, lonelf, crlf;
40 /* These are just approximations! */
41 unsigned printable, nonprintable;
44 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
46 unsigned long i;
48 memset(stats, 0, sizeof(*stats));
50 for (i = 0; i < size; i++) {
51 unsigned char c = buf[i];
52 if (c == '\r') {
53 if (i+1 < size && buf[i+1] == '\n') {
54 stats->crlf++;
55 i++;
56 } else
57 stats->lonecr++;
58 continue;
60 if (c == '\n') {
61 stats->lonelf++;
62 continue;
64 if (c == 127)
65 /* DEL */
66 stats->nonprintable++;
67 else if (c < 32) {
68 switch (c) {
69 /* BS, HT, ESC and FF */
70 case '\b': case '\t': case '\033': case '\014':
71 stats->printable++;
72 break;
73 case 0:
74 stats->nul++;
75 /* fall through */
76 default:
77 stats->nonprintable++;
80 else
81 stats->printable++;
84 /* If file ends with EOF then don't count this EOF as non-printable. */
85 if (size >= 1 && buf[size-1] == '\032')
86 stats->nonprintable--;
90 * The same heuristics as diff.c::mmfile_is_binary()
91 * We treat files with bare CR as binary
93 static int convert_is_binary(unsigned long size, const struct text_stat *stats)
95 if (stats->lonecr)
96 return 1;
97 if (stats->nul)
98 return 1;
99 if ((stats->printable >> 7) < stats->nonprintable)
100 return 1;
101 return 0;
104 static unsigned int gather_convert_stats(const char *data, unsigned long size)
106 struct text_stat stats;
107 int ret = 0;
108 if (!data || !size)
109 return 0;
110 gather_stats(data, size, &stats);
111 if (convert_is_binary(size, &stats))
112 ret |= CONVERT_STAT_BITS_BIN;
113 if (stats.crlf)
114 ret |= CONVERT_STAT_BITS_TXT_CRLF;
115 if (stats.lonelf)
116 ret |= CONVERT_STAT_BITS_TXT_LF;
118 return ret;
121 static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
123 unsigned int convert_stats = gather_convert_stats(data, size);
125 if (convert_stats & CONVERT_STAT_BITS_BIN)
126 return "-text";
127 switch (convert_stats) {
128 case CONVERT_STAT_BITS_TXT_LF:
129 return "lf";
130 case CONVERT_STAT_BITS_TXT_CRLF:
131 return "crlf";
132 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
133 return "mixed";
134 default:
135 return "none";
139 const char *get_cached_convert_stats_ascii(const struct index_state *istate,
140 const char *path)
142 const char *ret;
143 unsigned long sz;
144 void *data = read_blob_data_from_index(istate, path, &sz);
145 ret = gather_convert_stats_ascii(data, sz);
146 free(data);
147 return ret;
150 const char *get_wt_convert_stats_ascii(const char *path)
152 const char *ret = "";
153 struct strbuf sb = STRBUF_INIT;
154 if (strbuf_read_file(&sb, path, 0) >= 0)
155 ret = gather_convert_stats_ascii(sb.buf, sb.len);
156 strbuf_release(&sb);
157 return ret;
160 static int text_eol_is_crlf(void)
162 if (auto_crlf == AUTO_CRLF_TRUE)
163 return 1;
164 else if (auto_crlf == AUTO_CRLF_INPUT)
165 return 0;
166 if (core_eol == EOL_CRLF)
167 return 1;
168 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
169 return 1;
170 return 0;
173 static enum eol output_eol(enum crlf_action crlf_action)
175 switch (crlf_action) {
176 case CRLF_BINARY:
177 return EOL_UNSET;
178 case CRLF_TEXT_CRLF:
179 return EOL_CRLF;
180 case CRLF_TEXT_INPUT:
181 return EOL_LF;
182 case CRLF_UNDEFINED:
183 case CRLF_AUTO_CRLF:
184 return EOL_CRLF;
185 case CRLF_AUTO_INPUT:
186 return EOL_LF;
187 case CRLF_TEXT:
188 case CRLF_AUTO:
189 /* fall through */
190 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
192 warning("Illegal crlf_action %d\n", (int)crlf_action);
193 return core_eol;
196 static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
197 struct text_stat *old_stats, struct text_stat *new_stats,
198 enum safe_crlf checksafe)
200 if (old_stats->crlf && !new_stats->crlf ) {
202 * CRLFs would not be restored by checkout
204 if (checksafe == SAFE_CRLF_WARN)
205 warning(_("CRLF will be replaced by LF in %s.\n"
206 "The file will have its original line"
207 " endings in your working directory."), path);
208 else /* i.e. SAFE_CRLF_FAIL */
209 die(_("CRLF would be replaced by LF in %s."), path);
210 } else if (old_stats->lonelf && !new_stats->lonelf ) {
212 * CRLFs would be added by checkout
214 if (checksafe == SAFE_CRLF_WARN)
215 warning(_("LF will be replaced by CRLF in %s.\n"
216 "The file will have its original line"
217 " endings in your working directory."), path);
218 else /* i.e. SAFE_CRLF_FAIL */
219 die(_("LF would be replaced by CRLF in %s"), path);
223 static int has_cr_in_index(const struct index_state *istate, const char *path)
225 unsigned long sz;
226 void *data;
227 int has_cr;
229 data = read_blob_data_from_index(istate, path, &sz);
230 if (!data)
231 return 0;
232 has_cr = memchr(data, '\r', sz) != NULL;
233 free(data);
234 return has_cr;
237 static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
238 enum crlf_action crlf_action)
240 if (output_eol(crlf_action) != EOL_CRLF)
241 return 0;
242 /* No "naked" LF? Nothing to convert, regardless. */
243 if (!stats->lonelf)
244 return 0;
246 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
247 /* If we have any CR or CRLF line endings, we do not touch it */
248 /* This is the new safer autocrlf-handling */
249 if (stats->lonecr || stats->crlf)
250 return 0;
252 if (convert_is_binary(len, stats))
253 return 0;
255 return 1;
259 static int crlf_to_git(const struct index_state *istate,
260 const char *path, const char *src, size_t len,
261 struct strbuf *buf,
262 enum crlf_action crlf_action, enum safe_crlf checksafe)
264 struct text_stat stats;
265 char *dst;
266 int convert_crlf_into_lf;
268 if (crlf_action == CRLF_BINARY ||
269 (src && !len))
270 return 0;
273 * If we are doing a dry-run and have no source buffer, there is
274 * nothing to analyze; we must assume we would convert.
276 if (!buf && !src)
277 return 1;
279 gather_stats(src, len, &stats);
280 /* Optimization: No CRLF? Nothing to convert, regardless. */
281 convert_crlf_into_lf = !!stats.crlf;
283 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
284 if (convert_is_binary(len, &stats))
285 return 0;
287 * If the file in the index has any CR in it, do not
288 * convert. This is the new safer autocrlf handling,
289 * unless we want to renormalize in a merge or
290 * cherry-pick.
292 if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
293 has_cr_in_index(istate, path))
294 convert_crlf_into_lf = 0;
296 if ((checksafe == SAFE_CRLF_WARN ||
297 (checksafe == SAFE_CRLF_FAIL)) && len) {
298 struct text_stat new_stats;
299 memcpy(&new_stats, &stats, sizeof(new_stats));
300 /* simulate "git add" */
301 if (convert_crlf_into_lf) {
302 new_stats.lonelf += new_stats.crlf;
303 new_stats.crlf = 0;
305 /* simulate "git checkout" */
306 if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) {
307 new_stats.crlf += new_stats.lonelf;
308 new_stats.lonelf = 0;
310 check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe);
312 if (!convert_crlf_into_lf)
313 return 0;
316 * At this point all of our source analysis is done, and we are sure we
317 * would convert. If we are in dry-run mode, we can give an answer.
319 if (!buf)
320 return 1;
322 /* only grow if not in place */
323 if (strbuf_avail(buf) + buf->len < len)
324 strbuf_grow(buf, len - buf->len);
325 dst = buf->buf;
326 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
328 * If we guessed, we already know we rejected a file with
329 * lone CR, and we can strip a CR without looking at what
330 * follow it.
332 do {
333 unsigned char c = *src++;
334 if (c != '\r')
335 *dst++ = c;
336 } while (--len);
337 } else {
338 do {
339 unsigned char c = *src++;
340 if (! (c == '\r' && (1 < len && *src == '\n')))
341 *dst++ = c;
342 } while (--len);
344 strbuf_setlen(buf, dst - buf->buf);
345 return 1;
348 static int crlf_to_worktree(const char *path, const char *src, size_t len,
349 struct strbuf *buf, enum crlf_action crlf_action)
351 char *to_free = NULL;
352 struct text_stat stats;
354 if (!len || output_eol(crlf_action) != EOL_CRLF)
355 return 0;
357 gather_stats(src, len, &stats);
358 if (!will_convert_lf_to_crlf(len, &stats, crlf_action))
359 return 0;
361 /* are we "faking" in place editing ? */
362 if (src == buf->buf)
363 to_free = strbuf_detach(buf, NULL);
365 strbuf_grow(buf, len + stats.lonelf);
366 for (;;) {
367 const char *nl = memchr(src, '\n', len);
368 if (!nl)
369 break;
370 if (nl > src && nl[-1] == '\r') {
371 strbuf_add(buf, src, nl + 1 - src);
372 } else {
373 strbuf_add(buf, src, nl - src);
374 strbuf_addstr(buf, "\r\n");
376 len -= nl + 1 - src;
377 src = nl + 1;
379 strbuf_add(buf, src, len);
381 free(to_free);
382 return 1;
385 struct filter_params {
386 const char *src;
387 unsigned long size;
388 int fd;
389 const char *cmd;
390 const char *path;
393 static int filter_buffer_or_fd(int in, int out, void *data)
396 * Spawn cmd and feed the buffer contents through its stdin.
398 struct child_process child_process = CHILD_PROCESS_INIT;
399 struct filter_params *params = (struct filter_params *)data;
400 int write_err, status;
401 const char *argv[] = { NULL, NULL };
403 /* apply % substitution to cmd */
404 struct strbuf cmd = STRBUF_INIT;
405 struct strbuf path = STRBUF_INIT;
406 struct strbuf_expand_dict_entry dict[] = {
407 { "f", NULL, },
408 { NULL, NULL, },
411 /* quote the path to preserve spaces, etc. */
412 sq_quote_buf(&path, params->path);
413 dict[0].value = path.buf;
415 /* expand all %f with the quoted path */
416 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
417 strbuf_release(&path);
419 argv[0] = cmd.buf;
421 child_process.argv = argv;
422 child_process.use_shell = 1;
423 child_process.in = -1;
424 child_process.out = out;
426 if (start_command(&child_process)) {
427 strbuf_release(&cmd);
428 return error("cannot fork to run external filter '%s'", params->cmd);
431 sigchain_push(SIGPIPE, SIG_IGN);
433 if (params->src) {
434 write_err = (write_in_full(child_process.in,
435 params->src, params->size) < 0);
436 if (errno == EPIPE)
437 write_err = 0;
438 } else {
439 write_err = copy_fd(params->fd, child_process.in);
440 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
441 write_err = 0;
444 if (close(child_process.in))
445 write_err = 1;
446 if (write_err)
447 error("cannot feed the input to external filter '%s'", params->cmd);
449 sigchain_pop(SIGPIPE);
451 status = finish_command(&child_process);
452 if (status)
453 error("external filter '%s' failed %d", params->cmd, status);
455 strbuf_release(&cmd);
456 return (write_err || status);
459 static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
460 struct strbuf *dst, const char *cmd)
463 * Create a pipeline to have the command filter the buffer's
464 * contents.
466 * (child --> cmd) --> us
468 int err = 0;
469 struct strbuf nbuf = STRBUF_INIT;
470 struct async async;
471 struct filter_params params;
473 memset(&async, 0, sizeof(async));
474 async.proc = filter_buffer_or_fd;
475 async.data = &params;
476 async.out = -1;
477 params.src = src;
478 params.size = len;
479 params.fd = fd;
480 params.cmd = cmd;
481 params.path = path;
483 fflush(NULL);
484 if (start_async(&async))
485 return 0; /* error was already reported */
487 if (strbuf_read(&nbuf, async.out, len) < 0) {
488 err = error("read from external filter '%s' failed", cmd);
490 if (close(async.out)) {
491 err = error("read from external filter '%s' failed", cmd);
493 if (finish_async(&async)) {
494 err = error("external filter '%s' failed", cmd);
497 if (!err) {
498 strbuf_swap(dst, &nbuf);
500 strbuf_release(&nbuf);
501 return !err;
504 #define CAP_CLEAN (1u<<0)
505 #define CAP_SMUDGE (1u<<1)
506 #define CAP_DELAY (1u<<2)
508 struct cmd2process {
509 struct subprocess_entry subprocess; /* must be the first member! */
510 unsigned int supported_capabilities;
513 static int subprocess_map_initialized;
514 static struct hashmap subprocess_map;
516 static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
518 static int versions[] = {2, 0};
519 static struct subprocess_capability capabilities[] = {
520 { "clean", CAP_CLEAN },
521 { "smudge", CAP_SMUDGE },
522 { "delay", CAP_DELAY },
523 { NULL, 0 }
525 struct cmd2process *entry = (struct cmd2process *)subprocess;
526 return subprocess_handshake(subprocess, "git-filter", versions, NULL,
527 capabilities,
528 &entry->supported_capabilities);
531 static void handle_filter_error(const struct strbuf *filter_status,
532 struct cmd2process *entry,
533 const unsigned int wanted_capability) {
534 if (!strcmp(filter_status->buf, "error"))
535 ; /* The filter signaled a problem with the file. */
536 else if (!strcmp(filter_status->buf, "abort") && wanted_capability) {
538 * The filter signaled a permanent problem. Don't try to filter
539 * files with the same command for the lifetime of the current
540 * Git process.
542 entry->supported_capabilities &= ~wanted_capability;
543 } else {
545 * Something went wrong with the protocol filter.
546 * Force shutdown and restart if another blob requires filtering.
548 error("external filter '%s' failed", entry->subprocess.cmd);
549 subprocess_stop(&subprocess_map, &entry->subprocess);
550 free(entry);
554 static int apply_multi_file_filter(const char *path, const char *src, size_t len,
555 int fd, struct strbuf *dst, const char *cmd,
556 const unsigned int wanted_capability,
557 struct delayed_checkout *dco)
559 int err;
560 int can_delay = 0;
561 struct cmd2process *entry;
562 struct child_process *process;
563 struct strbuf nbuf = STRBUF_INIT;
564 struct strbuf filter_status = STRBUF_INIT;
565 const char *filter_type;
567 if (!subprocess_map_initialized) {
568 subprocess_map_initialized = 1;
569 hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
570 entry = NULL;
571 } else {
572 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
575 fflush(NULL);
577 if (!entry) {
578 entry = xmalloc(sizeof(*entry));
579 entry->supported_capabilities = 0;
581 if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
582 free(entry);
583 return 0;
586 process = &entry->subprocess.process;
588 if (!(entry->supported_capabilities & wanted_capability))
589 return 0;
591 if (wanted_capability & CAP_CLEAN)
592 filter_type = "clean";
593 else if (wanted_capability & CAP_SMUDGE)
594 filter_type = "smudge";
595 else
596 die("unexpected filter type");
598 sigchain_push(SIGPIPE, SIG_IGN);
600 assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
601 err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
602 if (err)
603 goto done;
605 err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
606 if (err) {
607 error("path name too long for external filter");
608 goto done;
611 err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
612 if (err)
613 goto done;
615 if ((entry->supported_capabilities & CAP_DELAY) &&
616 dco && dco->state == CE_CAN_DELAY) {
617 can_delay = 1;
618 err = packet_write_fmt_gently(process->in, "can-delay=1\n");
619 if (err)
620 goto done;
623 err = packet_flush_gently(process->in);
624 if (err)
625 goto done;
627 if (fd >= 0)
628 err = write_packetized_from_fd(fd, process->in);
629 else
630 err = write_packetized_from_buf(src, len, process->in);
631 if (err)
632 goto done;
634 err = subprocess_read_status(process->out, &filter_status);
635 if (err)
636 goto done;
638 if (can_delay && !strcmp(filter_status.buf, "delayed")) {
639 string_list_insert(&dco->filters, cmd);
640 string_list_insert(&dco->paths, path);
641 } else {
642 /* The filter got the blob and wants to send us a response. */
643 err = strcmp(filter_status.buf, "success");
644 if (err)
645 goto done;
647 err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
648 if (err)
649 goto done;
651 err = subprocess_read_status(process->out, &filter_status);
652 if (err)
653 goto done;
655 err = strcmp(filter_status.buf, "success");
658 done:
659 sigchain_pop(SIGPIPE);
661 if (err)
662 handle_filter_error(&filter_status, entry, wanted_capability);
663 else
664 strbuf_swap(dst, &nbuf);
665 strbuf_release(&nbuf);
666 return !err;
670 int async_query_available_blobs(const char *cmd, struct string_list *available_paths)
672 int err;
673 char *line;
674 struct cmd2process *entry;
675 struct child_process *process;
676 struct strbuf filter_status = STRBUF_INIT;
678 assert(subprocess_map_initialized);
679 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
680 if (!entry) {
681 error("external filter '%s' is not available anymore although "
682 "not all paths have been filtered", cmd);
683 return 0;
685 process = &entry->subprocess.process;
686 sigchain_push(SIGPIPE, SIG_IGN);
688 err = packet_write_fmt_gently(
689 process->in, "command=list_available_blobs\n");
690 if (err)
691 goto done;
693 err = packet_flush_gently(process->in);
694 if (err)
695 goto done;
697 while ((line = packet_read_line(process->out, NULL))) {
698 const char *path;
699 if (skip_prefix(line, "pathname=", &path))
700 string_list_insert(available_paths, xstrdup(path));
701 else
702 ; /* ignore unknown keys */
705 err = subprocess_read_status(process->out, &filter_status);
706 if (err)
707 goto done;
709 err = strcmp(filter_status.buf, "success");
711 done:
712 sigchain_pop(SIGPIPE);
714 if (err)
715 handle_filter_error(&filter_status, entry, 0);
716 return !err;
719 static struct convert_driver {
720 const char *name;
721 struct convert_driver *next;
722 const char *smudge;
723 const char *clean;
724 const char *process;
725 int required;
726 } *user_convert, **user_convert_tail;
728 static int apply_filter(const char *path, const char *src, size_t len,
729 int fd, struct strbuf *dst, struct convert_driver *drv,
730 const unsigned int wanted_capability,
731 struct delayed_checkout *dco)
733 const char *cmd = NULL;
735 if (!drv)
736 return 0;
738 if (!dst)
739 return 1;
741 if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean)
742 cmd = drv->clean;
743 else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge)
744 cmd = drv->smudge;
746 if (cmd && *cmd)
747 return apply_single_file_filter(path, src, len, fd, dst, cmd);
748 else if (drv->process && *drv->process)
749 return apply_multi_file_filter(path, src, len, fd, dst,
750 drv->process, wanted_capability, dco);
752 return 0;
755 static int read_convert_config(const char *var, const char *value, void *cb)
757 const char *key, *name;
758 int namelen;
759 struct convert_driver *drv;
762 * External conversion drivers are configured using
763 * "filter.<name>.variable".
765 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
766 return 0;
767 for (drv = user_convert; drv; drv = drv->next)
768 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
769 break;
770 if (!drv) {
771 drv = xcalloc(1, sizeof(struct convert_driver));
772 drv->name = xmemdupz(name, namelen);
773 *user_convert_tail = drv;
774 user_convert_tail = &(drv->next);
778 * filter.<name>.smudge and filter.<name>.clean specifies
779 * the command line:
781 * command-line
783 * The command-line will not be interpolated in any way.
786 if (!strcmp("smudge", key))
787 return git_config_string(&drv->smudge, var, value);
789 if (!strcmp("clean", key))
790 return git_config_string(&drv->clean, var, value);
792 if (!strcmp("process", key))
793 return git_config_string(&drv->process, var, value);
795 if (!strcmp("required", key)) {
796 drv->required = git_config_bool(var, value);
797 return 0;
800 return 0;
803 static int count_ident(const char *cp, unsigned long size)
806 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
808 int cnt = 0;
809 char ch;
811 while (size) {
812 ch = *cp++;
813 size--;
814 if (ch != '$')
815 continue;
816 if (size < 3)
817 break;
818 if (memcmp("Id", cp, 2))
819 continue;
820 ch = cp[2];
821 cp += 3;
822 size -= 3;
823 if (ch == '$')
824 cnt++; /* $Id$ */
825 if (ch != ':')
826 continue;
829 * "$Id: ... "; scan up to the closing dollar sign and discard.
831 while (size) {
832 ch = *cp++;
833 size--;
834 if (ch == '$') {
835 cnt++;
836 break;
838 if (ch == '\n')
839 break;
842 return cnt;
845 static int ident_to_git(const char *path, const char *src, size_t len,
846 struct strbuf *buf, int ident)
848 char *dst, *dollar;
850 if (!ident || (src && !count_ident(src, len)))
851 return 0;
853 if (!buf)
854 return 1;
856 /* only grow if not in place */
857 if (strbuf_avail(buf) + buf->len < len)
858 strbuf_grow(buf, len - buf->len);
859 dst = buf->buf;
860 for (;;) {
861 dollar = memchr(src, '$', len);
862 if (!dollar)
863 break;
864 memmove(dst, src, dollar + 1 - src);
865 dst += dollar + 1 - src;
866 len -= dollar + 1 - src;
867 src = dollar + 1;
869 if (len > 3 && !memcmp(src, "Id:", 3)) {
870 dollar = memchr(src + 3, '$', len - 3);
871 if (!dollar)
872 break;
873 if (memchr(src + 3, '\n', dollar - src - 3)) {
874 /* Line break before the next dollar. */
875 continue;
878 memcpy(dst, "Id$", 3);
879 dst += 3;
880 len -= dollar + 1 - src;
881 src = dollar + 1;
884 memmove(dst, src, len);
885 strbuf_setlen(buf, dst + len - buf->buf);
886 return 1;
889 static int ident_to_worktree(const char *path, const char *src, size_t len,
890 struct strbuf *buf, int ident)
892 unsigned char sha1[20];
893 char *to_free = NULL, *dollar, *spc;
894 int cnt;
896 if (!ident)
897 return 0;
899 cnt = count_ident(src, len);
900 if (!cnt)
901 return 0;
903 /* are we "faking" in place editing ? */
904 if (src == buf->buf)
905 to_free = strbuf_detach(buf, NULL);
906 hash_sha1_file(src, len, "blob", sha1);
908 strbuf_grow(buf, len + cnt * 43);
909 for (;;) {
910 /* step 1: run to the next '$' */
911 dollar = memchr(src, '$', len);
912 if (!dollar)
913 break;
914 strbuf_add(buf, src, dollar + 1 - src);
915 len -= dollar + 1 - src;
916 src = dollar + 1;
918 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
919 if (len < 3 || memcmp("Id", src, 2))
920 continue;
922 /* step 3: skip over Id$ or Id:xxxxx$ */
923 if (src[2] == '$') {
924 src += 3;
925 len -= 3;
926 } else if (src[2] == ':') {
928 * It's possible that an expanded Id has crept its way into the
929 * repository, we cope with that by stripping the expansion out.
930 * This is probably not a good idea, since it will cause changes
931 * on checkout, which won't go away by stash, but let's keep it
932 * for git-style ids.
934 dollar = memchr(src + 3, '$', len - 3);
935 if (!dollar) {
936 /* incomplete keyword, no more '$', so just quit the loop */
937 break;
940 if (memchr(src + 3, '\n', dollar - src - 3)) {
941 /* Line break before the next dollar. */
942 continue;
945 spc = memchr(src + 4, ' ', dollar - src - 4);
946 if (spc && spc < dollar-1) {
947 /* There are spaces in unexpected places.
948 * This is probably an id from some other
949 * versioning system. Keep it for now.
951 continue;
954 len -= dollar + 1 - src;
955 src = dollar + 1;
956 } else {
957 /* it wasn't a "Id$" or "Id:xxxx$" */
958 continue;
961 /* step 4: substitute */
962 strbuf_addstr(buf, "Id: ");
963 strbuf_add(buf, sha1_to_hex(sha1), 40);
964 strbuf_addstr(buf, " $");
966 strbuf_add(buf, src, len);
968 free(to_free);
969 return 1;
972 static enum crlf_action git_path_check_crlf(struct attr_check_item *check)
974 const char *value = check->value;
976 if (ATTR_TRUE(value))
977 return CRLF_TEXT;
978 else if (ATTR_FALSE(value))
979 return CRLF_BINARY;
980 else if (ATTR_UNSET(value))
982 else if (!strcmp(value, "input"))
983 return CRLF_TEXT_INPUT;
984 else if (!strcmp(value, "auto"))
985 return CRLF_AUTO;
986 return CRLF_UNDEFINED;
989 static enum eol git_path_check_eol(struct attr_check_item *check)
991 const char *value = check->value;
993 if (ATTR_UNSET(value))
995 else if (!strcmp(value, "lf"))
996 return EOL_LF;
997 else if (!strcmp(value, "crlf"))
998 return EOL_CRLF;
999 return EOL_UNSET;
1002 static struct convert_driver *git_path_check_convert(struct attr_check_item *check)
1004 const char *value = check->value;
1005 struct convert_driver *drv;
1007 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
1008 return NULL;
1009 for (drv = user_convert; drv; drv = drv->next)
1010 if (!strcmp(value, drv->name))
1011 return drv;
1012 return NULL;
1015 static int git_path_check_ident(struct attr_check_item *check)
1017 const char *value = check->value;
1019 return !!ATTR_TRUE(value);
1022 struct conv_attrs {
1023 struct convert_driver *drv;
1024 enum crlf_action attr_action; /* What attr says */
1025 enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
1026 int ident;
1029 static void convert_attrs(struct conv_attrs *ca, const char *path)
1031 static struct attr_check *check;
1033 if (!check) {
1034 check = attr_check_initl("crlf", "ident", "filter",
1035 "eol", "text", NULL);
1036 user_convert_tail = &user_convert;
1037 git_config(read_convert_config, NULL);
1040 if (!git_check_attr(path, check)) {
1041 struct attr_check_item *ccheck = check->items;
1042 ca->crlf_action = git_path_check_crlf(ccheck + 4);
1043 if (ca->crlf_action == CRLF_UNDEFINED)
1044 ca->crlf_action = git_path_check_crlf(ccheck + 0);
1045 ca->attr_action = ca->crlf_action;
1046 ca->ident = git_path_check_ident(ccheck + 1);
1047 ca->drv = git_path_check_convert(ccheck + 2);
1048 if (ca->crlf_action != CRLF_BINARY) {
1049 enum eol eol_attr = git_path_check_eol(ccheck + 3);
1050 if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
1051 ca->crlf_action = CRLF_AUTO_INPUT;
1052 else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
1053 ca->crlf_action = CRLF_AUTO_CRLF;
1054 else if (eol_attr == EOL_LF)
1055 ca->crlf_action = CRLF_TEXT_INPUT;
1056 else if (eol_attr == EOL_CRLF)
1057 ca->crlf_action = CRLF_TEXT_CRLF;
1059 ca->attr_action = ca->crlf_action;
1060 } else {
1061 ca->drv = NULL;
1062 ca->crlf_action = CRLF_UNDEFINED;
1063 ca->ident = 0;
1065 if (ca->crlf_action == CRLF_TEXT)
1066 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
1067 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
1068 ca->crlf_action = CRLF_BINARY;
1069 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
1070 ca->crlf_action = CRLF_AUTO_CRLF;
1071 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
1072 ca->crlf_action = CRLF_AUTO_INPUT;
1075 int would_convert_to_git_filter_fd(const char *path)
1077 struct conv_attrs ca;
1079 convert_attrs(&ca, path);
1080 if (!ca.drv)
1081 return 0;
1084 * Apply a filter to an fd only if the filter is required to succeed.
1085 * We must die if the filter fails, because the original data before
1086 * filtering is not available.
1088 if (!ca.drv->required)
1089 return 0;
1091 return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL);
1094 const char *get_convert_attr_ascii(const char *path)
1096 struct conv_attrs ca;
1098 convert_attrs(&ca, path);
1099 switch (ca.attr_action) {
1100 case CRLF_UNDEFINED:
1101 return "";
1102 case CRLF_BINARY:
1103 return "-text";
1104 case CRLF_TEXT:
1105 return "text";
1106 case CRLF_TEXT_INPUT:
1107 return "text eol=lf";
1108 case CRLF_TEXT_CRLF:
1109 return "text eol=crlf";
1110 case CRLF_AUTO:
1111 return "text=auto";
1112 case CRLF_AUTO_CRLF:
1113 return "text=auto eol=crlf";
1114 case CRLF_AUTO_INPUT:
1115 return "text=auto eol=lf";
1117 return "";
1120 int convert_to_git(const struct index_state *istate,
1121 const char *path, const char *src, size_t len,
1122 struct strbuf *dst, enum safe_crlf checksafe)
1124 int ret = 0;
1125 struct conv_attrs ca;
1127 convert_attrs(&ca, path);
1129 ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL);
1130 if (!ret && ca.drv && ca.drv->required)
1131 die("%s: clean filter '%s' failed", path, ca.drv->name);
1133 if (ret && dst) {
1134 src = dst->buf;
1135 len = dst->len;
1137 if (checksafe != SAFE_CRLF_KEEP_CRLF) {
1138 ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
1139 if (ret && dst) {
1140 src = dst->buf;
1141 len = dst->len;
1144 return ret | ident_to_git(path, src, len, dst, ca.ident);
1147 void convert_to_git_filter_fd(const struct index_state *istate,
1148 const char *path, int fd, struct strbuf *dst,
1149 enum safe_crlf checksafe)
1151 struct conv_attrs ca;
1152 convert_attrs(&ca, path);
1154 assert(ca.drv);
1155 assert(ca.drv->clean || ca.drv->process);
1157 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL))
1158 die("%s: clean filter '%s' failed", path, ca.drv->name);
1160 crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
1161 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
1164 static int convert_to_working_tree_internal(const char *path, const char *src,
1165 size_t len, struct strbuf *dst,
1166 int normalizing, struct delayed_checkout *dco)
1168 int ret = 0, ret_filter = 0;
1169 struct conv_attrs ca;
1171 convert_attrs(&ca, path);
1173 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
1174 if (ret) {
1175 src = dst->buf;
1176 len = dst->len;
1179 * CRLF conversion can be skipped if normalizing, unless there
1180 * is a smudge or process filter (even if the process filter doesn't
1181 * support smudge). The filters might expect CRLFs.
1183 if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
1184 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
1185 if (ret) {
1186 src = dst->buf;
1187 len = dst->len;
1191 ret_filter = apply_filter(
1192 path, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco);
1193 if (!ret_filter && ca.drv && ca.drv->required)
1194 die("%s: smudge filter %s failed", path, ca.drv->name);
1196 return ret | ret_filter;
1199 int async_convert_to_working_tree(const char *path, const char *src,
1200 size_t len, struct strbuf *dst,
1201 void *dco)
1203 return convert_to_working_tree_internal(path, src, len, dst, 0, dco);
1206 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
1208 return convert_to_working_tree_internal(path, src, len, dst, 0, NULL);
1211 int renormalize_buffer(const struct index_state *istate, const char *path,
1212 const char *src, size_t len, struct strbuf *dst)
1214 int ret = convert_to_working_tree_internal(path, src, len, dst, 1, NULL);
1215 if (ret) {
1216 src = dst->buf;
1217 len = dst->len;
1219 return ret | convert_to_git(istate, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
1222 /*****************************************************************
1224 * Streaming conversion support
1226 *****************************************************************/
1228 typedef int (*filter_fn)(struct stream_filter *,
1229 const char *input, size_t *isize_p,
1230 char *output, size_t *osize_p);
1231 typedef void (*free_fn)(struct stream_filter *);
1233 struct stream_filter_vtbl {
1234 filter_fn filter;
1235 free_fn free;
1238 struct stream_filter {
1239 struct stream_filter_vtbl *vtbl;
1242 static int null_filter_fn(struct stream_filter *filter,
1243 const char *input, size_t *isize_p,
1244 char *output, size_t *osize_p)
1246 size_t count;
1248 if (!input)
1249 return 0; /* we do not keep any states */
1250 count = *isize_p;
1251 if (*osize_p < count)
1252 count = *osize_p;
1253 if (count) {
1254 memmove(output, input, count);
1255 *isize_p -= count;
1256 *osize_p -= count;
1258 return 0;
1261 static void null_free_fn(struct stream_filter *filter)
1263 ; /* nothing -- null instances are shared */
1266 static struct stream_filter_vtbl null_vtbl = {
1267 null_filter_fn,
1268 null_free_fn,
1271 static struct stream_filter null_filter_singleton = {
1272 &null_vtbl,
1275 int is_null_stream_filter(struct stream_filter *filter)
1277 return filter == &null_filter_singleton;
1282 * LF-to-CRLF filter
1285 struct lf_to_crlf_filter {
1286 struct stream_filter filter;
1287 unsigned has_held:1;
1288 char held;
1291 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1292 const char *input, size_t *isize_p,
1293 char *output, size_t *osize_p)
1295 size_t count, o = 0;
1296 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1299 * We may be holding onto the CR to see if it is followed by a
1300 * LF, in which case we would need to go to the main loop.
1301 * Otherwise, just emit it to the output stream.
1303 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1304 output[o++] = lf_to_crlf->held;
1305 lf_to_crlf->has_held = 0;
1308 /* We are told to drain */
1309 if (!input) {
1310 *osize_p -= o;
1311 return 0;
1314 count = *isize_p;
1315 if (count || lf_to_crlf->has_held) {
1316 size_t i;
1317 int was_cr = 0;
1319 if (lf_to_crlf->has_held) {
1320 was_cr = 1;
1321 lf_to_crlf->has_held = 0;
1324 for (i = 0; o < *osize_p && i < count; i++) {
1325 char ch = input[i];
1327 if (ch == '\n') {
1328 output[o++] = '\r';
1329 } else if (was_cr) {
1331 * Previous round saw CR and it is not followed
1332 * by a LF; emit the CR before processing the
1333 * current character.
1335 output[o++] = '\r';
1339 * We may have consumed the last output slot,
1340 * in which case we need to break out of this
1341 * loop; hold the current character before
1342 * returning.
1344 if (*osize_p <= o) {
1345 lf_to_crlf->has_held = 1;
1346 lf_to_crlf->held = ch;
1347 continue; /* break but increment i */
1350 if (ch == '\r') {
1351 was_cr = 1;
1352 continue;
1355 was_cr = 0;
1356 output[o++] = ch;
1359 *osize_p -= o;
1360 *isize_p -= i;
1362 if (!lf_to_crlf->has_held && was_cr) {
1363 lf_to_crlf->has_held = 1;
1364 lf_to_crlf->held = '\r';
1367 return 0;
1370 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1372 free(filter);
1375 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1376 lf_to_crlf_filter_fn,
1377 lf_to_crlf_free_fn,
1380 static struct stream_filter *lf_to_crlf_filter(void)
1382 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1384 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1385 return (struct stream_filter *)lf_to_crlf;
1389 * Cascade filter
1391 #define FILTER_BUFFER 1024
1392 struct cascade_filter {
1393 struct stream_filter filter;
1394 struct stream_filter *one;
1395 struct stream_filter *two;
1396 char buf[FILTER_BUFFER];
1397 int end, ptr;
1400 static int cascade_filter_fn(struct stream_filter *filter,
1401 const char *input, size_t *isize_p,
1402 char *output, size_t *osize_p)
1404 struct cascade_filter *cas = (struct cascade_filter *) filter;
1405 size_t filled = 0;
1406 size_t sz = *osize_p;
1407 size_t to_feed, remaining;
1410 * input -- (one) --> buf -- (two) --> output
1412 while (filled < sz) {
1413 remaining = sz - filled;
1415 /* do we already have something to feed two with? */
1416 if (cas->ptr < cas->end) {
1417 to_feed = cas->end - cas->ptr;
1418 if (stream_filter(cas->two,
1419 cas->buf + cas->ptr, &to_feed,
1420 output + filled, &remaining))
1421 return -1;
1422 cas->ptr += (cas->end - cas->ptr) - to_feed;
1423 filled = sz - remaining;
1424 continue;
1427 /* feed one from upstream and have it emit into our buffer */
1428 to_feed = input ? *isize_p : 0;
1429 if (input && !to_feed)
1430 break;
1431 remaining = sizeof(cas->buf);
1432 if (stream_filter(cas->one,
1433 input, &to_feed,
1434 cas->buf, &remaining))
1435 return -1;
1436 cas->end = sizeof(cas->buf) - remaining;
1437 cas->ptr = 0;
1438 if (input) {
1439 size_t fed = *isize_p - to_feed;
1440 *isize_p -= fed;
1441 input += fed;
1444 /* do we know that we drained one completely? */
1445 if (input || cas->end)
1446 continue;
1448 /* tell two to drain; we have nothing more to give it */
1449 to_feed = 0;
1450 remaining = sz - filled;
1451 if (stream_filter(cas->two,
1452 NULL, &to_feed,
1453 output + filled, &remaining))
1454 return -1;
1455 if (remaining == (sz - filled))
1456 break; /* completely drained two */
1457 filled = sz - remaining;
1459 *osize_p -= filled;
1460 return 0;
1463 static void cascade_free_fn(struct stream_filter *filter)
1465 struct cascade_filter *cas = (struct cascade_filter *)filter;
1466 free_stream_filter(cas->one);
1467 free_stream_filter(cas->two);
1468 free(filter);
1471 static struct stream_filter_vtbl cascade_vtbl = {
1472 cascade_filter_fn,
1473 cascade_free_fn,
1476 static struct stream_filter *cascade_filter(struct stream_filter *one,
1477 struct stream_filter *two)
1479 struct cascade_filter *cascade;
1481 if (!one || is_null_stream_filter(one))
1482 return two;
1483 if (!two || is_null_stream_filter(two))
1484 return one;
1486 cascade = xmalloc(sizeof(*cascade));
1487 cascade->one = one;
1488 cascade->two = two;
1489 cascade->end = cascade->ptr = 0;
1490 cascade->filter.vtbl = &cascade_vtbl;
1491 return (struct stream_filter *)cascade;
1495 * ident filter
1497 #define IDENT_DRAINING (-1)
1498 #define IDENT_SKIPPING (-2)
1499 struct ident_filter {
1500 struct stream_filter filter;
1501 struct strbuf left;
1502 int state;
1503 char ident[45]; /* ": x40 $" */
1506 static int is_foreign_ident(const char *str)
1508 int i;
1510 if (!skip_prefix(str, "$Id: ", &str))
1511 return 0;
1512 for (i = 0; str[i]; i++) {
1513 if (isspace(str[i]) && str[i+1] != '$')
1514 return 1;
1516 return 0;
1519 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1521 size_t to_drain = ident->left.len;
1523 if (*osize_p < to_drain)
1524 to_drain = *osize_p;
1525 if (to_drain) {
1526 memcpy(*output_p, ident->left.buf, to_drain);
1527 strbuf_remove(&ident->left, 0, to_drain);
1528 *output_p += to_drain;
1529 *osize_p -= to_drain;
1531 if (!ident->left.len)
1532 ident->state = 0;
1535 static int ident_filter_fn(struct stream_filter *filter,
1536 const char *input, size_t *isize_p,
1537 char *output, size_t *osize_p)
1539 struct ident_filter *ident = (struct ident_filter *)filter;
1540 static const char head[] = "$Id";
1542 if (!input) {
1543 /* drain upon eof */
1544 switch (ident->state) {
1545 default:
1546 strbuf_add(&ident->left, head, ident->state);
1547 case IDENT_SKIPPING:
1548 /* fallthru */
1549 case IDENT_DRAINING:
1550 ident_drain(ident, &output, osize_p);
1552 return 0;
1555 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1556 int ch;
1558 if (ident->state == IDENT_DRAINING) {
1559 ident_drain(ident, &output, osize_p);
1560 if (!*osize_p)
1561 break;
1562 continue;
1565 ch = *(input++);
1566 (*isize_p)--;
1568 if (ident->state == IDENT_SKIPPING) {
1570 * Skipping until '$' or LF, but keeping them
1571 * in case it is a foreign ident.
1573 strbuf_addch(&ident->left, ch);
1574 if (ch != '\n' && ch != '$')
1575 continue;
1576 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1577 strbuf_setlen(&ident->left, sizeof(head) - 1);
1578 strbuf_addstr(&ident->left, ident->ident);
1580 ident->state = IDENT_DRAINING;
1581 continue;
1584 if (ident->state < sizeof(head) &&
1585 head[ident->state] == ch) {
1586 ident->state++;
1587 continue;
1590 if (ident->state)
1591 strbuf_add(&ident->left, head, ident->state);
1592 if (ident->state == sizeof(head) - 1) {
1593 if (ch != ':' && ch != '$') {
1594 strbuf_addch(&ident->left, ch);
1595 ident->state = 0;
1596 continue;
1599 if (ch == ':') {
1600 strbuf_addch(&ident->left, ch);
1601 ident->state = IDENT_SKIPPING;
1602 } else {
1603 strbuf_addstr(&ident->left, ident->ident);
1604 ident->state = IDENT_DRAINING;
1606 continue;
1609 strbuf_addch(&ident->left, ch);
1610 ident->state = IDENT_DRAINING;
1612 return 0;
1615 static void ident_free_fn(struct stream_filter *filter)
1617 struct ident_filter *ident = (struct ident_filter *)filter;
1618 strbuf_release(&ident->left);
1619 free(filter);
1622 static struct stream_filter_vtbl ident_vtbl = {
1623 ident_filter_fn,
1624 ident_free_fn,
1627 static struct stream_filter *ident_filter(const unsigned char *sha1)
1629 struct ident_filter *ident = xmalloc(sizeof(*ident));
1631 xsnprintf(ident->ident, sizeof(ident->ident),
1632 ": %s $", sha1_to_hex(sha1));
1633 strbuf_init(&ident->left, 0);
1634 ident->filter.vtbl = &ident_vtbl;
1635 ident->state = 0;
1636 return (struct stream_filter *)ident;
1640 * Return an appropriately constructed filter for the path, or NULL if
1641 * the contents cannot be filtered without reading the whole thing
1642 * in-core.
1644 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1645 * large binary blob you would want us not to slurp into the memory!
1647 struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1649 struct conv_attrs ca;
1650 struct stream_filter *filter = NULL;
1652 convert_attrs(&ca, path);
1653 if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
1654 return NULL;
1656 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1657 return NULL;
1659 if (ca.ident)
1660 filter = ident_filter(sha1);
1662 if (output_eol(ca.crlf_action) == EOL_CRLF)
1663 filter = cascade_filter(filter, lf_to_crlf_filter());
1664 else
1665 filter = cascade_filter(filter, &null_filter_singleton);
1667 return filter;
1670 void free_stream_filter(struct stream_filter *filter)
1672 filter->vtbl->free(filter);
1675 int stream_filter(struct stream_filter *filter,
1676 const char *input, size_t *isize_p,
1677 char *output, size_t *osize_p)
1679 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);