3 #include "object-store.h"
5 #include "run-command.h"
9 #include "sub-process.h"
14 * convert.c - convert a file when checking it out and checking it in.
16 * This should use the pathname to decide on whether it wants to do some
17 * more interesting conversions (automatic gzip/unzip, general format
18 * conversions etc etc), but by default it just does automatic CRLF<->LF
19 * translation when the "text" attribute or "auto_crlf" option is set.
22 /* Stat bits: When BIN is set, the txt bits are unset */
23 #define CONVERT_STAT_BITS_TXT_LF 0x1
24 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
25 #define CONVERT_STAT_BITS_BIN 0x4
39 /* NUL, CR, LF and CRLF counts */
40 unsigned nul
, lonecr
, lonelf
, crlf
;
42 /* These are just approximations! */
43 unsigned printable
, nonprintable
;
46 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
50 memset(stats
, 0, sizeof(*stats
));
52 for (i
= 0; i
< size
; i
++) {
53 unsigned char c
= buf
[i
];
55 if (i
+1 < size
&& buf
[i
+1] == '\n') {
68 stats
->nonprintable
++;
71 /* BS, HT, ESC and FF */
72 case '\b': case '\t': case '\033': case '\014':
79 stats
->nonprintable
++;
86 /* If file ends with EOF then don't count this EOF as non-printable. */
87 if (size
>= 1 && buf
[size
-1] == '\032')
88 stats
->nonprintable
--;
92 * The same heuristics as diff.c::mmfile_is_binary()
93 * We treat files with bare CR as binary
95 static int convert_is_binary(const struct text_stat
*stats
)
101 if ((stats
->printable
>> 7) < stats
->nonprintable
)
106 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
108 struct text_stat stats
;
112 gather_stats(data
, size
, &stats
);
113 if (convert_is_binary(&stats
))
114 ret
|= CONVERT_STAT_BITS_BIN
;
116 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
118 ret
|= CONVERT_STAT_BITS_TXT_LF
;
123 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
125 unsigned int convert_stats
= gather_convert_stats(data
, size
);
127 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
129 switch (convert_stats
) {
130 case CONVERT_STAT_BITS_TXT_LF
:
132 case CONVERT_STAT_BITS_TXT_CRLF
:
134 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
141 const char *get_cached_convert_stats_ascii(const struct index_state
*istate
,
146 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
147 ret
= gather_convert_stats_ascii(data
, sz
);
152 const char *get_wt_convert_stats_ascii(const char *path
)
154 const char *ret
= "";
155 struct strbuf sb
= STRBUF_INIT
;
156 if (strbuf_read_file(&sb
, path
, 0) >= 0)
157 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
162 static int text_eol_is_crlf(void)
164 if (auto_crlf
== AUTO_CRLF_TRUE
)
166 else if (auto_crlf
== AUTO_CRLF_INPUT
)
168 if (core_eol
== EOL_CRLF
)
170 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
175 static enum eol
output_eol(enum crlf_action crlf_action
)
177 switch (crlf_action
) {
182 case CRLF_TEXT_INPUT
:
187 case CRLF_AUTO_INPUT
:
192 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
194 warning(_("illegal crlf_action %d"), (int)crlf_action
);
198 static void check_global_conv_flags_eol(const char *path
, enum crlf_action crlf_action
,
199 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
202 if (old_stats
->crlf
&& !new_stats
->crlf
) {
204 * CRLFs would not be restored by checkout
206 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
207 die(_("CRLF would be replaced by LF in %s"), path
);
208 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
209 warning(_("CRLF will be replaced by LF in %s.\n"
210 "The file will have its original line"
211 " endings in your working directory"), path
);
212 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
214 * CRLFs would be added by checkout
216 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
217 die(_("LF would be replaced by CRLF in %s"), path
);
218 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
219 warning(_("LF will be replaced by CRLF in %s.\n"
220 "The file will have its original line"
221 " endings in your working directory"), path
);
225 static int has_crlf_in_index(const struct index_state
*istate
, const char *path
)
232 data
= read_blob_data_from_index(istate
, path
, &sz
);
236 crp
= memchr(data
, '\r', sz
);
238 unsigned int ret_stats
;
239 ret_stats
= gather_convert_stats(data
, sz
);
240 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
241 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
248 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
249 enum crlf_action crlf_action
)
251 if (output_eol(crlf_action
) != EOL_CRLF
)
253 /* No "naked" LF? Nothing to convert, regardless. */
257 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
258 /* If we have any CR or CRLF line endings, we do not touch it */
259 /* This is the new safer autocrlf-handling */
260 if (stats
->lonecr
|| stats
->crlf
)
263 if (convert_is_binary(stats
))
270 static int validate_encoding(const char *path
, const char *enc
,
271 const char *data
, size_t len
, int die_on_error
)
273 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
274 if (istarts_with(enc
, "UTF")) {
276 * Check for detectable errors in UTF encodings
278 if (has_prohibited_utf_bom(enc
, data
, len
)) {
279 const char *error_msg
= _(
280 "BOM is prohibited in '%s' if encoded as %s");
282 * This advice is shown for UTF-??BE and UTF-??LE encodings.
283 * We cut off the last two characters of the encoding name
284 * to generate the encoding name suitable for BOMs.
286 const char *advise_msg
= _(
287 "The file '%s' contains a byte order "
288 "mark (BOM). Please use UTF-%s as "
289 "working-tree-encoding.");
290 const char *stripped
= NULL
;
291 char *upper
= xstrdup_toupper(enc
);
292 upper
[strlen(upper
)-2] = '\0';
293 if (!skip_prefix(upper
, "UTF-", &stripped
))
294 skip_prefix(stripped
, "UTF", &stripped
);
295 advise(advise_msg
, path
, stripped
);
298 die(error_msg
, path
, enc
);
300 return error(error_msg
, path
, enc
);
303 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
304 const char *error_msg
= _(
305 "BOM is required in '%s' if encoded as %s");
306 const char *advise_msg
= _(
307 "The file '%s' is missing a byte order "
308 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
309 "(depending on the byte order) as "
310 "working-tree-encoding.");
311 const char *stripped
= NULL
;
312 char *upper
= xstrdup_toupper(enc
);
313 if (!skip_prefix(upper
, "UTF-", &stripped
))
314 skip_prefix(stripped
, "UTF", &stripped
);
315 advise(advise_msg
, path
, stripped
, stripped
);
318 die(error_msg
, path
, enc
);
320 return error(error_msg
, path
, enc
);
328 static void trace_encoding(const char *context
, const char *path
,
329 const char *encoding
, const char *buf
, size_t len
)
331 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
332 struct strbuf trace
= STRBUF_INIT
;
335 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
336 for (i
= 0; i
< len
&& buf
; ++i
) {
338 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
340 (unsigned char) buf
[i
],
341 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
342 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
345 strbuf_addchars(&trace
, '\n', 1);
347 trace_strbuf(&coe
, &trace
);
348 strbuf_release(&trace
);
351 static int check_roundtrip(const char *enc_name
)
354 * check_roundtrip_encoding contains a string of comma and/or
355 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
356 * Search for the given encoding in that string.
358 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
363 next
= found
+ strlen(enc_name
);
364 len
= strlen(check_roundtrip_encoding
);
367 * check that the found encoding is at the
368 * beginning of check_roundtrip_encoding or
369 * that it is prefixed with a space or comma
371 found
== check_roundtrip_encoding
|| (
372 (isspace(found
[-1]) || found
[-1] == ',')
376 * check that the found encoding is at the
377 * end of check_roundtrip_encoding or
378 * that it is suffixed with a space or comma
380 next
== check_roundtrip_encoding
+ len
|| (
381 next
< check_roundtrip_encoding
+ len
&&
382 (isspace(next
[0]) || next
[0] == ',')
387 static const char *default_encoding
= "UTF-8";
389 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
390 struct strbuf
*buf
, const char *enc
, int conv_flags
)
394 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
397 * No encoding is specified or there is nothing to encode.
398 * Tell the caller that the content was not modified.
400 if (!enc
|| (src
&& !src_len
))
404 * Looks like we got called from "would_convert_to_git()".
405 * This means Git wants to know if it would encode (= modify!)
406 * the content. Let's answer with "yes", since an encoding was
412 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
415 trace_encoding("source", path
, enc
, src
, src_len
);
416 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
420 * We could add the blob "as-is" to Git. However, on checkout
421 * we would try to reencode to the original encoding. This
422 * would fail and we would leave the user with a messed-up
423 * working tree. Let's try to avoid this by screaming loud.
425 const char* msg
= _("failed to encode '%s' from %s to %s");
427 die(msg
, path
, enc
, default_encoding
);
429 error(msg
, path
, enc
, default_encoding
);
433 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
436 * UTF supports lossless conversion round tripping [1] and conversions
437 * between UTF and other encodings are mostly round trip safe as
438 * Unicode aims to be a superset of all other character encodings.
439 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
440 * trip issues [2]. Check the round trip conversion for all encodings
441 * listed in core.checkRoundtripEncoding.
443 * The round trip check is only performed if content is written to Git.
444 * This ensures that no information is lost during conversion to/from
445 * the internal UTF-8 representation.
447 * Please note, the code below is not tested because I was not able to
448 * generate a faulty round trip without an iconv error. Iconv errors
449 * are already caught above.
451 * [1] http://unicode.org/faq/utf_bom.html#gen2
452 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
454 if (die_on_error
&& check_roundtrip(enc
)) {
458 re_src
= reencode_string_len(dst
, dst_len
,
459 enc
, default_encoding
,
462 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
463 trace_encoding("reencoded source", path
, enc
,
466 if (!re_src
|| src_len
!= re_src_len
||
467 memcmp(src
, re_src
, src_len
)) {
468 const char* msg
= _("encoding '%s' from %s to %s and "
469 "back is not the same");
470 die(msg
, path
, enc
, default_encoding
);
476 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
480 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
481 struct strbuf
*buf
, const char *enc
)
487 * No encoding is specified or there is nothing to encode.
488 * Tell the caller that the content was not modified.
490 if (!enc
|| (src
&& !src_len
))
493 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
496 error(_("failed to encode '%s' from %s to %s"),
497 path
, default_encoding
, enc
);
501 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
505 static int crlf_to_git(const struct index_state
*istate
,
506 const char *path
, const char *src
, size_t len
,
508 enum crlf_action crlf_action
, int conv_flags
)
510 struct text_stat stats
;
512 int convert_crlf_into_lf
;
514 if (crlf_action
== CRLF_BINARY
||
519 * If we are doing a dry-run and have no source buffer, there is
520 * nothing to analyze; we must assume we would convert.
525 gather_stats(src
, len
, &stats
);
526 /* Optimization: No CRLF? Nothing to convert, regardless. */
527 convert_crlf_into_lf
= !!stats
.crlf
;
529 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
530 if (convert_is_binary(&stats
))
533 * If the file in the index has any CR in it, do not
534 * convert. This is the new safer autocrlf handling,
535 * unless we want to renormalize in a merge or
538 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
539 has_crlf_in_index(istate
, path
))
540 convert_crlf_into_lf
= 0;
542 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
543 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
544 struct text_stat new_stats
;
545 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
546 /* simulate "git add" */
547 if (convert_crlf_into_lf
) {
548 new_stats
.lonelf
+= new_stats
.crlf
;
551 /* simulate "git checkout" */
552 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
553 new_stats
.crlf
+= new_stats
.lonelf
;
554 new_stats
.lonelf
= 0;
556 check_global_conv_flags_eol(path
, crlf_action
, &stats
, &new_stats
, conv_flags
);
558 if (!convert_crlf_into_lf
)
562 * At this point all of our source analysis is done, and we are sure we
563 * would convert. If we are in dry-run mode, we can give an answer.
568 /* only grow if not in place */
569 if (strbuf_avail(buf
) + buf
->len
< len
)
570 strbuf_grow(buf
, len
- buf
->len
);
572 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
574 * If we guessed, we already know we rejected a file with
575 * lone CR, and we can strip a CR without looking at what
579 unsigned char c
= *src
++;
585 unsigned char c
= *src
++;
586 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
590 strbuf_setlen(buf
, dst
- buf
->buf
);
594 static int crlf_to_worktree(const char *src
, size_t len
,
595 struct strbuf
*buf
, enum crlf_action crlf_action
)
597 char *to_free
= NULL
;
598 struct text_stat stats
;
600 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
603 gather_stats(src
, len
, &stats
);
604 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
607 /* are we "faking" in place editing ? */
609 to_free
= strbuf_detach(buf
, NULL
);
611 strbuf_grow(buf
, len
+ stats
.lonelf
);
613 const char *nl
= memchr(src
, '\n', len
);
616 if (nl
> src
&& nl
[-1] == '\r') {
617 strbuf_add(buf
, src
, nl
+ 1 - src
);
619 strbuf_add(buf
, src
, nl
- src
);
620 strbuf_addstr(buf
, "\r\n");
625 strbuf_add(buf
, src
, len
);
631 struct filter_params
{
639 static int filter_buffer_or_fd(int in
, int out
, void *data
)
642 * Spawn cmd and feed the buffer contents through its stdin.
644 struct child_process child_process
= CHILD_PROCESS_INIT
;
645 struct filter_params
*params
= (struct filter_params
*)data
;
646 int write_err
, status
;
647 const char *argv
[] = { NULL
, NULL
};
649 /* apply % substitution to cmd */
650 struct strbuf cmd
= STRBUF_INIT
;
651 struct strbuf path
= STRBUF_INIT
;
652 struct strbuf_expand_dict_entry dict
[] = {
657 /* quote the path to preserve spaces, etc. */
658 sq_quote_buf(&path
, params
->path
);
659 dict
[0].value
= path
.buf
;
661 /* expand all %f with the quoted path */
662 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
663 strbuf_release(&path
);
667 child_process
.argv
= argv
;
668 child_process
.use_shell
= 1;
669 child_process
.in
= -1;
670 child_process
.out
= out
;
672 if (start_command(&child_process
)) {
673 strbuf_release(&cmd
);
674 return error(_("cannot fork to run external filter '%s'"),
678 sigchain_push(SIGPIPE
, SIG_IGN
);
681 write_err
= (write_in_full(child_process
.in
,
682 params
->src
, params
->size
) < 0);
686 write_err
= copy_fd(params
->fd
, child_process
.in
);
687 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
691 if (close(child_process
.in
))
694 error(_("cannot feed the input to external filter '%s'"),
697 sigchain_pop(SIGPIPE
);
699 status
= finish_command(&child_process
);
701 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
703 strbuf_release(&cmd
);
704 return (write_err
|| status
);
707 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
708 struct strbuf
*dst
, const char *cmd
)
711 * Create a pipeline to have the command filter the buffer's
714 * (child --> cmd) --> us
717 struct strbuf nbuf
= STRBUF_INIT
;
719 struct filter_params params
;
721 memset(&async
, 0, sizeof(async
));
722 async
.proc
= filter_buffer_or_fd
;
723 async
.data
= ¶ms
;
732 if (start_async(&async
))
733 return 0; /* error was already reported */
735 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
736 err
= error(_("read from external filter '%s' failed"), cmd
);
738 if (close(async
.out
)) {
739 err
= error(_("read from external filter '%s' failed"), cmd
);
741 if (finish_async(&async
)) {
742 err
= error(_("external filter '%s' failed"), cmd
);
746 strbuf_swap(dst
, &nbuf
);
748 strbuf_release(&nbuf
);
752 #define CAP_CLEAN (1u<<0)
753 #define CAP_SMUDGE (1u<<1)
754 #define CAP_DELAY (1u<<2)
757 struct subprocess_entry subprocess
; /* must be the first member! */
758 unsigned int supported_capabilities
;
761 static int subprocess_map_initialized
;
762 static struct hashmap subprocess_map
;
764 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
766 static int versions
[] = {2, 0};
767 static struct subprocess_capability capabilities
[] = {
768 { "clean", CAP_CLEAN
},
769 { "smudge", CAP_SMUDGE
},
770 { "delay", CAP_DELAY
},
773 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
774 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
776 &entry
->supported_capabilities
);
779 static void handle_filter_error(const struct strbuf
*filter_status
,
780 struct cmd2process
*entry
,
781 const unsigned int wanted_capability
)
783 if (!strcmp(filter_status
->buf
, "error"))
784 ; /* The filter signaled a problem with the file. */
785 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
787 * The filter signaled a permanent problem. Don't try to filter
788 * files with the same command for the lifetime of the current
791 entry
->supported_capabilities
&= ~wanted_capability
;
794 * Something went wrong with the protocol filter.
795 * Force shutdown and restart if another blob requires filtering.
797 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
798 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
803 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
804 int fd
, struct strbuf
*dst
, const char *cmd
,
805 const unsigned int wanted_capability
,
806 struct delayed_checkout
*dco
)
810 struct cmd2process
*entry
;
811 struct child_process
*process
;
812 struct strbuf nbuf
= STRBUF_INIT
;
813 struct strbuf filter_status
= STRBUF_INIT
;
814 const char *filter_type
;
816 if (!subprocess_map_initialized
) {
817 subprocess_map_initialized
= 1;
818 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
821 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
827 entry
= xmalloc(sizeof(*entry
));
828 entry
->supported_capabilities
= 0;
830 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
835 process
= &entry
->subprocess
.process
;
837 if (!(entry
->supported_capabilities
& wanted_capability
))
840 if (wanted_capability
& CAP_CLEAN
)
841 filter_type
= "clean";
842 else if (wanted_capability
& CAP_SMUDGE
)
843 filter_type
= "smudge";
845 die(_("unexpected filter type"));
847 sigchain_push(SIGPIPE
, SIG_IGN
);
849 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
850 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
854 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
856 error(_("path name too long for external filter"));
860 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
864 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
865 dco
&& dco
->state
== CE_CAN_DELAY
) {
867 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
872 err
= packet_flush_gently(process
->in
);
877 err
= write_packetized_from_fd(fd
, process
->in
);
879 err
= write_packetized_from_buf(src
, len
, process
->in
);
883 err
= subprocess_read_status(process
->out
, &filter_status
);
887 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
888 string_list_insert(&dco
->filters
, cmd
);
889 string_list_insert(&dco
->paths
, path
);
891 /* The filter got the blob and wants to send us a response. */
892 err
= strcmp(filter_status
.buf
, "success");
896 err
= read_packetized_to_strbuf(process
->out
, &nbuf
) < 0;
900 err
= subprocess_read_status(process
->out
, &filter_status
);
904 err
= strcmp(filter_status
.buf
, "success");
908 sigchain_pop(SIGPIPE
);
911 handle_filter_error(&filter_status
, entry
, wanted_capability
);
913 strbuf_swap(dst
, &nbuf
);
914 strbuf_release(&nbuf
);
919 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
923 struct cmd2process
*entry
;
924 struct child_process
*process
;
925 struct strbuf filter_status
= STRBUF_INIT
;
927 assert(subprocess_map_initialized
);
928 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
930 error(_("external filter '%s' is not available anymore although "
931 "not all paths have been filtered"), cmd
);
934 process
= &entry
->subprocess
.process
;
935 sigchain_push(SIGPIPE
, SIG_IGN
);
937 err
= packet_write_fmt_gently(
938 process
->in
, "command=list_available_blobs\n");
942 err
= packet_flush_gently(process
->in
);
946 while ((line
= packet_read_line(process
->out
, NULL
))) {
948 if (skip_prefix(line
, "pathname=", &path
))
949 string_list_insert(available_paths
, xstrdup(path
));
951 ; /* ignore unknown keys */
954 err
= subprocess_read_status(process
->out
, &filter_status
);
958 err
= strcmp(filter_status
.buf
, "success");
961 sigchain_pop(SIGPIPE
);
964 handle_filter_error(&filter_status
, entry
, 0);
968 static struct convert_driver
{
970 struct convert_driver
*next
;
975 } *user_convert
, **user_convert_tail
;
977 static int apply_filter(const char *path
, const char *src
, size_t len
,
978 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
979 const unsigned int wanted_capability
,
980 struct delayed_checkout
*dco
)
982 const char *cmd
= NULL
;
990 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
992 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
996 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
997 else if (drv
->process
&& *drv
->process
)
998 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
999 drv
->process
, wanted_capability
, dco
);
1004 static int read_convert_config(const char *var
, const char *value
, void *cb
)
1006 const char *key
, *name
;
1008 struct convert_driver
*drv
;
1011 * External conversion drivers are configured using
1012 * "filter.<name>.variable".
1014 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1016 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1017 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1020 drv
= xcalloc(1, sizeof(struct convert_driver
));
1021 drv
->name
= xmemdupz(name
, namelen
);
1022 *user_convert_tail
= drv
;
1023 user_convert_tail
= &(drv
->next
);
1027 * filter.<name>.smudge and filter.<name>.clean specifies
1032 * The command-line will not be interpolated in any way.
1035 if (!strcmp("smudge", key
))
1036 return git_config_string(&drv
->smudge
, var
, value
);
1038 if (!strcmp("clean", key
))
1039 return git_config_string(&drv
->clean
, var
, value
);
1041 if (!strcmp("process", key
))
1042 return git_config_string(&drv
->process
, var
, value
);
1044 if (!strcmp("required", key
)) {
1045 drv
->required
= git_config_bool(var
, value
);
1052 static int count_ident(const char *cp
, unsigned long size
)
1055 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1067 if (memcmp("Id", cp
, 2))
1078 * "$Id: ... "; scan up to the closing dollar sign and discard.
1094 static int ident_to_git(const char *src
, size_t len
,
1095 struct strbuf
*buf
, int ident
)
1099 if (!ident
|| (src
&& !count_ident(src
, len
)))
1105 /* only grow if not in place */
1106 if (strbuf_avail(buf
) + buf
->len
< len
)
1107 strbuf_grow(buf
, len
- buf
->len
);
1110 dollar
= memchr(src
, '$', len
);
1113 memmove(dst
, src
, dollar
+ 1 - src
);
1114 dst
+= dollar
+ 1 - src
;
1115 len
-= dollar
+ 1 - src
;
1118 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1119 dollar
= memchr(src
+ 3, '$', len
- 3);
1122 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1123 /* Line break before the next dollar. */
1127 memcpy(dst
, "Id$", 3);
1129 len
-= dollar
+ 1 - src
;
1133 memmove(dst
, src
, len
);
1134 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1138 static int ident_to_worktree(const char *src
, size_t len
,
1139 struct strbuf
*buf
, int ident
)
1141 struct object_id oid
;
1142 char *to_free
= NULL
, *dollar
, *spc
;
1148 cnt
= count_ident(src
, len
);
1152 /* are we "faking" in place editing ? */
1153 if (src
== buf
->buf
)
1154 to_free
= strbuf_detach(buf
, NULL
);
1155 hash_object_file(src
, len
, "blob", &oid
);
1157 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1159 /* step 1: run to the next '$' */
1160 dollar
= memchr(src
, '$', len
);
1163 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1164 len
-= dollar
+ 1 - src
;
1167 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1168 if (len
< 3 || memcmp("Id", src
, 2))
1171 /* step 3: skip over Id$ or Id:xxxxx$ */
1172 if (src
[2] == '$') {
1175 } else if (src
[2] == ':') {
1177 * It's possible that an expanded Id has crept its way into the
1178 * repository, we cope with that by stripping the expansion out.
1179 * This is probably not a good idea, since it will cause changes
1180 * on checkout, which won't go away by stash, but let's keep it
1181 * for git-style ids.
1183 dollar
= memchr(src
+ 3, '$', len
- 3);
1185 /* incomplete keyword, no more '$', so just quit the loop */
1189 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1190 /* Line break before the next dollar. */
1194 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1195 if (spc
&& spc
< dollar
-1) {
1196 /* There are spaces in unexpected places.
1197 * This is probably an id from some other
1198 * versioning system. Keep it for now.
1203 len
-= dollar
+ 1 - src
;
1206 /* it wasn't a "Id$" or "Id:xxxx$" */
1210 /* step 4: substitute */
1211 strbuf_addstr(buf
, "Id: ");
1212 strbuf_addstr(buf
, oid_to_hex(&oid
));
1213 strbuf_addstr(buf
, " $");
1215 strbuf_add(buf
, src
, len
);
1221 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1223 const char *value
= check
->value
;
1225 if (ATTR_UNSET(value
) || !strlen(value
))
1228 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1229 die(_("true/false are no valid working-tree-encodings"));
1232 /* Don't encode to the default encoding */
1233 if (same_encoding(value
, default_encoding
))
1239 static enum crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1241 const char *value
= check
->value
;
1243 if (ATTR_TRUE(value
))
1245 else if (ATTR_FALSE(value
))
1247 else if (ATTR_UNSET(value
))
1249 else if (!strcmp(value
, "input"))
1250 return CRLF_TEXT_INPUT
;
1251 else if (!strcmp(value
, "auto"))
1253 return CRLF_UNDEFINED
;
1256 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1258 const char *value
= check
->value
;
1260 if (ATTR_UNSET(value
))
1262 else if (!strcmp(value
, "lf"))
1264 else if (!strcmp(value
, "crlf"))
1269 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1271 const char *value
= check
->value
;
1272 struct convert_driver
*drv
;
1274 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1276 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1277 if (!strcmp(value
, drv
->name
))
1282 static int git_path_check_ident(struct attr_check_item
*check
)
1284 const char *value
= check
->value
;
1286 return !!ATTR_TRUE(value
);
1290 struct convert_driver
*drv
;
1291 enum crlf_action attr_action
; /* What attr says */
1292 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
1294 const char *working_tree_encoding
; /* Supported encoding or default encoding if NULL */
1297 static struct attr_check
*check
;
1299 static void convert_attrs(const struct index_state
*istate
,
1300 struct conv_attrs
*ca
, const char *path
)
1302 struct attr_check_item
*ccheck
= NULL
;
1305 check
= attr_check_initl("crlf", "ident", "filter",
1306 "eol", "text", "working-tree-encoding",
1308 user_convert_tail
= &user_convert
;
1309 git_config(read_convert_config
, NULL
);
1312 git_check_attr(istate
, path
, check
);
1313 ccheck
= check
->items
;
1314 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1315 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1316 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1317 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1318 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1319 if (ca
->crlf_action
!= CRLF_BINARY
) {
1320 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1321 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1322 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1323 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1324 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1325 else if (eol_attr
== EOL_LF
)
1326 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1327 else if (eol_attr
== EOL_CRLF
)
1328 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1330 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1332 /* Save attr and make a decision for action */
1333 ca
->attr_action
= ca
->crlf_action
;
1334 if (ca
->crlf_action
== CRLF_TEXT
)
1335 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1336 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1337 ca
->crlf_action
= CRLF_BINARY
;
1338 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1339 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1340 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1341 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1344 void reset_parsed_attributes(void)
1346 struct convert_driver
*drv
, *next
;
1348 attr_check_free(check
);
1350 reset_merge_attributes();
1352 for (drv
= user_convert
; drv
; drv
= next
) {
1354 free((void *)drv
->name
);
1357 user_convert
= NULL
;
1358 user_convert_tail
= NULL
;
1361 int would_convert_to_git_filter_fd(const struct index_state
*istate
, const char *path
)
1363 struct conv_attrs ca
;
1365 convert_attrs(istate
, &ca
, path
);
1370 * Apply a filter to an fd only if the filter is required to succeed.
1371 * We must die if the filter fails, because the original data before
1372 * filtering is not available.
1374 if (!ca
.drv
->required
)
1377 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
);
1380 const char *get_convert_attr_ascii(const struct index_state
*istate
, const char *path
)
1382 struct conv_attrs ca
;
1384 convert_attrs(istate
, &ca
, path
);
1385 switch (ca
.attr_action
) {
1386 case CRLF_UNDEFINED
:
1392 case CRLF_TEXT_INPUT
:
1393 return "text eol=lf";
1394 case CRLF_TEXT_CRLF
:
1395 return "text eol=crlf";
1398 case CRLF_AUTO_CRLF
:
1399 return "text=auto eol=crlf";
1400 case CRLF_AUTO_INPUT
:
1401 return "text=auto eol=lf";
1406 int convert_to_git(const struct index_state
*istate
,
1407 const char *path
, const char *src
, size_t len
,
1408 struct strbuf
*dst
, int conv_flags
)
1411 struct conv_attrs ca
;
1413 convert_attrs(istate
, &ca
, path
);
1415 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
);
1416 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1417 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1424 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1430 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1431 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1437 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1440 void convert_to_git_filter_fd(const struct index_state
*istate
,
1441 const char *path
, int fd
, struct strbuf
*dst
,
1444 struct conv_attrs ca
;
1445 convert_attrs(istate
, &ca
, path
);
1448 assert(ca
.drv
->clean
|| ca
.drv
->process
);
1450 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
))
1451 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1453 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1454 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1455 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1458 static int convert_to_working_tree_internal(const struct index_state
*istate
,
1459 const char *path
, const char *src
,
1460 size_t len
, struct strbuf
*dst
,
1461 int normalizing
, struct delayed_checkout
*dco
)
1463 int ret
= 0, ret_filter
= 0;
1464 struct conv_attrs ca
;
1466 convert_attrs(istate
, &ca
, path
);
1468 ret
|= ident_to_worktree(src
, len
, dst
, ca
.ident
);
1474 * CRLF conversion can be skipped if normalizing, unless there
1475 * is a smudge or process filter (even if the process filter doesn't
1476 * support smudge). The filters might expect CRLFs.
1478 if ((ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->process
)) || !normalizing
) {
1479 ret
|= crlf_to_worktree(src
, len
, dst
, ca
.crlf_action
);
1486 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
.working_tree_encoding
);
1492 ret_filter
= apply_filter(
1493 path
, src
, len
, -1, dst
, ca
.drv
, CAP_SMUDGE
, dco
);
1494 if (!ret_filter
&& ca
.drv
&& ca
.drv
->required
)
1495 die(_("%s: smudge filter %s failed"), path
, ca
.drv
->name
);
1497 return ret
| ret_filter
;
1500 int async_convert_to_working_tree(const struct index_state
*istate
,
1501 const char *path
, const char *src
,
1502 size_t len
, struct strbuf
*dst
,
1505 return convert_to_working_tree_internal(istate
, path
, src
, len
, dst
, 0, dco
);
1508 int convert_to_working_tree(const struct index_state
*istate
,
1509 const char *path
, const char *src
,
1510 size_t len
, struct strbuf
*dst
)
1512 return convert_to_working_tree_internal(istate
, path
, src
, len
, dst
, 0, NULL
);
1515 int renormalize_buffer(const struct index_state
*istate
, const char *path
,
1516 const char *src
, size_t len
, struct strbuf
*dst
)
1518 int ret
= convert_to_working_tree_internal(istate
, path
, src
, len
, dst
, 1, NULL
);
1523 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1526 /*****************************************************************
1528 * Streaming conversion support
1530 *****************************************************************/
1532 typedef int (*filter_fn
)(struct stream_filter
*,
1533 const char *input
, size_t *isize_p
,
1534 char *output
, size_t *osize_p
);
1535 typedef void (*free_fn
)(struct stream_filter
*);
1537 struct stream_filter_vtbl
{
1542 struct stream_filter
{
1543 struct stream_filter_vtbl
*vtbl
;
1546 static int null_filter_fn(struct stream_filter
*filter
,
1547 const char *input
, size_t *isize_p
,
1548 char *output
, size_t *osize_p
)
1553 return 0; /* we do not keep any states */
1555 if (*osize_p
< count
)
1558 memmove(output
, input
, count
);
1565 static void null_free_fn(struct stream_filter
*filter
)
1567 ; /* nothing -- null instances are shared */
1570 static struct stream_filter_vtbl null_vtbl
= {
1575 static struct stream_filter null_filter_singleton
= {
1579 int is_null_stream_filter(struct stream_filter
*filter
)
1581 return filter
== &null_filter_singleton
;
1589 struct lf_to_crlf_filter
{
1590 struct stream_filter filter
;
1591 unsigned has_held
:1;
1595 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1596 const char *input
, size_t *isize_p
,
1597 char *output
, size_t *osize_p
)
1599 size_t count
, o
= 0;
1600 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1603 * We may be holding onto the CR to see if it is followed by a
1604 * LF, in which case we would need to go to the main loop.
1605 * Otherwise, just emit it to the output stream.
1607 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1608 output
[o
++] = lf_to_crlf
->held
;
1609 lf_to_crlf
->has_held
= 0;
1612 /* We are told to drain */
1619 if (count
|| lf_to_crlf
->has_held
) {
1623 if (lf_to_crlf
->has_held
) {
1625 lf_to_crlf
->has_held
= 0;
1628 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1633 } else if (was_cr
) {
1635 * Previous round saw CR and it is not followed
1636 * by a LF; emit the CR before processing the
1637 * current character.
1643 * We may have consumed the last output slot,
1644 * in which case we need to break out of this
1645 * loop; hold the current character before
1648 if (*osize_p
<= o
) {
1649 lf_to_crlf
->has_held
= 1;
1650 lf_to_crlf
->held
= ch
;
1651 continue; /* break but increment i */
1666 if (!lf_to_crlf
->has_held
&& was_cr
) {
1667 lf_to_crlf
->has_held
= 1;
1668 lf_to_crlf
->held
= '\r';
1674 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1679 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1680 lf_to_crlf_filter_fn
,
1684 static struct stream_filter
*lf_to_crlf_filter(void)
1686 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1688 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1689 return (struct stream_filter
*)lf_to_crlf
;
1695 #define FILTER_BUFFER 1024
1696 struct cascade_filter
{
1697 struct stream_filter filter
;
1698 struct stream_filter
*one
;
1699 struct stream_filter
*two
;
1700 char buf
[FILTER_BUFFER
];
1704 static int cascade_filter_fn(struct stream_filter
*filter
,
1705 const char *input
, size_t *isize_p
,
1706 char *output
, size_t *osize_p
)
1708 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1710 size_t sz
= *osize_p
;
1711 size_t to_feed
, remaining
;
1714 * input -- (one) --> buf -- (two) --> output
1716 while (filled
< sz
) {
1717 remaining
= sz
- filled
;
1719 /* do we already have something to feed two with? */
1720 if (cas
->ptr
< cas
->end
) {
1721 to_feed
= cas
->end
- cas
->ptr
;
1722 if (stream_filter(cas
->two
,
1723 cas
->buf
+ cas
->ptr
, &to_feed
,
1724 output
+ filled
, &remaining
))
1726 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1727 filled
= sz
- remaining
;
1731 /* feed one from upstream and have it emit into our buffer */
1732 to_feed
= input
? *isize_p
: 0;
1733 if (input
&& !to_feed
)
1735 remaining
= sizeof(cas
->buf
);
1736 if (stream_filter(cas
->one
,
1738 cas
->buf
, &remaining
))
1740 cas
->end
= sizeof(cas
->buf
) - remaining
;
1743 size_t fed
= *isize_p
- to_feed
;
1748 /* do we know that we drained one completely? */
1749 if (input
|| cas
->end
)
1752 /* tell two to drain; we have nothing more to give it */
1754 remaining
= sz
- filled
;
1755 if (stream_filter(cas
->two
,
1757 output
+ filled
, &remaining
))
1759 if (remaining
== (sz
- filled
))
1760 break; /* completely drained two */
1761 filled
= sz
- remaining
;
1767 static void cascade_free_fn(struct stream_filter
*filter
)
1769 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1770 free_stream_filter(cas
->one
);
1771 free_stream_filter(cas
->two
);
1775 static struct stream_filter_vtbl cascade_vtbl
= {
1780 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1781 struct stream_filter
*two
)
1783 struct cascade_filter
*cascade
;
1785 if (!one
|| is_null_stream_filter(one
))
1787 if (!two
|| is_null_stream_filter(two
))
1790 cascade
= xmalloc(sizeof(*cascade
));
1793 cascade
->end
= cascade
->ptr
= 0;
1794 cascade
->filter
.vtbl
= &cascade_vtbl
;
1795 return (struct stream_filter
*)cascade
;
1801 #define IDENT_DRAINING (-1)
1802 #define IDENT_SKIPPING (-2)
1803 struct ident_filter
{
1804 struct stream_filter filter
;
1807 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1810 static int is_foreign_ident(const char *str
)
1814 if (!skip_prefix(str
, "$Id: ", &str
))
1816 for (i
= 0; str
[i
]; i
++) {
1817 if (isspace(str
[i
]) && str
[i
+1] != '$')
1823 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1825 size_t to_drain
= ident
->left
.len
;
1827 if (*osize_p
< to_drain
)
1828 to_drain
= *osize_p
;
1830 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1831 strbuf_remove(&ident
->left
, 0, to_drain
);
1832 *output_p
+= to_drain
;
1833 *osize_p
-= to_drain
;
1835 if (!ident
->left
.len
)
1839 static int ident_filter_fn(struct stream_filter
*filter
,
1840 const char *input
, size_t *isize_p
,
1841 char *output
, size_t *osize_p
)
1843 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1844 static const char head
[] = "$Id";
1847 /* drain upon eof */
1848 switch (ident
->state
) {
1850 strbuf_add(&ident
->left
, head
, ident
->state
);
1852 case IDENT_SKIPPING
:
1854 case IDENT_DRAINING
:
1855 ident_drain(ident
, &output
, osize_p
);
1860 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1863 if (ident
->state
== IDENT_DRAINING
) {
1864 ident_drain(ident
, &output
, osize_p
);
1873 if (ident
->state
== IDENT_SKIPPING
) {
1875 * Skipping until '$' or LF, but keeping them
1876 * in case it is a foreign ident.
1878 strbuf_addch(&ident
->left
, ch
);
1879 if (ch
!= '\n' && ch
!= '$')
1881 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1882 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1883 strbuf_addstr(&ident
->left
, ident
->ident
);
1885 ident
->state
= IDENT_DRAINING
;
1889 if (ident
->state
< sizeof(head
) &&
1890 head
[ident
->state
] == ch
) {
1896 strbuf_add(&ident
->left
, head
, ident
->state
);
1897 if (ident
->state
== sizeof(head
) - 1) {
1898 if (ch
!= ':' && ch
!= '$') {
1899 strbuf_addch(&ident
->left
, ch
);
1905 strbuf_addch(&ident
->left
, ch
);
1906 ident
->state
= IDENT_SKIPPING
;
1908 strbuf_addstr(&ident
->left
, ident
->ident
);
1909 ident
->state
= IDENT_DRAINING
;
1914 strbuf_addch(&ident
->left
, ch
);
1915 ident
->state
= IDENT_DRAINING
;
1920 static void ident_free_fn(struct stream_filter
*filter
)
1922 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1923 strbuf_release(&ident
->left
);
1927 static struct stream_filter_vtbl ident_vtbl
= {
1932 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1934 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1936 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1937 ": %s $", oid_to_hex(oid
));
1938 strbuf_init(&ident
->left
, 0);
1939 ident
->filter
.vtbl
= &ident_vtbl
;
1941 return (struct stream_filter
*)ident
;
1945 * Return an appropriately constructed filter for the path, or NULL if
1946 * the contents cannot be filtered without reading the whole thing
1949 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1950 * large binary blob you would want us not to slurp into the memory!
1952 struct stream_filter
*get_stream_filter(const struct index_state
*istate
,
1954 const struct object_id
*oid
)
1956 struct conv_attrs ca
;
1957 struct stream_filter
*filter
= NULL
;
1959 convert_attrs(istate
, &ca
, path
);
1960 if (ca
.drv
&& (ca
.drv
->process
|| ca
.drv
->smudge
|| ca
.drv
->clean
))
1963 if (ca
.working_tree_encoding
)
1966 if (ca
.crlf_action
== CRLF_AUTO
|| ca
.crlf_action
== CRLF_AUTO_CRLF
)
1970 filter
= ident_filter(oid
);
1972 if (output_eol(ca
.crlf_action
) == EOL_CRLF
)
1973 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1975 filter
= cascade_filter(filter
, &null_filter_singleton
);
1980 void free_stream_filter(struct stream_filter
*filter
)
1982 filter
->vtbl
->free(filter
);
1985 int stream_filter(struct stream_filter
*filter
,
1986 const char *input
, size_t *isize_p
,
1987 char *output
, size_t *osize_p
)
1989 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);