1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
11 #include "object-file.h"
13 #include "run-command.h"
15 #include "read-cache-ll.h"
18 #include "sub-process.h"
24 * convert.c - convert a file when checking it out and checking it in.
26 * This should use the pathname to decide on whether it wants to do some
27 * more interesting conversions (automatic gzip/unzip, general format
28 * conversions etc etc), but by default it just does automatic CRLF<->LF
29 * translation when the "text" attribute or "auto_crlf" option is set.
32 /* Stat bits: When BIN is set, the txt bits are unset */
33 #define CONVERT_STAT_BITS_TXT_LF 0x1
34 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
35 #define CONVERT_STAT_BITS_BIN 0x4
38 /* NUL, CR, LF and CRLF counts */
39 unsigned nul
, lonecr
, lonelf
, crlf
;
41 /* These are just approximations! */
42 unsigned printable
, nonprintable
;
45 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
49 memset(stats
, 0, sizeof(*stats
));
51 for (i
= 0; i
< size
; i
++) {
52 unsigned char c
= buf
[i
];
54 if (i
+1 < size
&& buf
[i
+1] == '\n') {
67 stats
->nonprintable
++;
70 /* BS, HT, ESC and FF */
71 case '\b': case '\t': case '\033': case '\014':
78 stats
->nonprintable
++;
85 /* If file ends with EOF then don't count this EOF as non-printable. */
86 if (size
>= 1 && buf
[size
-1] == '\032')
87 stats
->nonprintable
--;
91 * The same heuristics as diff.c::mmfile_is_binary()
92 * We treat files with bare CR as binary
94 static int convert_is_binary(const struct text_stat
*stats
)
100 if ((stats
->printable
>> 7) < stats
->nonprintable
)
105 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
107 struct text_stat stats
;
111 gather_stats(data
, size
, &stats
);
112 if (convert_is_binary(&stats
))
113 ret
|= CONVERT_STAT_BITS_BIN
;
115 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
117 ret
|= CONVERT_STAT_BITS_TXT_LF
;
122 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
124 unsigned int convert_stats
= gather_convert_stats(data
, size
);
126 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
128 switch (convert_stats
) {
129 case CONVERT_STAT_BITS_TXT_LF
:
131 case CONVERT_STAT_BITS_TXT_CRLF
:
133 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
140 const char *get_cached_convert_stats_ascii(struct index_state
*istate
,
145 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
146 ret
= gather_convert_stats_ascii(data
, sz
);
151 const char *get_wt_convert_stats_ascii(const char *path
)
153 const char *ret
= "";
154 struct strbuf sb
= STRBUF_INIT
;
155 if (strbuf_read_file(&sb
, path
, 0) >= 0)
156 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
161 static int text_eol_is_crlf(void)
163 if (auto_crlf
== AUTO_CRLF_TRUE
)
165 else if (auto_crlf
== AUTO_CRLF_INPUT
)
167 if (core_eol
== EOL_CRLF
)
169 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
174 static enum eol
output_eol(enum convert_crlf_action crlf_action
)
176 switch (crlf_action
) {
181 case CRLF_TEXT_INPUT
:
186 case CRLF_AUTO_INPUT
:
191 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
193 warning(_("illegal crlf_action %d"), (int)crlf_action
);
197 static void check_global_conv_flags_eol(const char *path
,
198 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
201 if (old_stats
->crlf
&& !new_stats
->crlf
) {
203 * CRLFs would not be restored by checkout
205 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
206 die(_("CRLF would be replaced by LF in %s"), path
);
207 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
208 warning(_("in the working copy of '%s', CRLF will be"
209 " replaced by LF the next time Git touches"
211 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
213 * CRLFs would be added by checkout
215 if (conv_flags
& CONV_EOL_RNDTRP_DIE
)
216 die(_("LF would be replaced by CRLF in %s"), path
);
217 else if (conv_flags
& CONV_EOL_RNDTRP_WARN
)
218 warning(_("in the working copy of '%s', LF will be"
219 " replaced by CRLF the next time Git touches"
224 static int has_crlf_in_index(struct index_state
*istate
, const char *path
)
231 data
= read_blob_data_from_index(istate
, path
, &sz
);
235 crp
= memchr(data
, '\r', sz
);
237 unsigned int ret_stats
;
238 ret_stats
= gather_convert_stats(data
, sz
);
239 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
240 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
247 static int will_convert_lf_to_crlf(struct text_stat
*stats
,
248 enum convert_crlf_action crlf_action
)
250 if (output_eol(crlf_action
) != EOL_CRLF
)
252 /* No "naked" LF? Nothing to convert, regardless. */
256 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
257 /* If we have any CR or CRLF line endings, we do not touch it */
258 /* This is the new safer autocrlf-handling */
259 if (stats
->lonecr
|| stats
->crlf
)
262 if (convert_is_binary(stats
))
269 static int validate_encoding(const char *path
, const char *enc
,
270 const char *data
, size_t len
, int die_on_error
)
272 const char *stripped
;
274 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
275 if (skip_iprefix(enc
, "UTF", &stripped
)) {
276 skip_prefix(stripped
, "-", &stripped
);
279 * Check for detectable errors in UTF encodings
281 if (has_prohibited_utf_bom(enc
, data
, len
)) {
282 const char *error_msg
= _(
283 "BOM is prohibited in '%s' if encoded as %s");
285 * This advice is shown for UTF-??BE and UTF-??LE encodings.
286 * We cut off the last two characters of the encoding name
287 * to generate the encoding name suitable for BOMs.
289 const char *advise_msg
= _(
290 "The file '%s' contains a byte order "
291 "mark (BOM). Please use UTF-%.*s as "
292 "working-tree-encoding.");
293 int stripped_len
= strlen(stripped
) - strlen("BE");
294 advise(advise_msg
, path
, stripped_len
, stripped
);
296 die(error_msg
, path
, enc
);
298 return error(error_msg
, path
, enc
);
301 } else if (is_missing_required_utf_bom(enc
, data
, len
)) {
302 const char *error_msg
= _(
303 "BOM is required in '%s' if encoded as %s");
304 const char *advise_msg
= _(
305 "The file '%s' is missing a byte order "
306 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
307 "(depending on the byte order) as "
308 "working-tree-encoding.");
309 advise(advise_msg
, path
, stripped
, stripped
);
311 die(error_msg
, path
, enc
);
313 return error(error_msg
, path
, enc
);
321 static void trace_encoding(const char *context
, const char *path
,
322 const char *encoding
, const char *buf
, size_t len
)
324 static struct trace_key coe
= TRACE_KEY_INIT(WORKING_TREE_ENCODING
);
325 struct strbuf trace
= STRBUF_INIT
;
328 if (!trace_want(&coe
))
331 strbuf_addf(&trace
, "%s (%s, considered %s):\n", context
, path
, encoding
);
332 for (i
= 0; i
< len
&& buf
; ++i
) {
334 &trace
, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
336 (unsigned char) buf
[i
],
337 (buf
[i
] > 32 && buf
[i
] < 127 ? buf
[i
] : ' '),
338 ((i
+1) % 8 && (i
+1) < len
? ' ' : '\n')
341 strbuf_addchars(&trace
, '\n', 1);
343 trace_strbuf(&coe
, &trace
);
344 strbuf_release(&trace
);
347 static int check_roundtrip(const char *enc_name
)
350 * check_roundtrip_encoding contains a string of comma and/or
351 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
352 * Search for the given encoding in that string.
354 const char *encoding
= check_roundtrip_encoding
?
355 check_roundtrip_encoding
: "SHIFT-JIS";
356 const char *found
= strcasestr(encoding
, enc_name
);
361 next
= found
+ strlen(enc_name
);
362 len
= strlen(encoding
);
365 * Check that the found encoding is at the beginning of
366 * encoding or that it is prefixed with a space or
369 found
== encoding
|| (
370 (isspace(found
[-1]) || found
[-1] == ',')
374 * Check that the found encoding is at the end of
375 * encoding or that it is suffixed with a space
378 next
== encoding
+ len
|| (
379 next
< encoding
+ len
&&
380 (isspace(next
[0]) || next
[0] == ',')
385 static const char *default_encoding
= "UTF-8";
387 static int encode_to_git(const char *path
, const char *src
, size_t src_len
,
388 struct strbuf
*buf
, const char *enc
, int conv_flags
)
392 int die_on_error
= conv_flags
& CONV_WRITE_OBJECT
;
395 * No encoding is specified or there is nothing to encode.
396 * Tell the caller that the content was not modified.
398 if (!enc
|| (src
&& !src_len
))
402 * Looks like we got called from "would_convert_to_git()".
403 * This means Git wants to know if it would encode (= modify!)
404 * the content. Let's answer with "yes", since an encoding was
410 if (validate_encoding(path
, enc
, src
, src_len
, die_on_error
))
413 trace_encoding("source", path
, enc
, src
, src_len
);
414 dst
= reencode_string_len(src
, src_len
, default_encoding
, enc
,
418 * We could add the blob "as-is" to Git. However, on checkout
419 * we would try to re-encode to the original encoding. This
420 * would fail and we would leave the user with a messed-up
421 * working tree. Let's try to avoid this by screaming loud.
423 const char* msg
= _("failed to encode '%s' from %s to %s");
425 die(msg
, path
, enc
, default_encoding
);
427 error(msg
, path
, enc
, default_encoding
);
431 trace_encoding("destination", path
, default_encoding
, dst
, dst_len
);
434 * UTF supports lossless conversion round tripping [1] and conversions
435 * between UTF and other encodings are mostly round trip safe as
436 * Unicode aims to be a superset of all other character encodings.
437 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
438 * trip issues [2]. Check the round trip conversion for all encodings
439 * listed in core.checkRoundtripEncoding.
441 * The round trip check is only performed if content is written to Git.
442 * This ensures that no information is lost during conversion to/from
443 * the internal UTF-8 representation.
445 * Please note, the code below is not tested because I was not able to
446 * generate a faulty round trip without an iconv error. Iconv errors
447 * are already caught above.
449 * [1] http://unicode.org/faq/utf_bom.html#gen2
450 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
452 if (die_on_error
&& check_roundtrip(enc
)) {
456 re_src
= reencode_string_len(dst
, dst_len
,
457 enc
, default_encoding
,
460 trace_printf("Checking roundtrip encoding for %s...\n", enc
);
461 trace_encoding("reencoded source", path
, enc
,
464 if (!re_src
|| src_len
!= re_src_len
||
465 memcmp(src
, re_src
, src_len
)) {
466 const char* msg
= _("encoding '%s' from %s to %s and "
467 "back is not the same");
468 die(msg
, path
, enc
, default_encoding
);
474 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
478 static int encode_to_worktree(const char *path
, const char *src
, size_t src_len
,
479 struct strbuf
*buf
, const char *enc
)
485 * No encoding is specified or there is nothing to encode.
486 * Tell the caller that the content was not modified.
488 if (!enc
|| (src
&& !src_len
))
491 dst
= reencode_string_len(src
, src_len
, enc
, default_encoding
,
494 error(_("failed to encode '%s' from %s to %s"),
495 path
, default_encoding
, enc
);
499 strbuf_attach(buf
, dst
, dst_len
, dst_len
+ 1);
503 static int crlf_to_git(struct index_state
*istate
,
504 const char *path
, const char *src
, size_t len
,
506 enum convert_crlf_action crlf_action
, int conv_flags
)
508 struct text_stat stats
;
510 int convert_crlf_into_lf
;
512 if (crlf_action
== CRLF_BINARY
||
517 * If we are doing a dry-run and have no source buffer, there is
518 * nothing to analyze; we must assume we would convert.
523 gather_stats(src
, len
, &stats
);
524 /* Optimization: No CRLF? Nothing to convert, regardless. */
525 convert_crlf_into_lf
= !!stats
.crlf
;
527 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
528 if (convert_is_binary(&stats
))
531 * If the file in the index has any CR in it, do not
532 * convert. This is the new safer autocrlf handling,
533 * unless we want to renormalize in a merge or
536 if ((!(conv_flags
& CONV_EOL_RENORMALIZE
)) &&
537 has_crlf_in_index(istate
, path
))
538 convert_crlf_into_lf
= 0;
540 if (((conv_flags
& CONV_EOL_RNDTRP_WARN
) ||
541 ((conv_flags
& CONV_EOL_RNDTRP_DIE
) && len
))) {
542 struct text_stat new_stats
;
543 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
544 /* simulate "git add" */
545 if (convert_crlf_into_lf
) {
546 new_stats
.lonelf
+= new_stats
.crlf
;
549 /* simulate "git checkout" */
550 if (will_convert_lf_to_crlf(&new_stats
, crlf_action
)) {
551 new_stats
.crlf
+= new_stats
.lonelf
;
552 new_stats
.lonelf
= 0;
554 check_global_conv_flags_eol(path
, &stats
, &new_stats
, conv_flags
);
556 if (!convert_crlf_into_lf
)
560 * At this point all of our source analysis is done, and we are sure we
561 * would convert. If we are in dry-run mode, we can give an answer.
566 /* only grow if not in place */
567 if (strbuf_avail(buf
) + buf
->len
< len
)
568 strbuf_grow(buf
, len
- buf
->len
);
570 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
572 * If we guessed, we already know we rejected a file with
573 * lone CR, and we can strip a CR without looking at what
577 unsigned char c
= *src
++;
583 unsigned char c
= *src
++;
584 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
588 strbuf_setlen(buf
, dst
- buf
->buf
);
592 static int crlf_to_worktree(const char *src
, size_t len
, struct strbuf
*buf
,
593 enum convert_crlf_action crlf_action
)
595 char *to_free
= NULL
;
596 struct text_stat stats
;
598 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
601 gather_stats(src
, len
, &stats
);
602 if (!will_convert_lf_to_crlf(&stats
, crlf_action
))
605 /* are we "faking" in place editing ? */
607 to_free
= strbuf_detach(buf
, NULL
);
609 strbuf_grow(buf
, len
+ stats
.lonelf
);
611 const char *nl
= memchr(src
, '\n', len
);
614 if (nl
> src
&& nl
[-1] == '\r') {
615 strbuf_add(buf
, src
, nl
+ 1 - src
);
617 strbuf_add(buf
, src
, nl
- src
);
618 strbuf_addstr(buf
, "\r\n");
623 strbuf_add(buf
, src
, len
);
629 struct filter_params
{
637 static int filter_buffer_or_fd(int in UNUSED
, int out
, void *data
)
640 * Spawn cmd and feed the buffer contents through its stdin.
642 struct child_process child_process
= CHILD_PROCESS_INIT
;
643 struct filter_params
*params
= (struct filter_params
*)data
;
644 const char *format
= params
->cmd
;
645 int write_err
, status
;
647 /* apply % substitution to cmd */
648 struct strbuf cmd
= STRBUF_INIT
;
650 /* expand all %f with the quoted path; quote to preserve space, etc. */
651 while (strbuf_expand_step(&cmd
, &format
)) {
652 if (skip_prefix(format
, "%", &format
))
653 strbuf_addch(&cmd
, '%');
654 else if (skip_prefix(format
, "f", &format
))
655 sq_quote_buf(&cmd
, params
->path
);
657 strbuf_addch(&cmd
, '%');
660 strvec_push(&child_process
.args
, cmd
.buf
);
661 child_process
.use_shell
= 1;
662 child_process
.in
= -1;
663 child_process
.out
= out
;
665 if (start_command(&child_process
)) {
666 strbuf_release(&cmd
);
667 return error(_("cannot fork to run external filter '%s'"),
671 sigchain_push(SIGPIPE
, SIG_IGN
);
674 write_err
= (write_in_full(child_process
.in
,
675 params
->src
, params
->size
) < 0);
679 write_err
= copy_fd(params
->fd
, child_process
.in
);
680 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
684 if (close(child_process
.in
))
687 error(_("cannot feed the input to external filter '%s'"),
690 sigchain_pop(SIGPIPE
);
692 status
= finish_command(&child_process
);
694 error(_("external filter '%s' failed %d"), params
->cmd
, status
);
696 strbuf_release(&cmd
);
697 return (write_err
|| status
);
700 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
701 struct strbuf
*dst
, const char *cmd
)
704 * Create a pipeline to have the command filter the buffer's
707 * (child --> cmd) --> us
710 struct strbuf nbuf
= STRBUF_INIT
;
712 struct filter_params params
;
714 memset(&async
, 0, sizeof(async
));
715 async
.proc
= filter_buffer_or_fd
;
716 async
.data
= ¶ms
;
725 if (start_async(&async
))
726 return 0; /* error was already reported */
728 if (strbuf_read(&nbuf
, async
.out
, 0) < 0) {
729 err
= error(_("read from external filter '%s' failed"), cmd
);
731 if (close(async
.out
)) {
732 err
= error(_("read from external filter '%s' failed"), cmd
);
734 if (finish_async(&async
)) {
735 err
= error(_("external filter '%s' failed"), cmd
);
739 strbuf_swap(dst
, &nbuf
);
741 strbuf_release(&nbuf
);
745 #define CAP_CLEAN (1u<<0)
746 #define CAP_SMUDGE (1u<<1)
747 #define CAP_DELAY (1u<<2)
750 struct subprocess_entry subprocess
; /* must be the first member! */
751 unsigned int supported_capabilities
;
754 static int subprocess_map_initialized
;
755 static struct hashmap subprocess_map
;
757 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
759 static int versions
[] = {2, 0};
760 static struct subprocess_capability capabilities
[] = {
761 { "clean", CAP_CLEAN
},
762 { "smudge", CAP_SMUDGE
},
763 { "delay", CAP_DELAY
},
766 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
767 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
769 &entry
->supported_capabilities
);
772 static void handle_filter_error(const struct strbuf
*filter_status
,
773 struct cmd2process
*entry
,
774 const unsigned int wanted_capability
)
776 if (!strcmp(filter_status
->buf
, "error"))
777 ; /* The filter signaled a problem with the file. */
778 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
780 * The filter signaled a permanent problem. Don't try to filter
781 * files with the same command for the lifetime of the current
784 entry
->supported_capabilities
&= ~wanted_capability
;
787 * Something went wrong with the protocol filter.
788 * Force shutdown and restart if another blob requires filtering.
790 error(_("external filter '%s' failed"), entry
->subprocess
.cmd
);
791 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
796 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
797 int fd
, struct strbuf
*dst
, const char *cmd
,
798 const unsigned int wanted_capability
,
799 const struct checkout_metadata
*meta
,
800 struct delayed_checkout
*dco
)
804 struct cmd2process
*entry
;
805 struct child_process
*process
;
806 struct strbuf nbuf
= STRBUF_INIT
;
807 struct strbuf filter_status
= STRBUF_INIT
;
808 const char *filter_type
;
810 if (!subprocess_map_initialized
) {
811 subprocess_map_initialized
= 1;
812 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
815 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
821 entry
= xmalloc(sizeof(*entry
));
822 entry
->supported_capabilities
= 0;
824 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
829 process
= &entry
->subprocess
.process
;
831 if (!(entry
->supported_capabilities
& wanted_capability
))
834 if (wanted_capability
& CAP_CLEAN
)
835 filter_type
= "clean";
836 else if (wanted_capability
& CAP_SMUDGE
)
837 filter_type
= "smudge";
839 die(_("unexpected filter type"));
841 sigchain_push(SIGPIPE
, SIG_IGN
);
843 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
844 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
848 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
850 error(_("path name too long for external filter"));
854 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
858 if (meta
&& meta
->refname
) {
859 err
= packet_write_fmt_gently(process
->in
, "ref=%s\n", meta
->refname
);
864 if (meta
&& !is_null_oid(&meta
->treeish
)) {
865 err
= packet_write_fmt_gently(process
->in
, "treeish=%s\n", oid_to_hex(&meta
->treeish
));
870 if (meta
&& !is_null_oid(&meta
->blob
)) {
871 err
= packet_write_fmt_gently(process
->in
, "blob=%s\n", oid_to_hex(&meta
->blob
));
876 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
877 dco
&& dco
->state
== CE_CAN_DELAY
) {
879 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
884 err
= packet_flush_gently(process
->in
);
889 err
= write_packetized_from_fd_no_flush(fd
, process
->in
);
891 err
= write_packetized_from_buf_no_flush(src
, len
, process
->in
);
895 err
= packet_flush_gently(process
->in
);
899 err
= subprocess_read_status(process
->out
, &filter_status
);
903 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
904 string_list_insert(&dco
->filters
, cmd
);
905 string_list_insert(&dco
->paths
, path
);
907 /* The filter got the blob and wants to send us a response. */
908 err
= strcmp(filter_status
.buf
, "success");
912 err
= read_packetized_to_strbuf(process
->out
, &nbuf
,
913 PACKET_READ_GENTLE_ON_EOF
) < 0;
917 err
= subprocess_read_status(process
->out
, &filter_status
);
921 err
= strcmp(filter_status
.buf
, "success");
925 sigchain_pop(SIGPIPE
);
928 handle_filter_error(&filter_status
, entry
, wanted_capability
);
930 strbuf_swap(dst
, &nbuf
);
931 strbuf_release(&nbuf
);
932 strbuf_release(&filter_status
);
937 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
941 struct cmd2process
*entry
;
942 struct child_process
*process
;
943 struct strbuf filter_status
= STRBUF_INIT
;
945 assert(subprocess_map_initialized
);
946 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
948 error(_("external filter '%s' is not available anymore although "
949 "not all paths have been filtered"), cmd
);
952 process
= &entry
->subprocess
.process
;
953 sigchain_push(SIGPIPE
, SIG_IGN
);
955 err
= packet_write_fmt_gently(
956 process
->in
, "command=list_available_blobs\n");
960 err
= packet_flush_gently(process
->in
);
964 while ((line
= packet_read_line(process
->out
, NULL
))) {
966 if (skip_prefix(line
, "pathname=", &path
))
967 string_list_insert(available_paths
, path
);
969 ; /* ignore unknown keys */
972 err
= subprocess_read_status(process
->out
, &filter_status
);
976 err
= strcmp(filter_status
.buf
, "success");
979 sigchain_pop(SIGPIPE
);
982 handle_filter_error(&filter_status
, entry
, 0);
983 strbuf_release(&filter_status
);
987 static struct convert_driver
{
989 struct convert_driver
*next
;
994 } *user_convert
, **user_convert_tail
;
996 static int apply_filter(const char *path
, const char *src
, size_t len
,
997 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
998 const unsigned int wanted_capability
,
999 const struct checkout_metadata
*meta
,
1000 struct delayed_checkout
*dco
)
1002 const char *cmd
= NULL
;
1010 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
1012 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
1016 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
1017 else if (drv
->process
&& *drv
->process
)
1018 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
1019 drv
->process
, wanted_capability
, meta
, dco
);
1024 static int read_convert_config(const char *var
, const char *value
,
1025 const struct config_context
*ctx UNUSED
,
1028 const char *key
, *name
;
1030 struct convert_driver
*drv
;
1033 * External conversion drivers are configured using
1034 * "filter.<name>.variable".
1036 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
1038 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1039 if (!xstrncmpz(drv
->name
, name
, namelen
))
1042 CALLOC_ARRAY(drv
, 1);
1043 drv
->name
= xmemdupz(name
, namelen
);
1044 *user_convert_tail
= drv
;
1045 user_convert_tail
= &(drv
->next
);
1049 * filter.<name>.smudge and filter.<name>.clean specifies
1054 * The command-line will not be interpolated in any way.
1057 if (!strcmp("smudge", key
)) {
1058 FREE_AND_NULL(drv
->smudge
);
1059 return git_config_string(&drv
->smudge
, var
, value
);
1062 if (!strcmp("clean", key
)) {
1063 FREE_AND_NULL(drv
->clean
);
1064 return git_config_string(&drv
->clean
, var
, value
);
1067 if (!strcmp("process", key
)) {
1068 FREE_AND_NULL(drv
->process
);
1069 return git_config_string(&drv
->process
, var
, value
);
1072 if (!strcmp("required", key
)) {
1073 drv
->required
= git_config_bool(var
, value
);
1080 static int count_ident(const char *cp
, unsigned long size
)
1083 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1095 if (memcmp("Id", cp
, 2))
1106 * "$Id: ... "; scan up to the closing dollar sign and discard.
1122 static int ident_to_git(const char *src
, size_t len
,
1123 struct strbuf
*buf
, int ident
)
1127 if (!ident
|| (src
&& !count_ident(src
, len
)))
1133 /* only grow if not in place */
1134 if (strbuf_avail(buf
) + buf
->len
< len
)
1135 strbuf_grow(buf
, len
- buf
->len
);
1138 dollar
= memchr(src
, '$', len
);
1141 memmove(dst
, src
, dollar
+ 1 - src
);
1142 dst
+= dollar
+ 1 - src
;
1143 len
-= dollar
+ 1 - src
;
1146 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
1147 dollar
= memchr(src
+ 3, '$', len
- 3);
1150 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1151 /* Line break before the next dollar. */
1155 memcpy(dst
, "Id$", 3);
1157 len
-= dollar
+ 1 - src
;
1161 memmove(dst
, src
, len
);
1162 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
1166 static int ident_to_worktree(const char *src
, size_t len
,
1167 struct strbuf
*buf
, int ident
)
1169 struct object_id oid
;
1170 char *to_free
= NULL
, *dollar
, *spc
;
1176 cnt
= count_ident(src
, len
);
1180 /* are we "faking" in place editing ? */
1181 if (src
== buf
->buf
)
1182 to_free
= strbuf_detach(buf
, NULL
);
1183 hash_object_file(the_hash_algo
, src
, len
, OBJ_BLOB
, &oid
);
1185 strbuf_grow(buf
, len
+ cnt
* (the_hash_algo
->hexsz
+ 3));
1187 /* step 1: run to the next '$' */
1188 dollar
= memchr(src
, '$', len
);
1191 strbuf_add(buf
, src
, dollar
+ 1 - src
);
1192 len
-= dollar
+ 1 - src
;
1195 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1196 if (len
< 3 || memcmp("Id", src
, 2))
1199 /* step 3: skip over Id$ or Id:xxxxx$ */
1200 if (src
[2] == '$') {
1203 } else if (src
[2] == ':') {
1205 * It's possible that an expanded Id has crept its way into the
1206 * repository, we cope with that by stripping the expansion out.
1207 * This is probably not a good idea, since it will cause changes
1208 * on checkout, which won't go away by stash, but let's keep it
1209 * for git-style ids.
1211 dollar
= memchr(src
+ 3, '$', len
- 3);
1213 /* incomplete keyword, no more '$', so just quit the loop */
1217 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1218 /* Line break before the next dollar. */
1222 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1223 if (spc
&& spc
< dollar
-1) {
1224 /* There are spaces in unexpected places.
1225 * This is probably an id from some other
1226 * versioning system. Keep it for now.
1231 len
-= dollar
+ 1 - src
;
1234 /* it wasn't a "Id$" or "Id:xxxx$" */
1238 /* step 4: substitute */
1239 strbuf_addstr(buf
, "Id: ");
1240 strbuf_addstr(buf
, oid_to_hex(&oid
));
1241 strbuf_addstr(buf
, " $");
1243 strbuf_add(buf
, src
, len
);
1249 static const char *git_path_check_encoding(struct attr_check_item
*check
)
1251 const char *value
= check
->value
;
1253 if (ATTR_UNSET(value
) || !strlen(value
))
1256 if (ATTR_TRUE(value
) || ATTR_FALSE(value
)) {
1257 die(_("true/false are no valid working-tree-encodings"));
1260 /* Don't encode to the default encoding */
1261 if (same_encoding(value
, default_encoding
))
1267 static enum convert_crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1269 const char *value
= check
->value
;
1271 if (ATTR_TRUE(value
))
1273 else if (ATTR_FALSE(value
))
1275 else if (ATTR_UNSET(value
))
1277 else if (!strcmp(value
, "input"))
1278 return CRLF_TEXT_INPUT
;
1279 else if (!strcmp(value
, "auto"))
1281 return CRLF_UNDEFINED
;
1284 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1286 const char *value
= check
->value
;
1288 if (ATTR_UNSET(value
))
1290 else if (!strcmp(value
, "lf"))
1292 else if (!strcmp(value
, "crlf"))
1297 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1299 const char *value
= check
->value
;
1300 struct convert_driver
*drv
;
1302 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1304 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1305 if (!strcmp(value
, drv
->name
))
1310 static int git_path_check_ident(struct attr_check_item
*check
)
1312 const char *value
= check
->value
;
1314 return !!ATTR_TRUE(value
);
1317 static struct attr_check
*check
;
1319 void convert_attrs(struct index_state
*istate
,
1320 struct conv_attrs
*ca
, const char *path
)
1322 struct attr_check_item
*ccheck
= NULL
;
1325 check
= attr_check_initl("crlf", "ident", "filter",
1326 "eol", "text", "working-tree-encoding",
1328 user_convert_tail
= &user_convert
;
1329 git_config(read_convert_config
, NULL
);
1332 git_check_attr(istate
, path
, check
);
1333 ccheck
= check
->items
;
1334 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1335 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1336 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1337 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1338 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1339 if (ca
->crlf_action
!= CRLF_BINARY
) {
1340 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1341 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1342 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1343 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1344 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1345 else if (eol_attr
== EOL_LF
)
1346 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1347 else if (eol_attr
== EOL_CRLF
)
1348 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1350 ca
->working_tree_encoding
= git_path_check_encoding(ccheck
+ 5);
1352 /* Save attr and make a decision for action */
1353 ca
->attr_action
= ca
->crlf_action
;
1354 if (ca
->crlf_action
== CRLF_TEXT
)
1355 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1356 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1357 ca
->crlf_action
= CRLF_BINARY
;
1358 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1359 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1360 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1361 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1364 void reset_parsed_attributes(void)
1366 struct convert_driver
*drv
, *next
;
1368 attr_check_free(check
);
1370 reset_merge_attributes();
1372 for (drv
= user_convert
; drv
; drv
= next
) {
1374 free((void *)drv
->name
);
1375 free((void *)drv
->smudge
);
1376 free((void *)drv
->clean
);
1377 free((void *)drv
->process
);
1380 user_convert
= NULL
;
1381 user_convert_tail
= NULL
;
1384 int would_convert_to_git_filter_fd(struct index_state
*istate
, const char *path
)
1386 struct conv_attrs ca
;
1388 convert_attrs(istate
, &ca
, path
);
1393 * Apply a filter to an fd only if the filter is required to succeed.
1394 * We must die if the filter fails, because the original data before
1395 * filtering is not available.
1397 if (!ca
.drv
->required
)
1400 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1403 const char *get_convert_attr_ascii(struct index_state
*istate
, const char *path
)
1405 struct conv_attrs ca
;
1407 convert_attrs(istate
, &ca
, path
);
1408 switch (ca
.attr_action
) {
1409 case CRLF_UNDEFINED
:
1415 case CRLF_TEXT_INPUT
:
1416 return "text eol=lf";
1417 case CRLF_TEXT_CRLF
:
1418 return "text eol=crlf";
1421 case CRLF_AUTO_CRLF
:
1422 return "text=auto eol=crlf";
1423 case CRLF_AUTO_INPUT
:
1424 return "text=auto eol=lf";
1429 int convert_to_git(struct index_state
*istate
,
1430 const char *path
, const char *src
, size_t len
,
1431 struct strbuf
*dst
, int conv_flags
)
1434 struct conv_attrs ca
;
1436 convert_attrs(istate
, &ca
, path
);
1438 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
);
1439 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1440 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1447 ret
|= encode_to_git(path
, src
, len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1453 if (!(conv_flags
& CONV_EOL_KEEP_CRLF
)) {
1454 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, conv_flags
);
1460 return ret
| ident_to_git(src
, len
, dst
, ca
.ident
);
1463 void convert_to_git_filter_fd(struct index_state
*istate
,
1464 const char *path
, int fd
, struct strbuf
*dst
,
1467 struct conv_attrs ca
;
1468 convert_attrs(istate
, &ca
, path
);
1472 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
, NULL
))
1473 die(_("%s: clean filter '%s' failed"), path
, ca
.drv
->name
);
1475 encode_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.working_tree_encoding
, conv_flags
);
1476 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, conv_flags
);
1477 ident_to_git(dst
->buf
, dst
->len
, dst
, ca
.ident
);
1480 static int convert_to_working_tree_ca_internal(const struct conv_attrs
*ca
,
1481 const char *path
, const char *src
,
1482 size_t len
, struct strbuf
*dst
,
1484 const struct checkout_metadata
*meta
,
1485 struct delayed_checkout
*dco
)
1487 int ret
= 0, ret_filter
= 0;
1489 ret
|= ident_to_worktree(src
, len
, dst
, ca
->ident
);
1495 * CRLF conversion can be skipped if normalizing, unless there
1496 * is a smudge or process filter (even if the process filter doesn't
1497 * support smudge). The filters might expect CRLFs.
1499 if ((ca
->drv
&& (ca
->drv
->smudge
|| ca
->drv
->process
)) || !normalizing
) {
1500 ret
|= crlf_to_worktree(src
, len
, dst
, ca
->crlf_action
);
1507 ret
|= encode_to_worktree(path
, src
, len
, dst
, ca
->working_tree_encoding
);
1513 ret_filter
= apply_filter(
1514 path
, src
, len
, -1, dst
, ca
->drv
, CAP_SMUDGE
, meta
, dco
);
1515 if (!ret_filter
&& ca
->drv
&& ca
->drv
->required
)
1516 die(_("%s: smudge filter %s failed"), path
, ca
->drv
->name
);
1518 return ret
| ret_filter
;
1521 int async_convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1522 const char *path
, const char *src
,
1523 size_t len
, struct strbuf
*dst
,
1524 const struct checkout_metadata
*meta
,
1527 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1531 int convert_to_working_tree_ca(const struct conv_attrs
*ca
,
1532 const char *path
, const char *src
,
1533 size_t len
, struct strbuf
*dst
,
1534 const struct checkout_metadata
*meta
)
1536 return convert_to_working_tree_ca_internal(ca
, path
, src
, len
, dst
, 0,
1540 int renormalize_buffer(struct index_state
*istate
, const char *path
,
1541 const char *src
, size_t len
, struct strbuf
*dst
)
1543 struct conv_attrs ca
;
1546 convert_attrs(istate
, &ca
, path
);
1547 ret
= convert_to_working_tree_ca_internal(&ca
, path
, src
, len
, dst
, 1,
1553 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, CONV_EOL_RENORMALIZE
);
1556 /*****************************************************************
1558 * Streaming conversion support
1560 *****************************************************************/
1562 typedef int (*filter_fn
)(struct stream_filter
*,
1563 const char *input
, size_t *isize_p
,
1564 char *output
, size_t *osize_p
);
1565 typedef void (*free_fn
)(struct stream_filter
*);
1567 struct stream_filter_vtbl
{
1572 struct stream_filter
{
1573 struct stream_filter_vtbl
*vtbl
;
1576 static int null_filter_fn(struct stream_filter
*filter UNUSED
,
1577 const char *input
, size_t *isize_p
,
1578 char *output
, size_t *osize_p
)
1583 return 0; /* we do not keep any states */
1585 if (*osize_p
< count
)
1588 memmove(output
, input
, count
);
1595 static void null_free_fn(struct stream_filter
*filter UNUSED
)
1597 ; /* nothing -- null instances are shared */
1600 static struct stream_filter_vtbl null_vtbl
= {
1601 .filter
= null_filter_fn
,
1602 .free
= null_free_fn
,
1605 static struct stream_filter null_filter_singleton
= {
1609 int is_null_stream_filter(struct stream_filter
*filter
)
1611 return filter
== &null_filter_singleton
;
1619 struct lf_to_crlf_filter
{
1620 struct stream_filter filter
;
1621 unsigned has_held
:1;
1625 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1626 const char *input
, size_t *isize_p
,
1627 char *output
, size_t *osize_p
)
1629 size_t count
, o
= 0;
1630 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1633 * We may be holding onto the CR to see if it is followed by a
1634 * LF, in which case we would need to go to the main loop.
1635 * Otherwise, just emit it to the output stream.
1637 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1638 output
[o
++] = lf_to_crlf
->held
;
1639 lf_to_crlf
->has_held
= 0;
1642 /* We are told to drain */
1649 if (count
|| lf_to_crlf
->has_held
) {
1653 if (lf_to_crlf
->has_held
) {
1655 lf_to_crlf
->has_held
= 0;
1658 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1663 } else if (was_cr
) {
1665 * Previous round saw CR and it is not followed
1666 * by a LF; emit the CR before processing the
1667 * current character.
1673 * We may have consumed the last output slot,
1674 * in which case we need to break out of this
1675 * loop; hold the current character before
1678 if (*osize_p
<= o
) {
1679 lf_to_crlf
->has_held
= 1;
1680 lf_to_crlf
->held
= ch
;
1681 continue; /* break but increment i */
1696 if (!lf_to_crlf
->has_held
&& was_cr
) {
1697 lf_to_crlf
->has_held
= 1;
1698 lf_to_crlf
->held
= '\r';
1704 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1709 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1710 .filter
= lf_to_crlf_filter_fn
,
1711 .free
= lf_to_crlf_free_fn
,
1714 static struct stream_filter
*lf_to_crlf_filter(void)
1716 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1718 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1719 return (struct stream_filter
*)lf_to_crlf
;
1725 #define FILTER_BUFFER 1024
1726 struct cascade_filter
{
1727 struct stream_filter filter
;
1728 struct stream_filter
*one
;
1729 struct stream_filter
*two
;
1730 char buf
[FILTER_BUFFER
];
1734 static int cascade_filter_fn(struct stream_filter
*filter
,
1735 const char *input
, size_t *isize_p
,
1736 char *output
, size_t *osize_p
)
1738 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1740 size_t sz
= *osize_p
;
1741 size_t to_feed
, remaining
;
1744 * input -- (one) --> buf -- (two) --> output
1746 while (filled
< sz
) {
1747 remaining
= sz
- filled
;
1749 /* do we already have something to feed two with? */
1750 if (cas
->ptr
< cas
->end
) {
1751 to_feed
= cas
->end
- cas
->ptr
;
1752 if (stream_filter(cas
->two
,
1753 cas
->buf
+ cas
->ptr
, &to_feed
,
1754 output
+ filled
, &remaining
))
1756 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1757 filled
= sz
- remaining
;
1761 /* feed one from upstream and have it emit into our buffer */
1762 to_feed
= input
? *isize_p
: 0;
1763 if (input
&& !to_feed
)
1765 remaining
= sizeof(cas
->buf
);
1766 if (stream_filter(cas
->one
,
1768 cas
->buf
, &remaining
))
1770 cas
->end
= sizeof(cas
->buf
) - remaining
;
1773 size_t fed
= *isize_p
- to_feed
;
1778 /* do we know that we drained one completely? */
1779 if (input
|| cas
->end
)
1782 /* tell two to drain; we have nothing more to give it */
1784 remaining
= sz
- filled
;
1785 if (stream_filter(cas
->two
,
1787 output
+ filled
, &remaining
))
1789 if (remaining
== (sz
- filled
))
1790 break; /* completely drained two */
1791 filled
= sz
- remaining
;
1797 static void cascade_free_fn(struct stream_filter
*filter
)
1799 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1800 free_stream_filter(cas
->one
);
1801 free_stream_filter(cas
->two
);
1805 static struct stream_filter_vtbl cascade_vtbl
= {
1806 .filter
= cascade_filter_fn
,
1807 .free
= cascade_free_fn
,
1810 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1811 struct stream_filter
*two
)
1813 struct cascade_filter
*cascade
;
1815 if (!one
|| is_null_stream_filter(one
))
1817 if (!two
|| is_null_stream_filter(two
))
1820 cascade
= xmalloc(sizeof(*cascade
));
1823 cascade
->end
= cascade
->ptr
= 0;
1824 cascade
->filter
.vtbl
= &cascade_vtbl
;
1825 return (struct stream_filter
*)cascade
;
1831 #define IDENT_DRAINING (-1)
1832 #define IDENT_SKIPPING (-2)
1833 struct ident_filter
{
1834 struct stream_filter filter
;
1837 char ident
[GIT_MAX_HEXSZ
+ 5]; /* ": x40 $" */
1840 static int is_foreign_ident(const char *str
)
1844 if (!skip_prefix(str
, "$Id: ", &str
))
1846 for (i
= 0; str
[i
]; i
++) {
1847 if (isspace(str
[i
]) && str
[i
+1] != '$')
1853 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1855 size_t to_drain
= ident
->left
.len
;
1857 if (*osize_p
< to_drain
)
1858 to_drain
= *osize_p
;
1860 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1861 strbuf_remove(&ident
->left
, 0, to_drain
);
1862 *output_p
+= to_drain
;
1863 *osize_p
-= to_drain
;
1865 if (!ident
->left
.len
)
1869 static int ident_filter_fn(struct stream_filter
*filter
,
1870 const char *input
, size_t *isize_p
,
1871 char *output
, size_t *osize_p
)
1873 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1874 static const char head
[] = "$Id";
1877 /* drain upon eof */
1878 switch (ident
->state
) {
1880 strbuf_add(&ident
->left
, head
, ident
->state
);
1882 case IDENT_SKIPPING
:
1884 case IDENT_DRAINING
:
1885 ident_drain(ident
, &output
, osize_p
);
1890 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1893 if (ident
->state
== IDENT_DRAINING
) {
1894 ident_drain(ident
, &output
, osize_p
);
1903 if (ident
->state
== IDENT_SKIPPING
) {
1905 * Skipping until '$' or LF, but keeping them
1906 * in case it is a foreign ident.
1908 strbuf_addch(&ident
->left
, ch
);
1909 if (ch
!= '\n' && ch
!= '$')
1911 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1912 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1913 strbuf_addstr(&ident
->left
, ident
->ident
);
1915 ident
->state
= IDENT_DRAINING
;
1919 if (ident
->state
< sizeof(head
) &&
1920 head
[ident
->state
] == ch
) {
1926 strbuf_add(&ident
->left
, head
, ident
->state
);
1927 if (ident
->state
== sizeof(head
) - 1) {
1928 if (ch
!= ':' && ch
!= '$') {
1929 strbuf_addch(&ident
->left
, ch
);
1935 strbuf_addch(&ident
->left
, ch
);
1936 ident
->state
= IDENT_SKIPPING
;
1938 strbuf_addstr(&ident
->left
, ident
->ident
);
1939 ident
->state
= IDENT_DRAINING
;
1944 strbuf_addch(&ident
->left
, ch
);
1945 ident
->state
= IDENT_DRAINING
;
1950 static void ident_free_fn(struct stream_filter
*filter
)
1952 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1953 strbuf_release(&ident
->left
);
1957 static struct stream_filter_vtbl ident_vtbl
= {
1958 .filter
= ident_filter_fn
,
1959 .free
= ident_free_fn
,
1962 static struct stream_filter
*ident_filter(const struct object_id
*oid
)
1964 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1966 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1967 ": %s $", oid_to_hex(oid
));
1968 strbuf_init(&ident
->left
, 0);
1969 ident
->filter
.vtbl
= &ident_vtbl
;
1971 return (struct stream_filter
*)ident
;
1975 * Return an appropriately constructed filter for the given ca, or NULL if
1976 * the contents cannot be filtered without reading the whole thing
1979 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1980 * large binary blob you would want us not to slurp into the memory!
1982 struct stream_filter
*get_stream_filter_ca(const struct conv_attrs
*ca
,
1983 const struct object_id
*oid
)
1985 struct stream_filter
*filter
= NULL
;
1987 if (classify_conv_attrs(ca
) != CA_CLASS_STREAMABLE
)
1991 filter
= ident_filter(oid
);
1993 if (output_eol(ca
->crlf_action
) == EOL_CRLF
)
1994 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1996 filter
= cascade_filter(filter
, &null_filter_singleton
);
2001 struct stream_filter
*get_stream_filter(struct index_state
*istate
,
2003 const struct object_id
*oid
)
2005 struct conv_attrs ca
;
2006 convert_attrs(istate
, &ca
, path
);
2007 return get_stream_filter_ca(&ca
, oid
);
2010 void free_stream_filter(struct stream_filter
*filter
)
2012 filter
->vtbl
->free(filter
);
2015 int stream_filter(struct stream_filter
*filter
,
2016 const char *input
, size_t *isize_p
,
2017 char *output
, size_t *osize_p
)
2019 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);
2022 void init_checkout_metadata(struct checkout_metadata
*meta
, const char *refname
,
2023 const struct object_id
*treeish
,
2024 const struct object_id
*blob
)
2026 memset(meta
, 0, sizeof(*meta
));
2028 meta
->refname
= refname
;
2030 oidcpy(&meta
->treeish
, treeish
);
2032 oidcpy(&meta
->blob
, blob
);
2035 void clone_checkout_metadata(struct checkout_metadata
*dst
,
2036 const struct checkout_metadata
*src
,
2037 const struct object_id
*blob
)
2039 memcpy(dst
, src
, sizeof(*dst
));
2041 oidcpy(&dst
->blob
, blob
);
2044 enum conv_attrs_classification
classify_conv_attrs(const struct conv_attrs
*ca
)
2047 if (ca
->drv
->process
)
2048 return CA_CLASS_INCORE_PROCESS
;
2049 if (ca
->drv
->smudge
|| ca
->drv
->clean
)
2050 return CA_CLASS_INCORE_FILTER
;
2053 if (ca
->working_tree_encoding
)
2054 return CA_CLASS_INCORE
;
2056 if (ca
->crlf_action
== CRLF_AUTO
|| ca
->crlf_action
== CRLF_AUTO_CRLF
)
2057 return CA_CLASS_INCORE
;
2059 return CA_CLASS_STREAMABLE
;