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
39 /* NUL, CR, LF and CRLF counts */
40 unsigned nul
, lonecr
, lonelf
, crlf
;
42 /* These are just approximations! */
43 unsigned printable
, nonprintable
;
46 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
50 memset(stats
, 0, sizeof(*stats
));
52 for (i
= 0; i
< size
; i
++) {
53 unsigned char c
= buf
[i
];
55 if (i
+1 < size
&& buf
[i
+1] == '\n') {
68 stats
->nonprintable
++;
71 /* BS, HT, ESC and FF */
72 case '\b': case '\t': case '\033': case '\014':
79 stats
->nonprintable
++;
86 /* If file ends with EOF then don't count this EOF as non-printable. */
87 if (size
>= 1 && buf
[size
-1] == '\032')
88 stats
->nonprintable
--;
92 * The same heuristics as diff.c::mmfile_is_binary()
93 * We treat files with bare CR as binary
95 static int convert_is_binary(const struct text_stat
*stats
)
101 if ((stats
->printable
>> 7) < stats
->nonprintable
)
106 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
108 struct text_stat stats
;
112 gather_stats(data
, size
, &stats
);
113 if (convert_is_binary(&stats
))
114 ret
|= CONVERT_STAT_BITS_BIN
;
116 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
118 ret
|= CONVERT_STAT_BITS_TXT_LF
;
123 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
125 unsigned int convert_stats
= gather_convert_stats(data
, size
);
127 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
129 switch (convert_stats
) {
130 case CONVERT_STAT_BITS_TXT_LF
:
132 case CONVERT_STAT_BITS_TXT_CRLF
:
134 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
141 const char *get_cached_convert_stats_ascii(const struct index_state
*istate
,
146 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
147 ret
= gather_convert_stats_ascii(data
, sz
);
152 const char *get_wt_convert_stats_ascii(const char *path
)
154 const char *ret
= "";
155 struct strbuf sb
= STRBUF_INIT
;
156 if (strbuf_read_file(&sb
, path
, 0) >= 0)
157 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
162 static int text_eol_is_crlf(void)
164 if (auto_crlf
== AUTO_CRLF_TRUE
)
166 else if (auto_crlf
== AUTO_CRLF_INPUT
)
168 if (core_eol
== EOL_CRLF
)
170 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
175 static enum eol
output_eol(enum crlf_action crlf_action
)
177 switch (crlf_action
) {
182 case CRLF_TEXT_INPUT
:
187 case CRLF_AUTO_INPUT
:
192 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
194 warning(_("illegal crlf_action %d"), (int)crlf_action
);
198 static void check_global_conv_flags_eol(const char *path
,
199 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
202 if (old_stats
->crlf
&& !new_stats
->crlf
) {
204 * CRLFs would not be restored by checkout
206 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
207 die(_("CRLF would be replaced by LF in %s"), path
);
208 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
209 warning(_("CRLF will be replaced by LF in %s.\n"
210 "The file will have its original line"
211 " endings in your working directory"), path
);
212 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
214 * CRLFs would be added by checkout
216 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
217 die(_("LF would be replaced by CRLF in %s"), path
);
218 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
219 warning(_("LF will be replaced by CRLF in %s.\n"
220 "The file will have its original line"
221 " endings in your working directory"), path
);
225 static int has_crlf_in_index(const struct index_state
*istate
, const char *path
)
232 data
= read_blob_data_from_index(istate
, path
, &sz
);
236 crp
= memchr(data
, '\r', sz
);
238 unsigned int ret_stats
;
239 ret_stats
= gather_convert_stats(data
, sz
);
240 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
241 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
248 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
249 enum crlf_action crlf_action
)
251 if (output_eol(crlf_action
) != EOL_CRLF
)
253 /* No "naked" LF? Nothing to convert, regardless. */
257 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
258 /* If we have any CR or CRLF line endings, we do not touch it */
259 /* This is the new safer autocrlf-handling */
260 if (stats
->lonecr
|| stats
->crlf
)
263 if (convert_is_binary(stats
))
270 static int validate_encoding(const char *path
, const char *enc
,
271 const char *data
, size_t len
, int die_on_error
)
273 const char *stripped
;
275 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
276 if (skip_iprefix(enc
, "UTF", &stripped
)) {
277 skip_prefix(stripped
, "-", &stripped
);
280 * Check for detectable errors in UTF encodings
282 if (has_prohibited_utf_bom(enc
, data
, len
)) {
283 const char *error_msg
= _(
284 "BOM is prohibited in '%s' if encoded as %s");
286 * This advice is shown for UTF-??BE and UTF-??LE encodings.
287 * We cut off the last two characters of the encoding name
288 * to generate the encoding name suitable for BOMs.
290 const char *advise_msg
= _(
291 "The file '%s' contains a byte order "
292 "mark (BOM). Please use UTF-%.*s as "
293 "working-tree-encoding.");
294 int stripped_len
= strlen(stripped
) - strlen("BE");
295 advise(advise_msg
, path
, stripped_len
, stripped
);
297 die(error_msg
, path
, enc
);
299 return error(error_msg
, path
, enc
);
302 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
303 const char *error_msg
= _(
304 "BOM is required in '%s' if encoded as %s");
305 const char *advise_msg
= _(
306 "The file '%s' is missing a byte order "
307 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
308 "(depending on the byte order) as "
309 "working-tree-encoding.");
310 advise(advise_msg
, path
, stripped
, stripped
);
312 die(error_msg
, path
, enc
);
314 return error(error_msg
, path
, enc
);
322 static void trace_encoding(const char *context
, const char *path
,
323 const char *encoding
, const char *buf
, size_t len
)
325 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
326 struct strbuf trace
= STRBUF_INIT
;
329 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
330 for (i
= 0; i
< len
&& buf
; ++i
) {
332 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
334 (unsigned char) buf
[i
],
335 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
336 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
339 strbuf_addchars(&trace
, '\n', 1);
341 trace_strbuf(&coe
, &trace
);
342 strbuf_release(&trace
);
345 static int check_roundtrip(const char *enc_name
)
348 * check_roundtrip_encoding contains a string of comma and/or
349 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
350 * Search for the given encoding in that string.
352 const char *found
= strcasestr(check_roundtrip_encoding
, enc_name
);
357 next
= found
+ strlen(enc_name
);
358 len
= strlen(check_roundtrip_encoding
);
361 * check that the found encoding is at the
362 * beginning of check_roundtrip_encoding or
363 * that it is prefixed with a space or comma
365 found
== check_roundtrip_encoding
|| (
366 (isspace(found
[-1]) || found
[-1] == ',')
370 * check that the found encoding is at the
371 * end of check_roundtrip_encoding or
372 * that it is suffixed with a space or comma
374 next
== check_roundtrip_encoding
+ len
|| (
375 next
< check_roundtrip_encoding
+ len
&&
376 (isspace(next
[0]) || next
[0] == ',')
381 static const char *default_encoding
= "UTF-8";
383 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
384 struct strbuf
*buf
, const char *enc
, int conv_flags
)
388 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
391 * No encoding is specified or there is nothing to encode.
392 * Tell the caller that the content was not modified.
394 if (!enc
|| (src
&& !src_len
))
398 * Looks like we got called from "would_convert_to_git()".
399 * This means Git wants to know if it would encode (= modify!)
400 * the content. Let's answer with "yes", since an encoding was
406 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
409 trace_encoding("source", path
, enc
, src
, src_len
);
410 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
414 * We could add the blob "as-is" to Git. However, on checkout
415 * we would try to re-encode to the original encoding. This
416 * would fail and we would leave the user with a messed-up
417 * working tree. Let's try to avoid this by screaming loud.
419 const char* msg
= _("failed to encode '%s' from %s to %s");
421 die(msg
, path
, enc
, default_encoding
);
423 error(msg
, path
, enc
, default_encoding
);
427 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
430 * UTF supports lossless conversion round tripping [1] and conversions
431 * between UTF and other encodings are mostly round trip safe as
432 * Unicode aims to be a superset of all other character encodings.
433 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
434 * trip issues [2]. Check the round trip conversion for all encodings
435 * listed in core.checkRoundtripEncoding.
437 * The round trip check is only performed if content is written to Git.
438 * This ensures that no information is lost during conversion to/from
439 * the internal UTF-8 representation.
441 * Please note, the code below is not tested because I was not able to
442 * generate a faulty round trip without an iconv error. Iconv errors
443 * are already caught above.
445 * [1] http://unicode.org/faq/utf_bom.html#gen2
446 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
448 if (die_on_error
&& check_roundtrip(enc
)) {
452 re_src
= reencode_string_len(dst
, dst_len
,
453 enc
, default_encoding
,
456 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
457 trace_encoding("reencoded source", path
, enc
,
460 if (!re_src
|| src_len
!= re_src_len
||
461 memcmp(src
, re_src
, src_len
)) {
462 const char* msg
= _("encoding '%s' from %s to %s and "
463 "back is not the same");
464 die(msg
, path
, enc
, default_encoding
);
470 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
474 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
475 struct strbuf
*buf
, const char *enc
)
481 * No encoding is specified or there is nothing to encode.
482 * Tell the caller that the content was not modified.
484 if (!enc
|| (src
&& !src_len
))
487 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
490 error(_("failed to encode '%s' from %s to %s"),
491 path
, default_encoding
, enc
);
495 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
499 static int crlf_to_git(const struct index_state
*istate
,
500 const char *path
, const char *src
, size_t len
,
502 enum crlf_action crlf_action
, int conv_flags
)
504 struct text_stat stats
;
506 int convert_crlf_into_lf
;
508 if (crlf_action
== CRLF_BINARY
||
513 * If we are doing a dry-run and have no source buffer, there is
514 * nothing to analyze; we must assume we would convert.
519 gather_stats(src
, len
, &stats
);
520 /* Optimization: No CRLF? Nothing to convert, regardless. */
521 convert_crlf_into_lf
= !!stats
.crlf
;
523 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
524 if (convert_is_binary(&stats
))
527 * If the file in the index has any CR in it, do not
528 * convert. This is the new safer autocrlf handling,
529 * unless we want to renormalize in a merge or
532 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
533 has_crlf_in_index(istate
, path
))
534 convert_crlf_into_lf
= 0;
536 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
537 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
538 struct text_stat new_stats
;
539 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
540 /* simulate "git add" */
541 if (convert_crlf_into_lf
) {
542 new_stats
.lonelf
+= new_stats
.crlf
;
545 /* simulate "git checkout" */
546 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
547 new_stats
.crlf
+= new_stats
.lonelf
;
548 new_stats
.lonelf
= 0;
550 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
552 if (!convert_crlf_into_lf
)
556 * At this point all of our source analysis is done, and we are sure we
557 * would convert. If we are in dry-run mode, we can give an answer.
562 /* only grow if not in place */
563 if (strbuf_avail(buf
) + buf
->len
< len
)
564 strbuf_grow(buf
, len
- buf
->len
);
566 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
568 * If we guessed, we already know we rejected a file with
569 * lone CR, and we can strip a CR without looking at what
573 unsigned char c
= *src
++;
579 unsigned char c
= *src
++;
580 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
584 strbuf_setlen(buf
, dst
- buf
->buf
);
588 static int crlf_to_worktree(const char *src
, size_t len
,
589 struct strbuf
*buf
, enum crlf_action crlf_action
)
591 char *to_free
= NULL
;
592 struct text_stat stats
;
594 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
597 gather_stats(src
, len
, &stats
);
598 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
601 /* are we "faking" in place editing ? */
603 to_free
= strbuf_detach(buf
, NULL
);
605 strbuf_grow(buf
, len
+ stats
.lonelf
);
607 const char *nl
= memchr(src
, '\n', len
);
610 if (nl
> src
&& nl
[-1] == '\r') {
611 strbuf_add(buf
, src
, nl
+ 1 - src
);
613 strbuf_add(buf
, src
, nl
- src
);
614 strbuf_addstr(buf
, "\r\n");
619 strbuf_add(buf
, src
, len
);
625 struct filter_params
{
633 static int filter_buffer_or_fd(int in
, int out
, void *data
)
636 * Spawn cmd and feed the buffer contents through its stdin.
638 struct child_process child_process
= CHILD_PROCESS_INIT
;
639 struct filter_params
*params
= (struct filter_params
*)data
;
640 int write_err
, status
;
642 /* apply % substitution to cmd */
643 struct strbuf cmd
= STRBUF_INIT
;
644 struct strbuf path
= STRBUF_INIT
;
645 struct strbuf_expand_dict_entry dict
[] = {
650 /* quote the path to preserve spaces, etc. */
651 sq_quote_buf(&path
, params
->path
);
652 dict
[0].value
= path
.buf
;
654 /* expand all %f with the quoted path */
655 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
656 strbuf_release(&path
);
658 strvec_push(&child_process
.args
, cmd
.buf
);
659 child_process
.use_shell
= 1;
660 child_process
.in
= -1;
661 child_process
.out
= out
;
663 if (start_command(&child_process
)) {
664 strbuf_release(&cmd
);
665 return error(_("cannot fork to run external filter '%s'"),
669 sigchain_push(SIGPIPE
, SIG_IGN
);
672 write_err
= (write_in_full(child_process
.in
,
673 params
->src
, params
->size
) < 0);
677 write_err
= copy_fd(params
->fd
, child_process
.in
);
678 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
682 if (close(child_process
.in
))
685 error(_("cannot feed the input to external filter '%s'"),
688 sigchain_pop(SIGPIPE
);
690 status
= finish_command(&child_process
);
692 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
694 strbuf_release(&cmd
);
695 return (write_err
|| status
);
698 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
699 struct strbuf
*dst
, const char *cmd
)
702 * Create a pipeline to have the command filter the buffer's
705 * (child --> cmd) --> us
708 struct strbuf nbuf
= STRBUF_INIT
;
710 struct filter_params params
;
712 memset(&async
, 0, sizeof(async
));
713 async
.proc
= filter_buffer_or_fd
;
714 async
.data
= ¶ms
;
723 if (start_async(&async
))
724 return 0; /* error was already reported */
726 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
727 err
= error(_("read from external filter '%s' failed"), cmd
);
729 if (close(async
.out
)) {
730 err
= error(_("read from external filter '%s' failed"), cmd
);
732 if (finish_async(&async
)) {
733 err
= error(_("external filter '%s' failed"), cmd
);
737 strbuf_swap(dst
, &nbuf
);
739 strbuf_release(&nbuf
);
743 #define CAP_CLEAN (1u<<0)
744 #define CAP_SMUDGE (1u<<1)
745 #define CAP_DELAY (1u<<2)
748 struct subprocess_entry subprocess
; /* must be the first member! */
749 unsigned int supported_capabilities
;
752 static int subprocess_map_initialized
;
753 static struct hashmap subprocess_map
;
755 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
757 static int versions
[] = {2, 0};
758 static struct subprocess_capability capabilities
[] = {
759 { "clean", CAP_CLEAN
},
760 { "smudge", CAP_SMUDGE
},
761 { "delay", CAP_DELAY
},
764 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
765 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
767 &entry
->supported_capabilities
);
770 static void handle_filter_error(const struct strbuf
*filter_status
,
771 struct cmd2process
*entry
,
772 const unsigned int wanted_capability
)
774 if (!strcmp(filter_status
->buf
, "error"))
775 ; /* The filter signaled a problem with the file. */
776 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
778 * The filter signaled a permanent problem. Don't try to filter
779 * files with the same command for the lifetime of the current
782 entry
->supported_capabilities
&= ~wanted_capability
;
785 * Something went wrong with the protocol filter.
786 * Force shutdown and restart if another blob requires filtering.
788 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
789 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
794 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
795 int fd
, struct strbuf
*dst
, const char *cmd
,
796 const unsigned int wanted_capability
,
797 const struct checkout_metadata
*meta
,
798 struct delayed_checkout
*dco
)
802 struct cmd2process
*entry
;
803 struct child_process
*process
;
804 struct strbuf nbuf
= STRBUF_INIT
;
805 struct strbuf filter_status
= STRBUF_INIT
;
806 const char *filter_type
;
808 if (!subprocess_map_initialized
) {
809 subprocess_map_initialized
= 1;
810 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
813 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
819 entry
= xmalloc(sizeof(*entry
));
820 entry
->supported_capabilities
= 0;
822 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
827 process
= &entry
->subprocess
.process
;
829 if (!(entry
->supported_capabilities
& wanted_capability
))
832 if (wanted_capability
& CAP_CLEAN
)
833 filter_type
= "clean";
834 else if (wanted_capability
& CAP_SMUDGE
)
835 filter_type
= "smudge";
837 die(_("unexpected filter type"));
839 sigchain_push(SIGPIPE
, SIG_IGN
);
841 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
842 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
846 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
848 error(_("path name too long for external filter"));
852 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
856 if (meta
&& meta
->refname
) {
857 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
862 if (meta
&& !is_null_oid(&meta
->treeish
)) {
863 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
868 if (meta
&& !is_null_oid(&meta
->blob
)) {
869 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
874 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
875 dco
&& dco
->state
== CE_CAN_DELAY
) {
877 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
882 err
= packet_flush_gently(process
->in
);
887 err
= write_packetized_from_fd(fd
, process
->in
);
889 err
= write_packetized_from_buf(src
, len
, process
->in
);
893 err
= subprocess_read_status(process
->out
, &filter_status
);
897 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
898 string_list_insert(&dco
->filters
, cmd
);
899 string_list_insert(&dco
->paths
, path
);
901 /* The filter got the blob and wants to send us a response. */
902 err
= strcmp(filter_status
.buf
, "success");
906 err
= read_packetized_to_strbuf(process
->out
, &nbuf
) < 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
);
929 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
933 struct cmd2process
*entry
;
934 struct child_process
*process
;
935 struct strbuf filter_status
= STRBUF_INIT
;
937 assert(subprocess_map_initialized
);
938 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
940 error(_("external filter '%s' is not available anymore although "
941 "not all paths have been filtered"), cmd
);
944 process
= &entry
->subprocess
.process
;
945 sigchain_push(SIGPIPE
, SIG_IGN
);
947 err
= packet_write_fmt_gently(
948 process
->in
, "command=list_available_blobs\n");
952 err
= packet_flush_gently(process
->in
);
956 while ((line
= packet_read_line(process
->out
, NULL
))) {
958 if (skip_prefix(line
, "pathname=", &path
))
959 string_list_insert(available_paths
, xstrdup(path
));
961 ; /* ignore unknown keys */
964 err
= subprocess_read_status(process
->out
, &filter_status
);
968 err
= strcmp(filter_status
.buf
, "success");
971 sigchain_pop(SIGPIPE
);
974 handle_filter_error(&filter_status
, entry
, 0);
978 static struct convert_driver
{
980 struct convert_driver
*next
;
985 } *user_convert
, **user_convert_tail
;
987 static int apply_filter(const char *path
, const char *src
, size_t len
,
988 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
989 const unsigned int wanted_capability
,
990 const struct checkout_metadata
*meta
,
991 struct delayed_checkout
*dco
)
993 const char *cmd
= NULL
;
1001 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
1003 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1007 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1008 else if (drv
->process
&& *drv
->process
)
1009 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1010 drv
->process
, wanted_capability
, meta
, dco
);
1015 static int read_convert_config(const char *var
, const char *value
, void *cb
)
1017 const char *key
, *name
;
1019 struct convert_driver
*drv
;
1022 * External conversion drivers are configured using
1023 * "filter.<name>.variable".
1025 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1027 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1028 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
1031 CALLOC_ARRAY(drv
, 1);
1032 drv
->name
= xmemdupz(name
, namelen
);
1033 *user_convert_tail
= drv
;
1034 user_convert_tail
= &(drv
->next
);
1038 * filter.<name>.smudge and filter.<name>.clean specifies
1043 * The command-line will not be interpolated in any way.
1046 if (!strcmp("smudge", key
))
1047 return git_config_string(&drv
->smudge
, var
, value
);
1049 if (!strcmp("clean", key
))
1050 return git_config_string(&drv
->clean
, var
, value
);
1052 if (!strcmp("process", key
))
1053 return git_config_string(&drv
->process
, var
, value
);
1055 if (!strcmp("required", key
)) {
1056 drv
->required
= git_config_bool(var
, value
);
1063 static int count_ident(const char *cp
, unsigned long size
)
1066 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1078 if (memcmp("Id", cp
, 2))
1089 * "$Id: ... "; scan up to the closing dollar sign and discard.
1105 static int ident_to_git(const char *src
, size_t len
,
1106 struct strbuf
*buf
, int ident
)
1110 if (!ident
|| (src
&& !count_ident(src
, len
)))
1116 /* only grow if not in place */
1117 if (strbuf_avail(buf
) + buf
->len
< len
)
1118 strbuf_grow(buf
, len
- buf
->len
);
1121 dollar
= memchr(src
, '$', len
);
1124 memmove(dst
, src
, dollar
+ 1 - src
);
1125 dst
+= dollar
+ 1 - src
;
1126 len
-= dollar
+ 1 - src
;
1129 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1130 dollar
= memchr(src
+ 3, '$', len
- 3);
1133 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1134 /* Line break before the next dollar. */
1138 memcpy(dst
, "Id$", 3);
1140 len
-= dollar
+ 1 - src
;
1144 memmove(dst
, src
, len
);
1145 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1149 static int ident_to_worktree(const char *src
, size_t len
,
1150 struct strbuf
*buf
, int ident
)
1152 struct object_id oid
;
1153 char *to_free
= NULL
, *dollar
, *spc
;
1159 cnt
= count_ident(src
, len
);
1163 /* are we "faking" in place editing ? */
1164 if (src
== buf
->buf
)
1165 to_free
= strbuf_detach(buf
, NULL
);
1166 hash_object_file(the_hash_algo
, src
, len
, "blob", &oid
);
1168 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1170 /* step 1: run to the next '$' */
1171 dollar
= memchr(src
, '$', len
);
1174 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1175 len
-= dollar
+ 1 - src
;
1178 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1179 if (len
< 3 || memcmp("Id", src
, 2))
1182 /* step 3: skip over Id$ or Id:xxxxx$ */
1183 if (src
[2] == '$') {
1186 } else if (src
[2] == ':') {
1188 * It's possible that an expanded Id has crept its way into the
1189 * repository, we cope with that by stripping the expansion out.
1190 * This is probably not a good idea, since it will cause changes
1191 * on checkout, which won't go away by stash, but let's keep it
1192 * for git-style ids.
1194 dollar
= memchr(src
+ 3, '$', len
- 3);
1196 /* incomplete keyword, no more '$', so just quit the loop */
1200 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1201 /* Line break before the next dollar. */
1205 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1206 if (spc
&& spc
< dollar
-1) {
1207 /* There are spaces in unexpected places.
1208 * This is probably an id from some other
1209 * versioning system. Keep it for now.
1214 len
-= dollar
+ 1 - src
;
1217 /* it wasn't a "Id$" or "Id:xxxx$" */
1221 /* step 4: substitute */
1222 strbuf_addstr(buf
, "Id: ");
1223 strbuf_addstr(buf
, oid_to_hex(&oid
));
1224 strbuf_addstr(buf
, " $");
1226 strbuf_add(buf
, src
, len
);
1232 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1234 const char *value
= check
->value
;
1236 if (ATTR_UNSET(value
) || !strlen(value
))
1239 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1240 die(_("true/false are no valid working-tree-encodings"));
1243 /* Don't encode to the default encoding */
1244 if (same_encoding(value
, default_encoding
))
1250 static enum crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1252 const char *value
= check
->value
;
1254 if (ATTR_TRUE(value
))
1256 else if (ATTR_FALSE(value
))
1258 else if (ATTR_UNSET(value
))
1260 else if (!strcmp(value
, "input"))
1261 return CRLF_TEXT_INPUT
;
1262 else if (!strcmp(value
, "auto"))
1264 return CRLF_UNDEFINED
;
1267 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1269 const char *value
= check
->value
;
1271 if (ATTR_UNSET(value
))
1273 else if (!strcmp(value
, "lf"))
1275 else if (!strcmp(value
, "crlf"))
1280 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1282 const char *value
= check
->value
;
1283 struct convert_driver
*drv
;
1285 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1287 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1288 if (!strcmp(value
, drv
->name
))
1293 static int git_path_check_ident(struct attr_check_item
*check
)
1295 const char *value
= check
->value
;
1297 return !!ATTR_TRUE(value
);
1301 struct convert_driver
*drv
;
1302 enum crlf_action attr_action
; /* What attr says */
1303 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
1305 const char *working_tree_encoding
; /* Supported encoding or default encoding if NULL */
1308 static struct attr_check
*check
;
1310 static void convert_attrs(const struct index_state
*istate
,
1311 struct conv_attrs
*ca
, const char *path
)
1313 struct attr_check_item
*ccheck
= NULL
;
1316 check
= attr_check_initl("crlf", "ident", "filter",
1317 "eol", "text", "working-tree-encoding",
1319 user_convert_tail
= &user_convert
;
1320 git_config(read_convert_config
, NULL
);
1323 git_check_attr(istate
, path
, check
);
1324 ccheck
= check
->items
;
1325 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1326 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1327 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1328 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1329 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1330 if (ca
->crlf_action
!= CRLF_BINARY
) {
1331 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1332 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1333 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1334 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1335 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1336 else if (eol_attr
== EOL_LF
)
1337 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1338 else if (eol_attr
== EOL_CRLF
)
1339 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1341 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1343 /* Save attr and make a decision for action */
1344 ca
->attr_action
= ca
->crlf_action
;
1345 if (ca
->crlf_action
== CRLF_TEXT
)
1346 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1347 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1348 ca
->crlf_action
= CRLF_BINARY
;
1349 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1350 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1351 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1352 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1355 void reset_parsed_attributes(void)
1357 struct convert_driver
*drv
, *next
;
1359 attr_check_free(check
);
1361 reset_merge_attributes();
1363 for (drv
= user_convert
; drv
; drv
= next
) {
1365 free((void *)drv
->name
);
1368 user_convert
= NULL
;
1369 user_convert_tail
= NULL
;
1372 int would_convert_to_git_filter_fd(const struct index_state
*istate
, const char *path
)
1374 struct conv_attrs ca
;
1376 convert_attrs(istate
, &ca
, path
);
1381 * Apply a filter to an fd only if the filter is required to succeed.
1382 * We must die if the filter fails, because the original data before
1383 * filtering is not available.
1385 if (!ca
.drv
->required
)
1388 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1391 const char *get_convert_attr_ascii(const struct index_state
*istate
, const char *path
)
1393 struct conv_attrs ca
;
1395 convert_attrs(istate
, &ca
, path
);
1396 switch (ca
.attr_action
) {
1397 case CRLF_UNDEFINED
:
1403 case CRLF_TEXT_INPUT
:
1404 return "text eol=lf";
1405 case CRLF_TEXT_CRLF
:
1406 return "text eol=crlf";
1409 case CRLF_AUTO_CRLF
:
1410 return "text=auto eol=crlf";
1411 case CRLF_AUTO_INPUT
:
1412 return "text=auto eol=lf";
1417 int convert_to_git(const struct index_state
*istate
,
1418 const char *path
, const char *src
, size_t len
,
1419 struct strbuf
*dst
, int conv_flags
)
1422 struct conv_attrs ca
;
1424 convert_attrs(istate
, &ca
, path
);
1426 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1427 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1428 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1435 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1441 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1442 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1448 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1451 void convert_to_git_filter_fd(const struct index_state
*istate
,
1452 const char *path
, int fd
, struct strbuf
*dst
,
1455 struct conv_attrs ca
;
1456 convert_attrs(istate
, &ca
, path
);
1460 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1461 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1463 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1464 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1465 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1468 static int convert_to_working_tree_internal(const struct index_state
*istate
,
1469 const char *path
, const char *src
,
1470 size_t len
, struct strbuf
*dst
,
1472 const struct checkout_metadata
*meta
,
1473 struct delayed_checkout
*dco
)
1475 int ret
= 0, ret_filter
= 0;
1476 struct conv_attrs ca
;
1478 convert_attrs(istate
, &ca
, path
);
1480 ret
|= ident_to_worktree(src
, len
, dst
, ca
.ident
);
1486 * CRLF conversion can be skipped if normalizing, unless there
1487 * is a smudge or process filter (even if the process filter doesn't
1488 * support smudge). The filters might expect CRLFs.
1490 if ((ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->process
)) || !normalizing
) {
1491 ret
|= crlf_to_worktree(src
, len
, dst
, ca
.crlf_action
);
1498 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
.working_tree_encoding
);
1504 ret_filter
= apply_filter(
1505 path
, src
, len
, -1, dst
, ca
.drv
, CAP_SMUDGE
, meta
, dco
);
1506 if (!ret_filter
&& ca
.drv
&& ca
.drv
->required
)
1507 die(_("%s: smudge filter %s failed"), path
, ca
.drv
->name
);
1509 return ret
| ret_filter
;
1512 int async_convert_to_working_tree(const struct index_state
*istate
,
1513 const char *path
, const char *src
,
1514 size_t len
, struct strbuf
*dst
,
1515 const struct checkout_metadata
*meta
,
1518 return convert_to_working_tree_internal(istate
, path
, src
, len
, dst
, 0, meta
, dco
);
1521 int convert_to_working_tree(const struct index_state
*istate
,
1522 const char *path
, const char *src
,
1523 size_t len
, struct strbuf
*dst
,
1524 const struct checkout_metadata
*meta
)
1526 return convert_to_working_tree_internal(istate
, path
, src
, len
, dst
, 0, meta
, NULL
);
1529 int renormalize_buffer(const struct index_state
*istate
, const char *path
,
1530 const char *src
, size_t len
, struct strbuf
*dst
)
1532 int ret
= convert_to_working_tree_internal(istate
, path
, src
, len
, dst
, 1, NULL
, NULL
);
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
,
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
)
1581 ; /* nothing -- null instances are shared */
1584 static struct stream_filter_vtbl null_vtbl
= {
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 lf_to_crlf_filter_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
= {
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
= {
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 path, 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(const struct index_state
*istate
,
1968 const struct object_id
*oid
)
1970 struct conv_attrs ca
;
1971 struct stream_filter
*filter
= NULL
;
1973 convert_attrs(istate
, &ca
, path
);
1974 if (ca
.drv
&& (ca
.drv
->process
|| ca
.drv
->smudge
|| ca
.drv
->clean
))
1977 if (ca
.working_tree_encoding
)
1980 if (ca
.crlf_action
== CRLF_AUTO
|| ca
.crlf_action
== CRLF_AUTO_CRLF
)
1984 filter
= ident_filter(oid
);
1986 if (output_eol(ca
.crlf_action
) == EOL_CRLF
)
1987 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1989 filter
= cascade_filter(filter
, &null_filter_singleton
);
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
);