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
) || !len
)
202 gather_stats(src
, len
, &stats
);
204 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
) {
206 * We're currently not going to even try to convert stuff
207 * that has bare CR characters. Does anybody do that crazy
210 if (stats
.cr
!= stats
.crlf
)
214 * And add some heuristics for binary vs text, of course...
216 if (is_binary(len
, &stats
))
219 if (crlf_action
== CRLF_GUESS
) {
221 * If the file in the index has any CR in it, do not convert.
222 * This is the new safer autocrlf handling.
224 if (has_cr_in_index(path
))
229 check_safe_crlf(path
, crlf_action
, &stats
, checksafe
);
231 /* Optimization: No CR? Nothing to convert, regardless. */
235 /* only grow if not in place */
236 if (strbuf_avail(buf
) + buf
->len
< len
)
237 strbuf_grow(buf
, len
- buf
->len
);
239 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
) {
241 * If we guessed, we already know we rejected a file with
242 * lone CR, and we can strip a CR without looking at what
246 unsigned char c
= *src
++;
252 unsigned char c
= *src
++;
253 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
257 strbuf_setlen(buf
, dst
- buf
->buf
);
261 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
262 struct strbuf
*buf
, enum crlf_action crlf_action
)
264 char *to_free
= NULL
;
265 struct text_stat stats
;
267 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
270 gather_stats(src
, len
, &stats
);
272 /* No LF? Nothing to convert, regardless. */
276 /* Was it already in CRLF format? */
277 if (stats
.lf
== stats
.crlf
)
280 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
) {
281 if (crlf_action
== CRLF_GUESS
) {
282 /* If we have any CR or CRLF line endings, we do not touch it */
283 /* This is the new safer autocrlf-handling */
284 if (stats
.cr
> 0 || stats
.crlf
> 0)
288 /* If we have any bare CR characters, we're not going to touch it */
289 if (stats
.cr
!= stats
.crlf
)
292 if (is_binary(len
, &stats
))
296 /* are we "faking" in place editing ? */
298 to_free
= strbuf_detach(buf
, NULL
);
300 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
302 const char *nl
= memchr(src
, '\n', len
);
305 if (nl
> src
&& nl
[-1] == '\r') {
306 strbuf_add(buf
, src
, nl
+ 1 - src
);
308 strbuf_add(buf
, src
, nl
- src
);
309 strbuf_addstr(buf
, "\r\n");
314 strbuf_add(buf
, src
, len
);
320 struct filter_params
{
327 static int filter_buffer(int in
, int out
, void *data
)
330 * Spawn cmd and feed the buffer contents through its stdin.
332 struct child_process child_process
;
333 struct filter_params
*params
= (struct filter_params
*)data
;
334 int write_err
, status
;
335 const char *argv
[] = { NULL
, NULL
};
337 /* apply % substitution to cmd */
338 struct strbuf cmd
= STRBUF_INIT
;
339 struct strbuf path
= STRBUF_INIT
;
340 struct strbuf_expand_dict_entry dict
[] = {
345 /* quote the path to preserve spaces, etc. */
346 sq_quote_buf(&path
, params
->path
);
347 dict
[0].value
= path
.buf
;
349 /* expand all %f with the quoted path */
350 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
351 strbuf_release(&path
);
355 memset(&child_process
, 0, sizeof(child_process
));
356 child_process
.argv
= argv
;
357 child_process
.use_shell
= 1;
358 child_process
.in
= -1;
359 child_process
.out
= out
;
361 if (start_command(&child_process
))
362 return error("cannot fork to run external filter %s", params
->cmd
);
364 sigchain_push(SIGPIPE
, SIG_IGN
);
366 write_err
= (write_in_full(child_process
.in
, params
->src
, params
->size
) < 0);
367 if (close(child_process
.in
))
370 error("cannot feed the input to external filter %s", params
->cmd
);
372 sigchain_pop(SIGPIPE
);
374 status
= finish_command(&child_process
);
376 error("external filter %s failed %d", params
->cmd
, status
);
378 strbuf_release(&cmd
);
379 return (write_err
|| status
);
382 static int apply_filter(const char *path
, const char *src
, size_t len
,
383 struct strbuf
*dst
, const char *cmd
)
386 * Create a pipeline to have the command filter the buffer's
389 * (child --> cmd) --> us
392 struct strbuf nbuf
= STRBUF_INIT
;
394 struct filter_params params
;
399 memset(&async
, 0, sizeof(async
));
400 async
.proc
= filter_buffer
;
401 async
.data
= ¶ms
;
409 if (start_async(&async
))
410 return 0; /* error was already reported */
412 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
413 error("read from external filter %s failed", cmd
);
416 if (close(async
.out
)) {
417 error("read from external filter %s failed", cmd
);
420 if (finish_async(&async
)) {
421 error("external filter %s failed", cmd
);
426 strbuf_swap(dst
, &nbuf
);
428 strbuf_release(&nbuf
);
432 static struct convert_driver
{
434 struct convert_driver
*next
;
437 } *user_convert
, **user_convert_tail
;
439 static int read_convert_config(const char *var
, const char *value
, void *cb
)
441 const char *ep
, *name
;
443 struct convert_driver
*drv
;
446 * External conversion drivers are configured using
447 * "filter.<name>.variable".
449 if (prefixcmp(var
, "filter.") || (ep
= strrchr(var
, '.')) == var
+ 6)
453 for (drv
= user_convert
; drv
; drv
= drv
->next
)
454 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
457 drv
= xcalloc(1, sizeof(struct convert_driver
));
458 drv
->name
= xmemdupz(name
, namelen
);
459 *user_convert_tail
= drv
;
460 user_convert_tail
= &(drv
->next
);
466 * filter.<name>.smudge and filter.<name>.clean specifies
471 * The command-line will not be interpolated in any way.
474 if (!strcmp("smudge", ep
))
475 return git_config_string(&drv
->smudge
, var
, value
);
477 if (!strcmp("clean", ep
))
478 return git_config_string(&drv
->clean
, var
, value
);
483 static int count_ident(const char *cp
, unsigned long size
)
486 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
498 if (memcmp("Id", cp
, 2))
509 * "$Id: ... "; scan up to the closing dollar sign and discard.
525 static int ident_to_git(const char *path
, const char *src
, size_t len
,
526 struct strbuf
*buf
, int ident
)
530 if (!ident
|| !count_ident(src
, len
))
533 /* only grow if not in place */
534 if (strbuf_avail(buf
) + buf
->len
< len
)
535 strbuf_grow(buf
, len
- buf
->len
);
538 dollar
= memchr(src
, '$', len
);
541 memmove(dst
, src
, dollar
+ 1 - src
);
542 dst
+= dollar
+ 1 - src
;
543 len
-= dollar
+ 1 - src
;
546 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
547 dollar
= memchr(src
+ 3, '$', len
- 3);
550 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
551 /* Line break before the next dollar. */
555 memcpy(dst
, "Id$", 3);
557 len
-= dollar
+ 1 - src
;
561 memmove(dst
, src
, len
);
562 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
566 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
567 struct strbuf
*buf
, int ident
)
569 unsigned char sha1
[20];
570 char *to_free
= NULL
, *dollar
, *spc
;
576 cnt
= count_ident(src
, len
);
580 /* are we "faking" in place editing ? */
582 to_free
= strbuf_detach(buf
, NULL
);
583 hash_sha1_file(src
, len
, "blob", sha1
);
585 strbuf_grow(buf
, len
+ cnt
* 43);
587 /* step 1: run to the next '$' */
588 dollar
= memchr(src
, '$', len
);
591 strbuf_add(buf
, src
, dollar
+ 1 - src
);
592 len
-= dollar
+ 1 - src
;
595 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
596 if (len
< 3 || memcmp("Id", src
, 2))
599 /* step 3: skip over Id$ or Id:xxxxx$ */
603 } else if (src
[2] == ':') {
605 * It's possible that an expanded Id has crept its way into the
606 * repository, we cope with that by stripping the expansion out.
607 * This is probably not a good idea, since it will cause changes
608 * on checkout, which won't go away by stash, but let's keep it
611 dollar
= memchr(src
+ 3, '$', len
- 3);
613 /* incomplete keyword, no more '$', so just quit the loop */
617 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
618 /* Line break before the next dollar. */
622 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
623 if (spc
&& spc
< dollar
-1) {
624 /* There are spaces in unexpected places.
625 * This is probably an id from some other
626 * versioning system. Keep it for now.
631 len
-= dollar
+ 1 - src
;
634 /* it wasn't a "Id$" or "Id:xxxx$" */
638 /* step 4: substitute */
639 strbuf_addstr(buf
, "Id: ");
640 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
641 strbuf_addstr(buf
, " $");
643 strbuf_add(buf
, src
, len
);
649 static enum crlf_action
git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
651 const char *value
= check
->value
;
653 if (ATTR_TRUE(value
))
655 else if (ATTR_FALSE(value
))
657 else if (ATTR_UNSET(value
))
659 else if (!strcmp(value
, "input"))
661 else if (!strcmp(value
, "auto"))
666 static enum eol
git_path_check_eol(const char *path
, struct git_attr_check
*check
)
668 const char *value
= check
->value
;
670 if (ATTR_UNSET(value
))
672 else if (!strcmp(value
, "lf"))
674 else if (!strcmp(value
, "crlf"))
679 static struct convert_driver
*git_path_check_convert(const char *path
,
680 struct git_attr_check
*check
)
682 const char *value
= check
->value
;
683 struct convert_driver
*drv
;
685 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
687 for (drv
= user_convert
; drv
; drv
= drv
->next
)
688 if (!strcmp(value
, drv
->name
))
693 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
695 const char *value
= check
->value
;
697 return !!ATTR_TRUE(value
);
700 static enum crlf_action
input_crlf_action(enum crlf_action text_attr
, enum eol eol_attr
)
702 if (text_attr
== CRLF_BINARY
)
704 if (eol_attr
== EOL_LF
)
706 if (eol_attr
== EOL_CRLF
)
712 struct convert_driver
*drv
;
713 enum crlf_action crlf_action
;
718 static const char *conv_attr_name
[] = {
719 "crlf", "ident", "filter", "eol", "text",
721 #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
723 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
726 static struct git_attr_check ccheck
[NUM_CONV_ATTRS
];
728 if (!ccheck
[0].attr
) {
729 for (i
= 0; i
< NUM_CONV_ATTRS
; i
++)
730 ccheck
[i
].attr
= git_attr(conv_attr_name
[i
]);
731 user_convert_tail
= &user_convert
;
732 git_config(read_convert_config
, NULL
);
735 if (!git_check_attr(path
, NUM_CONV_ATTRS
, ccheck
)) {
736 ca
->crlf_action
= git_path_check_crlf(path
, ccheck
+ 4);
737 if (ca
->crlf_action
== CRLF_GUESS
)
738 ca
->crlf_action
= git_path_check_crlf(path
, ccheck
+ 0);
739 ca
->ident
= git_path_check_ident(path
, ccheck
+ 1);
740 ca
->drv
= git_path_check_convert(path
, ccheck
+ 2);
741 ca
->eol_attr
= git_path_check_eol(path
, ccheck
+ 3);
744 ca
->crlf_action
= CRLF_GUESS
;
745 ca
->eol_attr
= EOL_UNSET
;
750 int convert_to_git(const char *path
, const char *src
, size_t len
,
751 struct strbuf
*dst
, enum safe_crlf checksafe
)
754 const char *filter
= NULL
;
755 struct conv_attrs ca
;
757 convert_attrs(&ca
, path
);
759 filter
= ca
.drv
->clean
;
761 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
766 ca
.crlf_action
= input_crlf_action(ca
.crlf_action
, ca
.eol_attr
);
767 ret
|= crlf_to_git(path
, src
, len
, dst
, ca
.crlf_action
, checksafe
);
772 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
775 static int convert_to_working_tree_internal(const char *path
, const char *src
,
776 size_t len
, struct strbuf
*dst
,
780 const char *filter
= NULL
;
781 struct conv_attrs ca
;
783 convert_attrs(&ca
, path
);
785 filter
= ca
.drv
->smudge
;
787 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
793 * CRLF conversion can be skipped if normalizing, unless there
794 * is a smudge filter. The filter might expect CRLFs.
796 if (filter
|| !normalizing
) {
797 ca
.crlf_action
= input_crlf_action(ca
.crlf_action
, ca
.eol_attr
);
798 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
804 return ret
| apply_filter(path
, src
, len
, dst
, filter
);
807 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
809 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0);
812 int renormalize_buffer(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
814 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1);
819 return ret
| convert_to_git(path
, src
, len
, dst
, SAFE_CRLF_FALSE
);
822 /*****************************************************************
824 * Streaming converison support
826 *****************************************************************/
828 typedef int (*filter_fn
)(struct stream_filter
*,
829 const char *input
, size_t *isize_p
,
830 char *output
, size_t *osize_p
);
831 typedef void (*free_fn
)(struct stream_filter
*);
833 struct stream_filter_vtbl
{
838 struct stream_filter
{
839 struct stream_filter_vtbl
*vtbl
;
842 static int null_filter_fn(struct stream_filter
*filter
,
843 const char *input
, size_t *isize_p
,
844 char *output
, size_t *osize_p
)
849 return 0; /* we do not keep any states */
851 if (*osize_p
< count
)
854 memmove(output
, input
, count
);
861 static void null_free_fn(struct stream_filter
*filter
)
863 ; /* nothing -- null instances are shared */
866 static struct stream_filter_vtbl null_vtbl
= {
871 static struct stream_filter null_filter_singleton
= {
875 int is_null_stream_filter(struct stream_filter
*filter
)
877 return filter
== &null_filter_singleton
;
885 struct lf_to_crlf_filter
{
886 struct stream_filter filter
;
891 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
892 const char *input
, size_t *isize_p
,
893 char *output
, size_t *osize_p
)
896 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
899 * We may be holding onto the CR to see if it is followed by a
900 * LF, in which case we would need to go to the main loop.
901 * Otherwise, just emit it to the output stream.
903 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
904 output
[o
++] = lf_to_crlf
->held
;
905 lf_to_crlf
->has_held
= 0;
908 /* We are told to drain */
915 if (count
|| lf_to_crlf
->has_held
) {
919 if (lf_to_crlf
->has_held
) {
921 lf_to_crlf
->has_held
= 0;
924 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
931 * Previous round saw CR and it is not followed
932 * by a LF; emit the CR before processing the
939 * We may have consumed the last output slot,
940 * in which case we need to break out of this
941 * loop; hold the current character before
945 lf_to_crlf
->has_held
= 1;
946 lf_to_crlf
->held
= ch
;
947 continue; /* break but increment i */
962 if (!lf_to_crlf
->has_held
&& was_cr
) {
963 lf_to_crlf
->has_held
= 1;
964 lf_to_crlf
->held
= '\r';
970 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
975 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
976 lf_to_crlf_filter_fn
,
980 static struct stream_filter
*lf_to_crlf_filter(void)
982 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
984 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
985 return (struct stream_filter
*)lf_to_crlf
;
991 #define FILTER_BUFFER 1024
992 struct cascade_filter
{
993 struct stream_filter filter
;
994 struct stream_filter
*one
;
995 struct stream_filter
*two
;
996 char buf
[FILTER_BUFFER
];
1000 static int cascade_filter_fn(struct stream_filter
*filter
,
1001 const char *input
, size_t *isize_p
,
1002 char *output
, size_t *osize_p
)
1004 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1006 size_t sz
= *osize_p
;
1007 size_t to_feed
, remaining
;
1010 * input -- (one) --> buf -- (two) --> output
1012 while (filled
< sz
) {
1013 remaining
= sz
- filled
;
1015 /* do we already have something to feed two with? */
1016 if (cas
->ptr
< cas
->end
) {
1017 to_feed
= cas
->end
- cas
->ptr
;
1018 if (stream_filter(cas
->two
,
1019 cas
->buf
+ cas
->ptr
, &to_feed
,
1020 output
+ filled
, &remaining
))
1022 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1023 filled
= sz
- remaining
;
1027 /* feed one from upstream and have it emit into our buffer */
1028 to_feed
= input
? *isize_p
: 0;
1029 if (input
&& !to_feed
)
1031 remaining
= sizeof(cas
->buf
);
1032 if (stream_filter(cas
->one
,
1034 cas
->buf
, &remaining
))
1036 cas
->end
= sizeof(cas
->buf
) - remaining
;
1039 size_t fed
= *isize_p
- to_feed
;
1044 /* do we know that we drained one completely? */
1045 if (input
|| cas
->end
)
1048 /* tell two to drain; we have nothing more to give it */
1050 remaining
= sz
- filled
;
1051 if (stream_filter(cas
->two
,
1053 output
+ filled
, &remaining
))
1055 if (remaining
== (sz
- filled
))
1056 break; /* completely drained two */
1057 filled
= sz
- remaining
;
1063 static void cascade_free_fn(struct stream_filter
*filter
)
1065 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1066 free_stream_filter(cas
->one
);
1067 free_stream_filter(cas
->two
);
1071 static struct stream_filter_vtbl cascade_vtbl
= {
1076 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1077 struct stream_filter
*two
)
1079 struct cascade_filter
*cascade
;
1081 if (!one
|| is_null_stream_filter(one
))
1083 if (!two
|| is_null_stream_filter(two
))
1086 cascade
= xmalloc(sizeof(*cascade
));
1089 cascade
->end
= cascade
->ptr
= 0;
1090 cascade
->filter
.vtbl
= &cascade_vtbl
;
1091 return (struct stream_filter
*)cascade
;
1097 #define IDENT_DRAINING (-1)
1098 #define IDENT_SKIPPING (-2)
1099 struct ident_filter
{
1100 struct stream_filter filter
;
1103 char ident
[45]; /* ": x40 $" */
1106 static int is_foreign_ident(const char *str
)
1110 if (prefixcmp(str
, "$Id: "))
1112 for (i
= 5; str
[i
]; i
++) {
1113 if (isspace(str
[i
]) && str
[i
+1] != '$')
1119 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1121 size_t to_drain
= ident
->left
.len
;
1123 if (*osize_p
< to_drain
)
1124 to_drain
= *osize_p
;
1126 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1127 strbuf_remove(&ident
->left
, 0, to_drain
);
1128 *output_p
+= to_drain
;
1129 *osize_p
-= to_drain
;
1131 if (!ident
->left
.len
)
1135 static int ident_filter_fn(struct stream_filter
*filter
,
1136 const char *input
, size_t *isize_p
,
1137 char *output
, size_t *osize_p
)
1139 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1140 static const char head
[] = "$Id";
1143 /* drain upon eof */
1144 switch (ident
->state
) {
1146 strbuf_add(&ident
->left
, head
, ident
->state
);
1147 case IDENT_SKIPPING
:
1149 case IDENT_DRAINING
:
1150 ident_drain(ident
, &output
, osize_p
);
1155 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1158 if (ident
->state
== IDENT_DRAINING
) {
1159 ident_drain(ident
, &output
, osize_p
);
1168 if (ident
->state
== IDENT_SKIPPING
) {
1170 * Skipping until '$' or LF, but keeping them
1171 * in case it is a foreign ident.
1173 strbuf_addch(&ident
->left
, ch
);
1174 if (ch
!= '\n' && ch
!= '$')
1176 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1177 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1178 strbuf_addstr(&ident
->left
, ident
->ident
);
1180 ident
->state
= IDENT_DRAINING
;
1184 if (ident
->state
< sizeof(head
) &&
1185 head
[ident
->state
] == ch
) {
1191 strbuf_add(&ident
->left
, head
, ident
->state
);
1192 if (ident
->state
== sizeof(head
) - 1) {
1193 if (ch
!= ':' && ch
!= '$') {
1194 strbuf_addch(&ident
->left
, ch
);
1200 strbuf_addch(&ident
->left
, ch
);
1201 ident
->state
= IDENT_SKIPPING
;
1203 strbuf_addstr(&ident
->left
, ident
->ident
);
1204 ident
->state
= IDENT_DRAINING
;
1209 strbuf_addch(&ident
->left
, ch
);
1210 ident
->state
= IDENT_DRAINING
;
1215 static void ident_free_fn(struct stream_filter
*filter
)
1217 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1218 strbuf_release(&ident
->left
);
1222 static struct stream_filter_vtbl ident_vtbl
= {
1227 static struct stream_filter
*ident_filter(const unsigned char *sha1
)
1229 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1231 sprintf(ident
->ident
, ": %s $", sha1_to_hex(sha1
));
1232 strbuf_init(&ident
->left
, 0);
1233 ident
->filter
.vtbl
= &ident_vtbl
;
1235 return (struct stream_filter
*)ident
;
1239 * Return an appropriately constructed filter for the path, or NULL if
1240 * the contents cannot be filtered without reading the whole thing
1243 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1244 * large binary blob you would want us not to slurp into the memory!
1246 struct stream_filter
*get_stream_filter(const char *path
, const unsigned char *sha1
)
1248 struct conv_attrs ca
;
1249 enum crlf_action crlf_action
;
1250 struct stream_filter
*filter
= NULL
;
1252 convert_attrs(&ca
, path
);
1254 if (ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->clean
))
1258 filter
= ident_filter(sha1
);
1260 crlf_action
= input_crlf_action(ca
.crlf_action
, ca
.eol_attr
);
1262 if ((crlf_action
== CRLF_BINARY
) || (crlf_action
== CRLF_INPUT
) ||
1263 (crlf_action
== CRLF_GUESS
&& auto_crlf
== AUTO_CRLF_FALSE
))
1264 filter
= cascade_filter(filter
, &null_filter_singleton
);
1266 else if (output_eol(crlf_action
) == EOL_CRLF
&&
1267 !(crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_GUESS
))
1268 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1273 void free_stream_filter(struct stream_filter
*filter
)
1275 filter
->vtbl
->free(filter
);
1278 int stream_filter(struct stream_filter
*filter
,
1279 const char *input
, size_t *isize_p
,
1280 char *output
, size_t *osize_p
)
1282 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);