1 #include "git-compat-util.h"
8 #include "object-store-ll.h"
10 #include "run-command.h"
12 #include "read-cache-ll.h"
15 #include "sub-process.h"
22 * convert.c - convert a file when checking it out and checking it in.
24 * This should use the pathname to decide on whether it wants to do some
25 * more interesting conversions (automatic gzip/unzip, general format
26 * conversions etc etc), but by default it just does automatic CRLF<->LF
27 * translation when the "text" attribute or "auto_crlf" option is set.
30 /* Stat bits: When BIN is set, the txt bits are unset */
31 #define CONVERT_STAT_BITS_TXT_LF 0x1
32 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
33 #define CONVERT_STAT_BITS_BIN 0x4
36 /* NUL, CR, LF and CRLF counts */
37 unsigned nul
, lonecr
, lonelf
, crlf
;
39 /* These are just approximations! */
40 unsigned printable
, nonprintable
;
43 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
47 memset(stats
, 0, sizeof(*stats
));
49 for (i
= 0; i
< size
; i
++) {
50 unsigned char c
= buf
[i
];
52 if (i
+1 < size
&& buf
[i
+1] == '\n') {
65 stats
->nonprintable
++;
68 /* BS, HT, ESC and FF */
69 case '\b': case '\t': case '\033': case '\014':
76 stats
->nonprintable
++;
83 /* If file ends with EOF then don't count this EOF as non-printable. */
84 if (size
>= 1 && buf
[size
-1] == '\032')
85 stats
->nonprintable
--;
89 * The same heuristics as diff.c::mmfile_is_binary()
90 * We treat files with bare CR as binary
92 static int convert_is_binary(const struct text_stat
*stats
)
98 if ((stats
->printable
>> 7) < stats
->nonprintable
)
103 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
105 struct text_stat stats
;
109 gather_stats(data
, size
, &stats
);
110 if (convert_is_binary(&stats
))
111 ret
|= CONVERT_STAT_BITS_BIN
;
113 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
115 ret
|= CONVERT_STAT_BITS_TXT_LF
;
120 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
122 unsigned int convert_stats
= gather_convert_stats(data
, size
);
124 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
126 switch (convert_stats
) {
127 case CONVERT_STAT_BITS_TXT_LF
:
129 case CONVERT_STAT_BITS_TXT_CRLF
:
131 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
138 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
143 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
144 ret
= gather_convert_stats_ascii(data
, sz
);
149 const char *get_wt_convert_stats_ascii(const char *path
)
151 const char *ret
= "";
152 struct strbuf sb
= STRBUF_INIT
;
153 if (strbuf_read_file(&sb
, path
, 0) >= 0)
154 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
159 static int text_eol_is_crlf(void)
161 if (auto_crlf
== AUTO_CRLF_TRUE
)
163 else if (auto_crlf
== AUTO_CRLF_INPUT
)
165 if (core_eol
== EOL_CRLF
)
167 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
172 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
174 switch (crlf_action
) {
179 case CRLF_TEXT_INPUT
:
184 case CRLF_AUTO_INPUT
:
189 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
191 warning(_("illegal crlf_action %d"), (int)crlf_action
);
195 static void check_global_conv_flags_eol(const char *path
,
196 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
199 if (old_stats
->crlf
&& !new_stats
->crlf
) {
201 * CRLFs would not be restored by checkout
203 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
204 die(_("CRLF would be replaced by LF in %s"), path
);
205 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
206 warning(_("in the working copy of '%s', CRLF will be"
207 " replaced by LF the next time Git touches"
209 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
211 * CRLFs would be added by checkout
213 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
214 die(_("LF would be replaced by CRLF in %s"), path
);
215 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
216 warning(_("in the working copy of '%s', LF will be"
217 " replaced by CRLF the next time Git touches"
222 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
229 data
= read_blob_data_from_index(istate
, path
, &sz
);
233 crp
= memchr(data
, '\r', sz
);
235 unsigned int ret_stats
;
236 ret_stats
= gather_convert_stats(data
, sz
);
237 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
238 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
245 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
246 enum convert_crlf_action crlf_action
)
248 if (output_eol(crlf_action
) != EOL_CRLF
)
250 /* No "naked" LF? Nothing to convert, regardless. */
254 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
255 /* If we have any CR or CRLF line endings, we do not touch it */
256 /* This is the new safer autocrlf-handling */
257 if (stats
->lonecr
|| stats
->crlf
)
260 if (convert_is_binary(stats
))
267 static int validate_encoding(const char *path
, const char *enc
,
268 const char *data
, size_t len
, int die_on_error
)
270 const char *stripped
;
272 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
273 if (skip_iprefix(enc
, "UTF", &stripped
)) {
274 skip_prefix(stripped
, "-", &stripped
);
277 * Check for detectable errors in UTF encodings
279 if (has_prohibited_utf_bom(enc
, data
, len
)) {
280 const char *error_msg
= _(
281 "BOM is prohibited in '%s' if encoded as %s");
283 * This advice is shown for UTF-??BE and UTF-??LE encodings.
284 * We cut off the last two characters of the encoding name
285 * to generate the encoding name suitable for BOMs.
287 const char *advise_msg
= _(
288 "The file '%s' contains a byte order "
289 "mark (BOM). Please use UTF-%.*s as "
290 "working-tree-encoding.");
291 int stripped_len
= strlen(stripped
) - strlen("BE");
292 advise(advise_msg
, path
, stripped_len
, stripped
);
294 die(error_msg
, path
, enc
);
296 return error(error_msg
, path
, enc
);
299 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
300 const char *error_msg
= _(
301 "BOM is required in '%s' if encoded as %s");
302 const char *advise_msg
= _(
303 "The file '%s' is missing a byte order "
304 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
305 "(depending on the byte order) as "
306 "working-tree-encoding.");
307 advise(advise_msg
, path
, stripped
, stripped
);
309 die(error_msg
, path
, enc
);
311 return error(error_msg
, path
, enc
);
319 static void trace_encoding(const char *context
, const char *path
,
320 const char *encoding
, const char *buf
, size_t len
)
322 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
323 struct strbuf trace
= STRBUF_INIT
;
326 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
327 for (i
= 0; i
< len
&& buf
; ++i
) {
329 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
331 (unsigned char) buf
[i
],
332 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
333 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
336 strbuf_addchars(&trace
, '\n', 1);
338 trace_strbuf(&coe
, &trace
);
339 strbuf_release(&trace
);
342 static int check_roundtrip(const char *enc_name
)
345 * check_roundtrip_encoding contains a string of comma and/or
346 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
347 * Search for the given encoding in that string.
349 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
354 next
= found
+ strlen(enc_name
);
355 len
= strlen(check_roundtrip_encoding
);
358 * check that the found encoding is at the
359 * beginning of check_roundtrip_encoding or
360 * that it is prefixed with a space or comma
362 found
== check_roundtrip_encoding
|| (
363 (isspace(found
[-1]) || found
[-1] == ',')
367 * check that the found encoding is at the
368 * end of check_roundtrip_encoding or
369 * that it is suffixed with a space or comma
371 next
== check_roundtrip_encoding
+ len
|| (
372 next
< check_roundtrip_encoding
+ len
&&
373 (isspace(next
[0]) || next
[0] == ',')
378 static const char *default_encoding
= "UTF-8";
380 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
381 struct strbuf
*buf
, const char *enc
, int conv_flags
)
385 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
388 * No encoding is specified or there is nothing to encode.
389 * Tell the caller that the content was not modified.
391 if (!enc
|| (src
&& !src_len
))
395 * Looks like we got called from "would_convert_to_git()".
396 * This means Git wants to know if it would encode (= modify!)
397 * the content. Let's answer with "yes", since an encoding was
403 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
406 trace_encoding("source", path
, enc
, src
, src_len
);
407 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
411 * We could add the blob "as-is" to Git. However, on checkout
412 * we would try to re-encode to the original encoding. This
413 * would fail and we would leave the user with a messed-up
414 * working tree. Let's try to avoid this by screaming loud.
416 const char* msg
= _("failed to encode '%s' from %s to %s");
418 die(msg
, path
, enc
, default_encoding
);
420 error(msg
, path
, enc
, default_encoding
);
424 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
427 * UTF supports lossless conversion round tripping [1] and conversions
428 * between UTF and other encodings are mostly round trip safe as
429 * Unicode aims to be a superset of all other character encodings.
430 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
431 * trip issues [2]. Check the round trip conversion for all encodings
432 * listed in core.checkRoundtripEncoding.
434 * The round trip check is only performed if content is written to Git.
435 * This ensures that no information is lost during conversion to/from
436 * the internal UTF-8 representation.
438 * Please note, the code below is not tested because I was not able to
439 * generate a faulty round trip without an iconv error. Iconv errors
440 * are already caught above.
442 * [1] http://unicode.org/faq/utf_bom.html#gen2
443 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
445 if (die_on_error
&& check_roundtrip(enc
)) {
449 re_src
= reencode_string_len(dst
, dst_len
,
450 enc
, default_encoding
,
453 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
454 trace_encoding("reencoded source", path
, enc
,
457 if (!re_src
|| src_len
!= re_src_len
||
458 memcmp(src
, re_src
, src_len
)) {
459 const char* msg
= _("encoding '%s' from %s to %s and "
460 "back is not the same");
461 die(msg
, path
, enc
, default_encoding
);
467 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
471 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
472 struct strbuf
*buf
, const char *enc
)
478 * No encoding is specified or there is nothing to encode.
479 * Tell the caller that the content was not modified.
481 if (!enc
|| (src
&& !src_len
))
484 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
487 error(_("failed to encode '%s' from %s to %s"),
488 path
, default_encoding
, enc
);
492 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
496 static int crlf_to_git(struct index_state
*istate
,
497 const char *path
, const char *src
, size_t len
,
499 enum convert_crlf_action crlf_action
, int conv_flags
)
501 struct text_stat stats
;
503 int convert_crlf_into_lf
;
505 if (crlf_action
== CRLF_BINARY
||
510 * If we are doing a dry-run and have no source buffer, there is
511 * nothing to analyze; we must assume we would convert.
516 gather_stats(src
, len
, &stats
);
517 /* Optimization: No CRLF? Nothing to convert, regardless. */
518 convert_crlf_into_lf
= !!stats
.crlf
;
520 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
521 if (convert_is_binary(&stats
))
524 * If the file in the index has any CR in it, do not
525 * convert. This is the new safer autocrlf handling,
526 * unless we want to renormalize in a merge or
529 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
530 has_crlf_in_index(istate
, path
))
531 convert_crlf_into_lf
= 0;
533 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
534 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
535 struct text_stat new_stats
;
536 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
537 /* simulate "git add" */
538 if (convert_crlf_into_lf
) {
539 new_stats
.lonelf
+= new_stats
.crlf
;
542 /* simulate "git checkout" */
543 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
544 new_stats
.crlf
+= new_stats
.lonelf
;
545 new_stats
.lonelf
= 0;
547 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
549 if (!convert_crlf_into_lf
)
553 * At this point all of our source analysis is done, and we are sure we
554 * would convert. If we are in dry-run mode, we can give an answer.
559 /* only grow if not in place */
560 if (strbuf_avail(buf
) + buf
->len
< len
)
561 strbuf_grow(buf
, len
- buf
->len
);
563 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
565 * If we guessed, we already know we rejected a file with
566 * lone CR, and we can strip a CR without looking at what
570 unsigned char c
= *src
++;
576 unsigned char c
= *src
++;
577 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
581 strbuf_setlen(buf
, dst
- buf
->buf
);
585 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
586 enum convert_crlf_action crlf_action
)
588 char *to_free
= NULL
;
589 struct text_stat stats
;
591 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
594 gather_stats(src
, len
, &stats
);
595 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
598 /* are we "faking" in place editing ? */
600 to_free
= strbuf_detach(buf
, NULL
);
602 strbuf_grow(buf
, len
+ stats
.lonelf
);
604 const char *nl
= memchr(src
, '\n', len
);
607 if (nl
> src
&& nl
[-1] == '\r') {
608 strbuf_add(buf
, src
, nl
+ 1 - src
);
610 strbuf_add(buf
, src
, nl
- src
);
611 strbuf_addstr(buf
, "\r\n");
616 strbuf_add(buf
, src
, len
);
622 struct filter_params
{
630 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
633 * Spawn cmd and feed the buffer contents through its stdin.
635 struct child_process child_process
= CHILD_PROCESS_INIT
;
636 struct filter_params
*params
= (struct filter_params
*)data
;
637 const char *format
= params
->cmd
;
638 int write_err
, status
;
640 /* apply % substitution to cmd */
641 struct strbuf cmd
= STRBUF_INIT
;
643 /* expand all %f with the quoted path; quote to preserve space, etc. */
644 while (strbuf_expand_step(&cmd
, &format
)) {
645 if (skip_prefix(format
, "%", &format
))
646 strbuf_addch(&cmd
, '%');
647 else if (skip_prefix(format
, "f", &format
))
648 sq_quote_buf(&cmd
, params
->path
);
650 strbuf_addch(&cmd
, '%');
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
,
1018 const struct config_context
*ctx UNUSED
,
1021 const char *key
, *name
;
1023 struct convert_driver
*drv
;
1026 * External conversion drivers are configured using
1027 * "filter.<name>.variable".
1029 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1031 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1032 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1035 CALLOC_ARRAY(drv
, 1);
1036 drv
->name
= xmemdupz(name
, namelen
);
1037 *user_convert_tail
= drv
;
1038 user_convert_tail
= &(drv
->next
);
1042 * filter.<name>.smudge and filter.<name>.clean specifies
1047 * The command-line will not be interpolated in any way.
1050 if (!strcmp("smudge", key
))
1051 return git_config_string(&drv
->smudge
, var
, value
);
1053 if (!strcmp("clean", key
))
1054 return git_config_string(&drv
->clean
, var
, value
);
1056 if (!strcmp("process", key
))
1057 return git_config_string(&drv
->process
, var
, value
);
1059 if (!strcmp("required", key
)) {
1060 drv
->required
= git_config_bool(var
, value
);
1067 static int count_ident(const char *cp
, unsigned long size
)
1070 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1082 if (memcmp("Id", cp
, 2))
1093 * "$Id: ... "; scan up to the closing dollar sign and discard.
1109 static int ident_to_git(const char *src
, size_t len
,
1110 struct strbuf
*buf
, int ident
)
1114 if (!ident
|| (src
&& !count_ident(src
, len
)))
1120 /* only grow if not in place */
1121 if (strbuf_avail(buf
) + buf
->len
< len
)
1122 strbuf_grow(buf
, len
- buf
->len
);
1125 dollar
= memchr(src
, '$', len
);
1128 memmove(dst
, src
, dollar
+ 1 - src
);
1129 dst
+= dollar
+ 1 - src
;
1130 len
-= dollar
+ 1 - src
;
1133 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1134 dollar
= memchr(src
+ 3, '$', len
- 3);
1137 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1138 /* Line break before the next dollar. */
1142 memcpy(dst
, "Id$", 3);
1144 len
-= dollar
+ 1 - src
;
1148 memmove(dst
, src
, len
);
1149 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1153 static int ident_to_worktree(const char *src
, size_t len
,
1154 struct strbuf
*buf
, int ident
)
1156 struct object_id oid
;
1157 char *to_free
= NULL
, *dollar
, *spc
;
1163 cnt
= count_ident(src
, len
);
1167 /* are we "faking" in place editing ? */
1168 if (src
== buf
->buf
)
1169 to_free
= strbuf_detach(buf
, NULL
);
1170 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1172 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1174 /* step 1: run to the next '$' */
1175 dollar
= memchr(src
, '$', len
);
1178 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1179 len
-= dollar
+ 1 - src
;
1182 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1183 if (len
< 3 || memcmp("Id", src
, 2))
1186 /* step 3: skip over Id$ or Id:xxxxx$ */
1187 if (src
[2] == '$') {
1190 } else if (src
[2] == ':') {
1192 * It's possible that an expanded Id has crept its way into the
1193 * repository, we cope with that by stripping the expansion out.
1194 * This is probably not a good idea, since it will cause changes
1195 * on checkout, which won't go away by stash, but let's keep it
1196 * for git-style ids.
1198 dollar
= memchr(src
+ 3, '$', len
- 3);
1200 /* incomplete keyword, no more '$', so just quit the loop */
1204 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1205 /* Line break before the next dollar. */
1209 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1210 if (spc
&& spc
< dollar
-1) {
1211 /* There are spaces in unexpected places.
1212 * This is probably an id from some other
1213 * versioning system. Keep it for now.
1218 len
-= dollar
+ 1 - src
;
1221 /* it wasn't a "Id$" or "Id:xxxx$" */
1225 /* step 4: substitute */
1226 strbuf_addstr(buf
, "Id: ");
1227 strbuf_addstr(buf
, oid_to_hex(&oid
));
1228 strbuf_addstr(buf
, " $");
1230 strbuf_add(buf
, src
, len
);
1236 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1238 const char *value
= check
->value
;
1240 if (ATTR_UNSET(value
) || !strlen(value
))
1243 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1244 die(_("true/false are no valid working-tree-encodings"));
1247 /* Don't encode to the default encoding */
1248 if (same_encoding(value
, default_encoding
))
1254 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1256 const char *value
= check
->value
;
1258 if (ATTR_TRUE(value
))
1260 else if (ATTR_FALSE(value
))
1262 else if (ATTR_UNSET(value
))
1264 else if (!strcmp(value
, "input"))
1265 return CRLF_TEXT_INPUT
;
1266 else if (!strcmp(value
, "auto"))
1268 return CRLF_UNDEFINED
;
1271 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1273 const char *value
= check
->value
;
1275 if (ATTR_UNSET(value
))
1277 else if (!strcmp(value
, "lf"))
1279 else if (!strcmp(value
, "crlf"))
1284 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1286 const char *value
= check
->value
;
1287 struct convert_driver
*drv
;
1289 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1291 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1292 if (!strcmp(value
, drv
->name
))
1297 static int git_path_check_ident(struct attr_check_item
*check
)
1299 const char *value
= check
->value
;
1301 return !!ATTR_TRUE(value
);
1304 static struct attr_check
*check
;
1306 void convert_attrs(struct index_state
*istate
,
1307 struct conv_attrs
*ca
, const char *path
)
1309 struct attr_check_item
*ccheck
= NULL
;
1312 check
= attr_check_initl("crlf", "ident", "filter",
1313 "eol", "text", "working-tree-encoding",
1315 user_convert_tail
= &user_convert
;
1316 git_config(read_convert_config
, NULL
);
1319 git_check_attr(istate
, path
, check
);
1320 ccheck
= check
->items
;
1321 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1322 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1323 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1324 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1325 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1326 if (ca
->crlf_action
!= CRLF_BINARY
) {
1327 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1328 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1329 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1330 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1331 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1332 else if (eol_attr
== EOL_LF
)
1333 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1334 else if (eol_attr
== EOL_CRLF
)
1335 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1337 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1339 /* Save attr and make a decision for action */
1340 ca
->attr_action
= ca
->crlf_action
;
1341 if (ca
->crlf_action
== CRLF_TEXT
)
1342 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1343 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1344 ca
->crlf_action
= CRLF_BINARY
;
1345 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1346 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1347 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1348 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1351 void reset_parsed_attributes(void)
1353 struct convert_driver
*drv
, *next
;
1355 attr_check_free(check
);
1357 reset_merge_attributes();
1359 for (drv
= user_convert
; drv
; drv
= next
) {
1361 free((void *)drv
->name
);
1364 user_convert
= NULL
;
1365 user_convert_tail
= NULL
;
1368 int would_convert_to_git_filter_fd(struct index_state
*istate
, const char *path
)
1370 struct conv_attrs ca
;
1372 convert_attrs(istate
, &ca
, path
);
1377 * Apply a filter to an fd only if the filter is required to succeed.
1378 * We must die if the filter fails, because the original data before
1379 * filtering is not available.
1381 if (!ca
.drv
->required
)
1384 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1387 const char *get_convert_attr_ascii(struct index_state
*istate
, const char *path
)
1389 struct conv_attrs ca
;
1391 convert_attrs(istate
, &ca
, path
);
1392 switch (ca
.attr_action
) {
1393 case CRLF_UNDEFINED
:
1399 case CRLF_TEXT_INPUT
:
1400 return "text eol=lf";
1401 case CRLF_TEXT_CRLF
:
1402 return "text eol=crlf";
1405 case CRLF_AUTO_CRLF
:
1406 return "text=auto eol=crlf";
1407 case CRLF_AUTO_INPUT
:
1408 return "text=auto eol=lf";
1413 int convert_to_git(struct index_state
*istate
,
1414 const char *path
, const char *src
, size_t len
,
1415 struct strbuf
*dst
, int conv_flags
)
1418 struct conv_attrs ca
;
1420 convert_attrs(istate
, &ca
, path
);
1422 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1423 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1424 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1431 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1437 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1438 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1444 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1447 void convert_to_git_filter_fd(struct index_state
*istate
,
1448 const char *path
, int fd
, struct strbuf
*dst
,
1451 struct conv_attrs ca
;
1452 convert_attrs(istate
, &ca
, path
);
1456 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1457 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1459 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1460 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1461 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1464 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1465 const char *path
, const char *src
,
1466 size_t len
, struct strbuf
*dst
,
1468 const struct checkout_metadata
*meta
,
1469 struct delayed_checkout
*dco
)
1471 int ret
= 0, ret_filter
= 0;
1473 ret
|= ident_to_worktree(src
, len
, dst
, ca
->ident
);
1479 * CRLF conversion can be skipped if normalizing, unless there
1480 * is a smudge or process filter (even if the process filter doesn't
1481 * support smudge). The filters might expect CRLFs.
1483 if ((ca
->drv
&& (ca
->drv
->smudge
|| ca
->drv
->process
)) || !normalizing
) {
1484 ret
|= crlf_to_worktree(src
, len
, dst
, ca
->crlf_action
);
1491 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
->working_tree_encoding
);
1497 ret_filter
= apply_filter(
1498 path
, src
, len
, -1, dst
, ca
->drv
, CAP_SMUDGE
, meta
, dco
);
1499 if (!ret_filter
&& ca
->drv
&& ca
->drv
->required
)
1500 die(_("%s: smudge filter %s failed"), path
, ca
->drv
->name
);
1502 return ret
| ret_filter
;
1505 int async_convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1506 const char *path
, const char *src
,
1507 size_t len
, struct strbuf
*dst
,
1508 const struct checkout_metadata
*meta
,
1511 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1515 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1516 const char *path
, const char *src
,
1517 size_t len
, struct strbuf
*dst
,
1518 const struct checkout_metadata
*meta
)
1520 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1524 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1525 const char *src
, size_t len
, struct strbuf
*dst
)
1527 struct conv_attrs ca
;
1530 convert_attrs(istate
, &ca
, path
);
1531 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1537 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1540 /*****************************************************************
1542 * Streaming conversion support
1544 *****************************************************************/
1546 typedef int (*filter_fn
)(struct stream_filter
*,
1547 const char *input
, size_t *isize_p
,
1548 char *output
, size_t *osize_p
);
1549 typedef void (*free_fn
)(struct stream_filter
*);
1551 struct stream_filter_vtbl
{
1556 struct stream_filter
{
1557 struct stream_filter_vtbl
*vtbl
;
1560 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1561 const char *input
, size_t *isize_p
,
1562 char *output
, size_t *osize_p
)
1567 return 0; /* we do not keep any states */
1569 if (*osize_p
< count
)
1572 memmove(output
, input
, count
);
1579 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1581 ; /* nothing -- null instances are shared */
1584 static struct stream_filter_vtbl null_vtbl
= {
1585 .filter
= null_filter_fn
,
1586 .free
= null_free_fn
,
1589 static struct stream_filter null_filter_singleton
= {
1593 int is_null_stream_filter(struct stream_filter
*filter
)
1595 return filter
== &null_filter_singleton
;
1603 struct lf_to_crlf_filter
{
1604 struct stream_filter filter
;
1605 unsigned has_held
:1;
1609 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1610 const char *input
, size_t *isize_p
,
1611 char *output
, size_t *osize_p
)
1613 size_t count
, o
= 0;
1614 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1617 * We may be holding onto the CR to see if it is followed by a
1618 * LF, in which case we would need to go to the main loop.
1619 * Otherwise, just emit it to the output stream.
1621 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1622 output
[o
++] = lf_to_crlf
->held
;
1623 lf_to_crlf
->has_held
= 0;
1626 /* We are told to drain */
1633 if (count
|| lf_to_crlf
->has_held
) {
1637 if (lf_to_crlf
->has_held
) {
1639 lf_to_crlf
->has_held
= 0;
1642 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1647 } else if (was_cr
) {
1649 * Previous round saw CR and it is not followed
1650 * by a LF; emit the CR before processing the
1651 * current character.
1657 * We may have consumed the last output slot,
1658 * in which case we need to break out of this
1659 * loop; hold the current character before
1662 if (*osize_p
<= o
) {
1663 lf_to_crlf
->has_held
= 1;
1664 lf_to_crlf
->held
= ch
;
1665 continue; /* break but increment i */
1680 if (!lf_to_crlf
->has_held
&& was_cr
) {
1681 lf_to_crlf
->has_held
= 1;
1682 lf_to_crlf
->held
= '\r';
1688 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1693 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1694 .filter
= lf_to_crlf_filter_fn
,
1695 .free
= lf_to_crlf_free_fn
,
1698 static struct stream_filter
*lf_to_crlf_filter(void)
1700 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1702 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1703 return (struct stream_filter
*)lf_to_crlf
;
1709 #define FILTER_BUFFER 1024
1710 struct cascade_filter
{
1711 struct stream_filter filter
;
1712 struct stream_filter
*one
;
1713 struct stream_filter
*two
;
1714 char buf
[FILTER_BUFFER
];
1718 static int cascade_filter_fn(struct stream_filter
*filter
,
1719 const char *input
, size_t *isize_p
,
1720 char *output
, size_t *osize_p
)
1722 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1724 size_t sz
= *osize_p
;
1725 size_t to_feed
, remaining
;
1728 * input -- (one) --> buf -- (two) --> output
1730 while (filled
< sz
) {
1731 remaining
= sz
- filled
;
1733 /* do we already have something to feed two with? */
1734 if (cas
->ptr
< cas
->end
) {
1735 to_feed
= cas
->end
- cas
->ptr
;
1736 if (stream_filter(cas
->two
,
1737 cas
->buf
+ cas
->ptr
, &to_feed
,
1738 output
+ filled
, &remaining
))
1740 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1741 filled
= sz
- remaining
;
1745 /* feed one from upstream and have it emit into our buffer */
1746 to_feed
= input
? *isize_p
: 0;
1747 if (input
&& !to_feed
)
1749 remaining
= sizeof(cas
->buf
);
1750 if (stream_filter(cas
->one
,
1752 cas
->buf
, &remaining
))
1754 cas
->end
= sizeof(cas
->buf
) - remaining
;
1757 size_t fed
= *isize_p
- to_feed
;
1762 /* do we know that we drained one completely? */
1763 if (input
|| cas
->end
)
1766 /* tell two to drain; we have nothing more to give it */
1768 remaining
= sz
- filled
;
1769 if (stream_filter(cas
->two
,
1771 output
+ filled
, &remaining
))
1773 if (remaining
== (sz
- filled
))
1774 break; /* completely drained two */
1775 filled
= sz
- remaining
;
1781 static void cascade_free_fn(struct stream_filter
*filter
)
1783 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1784 free_stream_filter(cas
->one
);
1785 free_stream_filter(cas
->two
);
1789 static struct stream_filter_vtbl cascade_vtbl
= {
1790 .filter
= cascade_filter_fn
,
1791 .free
= cascade_free_fn
,
1794 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1795 struct stream_filter
*two
)
1797 struct cascade_filter
*cascade
;
1799 if (!one
|| is_null_stream_filter(one
))
1801 if (!two
|| is_null_stream_filter(two
))
1804 cascade
= xmalloc(sizeof(*cascade
));
1807 cascade
->end
= cascade
->ptr
= 0;
1808 cascade
->filter
.vtbl
= &cascade_vtbl
;
1809 return (struct stream_filter
*)cascade
;
1815 #define IDENT_DRAINING (-1)
1816 #define IDENT_SKIPPING (-2)
1817 struct ident_filter
{
1818 struct stream_filter filter
;
1821 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1824 static int is_foreign_ident(const char *str
)
1828 if (!skip_prefix(str
, "$Id: ", &str
))
1830 for (i
= 0; str
[i
]; i
++) {
1831 if (isspace(str
[i
]) && str
[i
+1] != '$')
1837 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1839 size_t to_drain
= ident
->left
.len
;
1841 if (*osize_p
< to_drain
)
1842 to_drain
= *osize_p
;
1844 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1845 strbuf_remove(&ident
->left
, 0, to_drain
);
1846 *output_p
+= to_drain
;
1847 *osize_p
-= to_drain
;
1849 if (!ident
->left
.len
)
1853 static int ident_filter_fn(struct stream_filter
*filter
,
1854 const char *input
, size_t *isize_p
,
1855 char *output
, size_t *osize_p
)
1857 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1858 static const char head
[] = "$Id";
1861 /* drain upon eof */
1862 switch (ident
->state
) {
1864 strbuf_add(&ident
->left
, head
, ident
->state
);
1866 case IDENT_SKIPPING
:
1868 case IDENT_DRAINING
:
1869 ident_drain(ident
, &output
, osize_p
);
1874 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1877 if (ident
->state
== IDENT_DRAINING
) {
1878 ident_drain(ident
, &output
, osize_p
);
1887 if (ident
->state
== IDENT_SKIPPING
) {
1889 * Skipping until '$' or LF, but keeping them
1890 * in case it is a foreign ident.
1892 strbuf_addch(&ident
->left
, ch
);
1893 if (ch
!= '\n' && ch
!= '$')
1895 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1896 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1897 strbuf_addstr(&ident
->left
, ident
->ident
);
1899 ident
->state
= IDENT_DRAINING
;
1903 if (ident
->state
< sizeof(head
) &&
1904 head
[ident
->state
] == ch
) {
1910 strbuf_add(&ident
->left
, head
, ident
->state
);
1911 if (ident
->state
== sizeof(head
) - 1) {
1912 if (ch
!= ':' && ch
!= '$') {
1913 strbuf_addch(&ident
->left
, ch
);
1919 strbuf_addch(&ident
->left
, ch
);
1920 ident
->state
= IDENT_SKIPPING
;
1922 strbuf_addstr(&ident
->left
, ident
->ident
);
1923 ident
->state
= IDENT_DRAINING
;
1928 strbuf_addch(&ident
->left
, ch
);
1929 ident
->state
= IDENT_DRAINING
;
1934 static void ident_free_fn(struct stream_filter
*filter
)
1936 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1937 strbuf_release(&ident
->left
);
1941 static struct stream_filter_vtbl ident_vtbl
= {
1942 .filter
= ident_filter_fn
,
1943 .free
= ident_free_fn
,
1946 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1948 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1950 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1951 ": %s $", oid_to_hex(oid
));
1952 strbuf_init(&ident
->left
, 0);
1953 ident
->filter
.vtbl
= &ident_vtbl
;
1955 return (struct stream_filter
*)ident
;
1959 * Return an appropriately constructed filter for the given ca, or NULL if
1960 * the contents cannot be filtered without reading the whole thing
1963 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1964 * large binary blob you would want us not to slurp into the memory!
1966 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1967 const struct object_id
*oid
)
1969 struct stream_filter
*filter
= NULL
;
1971 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
1975 filter
= ident_filter(oid
);
1977 if (output_eol(ca
->crlf_action
) == EOL_CRLF
)
1978 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1980 filter
= cascade_filter(filter
, &null_filter_singleton
);
1985 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
1987 const struct object_id
*oid
)
1989 struct conv_attrs ca
;
1990 convert_attrs(istate
, &ca
, path
);
1991 return get_stream_filter_ca(&ca
, oid
);
1994 void free_stream_filter(struct stream_filter
*filter
)
1996 filter
->vtbl
->free(filter
);
1999 int stream_filter(struct stream_filter
*filter
,
2000 const char *input
, size_t *isize_p
,
2001 char *output
, size_t *osize_p
)
2003 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
2006 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
2007 const struct object_id
*treeish
,
2008 const struct object_id
*blob
)
2010 memset(meta
, 0, sizeof(*meta
));
2012 meta
->refname
= refname
;
2014 oidcpy(&meta
->treeish
, treeish
);
2016 oidcpy(&meta
->blob
, blob
);
2019 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2020 const struct checkout_metadata
*src
,
2021 const struct object_id
*blob
)
2023 memcpy(dst
, src
, sizeof(*dst
));
2025 oidcpy(&dst
->blob
, blob
);
2028 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2031 if (ca
->drv
->process
)
2032 return CA_CLASS_INCORE_PROCESS
;
2033 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2034 return CA_CLASS_INCORE_FILTER
;
2037 if (ca
->working_tree_encoding
)
2038 return CA_CLASS_INCORE
;
2040 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2041 return CA_CLASS_INCORE
;
2043 return CA_CLASS_STREAMABLE
;