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 char *path
)
141 void *data
= read_blob_data_from_cache(path
, &sz
);
142 ret
= gather_convert_stats_ascii(data
, sz
);
147 const char *get_wt_convert_stats_ascii(const char *path
)
149 const char *ret
= "";
150 struct strbuf sb
= STRBUF_INIT
;
151 if (strbuf_read_file(&sb
, path
, 0) >= 0)
152 ret
= gather_convert_stats_ascii(sb
.buf
, sb
.len
);
157 static int text_eol_is_crlf(void)
159 if (auto_crlf
== AUTO_CRLF_TRUE
)
161 else if (auto_crlf
== AUTO_CRLF_INPUT
)
163 if (core_eol
== EOL_CRLF
)
165 if (core_eol
== EOL_UNSET
&& EOL_NATIVE
== EOL_CRLF
)
170 static enum eol
output_eol(enum crlf_action crlf_action
)
172 switch (crlf_action
) {
177 case CRLF_TEXT_INPUT
:
182 case CRLF_AUTO_INPUT
:
187 return text_eol_is_crlf() ? EOL_CRLF
: EOL_LF
;
189 warning("Illegal crlf_action %d\n", (int)crlf_action
);
193 static void check_safe_crlf(const char *path
, enum crlf_action crlf_action
,
194 struct text_stat
*old_stats
, struct text_stat
*new_stats
,
195 enum safe_crlf checksafe
)
197 if (old_stats
->crlf
&& !new_stats
->crlf
) {
199 * CRLFs would not be restored by checkout
201 if (checksafe
== SAFE_CRLF_WARN
)
202 warning(_("CRLF will be replaced by LF in %s.\n"
203 "The file will have its original line"
204 " endings in your working directory."), path
);
205 else /* i.e. SAFE_CRLF_FAIL */
206 die(_("CRLF would be replaced by LF in %s."), path
);
207 } else if (old_stats
->lonelf
&& !new_stats
->lonelf
) {
209 * CRLFs would be added by checkout
211 if (checksafe
== SAFE_CRLF_WARN
)
212 warning(_("LF will be replaced by CRLF in %s.\n"
213 "The file will have its original line"
214 " endings in your working directory."), path
);
215 else /* i.e. SAFE_CRLF_FAIL */
216 die(_("LF would be replaced by CRLF in %s"), path
);
220 static int has_cr_in_index(const char *path
)
226 data
= read_blob_data_from_cache(path
, &sz
);
229 has_cr
= memchr(data
, '\r', sz
) != NULL
;
234 static int will_convert_lf_to_crlf(size_t len
, struct text_stat
*stats
,
235 enum crlf_action crlf_action
)
237 if (output_eol(crlf_action
) != EOL_CRLF
)
239 /* No "naked" LF? Nothing to convert, regardless. */
243 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
244 /* If we have any CR or CRLF line endings, we do not touch it */
245 /* This is the new safer autocrlf-handling */
246 if (stats
->lonecr
|| stats
->crlf
)
249 if (convert_is_binary(len
, stats
))
256 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
258 enum crlf_action crlf_action
, enum safe_crlf checksafe
)
260 struct text_stat stats
;
262 int convert_crlf_into_lf
;
264 if (crlf_action
== CRLF_BINARY
||
269 * If we are doing a dry-run and have no source buffer, there is
270 * nothing to analyze; we must assume we would convert.
275 gather_stats(src
, len
, &stats
);
276 /* Optimization: No CRLF? Nothing to convert, regardless. */
277 convert_crlf_into_lf
= !!stats
.crlf
;
279 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
280 if (convert_is_binary(len
, &stats
))
283 * If the file in the index has any CR in it, do not
284 * convert. This is the new safer autocrlf handling,
285 * unless we want to renormalize in a merge or
288 if ((checksafe
!= SAFE_CRLF_RENORMALIZE
) && has_cr_in_index(path
))
289 convert_crlf_into_lf
= 0;
291 if ((checksafe
== SAFE_CRLF_WARN
||
292 (checksafe
== SAFE_CRLF_FAIL
)) && len
) {
293 struct text_stat new_stats
;
294 memcpy(&new_stats
, &stats
, sizeof(new_stats
));
295 /* simulate "git add" */
296 if (convert_crlf_into_lf
) {
297 new_stats
.lonelf
+= new_stats
.crlf
;
300 /* simulate "git checkout" */
301 if (will_convert_lf_to_crlf(len
, &new_stats
, crlf_action
)) {
302 new_stats
.crlf
+= new_stats
.lonelf
;
303 new_stats
.lonelf
= 0;
305 check_safe_crlf(path
, crlf_action
, &stats
, &new_stats
, checksafe
);
307 if (!convert_crlf_into_lf
)
311 * At this point all of our source analysis is done, and we are sure we
312 * would convert. If we are in dry-run mode, we can give an answer.
317 /* only grow if not in place */
318 if (strbuf_avail(buf
) + buf
->len
< len
)
319 strbuf_grow(buf
, len
- buf
->len
);
321 if (crlf_action
== CRLF_AUTO
|| crlf_action
== CRLF_AUTO_INPUT
|| crlf_action
== CRLF_AUTO_CRLF
) {
323 * If we guessed, we already know we rejected a file with
324 * lone CR, and we can strip a CR without looking at what
328 unsigned char c
= *src
++;
334 unsigned char c
= *src
++;
335 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
339 strbuf_setlen(buf
, dst
- buf
->buf
);
343 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
344 struct strbuf
*buf
, enum crlf_action crlf_action
)
346 char *to_free
= NULL
;
347 struct text_stat stats
;
349 if (!len
|| output_eol(crlf_action
) != EOL_CRLF
)
352 gather_stats(src
, len
, &stats
);
353 if (!will_convert_lf_to_crlf(len
, &stats
, crlf_action
))
356 /* are we "faking" in place editing ? */
358 to_free
= strbuf_detach(buf
, NULL
);
360 strbuf_grow(buf
, len
+ stats
.lonelf
);
362 const char *nl
= memchr(src
, '\n', len
);
365 if (nl
> src
&& nl
[-1] == '\r') {
366 strbuf_add(buf
, src
, nl
+ 1 - src
);
368 strbuf_add(buf
, src
, nl
- src
);
369 strbuf_addstr(buf
, "\r\n");
374 strbuf_add(buf
, src
, len
);
380 struct filter_params
{
388 static int filter_buffer_or_fd(int in
, int out
, void *data
)
391 * Spawn cmd and feed the buffer contents through its stdin.
393 struct child_process child_process
= CHILD_PROCESS_INIT
;
394 struct filter_params
*params
= (struct filter_params
*)data
;
395 int write_err
, status
;
396 const char *argv
[] = { NULL
, NULL
};
398 /* apply % substitution to cmd */
399 struct strbuf cmd
= STRBUF_INIT
;
400 struct strbuf path
= STRBUF_INIT
;
401 struct strbuf_expand_dict_entry dict
[] = {
406 /* quote the path to preserve spaces, etc. */
407 sq_quote_buf(&path
, params
->path
);
408 dict
[0].value
= path
.buf
;
410 /* expand all %f with the quoted path */
411 strbuf_expand(&cmd
, params
->cmd
, strbuf_expand_dict_cb
, &dict
);
412 strbuf_release(&path
);
416 child_process
.argv
= argv
;
417 child_process
.use_shell
= 1;
418 child_process
.in
= -1;
419 child_process
.out
= out
;
421 if (start_command(&child_process
))
422 return error("cannot fork to run external filter '%s'", params
->cmd
);
424 sigchain_push(SIGPIPE
, SIG_IGN
);
427 write_err
= (write_in_full(child_process
.in
,
428 params
->src
, params
->size
) < 0);
432 write_err
= copy_fd(params
->fd
, child_process
.in
);
433 if (write_err
== COPY_WRITE_ERROR
&& errno
== EPIPE
)
437 if (close(child_process
.in
))
440 error("cannot feed the input to external filter '%s'", params
->cmd
);
442 sigchain_pop(SIGPIPE
);
444 status
= finish_command(&child_process
);
446 error("external filter '%s' failed %d", params
->cmd
, status
);
448 strbuf_release(&cmd
);
449 return (write_err
|| status
);
452 static int apply_single_file_filter(const char *path
, const char *src
, size_t len
, int fd
,
453 struct strbuf
*dst
, const char *cmd
)
456 * Create a pipeline to have the command filter the buffer's
459 * (child --> cmd) --> us
462 struct strbuf nbuf
= STRBUF_INIT
;
464 struct filter_params params
;
466 memset(&async
, 0, sizeof(async
));
467 async
.proc
= filter_buffer_or_fd
;
468 async
.data
= ¶ms
;
477 if (start_async(&async
))
478 return 0; /* error was already reported */
480 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
481 err
= error("read from external filter '%s' failed", cmd
);
483 if (close(async
.out
)) {
484 err
= error("read from external filter '%s' failed", cmd
);
486 if (finish_async(&async
)) {
487 err
= error("external filter '%s' failed", cmd
);
491 strbuf_swap(dst
, &nbuf
);
493 strbuf_release(&nbuf
);
497 #define CAP_CLEAN (1u<<0)
498 #define CAP_SMUDGE (1u<<1)
501 struct subprocess_entry subprocess
; /* must be the first member! */
502 unsigned int supported_capabilities
;
505 static int subprocess_map_initialized
;
506 static struct hashmap subprocess_map
;
508 static int start_multi_file_filter_fn(struct subprocess_entry
*subprocess
)
511 struct cmd2process
*entry
= (struct cmd2process
*)subprocess
;
512 struct string_list cap_list
= STRING_LIST_INIT_NODUP
;
514 const char *cap_name
;
515 struct child_process
*process
= &subprocess
->process
;
516 const char *cmd
= subprocess
->cmd
;
518 sigchain_push(SIGPIPE
, SIG_IGN
);
520 err
= packet_writel(process
->in
, "git-filter-client", "version=2", NULL
);
524 err
= strcmp(packet_read_line(process
->out
, NULL
), "git-filter-server");
526 error("external filter '%s' does not support filter protocol version 2", cmd
);
529 err
= strcmp(packet_read_line(process
->out
, NULL
), "version=2");
532 err
= packet_read_line(process
->out
, NULL
) != NULL
;
536 err
= packet_writel(process
->in
, "capability=clean", "capability=smudge", NULL
);
539 cap_buf
= packet_read_line(process
->out
, NULL
);
542 string_list_split_in_place(&cap_list
, cap_buf
, '=', 1);
544 if (cap_list
.nr
!= 2 || strcmp(cap_list
.items
[0].string
, "capability"))
547 cap_name
= cap_list
.items
[1].string
;
548 if (!strcmp(cap_name
, "clean")) {
549 entry
->supported_capabilities
|= CAP_CLEAN
;
550 } else if (!strcmp(cap_name
, "smudge")) {
551 entry
->supported_capabilities
|= CAP_SMUDGE
;
554 "external filter '%s' requested unsupported filter capability '%s'",
559 string_list_clear(&cap_list
, 0);
563 sigchain_pop(SIGPIPE
);
568 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
569 int fd
, struct strbuf
*dst
, const char *cmd
,
570 const unsigned int wanted_capability
)
573 struct cmd2process
*entry
;
574 struct child_process
*process
;
575 struct strbuf nbuf
= STRBUF_INIT
;
576 struct strbuf filter_status
= STRBUF_INIT
;
577 const char *filter_type
;
579 if (!subprocess_map_initialized
) {
580 subprocess_map_initialized
= 1;
581 hashmap_init(&subprocess_map
, (hashmap_cmp_fn
) cmd2process_cmp
, 0);
584 entry
= (struct cmd2process
*)subprocess_find_entry(&subprocess_map
, cmd
);
590 entry
= xmalloc(sizeof(*entry
));
591 entry
->supported_capabilities
= 0;
593 if (subprocess_start(&subprocess_map
, &entry
->subprocess
, cmd
, start_multi_file_filter_fn
)) {
598 process
= &entry
->subprocess
.process
;
600 if (!(wanted_capability
& entry
->supported_capabilities
))
603 if (CAP_CLEAN
& wanted_capability
)
604 filter_type
= "clean";
605 else if (CAP_SMUDGE
& wanted_capability
)
606 filter_type
= "smudge";
608 die("unexpected filter type");
610 sigchain_push(SIGPIPE
, SIG_IGN
);
612 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
613 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
617 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
619 error("path name too long for external filter");
623 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
627 err
= packet_flush_gently(process
->in
);
632 err
= write_packetized_from_fd(fd
, process
->in
);
634 err
= write_packetized_from_buf(src
, len
, process
->in
);
638 subprocess_read_status(process
->out
, &filter_status
);
639 err
= strcmp(filter_status
.buf
, "success");
643 err
= read_packetized_to_strbuf(process
->out
, &nbuf
) < 0;
647 subprocess_read_status(process
->out
, &filter_status
);
648 err
= strcmp(filter_status
.buf
, "success");
651 sigchain_pop(SIGPIPE
);
654 if (!strcmp(filter_status
.buf
, "error")) {
655 /* The filter signaled a problem with the file. */
656 } else if (!strcmp(filter_status
.buf
, "abort")) {
658 * The filter signaled a permanent problem. Don't try to filter
659 * files with the same command for the lifetime of the current
662 entry
->supported_capabilities
&= ~wanted_capability
;
665 * Something went wrong with the protocol filter.
666 * Force shutdown and restart if another blob requires filtering.
668 error("external filter '%s' failed", cmd
);
669 subprocess_stop(&subprocess_map
, &entry
->subprocess
);
673 strbuf_swap(dst
, &nbuf
);
675 strbuf_release(&nbuf
);
679 static struct convert_driver
{
681 struct convert_driver
*next
;
686 } *user_convert
, **user_convert_tail
;
688 static int apply_filter(const char *path
, const char *src
, size_t len
,
689 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
690 const unsigned int wanted_capability
)
692 const char *cmd
= NULL
;
700 if ((CAP_CLEAN
& wanted_capability
) && !drv
->process
&& drv
->clean
)
702 else if ((CAP_SMUDGE
& wanted_capability
) && !drv
->process
&& drv
->smudge
)
706 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
707 else if (drv
->process
&& *drv
->process
)
708 return apply_multi_file_filter(path
, src
, len
, fd
, dst
, drv
->process
, wanted_capability
);
713 static int read_convert_config(const char *var
, const char *value
, void *cb
)
715 const char *key
, *name
;
717 struct convert_driver
*drv
;
720 * External conversion drivers are configured using
721 * "filter.<name>.variable".
723 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
725 for (drv
= user_convert
; drv
; drv
= drv
->next
)
726 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
729 drv
= xcalloc(1, sizeof(struct convert_driver
));
730 drv
->name
= xmemdupz(name
, namelen
);
731 *user_convert_tail
= drv
;
732 user_convert_tail
= &(drv
->next
);
736 * filter.<name>.smudge and filter.<name>.clean specifies
741 * The command-line will not be interpolated in any way.
744 if (!strcmp("smudge", key
))
745 return git_config_string(&drv
->smudge
, var
, value
);
747 if (!strcmp("clean", key
))
748 return git_config_string(&drv
->clean
, var
, value
);
750 if (!strcmp("process", key
))
751 return git_config_string(&drv
->process
, var
, value
);
753 if (!strcmp("required", key
)) {
754 drv
->required
= git_config_bool(var
, value
);
761 static int count_ident(const char *cp
, unsigned long size
)
764 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
776 if (memcmp("Id", cp
, 2))
787 * "$Id: ... "; scan up to the closing dollar sign and discard.
803 static int ident_to_git(const char *path
, const char *src
, size_t len
,
804 struct strbuf
*buf
, int ident
)
808 if (!ident
|| (src
&& !count_ident(src
, len
)))
814 /* only grow if not in place */
815 if (strbuf_avail(buf
) + buf
->len
< len
)
816 strbuf_grow(buf
, len
- buf
->len
);
819 dollar
= memchr(src
, '$', len
);
822 memmove(dst
, src
, dollar
+ 1 - src
);
823 dst
+= dollar
+ 1 - src
;
824 len
-= dollar
+ 1 - src
;
827 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
828 dollar
= memchr(src
+ 3, '$', len
- 3);
831 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
832 /* Line break before the next dollar. */
836 memcpy(dst
, "Id$", 3);
838 len
-= dollar
+ 1 - src
;
842 memmove(dst
, src
, len
);
843 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
847 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
848 struct strbuf
*buf
, int ident
)
850 unsigned char sha1
[20];
851 char *to_free
= NULL
, *dollar
, *spc
;
857 cnt
= count_ident(src
, len
);
861 /* are we "faking" in place editing ? */
863 to_free
= strbuf_detach(buf
, NULL
);
864 hash_sha1_file(src
, len
, "blob", sha1
);
866 strbuf_grow(buf
, len
+ cnt
* 43);
868 /* step 1: run to the next '$' */
869 dollar
= memchr(src
, '$', len
);
872 strbuf_add(buf
, src
, dollar
+ 1 - src
);
873 len
-= dollar
+ 1 - src
;
876 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
877 if (len
< 3 || memcmp("Id", src
, 2))
880 /* step 3: skip over Id$ or Id:xxxxx$ */
884 } else if (src
[2] == ':') {
886 * It's possible that an expanded Id has crept its way into the
887 * repository, we cope with that by stripping the expansion out.
888 * This is probably not a good idea, since it will cause changes
889 * on checkout, which won't go away by stash, but let's keep it
892 dollar
= memchr(src
+ 3, '$', len
- 3);
894 /* incomplete keyword, no more '$', so just quit the loop */
898 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
899 /* Line break before the next dollar. */
903 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
904 if (spc
&& spc
< dollar
-1) {
905 /* There are spaces in unexpected places.
906 * This is probably an id from some other
907 * versioning system. Keep it for now.
912 len
-= dollar
+ 1 - src
;
915 /* it wasn't a "Id$" or "Id:xxxx$" */
919 /* step 4: substitute */
920 strbuf_addstr(buf
, "Id: ");
921 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
922 strbuf_addstr(buf
, " $");
924 strbuf_add(buf
, src
, len
);
930 static enum crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
932 const char *value
= check
->value
;
934 if (ATTR_TRUE(value
))
936 else if (ATTR_FALSE(value
))
938 else if (ATTR_UNSET(value
))
940 else if (!strcmp(value
, "input"))
941 return CRLF_TEXT_INPUT
;
942 else if (!strcmp(value
, "auto"))
944 return CRLF_UNDEFINED
;
947 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
949 const char *value
= check
->value
;
951 if (ATTR_UNSET(value
))
953 else if (!strcmp(value
, "lf"))
955 else if (!strcmp(value
, "crlf"))
960 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
962 const char *value
= check
->value
;
963 struct convert_driver
*drv
;
965 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
967 for (drv
= user_convert
; drv
; drv
= drv
->next
)
968 if (!strcmp(value
, drv
->name
))
973 static int git_path_check_ident(struct attr_check_item
*check
)
975 const char *value
= check
->value
;
977 return !!ATTR_TRUE(value
);
981 struct convert_driver
*drv
;
982 enum crlf_action attr_action
; /* What attr says */
983 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
987 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
989 static struct attr_check
*check
;
992 check
= attr_check_initl("crlf", "ident", "filter",
993 "eol", "text", NULL
);
994 user_convert_tail
= &user_convert
;
995 git_config(read_convert_config
, NULL
);
998 if (!git_check_attr(path
, check
)) {
999 struct attr_check_item
*ccheck
= check
->items
;
1000 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1001 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1002 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1003 ca
->attr_action
= ca
->crlf_action
;
1004 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1005 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1006 if (ca
->crlf_action
!= CRLF_BINARY
) {
1007 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1008 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1009 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1010 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1011 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1012 else if (eol_attr
== EOL_LF
)
1013 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1014 else if (eol_attr
== EOL_CRLF
)
1015 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1017 ca
->attr_action
= ca
->crlf_action
;
1020 ca
->crlf_action
= CRLF_UNDEFINED
;
1023 if (ca
->crlf_action
== CRLF_TEXT
)
1024 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1025 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1026 ca
->crlf_action
= CRLF_BINARY
;
1027 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1028 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1029 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1030 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1033 int would_convert_to_git_filter_fd(const char *path
)
1035 struct conv_attrs ca
;
1037 convert_attrs(&ca
, path
);
1042 * Apply a filter to an fd only if the filter is required to succeed.
1043 * We must die if the filter fails, because the original data before
1044 * filtering is not available.
1046 if (!ca
.drv
->required
)
1049 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
);
1052 const char *get_convert_attr_ascii(const char *path
)
1054 struct conv_attrs ca
;
1056 convert_attrs(&ca
, path
);
1057 switch (ca
.attr_action
) {
1058 case CRLF_UNDEFINED
:
1064 case CRLF_TEXT_INPUT
:
1065 return "text eol=lf";
1066 case CRLF_TEXT_CRLF
:
1067 return "text eol=crlf";
1070 case CRLF_AUTO_CRLF
:
1071 return "text=auto eol=crlf";
1072 case CRLF_AUTO_INPUT
:
1073 return "text=auto eol=lf";
1078 int convert_to_git(const char *path
, const char *src
, size_t len
,
1079 struct strbuf
*dst
, enum safe_crlf checksafe
)
1082 struct conv_attrs ca
;
1084 convert_attrs(&ca
, path
);
1086 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
);
1087 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1088 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1094 ret
|= crlf_to_git(path
, src
, len
, dst
, ca
.crlf_action
, checksafe
);
1099 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
1102 void convert_to_git_filter_fd(const char *path
, int fd
, struct strbuf
*dst
,
1103 enum safe_crlf checksafe
)
1105 struct conv_attrs ca
;
1106 convert_attrs(&ca
, path
);
1109 assert(ca
.drv
->clean
|| ca
.drv
->process
);
1111 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
))
1112 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1114 crlf_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, checksafe
);
1115 ident_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.ident
);
1118 static int convert_to_working_tree_internal(const char *path
, const char *src
,
1119 size_t len
, struct strbuf
*dst
,
1122 int ret
= 0, ret_filter
= 0;
1123 struct conv_attrs ca
;
1125 convert_attrs(&ca
, path
);
1127 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
1133 * CRLF conversion can be skipped if normalizing, unless there
1134 * is a smudge or process filter (even if the process filter doesn't
1135 * support smudge). The filters might expect CRLFs.
1137 if ((ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->process
)) || !normalizing
) {
1138 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
1145 ret_filter
= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_SMUDGE
);
1146 if (!ret_filter
&& ca
.drv
&& ca
.drv
->required
)
1147 die("%s: smudge filter %s failed", path
, ca
.drv
->name
);
1149 return ret
| ret_filter
;
1152 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1154 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0);
1157 int renormalize_buffer(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1159 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1);
1164 return ret
| convert_to_git(path
, src
, len
, dst
, SAFE_CRLF_RENORMALIZE
);
1167 /*****************************************************************
1169 * Streaming conversion support
1171 *****************************************************************/
1173 typedef int (*filter_fn
)(struct stream_filter
*,
1174 const char *input
, size_t *isize_p
,
1175 char *output
, size_t *osize_p
);
1176 typedef void (*free_fn
)(struct stream_filter
*);
1178 struct stream_filter_vtbl
{
1183 struct stream_filter
{
1184 struct stream_filter_vtbl
*vtbl
;
1187 static int null_filter_fn(struct stream_filter
*filter
,
1188 const char *input
, size_t *isize_p
,
1189 char *output
, size_t *osize_p
)
1194 return 0; /* we do not keep any states */
1196 if (*osize_p
< count
)
1199 memmove(output
, input
, count
);
1206 static void null_free_fn(struct stream_filter
*filter
)
1208 ; /* nothing -- null instances are shared */
1211 static struct stream_filter_vtbl null_vtbl
= {
1216 static struct stream_filter null_filter_singleton
= {
1220 int is_null_stream_filter(struct stream_filter
*filter
)
1222 return filter
== &null_filter_singleton
;
1230 struct lf_to_crlf_filter
{
1231 struct stream_filter filter
;
1232 unsigned has_held
:1;
1236 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1237 const char *input
, size_t *isize_p
,
1238 char *output
, size_t *osize_p
)
1240 size_t count
, o
= 0;
1241 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1244 * We may be holding onto the CR to see if it is followed by a
1245 * LF, in which case we would need to go to the main loop.
1246 * Otherwise, just emit it to the output stream.
1248 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1249 output
[o
++] = lf_to_crlf
->held
;
1250 lf_to_crlf
->has_held
= 0;
1253 /* We are told to drain */
1260 if (count
|| lf_to_crlf
->has_held
) {
1264 if (lf_to_crlf
->has_held
) {
1266 lf_to_crlf
->has_held
= 0;
1269 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1274 } else if (was_cr
) {
1276 * Previous round saw CR and it is not followed
1277 * by a LF; emit the CR before processing the
1278 * current character.
1284 * We may have consumed the last output slot,
1285 * in which case we need to break out of this
1286 * loop; hold the current character before
1289 if (*osize_p
<= o
) {
1290 lf_to_crlf
->has_held
= 1;
1291 lf_to_crlf
->held
= ch
;
1292 continue; /* break but increment i */
1307 if (!lf_to_crlf
->has_held
&& was_cr
) {
1308 lf_to_crlf
->has_held
= 1;
1309 lf_to_crlf
->held
= '\r';
1315 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1320 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1321 lf_to_crlf_filter_fn
,
1325 static struct stream_filter
*lf_to_crlf_filter(void)
1327 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1329 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1330 return (struct stream_filter
*)lf_to_crlf
;
1336 #define FILTER_BUFFER 1024
1337 struct cascade_filter
{
1338 struct stream_filter filter
;
1339 struct stream_filter
*one
;
1340 struct stream_filter
*two
;
1341 char buf
[FILTER_BUFFER
];
1345 static int cascade_filter_fn(struct stream_filter
*filter
,
1346 const char *input
, size_t *isize_p
,
1347 char *output
, size_t *osize_p
)
1349 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1351 size_t sz
= *osize_p
;
1352 size_t to_feed
, remaining
;
1355 * input -- (one) --> buf -- (two) --> output
1357 while (filled
< sz
) {
1358 remaining
= sz
- filled
;
1360 /* do we already have something to feed two with? */
1361 if (cas
->ptr
< cas
->end
) {
1362 to_feed
= cas
->end
- cas
->ptr
;
1363 if (stream_filter(cas
->two
,
1364 cas
->buf
+ cas
->ptr
, &to_feed
,
1365 output
+ filled
, &remaining
))
1367 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1368 filled
= sz
- remaining
;
1372 /* feed one from upstream and have it emit into our buffer */
1373 to_feed
= input
? *isize_p
: 0;
1374 if (input
&& !to_feed
)
1376 remaining
= sizeof(cas
->buf
);
1377 if (stream_filter(cas
->one
,
1379 cas
->buf
, &remaining
))
1381 cas
->end
= sizeof(cas
->buf
) - remaining
;
1384 size_t fed
= *isize_p
- to_feed
;
1389 /* do we know that we drained one completely? */
1390 if (input
|| cas
->end
)
1393 /* tell two to drain; we have nothing more to give it */
1395 remaining
= sz
- filled
;
1396 if (stream_filter(cas
->two
,
1398 output
+ filled
, &remaining
))
1400 if (remaining
== (sz
- filled
))
1401 break; /* completely drained two */
1402 filled
= sz
- remaining
;
1408 static void cascade_free_fn(struct stream_filter
*filter
)
1410 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1411 free_stream_filter(cas
->one
);
1412 free_stream_filter(cas
->two
);
1416 static struct stream_filter_vtbl cascade_vtbl
= {
1421 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1422 struct stream_filter
*two
)
1424 struct cascade_filter
*cascade
;
1426 if (!one
|| is_null_stream_filter(one
))
1428 if (!two
|| is_null_stream_filter(two
))
1431 cascade
= xmalloc(sizeof(*cascade
));
1434 cascade
->end
= cascade
->ptr
= 0;
1435 cascade
->filter
.vtbl
= &cascade_vtbl
;
1436 return (struct stream_filter
*)cascade
;
1442 #define IDENT_DRAINING (-1)
1443 #define IDENT_SKIPPING (-2)
1444 struct ident_filter
{
1445 struct stream_filter filter
;
1448 char ident
[45]; /* ": x40 $" */
1451 static int is_foreign_ident(const char *str
)
1455 if (!skip_prefix(str
, "$Id: ", &str
))
1457 for (i
= 0; str
[i
]; i
++) {
1458 if (isspace(str
[i
]) && str
[i
+1] != '$')
1464 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1466 size_t to_drain
= ident
->left
.len
;
1468 if (*osize_p
< to_drain
)
1469 to_drain
= *osize_p
;
1471 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1472 strbuf_remove(&ident
->left
, 0, to_drain
);
1473 *output_p
+= to_drain
;
1474 *osize_p
-= to_drain
;
1476 if (!ident
->left
.len
)
1480 static int ident_filter_fn(struct stream_filter
*filter
,
1481 const char *input
, size_t *isize_p
,
1482 char *output
, size_t *osize_p
)
1484 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1485 static const char head
[] = "$Id";
1488 /* drain upon eof */
1489 switch (ident
->state
) {
1491 strbuf_add(&ident
->left
, head
, ident
->state
);
1492 case IDENT_SKIPPING
:
1494 case IDENT_DRAINING
:
1495 ident_drain(ident
, &output
, osize_p
);
1500 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1503 if (ident
->state
== IDENT_DRAINING
) {
1504 ident_drain(ident
, &output
, osize_p
);
1513 if (ident
->state
== IDENT_SKIPPING
) {
1515 * Skipping until '$' or LF, but keeping them
1516 * in case it is a foreign ident.
1518 strbuf_addch(&ident
->left
, ch
);
1519 if (ch
!= '\n' && ch
!= '$')
1521 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1522 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1523 strbuf_addstr(&ident
->left
, ident
->ident
);
1525 ident
->state
= IDENT_DRAINING
;
1529 if (ident
->state
< sizeof(head
) &&
1530 head
[ident
->state
] == ch
) {
1536 strbuf_add(&ident
->left
, head
, ident
->state
);
1537 if (ident
->state
== sizeof(head
) - 1) {
1538 if (ch
!= ':' && ch
!= '$') {
1539 strbuf_addch(&ident
->left
, ch
);
1545 strbuf_addch(&ident
->left
, ch
);
1546 ident
->state
= IDENT_SKIPPING
;
1548 strbuf_addstr(&ident
->left
, ident
->ident
);
1549 ident
->state
= IDENT_DRAINING
;
1554 strbuf_addch(&ident
->left
, ch
);
1555 ident
->state
= IDENT_DRAINING
;
1560 static void ident_free_fn(struct stream_filter
*filter
)
1562 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1563 strbuf_release(&ident
->left
);
1567 static struct stream_filter_vtbl ident_vtbl
= {
1572 static struct stream_filter
*ident_filter(const unsigned char *sha1
)
1574 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1576 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1577 ": %s $", sha1_to_hex(sha1
));
1578 strbuf_init(&ident
->left
, 0);
1579 ident
->filter
.vtbl
= &ident_vtbl
;
1581 return (struct stream_filter
*)ident
;
1585 * Return an appropriately constructed filter for the path, or NULL if
1586 * the contents cannot be filtered without reading the whole thing
1589 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1590 * large binary blob you would want us not to slurp into the memory!
1592 struct stream_filter
*get_stream_filter(const char *path
, const unsigned char *sha1
)
1594 struct conv_attrs ca
;
1595 struct stream_filter
*filter
= NULL
;
1597 convert_attrs(&ca
, path
);
1598 if (ca
.drv
&& (ca
.drv
->process
|| ca
.drv
->smudge
|| ca
.drv
->clean
))
1601 if (ca
.crlf_action
== CRLF_AUTO
|| ca
.crlf_action
== CRLF_AUTO_CRLF
)
1605 filter
= ident_filter(sha1
);
1607 if (output_eol(ca
.crlf_action
) == EOL_CRLF
)
1608 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1610 filter
= cascade_filter(filter
, &null_filter_singleton
);
1615 void free_stream_filter(struct stream_filter
*filter
)
1617 filter
->vtbl
->free(filter
);
1620 int stream_filter(struct stream_filter
*filter
,
1621 const char *input
, size_t *isize_p
,
1622 char *output
, size_t *osize_p
)
1624 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);