5 #include "object-store.h"
7 #include "run-command.h"
11 #include "sub-process.h"
17 * convert.c - convert a file when checking it out and checking it in.
19 * This should use the pathname to decide on whether it wants to do some
20 * more interesting conversions (automatic gzip/unzip, general format
21 * conversions etc etc), but by default it just does automatic CRLF<->LF
22 * translation when the "text" attribute or "auto_crlf" option is set.
25 /* Stat bits: When BIN is set, the txt bits are unset */
26 #define CONVERT_STAT_BITS_TXT_LF 0x1
27 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
28 #define CONVERT_STAT_BITS_BIN 0x4
31 /* NUL, CR, LF and CRLF counts */
32 unsigned nul
, lonecr
, lonelf
, crlf
;
34 /* These are just approximations! */
35 unsigned printable
, nonprintable
;
38 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
42 memset(stats
, 0, sizeof(*stats
));
44 for (i
= 0; i
< size
; i
++) {
45 unsigned char c
= buf
[i
];
47 if (i
+1 < size
&& buf
[i
+1] == '\n') {
60 stats
->nonprintable
++;
63 /* BS, HT, ESC and FF */
64 case '\b': case '\t': case '\033': case '\014':
71 stats
->nonprintable
++;
78 /* If file ends with EOF then don't count this EOF as non-printable. */
79 if (size
>= 1 && buf
[size
-1] == '\032')
80 stats
->nonprintable
--;
84 * The same heuristics as diff.c::mmfile_is_binary()
85 * We treat files with bare CR as binary
87 static int convert_is_binary(const struct text_stat
*stats
)
93 if ((stats
->printable
>> 7) < stats
->nonprintable
)
98 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
100 struct text_stat stats
;
104 gather_stats(data
, size
, &stats
);
105 if (convert_is_binary(&stats
))
106 ret
|= CONVERT_STAT_BITS_BIN
;
108 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
110 ret
|= CONVERT_STAT_BITS_TXT_LF
;
115 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
117 unsigned int convert_stats
= gather_convert_stats(data
, size
);
119 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
121 switch (convert_stats
) {
122 case CONVERT_STAT_BITS_TXT_LF
:
124 case CONVERT_STAT_BITS_TXT_CRLF
:
126 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
133 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
138 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
139 ret
= gather_convert_stats_ascii(data
, sz
);
144 const char *get_wt_convert_stats_ascii(const char *path
)
146 const char *ret
= "";
147 struct strbuf sb
= STRBUF_INIT
;
148 if (strbuf_read_file(&sb
, path
, 0) >= 0)
149 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
154 static int text_eol_is_crlf(void)
156 if (auto_crlf
== AUTO_CRLF_TRUE
)
158 else if (auto_crlf
== AUTO_CRLF_INPUT
)
160 if (core_eol
== EOL_CRLF
)
162 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
167 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
169 switch (crlf_action
) {
174 case CRLF_TEXT_INPUT
:
179 case CRLF_AUTO_INPUT
:
184 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
186 warning(_("illegal crlf_action %d"), (int)crlf_action
);
190 static void check_global_conv_flags_eol(const char *path
,
191 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
194 if (old_stats
->crlf
&& !new_stats
->crlf
) {
196 * CRLFs would not be restored by checkout
198 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
199 die(_("CRLF would be replaced by LF in %s"), path
);
200 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
201 warning(_("in the working copy of '%s', CRLF will be"
202 " replaced by LF the next time Git touches"
204 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
206 * CRLFs would be added by checkout
208 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
209 die(_("LF would be replaced by CRLF in %s"), path
);
210 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
211 warning(_("in the working copy of '%s', LF will be"
212 " replaced by CRLF the next time Git touches"
217 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
224 data
= read_blob_data_from_index(istate
, path
, &sz
);
228 crp
= memchr(data
, '\r', sz
);
230 unsigned int ret_stats
;
231 ret_stats
= gather_convert_stats(data
, sz
);
232 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
233 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
240 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
241 enum convert_crlf_action crlf_action
)
243 if (output_eol(crlf_action
) != EOL_CRLF
)
245 /* No "naked" LF? Nothing to convert, regardless. */
249 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
250 /* If we have any CR or CRLF line endings, we do not touch it */
251 /* This is the new safer autocrlf-handling */
252 if (stats
->lonecr
|| stats
->crlf
)
255 if (convert_is_binary(stats
))
262 static int validate_encoding(const char *path
, const char *enc
,
263 const char *data
, size_t len
, int die_on_error
)
265 const char *stripped
;
267 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
268 if (skip_iprefix(enc
, "UTF", &stripped
)) {
269 skip_prefix(stripped
, "-", &stripped
);
272 * Check for detectable errors in UTF encodings
274 if (has_prohibited_utf_bom(enc
, data
, len
)) {
275 const char *error_msg
= _(
276 "BOM is prohibited in '%s' if encoded as %s");
278 * This advice is shown for UTF-??BE and UTF-??LE encodings.
279 * We cut off the last two characters of the encoding name
280 * to generate the encoding name suitable for BOMs.
282 const char *advise_msg
= _(
283 "The file '%s' contains a byte order "
284 "mark (BOM). Please use UTF-%.*s as "
285 "working-tree-encoding.");
286 int stripped_len
= strlen(stripped
) - strlen("BE");
287 advise(advise_msg
, path
, stripped_len
, stripped
);
289 die(error_msg
, path
, enc
);
291 return error(error_msg
, path
, enc
);
294 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
295 const char *error_msg
= _(
296 "BOM is required in '%s' if encoded as %s");
297 const char *advise_msg
= _(
298 "The file '%s' is missing a byte order "
299 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
300 "(depending on the byte order) as "
301 "working-tree-encoding.");
302 advise(advise_msg
, path
, stripped
, stripped
);
304 die(error_msg
, path
, enc
);
306 return error(error_msg
, path
, enc
);
314 static void trace_encoding(const char *context
, const char *path
,
315 const char *encoding
, const char *buf
, size_t len
)
317 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
318 struct strbuf trace
= STRBUF_INIT
;
321 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
322 for (i
= 0; i
< len
&& buf
; ++i
) {
324 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
326 (unsigned char) buf
[i
],
327 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
328 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
331 strbuf_addchars(&trace
, '\n', 1);
333 trace_strbuf(&coe
, &trace
);
334 strbuf_release(&trace
);
337 static int check_roundtrip(const char *enc_name
)
340 * check_roundtrip_encoding contains a string of comma and/or
341 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
342 * Search for the given encoding in that string.
344 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
349 next
= found
+ strlen(enc_name
);
350 len
= strlen(check_roundtrip_encoding
);
353 * check that the found encoding is at the
354 * beginning of check_roundtrip_encoding or
355 * that it is prefixed with a space or comma
357 found
== check_roundtrip_encoding
|| (
358 (isspace(found
[-1]) || found
[-1] == ',')
362 * check that the found encoding is at the
363 * end of check_roundtrip_encoding or
364 * that it is suffixed with a space or comma
366 next
== check_roundtrip_encoding
+ len
|| (
367 next
< check_roundtrip_encoding
+ len
&&
368 (isspace(next
[0]) || next
[0] == ',')
373 static const char *default_encoding
= "UTF-8";
375 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
376 struct strbuf
*buf
, const char *enc
, int conv_flags
)
380 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
383 * No encoding is specified or there is nothing to encode.
384 * Tell the caller that the content was not modified.
386 if (!enc
|| (src
&& !src_len
))
390 * Looks like we got called from "would_convert_to_git()".
391 * This means Git wants to know if it would encode (= modify!)
392 * the content. Let's answer with "yes", since an encoding was
398 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
401 trace_encoding("source", path
, enc
, src
, src_len
);
402 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
406 * We could add the blob "as-is" to Git. However, on checkout
407 * we would try to re-encode to the original encoding. This
408 * would fail and we would leave the user with a messed-up
409 * working tree. Let's try to avoid this by screaming loud.
411 const char* msg
= _("failed to encode '%s' from %s to %s");
413 die(msg
, path
, enc
, default_encoding
);
415 error(msg
, path
, enc
, default_encoding
);
419 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
422 * UTF supports lossless conversion round tripping [1] and conversions
423 * between UTF and other encodings are mostly round trip safe as
424 * Unicode aims to be a superset of all other character encodings.
425 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
426 * trip issues [2]. Check the round trip conversion for all encodings
427 * listed in core.checkRoundtripEncoding.
429 * The round trip check is only performed if content is written to Git.
430 * This ensures that no information is lost during conversion to/from
431 * the internal UTF-8 representation.
433 * Please note, the code below is not tested because I was not able to
434 * generate a faulty round trip without an iconv error. Iconv errors
435 * are already caught above.
437 * [1] http://unicode.org/faq/utf_bom.html#gen2
438 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
440 if (die_on_error
&& check_roundtrip(enc
)) {
444 re_src
= reencode_string_len(dst
, dst_len
,
445 enc
, default_encoding
,
448 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
449 trace_encoding("reencoded source", path
, enc
,
452 if (!re_src
|| src_len
!= re_src_len
||
453 memcmp(src
, re_src
, src_len
)) {
454 const char* msg
= _("encoding '%s' from %s to %s and "
455 "back is not the same");
456 die(msg
, path
, enc
, default_encoding
);
462 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
466 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
467 struct strbuf
*buf
, const char *enc
)
473 * No encoding is specified or there is nothing to encode.
474 * Tell the caller that the content was not modified.
476 if (!enc
|| (src
&& !src_len
))
479 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
482 error(_("failed to encode '%s' from %s to %s"),
483 path
, default_encoding
, enc
);
487 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
491 static int crlf_to_git(struct index_state
*istate
,
492 const char *path
, const char *src
, size_t len
,
494 enum convert_crlf_action crlf_action
, int conv_flags
)
496 struct text_stat stats
;
498 int convert_crlf_into_lf
;
500 if (crlf_action
== CRLF_BINARY
||
505 * If we are doing a dry-run and have no source buffer, there is
506 * nothing to analyze; we must assume we would convert.
511 gather_stats(src
, len
, &stats
);
512 /* Optimization: No CRLF? Nothing to convert, regardless. */
513 convert_crlf_into_lf
= !!stats
.crlf
;
515 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
516 if (convert_is_binary(&stats
))
519 * If the file in the index has any CR in it, do not
520 * convert. This is the new safer autocrlf handling,
521 * unless we want to renormalize in a merge or
524 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
525 has_crlf_in_index(istate
, path
))
526 convert_crlf_into_lf
= 0;
528 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
529 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
530 struct text_stat new_stats
;
531 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
532 /* simulate "git add" */
533 if (convert_crlf_into_lf
) {
534 new_stats
.lonelf
+= new_stats
.crlf
;
537 /* simulate "git checkout" */
538 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
539 new_stats
.crlf
+= new_stats
.lonelf
;
540 new_stats
.lonelf
= 0;
542 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
544 if (!convert_crlf_into_lf
)
548 * At this point all of our source analysis is done, and we are sure we
549 * would convert. If we are in dry-run mode, we can give an answer.
554 /* only grow if not in place */
555 if (strbuf_avail(buf
) + buf
->len
< len
)
556 strbuf_grow(buf
, len
- buf
->len
);
558 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
560 * If we guessed, we already know we rejected a file with
561 * lone CR, and we can strip a CR without looking at what
565 unsigned char c
= *src
++;
571 unsigned char c
= *src
++;
572 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
576 strbuf_setlen(buf
, dst
- buf
->buf
);
580 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
581 enum convert_crlf_action crlf_action
)
583 char *to_free
= NULL
;
584 struct text_stat stats
;
586 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
589 gather_stats(src
, len
, &stats
);
590 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
593 /* are we "faking" in place editing ? */
595 to_free
= strbuf_detach(buf
, NULL
);
597 strbuf_grow(buf
, len
+ stats
.lonelf
);
599 const char *nl
= memchr(src
, '\n', len
);
602 if (nl
> src
&& nl
[-1] == '\r') {
603 strbuf_add(buf
, src
, nl
+ 1 - src
);
605 strbuf_add(buf
, src
, nl
- src
);
606 strbuf_addstr(buf
, "\r\n");
611 strbuf_add(buf
, src
, len
);
617 struct filter_params
{
625 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
628 * Spawn cmd and feed the buffer contents through its stdin.
630 struct child_process child_process
= CHILD_PROCESS_INIT
;
631 struct filter_params
*params
= (struct filter_params
*)data
;
632 int write_err
, status
;
634 /* apply % substitution to cmd */
635 struct strbuf cmd
= STRBUF_INIT
;
636 struct strbuf path
= STRBUF_INIT
;
637 struct strbuf_expand_dict_entry dict
[] = {
642 /* quote the path to preserve spaces, etc. */
643 sq_quote_buf(&path
, params
->path
);
644 dict
[0].value
= path
.buf
;
646 /* expand all %f with the quoted path */
647 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
648 strbuf_release(&path
);
650 strvec_push(&child_process
.args
, cmd
.buf
);
651 child_process
.use_shell
= 1;
652 child_process
.in
= -1;
653 child_process
.out
= out
;
655 if (start_command(&child_process
)) {
656 strbuf_release(&cmd
);
657 return error(_("cannot fork to run external filter '%s'"),
661 sigchain_push(SIGPIPE
, SIG_IGN
);
664 write_err
= (write_in_full(child_process
.in
,
665 params
->src
, params
->size
) < 0);
669 write_err
= copy_fd(params
->fd
, child_process
.in
);
670 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
674 if (close(child_process
.in
))
677 error(_("cannot feed the input to external filter '%s'"),
680 sigchain_pop(SIGPIPE
);
682 status
= finish_command(&child_process
);
684 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
686 strbuf_release(&cmd
);
687 return (write_err
|| status
);
690 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
691 struct strbuf
*dst
, const char *cmd
)
694 * Create a pipeline to have the command filter the buffer's
697 * (child --> cmd) --> us
700 struct strbuf nbuf
= STRBUF_INIT
;
702 struct filter_params params
;
704 memset(&async
, 0, sizeof(async
));
705 async
.proc
= filter_buffer_or_fd
;
706 async
.data
= ¶ms
;
715 if (start_async(&async
))
716 return 0; /* error was already reported */
718 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
719 err
= error(_("read from external filter '%s' failed"), cmd
);
721 if (close(async
.out
)) {
722 err
= error(_("read from external filter '%s' failed"), cmd
);
724 if (finish_async(&async
)) {
725 err
= error(_("external filter '%s' failed"), cmd
);
729 strbuf_swap(dst
, &nbuf
);
731 strbuf_release(&nbuf
);
735 #define CAP_CLEAN (1u<<0)
736 #define CAP_SMUDGE (1u<<1)
737 #define CAP_DELAY (1u<<2)
740 struct subprocess_entry subprocess
; /* must be the first member! */
741 unsigned int supported_capabilities
;
744 static int subprocess_map_initialized
;
745 static struct hashmap subprocess_map
;
747 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
749 static int versions
[] = {2, 0};
750 static struct subprocess_capability capabilities
[] = {
751 { "clean", CAP_CLEAN
},
752 { "smudge", CAP_SMUDGE
},
753 { "delay", CAP_DELAY
},
756 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
757 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
759 &entry
->supported_capabilities
);
762 static void handle_filter_error(const struct strbuf
*filter_status
,
763 struct cmd2process
*entry
,
764 const unsigned int wanted_capability
)
766 if (!strcmp(filter_status
->buf
, "error"))
767 ; /* The filter signaled a problem with the file. */
768 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
770 * The filter signaled a permanent problem. Don't try to filter
771 * files with the same command for the lifetime of the current
774 entry
->supported_capabilities
&= ~wanted_capability
;
777 * Something went wrong with the protocol filter.
778 * Force shutdown and restart if another blob requires filtering.
780 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
781 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
786 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
787 int fd
, struct strbuf
*dst
, const char *cmd
,
788 const unsigned int wanted_capability
,
789 const struct checkout_metadata
*meta
,
790 struct delayed_checkout
*dco
)
794 struct cmd2process
*entry
;
795 struct child_process
*process
;
796 struct strbuf nbuf
= STRBUF_INIT
;
797 struct strbuf filter_status
= STRBUF_INIT
;
798 const char *filter_type
;
800 if (!subprocess_map_initialized
) {
801 subprocess_map_initialized
= 1;
802 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
805 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
811 entry
= xmalloc(sizeof(*entry
));
812 entry
->supported_capabilities
= 0;
814 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
819 process
= &entry
->subprocess
.process
;
821 if (!(entry
->supported_capabilities
& wanted_capability
))
824 if (wanted_capability
& CAP_CLEAN
)
825 filter_type
= "clean";
826 else if (wanted_capability
& CAP_SMUDGE
)
827 filter_type
= "smudge";
829 die(_("unexpected filter type"));
831 sigchain_push(SIGPIPE
, SIG_IGN
);
833 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
834 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
838 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
840 error(_("path name too long for external filter"));
844 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
848 if (meta
&& meta
->refname
) {
849 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
854 if (meta
&& !is_null_oid(&meta
->treeish
)) {
855 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
860 if (meta
&& !is_null_oid(&meta
->blob
)) {
861 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
866 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
867 dco
&& dco
->state
== CE_CAN_DELAY
) {
869 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
874 err
= packet_flush_gently(process
->in
);
879 err
= write_packetized_from_fd_no_flush(fd
, process
->in
);
881 err
= write_packetized_from_buf_no_flush(src
, len
, process
->in
);
885 err
= packet_flush_gently(process
->in
);
889 err
= subprocess_read_status(process
->out
, &filter_status
);
893 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
894 string_list_insert(&dco
->filters
, cmd
);
895 string_list_insert(&dco
->paths
, path
);
897 /* The filter got the blob and wants to send us a response. */
898 err
= strcmp(filter_status
.buf
, "success");
902 err
= read_packetized_to_strbuf(process
->out
, &nbuf
,
903 PACKET_READ_GENTLE_ON_EOF
) < 0;
907 err
= subprocess_read_status(process
->out
, &filter_status
);
911 err
= strcmp(filter_status
.buf
, "success");
915 sigchain_pop(SIGPIPE
);
918 handle_filter_error(&filter_status
, entry
, wanted_capability
);
920 strbuf_swap(dst
, &nbuf
);
921 strbuf_release(&nbuf
);
922 strbuf_release(&filter_status
);
927 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
931 struct cmd2process
*entry
;
932 struct child_process
*process
;
933 struct strbuf filter_status
= STRBUF_INIT
;
935 assert(subprocess_map_initialized
);
936 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
938 error(_("external filter '%s' is not available anymore although "
939 "not all paths have been filtered"), cmd
);
942 process
= &entry
->subprocess
.process
;
943 sigchain_push(SIGPIPE
, SIG_IGN
);
945 err
= packet_write_fmt_gently(
946 process
->in
, "command=list_available_blobs\n");
950 err
= packet_flush_gently(process
->in
);
954 while ((line
= packet_read_line(process
->out
, NULL
))) {
956 if (skip_prefix(line
, "pathname=", &path
))
957 string_list_insert(available_paths
, xstrdup(path
));
959 ; /* ignore unknown keys */
962 err
= subprocess_read_status(process
->out
, &filter_status
);
966 err
= strcmp(filter_status
.buf
, "success");
969 sigchain_pop(SIGPIPE
);
972 handle_filter_error(&filter_status
, entry
, 0);
973 strbuf_release(&filter_status
);
977 static struct convert_driver
{
979 struct convert_driver
*next
;
984 } *user_convert
, **user_convert_tail
;
986 static int apply_filter(const char *path
, const char *src
, size_t len
,
987 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
988 const unsigned int wanted_capability
,
989 const struct checkout_metadata
*meta
,
990 struct delayed_checkout
*dco
)
992 const char *cmd
= NULL
;
1000 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
1002 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1006 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1007 else if (drv
->process
&& *drv
->process
)
1008 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1009 drv
->process
, wanted_capability
, meta
, dco
);
1014 static int read_convert_config(const char *var
, const char *value
, void *cb UNUSED
)
1016 const char *key
, *name
;
1018 struct convert_driver
*drv
;
1021 * External conversion drivers are configured using
1022 * "filter.<name>.variable".
1024 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1026 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1027 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1030 CALLOC_ARRAY(drv
, 1);
1031 drv
->name
= xmemdupz(name
, namelen
);
1032 *user_convert_tail
= drv
;
1033 user_convert_tail
= &(drv
->next
);
1037 * filter.<name>.smudge and filter.<name>.clean specifies
1042 * The command-line will not be interpolated in any way.
1045 if (!strcmp("smudge", key
))
1046 return git_config_string(&drv
->smudge
, var
, value
);
1048 if (!strcmp("clean", key
))
1049 return git_config_string(&drv
->clean
, var
, value
);
1051 if (!strcmp("process", key
))
1052 return git_config_string(&drv
->process
, var
, value
);
1054 if (!strcmp("required", key
)) {
1055 drv
->required
= git_config_bool(var
, value
);
1062 static int count_ident(const char *cp
, unsigned long size
)
1065 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1077 if (memcmp("Id", cp
, 2))
1088 * "$Id: ... "; scan up to the closing dollar sign and discard.
1104 static int ident_to_git(const char *src
, size_t len
,
1105 struct strbuf
*buf
, int ident
)
1109 if (!ident
|| (src
&& !count_ident(src
, len
)))
1115 /* only grow if not in place */
1116 if (strbuf_avail(buf
) + buf
->len
< len
)
1117 strbuf_grow(buf
, len
- buf
->len
);
1120 dollar
= memchr(src
, '$', len
);
1123 memmove(dst
, src
, dollar
+ 1 - src
);
1124 dst
+= dollar
+ 1 - src
;
1125 len
-= dollar
+ 1 - src
;
1128 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1129 dollar
= memchr(src
+ 3, '$', len
- 3);
1132 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1133 /* Line break before the next dollar. */
1137 memcpy(dst
, "Id$", 3);
1139 len
-= dollar
+ 1 - src
;
1143 memmove(dst
, src
, len
);
1144 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1148 static int ident_to_worktree(const char *src
, size_t len
,
1149 struct strbuf
*buf
, int ident
)
1151 struct object_id oid
;
1152 char *to_free
= NULL
, *dollar
, *spc
;
1158 cnt
= count_ident(src
, len
);
1162 /* are we "faking" in place editing ? */
1163 if (src
== buf
->buf
)
1164 to_free
= strbuf_detach(buf
, NULL
);
1165 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1167 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1169 /* step 1: run to the next '$' */
1170 dollar
= memchr(src
, '$', len
);
1173 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1174 len
-= dollar
+ 1 - src
;
1177 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1178 if (len
< 3 || memcmp("Id", src
, 2))
1181 /* step 3: skip over Id$ or Id:xxxxx$ */
1182 if (src
[2] == '$') {
1185 } else if (src
[2] == ':') {
1187 * It's possible that an expanded Id has crept its way into the
1188 * repository, we cope with that by stripping the expansion out.
1189 * This is probably not a good idea, since it will cause changes
1190 * on checkout, which won't go away by stash, but let's keep it
1191 * for git-style ids.
1193 dollar
= memchr(src
+ 3, '$', len
- 3);
1195 /* incomplete keyword, no more '$', so just quit the loop */
1199 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1200 /* Line break before the next dollar. */
1204 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1205 if (spc
&& spc
< dollar
-1) {
1206 /* There are spaces in unexpected places.
1207 * This is probably an id from some other
1208 * versioning system. Keep it for now.
1213 len
-= dollar
+ 1 - src
;
1216 /* it wasn't a "Id$" or "Id:xxxx$" */
1220 /* step 4: substitute */
1221 strbuf_addstr(buf
, "Id: ");
1222 strbuf_addstr(buf
, oid_to_hex(&oid
));
1223 strbuf_addstr(buf
, " $");
1225 strbuf_add(buf
, src
, len
);
1231 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1233 const char *value
= check
->value
;
1235 if (ATTR_UNSET(value
) || !strlen(value
))
1238 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1239 die(_("true/false are no valid working-tree-encodings"));
1242 /* Don't encode to the default encoding */
1243 if (same_encoding(value
, default_encoding
))
1249 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1251 const char *value
= check
->value
;
1253 if (ATTR_TRUE(value
))
1255 else if (ATTR_FALSE(value
))
1257 else if (ATTR_UNSET(value
))
1259 else if (!strcmp(value
, "input"))
1260 return CRLF_TEXT_INPUT
;
1261 else if (!strcmp(value
, "auto"))
1263 return CRLF_UNDEFINED
;
1266 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1268 const char *value
= check
->value
;
1270 if (ATTR_UNSET(value
))
1272 else if (!strcmp(value
, "lf"))
1274 else if (!strcmp(value
, "crlf"))
1279 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1281 const char *value
= check
->value
;
1282 struct convert_driver
*drv
;
1284 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1286 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1287 if (!strcmp(value
, drv
->name
))
1292 static int git_path_check_ident(struct attr_check_item
*check
)
1294 const char *value
= check
->value
;
1296 return !!ATTR_TRUE(value
);
1299 static struct attr_check
*check
;
1301 void convert_attrs(struct index_state
*istate
,
1302 struct conv_attrs
*ca
, const char *path
)
1304 struct attr_check_item
*ccheck
= NULL
;
1307 check
= attr_check_initl("crlf", "ident", "filter",
1308 "eol", "text", "working-tree-encoding",
1310 user_convert_tail
= &user_convert
;
1311 git_config(read_convert_config
, NULL
);
1314 git_check_attr(istate
, NULL
, path
, check
);
1315 ccheck
= check
->items
;
1316 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1317 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1318 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1319 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1320 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1321 if (ca
->crlf_action
!= CRLF_BINARY
) {
1322 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1323 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1324 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1325 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1326 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1327 else if (eol_attr
== EOL_LF
)
1328 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1329 else if (eol_attr
== EOL_CRLF
)
1330 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1332 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1334 /* Save attr and make a decision for action */
1335 ca
->attr_action
= ca
->crlf_action
;
1336 if (ca
->crlf_action
== CRLF_TEXT
)
1337 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1338 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1339 ca
->crlf_action
= CRLF_BINARY
;
1340 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1341 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1342 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1343 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1346 void reset_parsed_attributes(void)
1348 struct convert_driver
*drv
, *next
;
1350 attr_check_free(check
);
1352 reset_merge_attributes();
1354 for (drv
= user_convert
; drv
; drv
= next
) {
1356 free((void *)drv
->name
);
1359 user_convert
= NULL
;
1360 user_convert_tail
= NULL
;
1363 int would_convert_to_git_filter_fd(struct index_state
*istate
, const char *path
)
1365 struct conv_attrs ca
;
1367 convert_attrs(istate
, &ca
, path
);
1372 * Apply a filter to an fd only if the filter is required to succeed.
1373 * We must die if the filter fails, because the original data before
1374 * filtering is not available.
1376 if (!ca
.drv
->required
)
1379 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1382 const char *get_convert_attr_ascii(struct index_state
*istate
, const char *path
)
1384 struct conv_attrs ca
;
1386 convert_attrs(istate
, &ca
, path
);
1387 switch (ca
.attr_action
) {
1388 case CRLF_UNDEFINED
:
1394 case CRLF_TEXT_INPUT
:
1395 return "text eol=lf";
1396 case CRLF_TEXT_CRLF
:
1397 return "text eol=crlf";
1400 case CRLF_AUTO_CRLF
:
1401 return "text=auto eol=crlf";
1402 case CRLF_AUTO_INPUT
:
1403 return "text=auto eol=lf";
1408 int convert_to_git(struct index_state
*istate
,
1409 const char *path
, const char *src
, size_t len
,
1410 struct strbuf
*dst
, int conv_flags
)
1413 struct conv_attrs ca
;
1415 convert_attrs(istate
, &ca
, path
);
1417 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1418 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1419 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1426 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1432 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1433 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1439 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1442 void convert_to_git_filter_fd(struct index_state
*istate
,
1443 const char *path
, int fd
, struct strbuf
*dst
,
1446 struct conv_attrs ca
;
1447 convert_attrs(istate
, &ca
, path
);
1451 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1452 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1454 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1455 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1456 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1459 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1460 const char *path
, const char *src
,
1461 size_t len
, struct strbuf
*dst
,
1463 const struct checkout_metadata
*meta
,
1464 struct delayed_checkout
*dco
)
1466 int ret
= 0, ret_filter
= 0;
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
, meta
, 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_ca(const struct conv_attrs
*ca
,
1501 const char *path
, const char *src
,
1502 size_t len
, struct strbuf
*dst
,
1503 const struct checkout_metadata
*meta
,
1506 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1510 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1511 const char *path
, const char *src
,
1512 size_t len
, struct strbuf
*dst
,
1513 const struct checkout_metadata
*meta
)
1515 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1519 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1520 const char *src
, size_t len
, struct strbuf
*dst
)
1522 struct conv_attrs ca
;
1525 convert_attrs(istate
, &ca
, path
);
1526 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1532 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1535 /*****************************************************************
1537 * Streaming conversion support
1539 *****************************************************************/
1541 typedef int (*filter_fn
)(struct stream_filter
*,
1542 const char *input
, size_t *isize_p
,
1543 char *output
, size_t *osize_p
);
1544 typedef void (*free_fn
)(struct stream_filter
*);
1546 struct stream_filter_vtbl
{
1551 struct stream_filter
{
1552 struct stream_filter_vtbl
*vtbl
;
1555 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1556 const char *input
, size_t *isize_p
,
1557 char *output
, size_t *osize_p
)
1562 return 0; /* we do not keep any states */
1564 if (*osize_p
< count
)
1567 memmove(output
, input
, count
);
1574 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1576 ; /* nothing -- null instances are shared */
1579 static struct stream_filter_vtbl null_vtbl
= {
1580 .filter
= null_filter_fn
,
1581 .free
= null_free_fn
,
1584 static struct stream_filter null_filter_singleton
= {
1588 int is_null_stream_filter(struct stream_filter
*filter
)
1590 return filter
== &null_filter_singleton
;
1598 struct lf_to_crlf_filter
{
1599 struct stream_filter filter
;
1600 unsigned has_held
:1;
1604 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1605 const char *input
, size_t *isize_p
,
1606 char *output
, size_t *osize_p
)
1608 size_t count
, o
= 0;
1609 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1612 * We may be holding onto the CR to see if it is followed by a
1613 * LF, in which case we would need to go to the main loop.
1614 * Otherwise, just emit it to the output stream.
1616 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1617 output
[o
++] = lf_to_crlf
->held
;
1618 lf_to_crlf
->has_held
= 0;
1621 /* We are told to drain */
1628 if (count
|| lf_to_crlf
->has_held
) {
1632 if (lf_to_crlf
->has_held
) {
1634 lf_to_crlf
->has_held
= 0;
1637 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1642 } else if (was_cr
) {
1644 * Previous round saw CR and it is not followed
1645 * by a LF; emit the CR before processing the
1646 * current character.
1652 * We may have consumed the last output slot,
1653 * in which case we need to break out of this
1654 * loop; hold the current character before
1657 if (*osize_p
<= o
) {
1658 lf_to_crlf
->has_held
= 1;
1659 lf_to_crlf
->held
= ch
;
1660 continue; /* break but increment i */
1675 if (!lf_to_crlf
->has_held
&& was_cr
) {
1676 lf_to_crlf
->has_held
= 1;
1677 lf_to_crlf
->held
= '\r';
1683 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1688 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1689 .filter
= lf_to_crlf_filter_fn
,
1690 .free
= lf_to_crlf_free_fn
,
1693 static struct stream_filter
*lf_to_crlf_filter(void)
1695 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1697 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1698 return (struct stream_filter
*)lf_to_crlf
;
1704 #define FILTER_BUFFER 1024
1705 struct cascade_filter
{
1706 struct stream_filter filter
;
1707 struct stream_filter
*one
;
1708 struct stream_filter
*two
;
1709 char buf
[FILTER_BUFFER
];
1713 static int cascade_filter_fn(struct stream_filter
*filter
,
1714 const char *input
, size_t *isize_p
,
1715 char *output
, size_t *osize_p
)
1717 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1719 size_t sz
= *osize_p
;
1720 size_t to_feed
, remaining
;
1723 * input -- (one) --> buf -- (two) --> output
1725 while (filled
< sz
) {
1726 remaining
= sz
- filled
;
1728 /* do we already have something to feed two with? */
1729 if (cas
->ptr
< cas
->end
) {
1730 to_feed
= cas
->end
- cas
->ptr
;
1731 if (stream_filter(cas
->two
,
1732 cas
->buf
+ cas
->ptr
, &to_feed
,
1733 output
+ filled
, &remaining
))
1735 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1736 filled
= sz
- remaining
;
1740 /* feed one from upstream and have it emit into our buffer */
1741 to_feed
= input
? *isize_p
: 0;
1742 if (input
&& !to_feed
)
1744 remaining
= sizeof(cas
->buf
);
1745 if (stream_filter(cas
->one
,
1747 cas
->buf
, &remaining
))
1749 cas
->end
= sizeof(cas
->buf
) - remaining
;
1752 size_t fed
= *isize_p
- to_feed
;
1757 /* do we know that we drained one completely? */
1758 if (input
|| cas
->end
)
1761 /* tell two to drain; we have nothing more to give it */
1763 remaining
= sz
- filled
;
1764 if (stream_filter(cas
->two
,
1766 output
+ filled
, &remaining
))
1768 if (remaining
== (sz
- filled
))
1769 break; /* completely drained two */
1770 filled
= sz
- remaining
;
1776 static void cascade_free_fn(struct stream_filter
*filter
)
1778 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1779 free_stream_filter(cas
->one
);
1780 free_stream_filter(cas
->two
);
1784 static struct stream_filter_vtbl cascade_vtbl
= {
1785 .filter
= cascade_filter_fn
,
1786 .free
= cascade_free_fn
,
1789 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1790 struct stream_filter
*two
)
1792 struct cascade_filter
*cascade
;
1794 if (!one
|| is_null_stream_filter(one
))
1796 if (!two
|| is_null_stream_filter(two
))
1799 cascade
= xmalloc(sizeof(*cascade
));
1802 cascade
->end
= cascade
->ptr
= 0;
1803 cascade
->filter
.vtbl
= &cascade_vtbl
;
1804 return (struct stream_filter
*)cascade
;
1810 #define IDENT_DRAINING (-1)
1811 #define IDENT_SKIPPING (-2)
1812 struct ident_filter
{
1813 struct stream_filter filter
;
1816 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1819 static int is_foreign_ident(const char *str
)
1823 if (!skip_prefix(str
, "$Id: ", &str
))
1825 for (i
= 0; str
[i
]; i
++) {
1826 if (isspace(str
[i
]) && str
[i
+1] != '$')
1832 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1834 size_t to_drain
= ident
->left
.len
;
1836 if (*osize_p
< to_drain
)
1837 to_drain
= *osize_p
;
1839 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1840 strbuf_remove(&ident
->left
, 0, to_drain
);
1841 *output_p
+= to_drain
;
1842 *osize_p
-= to_drain
;
1844 if (!ident
->left
.len
)
1848 static int ident_filter_fn(struct stream_filter
*filter
,
1849 const char *input
, size_t *isize_p
,
1850 char *output
, size_t *osize_p
)
1852 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1853 static const char head
[] = "$Id";
1856 /* drain upon eof */
1857 switch (ident
->state
) {
1859 strbuf_add(&ident
->left
, head
, ident
->state
);
1861 case IDENT_SKIPPING
:
1863 case IDENT_DRAINING
:
1864 ident_drain(ident
, &output
, osize_p
);
1869 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1872 if (ident
->state
== IDENT_DRAINING
) {
1873 ident_drain(ident
, &output
, osize_p
);
1882 if (ident
->state
== IDENT_SKIPPING
) {
1884 * Skipping until '$' or LF, but keeping them
1885 * in case it is a foreign ident.
1887 strbuf_addch(&ident
->left
, ch
);
1888 if (ch
!= '\n' && ch
!= '$')
1890 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1891 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1892 strbuf_addstr(&ident
->left
, ident
->ident
);
1894 ident
->state
= IDENT_DRAINING
;
1898 if (ident
->state
< sizeof(head
) &&
1899 head
[ident
->state
] == ch
) {
1905 strbuf_add(&ident
->left
, head
, ident
->state
);
1906 if (ident
->state
== sizeof(head
) - 1) {
1907 if (ch
!= ':' && ch
!= '$') {
1908 strbuf_addch(&ident
->left
, ch
);
1914 strbuf_addch(&ident
->left
, ch
);
1915 ident
->state
= IDENT_SKIPPING
;
1917 strbuf_addstr(&ident
->left
, ident
->ident
);
1918 ident
->state
= IDENT_DRAINING
;
1923 strbuf_addch(&ident
->left
, ch
);
1924 ident
->state
= IDENT_DRAINING
;
1929 static void ident_free_fn(struct stream_filter
*filter
)
1931 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1932 strbuf_release(&ident
->left
);
1936 static struct stream_filter_vtbl ident_vtbl
= {
1937 .filter
= ident_filter_fn
,
1938 .free
= ident_free_fn
,
1941 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1943 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1945 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1946 ": %s $", oid_to_hex(oid
));
1947 strbuf_init(&ident
->left
, 0);
1948 ident
->filter
.vtbl
= &ident_vtbl
;
1950 return (struct stream_filter
*)ident
;
1954 * Return an appropriately constructed filter for the given ca, or NULL if
1955 * the contents cannot be filtered without reading the whole thing
1958 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1959 * large binary blob you would want us not to slurp into the memory!
1961 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1962 const struct object_id
*oid
)
1964 struct stream_filter
*filter
= NULL
;
1966 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
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 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
1982 const struct object_id
*oid
)
1984 struct conv_attrs ca
;
1985 convert_attrs(istate
, &ca
, path
);
1986 return get_stream_filter_ca(&ca
, oid
);
1989 void free_stream_filter(struct stream_filter
*filter
)
1991 filter
->vtbl
->free(filter
);
1994 int stream_filter(struct stream_filter
*filter
,
1995 const char *input
, size_t *isize_p
,
1996 char *output
, size_t *osize_p
)
1998 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
2001 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
2002 const struct object_id
*treeish
,
2003 const struct object_id
*blob
)
2005 memset(meta
, 0, sizeof(*meta
));
2007 meta
->refname
= refname
;
2009 oidcpy(&meta
->treeish
, treeish
);
2011 oidcpy(&meta
->blob
, blob
);
2014 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2015 const struct checkout_metadata
*src
,
2016 const struct object_id
*blob
)
2018 memcpy(dst
, src
, sizeof(*dst
));
2020 oidcpy(&dst
->blob
, blob
);
2023 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2026 if (ca
->drv
->process
)
2027 return CA_CLASS_INCORE_PROCESS
;
2028 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2029 return CA_CLASS_INCORE_FILTER
;
2032 if (ca
->working_tree_encoding
)
2033 return CA_CLASS_INCORE
;
2035 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2036 return CA_CLASS_INCORE
;
2038 return CA_CLASS_STREAMABLE
;