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
, cr
, lf
, 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
];
50 if (i
+1 < size
&& buf
[i
+1] == '\n')
60 stats
->nonprintable
++;
63 /* BS, HT, ESC and FF */
64 case '\b': case '\t': case '\033': case '\014':
71 stats
->nonprintable
++;
78 /* If file ends with EOF then don't count this EOF as non-printable. */
79 if (size
>= 1 && buf
[size
-1] == '\032')
80 stats
->nonprintable
--;
84 * The same heuristics as diff.c::mmfile_is_binary()
85 * We treat files with bare CR as binary
87 static int convert_is_binary(unsigned long size
, const struct text_stat
*stats
)
89 if (stats
->cr
!= stats
->crlf
)
93 if ((stats
->printable
>> 7) < stats
->nonprintable
)
98 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
100 struct text_stat stats
;
103 gather_stats(data
, size
, &stats
);
104 if (convert_is_binary(size
, &stats
))
105 return CONVERT_STAT_BITS_BIN
;
106 else if (stats
.crlf
&& stats
.crlf
== stats
.lf
)
107 return CONVERT_STAT_BITS_TXT_CRLF
;
108 else if (stats
.crlf
&& stats
.lf
)
109 return CONVERT_STAT_BITS_TXT_CRLF
| CONVERT_STAT_BITS_TXT_LF
;
111 return CONVERT_STAT_BITS_TXT_LF
;
116 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
118 unsigned int convert_stats
= gather_convert_stats(data
, size
);
120 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
122 switch (convert_stats
) {
123 case CONVERT_STAT_BITS_TXT_LF
:
125 case CONVERT_STAT_BITS_TXT_CRLF
:
127 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
134 const char *get_cached_convert_stats_ascii(const char *path
)
138 void *data
= read_blob_data_from_cache(path
, &sz
);
139 ret
= gather_convert_stats_ascii(data
, sz
);
144 const char *get_wt_convert_stats_ascii(const char *path
)
146 const char *ret
= "";
147 struct strbuf sb
= STRBUF_INIT
;
148 if (strbuf_read_file(&sb
, path
, 0) >= 0)
149 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
154 static int text_eol_is_crlf(void)
156 if (auto_crlf
== AUTO_CRLF_TRUE
)
158 else if (auto_crlf
== AUTO_CRLF_INPUT
)
160 if (core_eol
== EOL_CRLF
)
162 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
167 static enum eol
output_eol(enum crlf_action crlf_action
)
169 switch (crlf_action
) {
174 case CRLF_TEXT_INPUT
:
178 case CRLF_AUTO_INPUT
:
182 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
184 warning("Illegal crlf_action %d\n", (int)crlf_action
);
188 static void check_safe_crlf(const char *path
, enum crlf_action crlf_action
,
189 struct text_stat
*stats
, enum safe_crlf checksafe
)
194 if (output_eol(crlf_action
) == EOL_LF
) {
196 * CRLFs would not be restored by checkout:
197 * check if we'd remove CRLFs
200 if (checksafe
== SAFE_CRLF_WARN
)
201 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path
);
202 else /* i.e. SAFE_CRLF_FAIL */
203 die("CRLF would be replaced by LF in %s.", path
);
205 } else if (output_eol(crlf_action
) == EOL_CRLF
) {
207 * CRLFs would be added by checkout:
208 * check if we have "naked" LFs
210 if (stats
->lf
!= stats
->crlf
) {
211 if (checksafe
== SAFE_CRLF_WARN
)
212 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path
);
213 else /* i.e. SAFE_CRLF_FAIL */
214 die("LF would be replaced by CRLF in %s", path
);
219 static int has_cr_in_index(const char *path
)
225 data
= read_blob_data_from_cache(path
, &sz
);
228 has_cr
= memchr(data
, '\r', sz
) != NULL
;
233 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
235 enum crlf_action crlf_action
, enum safe_crlf checksafe
)
237 struct text_stat stats
;
240 if (crlf_action
== CRLF_BINARY
||
245 * If we are doing a dry-run and have no source buffer, there is
246 * nothing to analyze; we must assume we would convert.
251 gather_stats(src
, len
, &stats
);
253 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
254 if (convert_is_binary(len
, &stats
))
257 if (crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
259 * If the file in the index has any CR in it, do not convert.
260 * This is the new safer autocrlf handling.
262 if (has_cr_in_index(path
))
267 check_safe_crlf(path
, crlf_action
, &stats
, checksafe
);
269 /* Optimization: No CR? Nothing to convert, regardless. */
274 * At this point all of our source analysis is done, and we are sure we
275 * would convert. If we are in dry-run mode, we can give an answer.
280 /* only grow if not in place */
281 if (strbuf_avail(buf
) + buf
->len
< len
)
282 strbuf_grow(buf
, len
- buf
->len
);
284 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
286 * If we guessed, we already know we rejected a file with
287 * lone CR, and we can strip a CR without looking at what
291 unsigned char c
= *src
++;
297 unsigned char c
= *src
++;
298 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
302 strbuf_setlen(buf
, dst
- buf
->buf
);
306 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
307 struct strbuf
*buf
, enum crlf_action crlf_action
)
309 char *to_free
= NULL
;
310 struct text_stat stats
;
312 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
315 gather_stats(src
, len
, &stats
);
317 /* No LF? Nothing to convert, regardless. */
321 /* Was it already in CRLF format? */
322 if (stats
.lf
== stats
.crlf
)
325 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
326 if (crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
327 /* If we have any CR or CRLF line endings, we do not touch it */
328 /* This is the new safer autocrlf-handling */
329 if (stats
.cr
> 0 || stats
.crlf
> 0)
333 if (convert_is_binary(len
, &stats
))
337 /* are we "faking" in place editing ? */
339 to_free
= strbuf_detach(buf
, NULL
);
341 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
343 const char *nl
= memchr(src
, '\n', len
);
346 if (nl
> src
&& nl
[-1] == '\r') {
347 strbuf_add(buf
, src
, nl
+ 1 - src
);
349 strbuf_add(buf
, src
, nl
- src
);
350 strbuf_addstr(buf
, "\r\n");
355 strbuf_add(buf
, src
, len
);
361 struct filter_params
{
369 static int filter_buffer_or_fd(int in
, int out
, void *data
)
372 * Spawn cmd and feed the buffer contents through its stdin.
374 struct child_process child_process
= CHILD_PROCESS_INIT
;
375 struct filter_params
*params
= (struct filter_params
*)data
;
376 int write_err
, status
;
377 const char *argv
[] = { NULL
, NULL
};
379 /* apply % substitution to cmd */
380 struct strbuf cmd
= STRBUF_INIT
;
381 struct strbuf path
= STRBUF_INIT
;
382 struct strbuf_expand_dict_entry dict
[] = {
387 /* quote the path to preserve spaces, etc. */
388 sq_quote_buf(&path
, params
->path
);
389 dict
[0].value
= path
.buf
;
391 /* expand all %f with the quoted path */
392 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
393 strbuf_release(&path
);
397 child_process
.argv
= argv
;
398 child_process
.use_shell
= 1;
399 child_process
.in
= -1;
400 child_process
.out
= out
;
402 if (start_command(&child_process
))
403 return error("cannot fork to run external filter %s", params
->cmd
);
405 sigchain_push(SIGPIPE
, SIG_IGN
);
408 write_err
= (write_in_full(child_process
.in
,
409 params
->src
, params
->size
) < 0);
413 write_err
= copy_fd(params
->fd
, child_process
.in
);
414 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
418 if (close(child_process
.in
))
421 error("cannot feed the input to external filter %s", params
->cmd
);
423 sigchain_pop(SIGPIPE
);
425 status
= finish_command(&child_process
);
427 error("external filter %s failed %d", params
->cmd
, status
);
429 strbuf_release(&cmd
);
430 return (write_err
|| status
);
433 static int apply_filter(const char *path
, const char *src
, size_t len
, int fd
,
434 struct strbuf
*dst
, const char *cmd
)
437 * Create a pipeline to have the command filter the buffer's
440 * (child --> cmd) --> us
443 struct strbuf nbuf
= STRBUF_INIT
;
445 struct filter_params params
;
453 memset(&async
, 0, sizeof(async
));
454 async
.proc
= filter_buffer_or_fd
;
455 async
.data
= ¶ms
;
464 if (start_async(&async
))
465 return 0; /* error was already reported */
467 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
468 error("read from external filter %s failed", cmd
);
471 if (close(async
.out
)) {
472 error("read from external filter %s failed", cmd
);
475 if (finish_async(&async
)) {
476 error("external filter %s failed", cmd
);
481 strbuf_swap(dst
, &nbuf
);
483 strbuf_release(&nbuf
);
487 static struct convert_driver
{
489 struct convert_driver
*next
;
493 } *user_convert
, **user_convert_tail
;
495 static int read_convert_config(const char *var
, const char *value
, void *cb
)
497 const char *key
, *name
;
499 struct convert_driver
*drv
;
502 * External conversion drivers are configured using
503 * "filter.<name>.variable".
505 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
507 for (drv
= user_convert
; drv
; drv
= drv
->next
)
508 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
511 drv
= xcalloc(1, sizeof(struct convert_driver
));
512 drv
->name
= xmemdupz(name
, namelen
);
513 *user_convert_tail
= drv
;
514 user_convert_tail
= &(drv
->next
);
518 * filter.<name>.smudge and filter.<name>.clean specifies
523 * The command-line will not be interpolated in any way.
526 if (!strcmp("smudge", key
))
527 return git_config_string(&drv
->smudge
, var
, value
);
529 if (!strcmp("clean", key
))
530 return git_config_string(&drv
->clean
, var
, value
);
532 if (!strcmp("required", key
)) {
533 drv
->required
= git_config_bool(var
, value
);
540 static int count_ident(const char *cp
, unsigned long size
)
543 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
555 if (memcmp("Id", cp
, 2))
566 * "$Id: ... "; scan up to the closing dollar sign and discard.
582 static int ident_to_git(const char *path
, const char *src
, size_t len
,
583 struct strbuf
*buf
, int ident
)
587 if (!ident
|| (src
&& !count_ident(src
, len
)))
593 /* only grow if not in place */
594 if (strbuf_avail(buf
) + buf
->len
< len
)
595 strbuf_grow(buf
, len
- buf
->len
);
598 dollar
= memchr(src
, '$', len
);
601 memmove(dst
, src
, dollar
+ 1 - src
);
602 dst
+= dollar
+ 1 - src
;
603 len
-= dollar
+ 1 - src
;
606 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
607 dollar
= memchr(src
+ 3, '$', len
- 3);
610 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
611 /* Line break before the next dollar. */
615 memcpy(dst
, "Id$", 3);
617 len
-= dollar
+ 1 - src
;
621 memmove(dst
, src
, len
);
622 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
626 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
627 struct strbuf
*buf
, int ident
)
629 unsigned char sha1
[20];
630 char *to_free
= NULL
, *dollar
, *spc
;
636 cnt
= count_ident(src
, len
);
640 /* are we "faking" in place editing ? */
642 to_free
= strbuf_detach(buf
, NULL
);
643 hash_sha1_file(src
, len
, "blob", sha1
);
645 strbuf_grow(buf
, len
+ cnt
* 43);
647 /* step 1: run to the next '$' */
648 dollar
= memchr(src
, '$', len
);
651 strbuf_add(buf
, src
, dollar
+ 1 - src
);
652 len
-= dollar
+ 1 - src
;
655 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
656 if (len
< 3 || memcmp("Id", src
, 2))
659 /* step 3: skip over Id$ or Id:xxxxx$ */
663 } else if (src
[2] == ':') {
665 * It's possible that an expanded Id has crept its way into the
666 * repository, we cope with that by stripping the expansion out.
667 * This is probably not a good idea, since it will cause changes
668 * on checkout, which won't go away by stash, but let's keep it
671 dollar
= memchr(src
+ 3, '$', len
- 3);
673 /* incomplete keyword, no more '$', so just quit the loop */
677 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
678 /* Line break before the next dollar. */
682 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
683 if (spc
&& spc
< dollar
-1) {
684 /* There are spaces in unexpected places.
685 * This is probably an id from some other
686 * versioning system. Keep it for now.
691 len
-= dollar
+ 1 - src
;
694 /* it wasn't a "Id$" or "Id:xxxx$" */
698 /* step 4: substitute */
699 strbuf_addstr(buf
, "Id: ");
700 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
701 strbuf_addstr(buf
, " $");
703 strbuf_add(buf
, src
, len
);
709 static enum crlf_action
git_path_check_crlf(struct git_attr_check
*check
)
711 const char *value
= check
->value
;
713 if (ATTR_TRUE(value
))
714 return text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
715 else if (ATTR_FALSE(value
))
717 else if (ATTR_UNSET(value
))
719 else if (!strcmp(value
, "input"))
720 return CRLF_TEXT_INPUT
;
721 else if (!strcmp(value
, "auto"))
723 return CRLF_UNDEFINED
;
726 static enum eol
git_path_check_eol(struct git_attr_check
*check
)
728 const char *value
= check
->value
;
730 if (ATTR_UNSET(value
))
732 else if (!strcmp(value
, "lf"))
734 else if (!strcmp(value
, "crlf"))
739 static struct convert_driver
*git_path_check_convert(struct git_attr_check
*check
)
741 const char *value
= check
->value
;
742 struct convert_driver
*drv
;
744 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
746 for (drv
= user_convert
; drv
; drv
= drv
->next
)
747 if (!strcmp(value
, drv
->name
))
752 static int git_path_check_ident(struct git_attr_check
*check
)
754 const char *value
= check
->value
;
756 return !!ATTR_TRUE(value
);
760 struct convert_driver
*drv
;
761 enum crlf_action attr_action
; /* What attr says */
762 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
766 static const char *conv_attr_name
[] = {
767 "crlf", "ident", "filter", "eol", "text",
769 #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
771 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
774 static struct git_attr_check ccheck
[NUM_CONV_ATTRS
];
776 if (!ccheck
[0].attr
) {
777 for (i
= 0; i
< NUM_CONV_ATTRS
; i
++)
778 ccheck
[i
].attr
= git_attr(conv_attr_name
[i
]);
779 user_convert_tail
= &user_convert
;
780 git_config(read_convert_config
, NULL
);
783 if (!git_check_attr(path
, NUM_CONV_ATTRS
, ccheck
)) {
785 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
786 if (ca
->crlf_action
== CRLF_UNDEFINED
)
787 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
788 ca
->attr_action
= ca
->crlf_action
;
789 ca
->ident
= git_path_check_ident(ccheck
+ 1);
790 ca
->drv
= git_path_check_convert(ccheck
+ 2);
791 if (ca
->crlf_action
== CRLF_BINARY
)
793 eol_attr
= git_path_check_eol(ccheck
+ 3);
794 if (eol_attr
== EOL_LF
)
795 ca
->crlf_action
= CRLF_TEXT_INPUT
;
796 else if (eol_attr
== EOL_CRLF
)
797 ca
->crlf_action
= CRLF_TEXT_CRLF
;
800 ca
->crlf_action
= CRLF_UNDEFINED
;
803 if (ca
->crlf_action
== CRLF_TEXT
)
804 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
805 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
806 ca
->crlf_action
= CRLF_BINARY
;
807 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
808 ca
->crlf_action
= CRLF_AUTO_CRLF
;
809 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
810 ca
->crlf_action
= CRLF_AUTO_INPUT
;
813 int would_convert_to_git_filter_fd(const char *path
)
815 struct conv_attrs ca
;
817 convert_attrs(&ca
, path
);
822 * Apply a filter to an fd only if the filter is required to succeed.
823 * We must die if the filter fails, because the original data before
824 * filtering is not available.
826 if (!ca
.drv
->required
)
829 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
->clean
);
832 const char *get_convert_attr_ascii(const char *path
)
834 struct conv_attrs ca
;
836 convert_attrs(&ca
, path
);
837 switch (ca
.attr_action
) {
844 case CRLF_TEXT_INPUT
:
845 return "text eol=lf";
847 return "text eol=crlf";
851 return "text=auto eol=crlf"; /* This is not supported yet */
852 case CRLF_AUTO_INPUT
:
853 return "text=auto eol=lf"; /* This is not supported yet */
858 int convert_to_git(const char *path
, const char *src
, size_t len
,
859 struct strbuf
*dst
, enum safe_crlf checksafe
)
862 const char *filter
= NULL
;
864 struct conv_attrs ca
;
866 convert_attrs(&ca
, path
);
868 filter
= ca
.drv
->clean
;
869 required
= ca
.drv
->required
;
872 ret
|= apply_filter(path
, src
, len
, -1, dst
, filter
);
873 if (!ret
&& required
)
874 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
880 ret
|= crlf_to_git(path
, src
, len
, dst
, ca
.crlf_action
, checksafe
);
885 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
888 void convert_to_git_filter_fd(const char *path
, int fd
, struct strbuf
*dst
,
889 enum safe_crlf checksafe
)
891 struct conv_attrs ca
;
892 convert_attrs(&ca
, path
);
895 assert(ca
.drv
->clean
);
897 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
->clean
))
898 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
900 crlf_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, checksafe
);
901 ident_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.ident
);
904 static int convert_to_working_tree_internal(const char *path
, const char *src
,
905 size_t len
, struct strbuf
*dst
,
908 int ret
= 0, ret_filter
= 0;
909 const char *filter
= NULL
;
911 struct conv_attrs ca
;
913 convert_attrs(&ca
, path
);
915 filter
= ca
.drv
->smudge
;
916 required
= ca
.drv
->required
;
919 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
925 * CRLF conversion can be skipped if normalizing, unless there
926 * is a smudge filter. The filter might expect CRLFs.
928 if (filter
|| !normalizing
) {
929 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
936 ret_filter
= apply_filter(path
, src
, len
, -1, dst
, filter
);
937 if (!ret_filter
&& required
)
938 die("%s: smudge filter %s failed", path
, ca
.drv
->name
);
940 return ret
| ret_filter
;
943 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
945 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0);
948 int renormalize_buffer(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
950 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1);
955 return ret
| convert_to_git(path
, src
, len
, dst
, SAFE_CRLF_FALSE
);
958 /*****************************************************************
960 * Streaming conversion support
962 *****************************************************************/
964 typedef int (*filter_fn
)(struct stream_filter
*,
965 const char *input
, size_t *isize_p
,
966 char *output
, size_t *osize_p
);
967 typedef void (*free_fn
)(struct stream_filter
*);
969 struct stream_filter_vtbl
{
974 struct stream_filter
{
975 struct stream_filter_vtbl
*vtbl
;
978 static int null_filter_fn(struct stream_filter
*filter
,
979 const char *input
, size_t *isize_p
,
980 char *output
, size_t *osize_p
)
985 return 0; /* we do not keep any states */
987 if (*osize_p
< count
)
990 memmove(output
, input
, count
);
997 static void null_free_fn(struct stream_filter
*filter
)
999 ; /* nothing -- null instances are shared */
1002 static struct stream_filter_vtbl null_vtbl
= {
1007 static struct stream_filter null_filter_singleton
= {
1011 int is_null_stream_filter(struct stream_filter
*filter
)
1013 return filter
== &null_filter_singleton
;
1021 struct lf_to_crlf_filter
{
1022 struct stream_filter filter
;
1023 unsigned has_held
:1;
1027 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1028 const char *input
, size_t *isize_p
,
1029 char *output
, size_t *osize_p
)
1031 size_t count
, o
= 0;
1032 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1035 * We may be holding onto the CR to see if it is followed by a
1036 * LF, in which case we would need to go to the main loop.
1037 * Otherwise, just emit it to the output stream.
1039 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1040 output
[o
++] = lf_to_crlf
->held
;
1041 lf_to_crlf
->has_held
= 0;
1044 /* We are told to drain */
1051 if (count
|| lf_to_crlf
->has_held
) {
1055 if (lf_to_crlf
->has_held
) {
1057 lf_to_crlf
->has_held
= 0;
1060 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1065 } else if (was_cr
) {
1067 * Previous round saw CR and it is not followed
1068 * by a LF; emit the CR before processing the
1069 * current character.
1075 * We may have consumed the last output slot,
1076 * in which case we need to break out of this
1077 * loop; hold the current character before
1080 if (*osize_p
<= o
) {
1081 lf_to_crlf
->has_held
= 1;
1082 lf_to_crlf
->held
= ch
;
1083 continue; /* break but increment i */
1098 if (!lf_to_crlf
->has_held
&& was_cr
) {
1099 lf_to_crlf
->has_held
= 1;
1100 lf_to_crlf
->held
= '\r';
1106 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1111 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1112 lf_to_crlf_filter_fn
,
1116 static struct stream_filter
*lf_to_crlf_filter(void)
1118 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1120 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1121 return (struct stream_filter
*)lf_to_crlf
;
1127 #define FILTER_BUFFER 1024
1128 struct cascade_filter
{
1129 struct stream_filter filter
;
1130 struct stream_filter
*one
;
1131 struct stream_filter
*two
;
1132 char buf
[FILTER_BUFFER
];
1136 static int cascade_filter_fn(struct stream_filter
*filter
,
1137 const char *input
, size_t *isize_p
,
1138 char *output
, size_t *osize_p
)
1140 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1142 size_t sz
= *osize_p
;
1143 size_t to_feed
, remaining
;
1146 * input -- (one) --> buf -- (two) --> output
1148 while (filled
< sz
) {
1149 remaining
= sz
- filled
;
1151 /* do we already have something to feed two with? */
1152 if (cas
->ptr
< cas
->end
) {
1153 to_feed
= cas
->end
- cas
->ptr
;
1154 if (stream_filter(cas
->two
,
1155 cas
->buf
+ cas
->ptr
, &to_feed
,
1156 output
+ filled
, &remaining
))
1158 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1159 filled
= sz
- remaining
;
1163 /* feed one from upstream and have it emit into our buffer */
1164 to_feed
= input
? *isize_p
: 0;
1165 if (input
&& !to_feed
)
1167 remaining
= sizeof(cas
->buf
);
1168 if (stream_filter(cas
->one
,
1170 cas
->buf
, &remaining
))
1172 cas
->end
= sizeof(cas
->buf
) - remaining
;
1175 size_t fed
= *isize_p
- to_feed
;
1180 /* do we know that we drained one completely? */
1181 if (input
|| cas
->end
)
1184 /* tell two to drain; we have nothing more to give it */
1186 remaining
= sz
- filled
;
1187 if (stream_filter(cas
->two
,
1189 output
+ filled
, &remaining
))
1191 if (remaining
== (sz
- filled
))
1192 break; /* completely drained two */
1193 filled
= sz
- remaining
;
1199 static void cascade_free_fn(struct stream_filter
*filter
)
1201 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1202 free_stream_filter(cas
->one
);
1203 free_stream_filter(cas
->two
);
1207 static struct stream_filter_vtbl cascade_vtbl
= {
1212 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1213 struct stream_filter
*two
)
1215 struct cascade_filter
*cascade
;
1217 if (!one
|| is_null_stream_filter(one
))
1219 if (!two
|| is_null_stream_filter(two
))
1222 cascade
= xmalloc(sizeof(*cascade
));
1225 cascade
->end
= cascade
->ptr
= 0;
1226 cascade
->filter
.vtbl
= &cascade_vtbl
;
1227 return (struct stream_filter
*)cascade
;
1233 #define IDENT_DRAINING (-1)
1234 #define IDENT_SKIPPING (-2)
1235 struct ident_filter
{
1236 struct stream_filter filter
;
1239 char ident
[45]; /* ": x40 $" */
1242 static int is_foreign_ident(const char *str
)
1246 if (!skip_prefix(str
, "$Id: ", &str
))
1248 for (i
= 0; str
[i
]; i
++) {
1249 if (isspace(str
[i
]) && str
[i
+1] != '$')
1255 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1257 size_t to_drain
= ident
->left
.len
;
1259 if (*osize_p
< to_drain
)
1260 to_drain
= *osize_p
;
1262 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1263 strbuf_remove(&ident
->left
, 0, to_drain
);
1264 *output_p
+= to_drain
;
1265 *osize_p
-= to_drain
;
1267 if (!ident
->left
.len
)
1271 static int ident_filter_fn(struct stream_filter
*filter
,
1272 const char *input
, size_t *isize_p
,
1273 char *output
, size_t *osize_p
)
1275 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1276 static const char head
[] = "$Id";
1279 /* drain upon eof */
1280 switch (ident
->state
) {
1282 strbuf_add(&ident
->left
, head
, ident
->state
);
1283 case IDENT_SKIPPING
:
1285 case IDENT_DRAINING
:
1286 ident_drain(ident
, &output
, osize_p
);
1291 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1294 if (ident
->state
== IDENT_DRAINING
) {
1295 ident_drain(ident
, &output
, osize_p
);
1304 if (ident
->state
== IDENT_SKIPPING
) {
1306 * Skipping until '$' or LF, but keeping them
1307 * in case it is a foreign ident.
1309 strbuf_addch(&ident
->left
, ch
);
1310 if (ch
!= '\n' && ch
!= '$')
1312 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1313 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1314 strbuf_addstr(&ident
->left
, ident
->ident
);
1316 ident
->state
= IDENT_DRAINING
;
1320 if (ident
->state
< sizeof(head
) &&
1321 head
[ident
->state
] == ch
) {
1327 strbuf_add(&ident
->left
, head
, ident
->state
);
1328 if (ident
->state
== sizeof(head
) - 1) {
1329 if (ch
!= ':' && ch
!= '$') {
1330 strbuf_addch(&ident
->left
, ch
);
1336 strbuf_addch(&ident
->left
, ch
);
1337 ident
->state
= IDENT_SKIPPING
;
1339 strbuf_addstr(&ident
->left
, ident
->ident
);
1340 ident
->state
= IDENT_DRAINING
;
1345 strbuf_addch(&ident
->left
, ch
);
1346 ident
->state
= IDENT_DRAINING
;
1351 static void ident_free_fn(struct stream_filter
*filter
)
1353 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1354 strbuf_release(&ident
->left
);
1358 static struct stream_filter_vtbl ident_vtbl
= {
1363 static struct stream_filter
*ident_filter(const unsigned char *sha1
)
1365 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1367 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1368 ": %s $", sha1_to_hex(sha1
));
1369 strbuf_init(&ident
->left
, 0);
1370 ident
->filter
.vtbl
= &ident_vtbl
;
1372 return (struct stream_filter
*)ident
;
1376 * Return an appropriately constructed filter for the path, or NULL if
1377 * the contents cannot be filtered without reading the whole thing
1380 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1381 * large binary blob you would want us not to slurp into the memory!
1383 struct stream_filter
*get_stream_filter(const char *path
, const unsigned char *sha1
)
1385 struct conv_attrs ca
;
1386 enum crlf_action crlf_action
;
1387 struct stream_filter
*filter
= NULL
;
1389 convert_attrs(&ca
, path
);
1391 if (ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->clean
))
1395 filter
= ident_filter(sha1
);
1397 crlf_action
= ca
.crlf_action
;
1399 if ((crlf_action
== CRLF_BINARY
) ||
1400 crlf_action
== CRLF_AUTO_INPUT
||
1401 (crlf_action
== CRLF_TEXT_INPUT
))
1402 filter
= cascade_filter(filter
, &null_filter_singleton
);
1404 else if (output_eol(crlf_action
) == EOL_CRLF
&&
1405 !(crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_CRLF
))
1406 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1411 void free_stream_filter(struct stream_filter
*filter
)
1413 filter
->vtbl
->free(filter
);
1416 int stream_filter(struct stream_filter
*filter
,
1417 const char *input
, size_t *isize_p
,
1418 char *output
, size_t *osize_p
)
1420 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);