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
)
160 data
= read_blob_data_from_cache(path
, &sz
);
163 has_cr
= memchr(data
, '\r', sz
) != NULL
;
168 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
170 enum crlf_action crlf_action
, enum safe_crlf checksafe
)
172 struct text_stat stats
;
175 if (crlf_action
== CRLF_BINARY
||
176 (crlf_action
== CRLF_GUESS
&& auto_crlf
== AUTO_CRLF_FALSE
) ||
181 * If we are doing a dry-run and have no source buffer, there is
182 * nothing to analyze; we must assume we would convert.
187 gather_stats(src
, len
, &stats
);
189 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
) {
191 * We're currently not going to even try to convert stuff
192 * that has bare CR characters. Does anybody do that crazy
195 if (stats
.cr
!= stats
.crlf
)
199 * And add some heuristics for binary vs text, of course...
201 if (is_binary(len
, &stats
))
204 if (crlf_action
== CRLF_GUESS
) {
206 * If the file in the index has any CR in it, do not convert.
207 * This is the new safer autocrlf handling.
209 if (has_cr_in_index(path
))
214 check_safe_crlf(path
, crlf_action
, &stats
, checksafe
);
216 /* Optimization: No CR? Nothing to convert, regardless. */
221 * At this point all of our source analysis is done, and we are sure we
222 * would convert. If we are in dry-run mode, we can give an answer.
227 /* only grow if not in place */
228 if (strbuf_avail(buf
) + buf
->len
< len
)
229 strbuf_grow(buf
, len
- buf
->len
);
231 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
) {
233 * If we guessed, we already know we rejected a file with
234 * lone CR, and we can strip a CR without looking at what
238 unsigned char c
= *src
++;
244 unsigned char c
= *src
++;
245 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
249 strbuf_setlen(buf
, dst
- buf
->buf
);
253 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
254 struct strbuf
*buf
, enum crlf_action crlf_action
)
256 char *to_free
= NULL
;
257 struct text_stat stats
;
259 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
262 gather_stats(src
, len
, &stats
);
264 /* No LF? Nothing to convert, regardless. */
268 /* Was it already in CRLF format? */
269 if (stats
.lf
== stats
.crlf
)
272 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
) {
273 if (crlf_action
== CRLF_GUESS
) {
274 /* If we have any CR or CRLF line endings, we do not touch it */
275 /* This is the new safer autocrlf-handling */
276 if (stats
.cr
> 0 || stats
.crlf
> 0)
280 /* If we have any bare CR characters, we're not going to touch it */
281 if (stats
.cr
!= stats
.crlf
)
284 if (is_binary(len
, &stats
))
288 /* are we "faking" in place editing ? */
290 to_free
= strbuf_detach(buf
, NULL
);
292 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
294 const char *nl
= memchr(src
, '\n', len
);
297 if (nl
> src
&& nl
[-1] == '\r') {
298 strbuf_add(buf
, src
, nl
+ 1 - src
);
300 strbuf_add(buf
, src
, nl
- src
);
301 strbuf_addstr(buf
, "\r\n");
306 strbuf_add(buf
, src
, len
);
312 struct filter_params
{
320 static int filter_buffer_or_fd(int in
, int out
, void *data
)
323 * Spawn cmd and feed the buffer contents through its stdin.
325 struct child_process child_process
= CHILD_PROCESS_INIT
;
326 struct filter_params
*params
= (struct filter_params
*)data
;
327 int write_err
, status
;
328 const char *argv
[] = { NULL
, NULL
};
330 /* apply % substitution to cmd */
331 struct strbuf cmd
= STRBUF_INIT
;
332 struct strbuf path
= STRBUF_INIT
;
333 struct strbuf_expand_dict_entry dict
[] = {
338 /* quote the path to preserve spaces, etc. */
339 sq_quote_buf(&path
, params
->path
);
340 dict
[0].value
= path
.buf
;
342 /* expand all %f with the quoted path */
343 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
344 strbuf_release(&path
);
348 child_process
.argv
= argv
;
349 child_process
.use_shell
= 1;
350 child_process
.in
= -1;
351 child_process
.out
= out
;
353 if (start_command(&child_process
))
354 return error("cannot fork to run external filter %s", params
->cmd
);
356 sigchain_push(SIGPIPE
, SIG_IGN
);
359 write_err
= (write_in_full(child_process
.in
,
360 params
->src
, params
->size
) < 0);
364 write_err
= copy_fd(params
->fd
, child_process
.in
);
365 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
369 if (close(child_process
.in
))
372 error("cannot feed the input to external filter %s", params
->cmd
);
374 sigchain_pop(SIGPIPE
);
376 status
= finish_command(&child_process
);
378 error("external filter %s failed %d", params
->cmd
, status
);
380 strbuf_release(&cmd
);
381 return (write_err
|| status
);
384 static int apply_filter(const char *path
, const char *src
, size_t len
, int fd
,
385 struct strbuf
*dst
, const char *cmd
)
388 * Create a pipeline to have the command filter the buffer's
391 * (child --> cmd) --> us
394 struct strbuf nbuf
= STRBUF_INIT
;
396 struct filter_params params
;
404 memset(&async
, 0, sizeof(async
));
405 async
.proc
= filter_buffer_or_fd
;
406 async
.data
= ¶ms
;
415 if (start_async(&async
))
416 return 0; /* error was already reported */
418 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
419 error("read from external filter %s failed", cmd
);
422 if (close(async
.out
)) {
423 error("read from external filter %s failed", cmd
);
426 if (finish_async(&async
)) {
427 error("external filter %s failed", cmd
);
432 strbuf_swap(dst
, &nbuf
);
434 strbuf_release(&nbuf
);
438 static struct convert_driver
{
440 struct convert_driver
*next
;
444 } *user_convert
, **user_convert_tail
;
446 static int read_convert_config(const char *var
, const char *value
, void *cb
)
448 const char *key
, *name
;
450 struct convert_driver
*drv
;
453 * External conversion drivers are configured using
454 * "filter.<name>.variable".
456 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
458 for (drv
= user_convert
; drv
; drv
= drv
->next
)
459 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
462 drv
= xcalloc(1, sizeof(struct convert_driver
));
463 drv
->name
= xmemdupz(name
, namelen
);
464 *user_convert_tail
= drv
;
465 user_convert_tail
= &(drv
->next
);
469 * filter.<name>.smudge and filter.<name>.clean specifies
474 * The command-line will not be interpolated in any way.
477 if (!strcmp("smudge", key
))
478 return git_config_string(&drv
->smudge
, var
, value
);
480 if (!strcmp("clean", key
))
481 return git_config_string(&drv
->clean
, var
, value
);
483 if (!strcmp("required", key
)) {
484 drv
->required
= git_config_bool(var
, value
);
491 static int count_ident(const char *cp
, unsigned long size
)
494 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
506 if (memcmp("Id", cp
, 2))
517 * "$Id: ... "; scan up to the closing dollar sign and discard.
533 static int ident_to_git(const char *path
, const char *src
, size_t len
,
534 struct strbuf
*buf
, int ident
)
538 if (!ident
|| (src
&& !count_ident(src
, len
)))
544 /* only grow if not in place */
545 if (strbuf_avail(buf
) + buf
->len
< len
)
546 strbuf_grow(buf
, len
- buf
->len
);
549 dollar
= memchr(src
, '$', len
);
552 memmove(dst
, src
, dollar
+ 1 - src
);
553 dst
+= dollar
+ 1 - src
;
554 len
-= dollar
+ 1 - src
;
557 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
558 dollar
= memchr(src
+ 3, '$', len
- 3);
561 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
562 /* Line break before the next dollar. */
566 memcpy(dst
, "Id$", 3);
568 len
-= dollar
+ 1 - src
;
572 memmove(dst
, src
, len
);
573 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
577 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
578 struct strbuf
*buf
, int ident
)
580 unsigned char sha1
[20];
581 char *to_free
= NULL
, *dollar
, *spc
;
587 cnt
= count_ident(src
, len
);
591 /* are we "faking" in place editing ? */
593 to_free
= strbuf_detach(buf
, NULL
);
594 hash_sha1_file(src
, len
, "blob", sha1
);
596 strbuf_grow(buf
, len
+ cnt
* 43);
598 /* step 1: run to the next '$' */
599 dollar
= memchr(src
, '$', len
);
602 strbuf_add(buf
, src
, dollar
+ 1 - src
);
603 len
-= dollar
+ 1 - src
;
606 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
607 if (len
< 3 || memcmp("Id", src
, 2))
610 /* step 3: skip over Id$ or Id:xxxxx$ */
614 } else if (src
[2] == ':') {
616 * It's possible that an expanded Id has crept its way into the
617 * repository, we cope with that by stripping the expansion out.
618 * This is probably not a good idea, since it will cause changes
619 * on checkout, which won't go away by stash, but let's keep it
622 dollar
= memchr(src
+ 3, '$', len
- 3);
624 /* incomplete keyword, no more '$', so just quit the loop */
628 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
629 /* Line break before the next dollar. */
633 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
634 if (spc
&& spc
< dollar
-1) {
635 /* There are spaces in unexpected places.
636 * This is probably an id from some other
637 * versioning system. Keep it for now.
642 len
-= dollar
+ 1 - src
;
645 /* it wasn't a "Id$" or "Id:xxxx$" */
649 /* step 4: substitute */
650 strbuf_addstr(buf
, "Id: ");
651 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
652 strbuf_addstr(buf
, " $");
654 strbuf_add(buf
, src
, len
);
660 static enum crlf_action
git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
662 const char *value
= check
->value
;
664 if (ATTR_TRUE(value
))
666 else if (ATTR_FALSE(value
))
668 else if (ATTR_UNSET(value
))
670 else if (!strcmp(value
, "input"))
672 else if (!strcmp(value
, "auto"))
677 static enum eol
git_path_check_eol(const char *path
, struct git_attr_check
*check
)
679 const char *value
= check
->value
;
681 if (ATTR_UNSET(value
))
683 else if (!strcmp(value
, "lf"))
685 else if (!strcmp(value
, "crlf"))
690 static struct convert_driver
*git_path_check_convert(const char *path
,
691 struct git_attr_check
*check
)
693 const char *value
= check
->value
;
694 struct convert_driver
*drv
;
696 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
698 for (drv
= user_convert
; drv
; drv
= drv
->next
)
699 if (!strcmp(value
, drv
->name
))
704 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
706 const char *value
= check
->value
;
708 return !!ATTR_TRUE(value
);
711 static enum crlf_action
input_crlf_action(enum crlf_action text_attr
, enum eol eol_attr
)
713 if (text_attr
== CRLF_BINARY
)
715 if (eol_attr
== EOL_LF
)
717 if (eol_attr
== EOL_CRLF
)
723 struct convert_driver
*drv
;
724 enum crlf_action crlf_action
;
729 static const char *conv_attr_name
[] = {
730 "crlf", "ident", "filter", "eol", "text",
732 #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
734 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
737 static struct git_attr_check ccheck
[NUM_CONV_ATTRS
];
739 if (!ccheck
[0].attr
) {
740 for (i
= 0; i
< NUM_CONV_ATTRS
; i
++)
741 ccheck
[i
].attr
= git_attr(conv_attr_name
[i
]);
742 user_convert_tail
= &user_convert
;
743 git_config(read_convert_config
, NULL
);
746 if (!git_check_attr(path
, NUM_CONV_ATTRS
, ccheck
)) {
747 ca
->crlf_action
= git_path_check_crlf(path
, ccheck
+ 4);
748 if (ca
->crlf_action
== CRLF_GUESS
)
749 ca
->crlf_action
= git_path_check_crlf(path
, ccheck
+ 0);
750 ca
->ident
= git_path_check_ident(path
, ccheck
+ 1);
751 ca
->drv
= git_path_check_convert(path
, ccheck
+ 2);
752 ca
->eol_attr
= git_path_check_eol(path
, ccheck
+ 3);
755 ca
->crlf_action
= CRLF_GUESS
;
756 ca
->eol_attr
= EOL_UNSET
;
761 int would_convert_to_git_filter_fd(const char *path
)
763 struct conv_attrs ca
;
765 convert_attrs(&ca
, path
);
770 * Apply a filter to an fd only if the filter is required to succeed.
771 * We must die if the filter fails, because the original data before
772 * filtering is not available.
774 if (!ca
.drv
->required
)
777 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
->clean
);
780 int convert_to_git(const char *path
, const char *src
, size_t len
,
781 struct strbuf
*dst
, enum safe_crlf checksafe
)
784 const char *filter
= NULL
;
786 struct conv_attrs ca
;
788 convert_attrs(&ca
, path
);
790 filter
= ca
.drv
->clean
;
791 required
= ca
.drv
->required
;
794 ret
|= apply_filter(path
, src
, len
, -1, dst
, filter
);
795 if (!ret
&& required
)
796 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
802 ca
.crlf_action
= input_crlf_action(ca
.crlf_action
, ca
.eol_attr
);
803 ret
|= crlf_to_git(path
, src
, len
, dst
, ca
.crlf_action
, checksafe
);
808 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
811 void convert_to_git_filter_fd(const char *path
, int fd
, struct strbuf
*dst
,
812 enum safe_crlf checksafe
)
814 struct conv_attrs ca
;
815 convert_attrs(&ca
, path
);
818 assert(ca
.drv
->clean
);
820 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
->clean
))
821 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
823 ca
.crlf_action
= input_crlf_action(ca
.crlf_action
, ca
.eol_attr
);
824 crlf_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, checksafe
);
825 ident_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.ident
);
828 static int convert_to_working_tree_internal(const char *path
, const char *src
,
829 size_t len
, struct strbuf
*dst
,
832 int ret
= 0, ret_filter
= 0;
833 const char *filter
= NULL
;
835 struct conv_attrs ca
;
837 convert_attrs(&ca
, path
);
839 filter
= ca
.drv
->smudge
;
840 required
= ca
.drv
->required
;
843 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
849 * CRLF conversion can be skipped if normalizing, unless there
850 * is a smudge filter. The filter might expect CRLFs.
852 if (filter
|| !normalizing
) {
853 ca
.crlf_action
= input_crlf_action(ca
.crlf_action
, ca
.eol_attr
);
854 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
861 ret_filter
= apply_filter(path
, src
, len
, -1, dst
, filter
);
862 if (!ret_filter
&& required
)
863 die("%s: smudge filter %s failed", path
, ca
.drv
->name
);
865 return ret
| ret_filter
;
868 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
870 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0);
873 int renormalize_buffer(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
875 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1);
880 return ret
| convert_to_git(path
, src
, len
, dst
, SAFE_CRLF_FALSE
);
883 /*****************************************************************
885 * Streaming conversion support
887 *****************************************************************/
889 typedef int (*filter_fn
)(struct stream_filter
*,
890 const char *input
, size_t *isize_p
,
891 char *output
, size_t *osize_p
);
892 typedef void (*free_fn
)(struct stream_filter
*);
894 struct stream_filter_vtbl
{
899 struct stream_filter
{
900 struct stream_filter_vtbl
*vtbl
;
903 static int null_filter_fn(struct stream_filter
*filter
,
904 const char *input
, size_t *isize_p
,
905 char *output
, size_t *osize_p
)
910 return 0; /* we do not keep any states */
912 if (*osize_p
< count
)
915 memmove(output
, input
, count
);
922 static void null_free_fn(struct stream_filter
*filter
)
924 ; /* nothing -- null instances are shared */
927 static struct stream_filter_vtbl null_vtbl
= {
932 static struct stream_filter null_filter_singleton
= {
936 int is_null_stream_filter(struct stream_filter
*filter
)
938 return filter
== &null_filter_singleton
;
946 struct lf_to_crlf_filter
{
947 struct stream_filter filter
;
952 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
953 const char *input
, size_t *isize_p
,
954 char *output
, size_t *osize_p
)
957 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
960 * We may be holding onto the CR to see if it is followed by a
961 * LF, in which case we would need to go to the main loop.
962 * Otherwise, just emit it to the output stream.
964 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
965 output
[o
++] = lf_to_crlf
->held
;
966 lf_to_crlf
->has_held
= 0;
969 /* We are told to drain */
976 if (count
|| lf_to_crlf
->has_held
) {
980 if (lf_to_crlf
->has_held
) {
982 lf_to_crlf
->has_held
= 0;
985 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
992 * Previous round saw CR and it is not followed
993 * by a LF; emit the CR before processing the
1000 * We may have consumed the last output slot,
1001 * in which case we need to break out of this
1002 * loop; hold the current character before
1005 if (*osize_p
<= o
) {
1006 lf_to_crlf
->has_held
= 1;
1007 lf_to_crlf
->held
= ch
;
1008 continue; /* break but increment i */
1023 if (!lf_to_crlf
->has_held
&& was_cr
) {
1024 lf_to_crlf
->has_held
= 1;
1025 lf_to_crlf
->held
= '\r';
1031 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1036 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1037 lf_to_crlf_filter_fn
,
1041 static struct stream_filter
*lf_to_crlf_filter(void)
1043 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1045 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1046 return (struct stream_filter
*)lf_to_crlf
;
1052 #define FILTER_BUFFER 1024
1053 struct cascade_filter
{
1054 struct stream_filter filter
;
1055 struct stream_filter
*one
;
1056 struct stream_filter
*two
;
1057 char buf
[FILTER_BUFFER
];
1061 static int cascade_filter_fn(struct stream_filter
*filter
,
1062 const char *input
, size_t *isize_p
,
1063 char *output
, size_t *osize_p
)
1065 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1067 size_t sz
= *osize_p
;
1068 size_t to_feed
, remaining
;
1071 * input -- (one) --> buf -- (two) --> output
1073 while (filled
< sz
) {
1074 remaining
= sz
- filled
;
1076 /* do we already have something to feed two with? */
1077 if (cas
->ptr
< cas
->end
) {
1078 to_feed
= cas
->end
- cas
->ptr
;
1079 if (stream_filter(cas
->two
,
1080 cas
->buf
+ cas
->ptr
, &to_feed
,
1081 output
+ filled
, &remaining
))
1083 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1084 filled
= sz
- remaining
;
1088 /* feed one from upstream and have it emit into our buffer */
1089 to_feed
= input
? *isize_p
: 0;
1090 if (input
&& !to_feed
)
1092 remaining
= sizeof(cas
->buf
);
1093 if (stream_filter(cas
->one
,
1095 cas
->buf
, &remaining
))
1097 cas
->end
= sizeof(cas
->buf
) - remaining
;
1100 size_t fed
= *isize_p
- to_feed
;
1105 /* do we know that we drained one completely? */
1106 if (input
|| cas
->end
)
1109 /* tell two to drain; we have nothing more to give it */
1111 remaining
= sz
- filled
;
1112 if (stream_filter(cas
->two
,
1114 output
+ filled
, &remaining
))
1116 if (remaining
== (sz
- filled
))
1117 break; /* completely drained two */
1118 filled
= sz
- remaining
;
1124 static void cascade_free_fn(struct stream_filter
*filter
)
1126 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1127 free_stream_filter(cas
->one
);
1128 free_stream_filter(cas
->two
);
1132 static struct stream_filter_vtbl cascade_vtbl
= {
1137 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1138 struct stream_filter
*two
)
1140 struct cascade_filter
*cascade
;
1142 if (!one
|| is_null_stream_filter(one
))
1144 if (!two
|| is_null_stream_filter(two
))
1147 cascade
= xmalloc(sizeof(*cascade
));
1150 cascade
->end
= cascade
->ptr
= 0;
1151 cascade
->filter
.vtbl
= &cascade_vtbl
;
1152 return (struct stream_filter
*)cascade
;
1158 #define IDENT_DRAINING (-1)
1159 #define IDENT_SKIPPING (-2)
1160 struct ident_filter
{
1161 struct stream_filter filter
;
1164 char ident
[45]; /* ": x40 $" */
1167 static int is_foreign_ident(const char *str
)
1171 if (!skip_prefix(str
, "$Id: ", &str
))
1173 for (i
= 0; str
[i
]; i
++) {
1174 if (isspace(str
[i
]) && str
[i
+1] != '$')
1180 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1182 size_t to_drain
= ident
->left
.len
;
1184 if (*osize_p
< to_drain
)
1185 to_drain
= *osize_p
;
1187 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1188 strbuf_remove(&ident
->left
, 0, to_drain
);
1189 *output_p
+= to_drain
;
1190 *osize_p
-= to_drain
;
1192 if (!ident
->left
.len
)
1196 static int ident_filter_fn(struct stream_filter
*filter
,
1197 const char *input
, size_t *isize_p
,
1198 char *output
, size_t *osize_p
)
1200 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1201 static const char head
[] = "$Id";
1204 /* drain upon eof */
1205 switch (ident
->state
) {
1207 strbuf_add(&ident
->left
, head
, ident
->state
);
1208 case IDENT_SKIPPING
:
1210 case IDENT_DRAINING
:
1211 ident_drain(ident
, &output
, osize_p
);
1216 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1219 if (ident
->state
== IDENT_DRAINING
) {
1220 ident_drain(ident
, &output
, osize_p
);
1229 if (ident
->state
== IDENT_SKIPPING
) {
1231 * Skipping until '$' or LF, but keeping them
1232 * in case it is a foreign ident.
1234 strbuf_addch(&ident
->left
, ch
);
1235 if (ch
!= '\n' && ch
!= '$')
1237 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1238 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1239 strbuf_addstr(&ident
->left
, ident
->ident
);
1241 ident
->state
= IDENT_DRAINING
;
1245 if (ident
->state
< sizeof(head
) &&
1246 head
[ident
->state
] == ch
) {
1252 strbuf_add(&ident
->left
, head
, ident
->state
);
1253 if (ident
->state
== sizeof(head
) - 1) {
1254 if (ch
!= ':' && ch
!= '$') {
1255 strbuf_addch(&ident
->left
, ch
);
1261 strbuf_addch(&ident
->left
, ch
);
1262 ident
->state
= IDENT_SKIPPING
;
1264 strbuf_addstr(&ident
->left
, ident
->ident
);
1265 ident
->state
= IDENT_DRAINING
;
1270 strbuf_addch(&ident
->left
, ch
);
1271 ident
->state
= IDENT_DRAINING
;
1276 static void ident_free_fn(struct stream_filter
*filter
)
1278 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1279 strbuf_release(&ident
->left
);
1283 static struct stream_filter_vtbl ident_vtbl
= {
1288 static struct stream_filter
*ident_filter(const unsigned char *sha1
)
1290 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1292 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1293 ": %s $", sha1_to_hex(sha1
));
1294 strbuf_init(&ident
->left
, 0);
1295 ident
->filter
.vtbl
= &ident_vtbl
;
1297 return (struct stream_filter
*)ident
;
1301 * Return an appropriately constructed filter for the path, or NULL if
1302 * the contents cannot be filtered without reading the whole thing
1305 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1306 * large binary blob you would want us not to slurp into the memory!
1308 struct stream_filter
*get_stream_filter(const char *path
, const unsigned char *sha1
)
1310 struct conv_attrs ca
;
1311 enum crlf_action crlf_action
;
1312 struct stream_filter
*filter
= NULL
;
1314 convert_attrs(&ca
, path
);
1316 if (ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->clean
))
1320 filter
= ident_filter(sha1
);
1322 crlf_action
= input_crlf_action(ca
.crlf_action
, ca
.eol_attr
);
1324 if ((crlf_action
== CRLF_BINARY
) || (crlf_action
== CRLF_INPUT
) ||
1325 (crlf_action
== CRLF_GUESS
&& auto_crlf
== AUTO_CRLF_FALSE
))
1326 filter
= cascade_filter(filter
, &null_filter_singleton
);
1328 else if (output_eol(crlf_action
) == EOL_CRLF
&&
1329 !(crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
))
1330 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1335 void free_stream_filter(struct stream_filter
*filter
)
1337 filter
->vtbl
->free(filter
);
1340 int stream_filter(struct stream_filter
*filter
,
1341 const char *input
, size_t *isize_p
,
1342 char *output
, size_t *osize_p
)
1344 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);