Merge branch 'jk/submodule-fix-loose' into maint-2.13
[git.git] / convert.c
blob8d652bf27c9444d3696c4b942dc85f062e2bf53e
1 #include "cache.h"
2 #include "attr.h"
3 #include "run-command.h"
4 #include "quote.h"
5 #include "sigchain.h"
6 #include "pkt-line.h"
8 /*
9 * convert.c - convert a file when checking it out and checking it in.
11 * This should use the pathname to decide on whether it wants to do some
12 * more interesting conversions (automatic gzip/unzip, general format
13 * conversions etc etc), but by default it just does automatic CRLF<->LF
14 * translation when the "text" attribute or "auto_crlf" option is set.
17 /* Stat bits: When BIN is set, the txt bits are unset */
18 #define CONVERT_STAT_BITS_TXT_LF 0x1
19 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
20 #define CONVERT_STAT_BITS_BIN 0x4
22 enum crlf_action {
23 CRLF_UNDEFINED,
24 CRLF_BINARY,
25 CRLF_TEXT,
26 CRLF_TEXT_INPUT,
27 CRLF_TEXT_CRLF,
28 CRLF_AUTO,
29 CRLF_AUTO_INPUT,
30 CRLF_AUTO_CRLF
33 struct text_stat {
34 /* NUL, CR, LF and CRLF counts */
35 unsigned nul, lonecr, lonelf, crlf;
37 /* These are just approximations! */
38 unsigned printable, nonprintable;
41 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
43 unsigned long i;
45 memset(stats, 0, sizeof(*stats));
47 for (i = 0; i < size; i++) {
48 unsigned char c = buf[i];
49 if (c == '\r') {
50 if (i+1 < size && buf[i+1] == '\n') {
51 stats->crlf++;
52 i++;
53 } else
54 stats->lonecr++;
55 continue;
57 if (c == '\n') {
58 stats->lonelf++;
59 continue;
61 if (c == 127)
62 /* DEL */
63 stats->nonprintable++;
64 else if (c < 32) {
65 switch (c) {
66 /* BS, HT, ESC and FF */
67 case '\b': case '\t': case '\033': case '\014':
68 stats->printable++;
69 break;
70 case 0:
71 stats->nul++;
72 /* fall through */
73 default:
74 stats->nonprintable++;
77 else
78 stats->printable++;
81 /* If file ends with EOF then don't count this EOF as non-printable. */
82 if (size >= 1 && buf[size-1] == '\032')
83 stats->nonprintable--;
87 * The same heuristics as diff.c::mmfile_is_binary()
88 * We treat files with bare CR as binary
90 static int convert_is_binary(unsigned long size, const struct text_stat *stats)
92 if (stats->lonecr)
93 return 1;
94 if (stats->nul)
95 return 1;
96 if ((stats->printable >> 7) < stats->nonprintable)
97 return 1;
98 return 0;
101 static unsigned int gather_convert_stats(const char *data, unsigned long size)
103 struct text_stat stats;
104 int ret = 0;
105 if (!data || !size)
106 return 0;
107 gather_stats(data, size, &stats);
108 if (convert_is_binary(size, &stats))
109 ret |= CONVERT_STAT_BITS_BIN;
110 if (stats.crlf)
111 ret |= CONVERT_STAT_BITS_TXT_CRLF;
112 if (stats.lonelf)
113 ret |= CONVERT_STAT_BITS_TXT_LF;
115 return ret;
118 static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
120 unsigned int convert_stats = gather_convert_stats(data, size);
122 if (convert_stats & CONVERT_STAT_BITS_BIN)
123 return "-text";
124 switch (convert_stats) {
125 case CONVERT_STAT_BITS_TXT_LF:
126 return "lf";
127 case CONVERT_STAT_BITS_TXT_CRLF:
128 return "crlf";
129 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
130 return "mixed";
131 default:
132 return "none";
136 const char *get_cached_convert_stats_ascii(const char *path)
138 const char *ret;
139 unsigned long sz;
140 void *data = read_blob_data_from_cache(path, &sz);
141 ret = gather_convert_stats_ascii(data, sz);
142 free(data);
143 return ret;
146 const char *get_wt_convert_stats_ascii(const char *path)
148 const char *ret = "";
149 struct strbuf sb = STRBUF_INIT;
150 if (strbuf_read_file(&sb, path, 0) >= 0)
151 ret = gather_convert_stats_ascii(sb.buf, sb.len);
152 strbuf_release(&sb);
153 return ret;
156 static int text_eol_is_crlf(void)
158 if (auto_crlf == AUTO_CRLF_TRUE)
159 return 1;
160 else if (auto_crlf == AUTO_CRLF_INPUT)
161 return 0;
162 if (core_eol == EOL_CRLF)
163 return 1;
164 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
165 return 1;
166 return 0;
169 static enum eol output_eol(enum crlf_action crlf_action)
171 switch (crlf_action) {
172 case CRLF_BINARY:
173 return EOL_UNSET;
174 case CRLF_TEXT_CRLF:
175 return EOL_CRLF;
176 case CRLF_TEXT_INPUT:
177 return EOL_LF;
178 case CRLF_UNDEFINED:
179 case CRLF_AUTO_CRLF:
180 return EOL_CRLF;
181 case CRLF_AUTO_INPUT:
182 return EOL_LF;
183 case CRLF_TEXT:
184 case CRLF_AUTO:
185 /* fall through */
186 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
188 warning("Illegal crlf_action %d\n", (int)crlf_action);
189 return core_eol;
192 static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
193 struct text_stat *old_stats, struct text_stat *new_stats,
194 enum safe_crlf checksafe)
196 if (old_stats->crlf && !new_stats->crlf ) {
198 * CRLFs would not be restored by checkout
200 if (checksafe == SAFE_CRLF_WARN)
201 warning(_("CRLF will be replaced by LF in %s.\n"
202 "The file will have its original line"
203 " endings in your working directory."), path);
204 else /* i.e. SAFE_CRLF_FAIL */
205 die(_("CRLF would be replaced by LF in %s."), path);
206 } else if (old_stats->lonelf && !new_stats->lonelf ) {
208 * CRLFs would be added by checkout
210 if (checksafe == SAFE_CRLF_WARN)
211 warning(_("LF will be replaced by CRLF in %s.\n"
212 "The file will have its original line"
213 " endings in your working directory."), path);
214 else /* i.e. SAFE_CRLF_FAIL */
215 die(_("LF would be replaced by CRLF in %s"), path);
219 static int has_cr_in_index(const char *path)
221 unsigned long sz;
222 void *data;
223 int has_cr;
225 data = read_blob_data_from_cache(path, &sz);
226 if (!data)
227 return 0;
228 has_cr = memchr(data, '\r', sz) != NULL;
229 free(data);
230 return has_cr;
233 static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
234 enum crlf_action crlf_action)
236 if (output_eol(crlf_action) != EOL_CRLF)
237 return 0;
238 /* No "naked" LF? Nothing to convert, regardless. */
239 if (!stats->lonelf)
240 return 0;
242 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
243 /* If we have any CR or CRLF line endings, we do not touch it */
244 /* This is the new safer autocrlf-handling */
245 if (stats->lonecr || stats->crlf)
246 return 0;
248 if (convert_is_binary(len, stats))
249 return 0;
251 return 1;
255 static int crlf_to_git(const char *path, const char *src, size_t len,
256 struct strbuf *buf,
257 enum crlf_action crlf_action, enum safe_crlf checksafe)
259 struct text_stat stats;
260 char *dst;
261 int convert_crlf_into_lf;
263 if (crlf_action == CRLF_BINARY ||
264 (src && !len))
265 return 0;
268 * If we are doing a dry-run and have no source buffer, there is
269 * nothing to analyze; we must assume we would convert.
271 if (!buf && !src)
272 return 1;
274 gather_stats(src, len, &stats);
275 /* Optimization: No CRLF? Nothing to convert, regardless. */
276 convert_crlf_into_lf = !!stats.crlf;
278 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
279 if (convert_is_binary(len, &stats))
280 return 0;
282 * If the file in the index has any CR in it, do not
283 * convert. This is the new safer autocrlf handling,
284 * unless we want to renormalize in a merge or
285 * cherry-pick.
287 if ((checksafe != SAFE_CRLF_RENORMALIZE) && has_cr_in_index(path))
288 convert_crlf_into_lf = 0;
290 if ((checksafe == SAFE_CRLF_WARN ||
291 (checksafe == SAFE_CRLF_FAIL)) && len) {
292 struct text_stat new_stats;
293 memcpy(&new_stats, &stats, sizeof(new_stats));
294 /* simulate "git add" */
295 if (convert_crlf_into_lf) {
296 new_stats.lonelf += new_stats.crlf;
297 new_stats.crlf = 0;
299 /* simulate "git checkout" */
300 if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) {
301 new_stats.crlf += new_stats.lonelf;
302 new_stats.lonelf = 0;
304 check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe);
306 if (!convert_crlf_into_lf)
307 return 0;
310 * At this point all of our source analysis is done, and we are sure we
311 * would convert. If we are in dry-run mode, we can give an answer.
313 if (!buf)
314 return 1;
316 /* only grow if not in place */
317 if (strbuf_avail(buf) + buf->len < len)
318 strbuf_grow(buf, len - buf->len);
319 dst = buf->buf;
320 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
322 * If we guessed, we already know we rejected a file with
323 * lone CR, and we can strip a CR without looking at what
324 * follow it.
326 do {
327 unsigned char c = *src++;
328 if (c != '\r')
329 *dst++ = c;
330 } while (--len);
331 } else {
332 do {
333 unsigned char c = *src++;
334 if (! (c == '\r' && (1 < len && *src == '\n')))
335 *dst++ = c;
336 } while (--len);
338 strbuf_setlen(buf, dst - buf->buf);
339 return 1;
342 static int crlf_to_worktree(const char *path, const char *src, size_t len,
343 struct strbuf *buf, enum crlf_action crlf_action)
345 char *to_free = NULL;
346 struct text_stat stats;
348 if (!len || output_eol(crlf_action) != EOL_CRLF)
349 return 0;
351 gather_stats(src, len, &stats);
352 if (!will_convert_lf_to_crlf(len, &stats, crlf_action))
353 return 0;
355 /* are we "faking" in place editing ? */
356 if (src == buf->buf)
357 to_free = strbuf_detach(buf, NULL);
359 strbuf_grow(buf, len + stats.lonelf);
360 for (;;) {
361 const char *nl = memchr(src, '\n', len);
362 if (!nl)
363 break;
364 if (nl > src && nl[-1] == '\r') {
365 strbuf_add(buf, src, nl + 1 - src);
366 } else {
367 strbuf_add(buf, src, nl - src);
368 strbuf_addstr(buf, "\r\n");
370 len -= nl + 1 - src;
371 src = nl + 1;
373 strbuf_add(buf, src, len);
375 free(to_free);
376 return 1;
379 struct filter_params {
380 const char *src;
381 unsigned long size;
382 int fd;
383 const char *cmd;
384 const char *path;
387 static int filter_buffer_or_fd(int in, int out, void *data)
390 * Spawn cmd and feed the buffer contents through its stdin.
392 struct child_process child_process = CHILD_PROCESS_INIT;
393 struct filter_params *params = (struct filter_params *)data;
394 int write_err, status;
395 const char *argv[] = { NULL, NULL };
397 /* apply % substitution to cmd */
398 struct strbuf cmd = STRBUF_INIT;
399 struct strbuf path = STRBUF_INIT;
400 struct strbuf_expand_dict_entry dict[] = {
401 { "f", NULL, },
402 { NULL, NULL, },
405 /* quote the path to preserve spaces, etc. */
406 sq_quote_buf(&path, params->path);
407 dict[0].value = path.buf;
409 /* expand all %f with the quoted path */
410 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
411 strbuf_release(&path);
413 argv[0] = cmd.buf;
415 child_process.argv = argv;
416 child_process.use_shell = 1;
417 child_process.in = -1;
418 child_process.out = out;
420 if (start_command(&child_process))
421 return error("cannot fork to run external filter '%s'", params->cmd);
423 sigchain_push(SIGPIPE, SIG_IGN);
425 if (params->src) {
426 write_err = (write_in_full(child_process.in,
427 params->src, params->size) < 0);
428 if (errno == EPIPE)
429 write_err = 0;
430 } else {
431 write_err = copy_fd(params->fd, child_process.in);
432 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
433 write_err = 0;
436 if (close(child_process.in))
437 write_err = 1;
438 if (write_err)
439 error("cannot feed the input to external filter '%s'", params->cmd);
441 sigchain_pop(SIGPIPE);
443 status = finish_command(&child_process);
444 if (status)
445 error("external filter '%s' failed %d", params->cmd, status);
447 strbuf_release(&cmd);
448 return (write_err || status);
451 static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
452 struct strbuf *dst, const char *cmd)
455 * Create a pipeline to have the command filter the buffer's
456 * contents.
458 * (child --> cmd) --> us
460 int err = 0;
461 struct strbuf nbuf = STRBUF_INIT;
462 struct async async;
463 struct filter_params params;
465 memset(&async, 0, sizeof(async));
466 async.proc = filter_buffer_or_fd;
467 async.data = &params;
468 async.out = -1;
469 params.src = src;
470 params.size = len;
471 params.fd = fd;
472 params.cmd = cmd;
473 params.path = path;
475 fflush(NULL);
476 if (start_async(&async))
477 return 0; /* error was already reported */
479 if (strbuf_read(&nbuf, async.out, len) < 0) {
480 err = error("read from external filter '%s' failed", cmd);
482 if (close(async.out)) {
483 err = error("read from external filter '%s' failed", cmd);
485 if (finish_async(&async)) {
486 err = error("external filter '%s' failed", cmd);
489 if (!err) {
490 strbuf_swap(dst, &nbuf);
492 strbuf_release(&nbuf);
493 return !err;
496 #define CAP_CLEAN (1u<<0)
497 #define CAP_SMUDGE (1u<<1)
499 struct cmd2process {
500 struct hashmap_entry ent; /* must be the first member! */
501 unsigned int supported_capabilities;
502 const char *cmd;
503 struct child_process process;
506 static int cmd_process_map_initialized;
507 static struct hashmap cmd_process_map;
509 static int cmd2process_cmp(const struct cmd2process *e1,
510 const struct cmd2process *e2,
511 const void *unused)
513 return strcmp(e1->cmd, e2->cmd);
516 static struct cmd2process *find_multi_file_filter_entry(struct hashmap *hashmap, const char *cmd)
518 struct cmd2process key;
519 hashmap_entry_init(&key, strhash(cmd));
520 key.cmd = cmd;
521 return hashmap_get(hashmap, &key, NULL);
524 static int packet_write_list(int fd, const char *line, ...)
526 va_list args;
527 int err;
528 va_start(args, line);
529 for (;;) {
530 if (!line)
531 break;
532 if (strlen(line) > LARGE_PACKET_DATA_MAX)
533 return -1;
534 err = packet_write_fmt_gently(fd, "%s\n", line);
535 if (err)
536 return err;
537 line = va_arg(args, const char*);
539 va_end(args);
540 return packet_flush_gently(fd);
543 static void read_multi_file_filter_status(int fd, struct strbuf *status)
545 struct strbuf **pair;
546 char *line;
547 for (;;) {
548 line = packet_read_line(fd, NULL);
549 if (!line)
550 break;
551 pair = strbuf_split_str(line, '=', 2);
552 if (pair[0] && pair[0]->len && pair[1]) {
553 /* the last "status=<foo>" line wins */
554 if (!strcmp(pair[0]->buf, "status=")) {
555 strbuf_reset(status);
556 strbuf_addbuf(status, pair[1]);
559 strbuf_list_free(pair);
563 static void kill_multi_file_filter(struct hashmap *hashmap, struct cmd2process *entry)
565 if (!entry)
566 return;
568 entry->process.clean_on_exit = 0;
569 kill(entry->process.pid, SIGTERM);
570 finish_command(&entry->process);
572 hashmap_remove(hashmap, entry, NULL);
573 free(entry);
576 static void stop_multi_file_filter(struct child_process *process)
578 sigchain_push(SIGPIPE, SIG_IGN);
579 /* Closing the pipe signals the filter to initiate a shutdown. */
580 close(process->in);
581 close(process->out);
582 sigchain_pop(SIGPIPE);
583 /* Finish command will wait until the shutdown is complete. */
584 finish_command(process);
587 static struct cmd2process *start_multi_file_filter(struct hashmap *hashmap, const char *cmd)
589 int err;
590 struct cmd2process *entry;
591 struct child_process *process;
592 const char *argv[] = { cmd, NULL };
593 struct string_list cap_list = STRING_LIST_INIT_NODUP;
594 char *cap_buf;
595 const char *cap_name;
597 entry = xmalloc(sizeof(*entry));
598 entry->cmd = cmd;
599 entry->supported_capabilities = 0;
600 process = &entry->process;
602 child_process_init(process);
603 process->argv = argv;
604 process->use_shell = 1;
605 process->in = -1;
606 process->out = -1;
607 process->clean_on_exit = 1;
608 process->clean_on_exit_handler = stop_multi_file_filter;
610 if (start_command(process)) {
611 error("cannot fork to run external filter '%s'", cmd);
612 return NULL;
615 hashmap_entry_init(entry, strhash(cmd));
617 sigchain_push(SIGPIPE, SIG_IGN);
619 err = packet_write_list(process->in, "git-filter-client", "version=2", NULL);
620 if (err)
621 goto done;
623 err = strcmp(packet_read_line(process->out, NULL), "git-filter-server");
624 if (err) {
625 error("external filter '%s' does not support filter protocol version 2", cmd);
626 goto done;
628 err = strcmp(packet_read_line(process->out, NULL), "version=2");
629 if (err)
630 goto done;
631 err = packet_read_line(process->out, NULL) != NULL;
632 if (err)
633 goto done;
635 err = packet_write_list(process->in, "capability=clean", "capability=smudge", NULL);
637 for (;;) {
638 cap_buf = packet_read_line(process->out, NULL);
639 if (!cap_buf)
640 break;
641 string_list_split_in_place(&cap_list, cap_buf, '=', 1);
643 if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability"))
644 continue;
646 cap_name = cap_list.items[1].string;
647 if (!strcmp(cap_name, "clean")) {
648 entry->supported_capabilities |= CAP_CLEAN;
649 } else if (!strcmp(cap_name, "smudge")) {
650 entry->supported_capabilities |= CAP_SMUDGE;
651 } else {
652 warning(
653 "external filter '%s' requested unsupported filter capability '%s'",
654 cmd, cap_name
658 string_list_clear(&cap_list, 0);
661 done:
662 sigchain_pop(SIGPIPE);
664 if (err || errno == EPIPE) {
665 error("initialization for external filter '%s' failed", cmd);
666 kill_multi_file_filter(hashmap, entry);
667 return NULL;
670 hashmap_add(hashmap, entry);
671 return entry;
674 static int apply_multi_file_filter(const char *path, const char *src, size_t len,
675 int fd, struct strbuf *dst, const char *cmd,
676 const unsigned int wanted_capability)
678 int err;
679 struct cmd2process *entry;
680 struct child_process *process;
681 struct strbuf nbuf = STRBUF_INIT;
682 struct strbuf filter_status = STRBUF_INIT;
683 const char *filter_type;
685 if (!cmd_process_map_initialized) {
686 cmd_process_map_initialized = 1;
687 hashmap_init(&cmd_process_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
688 entry = NULL;
689 } else {
690 entry = find_multi_file_filter_entry(&cmd_process_map, cmd);
693 fflush(NULL);
695 if (!entry) {
696 entry = start_multi_file_filter(&cmd_process_map, cmd);
697 if (!entry)
698 return 0;
700 process = &entry->process;
702 if (!(wanted_capability & entry->supported_capabilities))
703 return 0;
705 if (CAP_CLEAN & wanted_capability)
706 filter_type = "clean";
707 else if (CAP_SMUDGE & wanted_capability)
708 filter_type = "smudge";
709 else
710 die("unexpected filter type");
712 sigchain_push(SIGPIPE, SIG_IGN);
714 assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
715 err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
716 if (err)
717 goto done;
719 err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
720 if (err) {
721 error("path name too long for external filter");
722 goto done;
725 err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
726 if (err)
727 goto done;
729 err = packet_flush_gently(process->in);
730 if (err)
731 goto done;
733 if (fd >= 0)
734 err = write_packetized_from_fd(fd, process->in);
735 else
736 err = write_packetized_from_buf(src, len, process->in);
737 if (err)
738 goto done;
740 read_multi_file_filter_status(process->out, &filter_status);
741 err = strcmp(filter_status.buf, "success");
742 if (err)
743 goto done;
745 err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
746 if (err)
747 goto done;
749 read_multi_file_filter_status(process->out, &filter_status);
750 err = strcmp(filter_status.buf, "success");
752 done:
753 sigchain_pop(SIGPIPE);
755 if (err || errno == EPIPE) {
756 if (!strcmp(filter_status.buf, "error")) {
757 /* The filter signaled a problem with the file. */
758 } else if (!strcmp(filter_status.buf, "abort")) {
760 * The filter signaled a permanent problem. Don't try to filter
761 * files with the same command for the lifetime of the current
762 * Git process.
764 entry->supported_capabilities &= ~wanted_capability;
765 } else {
767 * Something went wrong with the protocol filter.
768 * Force shutdown and restart if another blob requires filtering.
770 error("external filter '%s' failed", cmd);
771 kill_multi_file_filter(&cmd_process_map, entry);
773 } else {
774 strbuf_swap(dst, &nbuf);
776 strbuf_release(&nbuf);
777 return !err;
780 static struct convert_driver {
781 const char *name;
782 struct convert_driver *next;
783 const char *smudge;
784 const char *clean;
785 const char *process;
786 int required;
787 } *user_convert, **user_convert_tail;
789 static int apply_filter(const char *path, const char *src, size_t len,
790 int fd, struct strbuf *dst, struct convert_driver *drv,
791 const unsigned int wanted_capability)
793 const char *cmd = NULL;
795 if (!drv)
796 return 0;
798 if (!dst)
799 return 1;
801 if ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean)
802 cmd = drv->clean;
803 else if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge)
804 cmd = drv->smudge;
806 if (cmd && *cmd)
807 return apply_single_file_filter(path, src, len, fd, dst, cmd);
808 else if (drv->process && *drv->process)
809 return apply_multi_file_filter(path, src, len, fd, dst, drv->process, wanted_capability);
811 return 0;
814 static int read_convert_config(const char *var, const char *value, void *cb)
816 const char *key, *name;
817 int namelen;
818 struct convert_driver *drv;
821 * External conversion drivers are configured using
822 * "filter.<name>.variable".
824 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
825 return 0;
826 for (drv = user_convert; drv; drv = drv->next)
827 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
828 break;
829 if (!drv) {
830 drv = xcalloc(1, sizeof(struct convert_driver));
831 drv->name = xmemdupz(name, namelen);
832 *user_convert_tail = drv;
833 user_convert_tail = &(drv->next);
837 * filter.<name>.smudge and filter.<name>.clean specifies
838 * the command line:
840 * command-line
842 * The command-line will not be interpolated in any way.
845 if (!strcmp("smudge", key))
846 return git_config_string(&drv->smudge, var, value);
848 if (!strcmp("clean", key))
849 return git_config_string(&drv->clean, var, value);
851 if (!strcmp("process", key))
852 return git_config_string(&drv->process, var, value);
854 if (!strcmp("required", key)) {
855 drv->required = git_config_bool(var, value);
856 return 0;
859 return 0;
862 static int count_ident(const char *cp, unsigned long size)
865 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
867 int cnt = 0;
868 char ch;
870 while (size) {
871 ch = *cp++;
872 size--;
873 if (ch != '$')
874 continue;
875 if (size < 3)
876 break;
877 if (memcmp("Id", cp, 2))
878 continue;
879 ch = cp[2];
880 cp += 3;
881 size -= 3;
882 if (ch == '$')
883 cnt++; /* $Id$ */
884 if (ch != ':')
885 continue;
888 * "$Id: ... "; scan up to the closing dollar sign and discard.
890 while (size) {
891 ch = *cp++;
892 size--;
893 if (ch == '$') {
894 cnt++;
895 break;
897 if (ch == '\n')
898 break;
901 return cnt;
904 static int ident_to_git(const char *path, const char *src, size_t len,
905 struct strbuf *buf, int ident)
907 char *dst, *dollar;
909 if (!ident || (src && !count_ident(src, len)))
910 return 0;
912 if (!buf)
913 return 1;
915 /* only grow if not in place */
916 if (strbuf_avail(buf) + buf->len < len)
917 strbuf_grow(buf, len - buf->len);
918 dst = buf->buf;
919 for (;;) {
920 dollar = memchr(src, '$', len);
921 if (!dollar)
922 break;
923 memmove(dst, src, dollar + 1 - src);
924 dst += dollar + 1 - src;
925 len -= dollar + 1 - src;
926 src = dollar + 1;
928 if (len > 3 && !memcmp(src, "Id:", 3)) {
929 dollar = memchr(src + 3, '$', len - 3);
930 if (!dollar)
931 break;
932 if (memchr(src + 3, '\n', dollar - src - 3)) {
933 /* Line break before the next dollar. */
934 continue;
937 memcpy(dst, "Id$", 3);
938 dst += 3;
939 len -= dollar + 1 - src;
940 src = dollar + 1;
943 memmove(dst, src, len);
944 strbuf_setlen(buf, dst + len - buf->buf);
945 return 1;
948 static int ident_to_worktree(const char *path, const char *src, size_t len,
949 struct strbuf *buf, int ident)
951 unsigned char sha1[20];
952 char *to_free = NULL, *dollar, *spc;
953 int cnt;
955 if (!ident)
956 return 0;
958 cnt = count_ident(src, len);
959 if (!cnt)
960 return 0;
962 /* are we "faking" in place editing ? */
963 if (src == buf->buf)
964 to_free = strbuf_detach(buf, NULL);
965 hash_sha1_file(src, len, "blob", sha1);
967 strbuf_grow(buf, len + cnt * 43);
968 for (;;) {
969 /* step 1: run to the next '$' */
970 dollar = memchr(src, '$', len);
971 if (!dollar)
972 break;
973 strbuf_add(buf, src, dollar + 1 - src);
974 len -= dollar + 1 - src;
975 src = dollar + 1;
977 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
978 if (len < 3 || memcmp("Id", src, 2))
979 continue;
981 /* step 3: skip over Id$ or Id:xxxxx$ */
982 if (src[2] == '$') {
983 src += 3;
984 len -= 3;
985 } else if (src[2] == ':') {
987 * It's possible that an expanded Id has crept its way into the
988 * repository, we cope with that by stripping the expansion out.
989 * This is probably not a good idea, since it will cause changes
990 * on checkout, which won't go away by stash, but let's keep it
991 * for git-style ids.
993 dollar = memchr(src + 3, '$', len - 3);
994 if (!dollar) {
995 /* incomplete keyword, no more '$', so just quit the loop */
996 break;
999 if (memchr(src + 3, '\n', dollar - src - 3)) {
1000 /* Line break before the next dollar. */
1001 continue;
1004 spc = memchr(src + 4, ' ', dollar - src - 4);
1005 if (spc && spc < dollar-1) {
1006 /* There are spaces in unexpected places.
1007 * This is probably an id from some other
1008 * versioning system. Keep it for now.
1010 continue;
1013 len -= dollar + 1 - src;
1014 src = dollar + 1;
1015 } else {
1016 /* it wasn't a "Id$" or "Id:xxxx$" */
1017 continue;
1020 /* step 4: substitute */
1021 strbuf_addstr(buf, "Id: ");
1022 strbuf_add(buf, sha1_to_hex(sha1), 40);
1023 strbuf_addstr(buf, " $");
1025 strbuf_add(buf, src, len);
1027 free(to_free);
1028 return 1;
1031 static enum crlf_action git_path_check_crlf(struct attr_check_item *check)
1033 const char *value = check->value;
1035 if (ATTR_TRUE(value))
1036 return CRLF_TEXT;
1037 else if (ATTR_FALSE(value))
1038 return CRLF_BINARY;
1039 else if (ATTR_UNSET(value))
1041 else if (!strcmp(value, "input"))
1042 return CRLF_TEXT_INPUT;
1043 else if (!strcmp(value, "auto"))
1044 return CRLF_AUTO;
1045 return CRLF_UNDEFINED;
1048 static enum eol git_path_check_eol(struct attr_check_item *check)
1050 const char *value = check->value;
1052 if (ATTR_UNSET(value))
1054 else if (!strcmp(value, "lf"))
1055 return EOL_LF;
1056 else if (!strcmp(value, "crlf"))
1057 return EOL_CRLF;
1058 return EOL_UNSET;
1061 static struct convert_driver *git_path_check_convert(struct attr_check_item *check)
1063 const char *value = check->value;
1064 struct convert_driver *drv;
1066 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
1067 return NULL;
1068 for (drv = user_convert; drv; drv = drv->next)
1069 if (!strcmp(value, drv->name))
1070 return drv;
1071 return NULL;
1074 static int git_path_check_ident(struct attr_check_item *check)
1076 const char *value = check->value;
1078 return !!ATTR_TRUE(value);
1081 struct conv_attrs {
1082 struct convert_driver *drv;
1083 enum crlf_action attr_action; /* What attr says */
1084 enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
1085 int ident;
1088 static void convert_attrs(struct conv_attrs *ca, const char *path)
1090 static struct attr_check *check;
1092 if (!check) {
1093 check = attr_check_initl("crlf", "ident", "filter",
1094 "eol", "text", NULL);
1095 user_convert_tail = &user_convert;
1096 git_config(read_convert_config, NULL);
1099 if (!git_check_attr(path, check)) {
1100 struct attr_check_item *ccheck = check->items;
1101 ca->crlf_action = git_path_check_crlf(ccheck + 4);
1102 if (ca->crlf_action == CRLF_UNDEFINED)
1103 ca->crlf_action = git_path_check_crlf(ccheck + 0);
1104 ca->attr_action = ca->crlf_action;
1105 ca->ident = git_path_check_ident(ccheck + 1);
1106 ca->drv = git_path_check_convert(ccheck + 2);
1107 if (ca->crlf_action != CRLF_BINARY) {
1108 enum eol eol_attr = git_path_check_eol(ccheck + 3);
1109 if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
1110 ca->crlf_action = CRLF_AUTO_INPUT;
1111 else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
1112 ca->crlf_action = CRLF_AUTO_CRLF;
1113 else if (eol_attr == EOL_LF)
1114 ca->crlf_action = CRLF_TEXT_INPUT;
1115 else if (eol_attr == EOL_CRLF)
1116 ca->crlf_action = CRLF_TEXT_CRLF;
1118 ca->attr_action = ca->crlf_action;
1119 } else {
1120 ca->drv = NULL;
1121 ca->crlf_action = CRLF_UNDEFINED;
1122 ca->ident = 0;
1124 if (ca->crlf_action == CRLF_TEXT)
1125 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
1126 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
1127 ca->crlf_action = CRLF_BINARY;
1128 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
1129 ca->crlf_action = CRLF_AUTO_CRLF;
1130 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
1131 ca->crlf_action = CRLF_AUTO_INPUT;
1134 int would_convert_to_git_filter_fd(const char *path)
1136 struct conv_attrs ca;
1138 convert_attrs(&ca, path);
1139 if (!ca.drv)
1140 return 0;
1143 * Apply a filter to an fd only if the filter is required to succeed.
1144 * We must die if the filter fails, because the original data before
1145 * filtering is not available.
1147 if (!ca.drv->required)
1148 return 0;
1150 return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN);
1153 const char *get_convert_attr_ascii(const char *path)
1155 struct conv_attrs ca;
1157 convert_attrs(&ca, path);
1158 switch (ca.attr_action) {
1159 case CRLF_UNDEFINED:
1160 return "";
1161 case CRLF_BINARY:
1162 return "-text";
1163 case CRLF_TEXT:
1164 return "text";
1165 case CRLF_TEXT_INPUT:
1166 return "text eol=lf";
1167 case CRLF_TEXT_CRLF:
1168 return "text eol=crlf";
1169 case CRLF_AUTO:
1170 return "text=auto";
1171 case CRLF_AUTO_CRLF:
1172 return "text=auto eol=crlf";
1173 case CRLF_AUTO_INPUT:
1174 return "text=auto eol=lf";
1176 return "";
1179 int convert_to_git(const char *path, const char *src, size_t len,
1180 struct strbuf *dst, enum safe_crlf checksafe)
1182 int ret = 0;
1183 struct conv_attrs ca;
1185 convert_attrs(&ca, path);
1187 ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN);
1188 if (!ret && ca.drv && ca.drv->required)
1189 die("%s: clean filter '%s' failed", path, ca.drv->name);
1191 if (ret && dst) {
1192 src = dst->buf;
1193 len = dst->len;
1195 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
1196 if (ret && dst) {
1197 src = dst->buf;
1198 len = dst->len;
1200 return ret | ident_to_git(path, src, len, dst, ca.ident);
1203 void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
1204 enum safe_crlf checksafe)
1206 struct conv_attrs ca;
1207 convert_attrs(&ca, path);
1209 assert(ca.drv);
1210 assert(ca.drv->clean || ca.drv->process);
1212 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
1213 die("%s: clean filter '%s' failed", path, ca.drv->name);
1215 crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
1216 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
1219 static int convert_to_working_tree_internal(const char *path, const char *src,
1220 size_t len, struct strbuf *dst,
1221 int normalizing)
1223 int ret = 0, ret_filter = 0;
1224 struct conv_attrs ca;
1226 convert_attrs(&ca, path);
1228 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
1229 if (ret) {
1230 src = dst->buf;
1231 len = dst->len;
1234 * CRLF conversion can be skipped if normalizing, unless there
1235 * is a smudge or process filter (even if the process filter doesn't
1236 * support smudge). The filters might expect CRLFs.
1238 if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
1239 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
1240 if (ret) {
1241 src = dst->buf;
1242 len = dst->len;
1246 ret_filter = apply_filter(path, src, len, -1, dst, ca.drv, CAP_SMUDGE);
1247 if (!ret_filter && ca.drv && ca.drv->required)
1248 die("%s: smudge filter %s failed", path, ca.drv->name);
1250 return ret | ret_filter;
1253 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
1255 return convert_to_working_tree_internal(path, src, len, dst, 0);
1258 int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
1260 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
1261 if (ret) {
1262 src = dst->buf;
1263 len = dst->len;
1265 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE);
1268 /*****************************************************************
1270 * Streaming conversion support
1272 *****************************************************************/
1274 typedef int (*filter_fn)(struct stream_filter *,
1275 const char *input, size_t *isize_p,
1276 char *output, size_t *osize_p);
1277 typedef void (*free_fn)(struct stream_filter *);
1279 struct stream_filter_vtbl {
1280 filter_fn filter;
1281 free_fn free;
1284 struct stream_filter {
1285 struct stream_filter_vtbl *vtbl;
1288 static int null_filter_fn(struct stream_filter *filter,
1289 const char *input, size_t *isize_p,
1290 char *output, size_t *osize_p)
1292 size_t count;
1294 if (!input)
1295 return 0; /* we do not keep any states */
1296 count = *isize_p;
1297 if (*osize_p < count)
1298 count = *osize_p;
1299 if (count) {
1300 memmove(output, input, count);
1301 *isize_p -= count;
1302 *osize_p -= count;
1304 return 0;
1307 static void null_free_fn(struct stream_filter *filter)
1309 ; /* nothing -- null instances are shared */
1312 static struct stream_filter_vtbl null_vtbl = {
1313 null_filter_fn,
1314 null_free_fn,
1317 static struct stream_filter null_filter_singleton = {
1318 &null_vtbl,
1321 int is_null_stream_filter(struct stream_filter *filter)
1323 return filter == &null_filter_singleton;
1328 * LF-to-CRLF filter
1331 struct lf_to_crlf_filter {
1332 struct stream_filter filter;
1333 unsigned has_held:1;
1334 char held;
1337 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1338 const char *input, size_t *isize_p,
1339 char *output, size_t *osize_p)
1341 size_t count, o = 0;
1342 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1345 * We may be holding onto the CR to see if it is followed by a
1346 * LF, in which case we would need to go to the main loop.
1347 * Otherwise, just emit it to the output stream.
1349 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1350 output[o++] = lf_to_crlf->held;
1351 lf_to_crlf->has_held = 0;
1354 /* We are told to drain */
1355 if (!input) {
1356 *osize_p -= o;
1357 return 0;
1360 count = *isize_p;
1361 if (count || lf_to_crlf->has_held) {
1362 size_t i;
1363 int was_cr = 0;
1365 if (lf_to_crlf->has_held) {
1366 was_cr = 1;
1367 lf_to_crlf->has_held = 0;
1370 for (i = 0; o < *osize_p && i < count; i++) {
1371 char ch = input[i];
1373 if (ch == '\n') {
1374 output[o++] = '\r';
1375 } else if (was_cr) {
1377 * Previous round saw CR and it is not followed
1378 * by a LF; emit the CR before processing the
1379 * current character.
1381 output[o++] = '\r';
1385 * We may have consumed the last output slot,
1386 * in which case we need to break out of this
1387 * loop; hold the current character before
1388 * returning.
1390 if (*osize_p <= o) {
1391 lf_to_crlf->has_held = 1;
1392 lf_to_crlf->held = ch;
1393 continue; /* break but increment i */
1396 if (ch == '\r') {
1397 was_cr = 1;
1398 continue;
1401 was_cr = 0;
1402 output[o++] = ch;
1405 *osize_p -= o;
1406 *isize_p -= i;
1408 if (!lf_to_crlf->has_held && was_cr) {
1409 lf_to_crlf->has_held = 1;
1410 lf_to_crlf->held = '\r';
1413 return 0;
1416 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1418 free(filter);
1421 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1422 lf_to_crlf_filter_fn,
1423 lf_to_crlf_free_fn,
1426 static struct stream_filter *lf_to_crlf_filter(void)
1428 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1430 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1431 return (struct stream_filter *)lf_to_crlf;
1435 * Cascade filter
1437 #define FILTER_BUFFER 1024
1438 struct cascade_filter {
1439 struct stream_filter filter;
1440 struct stream_filter *one;
1441 struct stream_filter *two;
1442 char buf[FILTER_BUFFER];
1443 int end, ptr;
1446 static int cascade_filter_fn(struct stream_filter *filter,
1447 const char *input, size_t *isize_p,
1448 char *output, size_t *osize_p)
1450 struct cascade_filter *cas = (struct cascade_filter *) filter;
1451 size_t filled = 0;
1452 size_t sz = *osize_p;
1453 size_t to_feed, remaining;
1456 * input -- (one) --> buf -- (two) --> output
1458 while (filled < sz) {
1459 remaining = sz - filled;
1461 /* do we already have something to feed two with? */
1462 if (cas->ptr < cas->end) {
1463 to_feed = cas->end - cas->ptr;
1464 if (stream_filter(cas->two,
1465 cas->buf + cas->ptr, &to_feed,
1466 output + filled, &remaining))
1467 return -1;
1468 cas->ptr += (cas->end - cas->ptr) - to_feed;
1469 filled = sz - remaining;
1470 continue;
1473 /* feed one from upstream and have it emit into our buffer */
1474 to_feed = input ? *isize_p : 0;
1475 if (input && !to_feed)
1476 break;
1477 remaining = sizeof(cas->buf);
1478 if (stream_filter(cas->one,
1479 input, &to_feed,
1480 cas->buf, &remaining))
1481 return -1;
1482 cas->end = sizeof(cas->buf) - remaining;
1483 cas->ptr = 0;
1484 if (input) {
1485 size_t fed = *isize_p - to_feed;
1486 *isize_p -= fed;
1487 input += fed;
1490 /* do we know that we drained one completely? */
1491 if (input || cas->end)
1492 continue;
1494 /* tell two to drain; we have nothing more to give it */
1495 to_feed = 0;
1496 remaining = sz - filled;
1497 if (stream_filter(cas->two,
1498 NULL, &to_feed,
1499 output + filled, &remaining))
1500 return -1;
1501 if (remaining == (sz - filled))
1502 break; /* completely drained two */
1503 filled = sz - remaining;
1505 *osize_p -= filled;
1506 return 0;
1509 static void cascade_free_fn(struct stream_filter *filter)
1511 struct cascade_filter *cas = (struct cascade_filter *)filter;
1512 free_stream_filter(cas->one);
1513 free_stream_filter(cas->two);
1514 free(filter);
1517 static struct stream_filter_vtbl cascade_vtbl = {
1518 cascade_filter_fn,
1519 cascade_free_fn,
1522 static struct stream_filter *cascade_filter(struct stream_filter *one,
1523 struct stream_filter *two)
1525 struct cascade_filter *cascade;
1527 if (!one || is_null_stream_filter(one))
1528 return two;
1529 if (!two || is_null_stream_filter(two))
1530 return one;
1532 cascade = xmalloc(sizeof(*cascade));
1533 cascade->one = one;
1534 cascade->two = two;
1535 cascade->end = cascade->ptr = 0;
1536 cascade->filter.vtbl = &cascade_vtbl;
1537 return (struct stream_filter *)cascade;
1541 * ident filter
1543 #define IDENT_DRAINING (-1)
1544 #define IDENT_SKIPPING (-2)
1545 struct ident_filter {
1546 struct stream_filter filter;
1547 struct strbuf left;
1548 int state;
1549 char ident[45]; /* ": x40 $" */
1552 static int is_foreign_ident(const char *str)
1554 int i;
1556 if (!skip_prefix(str, "$Id: ", &str))
1557 return 0;
1558 for (i = 0; str[i]; i++) {
1559 if (isspace(str[i]) && str[i+1] != '$')
1560 return 1;
1562 return 0;
1565 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1567 size_t to_drain = ident->left.len;
1569 if (*osize_p < to_drain)
1570 to_drain = *osize_p;
1571 if (to_drain) {
1572 memcpy(*output_p, ident->left.buf, to_drain);
1573 strbuf_remove(&ident->left, 0, to_drain);
1574 *output_p += to_drain;
1575 *osize_p -= to_drain;
1577 if (!ident->left.len)
1578 ident->state = 0;
1581 static int ident_filter_fn(struct stream_filter *filter,
1582 const char *input, size_t *isize_p,
1583 char *output, size_t *osize_p)
1585 struct ident_filter *ident = (struct ident_filter *)filter;
1586 static const char head[] = "$Id";
1588 if (!input) {
1589 /* drain upon eof */
1590 switch (ident->state) {
1591 default:
1592 strbuf_add(&ident->left, head, ident->state);
1593 case IDENT_SKIPPING:
1594 /* fallthru */
1595 case IDENT_DRAINING:
1596 ident_drain(ident, &output, osize_p);
1598 return 0;
1601 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1602 int ch;
1604 if (ident->state == IDENT_DRAINING) {
1605 ident_drain(ident, &output, osize_p);
1606 if (!*osize_p)
1607 break;
1608 continue;
1611 ch = *(input++);
1612 (*isize_p)--;
1614 if (ident->state == IDENT_SKIPPING) {
1616 * Skipping until '$' or LF, but keeping them
1617 * in case it is a foreign ident.
1619 strbuf_addch(&ident->left, ch);
1620 if (ch != '\n' && ch != '$')
1621 continue;
1622 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1623 strbuf_setlen(&ident->left, sizeof(head) - 1);
1624 strbuf_addstr(&ident->left, ident->ident);
1626 ident->state = IDENT_DRAINING;
1627 continue;
1630 if (ident->state < sizeof(head) &&
1631 head[ident->state] == ch) {
1632 ident->state++;
1633 continue;
1636 if (ident->state)
1637 strbuf_add(&ident->left, head, ident->state);
1638 if (ident->state == sizeof(head) - 1) {
1639 if (ch != ':' && ch != '$') {
1640 strbuf_addch(&ident->left, ch);
1641 ident->state = 0;
1642 continue;
1645 if (ch == ':') {
1646 strbuf_addch(&ident->left, ch);
1647 ident->state = IDENT_SKIPPING;
1648 } else {
1649 strbuf_addstr(&ident->left, ident->ident);
1650 ident->state = IDENT_DRAINING;
1652 continue;
1655 strbuf_addch(&ident->left, ch);
1656 ident->state = IDENT_DRAINING;
1658 return 0;
1661 static void ident_free_fn(struct stream_filter *filter)
1663 struct ident_filter *ident = (struct ident_filter *)filter;
1664 strbuf_release(&ident->left);
1665 free(filter);
1668 static struct stream_filter_vtbl ident_vtbl = {
1669 ident_filter_fn,
1670 ident_free_fn,
1673 static struct stream_filter *ident_filter(const unsigned char *sha1)
1675 struct ident_filter *ident = xmalloc(sizeof(*ident));
1677 xsnprintf(ident->ident, sizeof(ident->ident),
1678 ": %s $", sha1_to_hex(sha1));
1679 strbuf_init(&ident->left, 0);
1680 ident->filter.vtbl = &ident_vtbl;
1681 ident->state = 0;
1682 return (struct stream_filter *)ident;
1686 * Return an appropriately constructed filter for the path, or NULL if
1687 * the contents cannot be filtered without reading the whole thing
1688 * in-core.
1690 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1691 * large binary blob you would want us not to slurp into the memory!
1693 struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1695 struct conv_attrs ca;
1696 struct stream_filter *filter = NULL;
1698 convert_attrs(&ca, path);
1699 if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
1700 return NULL;
1702 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1703 return NULL;
1705 if (ca.ident)
1706 filter = ident_filter(sha1);
1708 if (output_eol(ca.crlf_action) == EOL_CRLF)
1709 filter = cascade_filter(filter, lf_to_crlf_filter());
1710 else
1711 filter = cascade_filter(filter, &null_filter_singleton);
1713 return filter;
1716 void free_stream_filter(struct stream_filter *filter)
1718 filter->vtbl->free(filter);
1721 int stream_filter(struct stream_filter *filter,
1722 const char *input, size_t *isize_p,
1723 char *output, size_t *osize_p)
1725 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);