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
33 /* NUL, CR, LF and CRLF counts */
34 unsigned nul, lonecr, lonelf, crlf;
36 /* These are just approximations! */
37 unsigned printable, nonprintable;
40 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
44 memset(stats, 0, sizeof(*stats));
46 for (i = 0; i < size; i++) {
47 unsigned char c = buf[i];
49 if (i+1 < size && buf[i+1] == '\n') {
62 stats->nonprintable++;
65 /* BS, HT, ESC and FF */
66 case '\b': case '\t': case '\033': case '\014':
73 stats->nonprintable++;
80 /* If file ends with EOF then don't count this EOF as non-printable. */
81 if (size >= 1 && buf[size-1] == '\032')
82 stats->nonprintable--;
86 * The same heuristics as diff.c::mmfile_is_binary()
87 * We treat files with bare CR as binary
89 static int convert_is_binary(unsigned long size, const struct text_stat *stats)
95 if ((stats->printable >> 7) < stats->nonprintable)
100 static unsigned int gather_convert_stats(const char *data, unsigned long size)
102 struct text_stat stats;
106 gather_stats(data, size, &stats);
107 if (convert_is_binary(size, &stats))
108 ret |= CONVERT_STAT_BITS_BIN;
110 ret |= CONVERT_STAT_BITS_TXT_CRLF;
112 ret |= CONVERT_STAT_BITS_TXT_LF;
117 static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
119 unsigned int convert_stats = gather_convert_stats(data, size);
121 if (convert_stats & CONVERT_STAT_BITS_BIN)
123 switch (convert_stats) {
124 case CONVERT_STAT_BITS_TXT_LF:
126 case CONVERT_STAT_BITS_TXT_CRLF:
128 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
135 const char *get_cached_convert_stats_ascii(const char *path)
139 void *data = read_blob_data_from_cache(path, &sz);
140 ret = gather_convert_stats_ascii(data, sz);
145 const char *get_wt_convert_stats_ascii(const char *path)
147 const char *ret = "";
148 struct strbuf sb = STRBUF_INIT;
149 if (strbuf_read_file(&sb, path, 0) >= 0)
150 ret = gather_convert_stats_ascii(sb.buf, sb.len);
155 static int text_eol_is_crlf(void)
157 if (auto_crlf == AUTO_CRLF_TRUE)
159 else if (auto_crlf == AUTO_CRLF_INPUT)
161 if (core_eol == EOL_CRLF)
163 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
168 static enum eol output_eol(enum crlf_action crlf_action)
170 switch (crlf_action) {
175 case CRLF_TEXT_INPUT:
179 case CRLF_AUTO_INPUT:
183 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
185 warning("Illegal crlf_action %d\n", (int)crlf_action);
189 static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
190 struct text_stat *stats, enum safe_crlf checksafe)
195 if (output_eol(crlf_action) == EOL_LF) {
197 * CRLFs would not be restored by checkout:
198 * check if we'd remove CRLFs
201 if (checksafe == SAFE_CRLF_WARN)
202 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
203 else /* i.e. SAFE_CRLF_FAIL */
204 die("CRLF would be replaced by LF in %s.", path);
206 } else if (output_eol(crlf_action) == EOL_CRLF) {
208 * CRLFs would be added by checkout:
209 * check if we have "naked" LFs
212 if (checksafe == SAFE_CRLF_WARN)
213 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
214 else /* i.e. SAFE_CRLF_FAIL */
215 die("LF would be replaced by CRLF in %s", path);
220 static int has_cr_in_index(const char *path)
226 data = read_blob_data_from_cache(path, &sz);
229 has_cr = memchr(data, '\r', sz) != NULL;
234 static int crlf_to_git(const char *path, const char *src, size_t len,
236 enum crlf_action crlf_action, enum safe_crlf checksafe)
238 struct text_stat stats;
241 if (crlf_action == CRLF_BINARY ||
246 * If we are doing a dry-run and have no source buffer, there is
247 * nothing to analyze; we must assume we would convert.
252 gather_stats(src, len, &stats);
254 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
255 if (convert_is_binary(len, &stats))
258 if (crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
260 * If the file in the index has any CR in it, do not convert.
261 * This is the new safer autocrlf handling.
263 if (has_cr_in_index(path))
268 check_safe_crlf(path, crlf_action, &stats, checksafe);
270 /* Optimization: No CRLF? Nothing to convert, regardless. */
275 * At this point all of our source analysis is done, and we are sure we
276 * would convert. If we are in dry-run mode, we can give an answer.
281 /* only grow if not in place */
282 if (strbuf_avail(buf) + buf->len < len)
283 strbuf_grow(buf, len - buf->len);
285 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
287 * If we guessed, we already know we rejected a file with
288 * lone CR, and we can strip a CR without looking at what
292 unsigned char c = *src++;
298 unsigned char c = *src++;
299 if (! (c == '\r' && (1 < len && *src == '\n')))
303 strbuf_setlen(buf, dst - buf->buf);
307 static int crlf_to_worktree(const char *path, const char *src, size_t len,
308 struct strbuf *buf, enum crlf_action crlf_action)
310 char *to_free = NULL;
311 struct text_stat stats;
313 if (!len || output_eol(crlf_action) != EOL_CRLF)
316 gather_stats(src, len, &stats);
318 /* No "naked" LF? Nothing to convert, regardless. */
322 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
323 if (crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
324 /* If we have any CR or CRLF line endings, we do not touch it */
325 /* This is the new safer autocrlf-handling */
326 if (stats.lonecr || stats.crlf )
330 if (convert_is_binary(len, &stats))
334 /* are we "faking" in place editing ? */
336 to_free = strbuf_detach(buf, NULL);
338 strbuf_grow(buf, len + stats.lonelf);
340 const char *nl = memchr(src, '\n', len);
343 if (nl > src && nl[-1] == '\r') {
344 strbuf_add(buf, src, nl + 1 - src);
346 strbuf_add(buf, src, nl - src);
347 strbuf_addstr(buf, "\r\n");
352 strbuf_add(buf, src, len);
358 struct filter_params {
366 static int filter_buffer_or_fd(int in, int out, void *data)
369 * Spawn cmd and feed the buffer contents through its stdin.
371 struct child_process child_process = CHILD_PROCESS_INIT;
372 struct filter_params *params = (struct filter_params *)data;
373 int write_err, status;
374 const char *argv[] = { NULL, NULL };
376 /* apply % substitution to cmd */
377 struct strbuf cmd = STRBUF_INIT;
378 struct strbuf path = STRBUF_INIT;
379 struct strbuf_expand_dict_entry dict[] = {
384 /* quote the path to preserve spaces, etc. */
385 sq_quote_buf(&path, params->path);
386 dict[0].value = path.buf;
388 /* expand all %f with the quoted path */
389 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
390 strbuf_release(&path);
394 child_process.argv = argv;
395 child_process.use_shell = 1;
396 child_process.in = -1;
397 child_process.out = out;
399 if (start_command(&child_process))
400 return error("cannot fork to run external filter %s", params->cmd);
402 sigchain_push(SIGPIPE, SIG_IGN);
405 write_err = (write_in_full(child_process.in,
406 params->src, params->size) < 0);
410 write_err = copy_fd(params->fd, child_process.in);
411 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
415 if (close(child_process.in))
418 error("cannot feed the input to external filter %s", params->cmd);
420 sigchain_pop(SIGPIPE);
422 status = finish_command(&child_process);
424 error("external filter %s failed %d", params->cmd, status);
426 strbuf_release(&cmd);
427 return (write_err || status);
430 static int apply_filter(const char *path, const char *src, size_t len, int fd,
431 struct strbuf *dst, const char *cmd)
434 * Create a pipeline to have the command filter the buffer's
437 * (child --> cmd) --> us
440 struct strbuf nbuf = STRBUF_INIT;
442 struct filter_params params;
450 memset(&async, 0, sizeof(async));
451 async.proc = filter_buffer_or_fd;
452 async.data = ¶ms;
461 if (start_async(&async))
462 return 0; /* error was already reported */
464 if (strbuf_read(&nbuf, async.out, len) < 0) {
465 error("read from external filter %s failed", cmd);
468 if (close(async.out)) {
469 error("read from external filter %s failed", cmd);
472 if (finish_async(&async)) {
473 error("external filter %s failed", cmd);
478 strbuf_swap(dst, &nbuf);
480 strbuf_release(&nbuf);
484 static struct convert_driver {
486 struct convert_driver *next;
490 } *user_convert, **user_convert_tail;
492 static int read_convert_config(const char *var, const char *value, void *cb)
494 const char *key, *name;
496 struct convert_driver *drv;
499 * External conversion drivers are configured using
500 * "filter.<name>.variable".
502 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
504 for (drv = user_convert; drv; drv = drv->next)
505 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
508 drv = xcalloc(1, sizeof(struct convert_driver));
509 drv->name = xmemdupz(name, namelen);
510 *user_convert_tail = drv;
511 user_convert_tail = &(drv->next);
515 * filter.<name>.smudge and filter.<name>.clean specifies
520 * The command-line will not be interpolated in any way.
523 if (!strcmp("smudge", key))
524 return git_config_string(&drv->smudge, var, value);
526 if (!strcmp("clean", key))
527 return git_config_string(&drv->clean, var, value);
529 if (!strcmp("required", key)) {
530 drv->required = git_config_bool(var, value);
537 static int count_ident(const char *cp, unsigned long size)
540 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
552 if (memcmp("Id", cp, 2))
563 * "$Id: ... "; scan up to the closing dollar sign and discard.
579 static int ident_to_git(const char *path, const char *src, size_t len,
580 struct strbuf *buf, int ident)
584 if (!ident || (src && !count_ident(src, len)))
590 /* only grow if not in place */
591 if (strbuf_avail(buf) + buf->len < len)
592 strbuf_grow(buf, len - buf->len);
595 dollar = memchr(src, '$', len);
598 memmove(dst, src, dollar + 1 - src);
599 dst += dollar + 1 - src;
600 len -= dollar + 1 - src;
603 if (len > 3 && !memcmp(src, "Id:", 3)) {
604 dollar = memchr(src + 3, '$', len - 3);
607 if (memchr(src + 3, '\n', dollar - src - 3)) {
608 /* Line break before the next dollar. */
612 memcpy(dst, "Id$", 3);
614 len -= dollar + 1 - src;
618 memmove(dst, src, len);
619 strbuf_setlen(buf, dst + len - buf->buf);
623 static int ident_to_worktree(const char *path, const char *src, size_t len,
624 struct strbuf *buf, int ident)
626 unsigned char sha1[20];
627 char *to_free = NULL, *dollar, *spc;
633 cnt = count_ident(src, len);
637 /* are we "faking" in place editing ? */
639 to_free = strbuf_detach(buf, NULL);
640 hash_sha1_file(src, len, "blob", sha1);
642 strbuf_grow(buf, len + cnt * 43);
644 /* step 1: run to the next '$' */
645 dollar = memchr(src, '$', len);
648 strbuf_add(buf, src, dollar + 1 - src);
649 len -= dollar + 1 - src;
652 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
653 if (len < 3 || memcmp("Id", src, 2))
656 /* step 3: skip over Id$ or Id:xxxxx$ */
660 } else if (src[2] == ':') {
662 * It's possible that an expanded Id has crept its way into the
663 * repository, we cope with that by stripping the expansion out.
664 * This is probably not a good idea, since it will cause changes
665 * on checkout, which won't go away by stash, but let's keep it
668 dollar = memchr(src + 3, '$', len - 3);
670 /* incomplete keyword, no more '$', so just quit the loop */
674 if (memchr(src + 3, '\n', dollar - src - 3)) {
675 /* Line break before the next dollar. */
679 spc = memchr(src + 4, ' ', dollar - src - 4);
680 if (spc && spc < dollar-1) {
681 /* There are spaces in unexpected places.
682 * This is probably an id from some other
683 * versioning system. Keep it for now.
688 len -= dollar + 1 - src;
691 /* it wasn't a "Id$" or "Id:xxxx$" */
695 /* step 4: substitute */
696 strbuf_addstr(buf, "Id: ");
697 strbuf_add(buf, sha1_to_hex(sha1), 40);
698 strbuf_addstr(buf, " $");
700 strbuf_add(buf, src, len);
706 static enum crlf_action git_path_check_crlf(struct git_attr_check *check)
708 const char *value = check->value;
710 if (ATTR_TRUE(value))
712 else if (ATTR_FALSE(value))
714 else if (ATTR_UNSET(value))
716 else if (!strcmp(value, "input"))
717 return CRLF_TEXT_INPUT;
718 else if (!strcmp(value, "auto"))
720 return CRLF_UNDEFINED;
723 static enum eol git_path_check_eol(struct git_attr_check *check)
725 const char *value = check->value;
727 if (ATTR_UNSET(value))
729 else if (!strcmp(value, "lf"))
731 else if (!strcmp(value, "crlf"))
736 static struct convert_driver *git_path_check_convert(struct git_attr_check *check)
738 const char *value = check->value;
739 struct convert_driver *drv;
741 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
743 for (drv = user_convert; drv; drv = drv->next)
744 if (!strcmp(value, drv->name))
749 static int git_path_check_ident(struct git_attr_check *check)
751 const char *value = check->value;
753 return !!ATTR_TRUE(value);
757 struct convert_driver *drv;
758 enum crlf_action attr_action; /* What attr says */
759 enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
763 static const char *conv_attr_name[] = {
764 "crlf", "ident", "filter", "eol", "text",
766 #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
768 static void convert_attrs(struct conv_attrs *ca, const char *path)
771 static struct git_attr_check ccheck[NUM_CONV_ATTRS];
773 if (!ccheck[0].attr) {
774 for (i = 0; i < NUM_CONV_ATTRS; i++)
775 ccheck[i].attr = git_attr(conv_attr_name[i]);
776 user_convert_tail = &user_convert;
777 git_config(read_convert_config, NULL);
780 if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
781 ca->crlf_action = git_path_check_crlf(ccheck + 4);
782 if (ca->crlf_action == CRLF_UNDEFINED)
783 ca->crlf_action = git_path_check_crlf(ccheck + 0);
784 ca->attr_action = ca->crlf_action;
785 ca->ident = git_path_check_ident(ccheck + 1);
786 ca->drv = git_path_check_convert(ccheck + 2);
787 if (ca->crlf_action != CRLF_BINARY) {
788 enum eol eol_attr = git_path_check_eol(ccheck + 3);
789 if (eol_attr == EOL_LF)
790 ca->crlf_action = CRLF_TEXT_INPUT;
791 else if (eol_attr == EOL_CRLF)
792 ca->crlf_action = CRLF_TEXT_CRLF;
794 ca->attr_action = ca->crlf_action;
797 ca->crlf_action = CRLF_UNDEFINED;
800 if (ca->crlf_action == CRLF_TEXT)
801 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
802 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
803 ca->crlf_action = CRLF_BINARY;
804 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
805 ca->crlf_action = CRLF_AUTO_CRLF;
806 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
807 ca->crlf_action = CRLF_AUTO_INPUT;
810 int would_convert_to_git_filter_fd(const char *path)
812 struct conv_attrs ca;
814 convert_attrs(&ca, path);
819 * Apply a filter to an fd only if the filter is required to succeed.
820 * We must die if the filter fails, because the original data before
821 * filtering is not available.
823 if (!ca.drv->required)
826 return apply_filter(path, NULL, 0, -1, NULL, ca.drv->clean);
829 const char *get_convert_attr_ascii(const char *path)
831 struct conv_attrs ca;
833 convert_attrs(&ca, path);
834 switch (ca.attr_action) {
841 case CRLF_TEXT_INPUT:
842 return "text eol=lf";
844 return "text eol=crlf";
848 return "text=auto eol=crlf"; /* This is not supported yet */
849 case CRLF_AUTO_INPUT:
850 return "text=auto eol=lf"; /* This is not supported yet */
855 int convert_to_git(const char *path, const char *src, size_t len,
856 struct strbuf *dst, enum safe_crlf checksafe)
859 const char *filter = NULL;
861 struct conv_attrs ca;
863 convert_attrs(&ca, path);
865 filter = ca.drv->clean;
866 required = ca.drv->required;
869 ret |= apply_filter(path, src, len, -1, dst, filter);
870 if (!ret && required)
871 die("%s: clean filter '%s' failed", path, ca.drv->name);
877 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
882 return ret | ident_to_git(path, src, len, dst, ca.ident);
885 void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
886 enum safe_crlf checksafe)
888 struct conv_attrs ca;
889 convert_attrs(&ca, path);
892 assert(ca.drv->clean);
894 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))
895 die("%s: clean filter '%s' failed", path, ca.drv->name);
897 crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
898 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
901 static int convert_to_working_tree_internal(const char *path, const char *src,
902 size_t len, struct strbuf *dst,
905 int ret = 0, ret_filter = 0;
906 const char *filter = NULL;
908 struct conv_attrs ca;
910 convert_attrs(&ca, path);
912 filter = ca.drv->smudge;
913 required = ca.drv->required;
916 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
922 * CRLF conversion can be skipped if normalizing, unless there
923 * is a smudge filter. The filter might expect CRLFs.
925 if (filter || !normalizing) {
926 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
933 ret_filter = apply_filter(path, src, len, -1, dst, filter);
934 if (!ret_filter && required)
935 die("%s: smudge filter %s failed", path, ca.drv->name);
937 return ret | ret_filter;
940 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
942 return convert_to_working_tree_internal(path, src, len, dst, 0);
945 int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
947 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
952 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_FALSE);
955 /*****************************************************************
957 * Streaming conversion support
959 *****************************************************************/
961 typedef int (*filter_fn)(struct stream_filter *,
962 const char *input, size_t *isize_p,
963 char *output, size_t *osize_p);
964 typedef void (*free_fn)(struct stream_filter *);
966 struct stream_filter_vtbl {
971 struct stream_filter {
972 struct stream_filter_vtbl *vtbl;
975 static int null_filter_fn(struct stream_filter *filter,
976 const char *input, size_t *isize_p,
977 char *output, size_t *osize_p)
982 return 0; /* we do not keep any states */
984 if (*osize_p < count)
987 memmove(output, input, count);
994 static void null_free_fn(struct stream_filter *filter)
996 ; /* nothing -- null instances are shared */
999 static struct stream_filter_vtbl null_vtbl = {
1004 static struct stream_filter null_filter_singleton = {
1008 int is_null_stream_filter(struct stream_filter *filter)
1010 return filter == &null_filter_singleton;
1018 struct lf_to_crlf_filter {
1019 struct stream_filter filter;
1020 unsigned has_held:1;
1024 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1025 const char *input, size_t *isize_p,
1026 char *output, size_t *osize_p)
1028 size_t count, o = 0;
1029 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1032 * We may be holding onto the CR to see if it is followed by a
1033 * LF, in which case we would need to go to the main loop.
1034 * Otherwise, just emit it to the output stream.
1036 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1037 output[o++] = lf_to_crlf->held;
1038 lf_to_crlf->has_held = 0;
1041 /* We are told to drain */
1048 if (count || lf_to_crlf->has_held) {
1052 if (lf_to_crlf->has_held) {
1054 lf_to_crlf->has_held = 0;
1057 for (i = 0; o < *osize_p && i < count; i++) {
1062 } else if (was_cr) {
1064 * Previous round saw CR and it is not followed
1065 * by a LF; emit the CR before processing the
1066 * current character.
1072 * We may have consumed the last output slot,
1073 * in which case we need to break out of this
1074 * loop; hold the current character before
1077 if (*osize_p <= o) {
1078 lf_to_crlf->has_held = 1;
1079 lf_to_crlf->held = ch;
1080 continue; /* break but increment i */
1095 if (!lf_to_crlf->has_held && was_cr) {
1096 lf_to_crlf->has_held = 1;
1097 lf_to_crlf->held = '\r';
1103 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1108 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1109 lf_to_crlf_filter_fn,
1113 static struct stream_filter *lf_to_crlf_filter(void)
1115 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1117 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1118 return (struct stream_filter *)lf_to_crlf;
1124 #define FILTER_BUFFER 1024
1125 struct cascade_filter {
1126 struct stream_filter filter;
1127 struct stream_filter *one;
1128 struct stream_filter *two;
1129 char buf[FILTER_BUFFER];
1133 static int cascade_filter_fn(struct stream_filter *filter,
1134 const char *input, size_t *isize_p,
1135 char *output, size_t *osize_p)
1137 struct cascade_filter *cas = (struct cascade_filter *) filter;
1139 size_t sz = *osize_p;
1140 size_t to_feed, remaining;
1143 * input -- (one) --> buf -- (two) --> output
1145 while (filled < sz) {
1146 remaining = sz - filled;
1148 /* do we already have something to feed two with? */
1149 if (cas->ptr < cas->end) {
1150 to_feed = cas->end - cas->ptr;
1151 if (stream_filter(cas->two,
1152 cas->buf + cas->ptr, &to_feed,
1153 output + filled, &remaining))
1155 cas->ptr += (cas->end - cas->ptr) - to_feed;
1156 filled = sz - remaining;
1160 /* feed one from upstream and have it emit into our buffer */
1161 to_feed = input ? *isize_p : 0;
1162 if (input && !to_feed)
1164 remaining = sizeof(cas->buf);
1165 if (stream_filter(cas->one,
1167 cas->buf, &remaining))
1169 cas->end = sizeof(cas->buf) - remaining;
1172 size_t fed = *isize_p - to_feed;
1177 /* do we know that we drained one completely? */
1178 if (input || cas->end)
1181 /* tell two to drain; we have nothing more to give it */
1183 remaining = sz - filled;
1184 if (stream_filter(cas->two,
1186 output + filled, &remaining))
1188 if (remaining == (sz - filled))
1189 break; /* completely drained two */
1190 filled = sz - remaining;
1196 static void cascade_free_fn(struct stream_filter *filter)
1198 struct cascade_filter *cas = (struct cascade_filter *)filter;
1199 free_stream_filter(cas->one);
1200 free_stream_filter(cas->two);
1204 static struct stream_filter_vtbl cascade_vtbl = {
1209 static struct stream_filter *cascade_filter(struct stream_filter *one,
1210 struct stream_filter *two)
1212 struct cascade_filter *cascade;
1214 if (!one || is_null_stream_filter(one))
1216 if (!two || is_null_stream_filter(two))
1219 cascade = xmalloc(sizeof(*cascade));
1222 cascade->end = cascade->ptr = 0;
1223 cascade->filter.vtbl = &cascade_vtbl;
1224 return (struct stream_filter *)cascade;
1230 #define IDENT_DRAINING (-1)
1231 #define IDENT_SKIPPING (-2)
1232 struct ident_filter {
1233 struct stream_filter filter;
1236 char ident[45]; /* ": x40 $" */
1239 static int is_foreign_ident(const char *str)
1243 if (!skip_prefix(str, "$Id: ", &str))
1245 for (i = 0; str[i]; i++) {
1246 if (isspace(str[i]) && str[i+1] != '$')
1252 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1254 size_t to_drain = ident->left.len;
1256 if (*osize_p < to_drain)
1257 to_drain = *osize_p;
1259 memcpy(*output_p, ident->left.buf, to_drain);
1260 strbuf_remove(&ident->left, 0, to_drain);
1261 *output_p += to_drain;
1262 *osize_p -= to_drain;
1264 if (!ident->left.len)
1268 static int ident_filter_fn(struct stream_filter *filter,
1269 const char *input, size_t *isize_p,
1270 char *output, size_t *osize_p)
1272 struct ident_filter *ident = (struct ident_filter *)filter;
1273 static const char head[] = "$Id";
1276 /* drain upon eof */
1277 switch (ident->state) {
1279 strbuf_add(&ident->left, head, ident->state);
1280 case IDENT_SKIPPING:
1282 case IDENT_DRAINING:
1283 ident_drain(ident, &output, osize_p);
1288 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1291 if (ident->state == IDENT_DRAINING) {
1292 ident_drain(ident, &output, osize_p);
1301 if (ident->state == IDENT_SKIPPING) {
1303 * Skipping until '$' or LF, but keeping them
1304 * in case it is a foreign ident.
1306 strbuf_addch(&ident->left, ch);
1307 if (ch != '\n' && ch != '$')
1309 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1310 strbuf_setlen(&ident->left, sizeof(head) - 1);
1311 strbuf_addstr(&ident->left, ident->ident);
1313 ident->state = IDENT_DRAINING;
1317 if (ident->state < sizeof(head) &&
1318 head[ident->state] == ch) {
1324 strbuf_add(&ident->left, head, ident->state);
1325 if (ident->state == sizeof(head) - 1) {
1326 if (ch != ':' && ch != '$') {
1327 strbuf_addch(&ident->left, ch);
1333 strbuf_addch(&ident->left, ch);
1334 ident->state = IDENT_SKIPPING;
1336 strbuf_addstr(&ident->left, ident->ident);
1337 ident->state = IDENT_DRAINING;
1342 strbuf_addch(&ident->left, ch);
1343 ident->state = IDENT_DRAINING;
1348 static void ident_free_fn(struct stream_filter *filter)
1350 struct ident_filter *ident = (struct ident_filter *)filter;
1351 strbuf_release(&ident->left);
1355 static struct stream_filter_vtbl ident_vtbl = {
1360 static struct stream_filter *ident_filter(const unsigned char *sha1)
1362 struct ident_filter *ident = xmalloc(sizeof(*ident));
1364 xsnprintf(ident->ident, sizeof(ident->ident),
1365 ": %s $", sha1_to_hex(sha1));
1366 strbuf_init(&ident->left, 0);
1367 ident->filter.vtbl = &ident_vtbl;
1369 return (struct stream_filter *)ident;
1373 * Return an appropriately constructed filter for the path, or NULL if
1374 * the contents cannot be filtered without reading the whole thing
1377 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1378 * large binary blob you would want us not to slurp into the memory!
1380 struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1382 struct conv_attrs ca;
1383 struct stream_filter *filter = NULL;
1385 convert_attrs(&ca, path);
1386 if (ca.drv && (ca.drv->smudge || ca.drv->clean))
1389 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1393 filter = ident_filter(sha1);
1395 if (output_eol(ca.crlf_action) == EOL_CRLF)
1396 filter = cascade_filter(filter, lf_to_crlf_filter());
1398 filter = cascade_filter(filter, &null_filter_singleton);
1403 void free_stream_filter(struct stream_filter *filter)
1405 filter->vtbl->free(filter);
1408 int stream_filter(struct stream_filter *filter,
1409 const char *input, size_t *isize_p,
1410 char *output, size_t *osize_p)
1412 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);