4 #include "object-store.h"
6 #include "run-command.h"
10 #include "sub-process.h"
15 * convert.c - convert a file when checking it out and checking it in.
17 * This should use the pathname to decide on whether it wants to do some
18 * more interesting conversions (automatic gzip/unzip, general format
19 * conversions etc etc), but by default it just does automatic CRLF<->LF
20 * translation when the "text" attribute or "auto_crlf" option is set.
23 /* Stat bits: When BIN is set, the txt bits are unset */
24 #define CONVERT_STAT_BITS_TXT_LF 0x1
25 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
26 #define CONVERT_STAT_BITS_BIN 0x4
29 /* NUL, CR, LF and CRLF counts */
30 unsigned nul
, lonecr
, lonelf
, crlf
;
32 /* These are just approximations! */
33 unsigned printable
, nonprintable
;
36 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
40 memset(stats
, 0, sizeof(*stats
));
42 for (i
= 0; i
< size
; i
++) {
43 unsigned char c
= buf
[i
];
45 if (i
+1 < size
&& buf
[i
+1] == '\n') {
58 stats
->nonprintable
++;
61 /* BS, HT, ESC and FF */
62 case '\b': case '\t': case '\033': case '\014':
69 stats
->nonprintable
++;
76 /* If file ends with EOF then don't count this EOF as non-printable. */
77 if (size
>= 1 && buf
[size
-1] == '\032')
78 stats
->nonprintable
--;
82 * The same heuristics as diff.c::mmfile_is_binary()
83 * We treat files with bare CR as binary
85 static int convert_is_binary(const struct text_stat
*stats
)
91 if ((stats
->printable
>> 7) < stats
->nonprintable
)
96 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
98 struct text_stat stats
;
102 gather_stats(data
, size
, &stats
);
103 if (convert_is_binary(&stats
))
104 ret
|= CONVERT_STAT_BITS_BIN
;
106 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
108 ret
|= CONVERT_STAT_BITS_TXT_LF
;
113 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
115 unsigned int convert_stats
= gather_convert_stats(data
, size
);
117 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
119 switch (convert_stats
) {
120 case CONVERT_STAT_BITS_TXT_LF
:
122 case CONVERT_STAT_BITS_TXT_CRLF
:
124 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
131 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
136 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
137 ret
= gather_convert_stats_ascii(data
, sz
);
142 const char *get_wt_convert_stats_ascii(const char *path
)
144 const char *ret
= "";
145 struct strbuf sb
= STRBUF_INIT
;
146 if (strbuf_read_file(&sb
, path
, 0) >= 0)
147 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
152 static int text_eol_is_crlf(void)
154 if (auto_crlf
== AUTO_CRLF_TRUE
)
156 else if (auto_crlf
== AUTO_CRLF_INPUT
)
158 if (core_eol
== EOL_CRLF
)
160 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
165 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
167 switch (crlf_action
) {
172 case CRLF_TEXT_INPUT
:
177 case CRLF_AUTO_INPUT
:
182 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
184 warning(_("illegal crlf_action %d"), (int)crlf_action
);
188 static void check_global_conv_flags_eol(const char *path
,
189 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
192 if (old_stats
->crlf
&& !new_stats
->crlf
) {
194 * CRLFs would not be restored by checkout
196 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
197 die(_("CRLF would be replaced by LF in %s"), path
);
198 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
199 warning(_("in the working copy of '%s', CRLF will be"
200 " replaced by LF the next time Git touches"
202 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
204 * CRLFs would be added by checkout
206 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
207 die(_("LF would be replaced by CRLF in %s"), path
);
208 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
209 warning(_("in the working copy of '%s', LF will be"
210 " replaced by CRLF the next time Git touches"
215 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
222 data
= read_blob_data_from_index(istate
, path
, &sz
);
226 crp
= memchr(data
, '\r', sz
);
228 unsigned int ret_stats
;
229 ret_stats
= gather_convert_stats(data
, sz
);
230 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
231 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
238 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
239 enum convert_crlf_action crlf_action
)
241 if (output_eol(crlf_action
) != EOL_CRLF
)
243 /* No "naked" LF? Nothing to convert, regardless. */
247 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
248 /* If we have any CR or CRLF line endings, we do not touch it */
249 /* This is the new safer autocrlf-handling */
250 if (stats
->lonecr
|| stats
->crlf
)
253 if (convert_is_binary(stats
))
260 static int validate_encoding(const char *path
, const char *enc
,
261 const char *data
, size_t len
, int die_on_error
)
263 const char *stripped
;
265 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
266 if (skip_iprefix(enc
, "UTF", &stripped
)) {
267 skip_prefix(stripped
, "-", &stripped
);
270 * Check for detectable errors in UTF encodings
272 if (has_prohibited_utf_bom(enc
, data
, len
)) {
273 const char *error_msg
= _(
274 "BOM is prohibited in '%s' if encoded as %s");
276 * This advice is shown for UTF-??BE and UTF-??LE encodings.
277 * We cut off the last two characters of the encoding name
278 * to generate the encoding name suitable for BOMs.
280 const char *advise_msg
= _(
281 "The file '%s' contains a byte order "
282 "mark (BOM). Please use UTF-%.*s as "
283 "working-tree-encoding.");
284 int stripped_len
= strlen(stripped
) - strlen("BE");
285 advise(advise_msg
, path
, stripped_len
, stripped
);
287 die(error_msg
, path
, enc
);
289 return error(error_msg
, path
, enc
);
292 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
293 const char *error_msg
= _(
294 "BOM is required in '%s' if encoded as %s");
295 const char *advise_msg
= _(
296 "The file '%s' is missing a byte order "
297 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
298 "(depending on the byte order) as "
299 "working-tree-encoding.");
300 advise(advise_msg
, path
, stripped
, stripped
);
302 die(error_msg
, path
, enc
);
304 return error(error_msg
, path
, enc
);
312 static void trace_encoding(const char *context
, const char *path
,
313 const char *encoding
, const char *buf
, size_t len
)
315 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
316 struct strbuf trace
= STRBUF_INIT
;
319 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
320 for (i
= 0; i
< len
&& buf
; ++i
) {
322 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
324 (unsigned char) buf
[i
],
325 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
326 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
329 strbuf_addchars(&trace
, '\n', 1);
331 trace_strbuf(&coe
, &trace
);
332 strbuf_release(&trace
);
335 static int check_roundtrip(const char *enc_name
)
338 * check_roundtrip_encoding contains a string of comma and/or
339 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
340 * Search for the given encoding in that string.
342 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
347 next
= found
+ strlen(enc_name
);
348 len
= strlen(check_roundtrip_encoding
);
351 * check that the found encoding is at the
352 * beginning of check_roundtrip_encoding or
353 * that it is prefixed with a space or comma
355 found
== check_roundtrip_encoding
|| (
356 (isspace(found
[-1]) || found
[-1] == ',')
360 * check that the found encoding is at the
361 * end of check_roundtrip_encoding or
362 * that it is suffixed with a space or comma
364 next
== check_roundtrip_encoding
+ len
|| (
365 next
< check_roundtrip_encoding
+ len
&&
366 (isspace(next
[0]) || next
[0] == ',')
371 static const char *default_encoding
= "UTF-8";
373 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
374 struct strbuf
*buf
, const char *enc
, int conv_flags
)
378 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
381 * No encoding is specified or there is nothing to encode.
382 * Tell the caller that the content was not modified.
384 if (!enc
|| (src
&& !src_len
))
388 * Looks like we got called from "would_convert_to_git()".
389 * This means Git wants to know if it would encode (= modify!)
390 * the content. Let's answer with "yes", since an encoding was
396 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
399 trace_encoding("source", path
, enc
, src
, src_len
);
400 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
404 * We could add the blob "as-is" to Git. However, on checkout
405 * we would try to re-encode to the original encoding. This
406 * would fail and we would leave the user with a messed-up
407 * working tree. Let's try to avoid this by screaming loud.
409 const char* msg
= _("failed to encode '%s' from %s to %s");
411 die(msg
, path
, enc
, default_encoding
);
413 error(msg
, path
, enc
, default_encoding
);
417 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
420 * UTF supports lossless conversion round tripping [1] and conversions
421 * between UTF and other encodings are mostly round trip safe as
422 * Unicode aims to be a superset of all other character encodings.
423 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
424 * trip issues [2]. Check the round trip conversion for all encodings
425 * listed in core.checkRoundtripEncoding.
427 * The round trip check is only performed if content is written to Git.
428 * This ensures that no information is lost during conversion to/from
429 * the internal UTF-8 representation.
431 * Please note, the code below is not tested because I was not able to
432 * generate a faulty round trip without an iconv error. Iconv errors
433 * are already caught above.
435 * [1] http://unicode.org/faq/utf_bom.html#gen2
436 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
438 if (die_on_error
&& check_roundtrip(enc
)) {
442 re_src
= reencode_string_len(dst
, dst_len
,
443 enc
, default_encoding
,
446 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
447 trace_encoding("reencoded source", path
, enc
,
450 if (!re_src
|| src_len
!= re_src_len
||
451 memcmp(src
, re_src
, src_len
)) {
452 const char* msg
= _("encoding '%s' from %s to %s and "
453 "back is not the same");
454 die(msg
, path
, enc
, default_encoding
);
460 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
464 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
465 struct strbuf
*buf
, const char *enc
)
471 * No encoding is specified or there is nothing to encode.
472 * Tell the caller that the content was not modified.
474 if (!enc
|| (src
&& !src_len
))
477 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
480 error(_("failed to encode '%s' from %s to %s"),
481 path
, default_encoding
, enc
);
485 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
489 static int crlf_to_git(struct index_state
*istate
,
490 const char *path
, const char *src
, size_t len
,
492 enum convert_crlf_action crlf_action
, int conv_flags
)
494 struct text_stat stats
;
496 int convert_crlf_into_lf
;
498 if (crlf_action
== CRLF_BINARY
||
503 * If we are doing a dry-run and have no source buffer, there is
504 * nothing to analyze; we must assume we would convert.
509 gather_stats(src
, len
, &stats
);
510 /* Optimization: No CRLF? Nothing to convert, regardless. */
511 convert_crlf_into_lf
= !!stats
.crlf
;
513 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
514 if (convert_is_binary(&stats
))
517 * If the file in the index has any CR in it, do not
518 * convert. This is the new safer autocrlf handling,
519 * unless we want to renormalize in a merge or
522 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
523 has_crlf_in_index(istate
, path
))
524 convert_crlf_into_lf
= 0;
526 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
527 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
528 struct text_stat new_stats
;
529 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
530 /* simulate "git add" */
531 if (convert_crlf_into_lf
) {
532 new_stats
.lonelf
+= new_stats
.crlf
;
535 /* simulate "git checkout" */
536 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
537 new_stats
.crlf
+= new_stats
.lonelf
;
538 new_stats
.lonelf
= 0;
540 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
542 if (!convert_crlf_into_lf
)
546 * At this point all of our source analysis is done, and we are sure we
547 * would convert. If we are in dry-run mode, we can give an answer.
552 /* only grow if not in place */
553 if (strbuf_avail(buf
) + buf
->len
< len
)
554 strbuf_grow(buf
, len
- buf
->len
);
556 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
558 * If we guessed, we already know we rejected a file with
559 * lone CR, and we can strip a CR without looking at what
563 unsigned char c
= *src
++;
569 unsigned char c
= *src
++;
570 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
574 strbuf_setlen(buf
, dst
- buf
->buf
);
578 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
579 enum convert_crlf_action crlf_action
)
581 char *to_free
= NULL
;
582 struct text_stat stats
;
584 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
587 gather_stats(src
, len
, &stats
);
588 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
591 /* are we "faking" in place editing ? */
593 to_free
= strbuf_detach(buf
, NULL
);
595 strbuf_grow(buf
, len
+ stats
.lonelf
);
597 const char *nl
= memchr(src
, '\n', len
);
600 if (nl
> src
&& nl
[-1] == '\r') {
601 strbuf_add(buf
, src
, nl
+ 1 - src
);
603 strbuf_add(buf
, src
, nl
- src
);
604 strbuf_addstr(buf
, "\r\n");
609 strbuf_add(buf
, src
, len
);
615 struct filter_params
{
623 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
626 * Spawn cmd and feed the buffer contents through its stdin.
628 struct child_process child_process
= CHILD_PROCESS_INIT
;
629 struct filter_params
*params
= (struct filter_params
*)data
;
630 int write_err
, status
;
632 /* apply % substitution to cmd */
633 struct strbuf cmd
= STRBUF_INIT
;
634 struct strbuf path
= STRBUF_INIT
;
635 struct strbuf_expand_dict_entry dict
[] = {
640 /* quote the path to preserve spaces, etc. */
641 sq_quote_buf(&path
, params
->path
);
642 dict
[0].value
= path
.buf
;
644 /* expand all %f with the quoted path */
645 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
646 strbuf_release(&path
);
648 strvec_push(&child_process
.args
, cmd
.buf
);
649 child_process
.use_shell
= 1;
650 child_process
.in
= -1;
651 child_process
.out
= out
;
653 if (start_command(&child_process
)) {
654 strbuf_release(&cmd
);
655 return error(_("cannot fork to run external filter '%s'"),
659 sigchain_push(SIGPIPE
, SIG_IGN
);
662 write_err
= (write_in_full(child_process
.in
,
663 params
->src
, params
->size
) < 0);
667 write_err
= copy_fd(params
->fd
, child_process
.in
);
668 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
672 if (close(child_process
.in
))
675 error(_("cannot feed the input to external filter '%s'"),
678 sigchain_pop(SIGPIPE
);
680 status
= finish_command(&child_process
);
682 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
684 strbuf_release(&cmd
);
685 return (write_err
|| status
);
688 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
689 struct strbuf
*dst
, const char *cmd
)
692 * Create a pipeline to have the command filter the buffer's
695 * (child --> cmd) --> us
698 struct strbuf nbuf
= STRBUF_INIT
;
700 struct filter_params params
;
702 memset(&async
, 0, sizeof(async
));
703 async
.proc
= filter_buffer_or_fd
;
704 async
.data
= ¶ms
;
713 if (start_async(&async
))
714 return 0; /* error was already reported */
716 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
717 err
= error(_("read from external filter '%s' failed"), cmd
);
719 if (close(async
.out
)) {
720 err
= error(_("read from external filter '%s' failed"), cmd
);
722 if (finish_async(&async
)) {
723 err
= error(_("external filter '%s' failed"), cmd
);
727 strbuf_swap(dst
, &nbuf
);
729 strbuf_release(&nbuf
);
733 #define CAP_CLEAN (1u<<0)
734 #define CAP_SMUDGE (1u<<1)
735 #define CAP_DELAY (1u<<2)
738 struct subprocess_entry subprocess
; /* must be the first member! */
739 unsigned int supported_capabilities
;
742 static int subprocess_map_initialized
;
743 static struct hashmap subprocess_map
;
745 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
747 static int versions
[] = {2, 0};
748 static struct subprocess_capability capabilities
[] = {
749 { "clean", CAP_CLEAN
},
750 { "smudge", CAP_SMUDGE
},
751 { "delay", CAP_DELAY
},
754 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
755 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
757 &entry
->supported_capabilities
);
760 static void handle_filter_error(const struct strbuf
*filter_status
,
761 struct cmd2process
*entry
,
762 const unsigned int wanted_capability
)
764 if (!strcmp(filter_status
->buf
, "error"))
765 ; /* The filter signaled a problem with the file. */
766 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
768 * The filter signaled a permanent problem. Don't try to filter
769 * files with the same command for the lifetime of the current
772 entry
->supported_capabilities
&= ~wanted_capability
;
775 * Something went wrong with the protocol filter.
776 * Force shutdown and restart if another blob requires filtering.
778 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
779 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
784 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
785 int fd
, struct strbuf
*dst
, const char *cmd
,
786 const unsigned int wanted_capability
,
787 const struct checkout_metadata
*meta
,
788 struct delayed_checkout
*dco
)
792 struct cmd2process
*entry
;
793 struct child_process
*process
;
794 struct strbuf nbuf
= STRBUF_INIT
;
795 struct strbuf filter_status
= STRBUF_INIT
;
796 const char *filter_type
;
798 if (!subprocess_map_initialized
) {
799 subprocess_map_initialized
= 1;
800 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
803 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
809 entry
= xmalloc(sizeof(*entry
));
810 entry
->supported_capabilities
= 0;
812 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
817 process
= &entry
->subprocess
.process
;
819 if (!(entry
->supported_capabilities
& wanted_capability
))
822 if (wanted_capability
& CAP_CLEAN
)
823 filter_type
= "clean";
824 else if (wanted_capability
& CAP_SMUDGE
)
825 filter_type
= "smudge";
827 die(_("unexpected filter type"));
829 sigchain_push(SIGPIPE
, SIG_IGN
);
831 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
832 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
836 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
838 error(_("path name too long for external filter"));
842 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
846 if (meta
&& meta
->refname
) {
847 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
852 if (meta
&& !is_null_oid(&meta
->treeish
)) {
853 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
858 if (meta
&& !is_null_oid(&meta
->blob
)) {
859 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
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_no_flush(fd
, process
->in
);
879 err
= write_packetized_from_buf_no_flush(src
, len
, process
->in
);
883 err
= packet_flush_gently(process
->in
);
887 err
= subprocess_read_status(process
->out
, &filter_status
);
891 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
892 string_list_insert(&dco
->filters
, cmd
);
893 string_list_insert(&dco
->paths
, path
);
895 /* The filter got the blob and wants to send us a response. */
896 err
= strcmp(filter_status
.buf
, "success");
900 err
= read_packetized_to_strbuf(process
->out
, &nbuf
,
901 PACKET_READ_GENTLE_ON_EOF
) < 0;
905 err
= subprocess_read_status(process
->out
, &filter_status
);
909 err
= strcmp(filter_status
.buf
, "success");
913 sigchain_pop(SIGPIPE
);
916 handle_filter_error(&filter_status
, entry
, wanted_capability
);
918 strbuf_swap(dst
, &nbuf
);
919 strbuf_release(&nbuf
);
920 strbuf_release(&filter_status
);
925 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
929 struct cmd2process
*entry
;
930 struct child_process
*process
;
931 struct strbuf filter_status
= STRBUF_INIT
;
933 assert(subprocess_map_initialized
);
934 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
936 error(_("external filter '%s' is not available anymore although "
937 "not all paths have been filtered"), cmd
);
940 process
= &entry
->subprocess
.process
;
941 sigchain_push(SIGPIPE
, SIG_IGN
);
943 err
= packet_write_fmt_gently(
944 process
->in
, "command=list_available_blobs\n");
948 err
= packet_flush_gently(process
->in
);
952 while ((line
= packet_read_line(process
->out
, NULL
))) {
954 if (skip_prefix(line
, "pathname=", &path
))
955 string_list_insert(available_paths
, xstrdup(path
));
957 ; /* ignore unknown keys */
960 err
= subprocess_read_status(process
->out
, &filter_status
);
964 err
= strcmp(filter_status
.buf
, "success");
967 sigchain_pop(SIGPIPE
);
970 handle_filter_error(&filter_status
, entry
, 0);
971 strbuf_release(&filter_status
);
975 static struct convert_driver
{
977 struct convert_driver
*next
;
982 } *user_convert
, **user_convert_tail
;
984 static int apply_filter(const char *path
, const char *src
, size_t len
,
985 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
986 const unsigned int wanted_capability
,
987 const struct checkout_metadata
*meta
,
988 struct delayed_checkout
*dco
)
990 const char *cmd
= NULL
;
998 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
1000 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1004 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1005 else if (drv
->process
&& *drv
->process
)
1006 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1007 drv
->process
, wanted_capability
, meta
, dco
);
1012 static int read_convert_config(const char *var
, const char *value
, void *cb UNUSED
)
1014 const char *key
, *name
;
1016 struct convert_driver
*drv
;
1019 * External conversion drivers are configured using
1020 * "filter.<name>.variable".
1022 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1024 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1025 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1028 CALLOC_ARRAY(drv
, 1);
1029 drv
->name
= xmemdupz(name
, namelen
);
1030 *user_convert_tail
= drv
;
1031 user_convert_tail
= &(drv
->next
);
1035 * filter.<name>.smudge and filter.<name>.clean specifies
1040 * The command-line will not be interpolated in any way.
1043 if (!strcmp("smudge", key
))
1044 return git_config_string(&drv
->smudge
, var
, value
);
1046 if (!strcmp("clean", key
))
1047 return git_config_string(&drv
->clean
, var
, value
);
1049 if (!strcmp("process", key
))
1050 return git_config_string(&drv
->process
, var
, value
);
1052 if (!strcmp("required", key
)) {
1053 drv
->required
= git_config_bool(var
, value
);
1060 static int count_ident(const char *cp
, unsigned long size
)
1063 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1075 if (memcmp("Id", cp
, 2))
1086 * "$Id: ... "; scan up to the closing dollar sign and discard.
1102 static int ident_to_git(const char *src
, size_t len
,
1103 struct strbuf
*buf
, int ident
)
1107 if (!ident
|| (src
&& !count_ident(src
, len
)))
1113 /* only grow if not in place */
1114 if (strbuf_avail(buf
) + buf
->len
< len
)
1115 strbuf_grow(buf
, len
- buf
->len
);
1118 dollar
= memchr(src
, '$', len
);
1121 memmove(dst
, src
, dollar
+ 1 - src
);
1122 dst
+= dollar
+ 1 - src
;
1123 len
-= dollar
+ 1 - src
;
1126 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1127 dollar
= memchr(src
+ 3, '$', len
- 3);
1130 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1131 /* Line break before the next dollar. */
1135 memcpy(dst
, "Id$", 3);
1137 len
-= dollar
+ 1 - src
;
1141 memmove(dst
, src
, len
);
1142 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1146 static int ident_to_worktree(const char *src
, size_t len
,
1147 struct strbuf
*buf
, int ident
)
1149 struct object_id oid
;
1150 char *to_free
= NULL
, *dollar
, *spc
;
1156 cnt
= count_ident(src
, len
);
1160 /* are we "faking" in place editing ? */
1161 if (src
== buf
->buf
)
1162 to_free
= strbuf_detach(buf
, NULL
);
1163 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1165 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1167 /* step 1: run to the next '$' */
1168 dollar
= memchr(src
, '$', len
);
1171 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1172 len
-= dollar
+ 1 - src
;
1175 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1176 if (len
< 3 || memcmp("Id", src
, 2))
1179 /* step 3: skip over Id$ or Id:xxxxx$ */
1180 if (src
[2] == '$') {
1183 } else if (src
[2] == ':') {
1185 * It's possible that an expanded Id has crept its way into the
1186 * repository, we cope with that by stripping the expansion out.
1187 * This is probably not a good idea, since it will cause changes
1188 * on checkout, which won't go away by stash, but let's keep it
1189 * for git-style ids.
1191 dollar
= memchr(src
+ 3, '$', len
- 3);
1193 /* incomplete keyword, no more '$', so just quit the loop */
1197 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1198 /* Line break before the next dollar. */
1202 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1203 if (spc
&& spc
< dollar
-1) {
1204 /* There are spaces in unexpected places.
1205 * This is probably an id from some other
1206 * versioning system. Keep it for now.
1211 len
-= dollar
+ 1 - src
;
1214 /* it wasn't a "Id$" or "Id:xxxx$" */
1218 /* step 4: substitute */
1219 strbuf_addstr(buf
, "Id: ");
1220 strbuf_addstr(buf
, oid_to_hex(&oid
));
1221 strbuf_addstr(buf
, " $");
1223 strbuf_add(buf
, src
, len
);
1229 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1231 const char *value
= check
->value
;
1233 if (ATTR_UNSET(value
) || !strlen(value
))
1236 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1237 die(_("true/false are no valid working-tree-encodings"));
1240 /* Don't encode to the default encoding */
1241 if (same_encoding(value
, default_encoding
))
1247 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1249 const char *value
= check
->value
;
1251 if (ATTR_TRUE(value
))
1253 else if (ATTR_FALSE(value
))
1255 else if (ATTR_UNSET(value
))
1257 else if (!strcmp(value
, "input"))
1258 return CRLF_TEXT_INPUT
;
1259 else if (!strcmp(value
, "auto"))
1261 return CRLF_UNDEFINED
;
1264 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1266 const char *value
= check
->value
;
1268 if (ATTR_UNSET(value
))
1270 else if (!strcmp(value
, "lf"))
1272 else if (!strcmp(value
, "crlf"))
1277 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1279 const char *value
= check
->value
;
1280 struct convert_driver
*drv
;
1282 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1284 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1285 if (!strcmp(value
, drv
->name
))
1290 static int git_path_check_ident(struct attr_check_item
*check
)
1292 const char *value
= check
->value
;
1294 return !!ATTR_TRUE(value
);
1297 static struct attr_check
*check
;
1299 void convert_attrs(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
, NULL
, 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(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
, NULL
);
1380 const char *get_convert_attr_ascii(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(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
, 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(struct index_state
*istate
,
1441 const char *path
, int fd
, struct strbuf
*dst
,
1444 struct conv_attrs ca
;
1445 convert_attrs(istate
, &ca
, path
);
1449 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1450 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1452 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1453 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1454 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1457 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1458 const char *path
, const char *src
,
1459 size_t len
, struct strbuf
*dst
,
1461 const struct checkout_metadata
*meta
,
1462 struct delayed_checkout
*dco
)
1464 int ret
= 0, ret_filter
= 0;
1466 ret
|= ident_to_worktree(src
, len
, dst
, ca
->ident
);
1472 * CRLF conversion can be skipped if normalizing, unless there
1473 * is a smudge or process filter (even if the process filter doesn't
1474 * support smudge). The filters might expect CRLFs.
1476 if ((ca
->drv
&& (ca
->drv
->smudge
|| ca
->drv
->process
)) || !normalizing
) {
1477 ret
|= crlf_to_worktree(src
, len
, dst
, ca
->crlf_action
);
1484 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
->working_tree_encoding
);
1490 ret_filter
= apply_filter(
1491 path
, src
, len
, -1, dst
, ca
->drv
, CAP_SMUDGE
, meta
, dco
);
1492 if (!ret_filter
&& ca
->drv
&& ca
->drv
->required
)
1493 die(_("%s: smudge filter %s failed"), path
, ca
->drv
->name
);
1495 return ret
| ret_filter
;
1498 int async_convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1499 const char *path
, const char *src
,
1500 size_t len
, struct strbuf
*dst
,
1501 const struct checkout_metadata
*meta
,
1504 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1508 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1509 const char *path
, const char *src
,
1510 size_t len
, struct strbuf
*dst
,
1511 const struct checkout_metadata
*meta
)
1513 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1517 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1518 const char *src
, size_t len
, struct strbuf
*dst
)
1520 struct conv_attrs ca
;
1523 convert_attrs(istate
, &ca
, path
);
1524 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1530 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1533 /*****************************************************************
1535 * Streaming conversion support
1537 *****************************************************************/
1539 typedef int (*filter_fn
)(struct stream_filter
*,
1540 const char *input
, size_t *isize_p
,
1541 char *output
, size_t *osize_p
);
1542 typedef void (*free_fn
)(struct stream_filter
*);
1544 struct stream_filter_vtbl
{
1549 struct stream_filter
{
1550 struct stream_filter_vtbl
*vtbl
;
1553 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1554 const char *input
, size_t *isize_p
,
1555 char *output
, size_t *osize_p
)
1560 return 0; /* we do not keep any states */
1562 if (*osize_p
< count
)
1565 memmove(output
, input
, count
);
1572 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1574 ; /* nothing -- null instances are shared */
1577 static struct stream_filter_vtbl null_vtbl
= {
1578 .filter
= null_filter_fn
,
1579 .free
= null_free_fn
,
1582 static struct stream_filter null_filter_singleton
= {
1586 int is_null_stream_filter(struct stream_filter
*filter
)
1588 return filter
== &null_filter_singleton
;
1596 struct lf_to_crlf_filter
{
1597 struct stream_filter filter
;
1598 unsigned has_held
:1;
1602 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1603 const char *input
, size_t *isize_p
,
1604 char *output
, size_t *osize_p
)
1606 size_t count
, o
= 0;
1607 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1610 * We may be holding onto the CR to see if it is followed by a
1611 * LF, in which case we would need to go to the main loop.
1612 * Otherwise, just emit it to the output stream.
1614 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1615 output
[o
++] = lf_to_crlf
->held
;
1616 lf_to_crlf
->has_held
= 0;
1619 /* We are told to drain */
1626 if (count
|| lf_to_crlf
->has_held
) {
1630 if (lf_to_crlf
->has_held
) {
1632 lf_to_crlf
->has_held
= 0;
1635 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1640 } else if (was_cr
) {
1642 * Previous round saw CR and it is not followed
1643 * by a LF; emit the CR before processing the
1644 * current character.
1650 * We may have consumed the last output slot,
1651 * in which case we need to break out of this
1652 * loop; hold the current character before
1655 if (*osize_p
<= o
) {
1656 lf_to_crlf
->has_held
= 1;
1657 lf_to_crlf
->held
= ch
;
1658 continue; /* break but increment i */
1673 if (!lf_to_crlf
->has_held
&& was_cr
) {
1674 lf_to_crlf
->has_held
= 1;
1675 lf_to_crlf
->held
= '\r';
1681 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1686 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1687 .filter
= lf_to_crlf_filter_fn
,
1688 .free
= lf_to_crlf_free_fn
,
1691 static struct stream_filter
*lf_to_crlf_filter(void)
1693 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1695 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1696 return (struct stream_filter
*)lf_to_crlf
;
1702 #define FILTER_BUFFER 1024
1703 struct cascade_filter
{
1704 struct stream_filter filter
;
1705 struct stream_filter
*one
;
1706 struct stream_filter
*two
;
1707 char buf
[FILTER_BUFFER
];
1711 static int cascade_filter_fn(struct stream_filter
*filter
,
1712 const char *input
, size_t *isize_p
,
1713 char *output
, size_t *osize_p
)
1715 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1717 size_t sz
= *osize_p
;
1718 size_t to_feed
, remaining
;
1721 * input -- (one) --> buf -- (two) --> output
1723 while (filled
< sz
) {
1724 remaining
= sz
- filled
;
1726 /* do we already have something to feed two with? */
1727 if (cas
->ptr
< cas
->end
) {
1728 to_feed
= cas
->end
- cas
->ptr
;
1729 if (stream_filter(cas
->two
,
1730 cas
->buf
+ cas
->ptr
, &to_feed
,
1731 output
+ filled
, &remaining
))
1733 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1734 filled
= sz
- remaining
;
1738 /* feed one from upstream and have it emit into our buffer */
1739 to_feed
= input
? *isize_p
: 0;
1740 if (input
&& !to_feed
)
1742 remaining
= sizeof(cas
->buf
);
1743 if (stream_filter(cas
->one
,
1745 cas
->buf
, &remaining
))
1747 cas
->end
= sizeof(cas
->buf
) - remaining
;
1750 size_t fed
= *isize_p
- to_feed
;
1755 /* do we know that we drained one completely? */
1756 if (input
|| cas
->end
)
1759 /* tell two to drain; we have nothing more to give it */
1761 remaining
= sz
- filled
;
1762 if (stream_filter(cas
->two
,
1764 output
+ filled
, &remaining
))
1766 if (remaining
== (sz
- filled
))
1767 break; /* completely drained two */
1768 filled
= sz
- remaining
;
1774 static void cascade_free_fn(struct stream_filter
*filter
)
1776 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1777 free_stream_filter(cas
->one
);
1778 free_stream_filter(cas
->two
);
1782 static struct stream_filter_vtbl cascade_vtbl
= {
1783 .filter
= cascade_filter_fn
,
1784 .free
= cascade_free_fn
,
1787 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1788 struct stream_filter
*two
)
1790 struct cascade_filter
*cascade
;
1792 if (!one
|| is_null_stream_filter(one
))
1794 if (!two
|| is_null_stream_filter(two
))
1797 cascade
= xmalloc(sizeof(*cascade
));
1800 cascade
->end
= cascade
->ptr
= 0;
1801 cascade
->filter
.vtbl
= &cascade_vtbl
;
1802 return (struct stream_filter
*)cascade
;
1808 #define IDENT_DRAINING (-1)
1809 #define IDENT_SKIPPING (-2)
1810 struct ident_filter
{
1811 struct stream_filter filter
;
1814 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1817 static int is_foreign_ident(const char *str
)
1821 if (!skip_prefix(str
, "$Id: ", &str
))
1823 for (i
= 0; str
[i
]; i
++) {
1824 if (isspace(str
[i
]) && str
[i
+1] != '$')
1830 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1832 size_t to_drain
= ident
->left
.len
;
1834 if (*osize_p
< to_drain
)
1835 to_drain
= *osize_p
;
1837 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1838 strbuf_remove(&ident
->left
, 0, to_drain
);
1839 *output_p
+= to_drain
;
1840 *osize_p
-= to_drain
;
1842 if (!ident
->left
.len
)
1846 static int ident_filter_fn(struct stream_filter
*filter
,
1847 const char *input
, size_t *isize_p
,
1848 char *output
, size_t *osize_p
)
1850 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1851 static const char head
[] = "$Id";
1854 /* drain upon eof */
1855 switch (ident
->state
) {
1857 strbuf_add(&ident
->left
, head
, ident
->state
);
1859 case IDENT_SKIPPING
:
1861 case IDENT_DRAINING
:
1862 ident_drain(ident
, &output
, osize_p
);
1867 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1870 if (ident
->state
== IDENT_DRAINING
) {
1871 ident_drain(ident
, &output
, osize_p
);
1880 if (ident
->state
== IDENT_SKIPPING
) {
1882 * Skipping until '$' or LF, but keeping them
1883 * in case it is a foreign ident.
1885 strbuf_addch(&ident
->left
, ch
);
1886 if (ch
!= '\n' && ch
!= '$')
1888 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1889 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1890 strbuf_addstr(&ident
->left
, ident
->ident
);
1892 ident
->state
= IDENT_DRAINING
;
1896 if (ident
->state
< sizeof(head
) &&
1897 head
[ident
->state
] == ch
) {
1903 strbuf_add(&ident
->left
, head
, ident
->state
);
1904 if (ident
->state
== sizeof(head
) - 1) {
1905 if (ch
!= ':' && ch
!= '$') {
1906 strbuf_addch(&ident
->left
, ch
);
1912 strbuf_addch(&ident
->left
, ch
);
1913 ident
->state
= IDENT_SKIPPING
;
1915 strbuf_addstr(&ident
->left
, ident
->ident
);
1916 ident
->state
= IDENT_DRAINING
;
1921 strbuf_addch(&ident
->left
, ch
);
1922 ident
->state
= IDENT_DRAINING
;
1927 static void ident_free_fn(struct stream_filter
*filter
)
1929 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1930 strbuf_release(&ident
->left
);
1934 static struct stream_filter_vtbl ident_vtbl
= {
1935 .filter
= ident_filter_fn
,
1936 .free
= ident_free_fn
,
1939 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1941 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1943 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1944 ": %s $", oid_to_hex(oid
));
1945 strbuf_init(&ident
->left
, 0);
1946 ident
->filter
.vtbl
= &ident_vtbl
;
1948 return (struct stream_filter
*)ident
;
1952 * Return an appropriately constructed filter for the given ca, or NULL if
1953 * the contents cannot be filtered without reading the whole thing
1956 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1957 * large binary blob you would want us not to slurp into the memory!
1959 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1960 const struct object_id
*oid
)
1962 struct stream_filter
*filter
= NULL
;
1964 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
1968 filter
= ident_filter(oid
);
1970 if (output_eol(ca
->crlf_action
) == EOL_CRLF
)
1971 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1973 filter
= cascade_filter(filter
, &null_filter_singleton
);
1978 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
1980 const struct object_id
*oid
)
1982 struct conv_attrs ca
;
1983 convert_attrs(istate
, &ca
, path
);
1984 return get_stream_filter_ca(&ca
, oid
);
1987 void free_stream_filter(struct stream_filter
*filter
)
1989 filter
->vtbl
->free(filter
);
1992 int stream_filter(struct stream_filter
*filter
,
1993 const char *input
, size_t *isize_p
,
1994 char *output
, size_t *osize_p
)
1996 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
1999 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
2000 const struct object_id
*treeish
,
2001 const struct object_id
*blob
)
2003 memset(meta
, 0, sizeof(*meta
));
2005 meta
->refname
= refname
;
2007 oidcpy(&meta
->treeish
, treeish
);
2009 oidcpy(&meta
->blob
, blob
);
2012 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2013 const struct checkout_metadata
*src
,
2014 const struct object_id
*blob
)
2016 memcpy(dst
, src
, sizeof(*dst
));
2018 oidcpy(&dst
->blob
, blob
);
2021 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2024 if (ca
->drv
->process
)
2025 return CA_CLASS_INCORE_PROCESS
;
2026 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2027 return CA_CLASS_INCORE_FILTER
;
2030 if (ca
->working_tree_encoding
)
2031 return CA_CLASS_INCORE
;
2033 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2034 return CA_CLASS_INCORE
;
2036 return CA_CLASS_STREAMABLE
;