3 #include "object-store.h"
5 #include "run-command.h"
9 #include "sub-process.h"
14 * convert.c - convert a file when checking it out and checking it in.
16 * This should use the pathname to decide on whether it wants to do some
17 * more interesting conversions (automatic gzip/unzip, general format
18 * conversions etc etc), but by default it just does automatic CRLF<->LF
19 * translation when the "text" attribute or "auto_crlf" option is set.
22 /* Stat bits: When BIN is set, the txt bits are unset */
23 #define CONVERT_STAT_BITS_TXT_LF 0x1
24 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
25 #define CONVERT_STAT_BITS_BIN 0x4
28 /* NUL, CR, LF and CRLF counts */
29 unsigned nul
, lonecr
, lonelf
, crlf
;
31 /* These are just approximations! */
32 unsigned printable
, nonprintable
;
35 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
39 memset(stats
, 0, sizeof(*stats
));
41 for (i
= 0; i
< size
; i
++) {
42 unsigned char c
= buf
[i
];
44 if (i
+1 < size
&& buf
[i
+1] == '\n') {
57 stats
->nonprintable
++;
60 /* BS, HT, ESC and FF */
61 case '\b': case '\t': case '\033': case '\014':
68 stats
->nonprintable
++;
75 /* If file ends with EOF then don't count this EOF as non-printable. */
76 if (size
>= 1 && buf
[size
-1] == '\032')
77 stats
->nonprintable
--;
81 * The same heuristics as diff.c::mmfile_is_binary()
82 * We treat files with bare CR as binary
84 static int convert_is_binary(const struct text_stat
*stats
)
90 if ((stats
->printable
>> 7) < stats
->nonprintable
)
95 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
97 struct text_stat stats
;
101 gather_stats(data
, size
, &stats
);
102 if (convert_is_binary(&stats
))
103 ret
|= CONVERT_STAT_BITS_BIN
;
105 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
107 ret
|= CONVERT_STAT_BITS_TXT_LF
;
112 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
114 unsigned int convert_stats
= gather_convert_stats(data
, size
);
116 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
118 switch (convert_stats
) {
119 case CONVERT_STAT_BITS_TXT_LF
:
121 case CONVERT_STAT_BITS_TXT_CRLF
:
123 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
130 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
135 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
136 ret
= gather_convert_stats_ascii(data
, sz
);
141 const char *get_wt_convert_stats_ascii(const char *path
)
143 const char *ret
= "";
144 struct strbuf sb
= STRBUF_INIT
;
145 if (strbuf_read_file(&sb
, path
, 0) >= 0)
146 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
151 static int text_eol_is_crlf(void)
153 if (auto_crlf
== AUTO_CRLF_TRUE
)
155 else if (auto_crlf
== AUTO_CRLF_INPUT
)
157 if (core_eol
== EOL_CRLF
)
159 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
164 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
166 switch (crlf_action
) {
171 case CRLF_TEXT_INPUT
:
176 case CRLF_AUTO_INPUT
:
181 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
183 warning(_("illegal crlf_action %d"), (int)crlf_action
);
187 static void check_global_conv_flags_eol(const char *path
,
188 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
191 if (old_stats
->crlf
&& !new_stats
->crlf
) {
193 * CRLFs would not be restored by checkout
195 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
196 die(_("CRLF would be replaced by LF in %s"), path
);
197 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
198 warning(_("in the working copy of '%s', CRLF will be"
199 " replaced by LF the next time Git touches"
201 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
203 * CRLFs would be added by checkout
205 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
206 die(_("LF would be replaced by CRLF in %s"), path
);
207 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
208 warning(_("in the working copy of '%s', LF will be"
209 " replaced by CRLF the next time Git touches"
214 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
221 data
= read_blob_data_from_index(istate
, path
, &sz
);
225 crp
= memchr(data
, '\r', sz
);
227 unsigned int ret_stats
;
228 ret_stats
= gather_convert_stats(data
, sz
);
229 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
230 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
237 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
238 enum convert_crlf_action crlf_action
)
240 if (output_eol(crlf_action
) != EOL_CRLF
)
242 /* No "naked" LF? Nothing to convert, regardless. */
246 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
247 /* If we have any CR or CRLF line endings, we do not touch it */
248 /* This is the new safer autocrlf-handling */
249 if (stats
->lonecr
|| stats
->crlf
)
252 if (convert_is_binary(stats
))
259 static int validate_encoding(const char *path
, const char *enc
,
260 const char *data
, size_t len
, int die_on_error
)
262 const char *stripped
;
264 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
265 if (skip_iprefix(enc
, "UTF", &stripped
)) {
266 skip_prefix(stripped
, "-", &stripped
);
269 * Check for detectable errors in UTF encodings
271 if (has_prohibited_utf_bom(enc
, data
, len
)) {
272 const char *error_msg
= _(
273 "BOM is prohibited in '%s' if encoded as %s");
275 * This advice is shown for UTF-??BE and UTF-??LE encodings.
276 * We cut off the last two characters of the encoding name
277 * to generate the encoding name suitable for BOMs.
279 const char *advise_msg
= _(
280 "The file '%s' contains a byte order "
281 "mark (BOM). Please use UTF-%.*s as "
282 "working-tree-encoding.");
283 int stripped_len
= strlen(stripped
) - strlen("BE");
284 advise(advise_msg
, path
, stripped_len
, stripped
);
286 die(error_msg
, path
, enc
);
288 return error(error_msg
, path
, enc
);
291 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
292 const char *error_msg
= _(
293 "BOM is required in '%s' if encoded as %s");
294 const char *advise_msg
= _(
295 "The file '%s' is missing a byte order "
296 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
297 "(depending on the byte order) as "
298 "working-tree-encoding.");
299 advise(advise_msg
, path
, stripped
, stripped
);
301 die(error_msg
, path
, enc
);
303 return error(error_msg
, path
, enc
);
311 static void trace_encoding(const char *context
, const char *path
,
312 const char *encoding
, const char *buf
, size_t len
)
314 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
315 struct strbuf trace
= STRBUF_INIT
;
318 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
319 for (i
= 0; i
< len
&& buf
; ++i
) {
321 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
323 (unsigned char) buf
[i
],
324 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
325 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
328 strbuf_addchars(&trace
, '\n', 1);
330 trace_strbuf(&coe
, &trace
);
331 strbuf_release(&trace
);
334 static int check_roundtrip(const char *enc_name
)
337 * check_roundtrip_encoding contains a string of comma and/or
338 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
339 * Search for the given encoding in that string.
341 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
346 next
= found
+ strlen(enc_name
);
347 len
= strlen(check_roundtrip_encoding
);
350 * check that the found encoding is at the
351 * beginning of check_roundtrip_encoding or
352 * that it is prefixed with a space or comma
354 found
== check_roundtrip_encoding
|| (
355 (isspace(found
[-1]) || found
[-1] == ',')
359 * check that the found encoding is at the
360 * end of check_roundtrip_encoding or
361 * that it is suffixed with a space or comma
363 next
== check_roundtrip_encoding
+ len
|| (
364 next
< check_roundtrip_encoding
+ len
&&
365 (isspace(next
[0]) || next
[0] == ',')
370 static const char *default_encoding
= "UTF-8";
372 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
373 struct strbuf
*buf
, const char *enc
, int conv_flags
)
377 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
380 * No encoding is specified or there is nothing to encode.
381 * Tell the caller that the content was not modified.
383 if (!enc
|| (src
&& !src_len
))
387 * Looks like we got called from "would_convert_to_git()".
388 * This means Git wants to know if it would encode (= modify!)
389 * the content. Let's answer with "yes", since an encoding was
395 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
398 trace_encoding("source", path
, enc
, src
, src_len
);
399 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
403 * We could add the blob "as-is" to Git. However, on checkout
404 * we would try to re-encode to the original encoding. This
405 * would fail and we would leave the user with a messed-up
406 * working tree. Let's try to avoid this by screaming loud.
408 const char* msg
= _("failed to encode '%s' from %s to %s");
410 die(msg
, path
, enc
, default_encoding
);
412 error(msg
, path
, enc
, default_encoding
);
416 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
419 * UTF supports lossless conversion round tripping [1] and conversions
420 * between UTF and other encodings are mostly round trip safe as
421 * Unicode aims to be a superset of all other character encodings.
422 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
423 * trip issues [2]. Check the round trip conversion for all encodings
424 * listed in core.checkRoundtripEncoding.
426 * The round trip check is only performed if content is written to Git.
427 * This ensures that no information is lost during conversion to/from
428 * the internal UTF-8 representation.
430 * Please note, the code below is not tested because I was not able to
431 * generate a faulty round trip without an iconv error. Iconv errors
432 * are already caught above.
434 * [1] http://unicode.org/faq/utf_bom.html#gen2
435 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
437 if (die_on_error
&& check_roundtrip(enc
)) {
441 re_src
= reencode_string_len(dst
, dst_len
,
442 enc
, default_encoding
,
445 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
446 trace_encoding("reencoded source", path
, enc
,
449 if (!re_src
|| src_len
!= re_src_len
||
450 memcmp(src
, re_src
, src_len
)) {
451 const char* msg
= _("encoding '%s' from %s to %s and "
452 "back is not the same");
453 die(msg
, path
, enc
, default_encoding
);
459 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
463 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
464 struct strbuf
*buf
, const char *enc
)
470 * No encoding is specified or there is nothing to encode.
471 * Tell the caller that the content was not modified.
473 if (!enc
|| (src
&& !src_len
))
476 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
479 error(_("failed to encode '%s' from %s to %s"),
480 path
, default_encoding
, enc
);
484 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
488 static int crlf_to_git(struct index_state
*istate
,
489 const char *path
, const char *src
, size_t len
,
491 enum convert_crlf_action crlf_action
, int conv_flags
)
493 struct text_stat stats
;
495 int convert_crlf_into_lf
;
497 if (crlf_action
== CRLF_BINARY
||
502 * If we are doing a dry-run and have no source buffer, there is
503 * nothing to analyze; we must assume we would convert.
508 gather_stats(src
, len
, &stats
);
509 /* Optimization: No CRLF? Nothing to convert, regardless. */
510 convert_crlf_into_lf
= !!stats
.crlf
;
512 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
513 if (convert_is_binary(&stats
))
516 * If the file in the index has any CR in it, do not
517 * convert. This is the new safer autocrlf handling,
518 * unless we want to renormalize in a merge or
521 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
522 has_crlf_in_index(istate
, path
))
523 convert_crlf_into_lf
= 0;
525 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
526 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
527 struct text_stat new_stats
;
528 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
529 /* simulate "git add" */
530 if (convert_crlf_into_lf
) {
531 new_stats
.lonelf
+= new_stats
.crlf
;
534 /* simulate "git checkout" */
535 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
536 new_stats
.crlf
+= new_stats
.lonelf
;
537 new_stats
.lonelf
= 0;
539 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
541 if (!convert_crlf_into_lf
)
545 * At this point all of our source analysis is done, and we are sure we
546 * would convert. If we are in dry-run mode, we can give an answer.
551 /* only grow if not in place */
552 if (strbuf_avail(buf
) + buf
->len
< len
)
553 strbuf_grow(buf
, len
- buf
->len
);
555 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
557 * If we guessed, we already know we rejected a file with
558 * lone CR, and we can strip a CR without looking at what
562 unsigned char c
= *src
++;
568 unsigned char c
= *src
++;
569 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
573 strbuf_setlen(buf
, dst
- buf
->buf
);
577 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
578 enum convert_crlf_action crlf_action
)
580 char *to_free
= NULL
;
581 struct text_stat stats
;
583 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
586 gather_stats(src
, len
, &stats
);
587 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
590 /* are we "faking" in place editing ? */
592 to_free
= strbuf_detach(buf
, NULL
);
594 strbuf_grow(buf
, len
+ stats
.lonelf
);
596 const char *nl
= memchr(src
, '\n', len
);
599 if (nl
> src
&& nl
[-1] == '\r') {
600 strbuf_add(buf
, src
, nl
+ 1 - src
);
602 strbuf_add(buf
, src
, nl
- src
);
603 strbuf_addstr(buf
, "\r\n");
608 strbuf_add(buf
, src
, len
);
614 struct filter_params
{
622 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
625 * Spawn cmd and feed the buffer contents through its stdin.
627 struct child_process child_process
= CHILD_PROCESS_INIT
;
628 struct filter_params
*params
= (struct filter_params
*)data
;
629 int write_err
, status
;
631 /* apply % substitution to cmd */
632 struct strbuf cmd
= STRBUF_INIT
;
633 struct strbuf path
= STRBUF_INIT
;
634 struct strbuf_expand_dict_entry dict
[] = {
639 /* quote the path to preserve spaces, etc. */
640 sq_quote_buf(&path
, params
->path
);
641 dict
[0].value
= path
.buf
;
643 /* expand all %f with the quoted path */
644 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
645 strbuf_release(&path
);
647 strvec_push(&child_process
.args
, cmd
.buf
);
648 child_process
.use_shell
= 1;
649 child_process
.in
= -1;
650 child_process
.out
= out
;
652 if (start_command(&child_process
)) {
653 strbuf_release(&cmd
);
654 return error(_("cannot fork to run external filter '%s'"),
658 sigchain_push(SIGPIPE
, SIG_IGN
);
661 write_err
= (write_in_full(child_process
.in
,
662 params
->src
, params
->size
) < 0);
666 write_err
= copy_fd(params
->fd
, child_process
.in
);
667 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
671 if (close(child_process
.in
))
674 error(_("cannot feed the input to external filter '%s'"),
677 sigchain_pop(SIGPIPE
);
679 status
= finish_command(&child_process
);
681 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
683 strbuf_release(&cmd
);
684 return (write_err
|| status
);
687 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
688 struct strbuf
*dst
, const char *cmd
)
691 * Create a pipeline to have the command filter the buffer's
694 * (child --> cmd) --> us
697 struct strbuf nbuf
= STRBUF_INIT
;
699 struct filter_params params
;
701 memset(&async
, 0, sizeof(async
));
702 async
.proc
= filter_buffer_or_fd
;
703 async
.data
= ¶ms
;
712 if (start_async(&async
))
713 return 0; /* error was already reported */
715 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
716 err
= error(_("read from external filter '%s' failed"), cmd
);
718 if (close(async
.out
)) {
719 err
= error(_("read from external filter '%s' failed"), cmd
);
721 if (finish_async(&async
)) {
722 err
= error(_("external filter '%s' failed"), cmd
);
726 strbuf_swap(dst
, &nbuf
);
728 strbuf_release(&nbuf
);
732 #define CAP_CLEAN (1u<<0)
733 #define CAP_SMUDGE (1u<<1)
734 #define CAP_DELAY (1u<<2)
737 struct subprocess_entry subprocess
; /* must be the first member! */
738 unsigned int supported_capabilities
;
741 static int subprocess_map_initialized
;
742 static struct hashmap subprocess_map
;
744 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
746 static int versions
[] = {2, 0};
747 static struct subprocess_capability capabilities
[] = {
748 { "clean", CAP_CLEAN
},
749 { "smudge", CAP_SMUDGE
},
750 { "delay", CAP_DELAY
},
753 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
754 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
756 &entry
->supported_capabilities
);
759 static void handle_filter_error(const struct strbuf
*filter_status
,
760 struct cmd2process
*entry
,
761 const unsigned int wanted_capability
)
763 if (!strcmp(filter_status
->buf
, "error"))
764 ; /* The filter signaled a problem with the file. */
765 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
767 * The filter signaled a permanent problem. Don't try to filter
768 * files with the same command for the lifetime of the current
771 entry
->supported_capabilities
&= ~wanted_capability
;
774 * Something went wrong with the protocol filter.
775 * Force shutdown and restart if another blob requires filtering.
777 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
778 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
783 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
784 int fd
, struct strbuf
*dst
, const char *cmd
,
785 const unsigned int wanted_capability
,
786 const struct checkout_metadata
*meta
,
787 struct delayed_checkout
*dco
)
791 struct cmd2process
*entry
;
792 struct child_process
*process
;
793 struct strbuf nbuf
= STRBUF_INIT
;
794 struct strbuf filter_status
= STRBUF_INIT
;
795 const char *filter_type
;
797 if (!subprocess_map_initialized
) {
798 subprocess_map_initialized
= 1;
799 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
802 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
808 entry
= xmalloc(sizeof(*entry
));
809 entry
->supported_capabilities
= 0;
811 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
816 process
= &entry
->subprocess
.process
;
818 if (!(entry
->supported_capabilities
& wanted_capability
))
821 if (wanted_capability
& CAP_CLEAN
)
822 filter_type
= "clean";
823 else if (wanted_capability
& CAP_SMUDGE
)
824 filter_type
= "smudge";
826 die(_("unexpected filter type"));
828 sigchain_push(SIGPIPE
, SIG_IGN
);
830 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
831 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
835 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
837 error(_("path name too long for external filter"));
841 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
845 if (meta
&& meta
->refname
) {
846 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
851 if (meta
&& !is_null_oid(&meta
->treeish
)) {
852 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
857 if (meta
&& !is_null_oid(&meta
->blob
)) {
858 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
863 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
864 dco
&& dco
->state
== CE_CAN_DELAY
) {
866 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
871 err
= packet_flush_gently(process
->in
);
876 err
= write_packetized_from_fd_no_flush(fd
, process
->in
);
878 err
= write_packetized_from_buf_no_flush(src
, len
, process
->in
);
882 err
= packet_flush_gently(process
->in
);
886 err
= subprocess_read_status(process
->out
, &filter_status
);
890 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
891 string_list_insert(&dco
->filters
, cmd
);
892 string_list_insert(&dco
->paths
, path
);
894 /* The filter got the blob and wants to send us a response. */
895 err
= strcmp(filter_status
.buf
, "success");
899 err
= read_packetized_to_strbuf(process
->out
, &nbuf
,
900 PACKET_READ_GENTLE_ON_EOF
) < 0;
904 err
= subprocess_read_status(process
->out
, &filter_status
);
908 err
= strcmp(filter_status
.buf
, "success");
912 sigchain_pop(SIGPIPE
);
915 handle_filter_error(&filter_status
, entry
, wanted_capability
);
917 strbuf_swap(dst
, &nbuf
);
918 strbuf_release(&nbuf
);
919 strbuf_release(&filter_status
);
924 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
928 struct cmd2process
*entry
;
929 struct child_process
*process
;
930 struct strbuf filter_status
= STRBUF_INIT
;
932 assert(subprocess_map_initialized
);
933 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
935 error(_("external filter '%s' is not available anymore although "
936 "not all paths have been filtered"), cmd
);
939 process
= &entry
->subprocess
.process
;
940 sigchain_push(SIGPIPE
, SIG_IGN
);
942 err
= packet_write_fmt_gently(
943 process
->in
, "command=list_available_blobs\n");
947 err
= packet_flush_gently(process
->in
);
951 while ((line
= packet_read_line(process
->out
, NULL
))) {
953 if (skip_prefix(line
, "pathname=", &path
))
954 string_list_insert(available_paths
, xstrdup(path
));
956 ; /* ignore unknown keys */
959 err
= subprocess_read_status(process
->out
, &filter_status
);
963 err
= strcmp(filter_status
.buf
, "success");
966 sigchain_pop(SIGPIPE
);
969 handle_filter_error(&filter_status
, entry
, 0);
970 strbuf_release(&filter_status
);
974 static struct convert_driver
{
976 struct convert_driver
*next
;
981 } *user_convert
, **user_convert_tail
;
983 static int apply_filter(const char *path
, const char *src
, size_t len
,
984 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
985 const unsigned int wanted_capability
,
986 const struct checkout_metadata
*meta
,
987 struct delayed_checkout
*dco
)
989 const char *cmd
= NULL
;
997 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
999 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1003 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1004 else if (drv
->process
&& *drv
->process
)
1005 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1006 drv
->process
, wanted_capability
, meta
, dco
);
1011 static int read_convert_config(const char *var
, const char *value
, void *cb UNUSED
)
1013 const char *key
, *name
;
1015 struct convert_driver
*drv
;
1018 * External conversion drivers are configured using
1019 * "filter.<name>.variable".
1021 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1023 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1024 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1027 CALLOC_ARRAY(drv
, 1);
1028 drv
->name
= xmemdupz(name
, namelen
);
1029 *user_convert_tail
= drv
;
1030 user_convert_tail
= &(drv
->next
);
1034 * filter.<name>.smudge and filter.<name>.clean specifies
1039 * The command-line will not be interpolated in any way.
1042 if (!strcmp("smudge", key
))
1043 return git_config_string(&drv
->smudge
, var
, value
);
1045 if (!strcmp("clean", key
))
1046 return git_config_string(&drv
->clean
, var
, value
);
1048 if (!strcmp("process", key
))
1049 return git_config_string(&drv
->process
, var
, value
);
1051 if (!strcmp("required", key
)) {
1052 drv
->required
= git_config_bool(var
, value
);
1059 static int count_ident(const char *cp
, unsigned long size
)
1062 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1074 if (memcmp("Id", cp
, 2))
1085 * "$Id: ... "; scan up to the closing dollar sign and discard.
1101 static int ident_to_git(const char *src
, size_t len
,
1102 struct strbuf
*buf
, int ident
)
1106 if (!ident
|| (src
&& !count_ident(src
, len
)))
1112 /* only grow if not in place */
1113 if (strbuf_avail(buf
) + buf
->len
< len
)
1114 strbuf_grow(buf
, len
- buf
->len
);
1117 dollar
= memchr(src
, '$', len
);
1120 memmove(dst
, src
, dollar
+ 1 - src
);
1121 dst
+= dollar
+ 1 - src
;
1122 len
-= dollar
+ 1 - src
;
1125 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1126 dollar
= memchr(src
+ 3, '$', len
- 3);
1129 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1130 /* Line break before the next dollar. */
1134 memcpy(dst
, "Id$", 3);
1136 len
-= dollar
+ 1 - src
;
1140 memmove(dst
, src
, len
);
1141 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1145 static int ident_to_worktree(const char *src
, size_t len
,
1146 struct strbuf
*buf
, int ident
)
1148 struct object_id oid
;
1149 char *to_free
= NULL
, *dollar
, *spc
;
1155 cnt
= count_ident(src
, len
);
1159 /* are we "faking" in place editing ? */
1160 if (src
== buf
->buf
)
1161 to_free
= strbuf_detach(buf
, NULL
);
1162 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1164 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1166 /* step 1: run to the next '$' */
1167 dollar
= memchr(src
, '$', len
);
1170 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1171 len
-= dollar
+ 1 - src
;
1174 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1175 if (len
< 3 || memcmp("Id", src
, 2))
1178 /* step 3: skip over Id$ or Id:xxxxx$ */
1179 if (src
[2] == '$') {
1182 } else if (src
[2] == ':') {
1184 * It's possible that an expanded Id has crept its way into the
1185 * repository, we cope with that by stripping the expansion out.
1186 * This is probably not a good idea, since it will cause changes
1187 * on checkout, which won't go away by stash, but let's keep it
1188 * for git-style ids.
1190 dollar
= memchr(src
+ 3, '$', len
- 3);
1192 /* incomplete keyword, no more '$', so just quit the loop */
1196 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1197 /* Line break before the next dollar. */
1201 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1202 if (spc
&& spc
< dollar
-1) {
1203 /* There are spaces in unexpected places.
1204 * This is probably an id from some other
1205 * versioning system. Keep it for now.
1210 len
-= dollar
+ 1 - src
;
1213 /* it wasn't a "Id$" or "Id:xxxx$" */
1217 /* step 4: substitute */
1218 strbuf_addstr(buf
, "Id: ");
1219 strbuf_addstr(buf
, oid_to_hex(&oid
));
1220 strbuf_addstr(buf
, " $");
1222 strbuf_add(buf
, src
, len
);
1228 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1230 const char *value
= check
->value
;
1232 if (ATTR_UNSET(value
) || !strlen(value
))
1235 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1236 die(_("true/false are no valid working-tree-encodings"));
1239 /* Don't encode to the default encoding */
1240 if (same_encoding(value
, default_encoding
))
1246 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1248 const char *value
= check
->value
;
1250 if (ATTR_TRUE(value
))
1252 else if (ATTR_FALSE(value
))
1254 else if (ATTR_UNSET(value
))
1256 else if (!strcmp(value
, "input"))
1257 return CRLF_TEXT_INPUT
;
1258 else if (!strcmp(value
, "auto"))
1260 return CRLF_UNDEFINED
;
1263 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1265 const char *value
= check
->value
;
1267 if (ATTR_UNSET(value
))
1269 else if (!strcmp(value
, "lf"))
1271 else if (!strcmp(value
, "crlf"))
1276 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1278 const char *value
= check
->value
;
1279 struct convert_driver
*drv
;
1281 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1283 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1284 if (!strcmp(value
, drv
->name
))
1289 static int git_path_check_ident(struct attr_check_item
*check
)
1291 const char *value
= check
->value
;
1293 return !!ATTR_TRUE(value
);
1296 static struct attr_check
*check
;
1298 void convert_attrs(struct index_state
*istate
,
1299 struct conv_attrs
*ca
, const char *path
)
1301 struct attr_check_item
*ccheck
= NULL
;
1304 check
= attr_check_initl("crlf", "ident", "filter",
1305 "eol", "text", "working-tree-encoding",
1307 user_convert_tail
= &user_convert
;
1308 git_config(read_convert_config
, NULL
);
1311 git_check_attr(istate
, NULL
, path
, check
);
1312 ccheck
= check
->items
;
1313 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1314 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1315 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1316 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1317 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1318 if (ca
->crlf_action
!= CRLF_BINARY
) {
1319 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1320 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1321 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1322 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1323 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1324 else if (eol_attr
== EOL_LF
)
1325 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1326 else if (eol_attr
== EOL_CRLF
)
1327 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1329 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1331 /* Save attr and make a decision for action */
1332 ca
->attr_action
= ca
->crlf_action
;
1333 if (ca
->crlf_action
== CRLF_TEXT
)
1334 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1335 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1336 ca
->crlf_action
= CRLF_BINARY
;
1337 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1338 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1339 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1340 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1343 void reset_parsed_attributes(void)
1345 struct convert_driver
*drv
, *next
;
1347 attr_check_free(check
);
1349 reset_merge_attributes();
1351 for (drv
= user_convert
; drv
; drv
= next
) {
1353 free((void *)drv
->name
);
1356 user_convert
= NULL
;
1357 user_convert_tail
= NULL
;
1360 int would_convert_to_git_filter_fd(struct index_state
*istate
, const char *path
)
1362 struct conv_attrs ca
;
1364 convert_attrs(istate
, &ca
, path
);
1369 * Apply a filter to an fd only if the filter is required to succeed.
1370 * We must die if the filter fails, because the original data before
1371 * filtering is not available.
1373 if (!ca
.drv
->required
)
1376 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1379 const char *get_convert_attr_ascii(struct index_state
*istate
, const char *path
)
1381 struct conv_attrs ca
;
1383 convert_attrs(istate
, &ca
, path
);
1384 switch (ca
.attr_action
) {
1385 case CRLF_UNDEFINED
:
1391 case CRLF_TEXT_INPUT
:
1392 return "text eol=lf";
1393 case CRLF_TEXT_CRLF
:
1394 return "text eol=crlf";
1397 case CRLF_AUTO_CRLF
:
1398 return "text=auto eol=crlf";
1399 case CRLF_AUTO_INPUT
:
1400 return "text=auto eol=lf";
1405 int convert_to_git(struct index_state
*istate
,
1406 const char *path
, const char *src
, size_t len
,
1407 struct strbuf
*dst
, int conv_flags
)
1410 struct conv_attrs ca
;
1412 convert_attrs(istate
, &ca
, path
);
1414 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1415 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1416 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1423 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1429 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1430 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1436 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1439 void convert_to_git_filter_fd(struct index_state
*istate
,
1440 const char *path
, int fd
, struct strbuf
*dst
,
1443 struct conv_attrs ca
;
1444 convert_attrs(istate
, &ca
, path
);
1448 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1449 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1451 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1452 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1453 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1456 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1457 const char *path
, const char *src
,
1458 size_t len
, struct strbuf
*dst
,
1460 const struct checkout_metadata
*meta
,
1461 struct delayed_checkout
*dco
)
1463 int ret
= 0, ret_filter
= 0;
1465 ret
|= ident_to_worktree(src
, len
, dst
, ca
->ident
);
1471 * CRLF conversion can be skipped if normalizing, unless there
1472 * is a smudge or process filter (even if the process filter doesn't
1473 * support smudge). The filters might expect CRLFs.
1475 if ((ca
->drv
&& (ca
->drv
->smudge
|| ca
->drv
->process
)) || !normalizing
) {
1476 ret
|= crlf_to_worktree(src
, len
, dst
, ca
->crlf_action
);
1483 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
->working_tree_encoding
);
1489 ret_filter
= apply_filter(
1490 path
, src
, len
, -1, dst
, ca
->drv
, CAP_SMUDGE
, meta
, dco
);
1491 if (!ret_filter
&& ca
->drv
&& ca
->drv
->required
)
1492 die(_("%s: smudge filter %s failed"), path
, ca
->drv
->name
);
1494 return ret
| ret_filter
;
1497 int async_convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1498 const char *path
, const char *src
,
1499 size_t len
, struct strbuf
*dst
,
1500 const struct checkout_metadata
*meta
,
1503 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1507 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1508 const char *path
, const char *src
,
1509 size_t len
, struct strbuf
*dst
,
1510 const struct checkout_metadata
*meta
)
1512 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1516 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1517 const char *src
, size_t len
, struct strbuf
*dst
)
1519 struct conv_attrs ca
;
1522 convert_attrs(istate
, &ca
, path
);
1523 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1529 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1532 /*****************************************************************
1534 * Streaming conversion support
1536 *****************************************************************/
1538 typedef int (*filter_fn
)(struct stream_filter
*,
1539 const char *input
, size_t *isize_p
,
1540 char *output
, size_t *osize_p
);
1541 typedef void (*free_fn
)(struct stream_filter
*);
1543 struct stream_filter_vtbl
{
1548 struct stream_filter
{
1549 struct stream_filter_vtbl
*vtbl
;
1552 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1553 const char *input
, size_t *isize_p
,
1554 char *output
, size_t *osize_p
)
1559 return 0; /* we do not keep any states */
1561 if (*osize_p
< count
)
1564 memmove(output
, input
, count
);
1571 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1573 ; /* nothing -- null instances are shared */
1576 static struct stream_filter_vtbl null_vtbl
= {
1577 .filter
= null_filter_fn
,
1578 .free
= null_free_fn
,
1581 static struct stream_filter null_filter_singleton
= {
1585 int is_null_stream_filter(struct stream_filter
*filter
)
1587 return filter
== &null_filter_singleton
;
1595 struct lf_to_crlf_filter
{
1596 struct stream_filter filter
;
1597 unsigned has_held
:1;
1601 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1602 const char *input
, size_t *isize_p
,
1603 char *output
, size_t *osize_p
)
1605 size_t count
, o
= 0;
1606 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1609 * We may be holding onto the CR to see if it is followed by a
1610 * LF, in which case we would need to go to the main loop.
1611 * Otherwise, just emit it to the output stream.
1613 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1614 output
[o
++] = lf_to_crlf
->held
;
1615 lf_to_crlf
->has_held
= 0;
1618 /* We are told to drain */
1625 if (count
|| lf_to_crlf
->has_held
) {
1629 if (lf_to_crlf
->has_held
) {
1631 lf_to_crlf
->has_held
= 0;
1634 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1639 } else if (was_cr
) {
1641 * Previous round saw CR and it is not followed
1642 * by a LF; emit the CR before processing the
1643 * current character.
1649 * We may have consumed the last output slot,
1650 * in which case we need to break out of this
1651 * loop; hold the current character before
1654 if (*osize_p
<= o
) {
1655 lf_to_crlf
->has_held
= 1;
1656 lf_to_crlf
->held
= ch
;
1657 continue; /* break but increment i */
1672 if (!lf_to_crlf
->has_held
&& was_cr
) {
1673 lf_to_crlf
->has_held
= 1;
1674 lf_to_crlf
->held
= '\r';
1680 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1685 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1686 .filter
= lf_to_crlf_filter_fn
,
1687 .free
= lf_to_crlf_free_fn
,
1690 static struct stream_filter
*lf_to_crlf_filter(void)
1692 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1694 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1695 return (struct stream_filter
*)lf_to_crlf
;
1701 #define FILTER_BUFFER 1024
1702 struct cascade_filter
{
1703 struct stream_filter filter
;
1704 struct stream_filter
*one
;
1705 struct stream_filter
*two
;
1706 char buf
[FILTER_BUFFER
];
1710 static int cascade_filter_fn(struct stream_filter
*filter
,
1711 const char *input
, size_t *isize_p
,
1712 char *output
, size_t *osize_p
)
1714 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1716 size_t sz
= *osize_p
;
1717 size_t to_feed
, remaining
;
1720 * input -- (one) --> buf -- (two) --> output
1722 while (filled
< sz
) {
1723 remaining
= sz
- filled
;
1725 /* do we already have something to feed two with? */
1726 if (cas
->ptr
< cas
->end
) {
1727 to_feed
= cas
->end
- cas
->ptr
;
1728 if (stream_filter(cas
->two
,
1729 cas
->buf
+ cas
->ptr
, &to_feed
,
1730 output
+ filled
, &remaining
))
1732 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1733 filled
= sz
- remaining
;
1737 /* feed one from upstream and have it emit into our buffer */
1738 to_feed
= input
? *isize_p
: 0;
1739 if (input
&& !to_feed
)
1741 remaining
= sizeof(cas
->buf
);
1742 if (stream_filter(cas
->one
,
1744 cas
->buf
, &remaining
))
1746 cas
->end
= sizeof(cas
->buf
) - remaining
;
1749 size_t fed
= *isize_p
- to_feed
;
1754 /* do we know that we drained one completely? */
1755 if (input
|| cas
->end
)
1758 /* tell two to drain; we have nothing more to give it */
1760 remaining
= sz
- filled
;
1761 if (stream_filter(cas
->two
,
1763 output
+ filled
, &remaining
))
1765 if (remaining
== (sz
- filled
))
1766 break; /* completely drained two */
1767 filled
= sz
- remaining
;
1773 static void cascade_free_fn(struct stream_filter
*filter
)
1775 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1776 free_stream_filter(cas
->one
);
1777 free_stream_filter(cas
->two
);
1781 static struct stream_filter_vtbl cascade_vtbl
= {
1782 .filter
= cascade_filter_fn
,
1783 .free
= cascade_free_fn
,
1786 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1787 struct stream_filter
*two
)
1789 struct cascade_filter
*cascade
;
1791 if (!one
|| is_null_stream_filter(one
))
1793 if (!two
|| is_null_stream_filter(two
))
1796 cascade
= xmalloc(sizeof(*cascade
));
1799 cascade
->end
= cascade
->ptr
= 0;
1800 cascade
->filter
.vtbl
= &cascade_vtbl
;
1801 return (struct stream_filter
*)cascade
;
1807 #define IDENT_DRAINING (-1)
1808 #define IDENT_SKIPPING (-2)
1809 struct ident_filter
{
1810 struct stream_filter filter
;
1813 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1816 static int is_foreign_ident(const char *str
)
1820 if (!skip_prefix(str
, "$Id: ", &str
))
1822 for (i
= 0; str
[i
]; i
++) {
1823 if (isspace(str
[i
]) && str
[i
+1] != '$')
1829 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1831 size_t to_drain
= ident
->left
.len
;
1833 if (*osize_p
< to_drain
)
1834 to_drain
= *osize_p
;
1836 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1837 strbuf_remove(&ident
->left
, 0, to_drain
);
1838 *output_p
+= to_drain
;
1839 *osize_p
-= to_drain
;
1841 if (!ident
->left
.len
)
1845 static int ident_filter_fn(struct stream_filter
*filter
,
1846 const char *input
, size_t *isize_p
,
1847 char *output
, size_t *osize_p
)
1849 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1850 static const char head
[] = "$Id";
1853 /* drain upon eof */
1854 switch (ident
->state
) {
1856 strbuf_add(&ident
->left
, head
, ident
->state
);
1858 case IDENT_SKIPPING
:
1860 case IDENT_DRAINING
:
1861 ident_drain(ident
, &output
, osize_p
);
1866 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1869 if (ident
->state
== IDENT_DRAINING
) {
1870 ident_drain(ident
, &output
, osize_p
);
1879 if (ident
->state
== IDENT_SKIPPING
) {
1881 * Skipping until '$' or LF, but keeping them
1882 * in case it is a foreign ident.
1884 strbuf_addch(&ident
->left
, ch
);
1885 if (ch
!= '\n' && ch
!= '$')
1887 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1888 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1889 strbuf_addstr(&ident
->left
, ident
->ident
);
1891 ident
->state
= IDENT_DRAINING
;
1895 if (ident
->state
< sizeof(head
) &&
1896 head
[ident
->state
] == ch
) {
1902 strbuf_add(&ident
->left
, head
, ident
->state
);
1903 if (ident
->state
== sizeof(head
) - 1) {
1904 if (ch
!= ':' && ch
!= '$') {
1905 strbuf_addch(&ident
->left
, ch
);
1911 strbuf_addch(&ident
->left
, ch
);
1912 ident
->state
= IDENT_SKIPPING
;
1914 strbuf_addstr(&ident
->left
, ident
->ident
);
1915 ident
->state
= IDENT_DRAINING
;
1920 strbuf_addch(&ident
->left
, ch
);
1921 ident
->state
= IDENT_DRAINING
;
1926 static void ident_free_fn(struct stream_filter
*filter
)
1928 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1929 strbuf_release(&ident
->left
);
1933 static struct stream_filter_vtbl ident_vtbl
= {
1934 .filter
= ident_filter_fn
,
1935 .free
= ident_free_fn
,
1938 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1940 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1942 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1943 ": %s $", oid_to_hex(oid
));
1944 strbuf_init(&ident
->left
, 0);
1945 ident
->filter
.vtbl
= &ident_vtbl
;
1947 return (struct stream_filter
*)ident
;
1951 * Return an appropriately constructed filter for the given ca, or NULL if
1952 * the contents cannot be filtered without reading the whole thing
1955 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1956 * large binary blob you would want us not to slurp into the memory!
1958 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1959 const struct object_id
*oid
)
1961 struct stream_filter
*filter
= NULL
;
1963 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
1967 filter
= ident_filter(oid
);
1969 if (output_eol(ca
->crlf_action
) == EOL_CRLF
)
1970 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1972 filter
= cascade_filter(filter
, &null_filter_singleton
);
1977 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
1979 const struct object_id
*oid
)
1981 struct conv_attrs ca
;
1982 convert_attrs(istate
, &ca
, path
);
1983 return get_stream_filter_ca(&ca
, oid
);
1986 void free_stream_filter(struct stream_filter
*filter
)
1988 filter
->vtbl
->free(filter
);
1991 int stream_filter(struct stream_filter
*filter
,
1992 const char *input
, size_t *isize_p
,
1993 char *output
, size_t *osize_p
)
1995 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
1998 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
1999 const struct object_id
*treeish
,
2000 const struct object_id
*blob
)
2002 memset(meta
, 0, sizeof(*meta
));
2004 meta
->refname
= refname
;
2006 oidcpy(&meta
->treeish
, treeish
);
2008 oidcpy(&meta
->blob
, blob
);
2011 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2012 const struct checkout_metadata
*src
,
2013 const struct object_id
*blob
)
2015 memcpy(dst
, src
, sizeof(*dst
));
2017 oidcpy(&dst
->blob
, blob
);
2020 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2023 if (ca
->drv
->process
)
2024 return CA_CLASS_INCORE_PROCESS
;
2025 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2026 return CA_CLASS_INCORE_FILTER
;
2029 if (ca
->working_tree_encoding
)
2030 return CA_CLASS_INCORE
;
2032 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2033 return CA_CLASS_INCORE
;
2035 return CA_CLASS_STREAMABLE
;