7 #include "object-store.h"
9 #include "run-command.h"
13 #include "sub-process.h"
20 * convert.c - convert a file when checking it out and checking it in.
22 * This should use the pathname to decide on whether it wants to do some
23 * more interesting conversions (automatic gzip/unzip, general format
24 * conversions etc etc), but by default it just does automatic CRLF<->LF
25 * translation when the "text" attribute or "auto_crlf" option is set.
28 /* Stat bits: When BIN is set, the txt bits are unset */
29 #define CONVERT_STAT_BITS_TXT_LF 0x1
30 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
31 #define CONVERT_STAT_BITS_BIN 0x4
34 /* NUL, CR, LF and CRLF counts */
35 unsigned nul
, lonecr
, lonelf
, crlf
;
37 /* These are just approximations! */
38 unsigned printable
, nonprintable
;
41 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
45 memset(stats
, 0, sizeof(*stats
));
47 for (i
= 0; i
< size
; i
++) {
48 unsigned char c
= buf
[i
];
50 if (i
+1 < size
&& buf
[i
+1] == '\n') {
63 stats
->nonprintable
++;
66 /* BS, HT, ESC and FF */
67 case '\b': case '\t': case '\033': case '\014':
74 stats
->nonprintable
++;
81 /* If file ends with EOF then don't count this EOF as non-printable. */
82 if (size
>= 1 && buf
[size
-1] == '\032')
83 stats
->nonprintable
--;
87 * The same heuristics as diff.c::mmfile_is_binary()
88 * We treat files with bare CR as binary
90 static int convert_is_binary(const struct text_stat
*stats
)
96 if ((stats
->printable
>> 7) < stats
->nonprintable
)
101 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
103 struct text_stat stats
;
107 gather_stats(data
, size
, &stats
);
108 if (convert_is_binary(&stats
))
109 ret
|= CONVERT_STAT_BITS_BIN
;
111 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
113 ret
|= CONVERT_STAT_BITS_TXT_LF
;
118 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
120 unsigned int convert_stats
= gather_convert_stats(data
, size
);
122 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
124 switch (convert_stats
) {
125 case CONVERT_STAT_BITS_TXT_LF
:
127 case CONVERT_STAT_BITS_TXT_CRLF
:
129 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
136 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
141 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
142 ret
= gather_convert_stats_ascii(data
, sz
);
147 const char *get_wt_convert_stats_ascii(const char *path
)
149 const char *ret
= "";
150 struct strbuf sb
= STRBUF_INIT
;
151 if (strbuf_read_file(&sb
, path
, 0) >= 0)
152 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
157 static int text_eol_is_crlf(void)
159 if (auto_crlf
== AUTO_CRLF_TRUE
)
161 else if (auto_crlf
== AUTO_CRLF_INPUT
)
163 if (core_eol
== EOL_CRLF
)
165 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
170 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
172 switch (crlf_action
) {
177 case CRLF_TEXT_INPUT
:
182 case CRLF_AUTO_INPUT
:
187 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
189 warning(_("illegal crlf_action %d"), (int)crlf_action
);
193 static void check_global_conv_flags_eol(const char *path
,
194 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
197 if (old_stats
->crlf
&& !new_stats
->crlf
) {
199 * CRLFs would not be restored by checkout
201 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
202 die(_("CRLF would be replaced by LF in %s"), path
);
203 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
204 warning(_("in the working copy of '%s', CRLF will be"
205 " replaced by LF the next time Git touches"
207 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
209 * CRLFs would be added by checkout
211 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
212 die(_("LF would be replaced by CRLF in %s"), path
);
213 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
214 warning(_("in the working copy of '%s', LF will be"
215 " replaced by CRLF the next time Git touches"
220 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
227 data
= read_blob_data_from_index(istate
, path
, &sz
);
231 crp
= memchr(data
, '\r', sz
);
233 unsigned int ret_stats
;
234 ret_stats
= gather_convert_stats(data
, sz
);
235 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
236 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
243 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
244 enum convert_crlf_action crlf_action
)
246 if (output_eol(crlf_action
) != EOL_CRLF
)
248 /* No "naked" LF? Nothing to convert, regardless. */
252 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
253 /* If we have any CR or CRLF line endings, we do not touch it */
254 /* This is the new safer autocrlf-handling */
255 if (stats
->lonecr
|| stats
->crlf
)
258 if (convert_is_binary(stats
))
265 static int validate_encoding(const char *path
, const char *enc
,
266 const char *data
, size_t len
, int die_on_error
)
268 const char *stripped
;
270 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
271 if (skip_iprefix(enc
, "UTF", &stripped
)) {
272 skip_prefix(stripped
, "-", &stripped
);
275 * Check for detectable errors in UTF encodings
277 if (has_prohibited_utf_bom(enc
, data
, len
)) {
278 const char *error_msg
= _(
279 "BOM is prohibited in '%s' if encoded as %s");
281 * This advice is shown for UTF-??BE and UTF-??LE encodings.
282 * We cut off the last two characters of the encoding name
283 * to generate the encoding name suitable for BOMs.
285 const char *advise_msg
= _(
286 "The file '%s' contains a byte order "
287 "mark (BOM). Please use UTF-%.*s as "
288 "working-tree-encoding.");
289 int stripped_len
= strlen(stripped
) - strlen("BE");
290 advise(advise_msg
, path
, stripped_len
, stripped
);
292 die(error_msg
, path
, enc
);
294 return error(error_msg
, path
, enc
);
297 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
298 const char *error_msg
= _(
299 "BOM is required in '%s' if encoded as %s");
300 const char *advise_msg
= _(
301 "The file '%s' is missing a byte order "
302 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
303 "(depending on the byte order) as "
304 "working-tree-encoding.");
305 advise(advise_msg
, path
, stripped
, stripped
);
307 die(error_msg
, path
, enc
);
309 return error(error_msg
, path
, enc
);
317 static void trace_encoding(const char *context
, const char *path
,
318 const char *encoding
, const char *buf
, size_t len
)
320 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
321 struct strbuf trace
= STRBUF_INIT
;
324 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
325 for (i
= 0; i
< len
&& buf
; ++i
) {
327 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
329 (unsigned char) buf
[i
],
330 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
331 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
334 strbuf_addchars(&trace
, '\n', 1);
336 trace_strbuf(&coe
, &trace
);
337 strbuf_release(&trace
);
340 static int check_roundtrip(const char *enc_name
)
343 * check_roundtrip_encoding contains a string of comma and/or
344 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
345 * Search for the given encoding in that string.
347 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
352 next
= found
+ strlen(enc_name
);
353 len
= strlen(check_roundtrip_encoding
);
356 * check that the found encoding is at the
357 * beginning of check_roundtrip_encoding or
358 * that it is prefixed with a space or comma
360 found
== check_roundtrip_encoding
|| (
361 (isspace(found
[-1]) || found
[-1] == ',')
365 * check that the found encoding is at the
366 * end of check_roundtrip_encoding or
367 * that it is suffixed with a space or comma
369 next
== check_roundtrip_encoding
+ len
|| (
370 next
< check_roundtrip_encoding
+ len
&&
371 (isspace(next
[0]) || next
[0] == ',')
376 static const char *default_encoding
= "UTF-8";
378 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
379 struct strbuf
*buf
, const char *enc
, int conv_flags
)
383 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
386 * No encoding is specified or there is nothing to encode.
387 * Tell the caller that the content was not modified.
389 if (!enc
|| (src
&& !src_len
))
393 * Looks like we got called from "would_convert_to_git()".
394 * This means Git wants to know if it would encode (= modify!)
395 * the content. Let's answer with "yes", since an encoding was
401 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
404 trace_encoding("source", path
, enc
, src
, src_len
);
405 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
409 * We could add the blob "as-is" to Git. However, on checkout
410 * we would try to re-encode to the original encoding. This
411 * would fail and we would leave the user with a messed-up
412 * working tree. Let's try to avoid this by screaming loud.
414 const char* msg
= _("failed to encode '%s' from %s to %s");
416 die(msg
, path
, enc
, default_encoding
);
418 error(msg
, path
, enc
, default_encoding
);
422 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
425 * UTF supports lossless conversion round tripping [1] and conversions
426 * between UTF and other encodings are mostly round trip safe as
427 * Unicode aims to be a superset of all other character encodings.
428 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
429 * trip issues [2]. Check the round trip conversion for all encodings
430 * listed in core.checkRoundtripEncoding.
432 * The round trip check is only performed if content is written to Git.
433 * This ensures that no information is lost during conversion to/from
434 * the internal UTF-8 representation.
436 * Please note, the code below is not tested because I was not able to
437 * generate a faulty round trip without an iconv error. Iconv errors
438 * are already caught above.
440 * [1] http://unicode.org/faq/utf_bom.html#gen2
441 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
443 if (die_on_error
&& check_roundtrip(enc
)) {
447 re_src
= reencode_string_len(dst
, dst_len
,
448 enc
, default_encoding
,
451 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
452 trace_encoding("reencoded source", path
, enc
,
455 if (!re_src
|| src_len
!= re_src_len
||
456 memcmp(src
, re_src
, src_len
)) {
457 const char* msg
= _("encoding '%s' from %s to %s and "
458 "back is not the same");
459 die(msg
, path
, enc
, default_encoding
);
465 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
469 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
470 struct strbuf
*buf
, const char *enc
)
476 * No encoding is specified or there is nothing to encode.
477 * Tell the caller that the content was not modified.
479 if (!enc
|| (src
&& !src_len
))
482 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
485 error(_("failed to encode '%s' from %s to %s"),
486 path
, default_encoding
, enc
);
490 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
494 static int crlf_to_git(struct index_state
*istate
,
495 const char *path
, const char *src
, size_t len
,
497 enum convert_crlf_action crlf_action
, int conv_flags
)
499 struct text_stat stats
;
501 int convert_crlf_into_lf
;
503 if (crlf_action
== CRLF_BINARY
||
508 * If we are doing a dry-run and have no source buffer, there is
509 * nothing to analyze; we must assume we would convert.
514 gather_stats(src
, len
, &stats
);
515 /* Optimization: No CRLF? Nothing to convert, regardless. */
516 convert_crlf_into_lf
= !!stats
.crlf
;
518 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
519 if (convert_is_binary(&stats
))
522 * If the file in the index has any CR in it, do not
523 * convert. This is the new safer autocrlf handling,
524 * unless we want to renormalize in a merge or
527 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
528 has_crlf_in_index(istate
, path
))
529 convert_crlf_into_lf
= 0;
531 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
532 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
533 struct text_stat new_stats
;
534 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
535 /* simulate "git add" */
536 if (convert_crlf_into_lf
) {
537 new_stats
.lonelf
+= new_stats
.crlf
;
540 /* simulate "git checkout" */
541 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
542 new_stats
.crlf
+= new_stats
.lonelf
;
543 new_stats
.lonelf
= 0;
545 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
547 if (!convert_crlf_into_lf
)
551 * At this point all of our source analysis is done, and we are sure we
552 * would convert. If we are in dry-run mode, we can give an answer.
557 /* only grow if not in place */
558 if (strbuf_avail(buf
) + buf
->len
< len
)
559 strbuf_grow(buf
, len
- buf
->len
);
561 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
563 * If we guessed, we already know we rejected a file with
564 * lone CR, and we can strip a CR without looking at what
568 unsigned char c
= *src
++;
574 unsigned char c
= *src
++;
575 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
579 strbuf_setlen(buf
, dst
- buf
->buf
);
583 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
584 enum convert_crlf_action crlf_action
)
586 char *to_free
= NULL
;
587 struct text_stat stats
;
589 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
592 gather_stats(src
, len
, &stats
);
593 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
596 /* are we "faking" in place editing ? */
598 to_free
= strbuf_detach(buf
, NULL
);
600 strbuf_grow(buf
, len
+ stats
.lonelf
);
602 const char *nl
= memchr(src
, '\n', len
);
605 if (nl
> src
&& nl
[-1] == '\r') {
606 strbuf_add(buf
, src
, nl
+ 1 - src
);
608 strbuf_add(buf
, src
, nl
- src
);
609 strbuf_addstr(buf
, "\r\n");
614 strbuf_add(buf
, src
, len
);
620 struct filter_params
{
628 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
631 * Spawn cmd and feed the buffer contents through its stdin.
633 struct child_process child_process
= CHILD_PROCESS_INIT
;
634 struct filter_params
*params
= (struct filter_params
*)data
;
635 int write_err
, status
;
637 /* apply % substitution to cmd */
638 struct strbuf cmd
= STRBUF_INIT
;
639 struct strbuf path
= STRBUF_INIT
;
640 struct strbuf_expand_dict_entry dict
[] = {
645 /* quote the path to preserve spaces, etc. */
646 sq_quote_buf(&path
, params
->path
);
647 dict
[0].value
= path
.buf
;
649 /* expand all %f with the quoted path */
650 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
651 strbuf_release(&path
);
653 strvec_push(&child_process
.args
, cmd
.buf
);
654 child_process
.use_shell
= 1;
655 child_process
.in
= -1;
656 child_process
.out
= out
;
658 if (start_command(&child_process
)) {
659 strbuf_release(&cmd
);
660 return error(_("cannot fork to run external filter '%s'"),
664 sigchain_push(SIGPIPE
, SIG_IGN
);
667 write_err
= (write_in_full(child_process
.in
,
668 params
->src
, params
->size
) < 0);
672 write_err
= copy_fd(params
->fd
, child_process
.in
);
673 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
677 if (close(child_process
.in
))
680 error(_("cannot feed the input to external filter '%s'"),
683 sigchain_pop(SIGPIPE
);
685 status
= finish_command(&child_process
);
687 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
689 strbuf_release(&cmd
);
690 return (write_err
|| status
);
693 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
694 struct strbuf
*dst
, const char *cmd
)
697 * Create a pipeline to have the command filter the buffer's
700 * (child --> cmd) --> us
703 struct strbuf nbuf
= STRBUF_INIT
;
705 struct filter_params params
;
707 memset(&async
, 0, sizeof(async
));
708 async
.proc
= filter_buffer_or_fd
;
709 async
.data
= ¶ms
;
718 if (start_async(&async
))
719 return 0; /* error was already reported */
721 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
722 err
= error(_("read from external filter '%s' failed"), cmd
);
724 if (close(async
.out
)) {
725 err
= error(_("read from external filter '%s' failed"), cmd
);
727 if (finish_async(&async
)) {
728 err
= error(_("external filter '%s' failed"), cmd
);
732 strbuf_swap(dst
, &nbuf
);
734 strbuf_release(&nbuf
);
738 #define CAP_CLEAN (1u<<0)
739 #define CAP_SMUDGE (1u<<1)
740 #define CAP_DELAY (1u<<2)
743 struct subprocess_entry subprocess
; /* must be the first member! */
744 unsigned int supported_capabilities
;
747 static int subprocess_map_initialized
;
748 static struct hashmap subprocess_map
;
750 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
752 static int versions
[] = {2, 0};
753 static struct subprocess_capability capabilities
[] = {
754 { "clean", CAP_CLEAN
},
755 { "smudge", CAP_SMUDGE
},
756 { "delay", CAP_DELAY
},
759 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
760 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
762 &entry
->supported_capabilities
);
765 static void handle_filter_error(const struct strbuf
*filter_status
,
766 struct cmd2process
*entry
,
767 const unsigned int wanted_capability
)
769 if (!strcmp(filter_status
->buf
, "error"))
770 ; /* The filter signaled a problem with the file. */
771 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
773 * The filter signaled a permanent problem. Don't try to filter
774 * files with the same command for the lifetime of the current
777 entry
->supported_capabilities
&= ~wanted_capability
;
780 * Something went wrong with the protocol filter.
781 * Force shutdown and restart if another blob requires filtering.
783 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
784 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
789 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
790 int fd
, struct strbuf
*dst
, const char *cmd
,
791 const unsigned int wanted_capability
,
792 const struct checkout_metadata
*meta
,
793 struct delayed_checkout
*dco
)
797 struct cmd2process
*entry
;
798 struct child_process
*process
;
799 struct strbuf nbuf
= STRBUF_INIT
;
800 struct strbuf filter_status
= STRBUF_INIT
;
801 const char *filter_type
;
803 if (!subprocess_map_initialized
) {
804 subprocess_map_initialized
= 1;
805 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
808 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
814 entry
= xmalloc(sizeof(*entry
));
815 entry
->supported_capabilities
= 0;
817 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
822 process
= &entry
->subprocess
.process
;
824 if (!(entry
->supported_capabilities
& wanted_capability
))
827 if (wanted_capability
& CAP_CLEAN
)
828 filter_type
= "clean";
829 else if (wanted_capability
& CAP_SMUDGE
)
830 filter_type
= "smudge";
832 die(_("unexpected filter type"));
834 sigchain_push(SIGPIPE
, SIG_IGN
);
836 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
837 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
841 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
843 error(_("path name too long for external filter"));
847 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
851 if (meta
&& meta
->refname
) {
852 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
857 if (meta
&& !is_null_oid(&meta
->treeish
)) {
858 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
863 if (meta
&& !is_null_oid(&meta
->blob
)) {
864 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
869 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
870 dco
&& dco
->state
== CE_CAN_DELAY
) {
872 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
877 err
= packet_flush_gently(process
->in
);
882 err
= write_packetized_from_fd_no_flush(fd
, process
->in
);
884 err
= write_packetized_from_buf_no_flush(src
, len
, process
->in
);
888 err
= packet_flush_gently(process
->in
);
892 err
= subprocess_read_status(process
->out
, &filter_status
);
896 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
897 string_list_insert(&dco
->filters
, cmd
);
898 string_list_insert(&dco
->paths
, path
);
900 /* The filter got the blob and wants to send us a response. */
901 err
= strcmp(filter_status
.buf
, "success");
905 err
= read_packetized_to_strbuf(process
->out
, &nbuf
,
906 PACKET_READ_GENTLE_ON_EOF
) < 0;
910 err
= subprocess_read_status(process
->out
, &filter_status
);
914 err
= strcmp(filter_status
.buf
, "success");
918 sigchain_pop(SIGPIPE
);
921 handle_filter_error(&filter_status
, entry
, wanted_capability
);
923 strbuf_swap(dst
, &nbuf
);
924 strbuf_release(&nbuf
);
925 strbuf_release(&filter_status
);
930 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
934 struct cmd2process
*entry
;
935 struct child_process
*process
;
936 struct strbuf filter_status
= STRBUF_INIT
;
938 assert(subprocess_map_initialized
);
939 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
941 error(_("external filter '%s' is not available anymore although "
942 "not all paths have been filtered"), cmd
);
945 process
= &entry
->subprocess
.process
;
946 sigchain_push(SIGPIPE
, SIG_IGN
);
948 err
= packet_write_fmt_gently(
949 process
->in
, "command=list_available_blobs\n");
953 err
= packet_flush_gently(process
->in
);
957 while ((line
= packet_read_line(process
->out
, NULL
))) {
959 if (skip_prefix(line
, "pathname=", &path
))
960 string_list_insert(available_paths
, xstrdup(path
));
962 ; /* ignore unknown keys */
965 err
= subprocess_read_status(process
->out
, &filter_status
);
969 err
= strcmp(filter_status
.buf
, "success");
972 sigchain_pop(SIGPIPE
);
975 handle_filter_error(&filter_status
, entry
, 0);
976 strbuf_release(&filter_status
);
980 static struct convert_driver
{
982 struct convert_driver
*next
;
987 } *user_convert
, **user_convert_tail
;
989 static int apply_filter(const char *path
, const char *src
, size_t len
,
990 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
991 const unsigned int wanted_capability
,
992 const struct checkout_metadata
*meta
,
993 struct delayed_checkout
*dco
)
995 const char *cmd
= NULL
;
1003 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
1005 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1009 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1010 else if (drv
->process
&& *drv
->process
)
1011 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1012 drv
->process
, wanted_capability
, meta
, dco
);
1017 static int read_convert_config(const char *var
, const char *value
, void *cb UNUSED
)
1019 const char *key
, *name
;
1021 struct convert_driver
*drv
;
1024 * External conversion drivers are configured using
1025 * "filter.<name>.variable".
1027 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1029 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1030 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1033 CALLOC_ARRAY(drv
, 1);
1034 drv
->name
= xmemdupz(name
, namelen
);
1035 *user_convert_tail
= drv
;
1036 user_convert_tail
= &(drv
->next
);
1040 * filter.<name>.smudge and filter.<name>.clean specifies
1045 * The command-line will not be interpolated in any way.
1048 if (!strcmp("smudge", key
))
1049 return git_config_string(&drv
->smudge
, var
, value
);
1051 if (!strcmp("clean", key
))
1052 return git_config_string(&drv
->clean
, var
, value
);
1054 if (!strcmp("process", key
))
1055 return git_config_string(&drv
->process
, var
, value
);
1057 if (!strcmp("required", key
)) {
1058 drv
->required
= git_config_bool(var
, value
);
1065 static int count_ident(const char *cp
, unsigned long size
)
1068 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1080 if (memcmp("Id", cp
, 2))
1091 * "$Id: ... "; scan up to the closing dollar sign and discard.
1107 static int ident_to_git(const char *src
, size_t len
,
1108 struct strbuf
*buf
, int ident
)
1112 if (!ident
|| (src
&& !count_ident(src
, len
)))
1118 /* only grow if not in place */
1119 if (strbuf_avail(buf
) + buf
->len
< len
)
1120 strbuf_grow(buf
, len
- buf
->len
);
1123 dollar
= memchr(src
, '$', len
);
1126 memmove(dst
, src
, dollar
+ 1 - src
);
1127 dst
+= dollar
+ 1 - src
;
1128 len
-= dollar
+ 1 - src
;
1131 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1132 dollar
= memchr(src
+ 3, '$', len
- 3);
1135 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1136 /* Line break before the next dollar. */
1140 memcpy(dst
, "Id$", 3);
1142 len
-= dollar
+ 1 - src
;
1146 memmove(dst
, src
, len
);
1147 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1151 static int ident_to_worktree(const char *src
, size_t len
,
1152 struct strbuf
*buf
, int ident
)
1154 struct object_id oid
;
1155 char *to_free
= NULL
, *dollar
, *spc
;
1161 cnt
= count_ident(src
, len
);
1165 /* are we "faking" in place editing ? */
1166 if (src
== buf
->buf
)
1167 to_free
= strbuf_detach(buf
, NULL
);
1168 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1170 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1172 /* step 1: run to the next '$' */
1173 dollar
= memchr(src
, '$', len
);
1176 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1177 len
-= dollar
+ 1 - src
;
1180 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1181 if (len
< 3 || memcmp("Id", src
, 2))
1184 /* step 3: skip over Id$ or Id:xxxxx$ */
1185 if (src
[2] == '$') {
1188 } else if (src
[2] == ':') {
1190 * It's possible that an expanded Id has crept its way into the
1191 * repository, we cope with that by stripping the expansion out.
1192 * This is probably not a good idea, since it will cause changes
1193 * on checkout, which won't go away by stash, but let's keep it
1194 * for git-style ids.
1196 dollar
= memchr(src
+ 3, '$', len
- 3);
1198 /* incomplete keyword, no more '$', so just quit the loop */
1202 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1203 /* Line break before the next dollar. */
1207 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1208 if (spc
&& spc
< dollar
-1) {
1209 /* There are spaces in unexpected places.
1210 * This is probably an id from some other
1211 * versioning system. Keep it for now.
1216 len
-= dollar
+ 1 - src
;
1219 /* it wasn't a "Id$" or "Id:xxxx$" */
1223 /* step 4: substitute */
1224 strbuf_addstr(buf
, "Id: ");
1225 strbuf_addstr(buf
, oid_to_hex(&oid
));
1226 strbuf_addstr(buf
, " $");
1228 strbuf_add(buf
, src
, len
);
1234 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1236 const char *value
= check
->value
;
1238 if (ATTR_UNSET(value
) || !strlen(value
))
1241 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1242 die(_("true/false are no valid working-tree-encodings"));
1245 /* Don't encode to the default encoding */
1246 if (same_encoding(value
, default_encoding
))
1252 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1254 const char *value
= check
->value
;
1256 if (ATTR_TRUE(value
))
1258 else if (ATTR_FALSE(value
))
1260 else if (ATTR_UNSET(value
))
1262 else if (!strcmp(value
, "input"))
1263 return CRLF_TEXT_INPUT
;
1264 else if (!strcmp(value
, "auto"))
1266 return CRLF_UNDEFINED
;
1269 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1271 const char *value
= check
->value
;
1273 if (ATTR_UNSET(value
))
1275 else if (!strcmp(value
, "lf"))
1277 else if (!strcmp(value
, "crlf"))
1282 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1284 const char *value
= check
->value
;
1285 struct convert_driver
*drv
;
1287 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1289 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1290 if (!strcmp(value
, drv
->name
))
1295 static int git_path_check_ident(struct attr_check_item
*check
)
1297 const char *value
= check
->value
;
1299 return !!ATTR_TRUE(value
);
1302 static struct attr_check
*check
;
1304 void convert_attrs(struct index_state
*istate
,
1305 struct conv_attrs
*ca
, const char *path
)
1307 struct attr_check_item
*ccheck
= NULL
;
1310 check
= attr_check_initl("crlf", "ident", "filter",
1311 "eol", "text", "working-tree-encoding",
1313 user_convert_tail
= &user_convert
;
1314 git_config(read_convert_config
, NULL
);
1317 git_check_attr(istate
, NULL
, path
, check
);
1318 ccheck
= check
->items
;
1319 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1320 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1321 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1322 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1323 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1324 if (ca
->crlf_action
!= CRLF_BINARY
) {
1325 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1326 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1327 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1328 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1329 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1330 else if (eol_attr
== EOL_LF
)
1331 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1332 else if (eol_attr
== EOL_CRLF
)
1333 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1335 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1337 /* Save attr and make a decision for action */
1338 ca
->attr_action
= ca
->crlf_action
;
1339 if (ca
->crlf_action
== CRLF_TEXT
)
1340 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1341 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1342 ca
->crlf_action
= CRLF_BINARY
;
1343 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1344 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1345 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1346 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1349 void reset_parsed_attributes(void)
1351 struct convert_driver
*drv
, *next
;
1353 attr_check_free(check
);
1355 reset_merge_attributes();
1357 for (drv
= user_convert
; drv
; drv
= next
) {
1359 free((void *)drv
->name
);
1362 user_convert
= NULL
;
1363 user_convert_tail
= NULL
;
1366 int would_convert_to_git_filter_fd(struct index_state
*istate
, const char *path
)
1368 struct conv_attrs ca
;
1370 convert_attrs(istate
, &ca
, path
);
1375 * Apply a filter to an fd only if the filter is required to succeed.
1376 * We must die if the filter fails, because the original data before
1377 * filtering is not available.
1379 if (!ca
.drv
->required
)
1382 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1385 const char *get_convert_attr_ascii(struct index_state
*istate
, const char *path
)
1387 struct conv_attrs ca
;
1389 convert_attrs(istate
, &ca
, path
);
1390 switch (ca
.attr_action
) {
1391 case CRLF_UNDEFINED
:
1397 case CRLF_TEXT_INPUT
:
1398 return "text eol=lf";
1399 case CRLF_TEXT_CRLF
:
1400 return "text eol=crlf";
1403 case CRLF_AUTO_CRLF
:
1404 return "text=auto eol=crlf";
1405 case CRLF_AUTO_INPUT
:
1406 return "text=auto eol=lf";
1411 int convert_to_git(struct index_state
*istate
,
1412 const char *path
, const char *src
, size_t len
,
1413 struct strbuf
*dst
, int conv_flags
)
1416 struct conv_attrs ca
;
1418 convert_attrs(istate
, &ca
, path
);
1420 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1421 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1422 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1429 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1435 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1436 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1442 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1445 void convert_to_git_filter_fd(struct index_state
*istate
,
1446 const char *path
, int fd
, struct strbuf
*dst
,
1449 struct conv_attrs ca
;
1450 convert_attrs(istate
, &ca
, path
);
1454 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1455 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1457 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1458 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1459 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1462 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1463 const char *path
, const char *src
,
1464 size_t len
, struct strbuf
*dst
,
1466 const struct checkout_metadata
*meta
,
1467 struct delayed_checkout
*dco
)
1469 int ret
= 0, ret_filter
= 0;
1471 ret
|= ident_to_worktree(src
, len
, dst
, ca
->ident
);
1477 * CRLF conversion can be skipped if normalizing, unless there
1478 * is a smudge or process filter (even if the process filter doesn't
1479 * support smudge). The filters might expect CRLFs.
1481 if ((ca
->drv
&& (ca
->drv
->smudge
|| ca
->drv
->process
)) || !normalizing
) {
1482 ret
|= crlf_to_worktree(src
, len
, dst
, ca
->crlf_action
);
1489 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
->working_tree_encoding
);
1495 ret_filter
= apply_filter(
1496 path
, src
, len
, -1, dst
, ca
->drv
, CAP_SMUDGE
, meta
, dco
);
1497 if (!ret_filter
&& ca
->drv
&& ca
->drv
->required
)
1498 die(_("%s: smudge filter %s failed"), path
, ca
->drv
->name
);
1500 return ret
| ret_filter
;
1503 int async_convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1504 const char *path
, const char *src
,
1505 size_t len
, struct strbuf
*dst
,
1506 const struct checkout_metadata
*meta
,
1509 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1513 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1514 const char *path
, const char *src
,
1515 size_t len
, struct strbuf
*dst
,
1516 const struct checkout_metadata
*meta
)
1518 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1522 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1523 const char *src
, size_t len
, struct strbuf
*dst
)
1525 struct conv_attrs ca
;
1528 convert_attrs(istate
, &ca
, path
);
1529 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1535 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1538 /*****************************************************************
1540 * Streaming conversion support
1542 *****************************************************************/
1544 typedef int (*filter_fn
)(struct stream_filter
*,
1545 const char *input
, size_t *isize_p
,
1546 char *output
, size_t *osize_p
);
1547 typedef void (*free_fn
)(struct stream_filter
*);
1549 struct stream_filter_vtbl
{
1554 struct stream_filter
{
1555 struct stream_filter_vtbl
*vtbl
;
1558 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1559 const char *input
, size_t *isize_p
,
1560 char *output
, size_t *osize_p
)
1565 return 0; /* we do not keep any states */
1567 if (*osize_p
< count
)
1570 memmove(output
, input
, count
);
1577 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1579 ; /* nothing -- null instances are shared */
1582 static struct stream_filter_vtbl null_vtbl
= {
1583 .filter
= null_filter_fn
,
1584 .free
= null_free_fn
,
1587 static struct stream_filter null_filter_singleton
= {
1591 int is_null_stream_filter(struct stream_filter
*filter
)
1593 return filter
== &null_filter_singleton
;
1601 struct lf_to_crlf_filter
{
1602 struct stream_filter filter
;
1603 unsigned has_held
:1;
1607 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1608 const char *input
, size_t *isize_p
,
1609 char *output
, size_t *osize_p
)
1611 size_t count
, o
= 0;
1612 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1615 * We may be holding onto the CR to see if it is followed by a
1616 * LF, in which case we would need to go to the main loop.
1617 * Otherwise, just emit it to the output stream.
1619 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1620 output
[o
++] = lf_to_crlf
->held
;
1621 lf_to_crlf
->has_held
= 0;
1624 /* We are told to drain */
1631 if (count
|| lf_to_crlf
->has_held
) {
1635 if (lf_to_crlf
->has_held
) {
1637 lf_to_crlf
->has_held
= 0;
1640 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1645 } else if (was_cr
) {
1647 * Previous round saw CR and it is not followed
1648 * by a LF; emit the CR before processing the
1649 * current character.
1655 * We may have consumed the last output slot,
1656 * in which case we need to break out of this
1657 * loop; hold the current character before
1660 if (*osize_p
<= o
) {
1661 lf_to_crlf
->has_held
= 1;
1662 lf_to_crlf
->held
= ch
;
1663 continue; /* break but increment i */
1678 if (!lf_to_crlf
->has_held
&& was_cr
) {
1679 lf_to_crlf
->has_held
= 1;
1680 lf_to_crlf
->held
= '\r';
1686 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1691 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1692 .filter
= lf_to_crlf_filter_fn
,
1693 .free
= lf_to_crlf_free_fn
,
1696 static struct stream_filter
*lf_to_crlf_filter(void)
1698 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1700 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1701 return (struct stream_filter
*)lf_to_crlf
;
1707 #define FILTER_BUFFER 1024
1708 struct cascade_filter
{
1709 struct stream_filter filter
;
1710 struct stream_filter
*one
;
1711 struct stream_filter
*two
;
1712 char buf
[FILTER_BUFFER
];
1716 static int cascade_filter_fn(struct stream_filter
*filter
,
1717 const char *input
, size_t *isize_p
,
1718 char *output
, size_t *osize_p
)
1720 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1722 size_t sz
= *osize_p
;
1723 size_t to_feed
, remaining
;
1726 * input -- (one) --> buf -- (two) --> output
1728 while (filled
< sz
) {
1729 remaining
= sz
- filled
;
1731 /* do we already have something to feed two with? */
1732 if (cas
->ptr
< cas
->end
) {
1733 to_feed
= cas
->end
- cas
->ptr
;
1734 if (stream_filter(cas
->two
,
1735 cas
->buf
+ cas
->ptr
, &to_feed
,
1736 output
+ filled
, &remaining
))
1738 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1739 filled
= sz
- remaining
;
1743 /* feed one from upstream and have it emit into our buffer */
1744 to_feed
= input
? *isize_p
: 0;
1745 if (input
&& !to_feed
)
1747 remaining
= sizeof(cas
->buf
);
1748 if (stream_filter(cas
->one
,
1750 cas
->buf
, &remaining
))
1752 cas
->end
= sizeof(cas
->buf
) - remaining
;
1755 size_t fed
= *isize_p
- to_feed
;
1760 /* do we know that we drained one completely? */
1761 if (input
|| cas
->end
)
1764 /* tell two to drain; we have nothing more to give it */
1766 remaining
= sz
- filled
;
1767 if (stream_filter(cas
->two
,
1769 output
+ filled
, &remaining
))
1771 if (remaining
== (sz
- filled
))
1772 break; /* completely drained two */
1773 filled
= sz
- remaining
;
1779 static void cascade_free_fn(struct stream_filter
*filter
)
1781 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1782 free_stream_filter(cas
->one
);
1783 free_stream_filter(cas
->two
);
1787 static struct stream_filter_vtbl cascade_vtbl
= {
1788 .filter
= cascade_filter_fn
,
1789 .free
= cascade_free_fn
,
1792 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1793 struct stream_filter
*two
)
1795 struct cascade_filter
*cascade
;
1797 if (!one
|| is_null_stream_filter(one
))
1799 if (!two
|| is_null_stream_filter(two
))
1802 cascade
= xmalloc(sizeof(*cascade
));
1805 cascade
->end
= cascade
->ptr
= 0;
1806 cascade
->filter
.vtbl
= &cascade_vtbl
;
1807 return (struct stream_filter
*)cascade
;
1813 #define IDENT_DRAINING (-1)
1814 #define IDENT_SKIPPING (-2)
1815 struct ident_filter
{
1816 struct stream_filter filter
;
1819 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1822 static int is_foreign_ident(const char *str
)
1826 if (!skip_prefix(str
, "$Id: ", &str
))
1828 for (i
= 0; str
[i
]; i
++) {
1829 if (isspace(str
[i
]) && str
[i
+1] != '$')
1835 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1837 size_t to_drain
= ident
->left
.len
;
1839 if (*osize_p
< to_drain
)
1840 to_drain
= *osize_p
;
1842 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1843 strbuf_remove(&ident
->left
, 0, to_drain
);
1844 *output_p
+= to_drain
;
1845 *osize_p
-= to_drain
;
1847 if (!ident
->left
.len
)
1851 static int ident_filter_fn(struct stream_filter
*filter
,
1852 const char *input
, size_t *isize_p
,
1853 char *output
, size_t *osize_p
)
1855 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1856 static const char head
[] = "$Id";
1859 /* drain upon eof */
1860 switch (ident
->state
) {
1862 strbuf_add(&ident
->left
, head
, ident
->state
);
1864 case IDENT_SKIPPING
:
1866 case IDENT_DRAINING
:
1867 ident_drain(ident
, &output
, osize_p
);
1872 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1875 if (ident
->state
== IDENT_DRAINING
) {
1876 ident_drain(ident
, &output
, osize_p
);
1885 if (ident
->state
== IDENT_SKIPPING
) {
1887 * Skipping until '$' or LF, but keeping them
1888 * in case it is a foreign ident.
1890 strbuf_addch(&ident
->left
, ch
);
1891 if (ch
!= '\n' && ch
!= '$')
1893 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1894 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1895 strbuf_addstr(&ident
->left
, ident
->ident
);
1897 ident
->state
= IDENT_DRAINING
;
1901 if (ident
->state
< sizeof(head
) &&
1902 head
[ident
->state
] == ch
) {
1908 strbuf_add(&ident
->left
, head
, ident
->state
);
1909 if (ident
->state
== sizeof(head
) - 1) {
1910 if (ch
!= ':' && ch
!= '$') {
1911 strbuf_addch(&ident
->left
, ch
);
1917 strbuf_addch(&ident
->left
, ch
);
1918 ident
->state
= IDENT_SKIPPING
;
1920 strbuf_addstr(&ident
->left
, ident
->ident
);
1921 ident
->state
= IDENT_DRAINING
;
1926 strbuf_addch(&ident
->left
, ch
);
1927 ident
->state
= IDENT_DRAINING
;
1932 static void ident_free_fn(struct stream_filter
*filter
)
1934 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1935 strbuf_release(&ident
->left
);
1939 static struct stream_filter_vtbl ident_vtbl
= {
1940 .filter
= ident_filter_fn
,
1941 .free
= ident_free_fn
,
1944 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1946 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1948 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1949 ": %s $", oid_to_hex(oid
));
1950 strbuf_init(&ident
->left
, 0);
1951 ident
->filter
.vtbl
= &ident_vtbl
;
1953 return (struct stream_filter
*)ident
;
1957 * Return an appropriately constructed filter for the given ca, or NULL if
1958 * the contents cannot be filtered without reading the whole thing
1961 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1962 * large binary blob you would want us not to slurp into the memory!
1964 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1965 const struct object_id
*oid
)
1967 struct stream_filter
*filter
= NULL
;
1969 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
1973 filter
= ident_filter(oid
);
1975 if (output_eol(ca
->crlf_action
) == EOL_CRLF
)
1976 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1978 filter
= cascade_filter(filter
, &null_filter_singleton
);
1983 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
1985 const struct object_id
*oid
)
1987 struct conv_attrs ca
;
1988 convert_attrs(istate
, &ca
, path
);
1989 return get_stream_filter_ca(&ca
, oid
);
1992 void free_stream_filter(struct stream_filter
*filter
)
1994 filter
->vtbl
->free(filter
);
1997 int stream_filter(struct stream_filter
*filter
,
1998 const char *input
, size_t *isize_p
,
1999 char *output
, size_t *osize_p
)
2001 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
2004 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
2005 const struct object_id
*treeish
,
2006 const struct object_id
*blob
)
2008 memset(meta
, 0, sizeof(*meta
));
2010 meta
->refname
= refname
;
2012 oidcpy(&meta
->treeish
, treeish
);
2014 oidcpy(&meta
->blob
, blob
);
2017 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2018 const struct checkout_metadata
*src
,
2019 const struct object_id
*blob
)
2021 memcpy(dst
, src
, sizeof(*dst
));
2023 oidcpy(&dst
->blob
, blob
);
2026 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2029 if (ca
->drv
->process
)
2030 return CA_CLASS_INCORE_PROCESS
;
2031 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2032 return CA_CLASS_INCORE_FILTER
;
2035 if (ca
->working_tree_encoding
)
2036 return CA_CLASS_INCORE
;
2038 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2039 return CA_CLASS_INCORE
;
2041 return CA_CLASS_STREAMABLE
;