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.
26 /* NUL, CR, LF and CRLF counts */
27 unsigned nul, cr, lf, crlf;
29 /* These are just approximations! */
30 unsigned printable, nonprintable;
33 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
37 memset(stats, 0, sizeof(*stats));
39 for (i = 0; i < size; i++) {
40 unsigned char c = buf[i];
43 if (i+1 < size && buf[i+1] == '\n')
53 stats->nonprintable++;
56 /* BS, HT, ESC and FF */
57 case '\b': case '\t': case '\033': case '\014':
64 stats->nonprintable++;
71 /* If file ends with EOF then don't count this EOF as non-printable. */
72 if (size >= 1 && buf[size-1] == '\032')
73 stats->nonprintable--;
77 * The same heuristics as diff.c::mmfile_is_binary()
79 static int is_binary(unsigned long size, struct text_stat *stats)
84 if ((stats->printable >> 7) < stats->nonprintable)
87 * Other heuristics? Average line length might be relevant,
88 * as might LF vs CR vs CRLF counts..
90 * NOTE! It might be normal to have a low ratio of CRLF to LF
91 * (somebody starts with a LF-only file and edits it with an editor
92 * that adds CRLF only to lines that are added..). But do we
93 * want to support CR-only? Probably not.
98 static enum eol output_eol(enum crlf_action crlf_action)
100 switch (crlf_action) {
113 if (auto_crlf == AUTO_CRLF_TRUE)
115 else if (auto_crlf == AUTO_CRLF_INPUT)
117 else if (core_eol == EOL_UNSET)
123 static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
124 struct text_stat *stats, enum safe_crlf checksafe)
129 if (output_eol(crlf_action) == EOL_LF) {
131 * CRLFs would not be restored by checkout:
132 * check if we'd remove CRLFs
135 if (checksafe == SAFE_CRLF_WARN)
136 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
137 else /* i.e. SAFE_CRLF_FAIL */
138 die("CRLF would be replaced by LF in %s.", path);
140 } else if (output_eol(crlf_action) == EOL_CRLF) {
142 * CRLFs would be added by checkout:
143 * check if we have "naked" LFs
145 if (stats->lf != stats->crlf) {
146 if (checksafe == SAFE_CRLF_WARN)
147 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
148 else /* i.e. SAFE_CRLF_FAIL */
149 die("LF would be replaced by CRLF in %s", path);
154 static int has_cr_in_index(const char *path)
158 enum object_type type;
161 struct index_state *istate = &the_index;
164 pos = index_name_pos(istate, path, len);
167 * We might be in the middle of a merge, in which
168 * case we would read stage #2 (ours).
172 (pos < 0 && i < istate->cache_nr &&
173 !strcmp(istate->cache[i]->name, path));
175 if (ce_stage(istate->cache[i]) == 2)
180 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
181 if (!data || type != OBJ_BLOB) {
186 has_cr = memchr(data, '\r', sz) != NULL;
191 static int crlf_to_git(const char *path, const char *src, size_t len,
193 enum crlf_action crlf_action, enum safe_crlf checksafe)
195 struct text_stat stats;
198 if (crlf_action == CRLF_BINARY ||
199 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) ||
204 * If we are doing a dry-run and have no source buffer, there is
205 * nothing to analyze; we must assume we would convert.
210 gather_stats(src, len, &stats);
212 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
214 * We're currently not going to even try to convert stuff
215 * that has bare CR characters. Does anybody do that crazy
218 if (stats.cr != stats.crlf)
222 * And add some heuristics for binary vs text, of course...
224 if (is_binary(len, &stats))
227 if (crlf_action == CRLF_GUESS) {
229 * If the file in the index has any CR in it, do not convert.
230 * This is the new safer autocrlf handling.
232 if (has_cr_in_index(path))
237 check_safe_crlf(path, crlf_action, &stats, checksafe);
239 /* Optimization: No CR? Nothing to convert, regardless. */
244 * At this point all of our source analysis is done, and we are sure we
245 * would convert. If we are in dry-run mode, we can give an answer.
250 /* only grow if not in place */
251 if (strbuf_avail(buf) + buf->len < len)
252 strbuf_grow(buf, len - buf->len);
254 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
256 * If we guessed, we already know we rejected a file with
257 * lone CR, and we can strip a CR without looking at what
261 unsigned char c = *src++;
267 unsigned char c = *src++;
268 if (! (c == '\r' && (1 < len && *src == '\n')))
272 strbuf_setlen(buf, dst - buf->buf);
276 static int crlf_to_worktree(const char *path, const char *src, size_t len,
277 struct strbuf *buf, enum crlf_action crlf_action)
279 char *to_free = NULL;
280 struct text_stat stats;
282 if (!len || output_eol(crlf_action) != EOL_CRLF)
285 gather_stats(src, len, &stats);
287 /* No LF? Nothing to convert, regardless. */
291 /* Was it already in CRLF format? */
292 if (stats.lf == stats.crlf)
295 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
296 if (crlf_action == CRLF_GUESS) {
297 /* If we have any CR or CRLF line endings, we do not touch it */
298 /* This is the new safer autocrlf-handling */
299 if (stats.cr > 0 || stats.crlf > 0)
303 /* If we have any bare CR characters, we're not going to touch it */
304 if (stats.cr != stats.crlf)
307 if (is_binary(len, &stats))
311 /* are we "faking" in place editing ? */
313 to_free = strbuf_detach(buf, NULL);
315 strbuf_grow(buf, len + stats.lf - stats.crlf);
317 const char *nl = memchr(src, '\n', len);
320 if (nl > src && nl[-1] == '\r') {
321 strbuf_add(buf, src, nl + 1 - src);
323 strbuf_add(buf, src, nl - src);
324 strbuf_addstr(buf, "\r\n");
329 strbuf_add(buf, src, len);
335 struct filter_params {
342 static int filter_buffer(int in, int out, void *data)
345 * Spawn cmd and feed the buffer contents through its stdin.
347 struct child_process child_process;
348 struct filter_params *params = (struct filter_params *)data;
349 int write_err, status;
350 const char *argv[] = { NULL, NULL };
352 /* apply % substitution to cmd */
353 struct strbuf cmd = STRBUF_INIT;
354 struct strbuf path = STRBUF_INIT;
355 struct strbuf_expand_dict_entry dict[] = {
360 /* quote the path to preserve spaces, etc. */
361 sq_quote_buf(&path, params->path);
362 dict[0].value = path.buf;
364 /* expand all %f with the quoted path */
365 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
366 strbuf_release(&path);
370 memset(&child_process, 0, sizeof(child_process));
371 child_process.argv = argv;
372 child_process.use_shell = 1;
373 child_process.in = -1;
374 child_process.out = out;
376 if (start_command(&child_process))
377 return error("cannot fork to run external filter %s", params->cmd);
379 sigchain_push(SIGPIPE, SIG_IGN);
381 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
382 if (close(child_process.in))
385 error("cannot feed the input to external filter %s", params->cmd);
387 sigchain_pop(SIGPIPE);
389 status = finish_command(&child_process);
391 error("external filter %s failed %d", params->cmd, status);
393 strbuf_release(&cmd);
394 return (write_err || status);
397 static int apply_filter(const char *path, const char *src, size_t len,
398 struct strbuf *dst, const char *cmd)
401 * Create a pipeline to have the command filter the buffer's
404 * (child --> cmd) --> us
407 struct strbuf nbuf = STRBUF_INIT;
409 struct filter_params params;
417 memset(&async, 0, sizeof(async));
418 async.proc = filter_buffer;
419 async.data = ¶ms;
427 if (start_async(&async))
428 return 0; /* error was already reported */
430 if (strbuf_read(&nbuf, async.out, len) < 0) {
431 error("read from external filter %s failed", cmd);
434 if (close(async.out)) {
435 error("read from external filter %s failed", cmd);
438 if (finish_async(&async)) {
439 error("external filter %s failed", cmd);
444 strbuf_swap(dst, &nbuf);
446 strbuf_release(&nbuf);
450 static struct convert_driver {
452 struct convert_driver *next;
456 } *user_convert, **user_convert_tail;
458 static int read_convert_config(const char *var, const char *value, void *cb)
460 const char *ep, *name;
462 struct convert_driver *drv;
465 * External conversion drivers are configured using
466 * "filter.<name>.variable".
468 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
472 for (drv = user_convert; drv; drv = drv->next)
473 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
476 drv = xcalloc(1, sizeof(struct convert_driver));
477 drv->name = xmemdupz(name, namelen);
478 *user_convert_tail = drv;
479 user_convert_tail = &(drv->next);
485 * filter.<name>.smudge and filter.<name>.clean specifies
490 * The command-line will not be interpolated in any way.
493 if (!strcmp("smudge", ep))
494 return git_config_string(&drv->smudge, var, value);
496 if (!strcmp("clean", ep))
497 return git_config_string(&drv->clean, var, value);
499 if (!strcmp("required", ep)) {
500 drv->required = git_config_bool(var, value);
507 static int count_ident(const char *cp, unsigned long size)
510 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
522 if (memcmp("Id", cp, 2))
533 * "$Id: ... "; scan up to the closing dollar sign and discard.
549 static int ident_to_git(const char *path, const char *src, size_t len,
550 struct strbuf *buf, int ident)
554 if (!ident || (src && !count_ident(src, len)))
560 /* only grow if not in place */
561 if (strbuf_avail(buf) + buf->len < len)
562 strbuf_grow(buf, len - buf->len);
565 dollar = memchr(src, '$', len);
568 memmove(dst, src, dollar + 1 - src);
569 dst += dollar + 1 - src;
570 len -= dollar + 1 - src;
573 if (len > 3 && !memcmp(src, "Id:", 3)) {
574 dollar = memchr(src + 3, '$', len - 3);
577 if (memchr(src + 3, '\n', dollar - src - 3)) {
578 /* Line break before the next dollar. */
582 memcpy(dst, "Id$", 3);
584 len -= dollar + 1 - src;
588 memmove(dst, src, len);
589 strbuf_setlen(buf, dst + len - buf->buf);
593 static int ident_to_worktree(const char *path, const char *src, size_t len,
594 struct strbuf *buf, int ident)
596 unsigned char sha1[20];
597 char *to_free = NULL, *dollar, *spc;
603 cnt = count_ident(src, len);
607 /* are we "faking" in place editing ? */
609 to_free = strbuf_detach(buf, NULL);
610 hash_sha1_file(src, len, "blob", sha1);
612 strbuf_grow(buf, len + cnt * 43);
614 /* step 1: run to the next '$' */
615 dollar = memchr(src, '$', len);
618 strbuf_add(buf, src, dollar + 1 - src);
619 len -= dollar + 1 - src;
622 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
623 if (len < 3 || memcmp("Id", src, 2))
626 /* step 3: skip over Id$ or Id:xxxxx$ */
630 } else if (src[2] == ':') {
632 * It's possible that an expanded Id has crept its way into the
633 * repository, we cope with that by stripping the expansion out.
634 * This is probably not a good idea, since it will cause changes
635 * on checkout, which won't go away by stash, but let's keep it
638 dollar = memchr(src + 3, '$', len - 3);
640 /* incomplete keyword, no more '$', so just quit the loop */
644 if (memchr(src + 3, '\n', dollar - src - 3)) {
645 /* Line break before the next dollar. */
649 spc = memchr(src + 4, ' ', dollar - src - 4);
650 if (spc && spc < dollar-1) {
651 /* There are spaces in unexpected places.
652 * This is probably an id from some other
653 * versioning system. Keep it for now.
658 len -= dollar + 1 - src;
661 /* it wasn't a "Id$" or "Id:xxxx$" */
665 /* step 4: substitute */
666 strbuf_addstr(buf, "Id: ");
667 strbuf_add(buf, sha1_to_hex(sha1), 40);
668 strbuf_addstr(buf, " $");
670 strbuf_add(buf, src, len);
676 static enum crlf_action git_path_check_crlf(const char *path, struct git_attr_check *check)
678 const char *value = check->value;
680 if (ATTR_TRUE(value))
682 else if (ATTR_FALSE(value))
684 else if (ATTR_UNSET(value))
686 else if (!strcmp(value, "input"))
688 else if (!strcmp(value, "auto"))
693 static enum eol git_path_check_eol(const char *path, struct git_attr_check *check)
695 const char *value = check->value;
697 if (ATTR_UNSET(value))
699 else if (!strcmp(value, "lf"))
701 else if (!strcmp(value, "crlf"))
706 static struct convert_driver *git_path_check_convert(const char *path,
707 struct git_attr_check *check)
709 const char *value = check->value;
710 struct convert_driver *drv;
712 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
714 for (drv = user_convert; drv; drv = drv->next)
715 if (!strcmp(value, drv->name))
720 static int git_path_check_ident(const char *path, struct git_attr_check *check)
722 const char *value = check->value;
724 return !!ATTR_TRUE(value);
727 static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol eol_attr)
729 if (text_attr == CRLF_BINARY)
731 if (eol_attr == EOL_LF)
733 if (eol_attr == EOL_CRLF)
739 struct convert_driver *drv;
740 enum crlf_action crlf_action;
745 static const char *conv_attr_name[] = {
746 "crlf", "ident", "filter", "eol", "text",
748 #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
750 static void convert_attrs(struct conv_attrs *ca, const char *path)
753 static struct git_attr_check ccheck[NUM_CONV_ATTRS];
755 if (!ccheck[0].attr) {
756 for (i = 0; i < NUM_CONV_ATTRS; i++)
757 ccheck[i].attr = git_attr(conv_attr_name[i]);
758 user_convert_tail = &user_convert;
759 git_config(read_convert_config, NULL);
762 if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
763 ca->crlf_action = git_path_check_crlf(path, ccheck + 4);
764 if (ca->crlf_action == CRLF_GUESS)
765 ca->crlf_action = git_path_check_crlf(path, ccheck + 0);
766 ca->ident = git_path_check_ident(path, ccheck + 1);
767 ca->drv = git_path_check_convert(path, ccheck + 2);
768 ca->eol_attr = git_path_check_eol(path, ccheck + 3);
771 ca->crlf_action = CRLF_GUESS;
772 ca->eol_attr = EOL_UNSET;
777 int convert_to_git(const char *path, const char *src, size_t len,
778 struct strbuf *dst, enum safe_crlf checksafe)
781 const char *filter = NULL;
783 struct conv_attrs ca;
785 convert_attrs(&ca, path);
787 filter = ca.drv->clean;
788 required = ca.drv->required;
791 ret |= apply_filter(path, src, len, dst, filter);
792 if (!ret && required)
793 die("%s: clean filter '%s' failed", path, ca.drv->name);
799 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
800 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
805 return ret | ident_to_git(path, src, len, dst, ca.ident);
808 static int convert_to_working_tree_internal(const char *path, const char *src,
809 size_t len, struct strbuf *dst,
812 int ret = 0, ret_filter = 0;
813 const char *filter = NULL;
815 struct conv_attrs ca;
817 convert_attrs(&ca, path);
819 filter = ca.drv->smudge;
820 required = ca.drv->required;
823 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
829 * CRLF conversion can be skipped if normalizing, unless there
830 * is a smudge filter. The filter might expect CRLFs.
832 if (filter || !normalizing) {
833 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
834 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
841 ret_filter = apply_filter(path, src, len, dst, filter);
842 if (!ret_filter && required)
843 die("%s: smudge filter %s failed", path, ca.drv->name);
845 return ret | ret_filter;
848 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
850 return convert_to_working_tree_internal(path, src, len, dst, 0);
853 int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
855 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
860 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_FALSE);
863 /*****************************************************************
865 * Streaming converison support
867 *****************************************************************/
869 typedef int (*filter_fn)(struct stream_filter *,
870 const char *input, size_t *isize_p,
871 char *output, size_t *osize_p);
872 typedef void (*free_fn)(struct stream_filter *);
874 struct stream_filter_vtbl {
879 struct stream_filter {
880 struct stream_filter_vtbl *vtbl;
883 static int null_filter_fn(struct stream_filter *filter,
884 const char *input, size_t *isize_p,
885 char *output, size_t *osize_p)
890 return 0; /* we do not keep any states */
892 if (*osize_p < count)
895 memmove(output, input, count);
902 static void null_free_fn(struct stream_filter *filter)
904 ; /* nothing -- null instances are shared */
907 static struct stream_filter_vtbl null_vtbl = {
912 static struct stream_filter null_filter_singleton = {
916 int is_null_stream_filter(struct stream_filter *filter)
918 return filter == &null_filter_singleton;
926 struct lf_to_crlf_filter {
927 struct stream_filter filter;
932 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
933 const char *input, size_t *isize_p,
934 char *output, size_t *osize_p)
937 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
940 * We may be holding onto the CR to see if it is followed by a
941 * LF, in which case we would need to go to the main loop.
942 * Otherwise, just emit it to the output stream.
944 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
945 output[o++] = lf_to_crlf->held;
946 lf_to_crlf->has_held = 0;
949 /* We are told to drain */
956 if (count || lf_to_crlf->has_held) {
960 if (lf_to_crlf->has_held) {
962 lf_to_crlf->has_held = 0;
965 for (i = 0; o < *osize_p && i < count; i++) {
972 * Previous round saw CR and it is not followed
973 * by a LF; emit the CR before processing the
980 * We may have consumed the last output slot,
981 * in which case we need to break out of this
982 * loop; hold the current character before
986 lf_to_crlf->has_held = 1;
987 lf_to_crlf->held = ch;
988 continue; /* break but increment i */
1003 if (!lf_to_crlf->has_held && was_cr) {
1004 lf_to_crlf->has_held = 1;
1005 lf_to_crlf->held = '\r';
1011 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1016 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1017 lf_to_crlf_filter_fn,
1021 static struct stream_filter *lf_to_crlf_filter(void)
1023 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1025 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1026 return (struct stream_filter *)lf_to_crlf;
1032 #define FILTER_BUFFER 1024
1033 struct cascade_filter {
1034 struct stream_filter filter;
1035 struct stream_filter *one;
1036 struct stream_filter *two;
1037 char buf[FILTER_BUFFER];
1041 static int cascade_filter_fn(struct stream_filter *filter,
1042 const char *input, size_t *isize_p,
1043 char *output, size_t *osize_p)
1045 struct cascade_filter *cas = (struct cascade_filter *) filter;
1047 size_t sz = *osize_p;
1048 size_t to_feed, remaining;
1051 * input -- (one) --> buf -- (two) --> output
1053 while (filled < sz) {
1054 remaining = sz - filled;
1056 /* do we already have something to feed two with? */
1057 if (cas->ptr < cas->end) {
1058 to_feed = cas->end - cas->ptr;
1059 if (stream_filter(cas->two,
1060 cas->buf + cas->ptr, &to_feed,
1061 output + filled, &remaining))
1063 cas->ptr += (cas->end - cas->ptr) - to_feed;
1064 filled = sz - remaining;
1068 /* feed one from upstream and have it emit into our buffer */
1069 to_feed = input ? *isize_p : 0;
1070 if (input && !to_feed)
1072 remaining = sizeof(cas->buf);
1073 if (stream_filter(cas->one,
1075 cas->buf, &remaining))
1077 cas->end = sizeof(cas->buf) - remaining;
1080 size_t fed = *isize_p - to_feed;
1085 /* do we know that we drained one completely? */
1086 if (input || cas->end)
1089 /* tell two to drain; we have nothing more to give it */
1091 remaining = sz - filled;
1092 if (stream_filter(cas->two,
1094 output + filled, &remaining))
1096 if (remaining == (sz - filled))
1097 break; /* completely drained two */
1098 filled = sz - remaining;
1104 static void cascade_free_fn(struct stream_filter *filter)
1106 struct cascade_filter *cas = (struct cascade_filter *)filter;
1107 free_stream_filter(cas->one);
1108 free_stream_filter(cas->two);
1112 static struct stream_filter_vtbl cascade_vtbl = {
1117 static struct stream_filter *cascade_filter(struct stream_filter *one,
1118 struct stream_filter *two)
1120 struct cascade_filter *cascade;
1122 if (!one || is_null_stream_filter(one))
1124 if (!two || is_null_stream_filter(two))
1127 cascade = xmalloc(sizeof(*cascade));
1130 cascade->end = cascade->ptr = 0;
1131 cascade->filter.vtbl = &cascade_vtbl;
1132 return (struct stream_filter *)cascade;
1138 #define IDENT_DRAINING (-1)
1139 #define IDENT_SKIPPING (-2)
1140 struct ident_filter {
1141 struct stream_filter filter;
1144 char ident[45]; /* ": x40 $" */
1147 static int is_foreign_ident(const char *str)
1151 if (prefixcmp(str, "$Id: "))
1153 for (i = 5; str[i]; i++) {
1154 if (isspace(str[i]) && str[i+1] != '$')
1160 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1162 size_t to_drain = ident->left.len;
1164 if (*osize_p < to_drain)
1165 to_drain = *osize_p;
1167 memcpy(*output_p, ident->left.buf, to_drain);
1168 strbuf_remove(&ident->left, 0, to_drain);
1169 *output_p += to_drain;
1170 *osize_p -= to_drain;
1172 if (!ident->left.len)
1176 static int ident_filter_fn(struct stream_filter *filter,
1177 const char *input, size_t *isize_p,
1178 char *output, size_t *osize_p)
1180 struct ident_filter *ident = (struct ident_filter *)filter;
1181 static const char head[] = "$Id";
1184 /* drain upon eof */
1185 switch (ident->state) {
1187 strbuf_add(&ident->left, head, ident->state);
1188 case IDENT_SKIPPING:
1190 case IDENT_DRAINING:
1191 ident_drain(ident, &output, osize_p);
1196 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1199 if (ident->state == IDENT_DRAINING) {
1200 ident_drain(ident, &output, osize_p);
1209 if (ident->state == IDENT_SKIPPING) {
1211 * Skipping until '$' or LF, but keeping them
1212 * in case it is a foreign ident.
1214 strbuf_addch(&ident->left, ch);
1215 if (ch != '\n' && ch != '$')
1217 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1218 strbuf_setlen(&ident->left, sizeof(head) - 1);
1219 strbuf_addstr(&ident->left, ident->ident);
1221 ident->state = IDENT_DRAINING;
1225 if (ident->state < sizeof(head) &&
1226 head[ident->state] == ch) {
1232 strbuf_add(&ident->left, head, ident->state);
1233 if (ident->state == sizeof(head) - 1) {
1234 if (ch != ':' && ch != '$') {
1235 strbuf_addch(&ident->left, ch);
1241 strbuf_addch(&ident->left, ch);
1242 ident->state = IDENT_SKIPPING;
1244 strbuf_addstr(&ident->left, ident->ident);
1245 ident->state = IDENT_DRAINING;
1250 strbuf_addch(&ident->left, ch);
1251 ident->state = IDENT_DRAINING;
1256 static void ident_free_fn(struct stream_filter *filter)
1258 struct ident_filter *ident = (struct ident_filter *)filter;
1259 strbuf_release(&ident->left);
1263 static struct stream_filter_vtbl ident_vtbl = {
1268 static struct stream_filter *ident_filter(const unsigned char *sha1)
1270 struct ident_filter *ident = xmalloc(sizeof(*ident));
1272 sprintf(ident->ident, ": %s $", sha1_to_hex(sha1));
1273 strbuf_init(&ident->left, 0);
1274 ident->filter.vtbl = &ident_vtbl;
1276 return (struct stream_filter *)ident;
1280 * Return an appropriately constructed filter for the path, or NULL if
1281 * the contents cannot be filtered without reading the whole thing
1284 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1285 * large binary blob you would want us not to slurp into the memory!
1287 struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1289 struct conv_attrs ca;
1290 enum crlf_action crlf_action;
1291 struct stream_filter *filter = NULL;
1293 convert_attrs(&ca, path);
1295 if (ca.drv && (ca.drv->smudge || ca.drv->clean))
1299 filter = ident_filter(sha1);
1301 crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
1303 if ((crlf_action == CRLF_BINARY) || (crlf_action == CRLF_INPUT) ||
1304 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE))
1305 filter = cascade_filter(filter, &null_filter_singleton);
1307 else if (output_eol(crlf_action) == EOL_CRLF &&
1308 !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS))
1309 filter = cascade_filter(filter, lf_to_crlf_filter());
1314 void free_stream_filter(struct stream_filter *filter)
1316 filter->vtbl->free(filter);
1319 int stream_filter(struct stream_filter *filter,
1320 const char *input, size_t *isize_p,
1321 char *output, size_t *osize_p)
1323 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);