5 #include "object-store.h"
7 #include "run-command.h"
11 #include "sub-process.h"
16 * convert.c - convert a file when checking it out and checking it in.
18 * This should use the pathname to decide on whether it wants to do some
19 * more interesting conversions (automatic gzip/unzip, general format
20 * conversions etc etc), but by default it just does automatic CRLF<->LF
21 * translation when the "text" attribute or "auto_crlf" option is set.
24 /* Stat bits: When BIN is set, the txt bits are unset */
25 #define CONVERT_STAT_BITS_TXT_LF 0x1
26 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
27 #define CONVERT_STAT_BITS_BIN 0x4
30 /* NUL, CR, LF and CRLF counts */
31 unsigned nul
, lonecr
, lonelf
, crlf
;
33 /* These are just approximations! */
34 unsigned printable
, nonprintable
;
37 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
41 memset(stats
, 0, sizeof(*stats
));
43 for (i
= 0; i
< size
; i
++) {
44 unsigned char c
= buf
[i
];
46 if (i
+1 < size
&& buf
[i
+1] == '\n') {
59 stats
->nonprintable
++;
62 /* BS, HT, ESC and FF */
63 case '\b': case '\t': case '\033': case '\014':
70 stats
->nonprintable
++;
77 /* If file ends with EOF then don't count this EOF as non-printable. */
78 if (size
>= 1 && buf
[size
-1] == '\032')
79 stats
->nonprintable
--;
83 * The same heuristics as diff.c::mmfile_is_binary()
84 * We treat files with bare CR as binary
86 static int convert_is_binary(const struct text_stat
*stats
)
92 if ((stats
->printable
>> 7) < stats
->nonprintable
)
97 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
99 struct text_stat stats
;
103 gather_stats(data
, size
, &stats
);
104 if (convert_is_binary(&stats
))
105 ret
|= CONVERT_STAT_BITS_BIN
;
107 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
109 ret
|= CONVERT_STAT_BITS_TXT_LF
;
114 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
116 unsigned int convert_stats
= gather_convert_stats(data
, size
);
118 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
120 switch (convert_stats
) {
121 case CONVERT_STAT_BITS_TXT_LF
:
123 case CONVERT_STAT_BITS_TXT_CRLF
:
125 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
132 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
137 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
138 ret
= gather_convert_stats_ascii(data
, sz
);
143 const char *get_wt_convert_stats_ascii(const char *path
)
145 const char *ret
= "";
146 struct strbuf sb
= STRBUF_INIT
;
147 if (strbuf_read_file(&sb
, path
, 0) >= 0)
148 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
153 static int text_eol_is_crlf(void)
155 if (auto_crlf
== AUTO_CRLF_TRUE
)
157 else if (auto_crlf
== AUTO_CRLF_INPUT
)
159 if (core_eol
== EOL_CRLF
)
161 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
166 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
168 switch (crlf_action
) {
173 case CRLF_TEXT_INPUT
:
178 case CRLF_AUTO_INPUT
:
183 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
185 warning(_("illegal crlf_action %d"), (int)crlf_action
);
189 static void check_global_conv_flags_eol(const char *path
,
190 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
193 if (old_stats
->crlf
&& !new_stats
->crlf
) {
195 * CRLFs would not be restored by checkout
197 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
198 die(_("CRLF would be replaced by LF in %s"), path
);
199 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
200 warning(_("in the working copy of '%s', CRLF will be"
201 " replaced by LF the next time Git touches"
203 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
205 * CRLFs would be added by checkout
207 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
208 die(_("LF would be replaced by CRLF in %s"), path
);
209 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
210 warning(_("in the working copy of '%s', LF will be"
211 " replaced by CRLF the next time Git touches"
216 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
223 data
= read_blob_data_from_index(istate
, path
, &sz
);
227 crp
= memchr(data
, '\r', sz
);
229 unsigned int ret_stats
;
230 ret_stats
= gather_convert_stats(data
, sz
);
231 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
232 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
239 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
240 enum convert_crlf_action crlf_action
)
242 if (output_eol(crlf_action
) != EOL_CRLF
)
244 /* No "naked" LF? Nothing to convert, regardless. */
248 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
249 /* If we have any CR or CRLF line endings, we do not touch it */
250 /* This is the new safer autocrlf-handling */
251 if (stats
->lonecr
|| stats
->crlf
)
254 if (convert_is_binary(stats
))
261 static int validate_encoding(const char *path
, const char *enc
,
262 const char *data
, size_t len
, int die_on_error
)
264 const char *stripped
;
266 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
267 if (skip_iprefix(enc
, "UTF", &stripped
)) {
268 skip_prefix(stripped
, "-", &stripped
);
271 * Check for detectable errors in UTF encodings
273 if (has_prohibited_utf_bom(enc
, data
, len
)) {
274 const char *error_msg
= _(
275 "BOM is prohibited in '%s' if encoded as %s");
277 * This advice is shown for UTF-??BE and UTF-??LE encodings.
278 * We cut off the last two characters of the encoding name
279 * to generate the encoding name suitable for BOMs.
281 const char *advise_msg
= _(
282 "The file '%s' contains a byte order "
283 "mark (BOM). Please use UTF-%.*s as "
284 "working-tree-encoding.");
285 int stripped_len
= strlen(stripped
) - strlen("BE");
286 advise(advise_msg
, path
, stripped_len
, stripped
);
288 die(error_msg
, path
, enc
);
290 return error(error_msg
, path
, enc
);
293 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
294 const char *error_msg
= _(
295 "BOM is required in '%s' if encoded as %s");
296 const char *advise_msg
= _(
297 "The file '%s' is missing a byte order "
298 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
299 "(depending on the byte order) as "
300 "working-tree-encoding.");
301 advise(advise_msg
, path
, stripped
, stripped
);
303 die(error_msg
, path
, enc
);
305 return error(error_msg
, path
, enc
);
313 static void trace_encoding(const char *context
, const char *path
,
314 const char *encoding
, const char *buf
, size_t len
)
316 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
317 struct strbuf trace
= STRBUF_INIT
;
320 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
321 for (i
= 0; i
< len
&& buf
; ++i
) {
323 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
325 (unsigned char) buf
[i
],
326 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
327 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
330 strbuf_addchars(&trace
, '\n', 1);
332 trace_strbuf(&coe
, &trace
);
333 strbuf_release(&trace
);
336 static int check_roundtrip(const char *enc_name
)
339 * check_roundtrip_encoding contains a string of comma and/or
340 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
341 * Search for the given encoding in that string.
343 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
348 next
= found
+ strlen(enc_name
);
349 len
= strlen(check_roundtrip_encoding
);
352 * check that the found encoding is at the
353 * beginning of check_roundtrip_encoding or
354 * that it is prefixed with a space or comma
356 found
== check_roundtrip_encoding
|| (
357 (isspace(found
[-1]) || found
[-1] == ',')
361 * check that the found encoding is at the
362 * end of check_roundtrip_encoding or
363 * that it is suffixed with a space or comma
365 next
== check_roundtrip_encoding
+ len
|| (
366 next
< check_roundtrip_encoding
+ len
&&
367 (isspace(next
[0]) || next
[0] == ',')
372 static const char *default_encoding
= "UTF-8";
374 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
375 struct strbuf
*buf
, const char *enc
, int conv_flags
)
379 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
382 * No encoding is specified or there is nothing to encode.
383 * Tell the caller that the content was not modified.
385 if (!enc
|| (src
&& !src_len
))
389 * Looks like we got called from "would_convert_to_git()".
390 * This means Git wants to know if it would encode (= modify!)
391 * the content. Let's answer with "yes", since an encoding was
397 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
400 trace_encoding("source", path
, enc
, src
, src_len
);
401 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
405 * We could add the blob "as-is" to Git. However, on checkout
406 * we would try to re-encode to the original encoding. This
407 * would fail and we would leave the user with a messed-up
408 * working tree. Let's try to avoid this by screaming loud.
410 const char* msg
= _("failed to encode '%s' from %s to %s");
412 die(msg
, path
, enc
, default_encoding
);
414 error(msg
, path
, enc
, default_encoding
);
418 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
421 * UTF supports lossless conversion round tripping [1] and conversions
422 * between UTF and other encodings are mostly round trip safe as
423 * Unicode aims to be a superset of all other character encodings.
424 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
425 * trip issues [2]. Check the round trip conversion for all encodings
426 * listed in core.checkRoundtripEncoding.
428 * The round trip check is only performed if content is written to Git.
429 * This ensures that no information is lost during conversion to/from
430 * the internal UTF-8 representation.
432 * Please note, the code below is not tested because I was not able to
433 * generate a faulty round trip without an iconv error. Iconv errors
434 * are already caught above.
436 * [1] http://unicode.org/faq/utf_bom.html#gen2
437 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
439 if (die_on_error
&& check_roundtrip(enc
)) {
443 re_src
= reencode_string_len(dst
, dst_len
,
444 enc
, default_encoding
,
447 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
448 trace_encoding("reencoded source", path
, enc
,
451 if (!re_src
|| src_len
!= re_src_len
||
452 memcmp(src
, re_src
, src_len
)) {
453 const char* msg
= _("encoding '%s' from %s to %s and "
454 "back is not the same");
455 die(msg
, path
, enc
, default_encoding
);
461 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
465 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
466 struct strbuf
*buf
, const char *enc
)
472 * No encoding is specified or there is nothing to encode.
473 * Tell the caller that the content was not modified.
475 if (!enc
|| (src
&& !src_len
))
478 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
481 error(_("failed to encode '%s' from %s to %s"),
482 path
, default_encoding
, enc
);
486 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
490 static int crlf_to_git(struct index_state
*istate
,
491 const char *path
, const char *src
, size_t len
,
493 enum convert_crlf_action crlf_action
, int conv_flags
)
495 struct text_stat stats
;
497 int convert_crlf_into_lf
;
499 if (crlf_action
== CRLF_BINARY
||
504 * If we are doing a dry-run and have no source buffer, there is
505 * nothing to analyze; we must assume we would convert.
510 gather_stats(src
, len
, &stats
);
511 /* Optimization: No CRLF? Nothing to convert, regardless. */
512 convert_crlf_into_lf
= !!stats
.crlf
;
514 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
515 if (convert_is_binary(&stats
))
518 * If the file in the index has any CR in it, do not
519 * convert. This is the new safer autocrlf handling,
520 * unless we want to renormalize in a merge or
523 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
524 has_crlf_in_index(istate
, path
))
525 convert_crlf_into_lf
= 0;
527 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
528 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
529 struct text_stat new_stats
;
530 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
531 /* simulate "git add" */
532 if (convert_crlf_into_lf
) {
533 new_stats
.lonelf
+= new_stats
.crlf
;
536 /* simulate "git checkout" */
537 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
538 new_stats
.crlf
+= new_stats
.lonelf
;
539 new_stats
.lonelf
= 0;
541 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
543 if (!convert_crlf_into_lf
)
547 * At this point all of our source analysis is done, and we are sure we
548 * would convert. If we are in dry-run mode, we can give an answer.
553 /* only grow if not in place */
554 if (strbuf_avail(buf
) + buf
->len
< len
)
555 strbuf_grow(buf
, len
- buf
->len
);
557 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
559 * If we guessed, we already know we rejected a file with
560 * lone CR, and we can strip a CR without looking at what
564 unsigned char c
= *src
++;
570 unsigned char c
= *src
++;
571 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
575 strbuf_setlen(buf
, dst
- buf
->buf
);
579 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
580 enum convert_crlf_action crlf_action
)
582 char *to_free
= NULL
;
583 struct text_stat stats
;
585 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
588 gather_stats(src
, len
, &stats
);
589 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
592 /* are we "faking" in place editing ? */
594 to_free
= strbuf_detach(buf
, NULL
);
596 strbuf_grow(buf
, len
+ stats
.lonelf
);
598 const char *nl
= memchr(src
, '\n', len
);
601 if (nl
> src
&& nl
[-1] == '\r') {
602 strbuf_add(buf
, src
, nl
+ 1 - src
);
604 strbuf_add(buf
, src
, nl
- src
);
605 strbuf_addstr(buf
, "\r\n");
610 strbuf_add(buf
, src
, len
);
616 struct filter_params
{
624 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
627 * Spawn cmd and feed the buffer contents through its stdin.
629 struct child_process child_process
= CHILD_PROCESS_INIT
;
630 struct filter_params
*params
= (struct filter_params
*)data
;
631 int write_err
, status
;
633 /* apply % substitution to cmd */
634 struct strbuf cmd
= STRBUF_INIT
;
635 struct strbuf path
= STRBUF_INIT
;
636 struct strbuf_expand_dict_entry dict
[] = {
641 /* quote the path to preserve spaces, etc. */
642 sq_quote_buf(&path
, params
->path
);
643 dict
[0].value
= path
.buf
;
645 /* expand all %f with the quoted path */
646 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
647 strbuf_release(&path
);
649 strvec_push(&child_process
.args
, cmd
.buf
);
650 child_process
.use_shell
= 1;
651 child_process
.in
= -1;
652 child_process
.out
= out
;
654 if (start_command(&child_process
)) {
655 strbuf_release(&cmd
);
656 return error(_("cannot fork to run external filter '%s'"),
660 sigchain_push(SIGPIPE
, SIG_IGN
);
663 write_err
= (write_in_full(child_process
.in
,
664 params
->src
, params
->size
) < 0);
668 write_err
= copy_fd(params
->fd
, child_process
.in
);
669 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
673 if (close(child_process
.in
))
676 error(_("cannot feed the input to external filter '%s'"),
679 sigchain_pop(SIGPIPE
);
681 status
= finish_command(&child_process
);
683 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
685 strbuf_release(&cmd
);
686 return (write_err
|| status
);
689 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
690 struct strbuf
*dst
, const char *cmd
)
693 * Create a pipeline to have the command filter the buffer's
696 * (child --> cmd) --> us
699 struct strbuf nbuf
= STRBUF_INIT
;
701 struct filter_params params
;
703 memset(&async
, 0, sizeof(async
));
704 async
.proc
= filter_buffer_or_fd
;
705 async
.data
= ¶ms
;
714 if (start_async(&async
))
715 return 0; /* error was already reported */
717 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
718 err
= error(_("read from external filter '%s' failed"), cmd
);
720 if (close(async
.out
)) {
721 err
= error(_("read from external filter '%s' failed"), cmd
);
723 if (finish_async(&async
)) {
724 err
= error(_("external filter '%s' failed"), cmd
);
728 strbuf_swap(dst
, &nbuf
);
730 strbuf_release(&nbuf
);
734 #define CAP_CLEAN (1u<<0)
735 #define CAP_SMUDGE (1u<<1)
736 #define CAP_DELAY (1u<<2)
739 struct subprocess_entry subprocess
; /* must be the first member! */
740 unsigned int supported_capabilities
;
743 static int subprocess_map_initialized
;
744 static struct hashmap subprocess_map
;
746 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
748 static int versions
[] = {2, 0};
749 static struct subprocess_capability capabilities
[] = {
750 { "clean", CAP_CLEAN
},
751 { "smudge", CAP_SMUDGE
},
752 { "delay", CAP_DELAY
},
755 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
756 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
758 &entry
->supported_capabilities
);
761 static void handle_filter_error(const struct strbuf
*filter_status
,
762 struct cmd2process
*entry
,
763 const unsigned int wanted_capability
)
765 if (!strcmp(filter_status
->buf
, "error"))
766 ; /* The filter signaled a problem with the file. */
767 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
769 * The filter signaled a permanent problem. Don't try to filter
770 * files with the same command for the lifetime of the current
773 entry
->supported_capabilities
&= ~wanted_capability
;
776 * Something went wrong with the protocol filter.
777 * Force shutdown and restart if another blob requires filtering.
779 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
780 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
785 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
786 int fd
, struct strbuf
*dst
, const char *cmd
,
787 const unsigned int wanted_capability
,
788 const struct checkout_metadata
*meta
,
789 struct delayed_checkout
*dco
)
793 struct cmd2process
*entry
;
794 struct child_process
*process
;
795 struct strbuf nbuf
= STRBUF_INIT
;
796 struct strbuf filter_status
= STRBUF_INIT
;
797 const char *filter_type
;
799 if (!subprocess_map_initialized
) {
800 subprocess_map_initialized
= 1;
801 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
804 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
810 entry
= xmalloc(sizeof(*entry
));
811 entry
->supported_capabilities
= 0;
813 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
818 process
= &entry
->subprocess
.process
;
820 if (!(entry
->supported_capabilities
& wanted_capability
))
823 if (wanted_capability
& CAP_CLEAN
)
824 filter_type
= "clean";
825 else if (wanted_capability
& CAP_SMUDGE
)
826 filter_type
= "smudge";
828 die(_("unexpected filter type"));
830 sigchain_push(SIGPIPE
, SIG_IGN
);
832 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
833 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
837 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
839 error(_("path name too long for external filter"));
843 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
847 if (meta
&& meta
->refname
) {
848 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
853 if (meta
&& !is_null_oid(&meta
->treeish
)) {
854 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
859 if (meta
&& !is_null_oid(&meta
->blob
)) {
860 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
865 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
866 dco
&& dco
->state
== CE_CAN_DELAY
) {
868 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
873 err
= packet_flush_gently(process
->in
);
878 err
= write_packetized_from_fd_no_flush(fd
, process
->in
);
880 err
= write_packetized_from_buf_no_flush(src
, len
, process
->in
);
884 err
= packet_flush_gently(process
->in
);
888 err
= subprocess_read_status(process
->out
, &filter_status
);
892 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
893 string_list_insert(&dco
->filters
, cmd
);
894 string_list_insert(&dco
->paths
, path
);
896 /* The filter got the blob and wants to send us a response. */
897 err
= strcmp(filter_status
.buf
, "success");
901 err
= read_packetized_to_strbuf(process
->out
, &nbuf
,
902 PACKET_READ_GENTLE_ON_EOF
) < 0;
906 err
= subprocess_read_status(process
->out
, &filter_status
);
910 err
= strcmp(filter_status
.buf
, "success");
914 sigchain_pop(SIGPIPE
);
917 handle_filter_error(&filter_status
, entry
, wanted_capability
);
919 strbuf_swap(dst
, &nbuf
);
920 strbuf_release(&nbuf
);
921 strbuf_release(&filter_status
);
926 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
930 struct cmd2process
*entry
;
931 struct child_process
*process
;
932 struct strbuf filter_status
= STRBUF_INIT
;
934 assert(subprocess_map_initialized
);
935 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
937 error(_("external filter '%s' is not available anymore although "
938 "not all paths have been filtered"), cmd
);
941 process
= &entry
->subprocess
.process
;
942 sigchain_push(SIGPIPE
, SIG_IGN
);
944 err
= packet_write_fmt_gently(
945 process
->in
, "command=list_available_blobs\n");
949 err
= packet_flush_gently(process
->in
);
953 while ((line
= packet_read_line(process
->out
, NULL
))) {
955 if (skip_prefix(line
, "pathname=", &path
))
956 string_list_insert(available_paths
, xstrdup(path
));
958 ; /* ignore unknown keys */
961 err
= subprocess_read_status(process
->out
, &filter_status
);
965 err
= strcmp(filter_status
.buf
, "success");
968 sigchain_pop(SIGPIPE
);
971 handle_filter_error(&filter_status
, entry
, 0);
972 strbuf_release(&filter_status
);
976 static struct convert_driver
{
978 struct convert_driver
*next
;
983 } *user_convert
, **user_convert_tail
;
985 static int apply_filter(const char *path
, const char *src
, size_t len
,
986 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
987 const unsigned int wanted_capability
,
988 const struct checkout_metadata
*meta
,
989 struct delayed_checkout
*dco
)
991 const char *cmd
= NULL
;
999 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
1001 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1005 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1006 else if (drv
->process
&& *drv
->process
)
1007 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1008 drv
->process
, wanted_capability
, meta
, dco
);
1013 static int read_convert_config(const char *var
, const char *value
, void *cb UNUSED
)
1015 const char *key
, *name
;
1017 struct convert_driver
*drv
;
1020 * External conversion drivers are configured using
1021 * "filter.<name>.variable".
1023 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1025 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1026 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1029 CALLOC_ARRAY(drv
, 1);
1030 drv
->name
= xmemdupz(name
, namelen
);
1031 *user_convert_tail
= drv
;
1032 user_convert_tail
= &(drv
->next
);
1036 * filter.<name>.smudge and filter.<name>.clean specifies
1041 * The command-line will not be interpolated in any way.
1044 if (!strcmp("smudge", key
))
1045 return git_config_string(&drv
->smudge
, var
, value
);
1047 if (!strcmp("clean", key
))
1048 return git_config_string(&drv
->clean
, var
, value
);
1050 if (!strcmp("process", key
))
1051 return git_config_string(&drv
->process
, var
, value
);
1053 if (!strcmp("required", key
)) {
1054 drv
->required
= git_config_bool(var
, value
);
1061 static int count_ident(const char *cp
, unsigned long size
)
1064 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1076 if (memcmp("Id", cp
, 2))
1087 * "$Id: ... "; scan up to the closing dollar sign and discard.
1103 static int ident_to_git(const char *src
, size_t len
,
1104 struct strbuf
*buf
, int ident
)
1108 if (!ident
|| (src
&& !count_ident(src
, len
)))
1114 /* only grow if not in place */
1115 if (strbuf_avail(buf
) + buf
->len
< len
)
1116 strbuf_grow(buf
, len
- buf
->len
);
1119 dollar
= memchr(src
, '$', len
);
1122 memmove(dst
, src
, dollar
+ 1 - src
);
1123 dst
+= dollar
+ 1 - src
;
1124 len
-= dollar
+ 1 - src
;
1127 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1128 dollar
= memchr(src
+ 3, '$', len
- 3);
1131 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1132 /* Line break before the next dollar. */
1136 memcpy(dst
, "Id$", 3);
1138 len
-= dollar
+ 1 - src
;
1142 memmove(dst
, src
, len
);
1143 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1147 static int ident_to_worktree(const char *src
, size_t len
,
1148 struct strbuf
*buf
, int ident
)
1150 struct object_id oid
;
1151 char *to_free
= NULL
, *dollar
, *spc
;
1157 cnt
= count_ident(src
, len
);
1161 /* are we "faking" in place editing ? */
1162 if (src
== buf
->buf
)
1163 to_free
= strbuf_detach(buf
, NULL
);
1164 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1166 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1168 /* step 1: run to the next '$' */
1169 dollar
= memchr(src
, '$', len
);
1172 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1173 len
-= dollar
+ 1 - src
;
1176 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1177 if (len
< 3 || memcmp("Id", src
, 2))
1180 /* step 3: skip over Id$ or Id:xxxxx$ */
1181 if (src
[2] == '$') {
1184 } else if (src
[2] == ':') {
1186 * It's possible that an expanded Id has crept its way into the
1187 * repository, we cope with that by stripping the expansion out.
1188 * This is probably not a good idea, since it will cause changes
1189 * on checkout, which won't go away by stash, but let's keep it
1190 * for git-style ids.
1192 dollar
= memchr(src
+ 3, '$', len
- 3);
1194 /* incomplete keyword, no more '$', so just quit the loop */
1198 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1199 /* Line break before the next dollar. */
1203 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1204 if (spc
&& spc
< dollar
-1) {
1205 /* There are spaces in unexpected places.
1206 * This is probably an id from some other
1207 * versioning system. Keep it for now.
1212 len
-= dollar
+ 1 - src
;
1215 /* it wasn't a "Id$" or "Id:xxxx$" */
1219 /* step 4: substitute */
1220 strbuf_addstr(buf
, "Id: ");
1221 strbuf_addstr(buf
, oid_to_hex(&oid
));
1222 strbuf_addstr(buf
, " $");
1224 strbuf_add(buf
, src
, len
);
1230 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1232 const char *value
= check
->value
;
1234 if (ATTR_UNSET(value
) || !strlen(value
))
1237 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1238 die(_("true/false are no valid working-tree-encodings"));
1241 /* Don't encode to the default encoding */
1242 if (same_encoding(value
, default_encoding
))
1248 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1250 const char *value
= check
->value
;
1252 if (ATTR_TRUE(value
))
1254 else if (ATTR_FALSE(value
))
1256 else if (ATTR_UNSET(value
))
1258 else if (!strcmp(value
, "input"))
1259 return CRLF_TEXT_INPUT
;
1260 else if (!strcmp(value
, "auto"))
1262 return CRLF_UNDEFINED
;
1265 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1267 const char *value
= check
->value
;
1269 if (ATTR_UNSET(value
))
1271 else if (!strcmp(value
, "lf"))
1273 else if (!strcmp(value
, "crlf"))
1278 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1280 const char *value
= check
->value
;
1281 struct convert_driver
*drv
;
1283 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1285 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1286 if (!strcmp(value
, drv
->name
))
1291 static int git_path_check_ident(struct attr_check_item
*check
)
1293 const char *value
= check
->value
;
1295 return !!ATTR_TRUE(value
);
1298 static struct attr_check
*check
;
1300 void convert_attrs(struct index_state
*istate
,
1301 struct conv_attrs
*ca
, const char *path
)
1303 struct attr_check_item
*ccheck
= NULL
;
1306 check
= attr_check_initl("crlf", "ident", "filter",
1307 "eol", "text", "working-tree-encoding",
1309 user_convert_tail
= &user_convert
;
1310 git_config(read_convert_config
, NULL
);
1313 git_check_attr(istate
, NULL
, path
, check
);
1314 ccheck
= check
->items
;
1315 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1316 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1317 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1318 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1319 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1320 if (ca
->crlf_action
!= CRLF_BINARY
) {
1321 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1322 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1323 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1324 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1325 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1326 else if (eol_attr
== EOL_LF
)
1327 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1328 else if (eol_attr
== EOL_CRLF
)
1329 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1331 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1333 /* Save attr and make a decision for action */
1334 ca
->attr_action
= ca
->crlf_action
;
1335 if (ca
->crlf_action
== CRLF_TEXT
)
1336 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1337 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1338 ca
->crlf_action
= CRLF_BINARY
;
1339 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1340 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1341 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1342 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1345 void reset_parsed_attributes(void)
1347 struct convert_driver
*drv
, *next
;
1349 attr_check_free(check
);
1351 reset_merge_attributes();
1353 for (drv
= user_convert
; drv
; drv
= next
) {
1355 free((void *)drv
->name
);
1358 user_convert
= NULL
;
1359 user_convert_tail
= NULL
;
1362 int would_convert_to_git_filter_fd(struct index_state
*istate
, const char *path
)
1364 struct conv_attrs ca
;
1366 convert_attrs(istate
, &ca
, path
);
1371 * Apply a filter to an fd only if the filter is required to succeed.
1372 * We must die if the filter fails, because the original data before
1373 * filtering is not available.
1375 if (!ca
.drv
->required
)
1378 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1381 const char *get_convert_attr_ascii(struct index_state
*istate
, const char *path
)
1383 struct conv_attrs ca
;
1385 convert_attrs(istate
, &ca
, path
);
1386 switch (ca
.attr_action
) {
1387 case CRLF_UNDEFINED
:
1393 case CRLF_TEXT_INPUT
:
1394 return "text eol=lf";
1395 case CRLF_TEXT_CRLF
:
1396 return "text eol=crlf";
1399 case CRLF_AUTO_CRLF
:
1400 return "text=auto eol=crlf";
1401 case CRLF_AUTO_INPUT
:
1402 return "text=auto eol=lf";
1407 int convert_to_git(struct index_state
*istate
,
1408 const char *path
, const char *src
, size_t len
,
1409 struct strbuf
*dst
, int conv_flags
)
1412 struct conv_attrs ca
;
1414 convert_attrs(istate
, &ca
, path
);
1416 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1417 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1418 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1425 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1431 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1432 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1438 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1441 void convert_to_git_filter_fd(struct index_state
*istate
,
1442 const char *path
, int fd
, struct strbuf
*dst
,
1445 struct conv_attrs ca
;
1446 convert_attrs(istate
, &ca
, path
);
1450 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1451 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1453 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1454 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1455 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1458 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1459 const char *path
, const char *src
,
1460 size_t len
, struct strbuf
*dst
,
1462 const struct checkout_metadata
*meta
,
1463 struct delayed_checkout
*dco
)
1465 int ret
= 0, ret_filter
= 0;
1467 ret
|= ident_to_worktree(src
, len
, dst
, ca
->ident
);
1473 * CRLF conversion can be skipped if normalizing, unless there
1474 * is a smudge or process filter (even if the process filter doesn't
1475 * support smudge). The filters might expect CRLFs.
1477 if ((ca
->drv
&& (ca
->drv
->smudge
|| ca
->drv
->process
)) || !normalizing
) {
1478 ret
|= crlf_to_worktree(src
, len
, dst
, ca
->crlf_action
);
1485 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
->working_tree_encoding
);
1491 ret_filter
= apply_filter(
1492 path
, src
, len
, -1, dst
, ca
->drv
, CAP_SMUDGE
, meta
, dco
);
1493 if (!ret_filter
&& ca
->drv
&& ca
->drv
->required
)
1494 die(_("%s: smudge filter %s failed"), path
, ca
->drv
->name
);
1496 return ret
| ret_filter
;
1499 int async_convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1500 const char *path
, const char *src
,
1501 size_t len
, struct strbuf
*dst
,
1502 const struct checkout_metadata
*meta
,
1505 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1509 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1510 const char *path
, const char *src
,
1511 size_t len
, struct strbuf
*dst
,
1512 const struct checkout_metadata
*meta
)
1514 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1518 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1519 const char *src
, size_t len
, struct strbuf
*dst
)
1521 struct conv_attrs ca
;
1524 convert_attrs(istate
, &ca
, path
);
1525 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1531 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1534 /*****************************************************************
1536 * Streaming conversion support
1538 *****************************************************************/
1540 typedef int (*filter_fn
)(struct stream_filter
*,
1541 const char *input
, size_t *isize_p
,
1542 char *output
, size_t *osize_p
);
1543 typedef void (*free_fn
)(struct stream_filter
*);
1545 struct stream_filter_vtbl
{
1550 struct stream_filter
{
1551 struct stream_filter_vtbl
*vtbl
;
1554 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1555 const char *input
, size_t *isize_p
,
1556 char *output
, size_t *osize_p
)
1561 return 0; /* we do not keep any states */
1563 if (*osize_p
< count
)
1566 memmove(output
, input
, count
);
1573 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1575 ; /* nothing -- null instances are shared */
1578 static struct stream_filter_vtbl null_vtbl
= {
1579 .filter
= null_filter_fn
,
1580 .free
= null_free_fn
,
1583 static struct stream_filter null_filter_singleton
= {
1587 int is_null_stream_filter(struct stream_filter
*filter
)
1589 return filter
== &null_filter_singleton
;
1597 struct lf_to_crlf_filter
{
1598 struct stream_filter filter
;
1599 unsigned has_held
:1;
1603 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1604 const char *input
, size_t *isize_p
,
1605 char *output
, size_t *osize_p
)
1607 size_t count
, o
= 0;
1608 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1611 * We may be holding onto the CR to see if it is followed by a
1612 * LF, in which case we would need to go to the main loop.
1613 * Otherwise, just emit it to the output stream.
1615 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1616 output
[o
++] = lf_to_crlf
->held
;
1617 lf_to_crlf
->has_held
= 0;
1620 /* We are told to drain */
1627 if (count
|| lf_to_crlf
->has_held
) {
1631 if (lf_to_crlf
->has_held
) {
1633 lf_to_crlf
->has_held
= 0;
1636 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1641 } else if (was_cr
) {
1643 * Previous round saw CR and it is not followed
1644 * by a LF; emit the CR before processing the
1645 * current character.
1651 * We may have consumed the last output slot,
1652 * in which case we need to break out of this
1653 * loop; hold the current character before
1656 if (*osize_p
<= o
) {
1657 lf_to_crlf
->has_held
= 1;
1658 lf_to_crlf
->held
= ch
;
1659 continue; /* break but increment i */
1674 if (!lf_to_crlf
->has_held
&& was_cr
) {
1675 lf_to_crlf
->has_held
= 1;
1676 lf_to_crlf
->held
= '\r';
1682 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1687 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1688 .filter
= lf_to_crlf_filter_fn
,
1689 .free
= lf_to_crlf_free_fn
,
1692 static struct stream_filter
*lf_to_crlf_filter(void)
1694 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1696 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1697 return (struct stream_filter
*)lf_to_crlf
;
1703 #define FILTER_BUFFER 1024
1704 struct cascade_filter
{
1705 struct stream_filter filter
;
1706 struct stream_filter
*one
;
1707 struct stream_filter
*two
;
1708 char buf
[FILTER_BUFFER
];
1712 static int cascade_filter_fn(struct stream_filter
*filter
,
1713 const char *input
, size_t *isize_p
,
1714 char *output
, size_t *osize_p
)
1716 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1718 size_t sz
= *osize_p
;
1719 size_t to_feed
, remaining
;
1722 * input -- (one) --> buf -- (two) --> output
1724 while (filled
< sz
) {
1725 remaining
= sz
- filled
;
1727 /* do we already have something to feed two with? */
1728 if (cas
->ptr
< cas
->end
) {
1729 to_feed
= cas
->end
- cas
->ptr
;
1730 if (stream_filter(cas
->two
,
1731 cas
->buf
+ cas
->ptr
, &to_feed
,
1732 output
+ filled
, &remaining
))
1734 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1735 filled
= sz
- remaining
;
1739 /* feed one from upstream and have it emit into our buffer */
1740 to_feed
= input
? *isize_p
: 0;
1741 if (input
&& !to_feed
)
1743 remaining
= sizeof(cas
->buf
);
1744 if (stream_filter(cas
->one
,
1746 cas
->buf
, &remaining
))
1748 cas
->end
= sizeof(cas
->buf
) - remaining
;
1751 size_t fed
= *isize_p
- to_feed
;
1756 /* do we know that we drained one completely? */
1757 if (input
|| cas
->end
)
1760 /* tell two to drain; we have nothing more to give it */
1762 remaining
= sz
- filled
;
1763 if (stream_filter(cas
->two
,
1765 output
+ filled
, &remaining
))
1767 if (remaining
== (sz
- filled
))
1768 break; /* completely drained two */
1769 filled
= sz
- remaining
;
1775 static void cascade_free_fn(struct stream_filter
*filter
)
1777 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1778 free_stream_filter(cas
->one
);
1779 free_stream_filter(cas
->two
);
1783 static struct stream_filter_vtbl cascade_vtbl
= {
1784 .filter
= cascade_filter_fn
,
1785 .free
= cascade_free_fn
,
1788 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1789 struct stream_filter
*two
)
1791 struct cascade_filter
*cascade
;
1793 if (!one
|| is_null_stream_filter(one
))
1795 if (!two
|| is_null_stream_filter(two
))
1798 cascade
= xmalloc(sizeof(*cascade
));
1801 cascade
->end
= cascade
->ptr
= 0;
1802 cascade
->filter
.vtbl
= &cascade_vtbl
;
1803 return (struct stream_filter
*)cascade
;
1809 #define IDENT_DRAINING (-1)
1810 #define IDENT_SKIPPING (-2)
1811 struct ident_filter
{
1812 struct stream_filter filter
;
1815 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1818 static int is_foreign_ident(const char *str
)
1822 if (!skip_prefix(str
, "$Id: ", &str
))
1824 for (i
= 0; str
[i
]; i
++) {
1825 if (isspace(str
[i
]) && str
[i
+1] != '$')
1831 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1833 size_t to_drain
= ident
->left
.len
;
1835 if (*osize_p
< to_drain
)
1836 to_drain
= *osize_p
;
1838 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1839 strbuf_remove(&ident
->left
, 0, to_drain
);
1840 *output_p
+= to_drain
;
1841 *osize_p
-= to_drain
;
1843 if (!ident
->left
.len
)
1847 static int ident_filter_fn(struct stream_filter
*filter
,
1848 const char *input
, size_t *isize_p
,
1849 char *output
, size_t *osize_p
)
1851 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1852 static const char head
[] = "$Id";
1855 /* drain upon eof */
1856 switch (ident
->state
) {
1858 strbuf_add(&ident
->left
, head
, ident
->state
);
1860 case IDENT_SKIPPING
:
1862 case IDENT_DRAINING
:
1863 ident_drain(ident
, &output
, osize_p
);
1868 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1871 if (ident
->state
== IDENT_DRAINING
) {
1872 ident_drain(ident
, &output
, osize_p
);
1881 if (ident
->state
== IDENT_SKIPPING
) {
1883 * Skipping until '$' or LF, but keeping them
1884 * in case it is a foreign ident.
1886 strbuf_addch(&ident
->left
, ch
);
1887 if (ch
!= '\n' && ch
!= '$')
1889 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1890 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1891 strbuf_addstr(&ident
->left
, ident
->ident
);
1893 ident
->state
= IDENT_DRAINING
;
1897 if (ident
->state
< sizeof(head
) &&
1898 head
[ident
->state
] == ch
) {
1904 strbuf_add(&ident
->left
, head
, ident
->state
);
1905 if (ident
->state
== sizeof(head
) - 1) {
1906 if (ch
!= ':' && ch
!= '$') {
1907 strbuf_addch(&ident
->left
, ch
);
1913 strbuf_addch(&ident
->left
, ch
);
1914 ident
->state
= IDENT_SKIPPING
;
1916 strbuf_addstr(&ident
->left
, ident
->ident
);
1917 ident
->state
= IDENT_DRAINING
;
1922 strbuf_addch(&ident
->left
, ch
);
1923 ident
->state
= IDENT_DRAINING
;
1928 static void ident_free_fn(struct stream_filter
*filter
)
1930 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1931 strbuf_release(&ident
->left
);
1935 static struct stream_filter_vtbl ident_vtbl
= {
1936 .filter
= ident_filter_fn
,
1937 .free
= ident_free_fn
,
1940 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1942 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1944 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1945 ": %s $", oid_to_hex(oid
));
1946 strbuf_init(&ident
->left
, 0);
1947 ident
->filter
.vtbl
= &ident_vtbl
;
1949 return (struct stream_filter
*)ident
;
1953 * Return an appropriately constructed filter for the given ca, or NULL if
1954 * the contents cannot be filtered without reading the whole thing
1957 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1958 * large binary blob you would want us not to slurp into the memory!
1960 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1961 const struct object_id
*oid
)
1963 struct stream_filter
*filter
= NULL
;
1965 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
1969 filter
= ident_filter(oid
);
1971 if (output_eol(ca
->crlf_action
) == EOL_CRLF
)
1972 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1974 filter
= cascade_filter(filter
, &null_filter_singleton
);
1979 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
1981 const struct object_id
*oid
)
1983 struct conv_attrs ca
;
1984 convert_attrs(istate
, &ca
, path
);
1985 return get_stream_filter_ca(&ca
, oid
);
1988 void free_stream_filter(struct stream_filter
*filter
)
1990 filter
->vtbl
->free(filter
);
1993 int stream_filter(struct stream_filter
*filter
,
1994 const char *input
, size_t *isize_p
,
1995 char *output
, size_t *osize_p
)
1997 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
2000 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
2001 const struct object_id
*treeish
,
2002 const struct object_id
*blob
)
2004 memset(meta
, 0, sizeof(*meta
));
2006 meta
->refname
= refname
;
2008 oidcpy(&meta
->treeish
, treeish
);
2010 oidcpy(&meta
->blob
, blob
);
2013 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2014 const struct checkout_metadata
*src
,
2015 const struct object_id
*blob
)
2017 memcpy(dst
, src
, sizeof(*dst
));
2019 oidcpy(&dst
->blob
, blob
);
2022 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2025 if (ca
->drv
->process
)
2026 return CA_CLASS_INCORE_PROCESS
;
2027 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2028 return CA_CLASS_INCORE_FILTER
;
2031 if (ca
->working_tree_encoding
)
2032 return CA_CLASS_INCORE
;
2034 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2035 return CA_CLASS_INCORE
;
2037 return CA_CLASS_STREAMABLE
;