1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
4 #include "run-command.h"
8 #include "sub-process.h"
11 * convert.c - convert a file when checking it out and checking it in.
13 * This should use the pathname to decide on whether it wants to do some
14 * more interesting conversions (automatic gzip/unzip, general format
15 * conversions etc etc), but by default it just does automatic CRLF<->LF
16 * translation when the "text" attribute or "auto_crlf" option is set.
19 /* Stat bits: When BIN is set, the txt bits are unset */
20 #define CONVERT_STAT_BITS_TXT_LF 0x1
21 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
22 #define CONVERT_STAT_BITS_BIN 0x4
36 /* NUL, CR, LF and CRLF counts */
37 unsigned nul, lonecr, lonelf, crlf;
39 /* These are just approximations! */
40 unsigned printable, nonprintable;
43 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
47 memset(stats, 0, sizeof(*stats));
49 for (i = 0; i < size; i++) {
50 unsigned char c = buf[i];
52 if (i+1 < size && buf[i+1] == '\n') {
65 stats->nonprintable++;
68 /* BS, HT, ESC and FF */
69 case '\b': case '\t': case '\033': case '\014':
76 stats->nonprintable++;
83 /* If file ends with EOF then don't count this EOF as non-printable. */
84 if (size >= 1 && buf[size-1] == '\032')
85 stats->nonprintable--;
89 * The same heuristics as diff.c::mmfile_is_binary()
90 * We treat files with bare CR as binary
92 static int convert_is_binary(unsigned long size, const struct text_stat *stats)
98 if ((stats->printable >> 7) < stats->nonprintable)
103 static unsigned int gather_convert_stats(const char *data, unsigned long size)
105 struct text_stat stats;
109 gather_stats(data, size, &stats);
110 if (convert_is_binary(size, &stats))
111 ret |= CONVERT_STAT_BITS_BIN;
113 ret |= CONVERT_STAT_BITS_TXT_CRLF;
115 ret |= CONVERT_STAT_BITS_TXT_LF;
120 static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
122 unsigned int convert_stats = gather_convert_stats(data, size);
124 if (convert_stats & CONVERT_STAT_BITS_BIN)
126 switch (convert_stats) {
127 case CONVERT_STAT_BITS_TXT_LF:
129 case CONVERT_STAT_BITS_TXT_CRLF:
131 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
138 const char *get_cached_convert_stats_ascii(const struct index_state *istate,
143 void *data = read_blob_data_from_index(istate, path, &sz);
144 ret = gather_convert_stats_ascii(data, sz);
149 const char *get_wt_convert_stats_ascii(const char *path)
151 const char *ret = "";
152 struct strbuf sb = STRBUF_INIT;
153 if (strbuf_read_file(&sb, path, 0) >= 0)
154 ret = gather_convert_stats_ascii(sb.buf, sb.len);
159 static int text_eol_is_crlf(void)
161 if (auto_crlf == AUTO_CRLF_TRUE)
163 else if (auto_crlf == AUTO_CRLF_INPUT)
165 if (core_eol == EOL_CRLF)
167 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
172 static enum eol output_eol(enum crlf_action crlf_action)
174 switch (crlf_action) {
179 case CRLF_TEXT_INPUT:
184 case CRLF_AUTO_INPUT:
189 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
191 warning("Illegal crlf_action %d\n", (int)crlf_action);
195 static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
196 struct text_stat *old_stats, struct text_stat *new_stats,
197 enum safe_crlf checksafe)
199 if (old_stats->crlf && !new_stats->crlf ) {
201 * CRLFs would not be restored by checkout
203 if (checksafe == SAFE_CRLF_WARN)
204 warning(_("CRLF will be replaced by LF in %s.\n"
205 "The file will have its original line"
206 " endings in your working directory."), path);
207 else /* i.e. SAFE_CRLF_FAIL */
208 die(_("CRLF would be replaced by LF in %s."), path);
209 } else if (old_stats->lonelf && !new_stats->lonelf ) {
211 * CRLFs would be added by checkout
213 if (checksafe == SAFE_CRLF_WARN)
214 warning(_("LF will be replaced by CRLF in %s.\n"
215 "The file will have its original line"
216 " endings in your working directory."), path);
217 else /* i.e. SAFE_CRLF_FAIL */
218 die(_("LF would be replaced by CRLF in %s"), path);
222 static int has_cr_in_index(const struct index_state *istate, const char *path)
228 data = read_blob_data_from_index(istate, path, &sz);
231 has_cr = memchr(data, '\r', sz) != NULL;
236 static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
237 enum crlf_action crlf_action)
239 if (output_eol(crlf_action) != EOL_CRLF)
241 /* No "naked" LF? Nothing to convert, regardless. */
245 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
246 /* If we have any CR or CRLF line endings, we do not touch it */
247 /* This is the new safer autocrlf-handling */
248 if (stats->lonecr || stats->crlf)
251 if (convert_is_binary(len, stats))
258 static int crlf_to_git(const struct index_state *istate,
259 const char *path, const char *src, size_t len,
261 enum crlf_action crlf_action, enum safe_crlf checksafe)
263 struct text_stat stats;
265 int convert_crlf_into_lf;
267 if (crlf_action == CRLF_BINARY ||
272 * If we are doing a dry-run and have no source buffer, there is
273 * nothing to analyze; we must assume we would convert.
278 gather_stats(src, len, &stats);
279 /* Optimization: No CRLF? Nothing to convert, regardless. */
280 convert_crlf_into_lf = !!stats.crlf;
282 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
283 if (convert_is_binary(len, &stats))
286 * If the file in the index has any CR in it, do not
287 * convert. This is the new safer autocrlf handling,
288 * unless we want to renormalize in a merge or
291 if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
292 has_cr_in_index(istate, path))
293 convert_crlf_into_lf = 0;
295 if ((checksafe == SAFE_CRLF_WARN ||
296 (checksafe == SAFE_CRLF_FAIL)) && len) {
297 struct text_stat new_stats;
298 memcpy(&new_stats, &stats, sizeof(new_stats));
299 /* simulate "git add" */
300 if (convert_crlf_into_lf) {
301 new_stats.lonelf += new_stats.crlf;
304 /* simulate "git checkout" */
305 if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) {
306 new_stats.crlf += new_stats.lonelf;
307 new_stats.lonelf = 0;
309 check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe);
311 if (!convert_crlf_into_lf)
315 * At this point all of our source analysis is done, and we are sure we
316 * would convert. If we are in dry-run mode, we can give an answer.
321 /* only grow if not in place */
322 if (strbuf_avail(buf) + buf->len < len)
323 strbuf_grow(buf, len - buf->len);
325 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
327 * If we guessed, we already know we rejected a file with
328 * lone CR, and we can strip a CR without looking at what
332 unsigned char c = *src++;
338 unsigned char c = *src++;
339 if (! (c == '\r' && (1 < len && *src == '\n')))
343 strbuf_setlen(buf, dst - buf->buf);
347 static int crlf_to_worktree(const char *path, const char *src, size_t len,
348 struct strbuf *buf, enum crlf_action crlf_action)
350 char *to_free = NULL;
351 struct text_stat stats;
353 if (!len || output_eol(crlf_action) != EOL_CRLF)
356 gather_stats(src, len, &stats);
357 if (!will_convert_lf_to_crlf(len, &stats, crlf_action))
360 /* are we "faking" in place editing ? */
362 to_free = strbuf_detach(buf, NULL);
364 strbuf_grow(buf, len + stats.lonelf);
366 const char *nl = memchr(src, '\n', len);
369 if (nl > src && nl[-1] == '\r') {
370 strbuf_add(buf, src, nl + 1 - src);
372 strbuf_add(buf, src, nl - src);
373 strbuf_addstr(buf, "\r\n");
378 strbuf_add(buf, src, len);
384 struct filter_params {
392 static int filter_buffer_or_fd(int in, int out, void *data)
395 * Spawn cmd and feed the buffer contents through its stdin.
397 struct child_process child_process = CHILD_PROCESS_INIT;
398 struct filter_params *params = (struct filter_params *)data;
399 int write_err, status;
400 const char *argv[] = { NULL, NULL };
402 /* apply % substitution to cmd */
403 struct strbuf cmd = STRBUF_INIT;
404 struct strbuf path = STRBUF_INIT;
405 struct strbuf_expand_dict_entry dict[] = {
410 /* quote the path to preserve spaces, etc. */
411 sq_quote_buf(&path, params->path);
412 dict[0].value = path.buf;
414 /* expand all %f with the quoted path */
415 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
416 strbuf_release(&path);
420 child_process.argv = argv;
421 child_process.use_shell = 1;
422 child_process.in = -1;
423 child_process.out = out;
425 if (start_command(&child_process))
426 return error("cannot fork to run external filter '%s'", params->cmd);
428 sigchain_push(SIGPIPE, SIG_IGN);
431 write_err = (write_in_full(child_process.in,
432 params->src, params->size) < 0);
436 write_err = copy_fd(params->fd, child_process.in);
437 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
441 if (close(child_process.in))
444 error("cannot feed the input to external filter '%s'", params->cmd);
446 sigchain_pop(SIGPIPE);
448 status = finish_command(&child_process);
450 error("external filter '%s' failed %d", params->cmd, status);
452 strbuf_release(&cmd);
453 return (write_err || status);
456 static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
457 struct strbuf *dst, const char *cmd)
460 * Create a pipeline to have the command filter the buffer's
463 * (child --> cmd) --> us
466 struct strbuf nbuf = STRBUF_INIT;
468 struct filter_params params;
470 memset(&async, 0, sizeof(async));
471 async.proc = filter_buffer_or_fd;
472 async.data = ¶ms;
481 if (start_async(&async))
482 return 0; /* error was already reported */
484 if (strbuf_read(&nbuf, async.out, len) < 0) {
485 err = error("read from external filter '%s' failed", cmd);
487 if (close(async.out)) {
488 err = error("read from external filter '%s' failed", cmd);
490 if (finish_async(&async)) {
491 err = error("external filter '%s' failed", cmd);
495 strbuf_swap(dst, &nbuf);
497 strbuf_release(&nbuf);
501 #define CAP_CLEAN (1u<<0)
502 #define CAP_SMUDGE (1u<<1)
505 struct subprocess_entry subprocess; /* must be the first member! */
506 unsigned int supported_capabilities;
509 static int subprocess_map_initialized;
510 static struct hashmap subprocess_map;
512 static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
515 struct cmd2process *entry = (struct cmd2process *)subprocess;
516 struct string_list cap_list = STRING_LIST_INIT_NODUP;
518 const char *cap_name;
519 struct child_process *process = &subprocess->process;
520 const char *cmd = subprocess->cmd;
522 sigchain_push(SIGPIPE, SIG_IGN);
524 err = packet_writel(process->in, "git-filter-client", "version=2", NULL);
528 err = strcmp(packet_read_line(process->out, NULL), "git-filter-server");
530 error("external filter '%s' does not support filter protocol version 2", cmd);
533 err = strcmp(packet_read_line(process->out, NULL), "version=2");
536 err = packet_read_line(process->out, NULL) != NULL;
540 err = packet_writel(process->in, "capability=clean", "capability=smudge", NULL);
543 cap_buf = packet_read_line(process->out, NULL);
546 string_list_split_in_place(&cap_list, cap_buf, '=', 1);
548 if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability"))
551 cap_name = cap_list.items[1].string;
552 if (!strcmp(cap_name, "clean")) {
553 entry->supported_capabilities |= CAP_CLEAN;
554 } else if (!strcmp(cap_name, "smudge")) {
555 entry->supported_capabilities |= CAP_SMUDGE;
558 "external filter '%s' requested unsupported filter capability '%s'",
563 string_list_clear(&cap_list, 0);
567 sigchain_pop(SIGPIPE);
572 static int apply_multi_file_filter(const char *path, const char *src, size_t len,
573 int fd, struct strbuf *dst, const char *cmd,
574 const unsigned int wanted_capability)
577 struct cmd2process *entry;
578 struct child_process *process;
579 struct strbuf nbuf = STRBUF_INIT;
580 struct strbuf filter_status = STRBUF_INIT;
581 const char *filter_type;
583 if (!subprocess_map_initialized) {
584 subprocess_map_initialized = 1;
585 hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
588 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
594 entry = xmalloc(sizeof(*entry));
595 entry->supported_capabilities = 0;
597 if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
602 process = &entry->subprocess.process;
604 if (!(wanted_capability & entry->supported_capabilities))
607 if (CAP_CLEAN & wanted_capability)
608 filter_type = "clean";
609 else if (CAP_SMUDGE & wanted_capability)
610 filter_type = "smudge";
612 die("unexpected filter type");
614 sigchain_push(SIGPIPE, SIG_IGN);
616 assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
617 err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
621 err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
623 error("path name too long for external filter");
627 err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
631 err = packet_flush_gently(process->in);
636 err = write_packetized_from_fd(fd, process->in);
638 err = write_packetized_from_buf(src, len, process->in);
642 err = subprocess_read_status(process->out, &filter_status);
646 err = strcmp(filter_status.buf, "success");
650 err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
654 err = subprocess_read_status(process->out, &filter_status);
658 err = strcmp(filter_status.buf, "success");
661 sigchain_pop(SIGPIPE);
664 if (!strcmp(filter_status.buf, "error")) {
665 /* The filter signaled a problem with the file. */
666 } else if (!strcmp(filter_status.buf, "abort")) {
668 * The filter signaled a permanent problem. Don't try to filter
669 * files with the same command for the lifetime of the current
672 entry->supported_capabilities &= ~wanted_capability;
675 * Something went wrong with the protocol filter.
676 * Force shutdown and restart if another blob requires filtering.
678 error("external filter '%s' failed", cmd);
679 subprocess_stop(&subprocess_map, &entry->subprocess);
683 strbuf_swap(dst, &nbuf);
685 strbuf_release(&nbuf);
689 static struct convert_driver {
691 struct convert_driver *next;
696 } *user_convert, **user_convert_tail;
698 static int apply_filter(const char *path, const char *src, size_t len,
699 int fd, struct strbuf *dst, struct convert_driver *drv,
700 const unsigned int wanted_capability)
702 const char *cmd = NULL;
710 if ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean)
712 else if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge)
716 return apply_single_file_filter(path, src, len, fd, dst, cmd);
717 else if (drv->process && *drv->process)
718 return apply_multi_file_filter(path, src, len, fd, dst, drv->process, wanted_capability);
723 static int read_convert_config(const char *var, const char *value, void *cb)
725 const char *key, *name;
727 struct convert_driver *drv;
730 * External conversion drivers are configured using
731 * "filter.<name>.variable".
733 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
735 for (drv = user_convert; drv; drv = drv->next)
736 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
739 drv = xcalloc(1, sizeof(struct convert_driver));
740 drv->name = xmemdupz(name, namelen);
741 *user_convert_tail = drv;
742 user_convert_tail = &(drv->next);
746 * filter.<name>.smudge and filter.<name>.clean specifies
751 * The command-line will not be interpolated in any way.
754 if (!strcmp("smudge", key))
755 return git_config_string(&drv->smudge, var, value);
757 if (!strcmp("clean", key))
758 return git_config_string(&drv->clean, var, value);
760 if (!strcmp("process", key))
761 return git_config_string(&drv->process, var, value);
763 if (!strcmp("required", key)) {
764 drv->required = git_config_bool(var, value);
771 static int count_ident(const char *cp, unsigned long size)
774 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
786 if (memcmp("Id", cp, 2))
797 * "$Id: ... "; scan up to the closing dollar sign and discard.
813 static int ident_to_git(const char *path, const char *src, size_t len,
814 struct strbuf *buf, int ident)
818 if (!ident || (src && !count_ident(src, len)))
824 /* only grow if not in place */
825 if (strbuf_avail(buf) + buf->len < len)
826 strbuf_grow(buf, len - buf->len);
829 dollar = memchr(src, '$', len);
832 memmove(dst, src, dollar + 1 - src);
833 dst += dollar + 1 - src;
834 len -= dollar + 1 - src;
837 if (len > 3 && !memcmp(src, "Id:", 3)) {
838 dollar = memchr(src + 3, '$', len - 3);
841 if (memchr(src + 3, '\n', dollar - src - 3)) {
842 /* Line break before the next dollar. */
846 memcpy(dst, "Id$", 3);
848 len -= dollar + 1 - src;
852 memmove(dst, src, len);
853 strbuf_setlen(buf, dst + len - buf->buf);
857 static int ident_to_worktree(const char *path, const char *src, size_t len,
858 struct strbuf *buf, int ident)
860 unsigned char sha1[20];
861 char *to_free = NULL, *dollar, *spc;
867 cnt = count_ident(src, len);
871 /* are we "faking" in place editing ? */
873 to_free = strbuf_detach(buf, NULL);
874 hash_sha1_file(src, len, "blob", sha1);
876 strbuf_grow(buf, len + cnt * 43);
878 /* step 1: run to the next '$' */
879 dollar = memchr(src, '$', len);
882 strbuf_add(buf, src, dollar + 1 - src);
883 len -= dollar + 1 - src;
886 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
887 if (len < 3 || memcmp("Id", src, 2))
890 /* step 3: skip over Id$ or Id:xxxxx$ */
894 } else if (src[2] == ':') {
896 * It's possible that an expanded Id has crept its way into the
897 * repository, we cope with that by stripping the expansion out.
898 * This is probably not a good idea, since it will cause changes
899 * on checkout, which won't go away by stash, but let's keep it
902 dollar = memchr(src + 3, '$', len - 3);
904 /* incomplete keyword, no more '$', so just quit the loop */
908 if (memchr(src + 3, '\n', dollar - src - 3)) {
909 /* Line break before the next dollar. */
913 spc = memchr(src + 4, ' ', dollar - src - 4);
914 if (spc && spc < dollar-1) {
915 /* There are spaces in unexpected places.
916 * This is probably an id from some other
917 * versioning system. Keep it for now.
922 len -= dollar + 1 - src;
925 /* it wasn't a "Id$" or "Id:xxxx$" */
929 /* step 4: substitute */
930 strbuf_addstr(buf, "Id: ");
931 strbuf_add(buf, sha1_to_hex(sha1), 40);
932 strbuf_addstr(buf, " $");
934 strbuf_add(buf, src, len);
940 static enum crlf_action git_path_check_crlf(struct attr_check_item *check)
942 const char *value = check->value;
944 if (ATTR_TRUE(value))
946 else if (ATTR_FALSE(value))
948 else if (ATTR_UNSET(value))
950 else if (!strcmp(value, "input"))
951 return CRLF_TEXT_INPUT;
952 else if (!strcmp(value, "auto"))
954 return CRLF_UNDEFINED;
957 static enum eol git_path_check_eol(struct attr_check_item *check)
959 const char *value = check->value;
961 if (ATTR_UNSET(value))
963 else if (!strcmp(value, "lf"))
965 else if (!strcmp(value, "crlf"))
970 static struct convert_driver *git_path_check_convert(struct attr_check_item *check)
972 const char *value = check->value;
973 struct convert_driver *drv;
975 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
977 for (drv = user_convert; drv; drv = drv->next)
978 if (!strcmp(value, drv->name))
983 static int git_path_check_ident(struct attr_check_item *check)
985 const char *value = check->value;
987 return !!ATTR_TRUE(value);
991 struct convert_driver *drv;
992 enum crlf_action attr_action; /* What attr says */
993 enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
997 static void convert_attrs(struct conv_attrs *ca, const char *path)
999 static struct attr_check *check;
1002 check = attr_check_initl("crlf", "ident", "filter",
1003 "eol", "text", NULL);
1004 user_convert_tail = &user_convert;
1005 git_config(read_convert_config, NULL);
1008 if (!git_check_attr(path, check)) {
1009 struct attr_check_item *ccheck = check->items;
1010 ca->crlf_action = git_path_check_crlf(ccheck + 4);
1011 if (ca->crlf_action == CRLF_UNDEFINED)
1012 ca->crlf_action = git_path_check_crlf(ccheck + 0);
1013 ca->attr_action = ca->crlf_action;
1014 ca->ident = git_path_check_ident(ccheck + 1);
1015 ca->drv = git_path_check_convert(ccheck + 2);
1016 if (ca->crlf_action != CRLF_BINARY) {
1017 enum eol eol_attr = git_path_check_eol(ccheck + 3);
1018 if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
1019 ca->crlf_action = CRLF_AUTO_INPUT;
1020 else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
1021 ca->crlf_action = CRLF_AUTO_CRLF;
1022 else if (eol_attr == EOL_LF)
1023 ca->crlf_action = CRLF_TEXT_INPUT;
1024 else if (eol_attr == EOL_CRLF)
1025 ca->crlf_action = CRLF_TEXT_CRLF;
1027 ca->attr_action = ca->crlf_action;
1030 ca->crlf_action = CRLF_UNDEFINED;
1033 if (ca->crlf_action == CRLF_TEXT)
1034 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
1035 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
1036 ca->crlf_action = CRLF_BINARY;
1037 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
1038 ca->crlf_action = CRLF_AUTO_CRLF;
1039 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
1040 ca->crlf_action = CRLF_AUTO_INPUT;
1043 int would_convert_to_git_filter_fd(const char *path)
1045 struct conv_attrs ca;
1047 convert_attrs(&ca, path);
1052 * Apply a filter to an fd only if the filter is required to succeed.
1053 * We must die if the filter fails, because the original data before
1054 * filtering is not available.
1056 if (!ca.drv->required)
1059 return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN);
1062 const char *get_convert_attr_ascii(const char *path)
1064 struct conv_attrs ca;
1066 convert_attrs(&ca, path);
1067 switch (ca.attr_action) {
1068 case CRLF_UNDEFINED:
1074 case CRLF_TEXT_INPUT:
1075 return "text eol=lf";
1076 case CRLF_TEXT_CRLF:
1077 return "text eol=crlf";
1080 case CRLF_AUTO_CRLF:
1081 return "text=auto eol=crlf";
1082 case CRLF_AUTO_INPUT:
1083 return "text=auto eol=lf";
1088 int convert_to_git(const struct index_state *istate,
1089 const char *path, const char *src, size_t len,
1090 struct strbuf *dst, enum safe_crlf checksafe)
1093 struct conv_attrs ca;
1095 convert_attrs(&ca, path);
1097 ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN);
1098 if (!ret && ca.drv && ca.drv->required)
1099 die("%s: clean filter '%s' failed", path, ca.drv->name);
1105 ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
1110 return ret | ident_to_git(path, src, len, dst, ca.ident);
1113 void convert_to_git_filter_fd(const struct index_state *istate,
1114 const char *path, int fd, struct strbuf *dst,
1115 enum safe_crlf checksafe)
1117 struct conv_attrs ca;
1118 convert_attrs(&ca, path);
1121 assert(ca.drv->clean || ca.drv->process);
1123 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
1124 die("%s: clean filter '%s' failed", path, ca.drv->name);
1126 crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
1127 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
1130 static int convert_to_working_tree_internal(const char *path, const char *src,
1131 size_t len, struct strbuf *dst,
1134 int ret = 0, ret_filter = 0;
1135 struct conv_attrs ca;
1137 convert_attrs(&ca, path);
1139 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
1145 * CRLF conversion can be skipped if normalizing, unless there
1146 * is a smudge or process filter (even if the process filter doesn't
1147 * support smudge). The filters might expect CRLFs.
1149 if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
1150 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
1157 ret_filter = apply_filter(path, src, len, -1, dst, ca.drv, CAP_SMUDGE);
1158 if (!ret_filter && ca.drv && ca.drv->required)
1159 die("%s: smudge filter %s failed", path, ca.drv->name);
1161 return ret | ret_filter;
1164 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
1166 return convert_to_working_tree_internal(path, src, len, dst, 0);
1169 int renormalize_buffer(const struct index_state *istate, const char *path,
1170 const char *src, size_t len, struct strbuf *dst)
1172 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
1177 return ret | convert_to_git(istate, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
1180 /*****************************************************************
1182 * Streaming conversion support
1184 *****************************************************************/
1186 typedef int (*filter_fn)(struct stream_filter *,
1187 const char *input, size_t *isize_p,
1188 char *output, size_t *osize_p);
1189 typedef void (*free_fn)(struct stream_filter *);
1191 struct stream_filter_vtbl {
1196 struct stream_filter {
1197 struct stream_filter_vtbl *vtbl;
1200 static int null_filter_fn(struct stream_filter *filter,
1201 const char *input, size_t *isize_p,
1202 char *output, size_t *osize_p)
1207 return 0; /* we do not keep any states */
1209 if (*osize_p < count)
1212 memmove(output, input, count);
1219 static void null_free_fn(struct stream_filter *filter)
1221 ; /* nothing -- null instances are shared */
1224 static struct stream_filter_vtbl null_vtbl = {
1229 static struct stream_filter null_filter_singleton = {
1233 int is_null_stream_filter(struct stream_filter *filter)
1235 return filter == &null_filter_singleton;
1243 struct lf_to_crlf_filter {
1244 struct stream_filter filter;
1245 unsigned has_held:1;
1249 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1250 const char *input, size_t *isize_p,
1251 char *output, size_t *osize_p)
1253 size_t count, o = 0;
1254 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1257 * We may be holding onto the CR to see if it is followed by a
1258 * LF, in which case we would need to go to the main loop.
1259 * Otherwise, just emit it to the output stream.
1261 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1262 output[o++] = lf_to_crlf->held;
1263 lf_to_crlf->has_held = 0;
1266 /* We are told to drain */
1273 if (count || lf_to_crlf->has_held) {
1277 if (lf_to_crlf->has_held) {
1279 lf_to_crlf->has_held = 0;
1282 for (i = 0; o < *osize_p && i < count; i++) {
1287 } else if (was_cr) {
1289 * Previous round saw CR and it is not followed
1290 * by a LF; emit the CR before processing the
1291 * current character.
1297 * We may have consumed the last output slot,
1298 * in which case we need to break out of this
1299 * loop; hold the current character before
1302 if (*osize_p <= o) {
1303 lf_to_crlf->has_held = 1;
1304 lf_to_crlf->held = ch;
1305 continue; /* break but increment i */
1320 if (!lf_to_crlf->has_held && was_cr) {
1321 lf_to_crlf->has_held = 1;
1322 lf_to_crlf->held = '\r';
1328 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1333 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1334 lf_to_crlf_filter_fn,
1338 static struct stream_filter *lf_to_crlf_filter(void)
1340 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1342 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1343 return (struct stream_filter *)lf_to_crlf;
1349 #define FILTER_BUFFER 1024
1350 struct cascade_filter {
1351 struct stream_filter filter;
1352 struct stream_filter *one;
1353 struct stream_filter *two;
1354 char buf[FILTER_BUFFER];
1358 static int cascade_filter_fn(struct stream_filter *filter,
1359 const char *input, size_t *isize_p,
1360 char *output, size_t *osize_p)
1362 struct cascade_filter *cas = (struct cascade_filter *) filter;
1364 size_t sz = *osize_p;
1365 size_t to_feed, remaining;
1368 * input -- (one) --> buf -- (two) --> output
1370 while (filled < sz) {
1371 remaining = sz - filled;
1373 /* do we already have something to feed two with? */
1374 if (cas->ptr < cas->end) {
1375 to_feed = cas->end - cas->ptr;
1376 if (stream_filter(cas->two,
1377 cas->buf + cas->ptr, &to_feed,
1378 output + filled, &remaining))
1380 cas->ptr += (cas->end - cas->ptr) - to_feed;
1381 filled = sz - remaining;
1385 /* feed one from upstream and have it emit into our buffer */
1386 to_feed = input ? *isize_p : 0;
1387 if (input && !to_feed)
1389 remaining = sizeof(cas->buf);
1390 if (stream_filter(cas->one,
1392 cas->buf, &remaining))
1394 cas->end = sizeof(cas->buf) - remaining;
1397 size_t fed = *isize_p - to_feed;
1402 /* do we know that we drained one completely? */
1403 if (input || cas->end)
1406 /* tell two to drain; we have nothing more to give it */
1408 remaining = sz - filled;
1409 if (stream_filter(cas->two,
1411 output + filled, &remaining))
1413 if (remaining == (sz - filled))
1414 break; /* completely drained two */
1415 filled = sz - remaining;
1421 static void cascade_free_fn(struct stream_filter *filter)
1423 struct cascade_filter *cas = (struct cascade_filter *)filter;
1424 free_stream_filter(cas->one);
1425 free_stream_filter(cas->two);
1429 static struct stream_filter_vtbl cascade_vtbl = {
1434 static struct stream_filter *cascade_filter(struct stream_filter *one,
1435 struct stream_filter *two)
1437 struct cascade_filter *cascade;
1439 if (!one || is_null_stream_filter(one))
1441 if (!two || is_null_stream_filter(two))
1444 cascade = xmalloc(sizeof(*cascade));
1447 cascade->end = cascade->ptr = 0;
1448 cascade->filter.vtbl = &cascade_vtbl;
1449 return (struct stream_filter *)cascade;
1455 #define IDENT_DRAINING (-1)
1456 #define IDENT_SKIPPING (-2)
1457 struct ident_filter {
1458 struct stream_filter filter;
1461 char ident[45]; /* ": x40 $" */
1464 static int is_foreign_ident(const char *str)
1468 if (!skip_prefix(str, "$Id: ", &str))
1470 for (i = 0; str[i]; i++) {
1471 if (isspace(str[i]) && str[i+1] != '$')
1477 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1479 size_t to_drain = ident->left.len;
1481 if (*osize_p < to_drain)
1482 to_drain = *osize_p;
1484 memcpy(*output_p, ident->left.buf, to_drain);
1485 strbuf_remove(&ident->left, 0, to_drain);
1486 *output_p += to_drain;
1487 *osize_p -= to_drain;
1489 if (!ident->left.len)
1493 static int ident_filter_fn(struct stream_filter *filter,
1494 const char *input, size_t *isize_p,
1495 char *output, size_t *osize_p)
1497 struct ident_filter *ident = (struct ident_filter *)filter;
1498 static const char head[] = "$Id";
1501 /* drain upon eof */
1502 switch (ident->state) {
1504 strbuf_add(&ident->left, head, ident->state);
1505 case IDENT_SKIPPING:
1507 case IDENT_DRAINING:
1508 ident_drain(ident, &output, osize_p);
1513 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1516 if (ident->state == IDENT_DRAINING) {
1517 ident_drain(ident, &output, osize_p);
1526 if (ident->state == IDENT_SKIPPING) {
1528 * Skipping until '$' or LF, but keeping them
1529 * in case it is a foreign ident.
1531 strbuf_addch(&ident->left, ch);
1532 if (ch != '\n' && ch != '$')
1534 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1535 strbuf_setlen(&ident->left, sizeof(head) - 1);
1536 strbuf_addstr(&ident->left, ident->ident);
1538 ident->state = IDENT_DRAINING;
1542 if (ident->state < sizeof(head) &&
1543 head[ident->state] == ch) {
1549 strbuf_add(&ident->left, head, ident->state);
1550 if (ident->state == sizeof(head) - 1) {
1551 if (ch != ':' && ch != '$') {
1552 strbuf_addch(&ident->left, ch);
1558 strbuf_addch(&ident->left, ch);
1559 ident->state = IDENT_SKIPPING;
1561 strbuf_addstr(&ident->left, ident->ident);
1562 ident->state = IDENT_DRAINING;
1567 strbuf_addch(&ident->left, ch);
1568 ident->state = IDENT_DRAINING;
1573 static void ident_free_fn(struct stream_filter *filter)
1575 struct ident_filter *ident = (struct ident_filter *)filter;
1576 strbuf_release(&ident->left);
1580 static struct stream_filter_vtbl ident_vtbl = {
1585 static struct stream_filter *ident_filter(const unsigned char *sha1)
1587 struct ident_filter *ident = xmalloc(sizeof(*ident));
1589 xsnprintf(ident->ident, sizeof(ident->ident),
1590 ": %s $", sha1_to_hex(sha1));
1591 strbuf_init(&ident->left, 0);
1592 ident->filter.vtbl = &ident_vtbl;
1594 return (struct stream_filter *)ident;
1598 * Return an appropriately constructed filter for the path, or NULL if
1599 * the contents cannot be filtered without reading the whole thing
1602 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1603 * large binary blob you would want us not to slurp into the memory!
1605 struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1607 struct conv_attrs ca;
1608 struct stream_filter *filter = NULL;
1610 convert_attrs(&ca, path);
1611 if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
1614 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1618 filter = ident_filter(sha1);
1620 if (output_eol(ca.crlf_action) == EOL_CRLF)
1621 filter = cascade_filter(filter, lf_to_crlf_filter());
1623 filter = cascade_filter(filter, &null_filter_singleton);
1628 void free_stream_filter(struct stream_filter *filter)
1630 filter->vtbl->free(filter);
1633 int stream_filter(struct stream_filter *filter,
1634 const char *input, size_t *isize_p,
1635 char *output, size_t *osize_p)
1637 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);