1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
5 #include "run-command.h"
9 #include "sub-process.h"
12 * convert.c - convert a file when checking it out and checking it in.
14 * This should use the pathname to decide on whether it wants to do some
15 * more interesting conversions (automatic gzip/unzip, general format
16 * conversions etc etc), but by default it just does automatic CRLF<->LF
17 * translation when the "text" attribute or "auto_crlf" option is set.
20 /* Stat bits: When BIN is set, the txt bits are unset */
21 #define CONVERT_STAT_BITS_TXT_LF 0x1
22 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
23 #define CONVERT_STAT_BITS_BIN 0x4
37 /* NUL, CR, LF and CRLF counts */
38 unsigned nul
, lonecr
, lonelf
, crlf
;
40 /* These are just approximations! */
41 unsigned printable
, nonprintable
;
44 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
48 memset(stats
, 0, sizeof(*stats
));
50 for (i
= 0; i
< size
; i
++) {
51 unsigned char c
= buf
[i
];
53 if (i
+1 < size
&& buf
[i
+1] == '\n') {
66 stats
->nonprintable
++;
69 /* BS, HT, ESC and FF */
70 case '\b': case '\t': case '\033': case '\014':
77 stats
->nonprintable
++;
84 /* If file ends with EOF then don't count this EOF as non-printable. */
85 if (size
>= 1 && buf
[size
-1] == '\032')
86 stats
->nonprintable
--;
90 * The same heuristics as diff.c::mmfile_is_binary()
91 * We treat files with bare CR as binary
93 static int convert_is_binary(unsigned long size
, const struct text_stat
*stats
)
99 if ((stats
->printable
>> 7) < stats
->nonprintable
)
104 static unsigned int gather_convert_stats(const char *data
, unsigned long size
)
106 struct text_stat stats
;
110 gather_stats(data
, size
, &stats
);
111 if (convert_is_binary(size
, &stats
))
112 ret
|= CONVERT_STAT_BITS_BIN
;
114 ret
|= CONVERT_STAT_BITS_TXT_CRLF
;
116 ret
|= CONVERT_STAT_BITS_TXT_LF
;
121 static const char *gather_convert_stats_ascii(const char *data
, unsigned long size
)
123 unsigned int convert_stats
= gather_convert_stats(data
, size
);
125 if (convert_stats
& CONVERT_STAT_BITS_BIN
)
127 switch (convert_stats
) {
128 case CONVERT_STAT_BITS_TXT_LF
:
130 case CONVERT_STAT_BITS_TXT_CRLF
:
132 case CONVERT_STAT_BITS_TXT_LF
| CONVERT_STAT_BITS_TXT_CRLF
:
139 const char *get_cached_convert_stats_ascii(const struct index_state
*istate
,
144 void *data
= read_blob_data_from_index(istate
, path
, &sz
);
145 ret
= gather_convert_stats_ascii(data
, sz
);
150 const char *get_wt_convert_stats_ascii(const char *path
)
152 const char *ret
= "";
153 struct strbuf sb
= STRBUF_INIT
;
154 if (strbuf_read_file(&sb
, path
, 0) >= 0)
155 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
160 static int text_eol_is_crlf(void)
162 if (auto_crlf
== AUTO_CRLF_TRUE
)
164 else if (auto_crlf
== AUTO_CRLF_INPUT
)
166 if (core_eol
== EOL_CRLF
)
168 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
173 static enum eol
output_eol(enum crlf_action crlf_action
)
175 switch (crlf_action
) {
180 case CRLF_TEXT_INPUT
:
185 case CRLF_AUTO_INPUT
:
190 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
192 warning("Illegal crlf_action %d\n", (int)crlf_action
);
196 static void check_safe_crlf(const char *path
, enum crlf_action crlf_action
,
197 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
198 enum safe_crlf checksafe
)
200 if (old_stats
->crlf
&& !new_stats
->crlf
) {
202 * CRLFs would not be restored by checkout
204 if (checksafe
== SAFE_CRLF_WARN
)
205 warning(_("CRLF will be replaced by LF in %s.\n"
206 "The file will have its original line"
207 " endings in your working directory."), path
);
208 else /* i.e. SAFE_CRLF_FAIL */
209 die(_("CRLF would be replaced by LF in %s."), path
);
210 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
212 * CRLFs would be added by checkout
214 if (checksafe
== SAFE_CRLF_WARN
)
215 warning(_("LF will be replaced by CRLF in %s.\n"
216 "The file will have its original line"
217 " endings in your working directory."), path
);
218 else /* i.e. SAFE_CRLF_FAIL */
219 die(_("LF would be replaced by CRLF in %s"), path
);
223 static int has_crlf_in_index(const struct index_state
*istate
, const char *path
)
230 data
= read_blob_data_from_index(istate
, path
, &sz
);
234 crp
= memchr(data
, '\r', sz
);
236 unsigned int ret_stats
;
237 ret_stats
= gather_convert_stats(data
, sz
);
238 if (!(ret_stats
& CONVERT_STAT_BITS_BIN
) &&
239 (ret_stats
& CONVERT_STAT_BITS_TXT_CRLF
))
246 static int will_convert_lf_to_crlf(size_t len
, struct text_stat
*stats
,
247 enum crlf_action crlf_action
)
249 if (output_eol(crlf_action
) != EOL_CRLF
)
251 /* No "naked" LF? Nothing to convert, regardless. */
255 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
256 /* If we have any CR or CRLF line endings, we do not touch it */
257 /* This is the new safer autocrlf-handling */
258 if (stats
->lonecr
|| stats
->crlf
)
261 if (convert_is_binary(len
, stats
))
268 static int crlf_to_git(const struct index_state
*istate
,
269 const char *path
, const char *src
, size_t len
,
271 enum crlf_action crlf_action
, enum safe_crlf checksafe
)
273 struct text_stat stats
;
275 int convert_crlf_into_lf
;
277 if (crlf_action
== CRLF_BINARY
||
282 * If we are doing a dry-run and have no source buffer, there is
283 * nothing to analyze; we must assume we would convert.
288 gather_stats(src
, len
, &stats
);
289 /* Optimization: No CRLF? Nothing to convert, regardless. */
290 convert_crlf_into_lf
= !!stats
.crlf
;
292 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
293 if (convert_is_binary(len
, &stats
))
296 * If the file in the index has any CR in it, do not
297 * convert. This is the new safer autocrlf handling,
298 * unless we want to renormalize in a merge or
301 if ((checksafe
!= SAFE_CRLF_RENORMALIZE
) &&
302 has_crlf_in_index(istate
, path
))
303 convert_crlf_into_lf
= 0;
305 if ((checksafe
== SAFE_CRLF_WARN
||
306 (checksafe
== SAFE_CRLF_FAIL
)) && len
) {
307 struct text_stat new_stats
;
308 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
309 /* simulate "git add" */
310 if (convert_crlf_into_lf
) {
311 new_stats
.lonelf
+= new_stats
.crlf
;
314 /* simulate "git checkout" */
315 if (will_convert_lf_to_crlf(len
, &new_stats
, crlf_action
)) {
316 new_stats
.crlf
+= new_stats
.lonelf
;
317 new_stats
.lonelf
= 0;
319 check_safe_crlf(path
, crlf_action
, &stats
, &new_stats
, checksafe
);
321 if (!convert_crlf_into_lf
)
325 * At this point all of our source analysis is done, and we are sure we
326 * would convert. If we are in dry-run mode, we can give an answer.
331 /* only grow if not in place */
332 if (strbuf_avail(buf
) + buf
->len
< len
)
333 strbuf_grow(buf
, len
- buf
->len
);
335 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
337 * If we guessed, we already know we rejected a file with
338 * lone CR, and we can strip a CR without looking at what
342 unsigned char c
= *src
++;
348 unsigned char c
= *src
++;
349 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
353 strbuf_setlen(buf
, dst
- buf
->buf
);
357 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
358 struct strbuf
*buf
, enum crlf_action crlf_action
)
360 char *to_free
= NULL
;
361 struct text_stat stats
;
363 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
366 gather_stats(src
, len
, &stats
);
367 if (!will_convert_lf_to_crlf(len
, &stats
, crlf_action
))
370 /* are we "faking" in place editing ? */
372 to_free
= strbuf_detach(buf
, NULL
);
374 strbuf_grow(buf
, len
+ stats
.lonelf
);
376 const char *nl
= memchr(src
, '\n', len
);
379 if (nl
> src
&& nl
[-1] == '\r') {
380 strbuf_add(buf
, src
, nl
+ 1 - src
);
382 strbuf_add(buf
, src
, nl
- src
);
383 strbuf_addstr(buf
, "\r\n");
388 strbuf_add(buf
, src
, len
);
394 struct filter_params
{
402 static int filter_buffer_or_fd(int in
, int out
, void *data
)
405 * Spawn cmd and feed the buffer contents through its stdin.
407 struct child_process child_process
= CHILD_PROCESS_INIT
;
408 struct filter_params
*params
= (struct filter_params
*)data
;
409 int write_err
, status
;
410 const char *argv
[] = { NULL
, NULL
};
412 /* apply % substitution to cmd */
413 struct strbuf cmd
= STRBUF_INIT
;
414 struct strbuf path
= STRBUF_INIT
;
415 struct strbuf_expand_dict_entry dict
[] = {
420 /* quote the path to preserve spaces, etc. */
421 sq_quote_buf(&path
, params
->path
);
422 dict
[0].value
= path
.buf
;
424 /* expand all %f with the quoted path */
425 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
426 strbuf_release(&path
);
430 child_process
.argv
= argv
;
431 child_process
.use_shell
= 1;
432 child_process
.in
= -1;
433 child_process
.out
= out
;
435 if (start_command(&child_process
)) {
436 strbuf_release(&cmd
);
437 return error("cannot fork to run external filter '%s'", params
->cmd
);
440 sigchain_push(SIGPIPE
, SIG_IGN
);
443 write_err
= (write_in_full(child_process
.in
,
444 params
->src
, params
->size
) < 0);
448 write_err
= copy_fd(params
->fd
, child_process
.in
);
449 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
453 if (close(child_process
.in
))
456 error("cannot feed the input to external filter '%s'", params
->cmd
);
458 sigchain_pop(SIGPIPE
);
460 status
= finish_command(&child_process
);
462 error("external filter '%s' failed %d", params
->cmd
, status
);
464 strbuf_release(&cmd
);
465 return (write_err
|| status
);
468 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
469 struct strbuf
*dst
, const char *cmd
)
472 * Create a pipeline to have the command filter the buffer's
475 * (child --> cmd) --> us
478 struct strbuf nbuf
= STRBUF_INIT
;
480 struct filter_params params
;
482 memset(&async
, 0, sizeof(async
));
483 async
.proc
= filter_buffer_or_fd
;
484 async
.data
= ¶ms
;
493 if (start_async(&async
))
494 return 0; /* error was already reported */
496 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
497 err
= error("read from external filter '%s' failed", cmd
);
499 if (close(async
.out
)) {
500 err
= error("read from external filter '%s' failed", cmd
);
502 if (finish_async(&async
)) {
503 err
= error("external filter '%s' failed", cmd
);
507 strbuf_swap(dst
, &nbuf
);
509 strbuf_release(&nbuf
);
513 #define CAP_CLEAN (1u<<0)
514 #define CAP_SMUDGE (1u<<1)
515 #define CAP_DELAY (1u<<2)
518 struct subprocess_entry subprocess
; /* must be the first member! */
519 unsigned int supported_capabilities
;
522 static int subprocess_map_initialized
;
523 static struct hashmap subprocess_map
;
525 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
527 static int versions
[] = {2, 0};
528 static struct subprocess_capability capabilities
[] = {
529 { "clean", CAP_CLEAN
},
530 { "smudge", CAP_SMUDGE
},
531 { "delay", CAP_DELAY
},
534 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
535 return subprocess_handshake(subprocess
, "git-filter", versions
, NULL
,
537 &entry
->supported_capabilities
);
540 static void handle_filter_error(const struct strbuf
*filter_status
,
541 struct cmd2process
*entry
,
542 const unsigned int wanted_capability
) {
543 if (!strcmp(filter_status
->buf
, "error"))
544 ; /* The filter signaled a problem with the file. */
545 else if (!strcmp(filter_status
->buf
, "abort") && wanted_capability
) {
547 * The filter signaled a permanent problem. Don't try to filter
548 * files with the same command for the lifetime of the current
551 entry
->supported_capabilities
&= ~wanted_capability
;
554 * Something went wrong with the protocol filter.
555 * Force shutdown and restart if another blob requires filtering.
557 error("external filter '%s' failed", entry
->subprocess
.cmd
);
558 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
563 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
564 int fd
, struct strbuf
*dst
, const char *cmd
,
565 const unsigned int wanted_capability
,
566 struct delayed_checkout
*dco
)
570 struct cmd2process
*entry
;
571 struct child_process
*process
;
572 struct strbuf nbuf
= STRBUF_INIT
;
573 struct strbuf filter_status
= STRBUF_INIT
;
574 const char *filter_type
;
576 if (!subprocess_map_initialized
) {
577 subprocess_map_initialized
= 1;
578 hashmap_init(&subprocess_map
, cmd2process_cmp
, NULL
, 0);
581 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
587 entry
= xmalloc(sizeof(*entry
));
588 entry
->supported_capabilities
= 0;
590 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
595 process
= &entry
->subprocess
.process
;
597 if (!(entry
->supported_capabilities
& wanted_capability
))
600 if (wanted_capability
& CAP_CLEAN
)
601 filter_type
= "clean";
602 else if (wanted_capability
& CAP_SMUDGE
)
603 filter_type
= "smudge";
605 die("unexpected filter type");
607 sigchain_push(SIGPIPE
, SIG_IGN
);
609 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
610 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
614 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
616 error("path name too long for external filter");
620 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
624 if ((entry
->supported_capabilities
& CAP_DELAY
) &&
625 dco
&& dco
->state
== CE_CAN_DELAY
) {
627 err
= packet_write_fmt_gently(process
->in
, "can-delay=1\n");
632 err
= packet_flush_gently(process
->in
);
637 err
= write_packetized_from_fd(fd
, process
->in
);
639 err
= write_packetized_from_buf(src
, len
, process
->in
);
643 err
= subprocess_read_status(process
->out
, &filter_status
);
647 if (can_delay
&& !strcmp(filter_status
.buf
, "delayed")) {
648 string_list_insert(&dco
->filters
, cmd
);
649 string_list_insert(&dco
->paths
, path
);
651 /* The filter got the blob and wants to send us a response. */
652 err
= strcmp(filter_status
.buf
, "success");
656 err
= read_packetized_to_strbuf(process
->out
, &nbuf
) < 0;
660 err
= subprocess_read_status(process
->out
, &filter_status
);
664 err
= strcmp(filter_status
.buf
, "success");
668 sigchain_pop(SIGPIPE
);
671 handle_filter_error(&filter_status
, entry
, wanted_capability
);
673 strbuf_swap(dst
, &nbuf
);
674 strbuf_release(&nbuf
);
679 int async_query_available_blobs(const char *cmd
, struct string_list
*available_paths
)
683 struct cmd2process
*entry
;
684 struct child_process
*process
;
685 struct strbuf filter_status
= STRBUF_INIT
;
687 assert(subprocess_map_initialized
);
688 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
690 error("external filter '%s' is not available anymore although "
691 "not all paths have been filtered", cmd
);
694 process
= &entry
->subprocess
.process
;
695 sigchain_push(SIGPIPE
, SIG_IGN
);
697 err
= packet_write_fmt_gently(
698 process
->in
, "command=list_available_blobs\n");
702 err
= packet_flush_gently(process
->in
);
706 while ((line
= packet_read_line(process
->out
, NULL
))) {
708 if (skip_prefix(line
, "pathname=", &path
))
709 string_list_insert(available_paths
, xstrdup(path
));
711 ; /* ignore unknown keys */
714 err
= subprocess_read_status(process
->out
, &filter_status
);
718 err
= strcmp(filter_status
.buf
, "success");
721 sigchain_pop(SIGPIPE
);
724 handle_filter_error(&filter_status
, entry
, 0);
728 static struct convert_driver
{
730 struct convert_driver
*next
;
735 } *user_convert
, **user_convert_tail
;
737 static int apply_filter(const char *path
, const char *src
, size_t len
,
738 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
739 const unsigned int wanted_capability
,
740 struct delayed_checkout
*dco
)
742 const char *cmd
= NULL
;
750 if ((wanted_capability
& CAP_CLEAN
) && !drv
->process
&& drv
->clean
)
752 else if ((wanted_capability
& CAP_SMUDGE
) && !drv
->process
&& drv
->smudge
)
756 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
757 else if (drv
->process
&& *drv
->process
)
758 return apply_multi_file_filter(path
, src
, len
, fd
, dst
,
759 drv
->process
, wanted_capability
, dco
);
764 static int read_convert_config(const char *var
, const char *value
, void *cb
)
766 const char *key
, *name
;
768 struct convert_driver
*drv
;
771 * External conversion drivers are configured using
772 * "filter.<name>.variable".
774 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
776 for (drv
= user_convert
; drv
; drv
= drv
->next
)
777 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
780 drv
= xcalloc(1, sizeof(struct convert_driver
));
781 drv
->name
= xmemdupz(name
, namelen
);
782 *user_convert_tail
= drv
;
783 user_convert_tail
= &(drv
->next
);
787 * filter.<name>.smudge and filter.<name>.clean specifies
792 * The command-line will not be interpolated in any way.
795 if (!strcmp("smudge", key
))
796 return git_config_string(&drv
->smudge
, var
, value
);
798 if (!strcmp("clean", key
))
799 return git_config_string(&drv
->clean
, var
, value
);
801 if (!strcmp("process", key
))
802 return git_config_string(&drv
->process
, var
, value
);
804 if (!strcmp("required", key
)) {
805 drv
->required
= git_config_bool(var
, value
);
812 static int count_ident(const char *cp
, unsigned long size
)
815 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
827 if (memcmp("Id", cp
, 2))
838 * "$Id: ... "; scan up to the closing dollar sign and discard.
854 static int ident_to_git(const char *path
, const char *src
, size_t len
,
855 struct strbuf
*buf
, int ident
)
859 if (!ident
|| (src
&& !count_ident(src
, len
)))
865 /* only grow if not in place */
866 if (strbuf_avail(buf
) + buf
->len
< len
)
867 strbuf_grow(buf
, len
- buf
->len
);
870 dollar
= memchr(src
, '$', len
);
873 memmove(dst
, src
, dollar
+ 1 - src
);
874 dst
+= dollar
+ 1 - src
;
875 len
-= dollar
+ 1 - src
;
878 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
879 dollar
= memchr(src
+ 3, '$', len
- 3);
882 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
883 /* Line break before the next dollar. */
887 memcpy(dst
, "Id$", 3);
889 len
-= dollar
+ 1 - src
;
893 memmove(dst
, src
, len
);
894 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
898 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
899 struct strbuf
*buf
, int ident
)
901 unsigned char sha1
[20];
902 char *to_free
= NULL
, *dollar
, *spc
;
908 cnt
= count_ident(src
, len
);
912 /* are we "faking" in place editing ? */
914 to_free
= strbuf_detach(buf
, NULL
);
915 hash_sha1_file(src
, len
, "blob", sha1
);
917 strbuf_grow(buf
, len
+ cnt
* 43);
919 /* step 1: run to the next '$' */
920 dollar
= memchr(src
, '$', len
);
923 strbuf_add(buf
, src
, dollar
+ 1 - src
);
924 len
-= dollar
+ 1 - src
;
927 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
928 if (len
< 3 || memcmp("Id", src
, 2))
931 /* step 3: skip over Id$ or Id:xxxxx$ */
935 } else if (src
[2] == ':') {
937 * It's possible that an expanded Id has crept its way into the
938 * repository, we cope with that by stripping the expansion out.
939 * This is probably not a good idea, since it will cause changes
940 * on checkout, which won't go away by stash, but let's keep it
943 dollar
= memchr(src
+ 3, '$', len
- 3);
945 /* incomplete keyword, no more '$', so just quit the loop */
949 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
950 /* Line break before the next dollar. */
954 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
955 if (spc
&& spc
< dollar
-1) {
956 /* There are spaces in unexpected places.
957 * This is probably an id from some other
958 * versioning system. Keep it for now.
963 len
-= dollar
+ 1 - src
;
966 /* it wasn't a "Id$" or "Id:xxxx$" */
970 /* step 4: substitute */
971 strbuf_addstr(buf
, "Id: ");
972 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
973 strbuf_addstr(buf
, " $");
975 strbuf_add(buf
, src
, len
);
981 static enum crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
983 const char *value
= check
->value
;
985 if (ATTR_TRUE(value
))
987 else if (ATTR_FALSE(value
))
989 else if (ATTR_UNSET(value
))
991 else if (!strcmp(value
, "input"))
992 return CRLF_TEXT_INPUT
;
993 else if (!strcmp(value
, "auto"))
995 return CRLF_UNDEFINED
;
998 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1000 const char *value
= check
->value
;
1002 if (ATTR_UNSET(value
))
1004 else if (!strcmp(value
, "lf"))
1006 else if (!strcmp(value
, "crlf"))
1011 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1013 const char *value
= check
->value
;
1014 struct convert_driver
*drv
;
1016 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1018 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1019 if (!strcmp(value
, drv
->name
))
1024 static int git_path_check_ident(struct attr_check_item
*check
)
1026 const char *value
= check
->value
;
1028 return !!ATTR_TRUE(value
);
1032 struct convert_driver
*drv
;
1033 enum crlf_action attr_action
; /* What attr says */
1034 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
1038 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
1040 static struct attr_check
*check
;
1043 check
= attr_check_initl("crlf", "ident", "filter",
1044 "eol", "text", NULL
);
1045 user_convert_tail
= &user_convert
;
1046 git_config(read_convert_config
, NULL
);
1049 if (!git_check_attr(path
, check
)) {
1050 struct attr_check_item
*ccheck
= check
->items
;
1051 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1052 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1053 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1054 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1055 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1056 if (ca
->crlf_action
!= CRLF_BINARY
) {
1057 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1058 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1059 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1060 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1061 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1062 else if (eol_attr
== EOL_LF
)
1063 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1064 else if (eol_attr
== EOL_CRLF
)
1065 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1069 ca
->crlf_action
= CRLF_UNDEFINED
;
1073 /* Save attr and make a decision for action */
1074 ca
->attr_action
= ca
->crlf_action
;
1075 if (ca
->crlf_action
== CRLF_TEXT
)
1076 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1077 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1078 ca
->crlf_action
= CRLF_BINARY
;
1079 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1080 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1081 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1082 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1085 int would_convert_to_git_filter_fd(const char *path
)
1087 struct conv_attrs ca
;
1089 convert_attrs(&ca
, path
);
1094 * Apply a filter to an fd only if the filter is required to succeed.
1095 * We must die if the filter fails, because the original data before
1096 * filtering is not available.
1098 if (!ca
.drv
->required
)
1101 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
, NULL
);
1104 const char *get_convert_attr_ascii(const char *path
)
1106 struct conv_attrs ca
;
1108 convert_attrs(&ca
, path
);
1109 switch (ca
.attr_action
) {
1110 case CRLF_UNDEFINED
:
1116 case CRLF_TEXT_INPUT
:
1117 return "text eol=lf";
1118 case CRLF_TEXT_CRLF
:
1119 return "text eol=crlf";
1122 case CRLF_AUTO_CRLF
:
1123 return "text=auto eol=crlf";
1124 case CRLF_AUTO_INPUT
:
1125 return "text=auto eol=lf";
1130 int convert_to_git(const struct index_state
*istate
,
1131 const char *path
, const char *src
, size_t len
,
1132 struct strbuf
*dst
, enum safe_crlf checksafe
)
1135 struct conv_attrs ca
;
1137 convert_attrs(&ca
, path
);
1139 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
, NULL
);
1140 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1141 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1147 if (checksafe
!= SAFE_CRLF_KEEP_CRLF
) {
1148 ret
|= crlf_to_git(istate
, path
, src
, len
, dst
, ca
.crlf_action
, checksafe
);
1154 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
1157 void convert_to_git_filter_fd(const struct index_state
*istate
,
1158 const char *path
, int fd
, struct strbuf
*dst
,
1159 enum safe_crlf checksafe
)
1161 struct conv_attrs ca
;
1162 convert_attrs(&ca
, path
);
1165 assert(ca
.drv
->clean
|| ca
.drv
->process
);
1167 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
, NULL
))
1168 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1170 crlf_to_git(istate
, path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, checksafe
);
1171 ident_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.ident
);
1174 static int convert_to_working_tree_internal(const char *path
, const char *src
,
1175 size_t len
, struct strbuf
*dst
,
1176 int normalizing
, struct delayed_checkout
*dco
)
1178 int ret
= 0, ret_filter
= 0;
1179 struct conv_attrs ca
;
1181 convert_attrs(&ca
, path
);
1183 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
1189 * CRLF conversion can be skipped if normalizing, unless there
1190 * is a smudge or process filter (even if the process filter doesn't
1191 * support smudge). The filters might expect CRLFs.
1193 if ((ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->process
)) || !normalizing
) {
1194 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
1201 ret_filter
= apply_filter(
1202 path
, src
, len
, -1, dst
, ca
.drv
, CAP_SMUDGE
, dco
);
1203 if (!ret_filter
&& ca
.drv
&& ca
.drv
->required
)
1204 die("%s: smudge filter %s failed", path
, ca
.drv
->name
);
1206 return ret
| ret_filter
;
1209 int async_convert_to_working_tree(const char *path
, const char *src
,
1210 size_t len
, struct strbuf
*dst
,
1213 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0, dco
);
1216 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1218 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0, NULL
);
1221 int renormalize_buffer(const struct index_state
*istate
, const char *path
,
1222 const char *src
, size_t len
, struct strbuf
*dst
)
1224 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1, NULL
);
1229 return ret
| convert_to_git(istate
, path
, src
, len
, dst
, SAFE_CRLF_RENORMALIZE
);
1232 /*****************************************************************
1234 * Streaming conversion support
1236 *****************************************************************/
1238 typedef int (*filter_fn
)(struct stream_filter
*,
1239 const char *input
, size_t *isize_p
,
1240 char *output
, size_t *osize_p
);
1241 typedef void (*free_fn
)(struct stream_filter
*);
1243 struct stream_filter_vtbl
{
1248 struct stream_filter
{
1249 struct stream_filter_vtbl
*vtbl
;
1252 static int null_filter_fn(struct stream_filter
*filter
,
1253 const char *input
, size_t *isize_p
,
1254 char *output
, size_t *osize_p
)
1259 return 0; /* we do not keep any states */
1261 if (*osize_p
< count
)
1264 memmove(output
, input
, count
);
1271 static void null_free_fn(struct stream_filter
*filter
)
1273 ; /* nothing -- null instances are shared */
1276 static struct stream_filter_vtbl null_vtbl
= {
1281 static struct stream_filter null_filter_singleton
= {
1285 int is_null_stream_filter(struct stream_filter
*filter
)
1287 return filter
== &null_filter_singleton
;
1295 struct lf_to_crlf_filter
{
1296 struct stream_filter filter
;
1297 unsigned has_held
:1;
1301 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1302 const char *input
, size_t *isize_p
,
1303 char *output
, size_t *osize_p
)
1305 size_t count
, o
= 0;
1306 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1309 * We may be holding onto the CR to see if it is followed by a
1310 * LF, in which case we would need to go to the main loop.
1311 * Otherwise, just emit it to the output stream.
1313 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1314 output
[o
++] = lf_to_crlf
->held
;
1315 lf_to_crlf
->has_held
= 0;
1318 /* We are told to drain */
1325 if (count
|| lf_to_crlf
->has_held
) {
1329 if (lf_to_crlf
->has_held
) {
1331 lf_to_crlf
->has_held
= 0;
1334 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1339 } else if (was_cr
) {
1341 * Previous round saw CR and it is not followed
1342 * by a LF; emit the CR before processing the
1343 * current character.
1349 * We may have consumed the last output slot,
1350 * in which case we need to break out of this
1351 * loop; hold the current character before
1354 if (*osize_p
<= o
) {
1355 lf_to_crlf
->has_held
= 1;
1356 lf_to_crlf
->held
= ch
;
1357 continue; /* break but increment i */
1372 if (!lf_to_crlf
->has_held
&& was_cr
) {
1373 lf_to_crlf
->has_held
= 1;
1374 lf_to_crlf
->held
= '\r';
1380 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1385 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1386 lf_to_crlf_filter_fn
,
1390 static struct stream_filter
*lf_to_crlf_filter(void)
1392 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1394 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1395 return (struct stream_filter
*)lf_to_crlf
;
1401 #define FILTER_BUFFER 1024
1402 struct cascade_filter
{
1403 struct stream_filter filter
;
1404 struct stream_filter
*one
;
1405 struct stream_filter
*two
;
1406 char buf
[FILTER_BUFFER
];
1410 static int cascade_filter_fn(struct stream_filter
*filter
,
1411 const char *input
, size_t *isize_p
,
1412 char *output
, size_t *osize_p
)
1414 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1416 size_t sz
= *osize_p
;
1417 size_t to_feed
, remaining
;
1420 * input -- (one) --> buf -- (two) --> output
1422 while (filled
< sz
) {
1423 remaining
= sz
- filled
;
1425 /* do we already have something to feed two with? */
1426 if (cas
->ptr
< cas
->end
) {
1427 to_feed
= cas
->end
- cas
->ptr
;
1428 if (stream_filter(cas
->two
,
1429 cas
->buf
+ cas
->ptr
, &to_feed
,
1430 output
+ filled
, &remaining
))
1432 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1433 filled
= sz
- remaining
;
1437 /* feed one from upstream and have it emit into our buffer */
1438 to_feed
= input
? *isize_p
: 0;
1439 if (input
&& !to_feed
)
1441 remaining
= sizeof(cas
->buf
);
1442 if (stream_filter(cas
->one
,
1444 cas
->buf
, &remaining
))
1446 cas
->end
= sizeof(cas
->buf
) - remaining
;
1449 size_t fed
= *isize_p
- to_feed
;
1454 /* do we know that we drained one completely? */
1455 if (input
|| cas
->end
)
1458 /* tell two to drain; we have nothing more to give it */
1460 remaining
= sz
- filled
;
1461 if (stream_filter(cas
->two
,
1463 output
+ filled
, &remaining
))
1465 if (remaining
== (sz
- filled
))
1466 break; /* completely drained two */
1467 filled
= sz
- remaining
;
1473 static void cascade_free_fn(struct stream_filter
*filter
)
1475 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1476 free_stream_filter(cas
->one
);
1477 free_stream_filter(cas
->two
);
1481 static struct stream_filter_vtbl cascade_vtbl
= {
1486 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1487 struct stream_filter
*two
)
1489 struct cascade_filter
*cascade
;
1491 if (!one
|| is_null_stream_filter(one
))
1493 if (!two
|| is_null_stream_filter(two
))
1496 cascade
= xmalloc(sizeof(*cascade
));
1499 cascade
->end
= cascade
->ptr
= 0;
1500 cascade
->filter
.vtbl
= &cascade_vtbl
;
1501 return (struct stream_filter
*)cascade
;
1507 #define IDENT_DRAINING (-1)
1508 #define IDENT_SKIPPING (-2)
1509 struct ident_filter
{
1510 struct stream_filter filter
;
1513 char ident
[45]; /* ": x40 $" */
1516 static int is_foreign_ident(const char *str
)
1520 if (!skip_prefix(str
, "$Id: ", &str
))
1522 for (i
= 0; str
[i
]; i
++) {
1523 if (isspace(str
[i
]) && str
[i
+1] != '$')
1529 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1531 size_t to_drain
= ident
->left
.len
;
1533 if (*osize_p
< to_drain
)
1534 to_drain
= *osize_p
;
1536 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1537 strbuf_remove(&ident
->left
, 0, to_drain
);
1538 *output_p
+= to_drain
;
1539 *osize_p
-= to_drain
;
1541 if (!ident
->left
.len
)
1545 static int ident_filter_fn(struct stream_filter
*filter
,
1546 const char *input
, size_t *isize_p
,
1547 char *output
, size_t *osize_p
)
1549 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1550 static const char head
[] = "$Id";
1553 /* drain upon eof */
1554 switch (ident
->state
) {
1556 strbuf_add(&ident
->left
, head
, ident
->state
);
1558 case IDENT_SKIPPING
:
1560 case IDENT_DRAINING
:
1561 ident_drain(ident
, &output
, osize_p
);
1566 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1569 if (ident
->state
== IDENT_DRAINING
) {
1570 ident_drain(ident
, &output
, osize_p
);
1579 if (ident
->state
== IDENT_SKIPPING
) {
1581 * Skipping until '$' or LF, but keeping them
1582 * in case it is a foreign ident.
1584 strbuf_addch(&ident
->left
, ch
);
1585 if (ch
!= '\n' && ch
!= '$')
1587 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1588 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1589 strbuf_addstr(&ident
->left
, ident
->ident
);
1591 ident
->state
= IDENT_DRAINING
;
1595 if (ident
->state
< sizeof(head
) &&
1596 head
[ident
->state
] == ch
) {
1602 strbuf_add(&ident
->left
, head
, ident
->state
);
1603 if (ident
->state
== sizeof(head
) - 1) {
1604 if (ch
!= ':' && ch
!= '$') {
1605 strbuf_addch(&ident
->left
, ch
);
1611 strbuf_addch(&ident
->left
, ch
);
1612 ident
->state
= IDENT_SKIPPING
;
1614 strbuf_addstr(&ident
->left
, ident
->ident
);
1615 ident
->state
= IDENT_DRAINING
;
1620 strbuf_addch(&ident
->left
, ch
);
1621 ident
->state
= IDENT_DRAINING
;
1626 static void ident_free_fn(struct stream_filter
*filter
)
1628 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1629 strbuf_release(&ident
->left
);
1633 static struct stream_filter_vtbl ident_vtbl
= {
1638 static struct stream_filter
*ident_filter(const unsigned char *sha1
)
1640 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1642 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1643 ": %s $", sha1_to_hex(sha1
));
1644 strbuf_init(&ident
->left
, 0);
1645 ident
->filter
.vtbl
= &ident_vtbl
;
1647 return (struct stream_filter
*)ident
;
1651 * Return an appropriately constructed filter for the path, or NULL if
1652 * the contents cannot be filtered without reading the whole thing
1655 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1656 * large binary blob you would want us not to slurp into the memory!
1658 struct stream_filter
*get_stream_filter(const char *path
, const unsigned char *sha1
)
1660 struct conv_attrs ca
;
1661 struct stream_filter
*filter
= NULL
;
1663 convert_attrs(&ca
, path
);
1664 if (ca
.drv
&& (ca
.drv
->process
|| ca
.drv
->smudge
|| ca
.drv
->clean
))
1667 if (ca
.crlf_action
== CRLF_AUTO
|| ca
.crlf_action
== CRLF_AUTO_CRLF
)
1671 filter
= ident_filter(sha1
);
1673 if (output_eol(ca
.crlf_action
) == EOL_CRLF
)
1674 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1676 filter
= cascade_filter(filter
, &null_filter_singleton
);
1681 void free_stream_filter(struct stream_filter
*filter
)
1683 filter
->vtbl
->free(filter
);
1686 int stream_filter(struct stream_filter
*filter
,
1687 const char *input
, size_t *isize_p
,
1688 char *output
, size_t *osize_p
)
1690 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);