1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
10 #include "object-store-ll.h"
12 #include "run-command.h"
14 #include "read-cache-ll.h"
17 #include "sub-process.h"
23 * convert.c - convert a file when checking it out and checking it in.
25 * This should use the pathname to decide on whether it wants to do some
26 * more interesting conversions (automatic gzip/unzip, general format
27 * conversions etc etc), but by default it just does automatic CRLF<->LF
28 * translation when the "text" attribute or "auto_crlf" option is set.
31 /* Stat bits: When BIN is set, the txt bits are unset */
32 #define CONVERT_STAT_BITS_TXT_LF 0x1
33 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
34 #define CONVERT_STAT_BITS_BIN 0x4
37 /* NUL, CR, LF and CRLF counts */
38 unsigned nul
, lonecr
, lonelf
, crlf
;
40 /* These are just approximations! */
41 unsigned printable
, nonprintable
;
44 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
48 memset(stats
, 0, sizeof(*stats
));
50 for (i
= 0; i
< size
; i
++) {
51 unsigned char c
= buf
[i
];
53 if (i
+1 < size
&& buf
[i
+1] == '\n') {
66 stats
->nonprintable
++;
69 /* BS, HT, ESC and FF */
70 case '\b': case '\t': case '\033': case '\014':
77 stats
->nonprintable
++;
84 /* If file ends with EOF then don't count this EOF as non-printable. */
85 if (size
>= 1 && buf
[size
-1] == '\032')
86 stats
->nonprintable
--;
90 * The same heuristics as diff.c::mmfile_is_binary()
91 * We treat files with bare CR as binary
93 static int convert_is_binary(const struct text_stat
*stats
)
99 if ((stats
->printable
>> 7) < stats
->nonprintable
)
104 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
106 struct text_stat stats
;
110 gather_stats(data
, size
, &stats
);
111 if (convert_is_binary(&stats
))
112 ret
|= CONVERT_STAT_BITS_BIN
;
114 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
116 ret
|= CONVERT_STAT_BITS_TXT_LF
;
121 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
123 unsigned int convert_stats
= gather_convert_stats(data
, size
);
125 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
127 switch (convert_stats
) {
128 case CONVERT_STAT_BITS_TXT_LF
:
130 case CONVERT_STAT_BITS_TXT_CRLF
:
132 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
139 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
144 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
145 ret
= gather_convert_stats_ascii(data
, sz
);
150 const char *get_wt_convert_stats_ascii(const char *path
)
152 const char *ret
= "";
153 struct strbuf sb
= STRBUF_INIT
;
154 if (strbuf_read_file(&sb
, path
, 0) >= 0)
155 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
160 static int text_eol_is_crlf(void)
162 if (auto_crlf
== AUTO_CRLF_TRUE
)
164 else if (auto_crlf
== AUTO_CRLF_INPUT
)
166 if (core_eol
== EOL_CRLF
)
168 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
173 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
175 switch (crlf_action
) {
180 case CRLF_TEXT_INPUT
:
185 case CRLF_AUTO_INPUT
:
190 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
192 warning(_("illegal crlf_action %d"), (int)crlf_action
);
196 static void check_global_conv_flags_eol(const char *path
,
197 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
200 if (old_stats
->crlf
&& !new_stats
->crlf
) {
202 * CRLFs would not be restored by checkout
204 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
205 die(_("CRLF would be replaced by LF in %s"), path
);
206 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
207 warning(_("in the working copy of '%s', CRLF will be"
208 " replaced by LF the next time Git touches"
210 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
212 * CRLFs would be added by checkout
214 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
215 die(_("LF would be replaced by CRLF in %s"), path
);
216 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
217 warning(_("in the working copy of '%s', LF will be"
218 " replaced by CRLF the next time Git touches"
223 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
230 data
= read_blob_data_from_index(istate
, path
, &sz
);
234 crp
= memchr(data
, '\r', sz
);
236 unsigned int ret_stats
;
237 ret_stats
= gather_convert_stats(data
, sz
);
238 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
239 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
246 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
247 enum convert_crlf_action crlf_action
)
249 if (output_eol(crlf_action
) != EOL_CRLF
)
251 /* No "naked" LF? Nothing to convert, regardless. */
255 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
256 /* If we have any CR or CRLF line endings, we do not touch it */
257 /* This is the new safer autocrlf-handling */
258 if (stats
->lonecr
|| stats
->crlf
)
261 if (convert_is_binary(stats
))
268 static int validate_encoding(const char *path
, const char *enc
,
269 const char *data
, size_t len
, int die_on_error
)
271 const char *stripped
;
273 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
274 if (skip_iprefix(enc
, "UTF", &stripped
)) {
275 skip_prefix(stripped
, "-", &stripped
);
278 * Check for detectable errors in UTF encodings
280 if (has_prohibited_utf_bom(enc
, data
, len
)) {
281 const char *error_msg
= _(
282 "BOM is prohibited in '%s' if encoded as %s");
284 * This advice is shown for UTF-??BE and UTF-??LE encodings.
285 * We cut off the last two characters of the encoding name
286 * to generate the encoding name suitable for BOMs.
288 const char *advise_msg
= _(
289 "The file '%s' contains a byte order "
290 "mark (BOM). Please use UTF-%.*s as "
291 "working-tree-encoding.");
292 int stripped_len
= strlen(stripped
) - strlen("BE");
293 advise(advise_msg
, path
, stripped_len
, stripped
);
295 die(error_msg
, path
, enc
);
297 return error(error_msg
, path
, enc
);
300 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
301 const char *error_msg
= _(
302 "BOM is required in '%s' if encoded as %s");
303 const char *advise_msg
= _(
304 "The file '%s' is missing a byte order "
305 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
306 "(depending on the byte order) as "
307 "working-tree-encoding.");
308 advise(advise_msg
, path
, stripped
, stripped
);
310 die(error_msg
, path
, enc
);
312 return error(error_msg
, path
, enc
);
320 static void trace_encoding(const char *context
, const char *path
,
321 const char *encoding
, const char *buf
, size_t len
)
323 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
324 struct strbuf trace
= STRBUF_INIT
;
327 if (!trace_want(&coe
))
330 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
331 for (i
= 0; i
< len
&& buf
; ++i
) {
333 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
335 (unsigned char) buf
[i
],
336 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
337 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
340 strbuf_addchars(&trace
, '\n', 1);
342 trace_strbuf(&coe
, &trace
);
343 strbuf_release(&trace
);
346 static int check_roundtrip(const char *enc_name
)
349 * check_roundtrip_encoding contains a string of comma and/or
350 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
351 * Search for the given encoding in that string.
353 const char *encoding
= check_roundtrip_encoding
?
354 check_roundtrip_encoding
: "SHIFT-JIS";
355 const char *found
= strcasestr(encoding
, enc_name
);
360 next
= found
+ strlen(enc_name
);
361 len
= strlen(encoding
);
364 * Check that the found encoding is at the beginning of
365 * encoding or that it is prefixed with a space or
368 found
== encoding
|| (
369 (isspace(found
[-1]) || found
[-1] == ',')
373 * Check that the found encoding is at the end of
374 * encoding or that it is suffixed with a space
377 next
== encoding
+ len
|| (
378 next
< encoding
+ len
&&
379 (isspace(next
[0]) || next
[0] == ',')
384 static const char *default_encoding
= "UTF-8";
386 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
387 struct strbuf
*buf
, const char *enc
, int conv_flags
)
391 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
394 * No encoding is specified or there is nothing to encode.
395 * Tell the caller that the content was not modified.
397 if (!enc
|| (src
&& !src_len
))
401 * Looks like we got called from "would_convert_to_git()".
402 * This means Git wants to know if it would encode (= modify!)
403 * the content. Let's answer with "yes", since an encoding was
409 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
412 trace_encoding("source", path
, enc
, src
, src_len
);
413 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
417 * We could add the blob "as-is" to Git. However, on checkout
418 * we would try to re-encode to the original encoding. This
419 * would fail and we would leave the user with a messed-up
420 * working tree. Let's try to avoid this by screaming loud.
422 const char* msg
= _("failed to encode '%s' from %s to %s");
424 die(msg
, path
, enc
, default_encoding
);
426 error(msg
, path
, enc
, default_encoding
);
430 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
433 * UTF supports lossless conversion round tripping [1] and conversions
434 * between UTF and other encodings are mostly round trip safe as
435 * Unicode aims to be a superset of all other character encodings.
436 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
437 * trip issues [2]. Check the round trip conversion for all encodings
438 * listed in core.checkRoundtripEncoding.
440 * The round trip check is only performed if content is written to Git.
441 * This ensures that no information is lost during conversion to/from
442 * the internal UTF-8 representation.
444 * Please note, the code below is not tested because I was not able to
445 * generate a faulty round trip without an iconv error. Iconv errors
446 * are already caught above.
448 * [1] http://unicode.org/faq/utf_bom.html#gen2
449 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
451 if (die_on_error
&& check_roundtrip(enc
)) {
455 re_src
= reencode_string_len(dst
, dst_len
,
456 enc
, default_encoding
,
459 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
460 trace_encoding("reencoded source", path
, enc
,
463 if (!re_src
|| src_len
!= re_src_len
||
464 memcmp(src
, re_src
, src_len
)) {
465 const char* msg
= _("encoding '%s' from %s to %s and "
466 "back is not the same");
467 die(msg
, path
, enc
, default_encoding
);
473 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
477 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
478 struct strbuf
*buf
, const char *enc
)
484 * No encoding is specified or there is nothing to encode.
485 * Tell the caller that the content was not modified.
487 if (!enc
|| (src
&& !src_len
))
490 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
493 error(_("failed to encode '%s' from %s to %s"),
494 path
, default_encoding
, enc
);
498 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
502 static int crlf_to_git(struct index_state
*istate
,
503 const char *path
, const char *src
, size_t len
,
505 enum convert_crlf_action crlf_action
, int conv_flags
)
507 struct text_stat stats
;
509 int convert_crlf_into_lf
;
511 if (crlf_action
== CRLF_BINARY
||
516 * If we are doing a dry-run and have no source buffer, there is
517 * nothing to analyze; we must assume we would convert.
522 gather_stats(src
, len
, &stats
);
523 /* Optimization: No CRLF? Nothing to convert, regardless. */
524 convert_crlf_into_lf
= !!stats
.crlf
;
526 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
527 if (convert_is_binary(&stats
))
530 * If the file in the index has any CR in it, do not
531 * convert. This is the new safer autocrlf handling,
532 * unless we want to renormalize in a merge or
535 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
536 has_crlf_in_index(istate
, path
))
537 convert_crlf_into_lf
= 0;
539 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
540 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
541 struct text_stat new_stats
;
542 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
543 /* simulate "git add" */
544 if (convert_crlf_into_lf
) {
545 new_stats
.lonelf
+= new_stats
.crlf
;
548 /* simulate "git checkout" */
549 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
550 new_stats
.crlf
+= new_stats
.lonelf
;
551 new_stats
.lonelf
= 0;
553 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
555 if (!convert_crlf_into_lf
)
559 * At this point all of our source analysis is done, and we are sure we
560 * would convert. If we are in dry-run mode, we can give an answer.
565 /* only grow if not in place */
566 if (strbuf_avail(buf
) + buf
->len
< len
)
567 strbuf_grow(buf
, len
- buf
->len
);
569 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
571 * If we guessed, we already know we rejected a file with
572 * lone CR, and we can strip a CR without looking at what
576 unsigned char c
= *src
++;
582 unsigned char c
= *src
++;
583 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
587 strbuf_setlen(buf
, dst
- buf
->buf
);
591 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
592 enum convert_crlf_action crlf_action
)
594 char *to_free
= NULL
;
595 struct text_stat stats
;
597 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
600 gather_stats(src
, len
, &stats
);
601 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
604 /* are we "faking" in place editing ? */
606 to_free
= strbuf_detach(buf
, NULL
);
608 strbuf_grow(buf
, len
+ stats
.lonelf
);
610 const char *nl
= memchr(src
, '\n', len
);
613 if (nl
> src
&& nl
[-1] == '\r') {
614 strbuf_add(buf
, src
, nl
+ 1 - src
);
616 strbuf_add(buf
, src
, nl
- src
);
617 strbuf_addstr(buf
, "\r\n");
622 strbuf_add(buf
, src
, len
);
628 struct filter_params
{
636 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
639 * Spawn cmd and feed the buffer contents through its stdin.
641 struct child_process child_process
= CHILD_PROCESS_INIT
;
642 struct filter_params
*params
= (struct filter_params
*)data
;
643 const char *format
= params
->cmd
;
644 int write_err
, status
;
646 /* apply % substitution to cmd */
647 struct strbuf cmd
= STRBUF_INIT
;
649 /* expand all %f with the quoted path; quote to preserve space, etc. */
650 while (strbuf_expand_step(&cmd
, &format
)) {
651 if (skip_prefix(format
, "%", &format
))
652 strbuf_addch(&cmd
, '%');
653 else if (skip_prefix(format
, "f", &format
))
654 sq_quote_buf(&cmd
, params
->path
);
656 strbuf_addch(&cmd
, '%');
659 strvec_push(&child_process
.args
, cmd
.buf
);
660 child_process
.use_shell
= 1;
661 child_process
.in
= -1;
662 child_process
.out
= out
;
664 if (start_command(&child_process
)) {
665 strbuf_release(&cmd
);
666 return error(_("cannot fork to run external filter '%s'"),
670 sigchain_push(SIGPIPE
, SIG_IGN
);
673 write_err
= (write_in_full(child_process
.in
,
674 params
->src
, params
->size
) < 0);
678 write_err
= copy_fd(params
->fd
, child_process
.in
);
679 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
683 if (close(child_process
.in
))
686 error(_("cannot feed the input to external filter '%s'"),
689 sigchain_pop(SIGPIPE
);
691 status
= finish_command(&child_process
);
693 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
695 strbuf_release(&cmd
);
696 return (write_err
|| status
);
699 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
700 struct strbuf
*dst
, const char *cmd
)
703 * Create a pipeline to have the command filter the buffer's
706 * (child --> cmd) --> us
709 struct strbuf nbuf
= STRBUF_INIT
;
711 struct filter_params params
;
713 memset(&async
, 0, sizeof(async
));
714 async
.proc
= filter_buffer_or_fd
;
715 async
.data
= ¶ms
;
724 if (start_async(&async
))
725 return 0; /* error was already reported */
727 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
728 err
= error(_("read from external filter '%s' failed"), cmd
);
730 if (close(async
.out
)) {
731 err
= error(_("read from external filter '%s' failed"), cmd
);
733 if (finish_async(&async
)) {
734 err
= error(_("external filter '%s' failed"), cmd
);
738 strbuf_swap(dst
, &nbuf
);
740 strbuf_release(&nbuf
);
744 #define CAP_CLEAN (1u<<0)
745 #define CAP_SMUDGE (1u<<1)
746 #define CAP_DELAY (1u<<2)
749 struct subprocess_entry subprocess
; /* must be the first member! */
750 unsigned int supported_capabilities
;
753 static int subprocess_map_initialized
;
754 static struct hashmap subprocess_map
;
756 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
758 static int versions
[] = {2, 0};
759 static struct subprocess_capability capabilities
[] = {
760 { "clean", CAP_CLEAN
},
761 { "smudge", CAP_SMUDGE
},
762 { "delay", CAP_DELAY
},
765 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
766 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
768 &entry
->supported_capabilities
);
771 static void handle_filter_error(const struct strbuf
*filter_status
,
772 struct cmd2process
*entry
,
773 const unsigned int wanted_capability
)
775 if (!strcmp(filter_status
->buf
, "error"))
776 ; /* The filter signaled a problem with the file. */
777 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
779 * The filter signaled a permanent problem. Don't try to filter
780 * files with the same command for the lifetime of the current
783 entry
->supported_capabilities
&= ~wanted_capability
;
786 * Something went wrong with the protocol filter.
787 * Force shutdown and restart if another blob requires filtering.
789 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
790 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
795 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
796 int fd
, struct strbuf
*dst
, const char *cmd
,
797 const unsigned int wanted_capability
,
798 const struct checkout_metadata
*meta
,
799 struct delayed_checkout
*dco
)
803 struct cmd2process
*entry
;
804 struct child_process
*process
;
805 struct strbuf nbuf
= STRBUF_INIT
;
806 struct strbuf filter_status
= STRBUF_INIT
;
807 const char *filter_type
;
809 if (!subprocess_map_initialized
) {
810 subprocess_map_initialized
= 1;
811 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
814 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
820 entry
= xmalloc(sizeof(*entry
));
821 entry
->supported_capabilities
= 0;
823 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
828 process
= &entry
->subprocess
.process
;
830 if (!(entry
->supported_capabilities
& wanted_capability
))
833 if (wanted_capability
& CAP_CLEAN
)
834 filter_type
= "clean";
835 else if (wanted_capability
& CAP_SMUDGE
)
836 filter_type
= "smudge";
838 die(_("unexpected filter type"));
840 sigchain_push(SIGPIPE
, SIG_IGN
);
842 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
843 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
847 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
849 error(_("path name too long for external filter"));
853 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
857 if (meta
&& meta
->refname
) {
858 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
863 if (meta
&& !is_null_oid(&meta
->treeish
)) {
864 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
869 if (meta
&& !is_null_oid(&meta
->blob
)) {
870 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
875 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
876 dco
&& dco
->state
== CE_CAN_DELAY
) {
878 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
883 err
= packet_flush_gently(process
->in
);
888 err
= write_packetized_from_fd_no_flush(fd
, process
->in
);
890 err
= write_packetized_from_buf_no_flush(src
, len
, process
->in
);
894 err
= packet_flush_gently(process
->in
);
898 err
= subprocess_read_status(process
->out
, &filter_status
);
902 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
903 string_list_insert(&dco
->filters
, cmd
);
904 string_list_insert(&dco
->paths
, path
);
906 /* The filter got the blob and wants to send us a response. */
907 err
= strcmp(filter_status
.buf
, "success");
911 err
= read_packetized_to_strbuf(process
->out
, &nbuf
,
912 PACKET_READ_GENTLE_ON_EOF
) < 0;
916 err
= subprocess_read_status(process
->out
, &filter_status
);
920 err
= strcmp(filter_status
.buf
, "success");
924 sigchain_pop(SIGPIPE
);
927 handle_filter_error(&filter_status
, entry
, wanted_capability
);
929 strbuf_swap(dst
, &nbuf
);
930 strbuf_release(&nbuf
);
931 strbuf_release(&filter_status
);
936 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
940 struct cmd2process
*entry
;
941 struct child_process
*process
;
942 struct strbuf filter_status
= STRBUF_INIT
;
944 assert(subprocess_map_initialized
);
945 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
947 error(_("external filter '%s' is not available anymore although "
948 "not all paths have been filtered"), cmd
);
951 process
= &entry
->subprocess
.process
;
952 sigchain_push(SIGPIPE
, SIG_IGN
);
954 err
= packet_write_fmt_gently(
955 process
->in
, "command=list_available_blobs\n");
959 err
= packet_flush_gently(process
->in
);
963 while ((line
= packet_read_line(process
->out
, NULL
))) {
965 if (skip_prefix(line
, "pathname=", &path
))
966 string_list_insert(available_paths
, xstrdup(path
));
968 ; /* ignore unknown keys */
971 err
= subprocess_read_status(process
->out
, &filter_status
);
975 err
= strcmp(filter_status
.buf
, "success");
978 sigchain_pop(SIGPIPE
);
981 handle_filter_error(&filter_status
, entry
, 0);
982 strbuf_release(&filter_status
);
986 static struct convert_driver
{
988 struct convert_driver
*next
;
993 } *user_convert
, **user_convert_tail
;
995 static int apply_filter(const char *path
, const char *src
, size_t len
,
996 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
997 const unsigned int wanted_capability
,
998 const struct checkout_metadata
*meta
,
999 struct delayed_checkout
*dco
)
1001 const char *cmd
= NULL
;
1009 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
1011 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1015 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1016 else if (drv
->process
&& *drv
->process
)
1017 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1018 drv
->process
, wanted_capability
, meta
, dco
);
1023 static int read_convert_config(const char *var
, const char *value
,
1024 const struct config_context
*ctx UNUSED
,
1027 const char *key
, *name
;
1029 struct convert_driver
*drv
;
1032 * External conversion drivers are configured using
1033 * "filter.<name>.variable".
1035 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1037 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1038 if (!xstrncmpz(drv
->name
, name
, namelen
))
1041 CALLOC_ARRAY(drv
, 1);
1042 drv
->name
= xmemdupz(name
, namelen
);
1043 *user_convert_tail
= drv
;
1044 user_convert_tail
= &(drv
->next
);
1048 * filter.<name>.smudge and filter.<name>.clean specifies
1053 * The command-line will not be interpolated in any way.
1056 if (!strcmp("smudge", key
))
1057 return git_config_string(&drv
->smudge
, var
, value
);
1059 if (!strcmp("clean", key
))
1060 return git_config_string(&drv
->clean
, var
, value
);
1062 if (!strcmp("process", key
))
1063 return git_config_string(&drv
->process
, var
, value
);
1065 if (!strcmp("required", key
)) {
1066 drv
->required
= git_config_bool(var
, value
);
1073 static int count_ident(const char *cp
, unsigned long size
)
1076 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1088 if (memcmp("Id", cp
, 2))
1099 * "$Id: ... "; scan up to the closing dollar sign and discard.
1115 static int ident_to_git(const char *src
, size_t len
,
1116 struct strbuf
*buf
, int ident
)
1120 if (!ident
|| (src
&& !count_ident(src
, len
)))
1126 /* only grow if not in place */
1127 if (strbuf_avail(buf
) + buf
->len
< len
)
1128 strbuf_grow(buf
, len
- buf
->len
);
1131 dollar
= memchr(src
, '$', len
);
1134 memmove(dst
, src
, dollar
+ 1 - src
);
1135 dst
+= dollar
+ 1 - src
;
1136 len
-= dollar
+ 1 - src
;
1139 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1140 dollar
= memchr(src
+ 3, '$', len
- 3);
1143 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1144 /* Line break before the next dollar. */
1148 memcpy(dst
, "Id$", 3);
1150 len
-= dollar
+ 1 - src
;
1154 memmove(dst
, src
, len
);
1155 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1159 static int ident_to_worktree(const char *src
, size_t len
,
1160 struct strbuf
*buf
, int ident
)
1162 struct object_id oid
;
1163 char *to_free
= NULL
, *dollar
, *spc
;
1169 cnt
= count_ident(src
, len
);
1173 /* are we "faking" in place editing ? */
1174 if (src
== buf
->buf
)
1175 to_free
= strbuf_detach(buf
, NULL
);
1176 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1178 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1180 /* step 1: run to the next '$' */
1181 dollar
= memchr(src
, '$', len
);
1184 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1185 len
-= dollar
+ 1 - src
;
1188 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1189 if (len
< 3 || memcmp("Id", src
, 2))
1192 /* step 3: skip over Id$ or Id:xxxxx$ */
1193 if (src
[2] == '$') {
1196 } else if (src
[2] == ':') {
1198 * It's possible that an expanded Id has crept its way into the
1199 * repository, we cope with that by stripping the expansion out.
1200 * This is probably not a good idea, since it will cause changes
1201 * on checkout, which won't go away by stash, but let's keep it
1202 * for git-style ids.
1204 dollar
= memchr(src
+ 3, '$', len
- 3);
1206 /* incomplete keyword, no more '$', so just quit the loop */
1210 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1211 /* Line break before the next dollar. */
1215 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1216 if (spc
&& spc
< dollar
-1) {
1217 /* There are spaces in unexpected places.
1218 * This is probably an id from some other
1219 * versioning system. Keep it for now.
1224 len
-= dollar
+ 1 - src
;
1227 /* it wasn't a "Id$" or "Id:xxxx$" */
1231 /* step 4: substitute */
1232 strbuf_addstr(buf
, "Id: ");
1233 strbuf_addstr(buf
, oid_to_hex(&oid
));
1234 strbuf_addstr(buf
, " $");
1236 strbuf_add(buf
, src
, len
);
1242 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1244 const char *value
= check
->value
;
1246 if (ATTR_UNSET(value
) || !strlen(value
))
1249 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1250 die(_("true/false are no valid working-tree-encodings"));
1253 /* Don't encode to the default encoding */
1254 if (same_encoding(value
, default_encoding
))
1260 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1262 const char *value
= check
->value
;
1264 if (ATTR_TRUE(value
))
1266 else if (ATTR_FALSE(value
))
1268 else if (ATTR_UNSET(value
))
1270 else if (!strcmp(value
, "input"))
1271 return CRLF_TEXT_INPUT
;
1272 else if (!strcmp(value
, "auto"))
1274 return CRLF_UNDEFINED
;
1277 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1279 const char *value
= check
->value
;
1281 if (ATTR_UNSET(value
))
1283 else if (!strcmp(value
, "lf"))
1285 else if (!strcmp(value
, "crlf"))
1290 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1292 const char *value
= check
->value
;
1293 struct convert_driver
*drv
;
1295 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1297 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1298 if (!strcmp(value
, drv
->name
))
1303 static int git_path_check_ident(struct attr_check_item
*check
)
1305 const char *value
= check
->value
;
1307 return !!ATTR_TRUE(value
);
1310 static struct attr_check
*check
;
1312 void convert_attrs(struct index_state
*istate
,
1313 struct conv_attrs
*ca
, const char *path
)
1315 struct attr_check_item
*ccheck
= NULL
;
1318 check
= attr_check_initl("crlf", "ident", "filter",
1319 "eol", "text", "working-tree-encoding",
1321 user_convert_tail
= &user_convert
;
1322 git_config(read_convert_config
, NULL
);
1325 git_check_attr(istate
, path
, check
);
1326 ccheck
= check
->items
;
1327 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1328 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1329 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1330 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1331 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1332 if (ca
->crlf_action
!= CRLF_BINARY
) {
1333 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1334 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1335 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1336 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1337 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1338 else if (eol_attr
== EOL_LF
)
1339 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1340 else if (eol_attr
== EOL_CRLF
)
1341 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1343 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1345 /* Save attr and make a decision for action */
1346 ca
->attr_action
= ca
->crlf_action
;
1347 if (ca
->crlf_action
== CRLF_TEXT
)
1348 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1349 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1350 ca
->crlf_action
= CRLF_BINARY
;
1351 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1352 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1353 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1354 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1357 void reset_parsed_attributes(void)
1359 struct convert_driver
*drv
, *next
;
1361 attr_check_free(check
);
1363 reset_merge_attributes();
1365 for (drv
= user_convert
; drv
; drv
= next
) {
1367 free((void *)drv
->name
);
1370 user_convert
= NULL
;
1371 user_convert_tail
= NULL
;
1374 int would_convert_to_git_filter_fd(struct index_state
*istate
, const char *path
)
1376 struct conv_attrs ca
;
1378 convert_attrs(istate
, &ca
, path
);
1383 * Apply a filter to an fd only if the filter is required to succeed.
1384 * We must die if the filter fails, because the original data before
1385 * filtering is not available.
1387 if (!ca
.drv
->required
)
1390 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1393 const char *get_convert_attr_ascii(struct index_state
*istate
, const char *path
)
1395 struct conv_attrs ca
;
1397 convert_attrs(istate
, &ca
, path
);
1398 switch (ca
.attr_action
) {
1399 case CRLF_UNDEFINED
:
1405 case CRLF_TEXT_INPUT
:
1406 return "text eol=lf";
1407 case CRLF_TEXT_CRLF
:
1408 return "text eol=crlf";
1411 case CRLF_AUTO_CRLF
:
1412 return "text=auto eol=crlf";
1413 case CRLF_AUTO_INPUT
:
1414 return "text=auto eol=lf";
1419 int convert_to_git(struct index_state
*istate
,
1420 const char *path
, const char *src
, size_t len
,
1421 struct strbuf
*dst
, int conv_flags
)
1424 struct conv_attrs ca
;
1426 convert_attrs(istate
, &ca
, path
);
1428 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1429 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1430 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1437 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1443 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1444 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1450 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1453 void convert_to_git_filter_fd(struct index_state
*istate
,
1454 const char *path
, int fd
, struct strbuf
*dst
,
1457 struct conv_attrs ca
;
1458 convert_attrs(istate
, &ca
, path
);
1462 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1463 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1465 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1466 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1467 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1470 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1471 const char *path
, const char *src
,
1472 size_t len
, struct strbuf
*dst
,
1474 const struct checkout_metadata
*meta
,
1475 struct delayed_checkout
*dco
)
1477 int ret
= 0, ret_filter
= 0;
1479 ret
|= ident_to_worktree(src
, len
, dst
, ca
->ident
);
1485 * CRLF conversion can be skipped if normalizing, unless there
1486 * is a smudge or process filter (even if the process filter doesn't
1487 * support smudge). The filters might expect CRLFs.
1489 if ((ca
->drv
&& (ca
->drv
->smudge
|| ca
->drv
->process
)) || !normalizing
) {
1490 ret
|= crlf_to_worktree(src
, len
, dst
, ca
->crlf_action
);
1497 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
->working_tree_encoding
);
1503 ret_filter
= apply_filter(
1504 path
, src
, len
, -1, dst
, ca
->drv
, CAP_SMUDGE
, meta
, dco
);
1505 if (!ret_filter
&& ca
->drv
&& ca
->drv
->required
)
1506 die(_("%s: smudge filter %s failed"), path
, ca
->drv
->name
);
1508 return ret
| ret_filter
;
1511 int async_convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1512 const char *path
, const char *src
,
1513 size_t len
, struct strbuf
*dst
,
1514 const struct checkout_metadata
*meta
,
1517 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1521 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1522 const char *path
, const char *src
,
1523 size_t len
, struct strbuf
*dst
,
1524 const struct checkout_metadata
*meta
)
1526 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1530 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1531 const char *src
, size_t len
, struct strbuf
*dst
)
1533 struct conv_attrs ca
;
1536 convert_attrs(istate
, &ca
, path
);
1537 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1543 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1546 /*****************************************************************
1548 * Streaming conversion support
1550 *****************************************************************/
1552 typedef int (*filter_fn
)(struct stream_filter
*,
1553 const char *input
, size_t *isize_p
,
1554 char *output
, size_t *osize_p
);
1555 typedef void (*free_fn
)(struct stream_filter
*);
1557 struct stream_filter_vtbl
{
1562 struct stream_filter
{
1563 struct stream_filter_vtbl
*vtbl
;
1566 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1567 const char *input
, size_t *isize_p
,
1568 char *output
, size_t *osize_p
)
1573 return 0; /* we do not keep any states */
1575 if (*osize_p
< count
)
1578 memmove(output
, input
, count
);
1585 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1587 ; /* nothing -- null instances are shared */
1590 static struct stream_filter_vtbl null_vtbl
= {
1591 .filter
= null_filter_fn
,
1592 .free
= null_free_fn
,
1595 static struct stream_filter null_filter_singleton
= {
1599 int is_null_stream_filter(struct stream_filter
*filter
)
1601 return filter
== &null_filter_singleton
;
1609 struct lf_to_crlf_filter
{
1610 struct stream_filter filter
;
1611 unsigned has_held
:1;
1615 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1616 const char *input
, size_t *isize_p
,
1617 char *output
, size_t *osize_p
)
1619 size_t count
, o
= 0;
1620 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1623 * We may be holding onto the CR to see if it is followed by a
1624 * LF, in which case we would need to go to the main loop.
1625 * Otherwise, just emit it to the output stream.
1627 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1628 output
[o
++] = lf_to_crlf
->held
;
1629 lf_to_crlf
->has_held
= 0;
1632 /* We are told to drain */
1639 if (count
|| lf_to_crlf
->has_held
) {
1643 if (lf_to_crlf
->has_held
) {
1645 lf_to_crlf
->has_held
= 0;
1648 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1653 } else if (was_cr
) {
1655 * Previous round saw CR and it is not followed
1656 * by a LF; emit the CR before processing the
1657 * current character.
1663 * We may have consumed the last output slot,
1664 * in which case we need to break out of this
1665 * loop; hold the current character before
1668 if (*osize_p
<= o
) {
1669 lf_to_crlf
->has_held
= 1;
1670 lf_to_crlf
->held
= ch
;
1671 continue; /* break but increment i */
1686 if (!lf_to_crlf
->has_held
&& was_cr
) {
1687 lf_to_crlf
->has_held
= 1;
1688 lf_to_crlf
->held
= '\r';
1694 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1699 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1700 .filter
= lf_to_crlf_filter_fn
,
1701 .free
= lf_to_crlf_free_fn
,
1704 static struct stream_filter
*lf_to_crlf_filter(void)
1706 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1708 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1709 return (struct stream_filter
*)lf_to_crlf
;
1715 #define FILTER_BUFFER 1024
1716 struct cascade_filter
{
1717 struct stream_filter filter
;
1718 struct stream_filter
*one
;
1719 struct stream_filter
*two
;
1720 char buf
[FILTER_BUFFER
];
1724 static int cascade_filter_fn(struct stream_filter
*filter
,
1725 const char *input
, size_t *isize_p
,
1726 char *output
, size_t *osize_p
)
1728 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1730 size_t sz
= *osize_p
;
1731 size_t to_feed
, remaining
;
1734 * input -- (one) --> buf -- (two) --> output
1736 while (filled
< sz
) {
1737 remaining
= sz
- filled
;
1739 /* do we already have something to feed two with? */
1740 if (cas
->ptr
< cas
->end
) {
1741 to_feed
= cas
->end
- cas
->ptr
;
1742 if (stream_filter(cas
->two
,
1743 cas
->buf
+ cas
->ptr
, &to_feed
,
1744 output
+ filled
, &remaining
))
1746 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1747 filled
= sz
- remaining
;
1751 /* feed one from upstream and have it emit into our buffer */
1752 to_feed
= input
? *isize_p
: 0;
1753 if (input
&& !to_feed
)
1755 remaining
= sizeof(cas
->buf
);
1756 if (stream_filter(cas
->one
,
1758 cas
->buf
, &remaining
))
1760 cas
->end
= sizeof(cas
->buf
) - remaining
;
1763 size_t fed
= *isize_p
- to_feed
;
1768 /* do we know that we drained one completely? */
1769 if (input
|| cas
->end
)
1772 /* tell two to drain; we have nothing more to give it */
1774 remaining
= sz
- filled
;
1775 if (stream_filter(cas
->two
,
1777 output
+ filled
, &remaining
))
1779 if (remaining
== (sz
- filled
))
1780 break; /* completely drained two */
1781 filled
= sz
- remaining
;
1787 static void cascade_free_fn(struct stream_filter
*filter
)
1789 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1790 free_stream_filter(cas
->one
);
1791 free_stream_filter(cas
->two
);
1795 static struct stream_filter_vtbl cascade_vtbl
= {
1796 .filter
= cascade_filter_fn
,
1797 .free
= cascade_free_fn
,
1800 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1801 struct stream_filter
*two
)
1803 struct cascade_filter
*cascade
;
1805 if (!one
|| is_null_stream_filter(one
))
1807 if (!two
|| is_null_stream_filter(two
))
1810 cascade
= xmalloc(sizeof(*cascade
));
1813 cascade
->end
= cascade
->ptr
= 0;
1814 cascade
->filter
.vtbl
= &cascade_vtbl
;
1815 return (struct stream_filter
*)cascade
;
1821 #define IDENT_DRAINING (-1)
1822 #define IDENT_SKIPPING (-2)
1823 struct ident_filter
{
1824 struct stream_filter filter
;
1827 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1830 static int is_foreign_ident(const char *str
)
1834 if (!skip_prefix(str
, "$Id: ", &str
))
1836 for (i
= 0; str
[i
]; i
++) {
1837 if (isspace(str
[i
]) && str
[i
+1] != '$')
1843 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1845 size_t to_drain
= ident
->left
.len
;
1847 if (*osize_p
< to_drain
)
1848 to_drain
= *osize_p
;
1850 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1851 strbuf_remove(&ident
->left
, 0, to_drain
);
1852 *output_p
+= to_drain
;
1853 *osize_p
-= to_drain
;
1855 if (!ident
->left
.len
)
1859 static int ident_filter_fn(struct stream_filter
*filter
,
1860 const char *input
, size_t *isize_p
,
1861 char *output
, size_t *osize_p
)
1863 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1864 static const char head
[] = "$Id";
1867 /* drain upon eof */
1868 switch (ident
->state
) {
1870 strbuf_add(&ident
->left
, head
, ident
->state
);
1872 case IDENT_SKIPPING
:
1874 case IDENT_DRAINING
:
1875 ident_drain(ident
, &output
, osize_p
);
1880 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1883 if (ident
->state
== IDENT_DRAINING
) {
1884 ident_drain(ident
, &output
, osize_p
);
1893 if (ident
->state
== IDENT_SKIPPING
) {
1895 * Skipping until '$' or LF, but keeping them
1896 * in case it is a foreign ident.
1898 strbuf_addch(&ident
->left
, ch
);
1899 if (ch
!= '\n' && ch
!= '$')
1901 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1902 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1903 strbuf_addstr(&ident
->left
, ident
->ident
);
1905 ident
->state
= IDENT_DRAINING
;
1909 if (ident
->state
< sizeof(head
) &&
1910 head
[ident
->state
] == ch
) {
1916 strbuf_add(&ident
->left
, head
, ident
->state
);
1917 if (ident
->state
== sizeof(head
) - 1) {
1918 if (ch
!= ':' && ch
!= '$') {
1919 strbuf_addch(&ident
->left
, ch
);
1925 strbuf_addch(&ident
->left
, ch
);
1926 ident
->state
= IDENT_SKIPPING
;
1928 strbuf_addstr(&ident
->left
, ident
->ident
);
1929 ident
->state
= IDENT_DRAINING
;
1934 strbuf_addch(&ident
->left
, ch
);
1935 ident
->state
= IDENT_DRAINING
;
1940 static void ident_free_fn(struct stream_filter
*filter
)
1942 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1943 strbuf_release(&ident
->left
);
1947 static struct stream_filter_vtbl ident_vtbl
= {
1948 .filter
= ident_filter_fn
,
1949 .free
= ident_free_fn
,
1952 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1954 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1956 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1957 ": %s $", oid_to_hex(oid
));
1958 strbuf_init(&ident
->left
, 0);
1959 ident
->filter
.vtbl
= &ident_vtbl
;
1961 return (struct stream_filter
*)ident
;
1965 * Return an appropriately constructed filter for the given ca, or NULL if
1966 * the contents cannot be filtered without reading the whole thing
1969 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1970 * large binary blob you would want us not to slurp into the memory!
1972 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1973 const struct object_id
*oid
)
1975 struct stream_filter
*filter
= NULL
;
1977 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
1981 filter
= ident_filter(oid
);
1983 if (output_eol(ca
->crlf_action
) == EOL_CRLF
)
1984 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1986 filter
= cascade_filter(filter
, &null_filter_singleton
);
1991 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
1993 const struct object_id
*oid
)
1995 struct conv_attrs ca
;
1996 convert_attrs(istate
, &ca
, path
);
1997 return get_stream_filter_ca(&ca
, oid
);
2000 void free_stream_filter(struct stream_filter
*filter
)
2002 filter
->vtbl
->free(filter
);
2005 int stream_filter(struct stream_filter
*filter
,
2006 const char *input
, size_t *isize_p
,
2007 char *output
, size_t *osize_p
)
2009 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
2012 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
2013 const struct object_id
*treeish
,
2014 const struct object_id
*blob
)
2016 memset(meta
, 0, sizeof(*meta
));
2018 meta
->refname
= refname
;
2020 oidcpy(&meta
->treeish
, treeish
);
2022 oidcpy(&meta
->blob
, blob
);
2025 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2026 const struct checkout_metadata
*src
,
2027 const struct object_id
*blob
)
2029 memcpy(dst
, src
, sizeof(*dst
));
2031 oidcpy(&dst
->blob
, blob
);
2034 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2037 if (ca
->drv
->process
)
2038 return CA_CLASS_INCORE_PROCESS
;
2039 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2040 return CA_CLASS_INCORE_FILTER
;
2043 if (ca
->working_tree_encoding
)
2044 return CA_CLASS_INCORE
;
2046 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2047 return CA_CLASS_INCORE
;
2049 return CA_CLASS_STREAMABLE
;