3 #include "run-command.h"
7 #include "sub-process.h"
10 * convert.c - convert a file when checking it out and checking it in.
12 * This should use the pathname to decide on whether it wants to do some
13 * more interesting conversions (automatic gzip/unzip, general format
14 * conversions etc etc), but by default it just does automatic CRLF<->LF
15 * translation when the "text" attribute or "auto_crlf" option is set.
18 /* Stat bits: When BIN is set, the txt bits are unset */
19 #define CONVERT_STAT_BITS_TXT_LF 0x1
20 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
21 #define CONVERT_STAT_BITS_BIN 0x4
35 /* NUL, CR, LF and CRLF counts */
36 unsigned nul
, lonecr
, lonelf
, crlf
;
38 /* These are just approximations! */
39 unsigned printable
, nonprintable
;
42 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
46 memset(stats
, 0, sizeof(*stats
));
48 for (i
= 0; i
< size
; i
++) {
49 unsigned char c
= buf
[i
];
51 if (i
+1 < size
&& buf
[i
+1] == '\n') {
64 stats
->nonprintable
++;
67 /* BS, HT, ESC and FF */
68 case '\b': case '\t': case '\033': case '\014':
75 stats
->nonprintable
++;
82 /* If file ends with EOF then don't count this EOF as non-printable. */
83 if (size
>= 1 && buf
[size
-1] == '\032')
84 stats
->nonprintable
--;
88 * The same heuristics as diff.c::mmfile_is_binary()
89 * We treat files with bare CR as binary
91 static int convert_is_binary(unsigned long size
, const struct text_stat
*stats
)
97 if ((stats
->printable
>> 7) < stats
->nonprintable
)
102 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
104 struct text_stat stats
;
108 gather_stats(data
, size
, &stats
);
109 if (convert_is_binary(size
, &stats
))
110 ret
|= CONVERT_STAT_BITS_BIN
;
112 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
114 ret
|= CONVERT_STAT_BITS_TXT_LF
;
119 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
121 unsigned int convert_stats
= gather_convert_stats(data
, size
);
123 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
125 switch (convert_stats
) {
126 case CONVERT_STAT_BITS_TXT_LF
:
128 case CONVERT_STAT_BITS_TXT_CRLF
:
130 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
137 const char *get_cached_convert_stats_ascii(const struct index_state
*istate
,
142 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
143 ret
= gather_convert_stats_ascii(data
, sz
);
148 const char *get_wt_convert_stats_ascii(const char *path
)
150 const char *ret
= "";
151 struct strbuf sb
= STRBUF_INIT
;
152 if (strbuf_read_file(&sb
, path
, 0) >= 0)
153 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
158 static int text_eol_is_crlf(void)
160 if (auto_crlf
== AUTO_CRLF_TRUE
)
162 else if (auto_crlf
== AUTO_CRLF_INPUT
)
164 if (core_eol
== EOL_CRLF
)
166 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
171 static enum eol
output_eol(enum crlf_action crlf_action
)
173 switch (crlf_action
) {
178 case CRLF_TEXT_INPUT
:
183 case CRLF_AUTO_INPUT
:
188 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
190 warning("Illegal crlf_action %d\n", (int)crlf_action
);
194 static void check_safe_crlf(const char *path
, enum crlf_action crlf_action
,
195 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
196 enum safe_crlf checksafe
)
198 if (old_stats
->crlf
&& !new_stats
->crlf
) {
200 * CRLFs would not be restored by checkout
202 if (checksafe
== SAFE_CRLF_WARN
)
203 warning(_("CRLF will be replaced by LF in %s.\n"
204 "The file will have its original line"
205 " endings in your working directory."), path
);
206 else /* i.e. SAFE_CRLF_FAIL */
207 die(_("CRLF would be replaced by LF in %s."), path
);
208 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
210 * CRLFs would be added by checkout
212 if (checksafe
== SAFE_CRLF_WARN
)
213 warning(_("LF will be replaced by CRLF in %s.\n"
214 "The file will have its original line"
215 " endings in your working directory."), path
);
216 else /* i.e. SAFE_CRLF_FAIL */
217 die(_("LF would be replaced by CRLF in %s"), path
);
221 static int has_cr_in_index(const struct index_state
*istate
, const char *path
)
227 data
= read_blob_data_from_index(istate
, path
, &sz
);
230 has_cr
= memchr(data
, '\r', sz
) != NULL
;
235 static int will_convert_lf_to_crlf(size_t len
, struct text_stat
*stats
,
236 enum crlf_action crlf_action
)
238 if (output_eol(crlf_action
) != EOL_CRLF
)
240 /* No "naked" LF? Nothing to convert, regardless. */
244 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
245 /* If we have any CR or CRLF line endings, we do not touch it */
246 /* This is the new safer autocrlf-handling */
247 if (stats
->lonecr
|| stats
->crlf
)
250 if (convert_is_binary(len
, stats
))
257 static int crlf_to_git(const struct index_state
*istate
,
258 const char *path
, const char *src
, size_t len
,
260 enum crlf_action crlf_action
, enum safe_crlf checksafe
)
262 struct text_stat stats
;
264 int convert_crlf_into_lf
;
266 if (crlf_action
== CRLF_BINARY
||
271 * If we are doing a dry-run and have no source buffer, there is
272 * nothing to analyze; we must assume we would convert.
277 gather_stats(src
, len
, &stats
);
278 /* Optimization: No CRLF? Nothing to convert, regardless. */
279 convert_crlf_into_lf
= !!stats
.crlf
;
281 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
282 if (convert_is_binary(len
, &stats
))
285 * If the file in the index has any CR in it, do not
286 * convert. This is the new safer autocrlf handling,
287 * unless we want to renormalize in a merge or
290 if ((checksafe
!= SAFE_CRLF_RENORMALIZE
) &&
291 has_cr_in_index(istate
, path
))
292 convert_crlf_into_lf
= 0;
294 if ((checksafe
== SAFE_CRLF_WARN
||
295 (checksafe
== SAFE_CRLF_FAIL
)) && len
) {
296 struct text_stat new_stats
;
297 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
298 /* simulate "git add" */
299 if (convert_crlf_into_lf
) {
300 new_stats
.lonelf
+= new_stats
.crlf
;
303 /* simulate "git checkout" */
304 if (will_convert_lf_to_crlf(len
, &new_stats
, crlf_action
)) {
305 new_stats
.crlf
+= new_stats
.lonelf
;
306 new_stats
.lonelf
= 0;
308 check_safe_crlf(path
, crlf_action
, &stats
, &new_stats
, checksafe
);
310 if (!convert_crlf_into_lf
)
314 * At this point all of our source analysis is done, and we are sure we
315 * would convert. If we are in dry-run mode, we can give an answer.
320 /* only grow if not in place */
321 if (strbuf_avail(buf
) + buf
->len
< len
)
322 strbuf_grow(buf
, len
- buf
->len
);
324 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
326 * If we guessed, we already know we rejected a file with
327 * lone CR, and we can strip a CR without looking at what
331 unsigned char c
= *src
++;
337 unsigned char c
= *src
++;
338 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
342 strbuf_setlen(buf
, dst
- buf
->buf
);
346 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
347 struct strbuf
*buf
, enum crlf_action crlf_action
)
349 char *to_free
= NULL
;
350 struct text_stat stats
;
352 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
355 gather_stats(src
, len
, &stats
);
356 if (!will_convert_lf_to_crlf(len
, &stats
, crlf_action
))
359 /* are we "faking" in place editing ? */
361 to_free
= strbuf_detach(buf
, NULL
);
363 strbuf_grow(buf
, len
+ stats
.lonelf
);
365 const char *nl
= memchr(src
, '\n', len
);
368 if (nl
> src
&& nl
[-1] == '\r') {
369 strbuf_add(buf
, src
, nl
+ 1 - src
);
371 strbuf_add(buf
, src
, nl
- src
);
372 strbuf_addstr(buf
, "\r\n");
377 strbuf_add(buf
, src
, len
);
383 struct filter_params
{
391 static int filter_buffer_or_fd(int in
, int out
, void *data
)
394 * Spawn cmd and feed the buffer contents through its stdin.
396 struct child_process child_process
= CHILD_PROCESS_INIT
;
397 struct filter_params
*params
= (struct filter_params
*)data
;
398 int write_err
, status
;
399 const char *argv
[] = { NULL
, NULL
};
401 /* apply % substitution to cmd */
402 struct strbuf cmd
= STRBUF_INIT
;
403 struct strbuf path
= STRBUF_INIT
;
404 struct strbuf_expand_dict_entry dict
[] = {
409 /* quote the path to preserve spaces, etc. */
410 sq_quote_buf(&path
, params
->path
);
411 dict
[0].value
= path
.buf
;
413 /* expand all %f with the quoted path */
414 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
415 strbuf_release(&path
);
419 child_process
.argv
= argv
;
420 child_process
.use_shell
= 1;
421 child_process
.in
= -1;
422 child_process
.out
= out
;
424 if (start_command(&child_process
))
425 return error("cannot fork to run external filter '%s'", params
->cmd
);
427 sigchain_push(SIGPIPE
, SIG_IGN
);
430 write_err
= (write_in_full(child_process
.in
,
431 params
->src
, params
->size
) < 0);
435 write_err
= copy_fd(params
->fd
, child_process
.in
);
436 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
440 if (close(child_process
.in
))
443 error("cannot feed the input to external filter '%s'", params
->cmd
);
445 sigchain_pop(SIGPIPE
);
447 status
= finish_command(&child_process
);
449 error("external filter '%s' failed %d", params
->cmd
, status
);
451 strbuf_release(&cmd
);
452 return (write_err
|| status
);
455 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
456 struct strbuf
*dst
, const char *cmd
)
459 * Create a pipeline to have the command filter the buffer's
462 * (child --> cmd) --> us
465 struct strbuf nbuf
= STRBUF_INIT
;
467 struct filter_params params
;
469 memset(&async
, 0, sizeof(async
));
470 async
.proc
= filter_buffer_or_fd
;
471 async
.data
= ¶ms
;
480 if (start_async(&async
))
481 return 0; /* error was already reported */
483 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
484 err
= error("read from external filter '%s' failed", cmd
);
486 if (close(async
.out
)) {
487 err
= error("read from external filter '%s' failed", cmd
);
489 if (finish_async(&async
)) {
490 err
= error("external filter '%s' failed", cmd
);
494 strbuf_swap(dst
, &nbuf
);
496 strbuf_release(&nbuf
);
500 #define CAP_CLEAN (1u<<0)
501 #define CAP_SMUDGE (1u<<1)
504 struct subprocess_entry subprocess
; /* must be the first member! */
505 unsigned int supported_capabilities
;
508 static int subprocess_map_initialized
;
509 static struct hashmap subprocess_map
;
511 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
514 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
515 struct string_list cap_list
= STRING_LIST_INIT_NODUP
;
517 const char *cap_name
;
518 struct child_process
*process
= &subprocess
->process
;
519 const char *cmd
= subprocess
->cmd
;
521 sigchain_push(SIGPIPE
, SIG_IGN
);
523 err
= packet_writel(process
->in
, "git-filter-client", "version=2", NULL
);
527 err
= strcmp(packet_read_line(process
->out
, NULL
), "git-filter-server");
529 error("external filter '%s' does not support filter protocol version 2", cmd
);
532 err
= strcmp(packet_read_line(process
->out
, NULL
), "version=2");
535 err
= packet_read_line(process
->out
, NULL
) != NULL
;
539 err
= packet_writel(process
->in
, "capability=clean", "capability=smudge", NULL
);
542 cap_buf
= packet_read_line(process
->out
, NULL
);
545 string_list_split_in_place(&cap_list
, cap_buf
, '=', 1);
547 if (cap_list
.nr
!= 2 || strcmp(cap_list
.items
[0].string
, "capability"))
550 cap_name
= cap_list
.items
[1].string
;
551 if (!strcmp(cap_name
, "clean")) {
552 entry
->supported_capabilities
|= CAP_CLEAN
;
553 } else if (!strcmp(cap_name
, "smudge")) {
554 entry
->supported_capabilities
|= CAP_SMUDGE
;
557 "external filter '%s' requested unsupported filter capability '%s'",
562 string_list_clear(&cap_list
, 0);
566 sigchain_pop(SIGPIPE
);
571 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
572 int fd
, struct strbuf
*dst
, const char *cmd
,
573 const unsigned int wanted_capability
)
576 struct cmd2process
*entry
;
577 struct child_process
*process
;
578 struct strbuf nbuf
= STRBUF_INIT
;
579 struct strbuf filter_status
= STRBUF_INIT
;
580 const char *filter_type
;
582 if (!subprocess_map_initialized
) {
583 subprocess_map_initialized
= 1;
584 hashmap_init(&subprocess_map
, (hashmap_cmp_fn
) cmd2process_cmp
, 0);
587 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
593 entry
= xmalloc(sizeof(*entry
));
594 entry
->supported_capabilities
= 0;
596 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
601 process
= &entry
->subprocess
.process
;
603 if (!(wanted_capability
& entry
->supported_capabilities
))
606 if (CAP_CLEAN
& wanted_capability
)
607 filter_type
= "clean";
608 else if (CAP_SMUDGE
& wanted_capability
)
609 filter_type
= "smudge";
611 die("unexpected filter type");
613 sigchain_push(SIGPIPE
, SIG_IGN
);
615 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
616 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
620 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
622 error("path name too long for external filter");
626 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
630 err
= packet_flush_gently(process
->in
);
635 err
= write_packetized_from_fd(fd
, process
->in
);
637 err
= write_packetized_from_buf(src
, len
, process
->in
);
641 err
= subprocess_read_status(process
->out
, &filter_status
);
645 err
= strcmp(filter_status
.buf
, "success");
649 err
= read_packetized_to_strbuf(process
->out
, &nbuf
) < 0;
653 err
= subprocess_read_status(process
->out
, &filter_status
);
657 err
= strcmp(filter_status
.buf
, "success");
660 sigchain_pop(SIGPIPE
);
663 if (!strcmp(filter_status
.buf
, "error")) {
664 /* The filter signaled a problem with the file. */
665 } else if (!strcmp(filter_status
.buf
, "abort")) {
667 * The filter signaled a permanent problem. Don't try to filter
668 * files with the same command for the lifetime of the current
671 entry
->supported_capabilities
&= ~wanted_capability
;
674 * Something went wrong with the protocol filter.
675 * Force shutdown and restart if another blob requires filtering.
677 error("external filter '%s' failed", cmd
);
678 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
682 strbuf_swap(dst
, &nbuf
);
684 strbuf_release(&nbuf
);
688 static struct convert_driver
{
690 struct convert_driver
*next
;
695 } *user_convert
, **user_convert_tail
;
697 static int apply_filter(const char *path
, const char *src
, size_t len
,
698 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
699 const unsigned int wanted_capability
)
701 const char *cmd
= NULL
;
709 if ((CAP_CLEAN
& wanted_capability
) && !drv
->process
&& drv
->clean
)
711 else if ((CAP_SMUDGE
& wanted_capability
) && !drv
->process
&& drv
->smudge
)
715 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
716 else if (drv
->process
&& *drv
->process
)
717 return apply_multi_file_filter(path
, src
, len
, fd
, dst
, drv
->process
, wanted_capability
);
722 static int read_convert_config(const char *var
, const char *value
, void *cb
)
724 const char *key
, *name
;
726 struct convert_driver
*drv
;
729 * External conversion drivers are configured using
730 * "filter.<name>.variable".
732 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
734 for (drv
= user_convert
; drv
; drv
= drv
->next
)
735 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
738 drv
= xcalloc(1, sizeof(struct convert_driver
));
739 drv
->name
= xmemdupz(name
, namelen
);
740 *user_convert_tail
= drv
;
741 user_convert_tail
= &(drv
->next
);
745 * filter.<name>.smudge and filter.<name>.clean specifies
750 * The command-line will not be interpolated in any way.
753 if (!strcmp("smudge", key
))
754 return git_config_string(&drv
->smudge
, var
, value
);
756 if (!strcmp("clean", key
))
757 return git_config_string(&drv
->clean
, var
, value
);
759 if (!strcmp("process", key
))
760 return git_config_string(&drv
->process
, var
, value
);
762 if (!strcmp("required", key
)) {
763 drv
->required
= git_config_bool(var
, value
);
770 static int count_ident(const char *cp
, unsigned long size
)
773 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
785 if (memcmp("Id", cp
, 2))
796 * "$Id: ... "; scan up to the closing dollar sign and discard.
812 static int ident_to_git(const char *path
, const char *src
, size_t len
,
813 struct strbuf
*buf
, int ident
)
817 if (!ident
|| (src
&& !count_ident(src
, len
)))
823 /* only grow if not in place */
824 if (strbuf_avail(buf
) + buf
->len
< len
)
825 strbuf_grow(buf
, len
- buf
->len
);
828 dollar
= memchr(src
, '$', len
);
831 memmove(dst
, src
, dollar
+ 1 - src
);
832 dst
+= dollar
+ 1 - src
;
833 len
-= dollar
+ 1 - src
;
836 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
837 dollar
= memchr(src
+ 3, '$', len
- 3);
840 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
841 /* Line break before the next dollar. */
845 memcpy(dst
, "Id$", 3);
847 len
-= dollar
+ 1 - src
;
851 memmove(dst
, src
, len
);
852 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
856 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
857 struct strbuf
*buf
, int ident
)
859 unsigned char sha1
[20];
860 char *to_free
= NULL
, *dollar
, *spc
;
866 cnt
= count_ident(src
, len
);
870 /* are we "faking" in place editing ? */
872 to_free
= strbuf_detach(buf
, NULL
);
873 hash_sha1_file(src
, len
, "blob", sha1
);
875 strbuf_grow(buf
, len
+ cnt
* 43);
877 /* step 1: run to the next '$' */
878 dollar
= memchr(src
, '$', len
);
881 strbuf_add(buf
, src
, dollar
+ 1 - src
);
882 len
-= dollar
+ 1 - src
;
885 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
886 if (len
< 3 || memcmp("Id", src
, 2))
889 /* step 3: skip over Id$ or Id:xxxxx$ */
893 } else if (src
[2] == ':') {
895 * It's possible that an expanded Id has crept its way into the
896 * repository, we cope with that by stripping the expansion out.
897 * This is probably not a good idea, since it will cause changes
898 * on checkout, which won't go away by stash, but let's keep it
901 dollar
= memchr(src
+ 3, '$', len
- 3);
903 /* incomplete keyword, no more '$', so just quit the loop */
907 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
908 /* Line break before the next dollar. */
912 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
913 if (spc
&& spc
< dollar
-1) {
914 /* There are spaces in unexpected places.
915 * This is probably an id from some other
916 * versioning system. Keep it for now.
921 len
-= dollar
+ 1 - src
;
924 /* it wasn't a "Id$" or "Id:xxxx$" */
928 /* step 4: substitute */
929 strbuf_addstr(buf
, "Id: ");
930 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
931 strbuf_addstr(buf
, " $");
933 strbuf_add(buf
, src
, len
);
939 static enum crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
941 const char *value
= check
->value
;
943 if (ATTR_TRUE(value
))
945 else if (ATTR_FALSE(value
))
947 else if (ATTR_UNSET(value
))
949 else if (!strcmp(value
, "input"))
950 return CRLF_TEXT_INPUT
;
951 else if (!strcmp(value
, "auto"))
953 return CRLF_UNDEFINED
;
956 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
958 const char *value
= check
->value
;
960 if (ATTR_UNSET(value
))
962 else if (!strcmp(value
, "lf"))
964 else if (!strcmp(value
, "crlf"))
969 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
971 const char *value
= check
->value
;
972 struct convert_driver
*drv
;
974 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
976 for (drv
= user_convert
; drv
; drv
= drv
->next
)
977 if (!strcmp(value
, drv
->name
))
982 static int git_path_check_ident(struct attr_check_item
*check
)
984 const char *value
= check
->value
;
986 return !!ATTR_TRUE(value
);
990 struct convert_driver
*drv
;
991 enum crlf_action attr_action
; /* What attr says */
992 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
996 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
998 static struct attr_check
*check
;
1001 check
= attr_check_initl("crlf", "ident", "filter",
1002 "eol", "text", NULL
);
1003 user_convert_tail
= &user_convert
;
1004 git_config(read_convert_config
, NULL
);
1007 if (!git_check_attr(path
, check
)) {
1008 struct attr_check_item
*ccheck
= check
->items
;
1009 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1010 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1011 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1012 ca
->attr_action
= ca
->crlf_action
;
1013 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1014 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1015 if (ca
->crlf_action
!= CRLF_BINARY
) {
1016 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1017 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1018 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1019 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1020 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1021 else if (eol_attr
== EOL_LF
)
1022 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1023 else if (eol_attr
== EOL_CRLF
)
1024 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1026 ca
->attr_action
= ca
->crlf_action
;
1029 ca
->crlf_action
= CRLF_UNDEFINED
;
1032 if (ca
->crlf_action
== CRLF_TEXT
)
1033 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1034 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1035 ca
->crlf_action
= CRLF_BINARY
;
1036 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1037 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1038 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1039 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1042 int would_convert_to_git_filter_fd(const char *path
)
1044 struct conv_attrs ca
;
1046 convert_attrs(&ca
, path
);
1051 * Apply a filter to an fd only if the filter is required to succeed.
1052 * We must die if the filter fails, because the original data before
1053 * filtering is not available.
1055 if (!ca
.drv
->required
)
1058 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
);
1061 const char *get_convert_attr_ascii(const char *path
)
1063 struct conv_attrs ca
;
1065 convert_attrs(&ca
, path
);
1066 switch (ca
.attr_action
) {
1067 case CRLF_UNDEFINED
:
1073 case CRLF_TEXT_INPUT
:
1074 return "text eol=lf";
1075 case CRLF_TEXT_CRLF
:
1076 return "text eol=crlf";
1079 case CRLF_AUTO_CRLF
:
1080 return "text=auto eol=crlf";
1081 case CRLF_AUTO_INPUT
:
1082 return "text=auto eol=lf";
1087 int convert_to_git(const char *path
, const char *src
, size_t len
,
1088 struct strbuf
*dst
, enum safe_crlf checksafe
)
1091 struct conv_attrs ca
;
1093 convert_attrs(&ca
, path
);
1095 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
);
1096 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1097 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1103 ret
|= crlf_to_git(&the_index
, path
, src
, len
, dst
, ca
.crlf_action
, checksafe
);
1108 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
1111 void convert_to_git_filter_fd(const struct index_state
*istate
,
1112 const char *path
, int fd
, struct strbuf
*dst
,
1113 enum safe_crlf checksafe
)
1115 struct conv_attrs ca
;
1116 convert_attrs(&ca
, path
);
1119 assert(ca
.drv
->clean
|| ca
.drv
->process
);
1121 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
))
1122 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1124 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, checksafe
);
1125 ident_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.ident
);
1128 static int convert_to_working_tree_internal(const char *path
, const char *src
,
1129 size_t len
, struct strbuf
*dst
,
1132 int ret
= 0, ret_filter
= 0;
1133 struct conv_attrs ca
;
1135 convert_attrs(&ca
, path
);
1137 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
1143 * CRLF conversion can be skipped if normalizing, unless there
1144 * is a smudge or process filter (even if the process filter doesn't
1145 * support smudge). The filters might expect CRLFs.
1147 if ((ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->process
)) || !normalizing
) {
1148 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
1155 ret_filter
= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_SMUDGE
);
1156 if (!ret_filter
&& ca
.drv
&& ca
.drv
->required
)
1157 die("%s: smudge filter %s failed", path
, ca
.drv
->name
);
1159 return ret
| ret_filter
;
1162 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1164 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0);
1167 int renormalize_buffer(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1169 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1);
1174 return ret
| convert_to_git(path
, src
, len
, dst
, SAFE_CRLF_RENORMALIZE
);
1177 /*****************************************************************
1179 * Streaming conversion support
1181 *****************************************************************/
1183 typedef int (*filter_fn
)(struct stream_filter
*,
1184 const char *input
, size_t *isize_p
,
1185 char *output
, size_t *osize_p
);
1186 typedef void (*free_fn
)(struct stream_filter
*);
1188 struct stream_filter_vtbl
{
1193 struct stream_filter
{
1194 struct stream_filter_vtbl
*vtbl
;
1197 static int null_filter_fn(struct stream_filter
*filter
,
1198 const char *input
, size_t *isize_p
,
1199 char *output
, size_t *osize_p
)
1204 return 0; /* we do not keep any states */
1206 if (*osize_p
< count
)
1209 memmove(output
, input
, count
);
1216 static void null_free_fn(struct stream_filter
*filter
)
1218 ; /* nothing -- null instances are shared */
1221 static struct stream_filter_vtbl null_vtbl
= {
1226 static struct stream_filter null_filter_singleton
= {
1230 int is_null_stream_filter(struct stream_filter
*filter
)
1232 return filter
== &null_filter_singleton
;
1240 struct lf_to_crlf_filter
{
1241 struct stream_filter filter
;
1242 unsigned has_held
:1;
1246 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1247 const char *input
, size_t *isize_p
,
1248 char *output
, size_t *osize_p
)
1250 size_t count
, o
= 0;
1251 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1254 * We may be holding onto the CR to see if it is followed by a
1255 * LF, in which case we would need to go to the main loop.
1256 * Otherwise, just emit it to the output stream.
1258 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1259 output
[o
++] = lf_to_crlf
->held
;
1260 lf_to_crlf
->has_held
= 0;
1263 /* We are told to drain */
1270 if (count
|| lf_to_crlf
->has_held
) {
1274 if (lf_to_crlf
->has_held
) {
1276 lf_to_crlf
->has_held
= 0;
1279 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1284 } else if (was_cr
) {
1286 * Previous round saw CR and it is not followed
1287 * by a LF; emit the CR before processing the
1288 * current character.
1294 * We may have consumed the last output slot,
1295 * in which case we need to break out of this
1296 * loop; hold the current character before
1299 if (*osize_p
<= o
) {
1300 lf_to_crlf
->has_held
= 1;
1301 lf_to_crlf
->held
= ch
;
1302 continue; /* break but increment i */
1317 if (!lf_to_crlf
->has_held
&& was_cr
) {
1318 lf_to_crlf
->has_held
= 1;
1319 lf_to_crlf
->held
= '\r';
1325 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1330 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1331 lf_to_crlf_filter_fn
,
1335 static struct stream_filter
*lf_to_crlf_filter(void)
1337 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1339 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1340 return (struct stream_filter
*)lf_to_crlf
;
1346 #define FILTER_BUFFER 1024
1347 struct cascade_filter
{
1348 struct stream_filter filter
;
1349 struct stream_filter
*one
;
1350 struct stream_filter
*two
;
1351 char buf
[FILTER_BUFFER
];
1355 static int cascade_filter_fn(struct stream_filter
*filter
,
1356 const char *input
, size_t *isize_p
,
1357 char *output
, size_t *osize_p
)
1359 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1361 size_t sz
= *osize_p
;
1362 size_t to_feed
, remaining
;
1365 * input -- (one) --> buf -- (two) --> output
1367 while (filled
< sz
) {
1368 remaining
= sz
- filled
;
1370 /* do we already have something to feed two with? */
1371 if (cas
->ptr
< cas
->end
) {
1372 to_feed
= cas
->end
- cas
->ptr
;
1373 if (stream_filter(cas
->two
,
1374 cas
->buf
+ cas
->ptr
, &to_feed
,
1375 output
+ filled
, &remaining
))
1377 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1378 filled
= sz
- remaining
;
1382 /* feed one from upstream and have it emit into our buffer */
1383 to_feed
= input
? *isize_p
: 0;
1384 if (input
&& !to_feed
)
1386 remaining
= sizeof(cas
->buf
);
1387 if (stream_filter(cas
->one
,
1389 cas
->buf
, &remaining
))
1391 cas
->end
= sizeof(cas
->buf
) - remaining
;
1394 size_t fed
= *isize_p
- to_feed
;
1399 /* do we know that we drained one completely? */
1400 if (input
|| cas
->end
)
1403 /* tell two to drain; we have nothing more to give it */
1405 remaining
= sz
- filled
;
1406 if (stream_filter(cas
->two
,
1408 output
+ filled
, &remaining
))
1410 if (remaining
== (sz
- filled
))
1411 break; /* completely drained two */
1412 filled
= sz
- remaining
;
1418 static void cascade_free_fn(struct stream_filter
*filter
)
1420 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1421 free_stream_filter(cas
->one
);
1422 free_stream_filter(cas
->two
);
1426 static struct stream_filter_vtbl cascade_vtbl
= {
1431 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1432 struct stream_filter
*two
)
1434 struct cascade_filter
*cascade
;
1436 if (!one
|| is_null_stream_filter(one
))
1438 if (!two
|| is_null_stream_filter(two
))
1441 cascade
= xmalloc(sizeof(*cascade
));
1444 cascade
->end
= cascade
->ptr
= 0;
1445 cascade
->filter
.vtbl
= &cascade_vtbl
;
1446 return (struct stream_filter
*)cascade
;
1452 #define IDENT_DRAINING (-1)
1453 #define IDENT_SKIPPING (-2)
1454 struct ident_filter
{
1455 struct stream_filter filter
;
1458 char ident
[45]; /* ": x40 $" */
1461 static int is_foreign_ident(const char *str
)
1465 if (!skip_prefix(str
, "$Id: ", &str
))
1467 for (i
= 0; str
[i
]; i
++) {
1468 if (isspace(str
[i
]) && str
[i
+1] != '$')
1474 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1476 size_t to_drain
= ident
->left
.len
;
1478 if (*osize_p
< to_drain
)
1479 to_drain
= *osize_p
;
1481 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1482 strbuf_remove(&ident
->left
, 0, to_drain
);
1483 *output_p
+= to_drain
;
1484 *osize_p
-= to_drain
;
1486 if (!ident
->left
.len
)
1490 static int ident_filter_fn(struct stream_filter
*filter
,
1491 const char *input
, size_t *isize_p
,
1492 char *output
, size_t *osize_p
)
1494 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1495 static const char head
[] = "$Id";
1498 /* drain upon eof */
1499 switch (ident
->state
) {
1501 strbuf_add(&ident
->left
, head
, ident
->state
);
1502 case IDENT_SKIPPING
:
1504 case IDENT_DRAINING
:
1505 ident_drain(ident
, &output
, osize_p
);
1510 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1513 if (ident
->state
== IDENT_DRAINING
) {
1514 ident_drain(ident
, &output
, osize_p
);
1523 if (ident
->state
== IDENT_SKIPPING
) {
1525 * Skipping until '$' or LF, but keeping them
1526 * in case it is a foreign ident.
1528 strbuf_addch(&ident
->left
, ch
);
1529 if (ch
!= '\n' && ch
!= '$')
1531 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1532 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1533 strbuf_addstr(&ident
->left
, ident
->ident
);
1535 ident
->state
= IDENT_DRAINING
;
1539 if (ident
->state
< sizeof(head
) &&
1540 head
[ident
->state
] == ch
) {
1546 strbuf_add(&ident
->left
, head
, ident
->state
);
1547 if (ident
->state
== sizeof(head
) - 1) {
1548 if (ch
!= ':' && ch
!= '$') {
1549 strbuf_addch(&ident
->left
, ch
);
1555 strbuf_addch(&ident
->left
, ch
);
1556 ident
->state
= IDENT_SKIPPING
;
1558 strbuf_addstr(&ident
->left
, ident
->ident
);
1559 ident
->state
= IDENT_DRAINING
;
1564 strbuf_addch(&ident
->left
, ch
);
1565 ident
->state
= IDENT_DRAINING
;
1570 static void ident_free_fn(struct stream_filter
*filter
)
1572 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1573 strbuf_release(&ident
->left
);
1577 static struct stream_filter_vtbl ident_vtbl
= {
1582 static struct stream_filter
*ident_filter(const unsigned char *sha1
)
1584 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1586 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1587 ": %s $", sha1_to_hex(sha1
));
1588 strbuf_init(&ident
->left
, 0);
1589 ident
->filter
.vtbl
= &ident_vtbl
;
1591 return (struct stream_filter
*)ident
;
1595 * Return an appropriately constructed filter for the path, or NULL if
1596 * the contents cannot be filtered without reading the whole thing
1599 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1600 * large binary blob you would want us not to slurp into the memory!
1602 struct stream_filter
*get_stream_filter(const char *path
, const unsigned char *sha1
)
1604 struct conv_attrs ca
;
1605 struct stream_filter
*filter
= NULL
;
1607 convert_attrs(&ca
, path
);
1608 if (ca
.drv
&& (ca
.drv
->process
|| ca
.drv
->smudge
|| ca
.drv
->clean
))
1611 if (ca
.crlf_action
== CRLF_AUTO
|| ca
.crlf_action
== CRLF_AUTO_CRLF
)
1615 filter
= ident_filter(sha1
);
1617 if (output_eol(ca
.crlf_action
) == EOL_CRLF
)
1618 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1620 filter
= cascade_filter(filter
, &null_filter_singleton
);
1625 void free_stream_filter(struct stream_filter
*filter
)
1627 filter
->vtbl
->free(filter
);
1630 int stream_filter(struct stream_filter
*filter
,
1631 const char *input
, size_t *isize_p
,
1632 char *output
, size_t *osize_p
)
1634 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);