1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
5 #include "run-command.h"
9 #include "sub-process.h"
13 * convert.c - convert a file when checking it out and checking it in.
15 * This should use the pathname to decide on whether it wants to do some
16 * more interesting conversions (automatic gzip/unzip, general format
17 * conversions etc etc), but by default it just does automatic CRLF<->LF
18 * translation when the "text" attribute or "auto_crlf" option is set.
21 /* Stat bits: When BIN is set, the txt bits are unset */
22 #define CONVERT_STAT_BITS_TXT_LF 0x1
23 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
24 #define CONVERT_STAT_BITS_BIN 0x4
38 /* NUL, CR, LF and CRLF counts */
39 unsigned nul
, lonecr
, lonelf
, crlf
;
41 /* These are just approximations! */
42 unsigned printable
, nonprintable
;
45 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
49 memset(stats
, 0, sizeof(*stats
));
51 for (i
= 0; i
< size
; i
++) {
52 unsigned char c
= buf
[i
];
54 if (i
+1 < size
&& buf
[i
+1] == '\n') {
67 stats
->nonprintable
++;
70 /* BS, HT, ESC and FF */
71 case '\b': case '\t': case '\033': case '\014':
78 stats
->nonprintable
++;
85 /* If file ends with EOF then don't count this EOF as non-printable. */
86 if (size
>= 1 && buf
[size
-1] == '\032')
87 stats
->nonprintable
--;
91 * The same heuristics as diff.c::mmfile_is_binary()
92 * We treat files with bare CR as binary
94 static int convert_is_binary(unsigned long size
, const struct text_stat
*stats
)
100 if ((stats
->printable
>> 7) < stats
->nonprintable
)
105 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
107 struct text_stat stats
;
111 gather_stats(data
, size
, &stats
);
112 if (convert_is_binary(size
, &stats
))
113 ret
|= CONVERT_STAT_BITS_BIN
;
115 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
117 ret
|= CONVERT_STAT_BITS_TXT_LF
;
122 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
124 unsigned int convert_stats
= gather_convert_stats(data
, size
);
126 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
128 switch (convert_stats
) {
129 case CONVERT_STAT_BITS_TXT_LF
:
131 case CONVERT_STAT_BITS_TXT_CRLF
:
133 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
140 const char *get_cached_convert_stats_ascii(const struct index_state
*istate
,
145 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
146 ret
= gather_convert_stats_ascii(data
, sz
);
151 const char *get_wt_convert_stats_ascii(const char *path
)
153 const char *ret
= "";
154 struct strbuf sb
= STRBUF_INIT
;
155 if (strbuf_read_file(&sb
, path
, 0) >= 0)
156 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
161 static int text_eol_is_crlf(void)
163 if (auto_crlf
== AUTO_CRLF_TRUE
)
165 else if (auto_crlf
== AUTO_CRLF_INPUT
)
167 if (core_eol
== EOL_CRLF
)
169 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
174 static enum eol
output_eol(enum crlf_action crlf_action
)
176 switch (crlf_action
) {
181 case CRLF_TEXT_INPUT
:
186 case CRLF_AUTO_INPUT
:
191 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
193 warning("Illegal crlf_action %d\n", (int)crlf_action
);
197 static void check_global_conv_flags_eol(const char *path
, enum crlf_action crlf_action
,
198 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
201 if (old_stats
->crlf
&& !new_stats
->crlf
) {
203 * CRLFs would not be restored by checkout
205 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
206 die(_("CRLF would be replaced by LF in %s."), path
);
207 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
208 warning(_("CRLF will be replaced by LF in %s.\n"
209 "The file will have its original line"
210 " endings in your working directory."), path
);
211 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
213 * CRLFs would be added by checkout
215 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
216 die(_("LF would be replaced by CRLF in %s"), path
);
217 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
218 warning(_("LF will be replaced by CRLF in %s.\n"
219 "The file will have its original line"
220 " endings in your working directory."), path
);
224 static int has_crlf_in_index(const struct index_state
*istate
, const char *path
)
231 data
= read_blob_data_from_index(istate
, path
, &sz
);
235 crp
= memchr(data
, '\r', sz
);
237 unsigned int ret_stats
;
238 ret_stats
= gather_convert_stats(data
, sz
);
239 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
240 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
247 static int will_convert_lf_to_crlf(size_t len
, struct text_stat
*stats
,
248 enum crlf_action crlf_action
)
250 if (output_eol(crlf_action
) != EOL_CRLF
)
252 /* No "naked" LF? Nothing to convert, regardless. */
256 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
257 /* If we have any CR or CRLF line endings, we do not touch it */
258 /* This is the new safer autocrlf-handling */
259 if (stats
->lonecr
|| stats
->crlf
)
262 if (convert_is_binary(len
, stats
))
269 static int validate_encoding(const char *path
, const char *enc
,
270 const char *data
, size_t len
, int die_on_error
)
272 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
273 if (istarts_with(enc
, "UTF")) {
275 * Check for detectable errors in UTF encodings
277 if (has_prohibited_utf_bom(enc
, data
, len
)) {
278 const char *error_msg
= _(
279 "BOM is prohibited in '%s' if encoded as %s");
281 * This advice is shown for UTF-??BE and UTF-??LE encodings.
282 * We cut off the last two characters of the encoding name
283 * to generate the encoding name suitable for BOMs.
285 const char *advise_msg
= _(
286 "The file '%s' contains a byte order "
287 "mark (BOM). Please use UTF-%s as "
288 "working-tree-encoding.");
289 const char *stripped
= NULL
;
290 char *upper
= xstrdup_toupper(enc
);
291 upper
[strlen(upper
)-2] = '\0';
292 if (!skip_prefix(upper
, "UTF-", &stripped
))
293 skip_prefix(stripped
, "UTF", &stripped
);
294 advise(advise_msg
, path
, stripped
);
297 die(error_msg
, path
, enc
);
299 return error(error_msg
, path
, enc
);
302 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
303 const char *error_msg
= _(
304 "BOM is required in '%s' if encoded as %s");
305 const char *advise_msg
= _(
306 "The file '%s' is missing a byte order "
307 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
308 "(depending on the byte order) as "
309 "working-tree-encoding.");
310 const char *stripped
= NULL
;
311 char *upper
= xstrdup_toupper(enc
);
312 if (!skip_prefix(upper
, "UTF-", &stripped
))
313 skip_prefix(stripped
, "UTF", &stripped
);
314 advise(advise_msg
, path
, stripped
, stripped
);
317 die(error_msg
, path
, enc
);
319 return error(error_msg
, path
, enc
);
327 static void trace_encoding(const char *context
, const char *path
,
328 const char *encoding
, const char *buf
, size_t len
)
330 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
331 struct strbuf trace
= STRBUF_INIT
;
334 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
335 for (i
= 0; i
< len
&& buf
; ++i
) {
337 &trace
,"| \e[2m%2i:\e[0m %2x \e[2m%c\e[0m%c",
339 (unsigned char) buf
[i
],
340 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
341 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
344 strbuf_addchars(&trace
, '\n', 1);
346 trace_strbuf(&coe
, &trace
);
347 strbuf_release(&trace
);
350 static int check_roundtrip(const char *enc_name
)
353 * check_roundtrip_encoding contains a string of comma and/or
354 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
355 * Search for the given encoding in that string.
357 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
362 next
= found
+ strlen(enc_name
);
363 len
= strlen(check_roundtrip_encoding
);
366 * check that the found encoding is at the
367 * beginning of check_roundtrip_encoding or
368 * that it is prefixed with a space or comma
370 found
== check_roundtrip_encoding
|| (
371 (isspace(found
[-1]) || found
[-1] == ',')
375 * check that the found encoding is at the
376 * end of check_roundtrip_encoding or
377 * that it is suffixed with a space or comma
379 next
== check_roundtrip_encoding
+ len
|| (
380 next
< check_roundtrip_encoding
+ len
&&
381 (isspace(next
[0]) || next
[0] == ',')
386 static const char *default_encoding
= "UTF-8";
388 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
389 struct strbuf
*buf
, const char *enc
, int conv_flags
)
393 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
396 * No encoding is specified or there is nothing to encode.
397 * Tell the caller that the content was not modified.
399 if (!enc
|| (src
&& !src_len
))
403 * Looks like we got called from "would_convert_to_git()".
404 * This means Git wants to know if it would encode (= modify!)
405 * the content. Let's answer with "yes", since an encoding was
411 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
414 trace_encoding("source", path
, enc
, src
, src_len
);
415 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
419 * We could add the blob "as-is" to Git. However, on checkout
420 * we would try to reencode to the original encoding. This
421 * would fail and we would leave the user with a messed-up
422 * working tree. Let's try to avoid this by screaming loud.
424 const char* msg
= _("failed to encode '%s' from %s to %s");
426 die(msg
, path
, enc
, default_encoding
);
428 error(msg
, path
, enc
, default_encoding
);
432 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
435 * UTF supports lossless conversion round tripping [1] and conversions
436 * between UTF and other encodings are mostly round trip safe as
437 * Unicode aims to be a superset of all other character encodings.
438 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
439 * trip issues [2]. Check the round trip conversion for all encodings
440 * listed in core.checkRoundtripEncoding.
442 * The round trip check is only performed if content is written to Git.
443 * This ensures that no information is lost during conversion to/from
444 * the internal UTF-8 representation.
446 * Please note, the code below is not tested because I was not able to
447 * generate a faulty round trip without an iconv error. Iconv errors
448 * are already caught above.
450 * [1] http://unicode.org/faq/utf_bom.html#gen2
451 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
453 if (die_on_error
&& check_roundtrip(enc
)) {
457 re_src
= reencode_string_len(dst
, dst_len
,
458 enc
, default_encoding
,
461 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
462 trace_encoding("reencoded source", path
, enc
,
465 if (!re_src
|| src_len
!= re_src_len
||
466 memcmp(src
, re_src
, src_len
)) {
467 const char* msg
= _("encoding '%s' from %s to %s and "
468 "back is not the same");
469 die(msg
, path
, enc
, default_encoding
);
475 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
479 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
480 struct strbuf
*buf
, const char *enc
)
486 * No encoding is specified or there is nothing to encode.
487 * Tell the caller that the content was not modified.
489 if (!enc
|| (src
&& !src_len
))
492 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
495 error("failed to encode '%s' from %s to %s",
496 path
, default_encoding
, enc
);
500 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
504 static int crlf_to_git(const struct index_state
*istate
,
505 const char *path
, const char *src
, size_t len
,
507 enum crlf_action crlf_action
, int conv_flags
)
509 struct text_stat stats
;
511 int convert_crlf_into_lf
;
513 if (crlf_action
== CRLF_BINARY
||
518 * If we are doing a dry-run and have no source buffer, there is
519 * nothing to analyze; we must assume we would convert.
524 gather_stats(src
, len
, &stats
);
525 /* Optimization: No CRLF? Nothing to convert, regardless. */
526 convert_crlf_into_lf
= !!stats
.crlf
;
528 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
529 if (convert_is_binary(len
, &stats
))
532 * If the file in the index has any CR in it, do not
533 * convert. This is the new safer autocrlf handling,
534 * unless we want to renormalize in a merge or
537 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
538 has_crlf_in_index(istate
, path
))
539 convert_crlf_into_lf
= 0;
541 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
542 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
543 struct text_stat new_stats
;
544 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
545 /* simulate "git add" */
546 if (convert_crlf_into_lf
) {
547 new_stats
.lonelf
+= new_stats
.crlf
;
550 /* simulate "git checkout" */
551 if (will_convert_lf_to_crlf(len
, &new_stats
, crlf_action
)) {
552 new_stats
.crlf
+= new_stats
.lonelf
;
553 new_stats
.lonelf
= 0;
555 check_global_conv_flags_eol(path
, crlf_action
, &stats
, &new_stats
, conv_flags
);
557 if (!convert_crlf_into_lf
)
561 * At this point all of our source analysis is done, and we are sure we
562 * would convert. If we are in dry-run mode, we can give an answer.
567 /* only grow if not in place */
568 if (strbuf_avail(buf
) + buf
->len
< len
)
569 strbuf_grow(buf
, len
- buf
->len
);
571 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
573 * If we guessed, we already know we rejected a file with
574 * lone CR, and we can strip a CR without looking at what
578 unsigned char c
= *src
++;
584 unsigned char c
= *src
++;
585 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
589 strbuf_setlen(buf
, dst
- buf
->buf
);
593 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
594 struct strbuf
*buf
, enum crlf_action crlf_action
)
596 char *to_free
= NULL
;
597 struct text_stat stats
;
599 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
602 gather_stats(src
, len
, &stats
);
603 if (!will_convert_lf_to_crlf(len
, &stats
, crlf_action
))
606 /* are we "faking" in place editing ? */
608 to_free
= strbuf_detach(buf
, NULL
);
610 strbuf_grow(buf
, len
+ stats
.lonelf
);
612 const char *nl
= memchr(src
, '\n', len
);
615 if (nl
> src
&& nl
[-1] == '\r') {
616 strbuf_add(buf
, src
, nl
+ 1 - src
);
618 strbuf_add(buf
, src
, nl
- src
);
619 strbuf_addstr(buf
, "\r\n");
624 strbuf_add(buf
, src
, len
);
630 struct filter_params
{
638 static int filter_buffer_or_fd(int in
, int out
, void *data
)
641 * Spawn cmd and feed the buffer contents through its stdin.
643 struct child_process child_process
= CHILD_PROCESS_INIT
;
644 struct filter_params
*params
= (struct filter_params
*)data
;
645 int write_err
, status
;
646 const char *argv
[] = { NULL
, NULL
};
648 /* apply % substitution to cmd */
649 struct strbuf cmd
= STRBUF_INIT
;
650 struct strbuf path
= STRBUF_INIT
;
651 struct strbuf_expand_dict_entry dict
[] = {
656 /* quote the path to preserve spaces, etc. */
657 sq_quote_buf(&path
, params
->path
);
658 dict
[0].value
= path
.buf
;
660 /* expand all %f with the quoted path */
661 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
662 strbuf_release(&path
);
666 child_process
.argv
= argv
;
667 child_process
.use_shell
= 1;
668 child_process
.in
= -1;
669 child_process
.out
= out
;
671 if (start_command(&child_process
)) {
672 strbuf_release(&cmd
);
673 return error("cannot fork to run external filter '%s'", params
->cmd
);
676 sigchain_push(SIGPIPE
, SIG_IGN
);
679 write_err
= (write_in_full(child_process
.in
,
680 params
->src
, params
->size
) < 0);
684 write_err
= copy_fd(params
->fd
, child_process
.in
);
685 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
689 if (close(child_process
.in
))
692 error("cannot feed the input to external filter '%s'", params
->cmd
);
694 sigchain_pop(SIGPIPE
);
696 status
= finish_command(&child_process
);
698 error("external filter '%s' failed %d", params
->cmd
, status
);
700 strbuf_release(&cmd
);
701 return (write_err
|| status
);
704 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
705 struct strbuf
*dst
, const char *cmd
)
708 * Create a pipeline to have the command filter the buffer's
711 * (child --> cmd) --> us
714 struct strbuf nbuf
= STRBUF_INIT
;
716 struct filter_params params
;
718 memset(&async
, 0, sizeof(async
));
719 async
.proc
= filter_buffer_or_fd
;
720 async
.data
= ¶ms
;
729 if (start_async(&async
))
730 return 0; /* error was already reported */
732 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
733 err
= error("read from external filter '%s' failed", cmd
);
735 if (close(async
.out
)) {
736 err
= error("read from external filter '%s' failed", cmd
);
738 if (finish_async(&async
)) {
739 err
= error("external filter '%s' failed", cmd
);
743 strbuf_swap(dst
, &nbuf
);
745 strbuf_release(&nbuf
);
749 #define CAP_CLEAN (1u<<0)
750 #define CAP_SMUDGE (1u<<1)
751 #define CAP_DELAY (1u<<2)
754 struct subprocess_entry subprocess
; /* must be the first member! */
755 unsigned int supported_capabilities
;
758 static int subprocess_map_initialized
;
759 static struct hashmap subprocess_map
;
761 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
763 static int versions
[] = {2, 0};
764 static struct subprocess_capability capabilities
[] = {
765 { "clean", CAP_CLEAN
},
766 { "smudge", CAP_SMUDGE
},
767 { "delay", CAP_DELAY
},
770 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
771 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
773 &entry
->supported_capabilities
);
776 static void handle_filter_error(const struct strbuf
*filter_status
,
777 struct cmd2process
*entry
,
778 const unsigned int wanted_capability
) {
779 if (!strcmp(filter_status
->buf
, "error"))
780 ; /* The filter signaled a problem with the file. */
781 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
783 * The filter signaled a permanent problem. Don't try to filter
784 * files with the same command for the lifetime of the current
787 entry
->supported_capabilities
&= ~wanted_capability
;
790 * Something went wrong with the protocol filter.
791 * Force shutdown and restart if another blob requires filtering.
793 error("external filter '%s' failed", entry
->subprocess
.cmd
);
794 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
799 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
800 int fd
, struct strbuf
*dst
, const char *cmd
,
801 const unsigned int wanted_capability
,
802 struct delayed_checkout
*dco
)
806 struct cmd2process
*entry
;
807 struct child_process
*process
;
808 struct strbuf nbuf
= STRBUF_INIT
;
809 struct strbuf filter_status
= STRBUF_INIT
;
810 const char *filter_type
;
812 if (!subprocess_map_initialized
) {
813 subprocess_map_initialized
= 1;
814 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
817 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
823 entry
= xmalloc(sizeof(*entry
));
824 entry
->supported_capabilities
= 0;
826 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
831 process
= &entry
->subprocess
.process
;
833 if (!(entry
->supported_capabilities
& wanted_capability
))
836 if (wanted_capability
& CAP_CLEAN
)
837 filter_type
= "clean";
838 else if (wanted_capability
& CAP_SMUDGE
)
839 filter_type
= "smudge";
841 die("unexpected filter type");
843 sigchain_push(SIGPIPE
, SIG_IGN
);
845 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
846 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
850 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
852 error("path name too long for external filter");
856 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
860 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
861 dco
&& dco
->state
== CE_CAN_DELAY
) {
863 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
868 err
= packet_flush_gently(process
->in
);
873 err
= write_packetized_from_fd(fd
, process
->in
);
875 err
= write_packetized_from_buf(src
, len
, process
->in
);
879 err
= subprocess_read_status(process
->out
, &filter_status
);
883 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
884 string_list_insert(&dco
->filters
, cmd
);
885 string_list_insert(&dco
->paths
, path
);
887 /* The filter got the blob and wants to send us a response. */
888 err
= strcmp(filter_status
.buf
, "success");
892 err
= read_packetized_to_strbuf(process
->out
, &nbuf
) < 0;
896 err
= subprocess_read_status(process
->out
, &filter_status
);
900 err
= strcmp(filter_status
.buf
, "success");
904 sigchain_pop(SIGPIPE
);
907 handle_filter_error(&filter_status
, entry
, wanted_capability
);
909 strbuf_swap(dst
, &nbuf
);
910 strbuf_release(&nbuf
);
915 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
919 struct cmd2process
*entry
;
920 struct child_process
*process
;
921 struct strbuf filter_status
= STRBUF_INIT
;
923 assert(subprocess_map_initialized
);
924 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
926 error("external filter '%s' is not available anymore although "
927 "not all paths have been filtered", cmd
);
930 process
= &entry
->subprocess
.process
;
931 sigchain_push(SIGPIPE
, SIG_IGN
);
933 err
= packet_write_fmt_gently(
934 process
->in
, "command=list_available_blobs\n");
938 err
= packet_flush_gently(process
->in
);
942 while ((line
= packet_read_line(process
->out
, NULL
))) {
944 if (skip_prefix(line
, "pathname=", &path
))
945 string_list_insert(available_paths
, xstrdup(path
));
947 ; /* ignore unknown keys */
950 err
= subprocess_read_status(process
->out
, &filter_status
);
954 err
= strcmp(filter_status
.buf
, "success");
957 sigchain_pop(SIGPIPE
);
960 handle_filter_error(&filter_status
, entry
, 0);
964 static struct convert_driver
{
966 struct convert_driver
*next
;
971 } *user_convert
, **user_convert_tail
;
973 static int apply_filter(const char *path
, const char *src
, size_t len
,
974 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
975 const unsigned int wanted_capability
,
976 struct delayed_checkout
*dco
)
978 const char *cmd
= NULL
;
986 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
988 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
992 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
993 else if (drv
->process
&& *drv
->process
)
994 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
995 drv
->process
, wanted_capability
, dco
);
1000 static int read_convert_config(const char *var
, const char *value
, void *cb
)
1002 const char *key
, *name
;
1004 struct convert_driver
*drv
;
1007 * External conversion drivers are configured using
1008 * "filter.<name>.variable".
1010 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1012 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1013 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1016 drv
= xcalloc(1, sizeof(struct convert_driver
));
1017 drv
->name
= xmemdupz(name
, namelen
);
1018 *user_convert_tail
= drv
;
1019 user_convert_tail
= &(drv
->next
);
1023 * filter.<name>.smudge and filter.<name>.clean specifies
1028 * The command-line will not be interpolated in any way.
1031 if (!strcmp("smudge", key
))
1032 return git_config_string(&drv
->smudge
, var
, value
);
1034 if (!strcmp("clean", key
))
1035 return git_config_string(&drv
->clean
, var
, value
);
1037 if (!strcmp("process", key
))
1038 return git_config_string(&drv
->process
, var
, value
);
1040 if (!strcmp("required", key
)) {
1041 drv
->required
= git_config_bool(var
, value
);
1048 static int count_ident(const char *cp
, unsigned long size
)
1051 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1063 if (memcmp("Id", cp
, 2))
1074 * "$Id: ... "; scan up to the closing dollar sign and discard.
1090 static int ident_to_git(const char *path
, const char *src
, size_t len
,
1091 struct strbuf
*buf
, int ident
)
1095 if (!ident
|| (src
&& !count_ident(src
, len
)))
1101 /* only grow if not in place */
1102 if (strbuf_avail(buf
) + buf
->len
< len
)
1103 strbuf_grow(buf
, len
- buf
->len
);
1106 dollar
= memchr(src
, '$', len
);
1109 memmove(dst
, src
, dollar
+ 1 - src
);
1110 dst
+= dollar
+ 1 - src
;
1111 len
-= dollar
+ 1 - src
;
1114 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1115 dollar
= memchr(src
+ 3, '$', len
- 3);
1118 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1119 /* Line break before the next dollar. */
1123 memcpy(dst
, "Id$", 3);
1125 len
-= dollar
+ 1 - src
;
1129 memmove(dst
, src
, len
);
1130 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1134 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
1135 struct strbuf
*buf
, int ident
)
1137 struct object_id oid
;
1138 char *to_free
= NULL
, *dollar
, *spc
;
1144 cnt
= count_ident(src
, len
);
1148 /* are we "faking" in place editing ? */
1149 if (src
== buf
->buf
)
1150 to_free
= strbuf_detach(buf
, NULL
);
1151 hash_object_file(src
, len
, "blob", &oid
);
1153 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1155 /* step 1: run to the next '$' */
1156 dollar
= memchr(src
, '$', len
);
1159 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1160 len
-= dollar
+ 1 - src
;
1163 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1164 if (len
< 3 || memcmp("Id", src
, 2))
1167 /* step 3: skip over Id$ or Id:xxxxx$ */
1168 if (src
[2] == '$') {
1171 } else if (src
[2] == ':') {
1173 * It's possible that an expanded Id has crept its way into the
1174 * repository, we cope with that by stripping the expansion out.
1175 * This is probably not a good idea, since it will cause changes
1176 * on checkout, which won't go away by stash, but let's keep it
1177 * for git-style ids.
1179 dollar
= memchr(src
+ 3, '$', len
- 3);
1181 /* incomplete keyword, no more '$', so just quit the loop */
1185 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1186 /* Line break before the next dollar. */
1190 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1191 if (spc
&& spc
< dollar
-1) {
1192 /* There are spaces in unexpected places.
1193 * This is probably an id from some other
1194 * versioning system. Keep it for now.
1199 len
-= dollar
+ 1 - src
;
1202 /* it wasn't a "Id$" or "Id:xxxx$" */
1206 /* step 4: substitute */
1207 strbuf_addstr(buf
, "Id: ");
1208 strbuf_addstr(buf
, oid_to_hex(&oid
));
1209 strbuf_addstr(buf
, " $");
1211 strbuf_add(buf
, src
, len
);
1217 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1219 const char *value
= check
->value
;
1221 if (ATTR_UNSET(value
) || !strlen(value
))
1224 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1225 die(_("true/false are no valid working-tree-encodings"));
1228 /* Don't encode to the default encoding */
1229 if (same_encoding(value
, default_encoding
))
1235 static enum crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1237 const char *value
= check
->value
;
1239 if (ATTR_TRUE(value
))
1241 else if (ATTR_FALSE(value
))
1243 else if (ATTR_UNSET(value
))
1245 else if (!strcmp(value
, "input"))
1246 return CRLF_TEXT_INPUT
;
1247 else if (!strcmp(value
, "auto"))
1249 return CRLF_UNDEFINED
;
1252 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1254 const char *value
= check
->value
;
1256 if (ATTR_UNSET(value
))
1258 else if (!strcmp(value
, "lf"))
1260 else if (!strcmp(value
, "crlf"))
1265 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1267 const char *value
= check
->value
;
1268 struct convert_driver
*drv
;
1270 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1272 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1273 if (!strcmp(value
, drv
->name
))
1278 static int git_path_check_ident(struct attr_check_item
*check
)
1280 const char *value
= check
->value
;
1282 return !!ATTR_TRUE(value
);
1286 struct convert_driver
*drv
;
1287 enum crlf_action attr_action
; /* What attr says */
1288 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
1290 const char *working_tree_encoding
; /* Supported encoding or default encoding if NULL */
1293 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
1295 static struct attr_check
*check
;
1298 check
= attr_check_initl("crlf", "ident", "filter",
1299 "eol", "text", "working-tree-encoding",
1301 user_convert_tail
= &user_convert
;
1302 git_config(read_convert_config
, NULL
);
1305 if (!git_check_attr(path
, check
)) {
1306 struct attr_check_item
*ccheck
= check
->items
;
1307 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1308 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1309 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1310 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1311 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1312 if (ca
->crlf_action
!= CRLF_BINARY
) {
1313 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1314 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1315 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1316 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1317 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1318 else if (eol_attr
== EOL_LF
)
1319 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1320 else if (eol_attr
== EOL_CRLF
)
1321 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1323 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1326 ca
->crlf_action
= CRLF_UNDEFINED
;
1330 /* Save attr and make a decision for action */
1331 ca
->attr_action
= ca
->crlf_action
;
1332 if (ca
->crlf_action
== CRLF_TEXT
)
1333 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1334 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1335 ca
->crlf_action
= CRLF_BINARY
;
1336 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1337 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1338 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1339 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1342 int would_convert_to_git_filter_fd(const char *path
)
1344 struct conv_attrs ca
;
1346 convert_attrs(&ca
, path
);
1351 * Apply a filter to an fd only if the filter is required to succeed.
1352 * We must die if the filter fails, because the original data before
1353 * filtering is not available.
1355 if (!ca
.drv
->required
)
1358 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
);
1361 const char *get_convert_attr_ascii(const char *path
)
1363 struct conv_attrs ca
;
1365 convert_attrs(&ca
, path
);
1366 switch (ca
.attr_action
) {
1367 case CRLF_UNDEFINED
:
1373 case CRLF_TEXT_INPUT
:
1374 return "text eol=lf";
1375 case CRLF_TEXT_CRLF
:
1376 return "text eol=crlf";
1379 case CRLF_AUTO_CRLF
:
1380 return "text=auto eol=crlf";
1381 case CRLF_AUTO_INPUT
:
1382 return "text=auto eol=lf";
1387 int convert_to_git(const struct index_state
*istate
,
1388 const char *path
, const char *src
, size_t len
,
1389 struct strbuf
*dst
, int conv_flags
)
1392 struct conv_attrs ca
;
1394 convert_attrs(&ca
, path
);
1396 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
);
1397 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1398 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1405 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1411 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1412 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1418 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
1421 void convert_to_git_filter_fd(const struct index_state
*istate
,
1422 const char *path
, int fd
, struct strbuf
*dst
,
1425 struct conv_attrs ca
;
1426 convert_attrs(&ca
, path
);
1429 assert(ca
.drv
->clean
|| ca
.drv
->process
);
1431 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
))
1432 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1434 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1435 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1436 ident_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.ident
);
1439 static int convert_to_working_tree_internal(const char *path
, const char *src
,
1440 size_t len
, struct strbuf
*dst
,
1441 int normalizing
, struct delayed_checkout
*dco
)
1443 int ret
= 0, ret_filter
= 0;
1444 struct conv_attrs ca
;
1446 convert_attrs(&ca
, path
);
1448 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
1454 * CRLF conversion can be skipped if normalizing, unless there
1455 * is a smudge or process filter (even if the process filter doesn't
1456 * support smudge). The filters might expect CRLFs.
1458 if ((ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->process
)) || !normalizing
) {
1459 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
1466 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
.working_tree_encoding
);
1472 ret_filter
= apply_filter(
1473 path
, src
, len
, -1, dst
, ca
.drv
, CAP_SMUDGE
, dco
);
1474 if (!ret_filter
&& ca
.drv
&& ca
.drv
->required
)
1475 die("%s: smudge filter %s failed", path
, ca
.drv
->name
);
1477 return ret
| ret_filter
;
1480 int async_convert_to_working_tree(const char *path
, const char *src
,
1481 size_t len
, struct strbuf
*dst
,
1484 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0, dco
);
1487 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1489 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0, NULL
);
1492 int renormalize_buffer(const struct index_state
*istate
, const char *path
,
1493 const char *src
, size_t len
, struct strbuf
*dst
)
1495 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1, NULL
);
1500 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1503 /*****************************************************************
1505 * Streaming conversion support
1507 *****************************************************************/
1509 typedef int (*filter_fn
)(struct stream_filter
*,
1510 const char *input
, size_t *isize_p
,
1511 char *output
, size_t *osize_p
);
1512 typedef void (*free_fn
)(struct stream_filter
*);
1514 struct stream_filter_vtbl
{
1519 struct stream_filter
{
1520 struct stream_filter_vtbl
*vtbl
;
1523 static int null_filter_fn(struct stream_filter
*filter
,
1524 const char *input
, size_t *isize_p
,
1525 char *output
, size_t *osize_p
)
1530 return 0; /* we do not keep any states */
1532 if (*osize_p
< count
)
1535 memmove(output
, input
, count
);
1542 static void null_free_fn(struct stream_filter
*filter
)
1544 ; /* nothing -- null instances are shared */
1547 static struct stream_filter_vtbl null_vtbl
= {
1552 static struct stream_filter null_filter_singleton
= {
1556 int is_null_stream_filter(struct stream_filter
*filter
)
1558 return filter
== &null_filter_singleton
;
1566 struct lf_to_crlf_filter
{
1567 struct stream_filter filter
;
1568 unsigned has_held
:1;
1572 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1573 const char *input
, size_t *isize_p
,
1574 char *output
, size_t *osize_p
)
1576 size_t count
, o
= 0;
1577 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1580 * We may be holding onto the CR to see if it is followed by a
1581 * LF, in which case we would need to go to the main loop.
1582 * Otherwise, just emit it to the output stream.
1584 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1585 output
[o
++] = lf_to_crlf
->held
;
1586 lf_to_crlf
->has_held
= 0;
1589 /* We are told to drain */
1596 if (count
|| lf_to_crlf
->has_held
) {
1600 if (lf_to_crlf
->has_held
) {
1602 lf_to_crlf
->has_held
= 0;
1605 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1610 } else if (was_cr
) {
1612 * Previous round saw CR and it is not followed
1613 * by a LF; emit the CR before processing the
1614 * current character.
1620 * We may have consumed the last output slot,
1621 * in which case we need to break out of this
1622 * loop; hold the current character before
1625 if (*osize_p
<= o
) {
1626 lf_to_crlf
->has_held
= 1;
1627 lf_to_crlf
->held
= ch
;
1628 continue; /* break but increment i */
1643 if (!lf_to_crlf
->has_held
&& was_cr
) {
1644 lf_to_crlf
->has_held
= 1;
1645 lf_to_crlf
->held
= '\r';
1651 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1656 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1657 lf_to_crlf_filter_fn
,
1661 static struct stream_filter
*lf_to_crlf_filter(void)
1663 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1665 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1666 return (struct stream_filter
*)lf_to_crlf
;
1672 #define FILTER_BUFFER 1024
1673 struct cascade_filter
{
1674 struct stream_filter filter
;
1675 struct stream_filter
*one
;
1676 struct stream_filter
*two
;
1677 char buf
[FILTER_BUFFER
];
1681 static int cascade_filter_fn(struct stream_filter
*filter
,
1682 const char *input
, size_t *isize_p
,
1683 char *output
, size_t *osize_p
)
1685 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1687 size_t sz
= *osize_p
;
1688 size_t to_feed
, remaining
;
1691 * input -- (one) --> buf -- (two) --> output
1693 while (filled
< sz
) {
1694 remaining
= sz
- filled
;
1696 /* do we already have something to feed two with? */
1697 if (cas
->ptr
< cas
->end
) {
1698 to_feed
= cas
->end
- cas
->ptr
;
1699 if (stream_filter(cas
->two
,
1700 cas
->buf
+ cas
->ptr
, &to_feed
,
1701 output
+ filled
, &remaining
))
1703 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1704 filled
= sz
- remaining
;
1708 /* feed one from upstream and have it emit into our buffer */
1709 to_feed
= input
? *isize_p
: 0;
1710 if (input
&& !to_feed
)
1712 remaining
= sizeof(cas
->buf
);
1713 if (stream_filter(cas
->one
,
1715 cas
->buf
, &remaining
))
1717 cas
->end
= sizeof(cas
->buf
) - remaining
;
1720 size_t fed
= *isize_p
- to_feed
;
1725 /* do we know that we drained one completely? */
1726 if (input
|| cas
->end
)
1729 /* tell two to drain; we have nothing more to give it */
1731 remaining
= sz
- filled
;
1732 if (stream_filter(cas
->two
,
1734 output
+ filled
, &remaining
))
1736 if (remaining
== (sz
- filled
))
1737 break; /* completely drained two */
1738 filled
= sz
- remaining
;
1744 static void cascade_free_fn(struct stream_filter
*filter
)
1746 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1747 free_stream_filter(cas
->one
);
1748 free_stream_filter(cas
->two
);
1752 static struct stream_filter_vtbl cascade_vtbl
= {
1757 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1758 struct stream_filter
*two
)
1760 struct cascade_filter
*cascade
;
1762 if (!one
|| is_null_stream_filter(one
))
1764 if (!two
|| is_null_stream_filter(two
))
1767 cascade
= xmalloc(sizeof(*cascade
));
1770 cascade
->end
= cascade
->ptr
= 0;
1771 cascade
->filter
.vtbl
= &cascade_vtbl
;
1772 return (struct stream_filter
*)cascade
;
1778 #define IDENT_DRAINING (-1)
1779 #define IDENT_SKIPPING (-2)
1780 struct ident_filter
{
1781 struct stream_filter filter
;
1784 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1787 static int is_foreign_ident(const char *str
)
1791 if (!skip_prefix(str
, "$Id: ", &str
))
1793 for (i
= 0; str
[i
]; i
++) {
1794 if (isspace(str
[i
]) && str
[i
+1] != '$')
1800 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1802 size_t to_drain
= ident
->left
.len
;
1804 if (*osize_p
< to_drain
)
1805 to_drain
= *osize_p
;
1807 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1808 strbuf_remove(&ident
->left
, 0, to_drain
);
1809 *output_p
+= to_drain
;
1810 *osize_p
-= to_drain
;
1812 if (!ident
->left
.len
)
1816 static int ident_filter_fn(struct stream_filter
*filter
,
1817 const char *input
, size_t *isize_p
,
1818 char *output
, size_t *osize_p
)
1820 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1821 static const char head
[] = "$Id";
1824 /* drain upon eof */
1825 switch (ident
->state
) {
1827 strbuf_add(&ident
->left
, head
, ident
->state
);
1829 case IDENT_SKIPPING
:
1831 case IDENT_DRAINING
:
1832 ident_drain(ident
, &output
, osize_p
);
1837 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1840 if (ident
->state
== IDENT_DRAINING
) {
1841 ident_drain(ident
, &output
, osize_p
);
1850 if (ident
->state
== IDENT_SKIPPING
) {
1852 * Skipping until '$' or LF, but keeping them
1853 * in case it is a foreign ident.
1855 strbuf_addch(&ident
->left
, ch
);
1856 if (ch
!= '\n' && ch
!= '$')
1858 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1859 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1860 strbuf_addstr(&ident
->left
, ident
->ident
);
1862 ident
->state
= IDENT_DRAINING
;
1866 if (ident
->state
< sizeof(head
) &&
1867 head
[ident
->state
] == ch
) {
1873 strbuf_add(&ident
->left
, head
, ident
->state
);
1874 if (ident
->state
== sizeof(head
) - 1) {
1875 if (ch
!= ':' && ch
!= '$') {
1876 strbuf_addch(&ident
->left
, ch
);
1882 strbuf_addch(&ident
->left
, ch
);
1883 ident
->state
= IDENT_SKIPPING
;
1885 strbuf_addstr(&ident
->left
, ident
->ident
);
1886 ident
->state
= IDENT_DRAINING
;
1891 strbuf_addch(&ident
->left
, ch
);
1892 ident
->state
= IDENT_DRAINING
;
1897 static void ident_free_fn(struct stream_filter
*filter
)
1899 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1900 strbuf_release(&ident
->left
);
1904 static struct stream_filter_vtbl ident_vtbl
= {
1909 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1911 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1913 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1914 ": %s $", oid_to_hex(oid
));
1915 strbuf_init(&ident
->left
, 0);
1916 ident
->filter
.vtbl
= &ident_vtbl
;
1918 return (struct stream_filter
*)ident
;
1922 * Return an appropriately constructed filter for the path, or NULL if
1923 * the contents cannot be filtered without reading the whole thing
1926 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1927 * large binary blob you would want us not to slurp into the memory!
1929 struct stream_filter
*get_stream_filter(const char *path
, const struct object_id
*oid
)
1931 struct conv_attrs ca
;
1932 struct stream_filter
*filter
= NULL
;
1934 convert_attrs(&ca
, path
);
1935 if (ca
.drv
&& (ca
.drv
->process
|| ca
.drv
->smudge
|| ca
.drv
->clean
))
1938 if (ca
.working_tree_encoding
)
1941 if (ca
.crlf_action
== CRLF_AUTO
|| ca
.crlf_action
== CRLF_AUTO_CRLF
)
1945 filter
= ident_filter(oid
);
1947 if (output_eol(ca
.crlf_action
) == EOL_CRLF
)
1948 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1950 filter
= cascade_filter(filter
, &null_filter_singleton
);
1955 void free_stream_filter(struct stream_filter
*filter
)
1957 filter
->vtbl
->free(filter
);
1960 int stream_filter(struct stream_filter
*filter
,
1961 const char *input
, size_t *isize_p
,
1962 char *output
, size_t *osize_p
)
1964 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);