3 #include "run-command.h"
8 * convert.c - convert a file when checking it out and checking it in.
10 * This should use the pathname to decide on whether it wants to do some
11 * more interesting conversions (automatic gzip/unzip, general format
12 * conversions etc etc), but by default it just does automatic CRLF<->LF
13 * translation when the "text" attribute or "auto_crlf" option is set.
16 /* Stat bits: When BIN is set, the txt bits are unset */
17 #define CONVERT_STAT_BITS_TXT_LF 0x1
18 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
19 #define CONVERT_STAT_BITS_BIN 0x4
31 /* NUL, CR, LF and CRLF counts */
32 unsigned nul, cr, lf, crlf;
34 /* These are just approximations! */
35 unsigned printable, nonprintable;
38 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
42 memset(stats, 0, sizeof(*stats));
44 for (i = 0; i < size; i++) {
45 unsigned char c = buf[i];
48 if (i+1 < size && buf[i+1] == '\n')
58 stats->nonprintable++;
61 /* BS, HT, ESC and FF */
62 case '\b': case '\t': case '\033': case '\014':
69 stats->nonprintable++;
76 /* If file ends with EOF then don't count this EOF as non-printable. */
77 if (size >= 1 && buf[size-1] == '\032')
78 stats->nonprintable--;
82 * The same heuristics as diff.c::mmfile_is_binary()
83 * We treat files with bare CR as binary
85 static int convert_is_binary(unsigned long size, const struct text_stat *stats)
87 if (stats->cr != stats->crlf)
91 if ((stats->printable >> 7) < stats->nonprintable)
96 static unsigned int gather_convert_stats(const char *data, unsigned long size)
98 struct text_stat stats;
101 gather_stats(data, size, &stats);
102 if (convert_is_binary(size, &stats))
103 return CONVERT_STAT_BITS_BIN;
104 else if (stats.crlf && stats.crlf == stats.lf)
105 return CONVERT_STAT_BITS_TXT_CRLF;
106 else if (stats.crlf && stats.lf)
107 return CONVERT_STAT_BITS_TXT_CRLF | CONVERT_STAT_BITS_TXT_LF;
109 return CONVERT_STAT_BITS_TXT_LF;
114 static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
116 unsigned int convert_stats = gather_convert_stats(data, size);
118 if (convert_stats & CONVERT_STAT_BITS_BIN)
120 switch (convert_stats) {
121 case CONVERT_STAT_BITS_TXT_LF:
123 case CONVERT_STAT_BITS_TXT_CRLF:
125 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
132 const char *get_cached_convert_stats_ascii(const char *path)
136 void *data = read_blob_data_from_cache(path, &sz);
137 ret = gather_convert_stats_ascii(data, sz);
142 const char *get_wt_convert_stats_ascii(const char *path)
144 const char *ret = "";
145 struct strbuf sb = STRBUF_INIT;
146 if (strbuf_read_file(&sb, path, 0) >= 0)
147 ret = gather_convert_stats_ascii(sb.buf, sb.len);
152 static enum eol output_eol(enum crlf_action crlf_action)
154 switch (crlf_action) {
167 if (auto_crlf == AUTO_CRLF_TRUE)
169 else if (auto_crlf == AUTO_CRLF_INPUT)
171 else if (core_eol == EOL_UNSET)
177 static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
178 struct text_stat *stats, enum safe_crlf checksafe)
183 if (output_eol(crlf_action) == EOL_LF) {
185 * CRLFs would not be restored by checkout:
186 * check if we'd remove CRLFs
189 if (checksafe == SAFE_CRLF_WARN)
190 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
191 else /* i.e. SAFE_CRLF_FAIL */
192 die("CRLF would be replaced by LF in %s.", path);
194 } else if (output_eol(crlf_action) == EOL_CRLF) {
196 * CRLFs would be added by checkout:
197 * check if we have "naked" LFs
199 if (stats->lf != stats->crlf) {
200 if (checksafe == SAFE_CRLF_WARN)
201 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
202 else /* i.e. SAFE_CRLF_FAIL */
203 die("LF would be replaced by CRLF in %s", path);
208 static int has_cr_in_index(const char *path)
214 data = read_blob_data_from_cache(path, &sz);
217 has_cr = memchr(data, '\r', sz) != NULL;
222 static int crlf_to_git(const char *path, const char *src, size_t len,
224 enum crlf_action crlf_action, enum safe_crlf checksafe)
226 struct text_stat stats;
229 if (crlf_action == CRLF_BINARY ||
230 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) ||
235 * If we are doing a dry-run and have no source buffer, there is
236 * nothing to analyze; we must assume we would convert.
241 gather_stats(src, len, &stats);
243 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
244 if (convert_is_binary(len, &stats))
247 if (crlf_action == CRLF_GUESS) {
249 * If the file in the index has any CR in it, do not convert.
250 * This is the new safer autocrlf handling.
252 if (has_cr_in_index(path))
257 check_safe_crlf(path, crlf_action, &stats, checksafe);
259 /* Optimization: No CR? Nothing to convert, regardless. */
264 * At this point all of our source analysis is done, and we are sure we
265 * would convert. If we are in dry-run mode, we can give an answer.
270 /* only grow if not in place */
271 if (strbuf_avail(buf) + buf->len < len)
272 strbuf_grow(buf, len - buf->len);
274 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
276 * If we guessed, we already know we rejected a file with
277 * lone CR, and we can strip a CR without looking at what
281 unsigned char c = *src++;
287 unsigned char c = *src++;
288 if (! (c == '\r' && (1 < len && *src == '\n')))
292 strbuf_setlen(buf, dst - buf->buf);
296 static int crlf_to_worktree(const char *path, const char *src, size_t len,
297 struct strbuf *buf, enum crlf_action crlf_action)
299 char *to_free = NULL;
300 struct text_stat stats;
302 if (!len || output_eol(crlf_action) != EOL_CRLF)
305 gather_stats(src, len, &stats);
307 /* No LF? Nothing to convert, regardless. */
311 /* Was it already in CRLF format? */
312 if (stats.lf == stats.crlf)
315 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
316 if (crlf_action == CRLF_GUESS) {
317 /* If we have any CR or CRLF line endings, we do not touch it */
318 /* This is the new safer autocrlf-handling */
319 if (stats.cr > 0 || stats.crlf > 0)
323 if (convert_is_binary(len, &stats))
327 /* are we "faking" in place editing ? */
329 to_free = strbuf_detach(buf, NULL);
331 strbuf_grow(buf, len + stats.lf - stats.crlf);
333 const char *nl = memchr(src, '\n', len);
336 if (nl > src && nl[-1] == '\r') {
337 strbuf_add(buf, src, nl + 1 - src);
339 strbuf_add(buf, src, nl - src);
340 strbuf_addstr(buf, "\r\n");
345 strbuf_add(buf, src, len);
351 struct filter_params {
359 static int filter_buffer_or_fd(int in, int out, void *data)
362 * Spawn cmd and feed the buffer contents through its stdin.
364 struct child_process child_process = CHILD_PROCESS_INIT;
365 struct filter_params *params = (struct filter_params *)data;
366 int write_err, status;
367 const char *argv[] = { NULL, NULL };
369 /* apply % substitution to cmd */
370 struct strbuf cmd = STRBUF_INIT;
371 struct strbuf path = STRBUF_INIT;
372 struct strbuf_expand_dict_entry dict[] = {
377 /* quote the path to preserve spaces, etc. */
378 sq_quote_buf(&path, params->path);
379 dict[0].value = path.buf;
381 /* expand all %f with the quoted path */
382 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
383 strbuf_release(&path);
387 child_process.argv = argv;
388 child_process.use_shell = 1;
389 child_process.in = -1;
390 child_process.out = out;
392 if (start_command(&child_process))
393 return error("cannot fork to run external filter %s", params->cmd);
395 sigchain_push(SIGPIPE, SIG_IGN);
398 write_err = (write_in_full(child_process.in,
399 params->src, params->size) < 0);
403 write_err = copy_fd(params->fd, child_process.in);
404 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
408 if (close(child_process.in))
411 error("cannot feed the input to external filter %s", params->cmd);
413 sigchain_pop(SIGPIPE);
415 status = finish_command(&child_process);
417 error("external filter %s failed %d", params->cmd, status);
419 strbuf_release(&cmd);
420 return (write_err || status);
423 static int apply_filter(const char *path, const char *src, size_t len, int fd,
424 struct strbuf *dst, const char *cmd)
427 * Create a pipeline to have the command filter the buffer's
430 * (child --> cmd) --> us
433 struct strbuf nbuf = STRBUF_INIT;
435 struct filter_params params;
443 memset(&async, 0, sizeof(async));
444 async.proc = filter_buffer_or_fd;
445 async.data = ¶ms;
454 if (start_async(&async))
455 return 0; /* error was already reported */
457 if (strbuf_read(&nbuf, async.out, len) < 0) {
458 error("read from external filter %s failed", cmd);
461 if (close(async.out)) {
462 error("read from external filter %s failed", cmd);
465 if (finish_async(&async)) {
466 error("external filter %s failed", cmd);
471 strbuf_swap(dst, &nbuf);
473 strbuf_release(&nbuf);
477 static struct convert_driver {
479 struct convert_driver *next;
483 } *user_convert, **user_convert_tail;
485 static int read_convert_config(const char *var, const char *value, void *cb)
487 const char *key, *name;
489 struct convert_driver *drv;
492 * External conversion drivers are configured using
493 * "filter.<name>.variable".
495 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
497 for (drv = user_convert; drv; drv = drv->next)
498 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
501 drv = xcalloc(1, sizeof(struct convert_driver));
502 drv->name = xmemdupz(name, namelen);
503 *user_convert_tail = drv;
504 user_convert_tail = &(drv->next);
508 * filter.<name>.smudge and filter.<name>.clean specifies
513 * The command-line will not be interpolated in any way.
516 if (!strcmp("smudge", key))
517 return git_config_string(&drv->smudge, var, value);
519 if (!strcmp("clean", key))
520 return git_config_string(&drv->clean, var, value);
522 if (!strcmp("required", key)) {
523 drv->required = git_config_bool(var, value);
530 static int count_ident(const char *cp, unsigned long size)
533 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
545 if (memcmp("Id", cp, 2))
556 * "$Id: ... "; scan up to the closing dollar sign and discard.
572 static int ident_to_git(const char *path, const char *src, size_t len,
573 struct strbuf *buf, int ident)
577 if (!ident || (src && !count_ident(src, len)))
583 /* only grow if not in place */
584 if (strbuf_avail(buf) + buf->len < len)
585 strbuf_grow(buf, len - buf->len);
588 dollar = memchr(src, '$', len);
591 memmove(dst, src, dollar + 1 - src);
592 dst += dollar + 1 - src;
593 len -= dollar + 1 - src;
596 if (len > 3 && !memcmp(src, "Id:", 3)) {
597 dollar = memchr(src + 3, '$', len - 3);
600 if (memchr(src + 3, '\n', dollar - src - 3)) {
601 /* Line break before the next dollar. */
605 memcpy(dst, "Id$", 3);
607 len -= dollar + 1 - src;
611 memmove(dst, src, len);
612 strbuf_setlen(buf, dst + len - buf->buf);
616 static int ident_to_worktree(const char *path, const char *src, size_t len,
617 struct strbuf *buf, int ident)
619 unsigned char sha1[20];
620 char *to_free = NULL, *dollar, *spc;
626 cnt = count_ident(src, len);
630 /* are we "faking" in place editing ? */
632 to_free = strbuf_detach(buf, NULL);
633 hash_sha1_file(src, len, "blob", sha1);
635 strbuf_grow(buf, len + cnt * 43);
637 /* step 1: run to the next '$' */
638 dollar = memchr(src, '$', len);
641 strbuf_add(buf, src, dollar + 1 - src);
642 len -= dollar + 1 - src;
645 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
646 if (len < 3 || memcmp("Id", src, 2))
649 /* step 3: skip over Id$ or Id:xxxxx$ */
653 } else if (src[2] == ':') {
655 * It's possible that an expanded Id has crept its way into the
656 * repository, we cope with that by stripping the expansion out.
657 * This is probably not a good idea, since it will cause changes
658 * on checkout, which won't go away by stash, but let's keep it
661 dollar = memchr(src + 3, '$', len - 3);
663 /* incomplete keyword, no more '$', so just quit the loop */
667 if (memchr(src + 3, '\n', dollar - src - 3)) {
668 /* Line break before the next dollar. */
672 spc = memchr(src + 4, ' ', dollar - src - 4);
673 if (spc && spc < dollar-1) {
674 /* There are spaces in unexpected places.
675 * This is probably an id from some other
676 * versioning system. Keep it for now.
681 len -= dollar + 1 - src;
684 /* it wasn't a "Id$" or "Id:xxxx$" */
688 /* step 4: substitute */
689 strbuf_addstr(buf, "Id: ");
690 strbuf_add(buf, sha1_to_hex(sha1), 40);
691 strbuf_addstr(buf, " $");
693 strbuf_add(buf, src, len);
699 static enum crlf_action git_path_check_crlf(const char *path, struct git_attr_check *check)
701 const char *value = check->value;
703 if (ATTR_TRUE(value))
705 else if (ATTR_FALSE(value))
707 else if (ATTR_UNSET(value))
709 else if (!strcmp(value, "input"))
711 else if (!strcmp(value, "auto"))
716 static enum eol git_path_check_eol(const char *path, struct git_attr_check *check)
718 const char *value = check->value;
720 if (ATTR_UNSET(value))
722 else if (!strcmp(value, "lf"))
724 else if (!strcmp(value, "crlf"))
729 static struct convert_driver *git_path_check_convert(const char *path,
730 struct git_attr_check *check)
732 const char *value = check->value;
733 struct convert_driver *drv;
735 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
737 for (drv = user_convert; drv; drv = drv->next)
738 if (!strcmp(value, drv->name))
743 static int git_path_check_ident(const char *path, struct git_attr_check *check)
745 const char *value = check->value;
747 return !!ATTR_TRUE(value);
750 static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol eol_attr)
752 if (text_attr == CRLF_BINARY)
754 if (eol_attr == EOL_LF)
756 if (eol_attr == EOL_CRLF)
762 struct convert_driver *drv;
763 enum crlf_action crlf_action;
768 static const char *conv_attr_name[] = {
769 "crlf", "ident", "filter", "eol", "text",
771 #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
773 static void convert_attrs(struct conv_attrs *ca, const char *path)
776 static struct git_attr_check ccheck[NUM_CONV_ATTRS];
778 if (!ccheck[0].attr) {
779 for (i = 0; i < NUM_CONV_ATTRS; i++)
780 ccheck[i].attr = git_attr(conv_attr_name[i]);
781 user_convert_tail = &user_convert;
782 git_config(read_convert_config, NULL);
785 if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
786 ca->crlf_action = git_path_check_crlf(path, ccheck + 4);
787 if (ca->crlf_action == CRLF_GUESS)
788 ca->crlf_action = git_path_check_crlf(path, ccheck + 0);
789 ca->ident = git_path_check_ident(path, ccheck + 1);
790 ca->drv = git_path_check_convert(path, ccheck + 2);
791 ca->eol_attr = git_path_check_eol(path, ccheck + 3);
794 ca->crlf_action = CRLF_GUESS;
795 ca->eol_attr = EOL_UNSET;
800 int would_convert_to_git_filter_fd(const char *path)
802 struct conv_attrs ca;
804 convert_attrs(&ca, path);
809 * Apply a filter to an fd only if the filter is required to succeed.
810 * We must die if the filter fails, because the original data before
811 * filtering is not available.
813 if (!ca.drv->required)
816 return apply_filter(path, NULL, 0, -1, NULL, ca.drv->clean);
819 const char *get_convert_attr_ascii(const char *path)
821 struct conv_attrs ca;
822 enum crlf_action crlf_action;
824 convert_attrs(&ca, path);
825 crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
826 switch (crlf_action) {
834 return "text eol=lf";
836 return "text=auto eol=crlf";
843 int convert_to_git(const char *path, const char *src, size_t len,
844 struct strbuf *dst, enum safe_crlf checksafe)
847 const char *filter = NULL;
849 struct conv_attrs ca;
851 convert_attrs(&ca, path);
853 filter = ca.drv->clean;
854 required = ca.drv->required;
857 ret |= apply_filter(path, src, len, -1, dst, filter);
858 if (!ret && required)
859 die("%s: clean filter '%s' failed", path, ca.drv->name);
865 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
866 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
871 return ret | ident_to_git(path, src, len, dst, ca.ident);
874 void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
875 enum safe_crlf checksafe)
877 struct conv_attrs ca;
878 convert_attrs(&ca, path);
881 assert(ca.drv->clean);
883 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))
884 die("%s: clean filter '%s' failed", path, ca.drv->name);
886 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
887 crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
888 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
891 static int convert_to_working_tree_internal(const char *path, const char *src,
892 size_t len, struct strbuf *dst,
895 int ret = 0, ret_filter = 0;
896 const char *filter = NULL;
898 struct conv_attrs ca;
900 convert_attrs(&ca, path);
902 filter = ca.drv->smudge;
903 required = ca.drv->required;
906 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
912 * CRLF conversion can be skipped if normalizing, unless there
913 * is a smudge filter. The filter might expect CRLFs.
915 if (filter || !normalizing) {
916 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
917 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
924 ret_filter = apply_filter(path, src, len, -1, dst, filter);
925 if (!ret_filter && required)
926 die("%s: smudge filter %s failed", path, ca.drv->name);
928 return ret | ret_filter;
931 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
933 return convert_to_working_tree_internal(path, src, len, dst, 0);
936 int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
938 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
943 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_FALSE);
946 /*****************************************************************
948 * Streaming conversion support
950 *****************************************************************/
952 typedef int (*filter_fn)(struct stream_filter *,
953 const char *input, size_t *isize_p,
954 char *output, size_t *osize_p);
955 typedef void (*free_fn)(struct stream_filter *);
957 struct stream_filter_vtbl {
962 struct stream_filter {
963 struct stream_filter_vtbl *vtbl;
966 static int null_filter_fn(struct stream_filter *filter,
967 const char *input, size_t *isize_p,
968 char *output, size_t *osize_p)
973 return 0; /* we do not keep any states */
975 if (*osize_p < count)
978 memmove(output, input, count);
985 static void null_free_fn(struct stream_filter *filter)
987 ; /* nothing -- null instances are shared */
990 static struct stream_filter_vtbl null_vtbl = {
995 static struct stream_filter null_filter_singleton = {
999 int is_null_stream_filter(struct stream_filter *filter)
1001 return filter == &null_filter_singleton;
1009 struct lf_to_crlf_filter {
1010 struct stream_filter filter;
1011 unsigned has_held:1;
1015 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1016 const char *input, size_t *isize_p,
1017 char *output, size_t *osize_p)
1019 size_t count, o = 0;
1020 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1023 * We may be holding onto the CR to see if it is followed by a
1024 * LF, in which case we would need to go to the main loop.
1025 * Otherwise, just emit it to the output stream.
1027 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1028 output[o++] = lf_to_crlf->held;
1029 lf_to_crlf->has_held = 0;
1032 /* We are told to drain */
1039 if (count || lf_to_crlf->has_held) {
1043 if (lf_to_crlf->has_held) {
1045 lf_to_crlf->has_held = 0;
1048 for (i = 0; o < *osize_p && i < count; i++) {
1053 } else if (was_cr) {
1055 * Previous round saw CR and it is not followed
1056 * by a LF; emit the CR before processing the
1057 * current character.
1063 * We may have consumed the last output slot,
1064 * in which case we need to break out of this
1065 * loop; hold the current character before
1068 if (*osize_p <= o) {
1069 lf_to_crlf->has_held = 1;
1070 lf_to_crlf->held = ch;
1071 continue; /* break but increment i */
1086 if (!lf_to_crlf->has_held && was_cr) {
1087 lf_to_crlf->has_held = 1;
1088 lf_to_crlf->held = '\r';
1094 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1099 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1100 lf_to_crlf_filter_fn,
1104 static struct stream_filter *lf_to_crlf_filter(void)
1106 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1108 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1109 return (struct stream_filter *)lf_to_crlf;
1115 #define FILTER_BUFFER 1024
1116 struct cascade_filter {
1117 struct stream_filter filter;
1118 struct stream_filter *one;
1119 struct stream_filter *two;
1120 char buf[FILTER_BUFFER];
1124 static int cascade_filter_fn(struct stream_filter *filter,
1125 const char *input, size_t *isize_p,
1126 char *output, size_t *osize_p)
1128 struct cascade_filter *cas = (struct cascade_filter *) filter;
1130 size_t sz = *osize_p;
1131 size_t to_feed, remaining;
1134 * input -- (one) --> buf -- (two) --> output
1136 while (filled < sz) {
1137 remaining = sz - filled;
1139 /* do we already have something to feed two with? */
1140 if (cas->ptr < cas->end) {
1141 to_feed = cas->end - cas->ptr;
1142 if (stream_filter(cas->two,
1143 cas->buf + cas->ptr, &to_feed,
1144 output + filled, &remaining))
1146 cas->ptr += (cas->end - cas->ptr) - to_feed;
1147 filled = sz - remaining;
1151 /* feed one from upstream and have it emit into our buffer */
1152 to_feed = input ? *isize_p : 0;
1153 if (input && !to_feed)
1155 remaining = sizeof(cas->buf);
1156 if (stream_filter(cas->one,
1158 cas->buf, &remaining))
1160 cas->end = sizeof(cas->buf) - remaining;
1163 size_t fed = *isize_p - to_feed;
1168 /* do we know that we drained one completely? */
1169 if (input || cas->end)
1172 /* tell two to drain; we have nothing more to give it */
1174 remaining = sz - filled;
1175 if (stream_filter(cas->two,
1177 output + filled, &remaining))
1179 if (remaining == (sz - filled))
1180 break; /* completely drained two */
1181 filled = sz - remaining;
1187 static void cascade_free_fn(struct stream_filter *filter)
1189 struct cascade_filter *cas = (struct cascade_filter *)filter;
1190 free_stream_filter(cas->one);
1191 free_stream_filter(cas->two);
1195 static struct stream_filter_vtbl cascade_vtbl = {
1200 static struct stream_filter *cascade_filter(struct stream_filter *one,
1201 struct stream_filter *two)
1203 struct cascade_filter *cascade;
1205 if (!one || is_null_stream_filter(one))
1207 if (!two || is_null_stream_filter(two))
1210 cascade = xmalloc(sizeof(*cascade));
1213 cascade->end = cascade->ptr = 0;
1214 cascade->filter.vtbl = &cascade_vtbl;
1215 return (struct stream_filter *)cascade;
1221 #define IDENT_DRAINING (-1)
1222 #define IDENT_SKIPPING (-2)
1223 struct ident_filter {
1224 struct stream_filter filter;
1227 char ident[45]; /* ": x40 $" */
1230 static int is_foreign_ident(const char *str)
1234 if (!skip_prefix(str, "$Id: ", &str))
1236 for (i = 0; str[i]; i++) {
1237 if (isspace(str[i]) && str[i+1] != '$')
1243 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1245 size_t to_drain = ident->left.len;
1247 if (*osize_p < to_drain)
1248 to_drain = *osize_p;
1250 memcpy(*output_p, ident->left.buf, to_drain);
1251 strbuf_remove(&ident->left, 0, to_drain);
1252 *output_p += to_drain;
1253 *osize_p -= to_drain;
1255 if (!ident->left.len)
1259 static int ident_filter_fn(struct stream_filter *filter,
1260 const char *input, size_t *isize_p,
1261 char *output, size_t *osize_p)
1263 struct ident_filter *ident = (struct ident_filter *)filter;
1264 static const char head[] = "$Id";
1267 /* drain upon eof */
1268 switch (ident->state) {
1270 strbuf_add(&ident->left, head, ident->state);
1271 case IDENT_SKIPPING:
1273 case IDENT_DRAINING:
1274 ident_drain(ident, &output, osize_p);
1279 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1282 if (ident->state == IDENT_DRAINING) {
1283 ident_drain(ident, &output, osize_p);
1292 if (ident->state == IDENT_SKIPPING) {
1294 * Skipping until '$' or LF, but keeping them
1295 * in case it is a foreign ident.
1297 strbuf_addch(&ident->left, ch);
1298 if (ch != '\n' && ch != '$')
1300 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1301 strbuf_setlen(&ident->left, sizeof(head) - 1);
1302 strbuf_addstr(&ident->left, ident->ident);
1304 ident->state = IDENT_DRAINING;
1308 if (ident->state < sizeof(head) &&
1309 head[ident->state] == ch) {
1315 strbuf_add(&ident->left, head, ident->state);
1316 if (ident->state == sizeof(head) - 1) {
1317 if (ch != ':' && ch != '$') {
1318 strbuf_addch(&ident->left, ch);
1324 strbuf_addch(&ident->left, ch);
1325 ident->state = IDENT_SKIPPING;
1327 strbuf_addstr(&ident->left, ident->ident);
1328 ident->state = IDENT_DRAINING;
1333 strbuf_addch(&ident->left, ch);
1334 ident->state = IDENT_DRAINING;
1339 static void ident_free_fn(struct stream_filter *filter)
1341 struct ident_filter *ident = (struct ident_filter *)filter;
1342 strbuf_release(&ident->left);
1346 static struct stream_filter_vtbl ident_vtbl = {
1351 static struct stream_filter *ident_filter(const unsigned char *sha1)
1353 struct ident_filter *ident = xmalloc(sizeof(*ident));
1355 xsnprintf(ident->ident, sizeof(ident->ident),
1356 ": %s $", sha1_to_hex(sha1));
1357 strbuf_init(&ident->left, 0);
1358 ident->filter.vtbl = &ident_vtbl;
1360 return (struct stream_filter *)ident;
1364 * Return an appropriately constructed filter for the path, or NULL if
1365 * the contents cannot be filtered without reading the whole thing
1368 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1369 * large binary blob you would want us not to slurp into the memory!
1371 struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1373 struct conv_attrs ca;
1374 enum crlf_action crlf_action;
1375 struct stream_filter *filter = NULL;
1377 convert_attrs(&ca, path);
1379 if (ca.drv && (ca.drv->smudge || ca.drv->clean))
1383 filter = ident_filter(sha1);
1385 crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
1387 if ((crlf_action == CRLF_BINARY) || (crlf_action == CRLF_INPUT) ||
1388 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE))
1389 filter = cascade_filter(filter, &null_filter_singleton);
1391 else if (output_eol(crlf_action) == EOL_CRLF &&
1392 !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS))
1393 filter = cascade_filter(filter, lf_to_crlf_filter());
1398 void free_stream_filter(struct stream_filter *filter)
1400 filter->vtbl->free(filter);
1403 int stream_filter(struct stream_filter *filter,
1404 const char *input, size_t *isize_p,
1405 char *output, size_t *osize_p)
1407 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);