4 #include "run-command.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 hashmap_entry ent
; /* must be the first member! */
502 unsigned int supported_capabilities
;
504 struct child_process process
;
507 static int cmd_process_map_initialized
;
508 static struct hashmap cmd_process_map
;
510 static int cmd2process_cmp(const struct cmd2process
*e1
,
511 const struct cmd2process
*e2
,
514 return strcmp(e1
->cmd
, e2
->cmd
);
517 static struct cmd2process
*find_multi_file_filter_entry(struct hashmap
*hashmap
, const char *cmd
)
519 struct cmd2process key
;
520 hashmap_entry_init(&key
, strhash(cmd
));
522 return hashmap_get(hashmap
, &key
, NULL
);
525 static int packet_write_list(int fd
, const char *line
, ...)
529 va_start(args
, line
);
533 if (strlen(line
) > LARGE_PACKET_DATA_MAX
)
535 err
= packet_write_fmt_gently(fd
, "%s\n", line
);
538 line
= va_arg(args
, const char*);
541 return packet_flush_gently(fd
);
544 static void read_multi_file_filter_status(int fd
, struct strbuf
*status
)
546 struct strbuf
**pair
;
549 line
= packet_read_line(fd
, NULL
);
552 pair
= strbuf_split_str(line
, '=', 2);
553 if (pair
[0] && pair
[0]->len
&& pair
[1]) {
554 /* the last "status=<foo>" line wins */
555 if (!strcmp(pair
[0]->buf
, "status=")) {
556 strbuf_reset(status
);
557 strbuf_addbuf(status
, pair
[1]);
560 strbuf_list_free(pair
);
564 static void kill_multi_file_filter(struct hashmap
*hashmap
, struct cmd2process
*entry
)
569 entry
->process
.clean_on_exit
= 0;
570 kill(entry
->process
.pid
, SIGTERM
);
571 finish_command(&entry
->process
);
573 hashmap_remove(hashmap
, entry
, NULL
);
577 static void stop_multi_file_filter(struct child_process
*process
)
579 sigchain_push(SIGPIPE
, SIG_IGN
);
580 /* Closing the pipe signals the filter to initiate a shutdown. */
583 sigchain_pop(SIGPIPE
);
584 /* Finish command will wait until the shutdown is complete. */
585 finish_command(process
);
588 static struct cmd2process
*start_multi_file_filter(struct hashmap
*hashmap
, const char *cmd
)
591 struct cmd2process
*entry
;
592 struct child_process
*process
;
593 const char *argv
[] = { cmd
, NULL
};
594 struct string_list cap_list
= STRING_LIST_INIT_NODUP
;
596 const char *cap_name
;
598 entry
= xmalloc(sizeof(*entry
));
600 entry
->supported_capabilities
= 0;
601 process
= &entry
->process
;
603 child_process_init(process
);
604 process
->argv
= argv
;
605 process
->use_shell
= 1;
608 process
->clean_on_exit
= 1;
609 process
->clean_on_exit_handler
= stop_multi_file_filter
;
611 if (start_command(process
)) {
612 error("cannot fork to run external filter '%s'", cmd
);
616 hashmap_entry_init(entry
, strhash(cmd
));
618 sigchain_push(SIGPIPE
, SIG_IGN
);
620 err
= packet_write_list(process
->in
, "git-filter-client", "version=2", NULL
);
624 err
= strcmp(packet_read_line(process
->out
, NULL
), "git-filter-server");
626 error("external filter '%s' does not support filter protocol version 2", cmd
);
629 err
= strcmp(packet_read_line(process
->out
, NULL
), "version=2");
632 err
= packet_read_line(process
->out
, NULL
) != NULL
;
636 err
= packet_write_list(process
->in
, "capability=clean", "capability=smudge", NULL
);
639 cap_buf
= packet_read_line(process
->out
, NULL
);
642 string_list_split_in_place(&cap_list
, cap_buf
, '=', 1);
644 if (cap_list
.nr
!= 2 || strcmp(cap_list
.items
[0].string
, "capability"))
647 cap_name
= cap_list
.items
[1].string
;
648 if (!strcmp(cap_name
, "clean")) {
649 entry
->supported_capabilities
|= CAP_CLEAN
;
650 } else if (!strcmp(cap_name
, "smudge")) {
651 entry
->supported_capabilities
|= CAP_SMUDGE
;
654 "external filter '%s' requested unsupported filter capability '%s'",
659 string_list_clear(&cap_list
, 0);
663 sigchain_pop(SIGPIPE
);
665 if (err
|| errno
== EPIPE
) {
666 error("initialization for external filter '%s' failed", cmd
);
667 kill_multi_file_filter(hashmap
, entry
);
671 hashmap_add(hashmap
, entry
);
675 static int apply_multi_file_filter(const char *path
, const char *src
, size_t len
,
676 int fd
, struct strbuf
*dst
, const char *cmd
,
677 const unsigned int wanted_capability
)
680 struct cmd2process
*entry
;
681 struct child_process
*process
;
682 struct strbuf nbuf
= STRBUF_INIT
;
683 struct strbuf filter_status
= STRBUF_INIT
;
684 const char *filter_type
;
686 if (!cmd_process_map_initialized
) {
687 cmd_process_map_initialized
= 1;
688 hashmap_init(&cmd_process_map
, (hashmap_cmp_fn
) cmd2process_cmp
, 0);
691 entry
= find_multi_file_filter_entry(&cmd_process_map
, cmd
);
697 entry
= start_multi_file_filter(&cmd_process_map
, cmd
);
701 process
= &entry
->process
;
703 if (!(wanted_capability
& entry
->supported_capabilities
))
706 if (CAP_CLEAN
& wanted_capability
)
707 filter_type
= "clean";
708 else if (CAP_SMUDGE
& wanted_capability
)
709 filter_type
= "smudge";
711 die("unexpected filter type");
713 sigchain_push(SIGPIPE
, SIG_IGN
);
715 assert(strlen(filter_type
) < LARGE_PACKET_DATA_MAX
- strlen("command=\n"));
716 err
= packet_write_fmt_gently(process
->in
, "command=%s\n", filter_type
);
720 err
= strlen(path
) > LARGE_PACKET_DATA_MAX
- strlen("pathname=\n");
722 error("path name too long for external filter");
726 err
= packet_write_fmt_gently(process
->in
, "pathname=%s\n", path
);
730 err
= packet_flush_gently(process
->in
);
735 err
= write_packetized_from_fd(fd
, process
->in
);
737 err
= write_packetized_from_buf(src
, len
, process
->in
);
741 read_multi_file_filter_status(process
->out
, &filter_status
);
742 err
= strcmp(filter_status
.buf
, "success");
746 err
= read_packetized_to_strbuf(process
->out
, &nbuf
) < 0;
750 read_multi_file_filter_status(process
->out
, &filter_status
);
751 err
= strcmp(filter_status
.buf
, "success");
754 sigchain_pop(SIGPIPE
);
756 if (err
|| errno
== EPIPE
) {
757 if (!strcmp(filter_status
.buf
, "error")) {
758 /* The filter signaled a problem with the file. */
759 } else if (!strcmp(filter_status
.buf
, "abort")) {
761 * The filter signaled a permanent problem. Don't try to filter
762 * files with the same command for the lifetime of the current
765 entry
->supported_capabilities
&= ~wanted_capability
;
768 * Something went wrong with the protocol filter.
769 * Force shutdown and restart if another blob requires filtering.
771 error("external filter '%s' failed", cmd
);
772 kill_multi_file_filter(&cmd_process_map
, entry
);
775 strbuf_swap(dst
, &nbuf
);
777 strbuf_release(&nbuf
);
781 static struct convert_driver
{
783 struct convert_driver
*next
;
788 } *user_convert
, **user_convert_tail
;
790 static int apply_filter(const char *path
, const char *src
, size_t len
,
791 int fd
, struct strbuf
*dst
, struct convert_driver
*drv
,
792 const unsigned int wanted_capability
)
794 const char *cmd
= NULL
;
802 if ((CAP_CLEAN
& wanted_capability
) && !drv
->process
&& drv
->clean
)
804 else if ((CAP_SMUDGE
& wanted_capability
) && !drv
->process
&& drv
->smudge
)
808 return apply_single_file_filter(path
, src
, len
, fd
, dst
, cmd
);
809 else if (drv
->process
&& *drv
->process
)
810 return apply_multi_file_filter(path
, src
, len
, fd
, dst
, drv
->process
, wanted_capability
);
815 static int read_convert_config(const char *var
, const char *value
, void *cb
)
817 const char *key
, *name
;
819 struct convert_driver
*drv
;
822 * External conversion drivers are configured using
823 * "filter.<name>.variable".
825 if (parse_config_key(var
, "filter", &name
, &namelen
, &key
) < 0 || !name
)
827 for (drv
= user_convert
; drv
; drv
= drv
->next
)
828 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
831 drv
= xcalloc(1, sizeof(struct convert_driver
));
832 drv
->name
= xmemdupz(name
, namelen
);
833 *user_convert_tail
= drv
;
834 user_convert_tail
= &(drv
->next
);
838 * filter.<name>.smudge and filter.<name>.clean specifies
843 * The command-line will not be interpolated in any way.
846 if (!strcmp("smudge", key
))
847 return git_config_string(&drv
->smudge
, var
, value
);
849 if (!strcmp("clean", key
))
850 return git_config_string(&drv
->clean
, var
, value
);
852 if (!strcmp("process", key
))
853 return git_config_string(&drv
->process
, var
, value
);
855 if (!strcmp("required", key
)) {
856 drv
->required
= git_config_bool(var
, value
);
863 static int count_ident(const char *cp
, unsigned long size
)
866 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
878 if (memcmp("Id", cp
, 2))
889 * "$Id: ... "; scan up to the closing dollar sign and discard.
905 static int ident_to_git(const char *path
, const char *src
, size_t len
,
906 struct strbuf
*buf
, int ident
)
910 if (!ident
|| (src
&& !count_ident(src
, len
)))
916 /* only grow if not in place */
917 if (strbuf_avail(buf
) + buf
->len
< len
)
918 strbuf_grow(buf
, len
- buf
->len
);
921 dollar
= memchr(src
, '$', len
);
924 memmove(dst
, src
, dollar
+ 1 - src
);
925 dst
+= dollar
+ 1 - src
;
926 len
-= dollar
+ 1 - src
;
929 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
930 dollar
= memchr(src
+ 3, '$', len
- 3);
933 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
934 /* Line break before the next dollar. */
938 memcpy(dst
, "Id$", 3);
940 len
-= dollar
+ 1 - src
;
944 memmove(dst
, src
, len
);
945 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
949 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
950 struct strbuf
*buf
, int ident
)
952 unsigned char sha1
[20];
953 char *to_free
= NULL
, *dollar
, *spc
;
959 cnt
= count_ident(src
, len
);
963 /* are we "faking" in place editing ? */
965 to_free
= strbuf_detach(buf
, NULL
);
966 hash_sha1_file(src
, len
, "blob", sha1
);
968 strbuf_grow(buf
, len
+ cnt
* 43);
970 /* step 1: run to the next '$' */
971 dollar
= memchr(src
, '$', len
);
974 strbuf_add(buf
, src
, dollar
+ 1 - src
);
975 len
-= dollar
+ 1 - src
;
978 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
979 if (len
< 3 || memcmp("Id", src
, 2))
982 /* step 3: skip over Id$ or Id:xxxxx$ */
986 } else if (src
[2] == ':') {
988 * It's possible that an expanded Id has crept its way into the
989 * repository, we cope with that by stripping the expansion out.
990 * This is probably not a good idea, since it will cause changes
991 * on checkout, which won't go away by stash, but let's keep it
994 dollar
= memchr(src
+ 3, '$', len
- 3);
996 /* incomplete keyword, no more '$', so just quit the loop */
1000 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
1001 /* Line break before the next dollar. */
1005 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
1006 if (spc
&& spc
< dollar
-1) {
1007 /* There are spaces in unexpected places.
1008 * This is probably an id from some other
1009 * versioning system. Keep it for now.
1014 len
-= dollar
+ 1 - src
;
1017 /* it wasn't a "Id$" or "Id:xxxx$" */
1021 /* step 4: substitute */
1022 strbuf_addstr(buf
, "Id: ");
1023 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
1024 strbuf_addstr(buf
, " $");
1026 strbuf_add(buf
, src
, len
);
1032 static enum crlf_action
git_path_check_crlf(struct attr_check_item
*check
)
1034 const char *value
= check
->value
;
1036 if (ATTR_TRUE(value
))
1038 else if (ATTR_FALSE(value
))
1040 else if (ATTR_UNSET(value
))
1042 else if (!strcmp(value
, "input"))
1043 return CRLF_TEXT_INPUT
;
1044 else if (!strcmp(value
, "auto"))
1046 return CRLF_UNDEFINED
;
1049 static enum eol
git_path_check_eol(struct attr_check_item
*check
)
1051 const char *value
= check
->value
;
1053 if (ATTR_UNSET(value
))
1055 else if (!strcmp(value
, "lf"))
1057 else if (!strcmp(value
, "crlf"))
1062 static struct convert_driver
*git_path_check_convert(struct attr_check_item
*check
)
1064 const char *value
= check
->value
;
1065 struct convert_driver
*drv
;
1067 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
1069 for (drv
= user_convert
; drv
; drv
= drv
->next
)
1070 if (!strcmp(value
, drv
->name
))
1075 static int git_path_check_ident(struct attr_check_item
*check
)
1077 const char *value
= check
->value
;
1079 return !!ATTR_TRUE(value
);
1083 struct convert_driver
*drv
;
1084 enum crlf_action attr_action
; /* What attr says */
1085 enum crlf_action crlf_action
; /* When no attr is set, use core.autocrlf */
1089 static void convert_attrs(struct conv_attrs
*ca
, const char *path
)
1091 static struct attr_check
*check
;
1094 check
= attr_check_initl("crlf", "ident", "filter",
1095 "eol", "text", NULL
);
1096 user_convert_tail
= &user_convert
;
1097 git_config(read_convert_config
, NULL
);
1100 if (!git_check_attr(path
, check
)) {
1101 struct attr_check_item
*ccheck
= check
->items
;
1102 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 4);
1103 if (ca
->crlf_action
== CRLF_UNDEFINED
)
1104 ca
->crlf_action
= git_path_check_crlf(ccheck
+ 0);
1105 ca
->attr_action
= ca
->crlf_action
;
1106 ca
->ident
= git_path_check_ident(ccheck
+ 1);
1107 ca
->drv
= git_path_check_convert(ccheck
+ 2);
1108 if (ca
->crlf_action
!= CRLF_BINARY
) {
1109 enum eol eol_attr
= git_path_check_eol(ccheck
+ 3);
1110 if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_LF
)
1111 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1112 else if (ca
->crlf_action
== CRLF_AUTO
&& eol_attr
== EOL_CRLF
)
1113 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1114 else if (eol_attr
== EOL_LF
)
1115 ca
->crlf_action
= CRLF_TEXT_INPUT
;
1116 else if (eol_attr
== EOL_CRLF
)
1117 ca
->crlf_action
= CRLF_TEXT_CRLF
;
1119 ca
->attr_action
= ca
->crlf_action
;
1122 ca
->crlf_action
= CRLF_UNDEFINED
;
1125 if (ca
->crlf_action
== CRLF_TEXT
)
1126 ca
->crlf_action
= text_eol_is_crlf() ? CRLF_TEXT_CRLF
: CRLF_TEXT_INPUT
;
1127 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_FALSE
)
1128 ca
->crlf_action
= CRLF_BINARY
;
1129 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_TRUE
)
1130 ca
->crlf_action
= CRLF_AUTO_CRLF
;
1131 if (ca
->crlf_action
== CRLF_UNDEFINED
&& auto_crlf
== AUTO_CRLF_INPUT
)
1132 ca
->crlf_action
= CRLF_AUTO_INPUT
;
1135 int would_convert_to_git_filter_fd(const char *path
)
1137 struct conv_attrs ca
;
1139 convert_attrs(&ca
, path
);
1144 * Apply a filter to an fd only if the filter is required to succeed.
1145 * We must die if the filter fails, because the original data before
1146 * filtering is not available.
1148 if (!ca
.drv
->required
)
1151 return apply_filter(path
, NULL
, 0, -1, NULL
, ca
.drv
, CAP_CLEAN
);
1154 const char *get_convert_attr_ascii(const char *path
)
1156 struct conv_attrs ca
;
1158 convert_attrs(&ca
, path
);
1159 switch (ca
.attr_action
) {
1160 case CRLF_UNDEFINED
:
1166 case CRLF_TEXT_INPUT
:
1167 return "text eol=lf";
1168 case CRLF_TEXT_CRLF
:
1169 return "text eol=crlf";
1172 case CRLF_AUTO_CRLF
:
1173 return "text=auto eol=crlf";
1174 case CRLF_AUTO_INPUT
:
1175 return "text=auto eol=lf";
1180 int convert_to_git(const char *path
, const char *src
, size_t len
,
1181 struct strbuf
*dst
, enum safe_crlf checksafe
)
1184 struct conv_attrs ca
;
1186 convert_attrs(&ca
, path
);
1188 ret
|= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_CLEAN
);
1189 if (!ret
&& ca
.drv
&& ca
.drv
->required
)
1190 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1196 ret
|= crlf_to_git(path
, src
, len
, dst
, ca
.crlf_action
, checksafe
);
1201 return ret
| ident_to_git(path
, src
, len
, dst
, ca
.ident
);
1204 void convert_to_git_filter_fd(const char *path
, int fd
, struct strbuf
*dst
,
1205 enum safe_crlf checksafe
)
1207 struct conv_attrs ca
;
1208 convert_attrs(&ca
, path
);
1211 assert(ca
.drv
->clean
|| ca
.drv
->process
);
1213 if (!apply_filter(path
, NULL
, 0, fd
, dst
, ca
.drv
, CAP_CLEAN
))
1214 die("%s: clean filter '%s' failed", path
, ca
.drv
->name
);
1216 crlf_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.crlf_action
, checksafe
);
1217 ident_to_git(path
, dst
->buf
, dst
->len
, dst
, ca
.ident
);
1220 static int convert_to_working_tree_internal(const char *path
, const char *src
,
1221 size_t len
, struct strbuf
*dst
,
1224 int ret
= 0, ret_filter
= 0;
1225 struct conv_attrs ca
;
1227 convert_attrs(&ca
, path
);
1229 ret
|= ident_to_worktree(path
, src
, len
, dst
, ca
.ident
);
1235 * CRLF conversion can be skipped if normalizing, unless there
1236 * is a smudge or process filter (even if the process filter doesn't
1237 * support smudge). The filters might expect CRLFs.
1239 if ((ca
.drv
&& (ca
.drv
->smudge
|| ca
.drv
->process
)) || !normalizing
) {
1240 ret
|= crlf_to_worktree(path
, src
, len
, dst
, ca
.crlf_action
);
1247 ret_filter
= apply_filter(path
, src
, len
, -1, dst
, ca
.drv
, CAP_SMUDGE
);
1248 if (!ret_filter
&& ca
.drv
&& ca
.drv
->required
)
1249 die("%s: smudge filter %s failed", path
, ca
.drv
->name
);
1251 return ret
| ret_filter
;
1254 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1256 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0);
1259 int renormalize_buffer(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
1261 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1);
1266 return ret
| convert_to_git(path
, src
, len
, dst
, SAFE_CRLF_RENORMALIZE
);
1269 /*****************************************************************
1271 * Streaming conversion support
1273 *****************************************************************/
1275 typedef int (*filter_fn
)(struct stream_filter
*,
1276 const char *input
, size_t *isize_p
,
1277 char *output
, size_t *osize_p
);
1278 typedef void (*free_fn
)(struct stream_filter
*);
1280 struct stream_filter_vtbl
{
1285 struct stream_filter
{
1286 struct stream_filter_vtbl
*vtbl
;
1289 static int null_filter_fn(struct stream_filter
*filter
,
1290 const char *input
, size_t *isize_p
,
1291 char *output
, size_t *osize_p
)
1296 return 0; /* we do not keep any states */
1298 if (*osize_p
< count
)
1301 memmove(output
, input
, count
);
1308 static void null_free_fn(struct stream_filter
*filter
)
1310 ; /* nothing -- null instances are shared */
1313 static struct stream_filter_vtbl null_vtbl
= {
1318 static struct stream_filter null_filter_singleton
= {
1322 int is_null_stream_filter(struct stream_filter
*filter
)
1324 return filter
== &null_filter_singleton
;
1332 struct lf_to_crlf_filter
{
1333 struct stream_filter filter
;
1334 unsigned has_held
:1;
1338 static int lf_to_crlf_filter_fn(struct stream_filter
*filter
,
1339 const char *input
, size_t *isize_p
,
1340 char *output
, size_t *osize_p
)
1342 size_t count
, o
= 0;
1343 struct lf_to_crlf_filter
*lf_to_crlf
= (struct lf_to_crlf_filter
*)filter
;
1346 * We may be holding onto the CR to see if it is followed by a
1347 * LF, in which case we would need to go to the main loop.
1348 * Otherwise, just emit it to the output stream.
1350 if (lf_to_crlf
->has_held
&& (lf_to_crlf
->held
!= '\r' || !input
)) {
1351 output
[o
++] = lf_to_crlf
->held
;
1352 lf_to_crlf
->has_held
= 0;
1355 /* We are told to drain */
1362 if (count
|| lf_to_crlf
->has_held
) {
1366 if (lf_to_crlf
->has_held
) {
1368 lf_to_crlf
->has_held
= 0;
1371 for (i
= 0; o
< *osize_p
&& i
< count
; i
++) {
1376 } else if (was_cr
) {
1378 * Previous round saw CR and it is not followed
1379 * by a LF; emit the CR before processing the
1380 * current character.
1386 * We may have consumed the last output slot,
1387 * in which case we need to break out of this
1388 * loop; hold the current character before
1391 if (*osize_p
<= o
) {
1392 lf_to_crlf
->has_held
= 1;
1393 lf_to_crlf
->held
= ch
;
1394 continue; /* break but increment i */
1409 if (!lf_to_crlf
->has_held
&& was_cr
) {
1410 lf_to_crlf
->has_held
= 1;
1411 lf_to_crlf
->held
= '\r';
1417 static void lf_to_crlf_free_fn(struct stream_filter
*filter
)
1422 static struct stream_filter_vtbl lf_to_crlf_vtbl
= {
1423 lf_to_crlf_filter_fn
,
1427 static struct stream_filter
*lf_to_crlf_filter(void)
1429 struct lf_to_crlf_filter
*lf_to_crlf
= xcalloc(1, sizeof(*lf_to_crlf
));
1431 lf_to_crlf
->filter
.vtbl
= &lf_to_crlf_vtbl
;
1432 return (struct stream_filter
*)lf_to_crlf
;
1438 #define FILTER_BUFFER 1024
1439 struct cascade_filter
{
1440 struct stream_filter filter
;
1441 struct stream_filter
*one
;
1442 struct stream_filter
*two
;
1443 char buf
[FILTER_BUFFER
];
1447 static int cascade_filter_fn(struct stream_filter
*filter
,
1448 const char *input
, size_t *isize_p
,
1449 char *output
, size_t *osize_p
)
1451 struct cascade_filter
*cas
= (struct cascade_filter
*) filter
;
1453 size_t sz
= *osize_p
;
1454 size_t to_feed
, remaining
;
1457 * input -- (one) --> buf -- (two) --> output
1459 while (filled
< sz
) {
1460 remaining
= sz
- filled
;
1462 /* do we already have something to feed two with? */
1463 if (cas
->ptr
< cas
->end
) {
1464 to_feed
= cas
->end
- cas
->ptr
;
1465 if (stream_filter(cas
->two
,
1466 cas
->buf
+ cas
->ptr
, &to_feed
,
1467 output
+ filled
, &remaining
))
1469 cas
->ptr
+= (cas
->end
- cas
->ptr
) - to_feed
;
1470 filled
= sz
- remaining
;
1474 /* feed one from upstream and have it emit into our buffer */
1475 to_feed
= input
? *isize_p
: 0;
1476 if (input
&& !to_feed
)
1478 remaining
= sizeof(cas
->buf
);
1479 if (stream_filter(cas
->one
,
1481 cas
->buf
, &remaining
))
1483 cas
->end
= sizeof(cas
->buf
) - remaining
;
1486 size_t fed
= *isize_p
- to_feed
;
1491 /* do we know that we drained one completely? */
1492 if (input
|| cas
->end
)
1495 /* tell two to drain; we have nothing more to give it */
1497 remaining
= sz
- filled
;
1498 if (stream_filter(cas
->two
,
1500 output
+ filled
, &remaining
))
1502 if (remaining
== (sz
- filled
))
1503 break; /* completely drained two */
1504 filled
= sz
- remaining
;
1510 static void cascade_free_fn(struct stream_filter
*filter
)
1512 struct cascade_filter
*cas
= (struct cascade_filter
*)filter
;
1513 free_stream_filter(cas
->one
);
1514 free_stream_filter(cas
->two
);
1518 static struct stream_filter_vtbl cascade_vtbl
= {
1523 static struct stream_filter
*cascade_filter(struct stream_filter
*one
,
1524 struct stream_filter
*two
)
1526 struct cascade_filter
*cascade
;
1528 if (!one
|| is_null_stream_filter(one
))
1530 if (!two
|| is_null_stream_filter(two
))
1533 cascade
= xmalloc(sizeof(*cascade
));
1536 cascade
->end
= cascade
->ptr
= 0;
1537 cascade
->filter
.vtbl
= &cascade_vtbl
;
1538 return (struct stream_filter
*)cascade
;
1544 #define IDENT_DRAINING (-1)
1545 #define IDENT_SKIPPING (-2)
1546 struct ident_filter
{
1547 struct stream_filter filter
;
1550 char ident
[45]; /* ": x40 $" */
1553 static int is_foreign_ident(const char *str
)
1557 if (!skip_prefix(str
, "$Id: ", &str
))
1559 for (i
= 0; str
[i
]; i
++) {
1560 if (isspace(str
[i
]) && str
[i
+1] != '$')
1566 static void ident_drain(struct ident_filter
*ident
, char **output_p
, size_t *osize_p
)
1568 size_t to_drain
= ident
->left
.len
;
1570 if (*osize_p
< to_drain
)
1571 to_drain
= *osize_p
;
1573 memcpy(*output_p
, ident
->left
.buf
, to_drain
);
1574 strbuf_remove(&ident
->left
, 0, to_drain
);
1575 *output_p
+= to_drain
;
1576 *osize_p
-= to_drain
;
1578 if (!ident
->left
.len
)
1582 static int ident_filter_fn(struct stream_filter
*filter
,
1583 const char *input
, size_t *isize_p
,
1584 char *output
, size_t *osize_p
)
1586 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1587 static const char head
[] = "$Id";
1590 /* drain upon eof */
1591 switch (ident
->state
) {
1593 strbuf_add(&ident
->left
, head
, ident
->state
);
1594 case IDENT_SKIPPING
:
1596 case IDENT_DRAINING
:
1597 ident_drain(ident
, &output
, osize_p
);
1602 while (*isize_p
|| (ident
->state
== IDENT_DRAINING
)) {
1605 if (ident
->state
== IDENT_DRAINING
) {
1606 ident_drain(ident
, &output
, osize_p
);
1615 if (ident
->state
== IDENT_SKIPPING
) {
1617 * Skipping until '$' or LF, but keeping them
1618 * in case it is a foreign ident.
1620 strbuf_addch(&ident
->left
, ch
);
1621 if (ch
!= '\n' && ch
!= '$')
1623 if (ch
== '$' && !is_foreign_ident(ident
->left
.buf
)) {
1624 strbuf_setlen(&ident
->left
, sizeof(head
) - 1);
1625 strbuf_addstr(&ident
->left
, ident
->ident
);
1627 ident
->state
= IDENT_DRAINING
;
1631 if (ident
->state
< sizeof(head
) &&
1632 head
[ident
->state
] == ch
) {
1638 strbuf_add(&ident
->left
, head
, ident
->state
);
1639 if (ident
->state
== sizeof(head
) - 1) {
1640 if (ch
!= ':' && ch
!= '$') {
1641 strbuf_addch(&ident
->left
, ch
);
1647 strbuf_addch(&ident
->left
, ch
);
1648 ident
->state
= IDENT_SKIPPING
;
1650 strbuf_addstr(&ident
->left
, ident
->ident
);
1651 ident
->state
= IDENT_DRAINING
;
1656 strbuf_addch(&ident
->left
, ch
);
1657 ident
->state
= IDENT_DRAINING
;
1662 static void ident_free_fn(struct stream_filter
*filter
)
1664 struct ident_filter
*ident
= (struct ident_filter
*)filter
;
1665 strbuf_release(&ident
->left
);
1669 static struct stream_filter_vtbl ident_vtbl
= {
1674 static struct stream_filter
*ident_filter(const unsigned char *sha1
)
1676 struct ident_filter
*ident
= xmalloc(sizeof(*ident
));
1678 xsnprintf(ident
->ident
, sizeof(ident
->ident
),
1679 ": %s $", sha1_to_hex(sha1
));
1680 strbuf_init(&ident
->left
, 0);
1681 ident
->filter
.vtbl
= &ident_vtbl
;
1683 return (struct stream_filter
*)ident
;
1687 * Return an appropriately constructed filter for the path, or NULL if
1688 * the contents cannot be filtered without reading the whole thing
1691 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1692 * large binary blob you would want us not to slurp into the memory!
1694 struct stream_filter
*get_stream_filter(const char *path
, const unsigned char *sha1
)
1696 struct conv_attrs ca
;
1697 struct stream_filter
*filter
= NULL
;
1699 convert_attrs(&ca
, path
);
1700 if (ca
.drv
&& (ca
.drv
->process
|| ca
.drv
->smudge
|| ca
.drv
->clean
))
1703 if (ca
.crlf_action
== CRLF_AUTO
|| ca
.crlf_action
== CRLF_AUTO_CRLF
)
1707 filter
= ident_filter(sha1
);
1709 if (output_eol(ca
.crlf_action
) == EOL_CRLF
)
1710 filter
= cascade_filter(filter
, lf_to_crlf_filter());
1712 filter
= cascade_filter(filter
, &null_filter_singleton
);
1717 void free_stream_filter(struct stream_filter
*filter
)
1719 filter
->vtbl
->free(filter
);
1722 int stream_filter(struct stream_filter
*filter
,
1723 const char *input
, size_t *isize_p
,
1724 char *output
, size_t *osize_p
)
1726 return filter
->vtbl
->filter(filter
, input
, isize_p
, output
, osize_p
);