Merge branch 'hb/gitweb-project-list'
[git.git] / convert.c
blobdbdbb24e4d37e1f4d4db8e479d48125c90a071a4
1 #define NO_THE_INDEX_COMPATIBILITY_MACROS
2 #include "cache.h"
3 #include "config.h"
4 #include "attr.h"
5 #include "run-command.h"
6 #include "quote.h"
7 #include "sigchain.h"
8 #include "pkt-line.h"
9 #include "sub-process.h"
12 * convert.c - convert a file when checking it out and checking it in.
14 * This should use the pathname to decide on whether it wants to do some
15 * more interesting conversions (automatic gzip/unzip, general format
16 * conversions etc etc), but by default it just does automatic CRLF<->LF
17 * translation when the "text" attribute or "auto_crlf" option is set.
20 /* Stat bits: When BIN is set, the txt bits are unset */
21 #define CONVERT_STAT_BITS_TXT_LF 0x1
22 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
23 #define CONVERT_STAT_BITS_BIN 0x4
25 enum crlf_action {
26 CRLF_UNDEFINED,
27 CRLF_BINARY,
28 CRLF_TEXT,
29 CRLF_TEXT_INPUT,
30 CRLF_TEXT_CRLF,
31 CRLF_AUTO,
32 CRLF_AUTO_INPUT,
33 CRLF_AUTO_CRLF
36 struct text_stat {
37 /* NUL, CR, LF and CRLF counts */
38 unsigned nul, lonecr, lonelf, crlf;
40 /* These are just approximations! */
41 unsigned printable, nonprintable;
44 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
46 unsigned long i;
48 memset(stats, 0, sizeof(*stats));
50 for (i = 0; i < size; i++) {
51 unsigned char c = buf[i];
52 if (c == '\r') {
53 if (i+1 < size && buf[i+1] == '\n') {
54 stats->crlf++;
55 i++;
56 } else
57 stats->lonecr++;
58 continue;
60 if (c == '\n') {
61 stats->lonelf++;
62 continue;
64 if (c == 127)
65 /* DEL */
66 stats->nonprintable++;
67 else if (c < 32) {
68 switch (c) {
69 /* BS, HT, ESC and FF */
70 case '\b': case '\t': case '\033': case '\014':
71 stats->printable++;
72 break;
73 case 0:
74 stats->nul++;
75 /* fall through */
76 default:
77 stats->nonprintable++;
80 else
81 stats->printable++;
84 /* If file ends with EOF then don't count this EOF as non-printable. */
85 if (size >= 1 && buf[size-1] == '\032')
86 stats->nonprintable--;
90 * The same heuristics as diff.c::mmfile_is_binary()
91 * We treat files with bare CR as binary
93 static int convert_is_binary(unsigned long size, const struct text_stat *stats)
95 if (stats->lonecr)
96 return 1;
97 if (stats->nul)
98 return 1;
99 if ((stats->printable >> 7) < stats->nonprintable)
100 return 1;
101 return 0;
104 static unsigned int gather_convert_stats(const char *data, unsigned long size)
106 struct text_stat stats;
107 int ret = 0;
108 if (!data || !size)
109 return 0;
110 gather_stats(data, size, &stats);
111 if (convert_is_binary(size, &stats))
112 ret |= CONVERT_STAT_BITS_BIN;
113 if (stats.crlf)
114 ret |= CONVERT_STAT_BITS_TXT_CRLF;
115 if (stats.lonelf)
116 ret |= CONVERT_STAT_BITS_TXT_LF;
118 return ret;
121 static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
123 unsigned int convert_stats = gather_convert_stats(data, size);
125 if (convert_stats & CONVERT_STAT_BITS_BIN)
126 return "-text";
127 switch (convert_stats) {
128 case CONVERT_STAT_BITS_TXT_LF:
129 return "lf";
130 case CONVERT_STAT_BITS_TXT_CRLF:
131 return "crlf";
132 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
133 return "mixed";
134 default:
135 return "none";
139 const char *get_cached_convert_stats_ascii(const struct index_state *istate,
140 const char *path)
142 const char *ret;
143 unsigned long sz;
144 void *data = read_blob_data_from_index(istate, path, &sz);
145 ret = gather_convert_stats_ascii(data, sz);
146 free(data);
147 return ret;
150 const char *get_wt_convert_stats_ascii(const char *path)
152 const char *ret = "";
153 struct strbuf sb = STRBUF_INIT;
154 if (strbuf_read_file(&sb, path, 0) >= 0)
155 ret = gather_convert_stats_ascii(sb.buf, sb.len);
156 strbuf_release(&sb);
157 return ret;
160 static int text_eol_is_crlf(void)
162 if (auto_crlf == AUTO_CRLF_TRUE)
163 return 1;
164 else if (auto_crlf == AUTO_CRLF_INPUT)
165 return 0;
166 if (core_eol == EOL_CRLF)
167 return 1;
168 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
169 return 1;
170 return 0;
173 static enum eol output_eol(enum crlf_action crlf_action)
175 switch (crlf_action) {
176 case CRLF_BINARY:
177 return EOL_UNSET;
178 case CRLF_TEXT_CRLF:
179 return EOL_CRLF;
180 case CRLF_TEXT_INPUT:
181 return EOL_LF;
182 case CRLF_UNDEFINED:
183 case CRLF_AUTO_CRLF:
184 return EOL_CRLF;
185 case CRLF_AUTO_INPUT:
186 return EOL_LF;
187 case CRLF_TEXT:
188 case CRLF_AUTO:
189 /* fall through */
190 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
192 warning("Illegal crlf_action %d\n", (int)crlf_action);
193 return core_eol;
196 static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
197 struct text_stat *old_stats, struct text_stat *new_stats,
198 enum safe_crlf checksafe)
200 if (old_stats->crlf && !new_stats->crlf ) {
202 * CRLFs would not be restored by checkout
204 if (checksafe == SAFE_CRLF_WARN)
205 warning(_("CRLF will be replaced by LF in %s.\n"
206 "The file will have its original line"
207 " endings in your working directory."), path);
208 else /* i.e. SAFE_CRLF_FAIL */
209 die(_("CRLF would be replaced by LF in %s."), path);
210 } else if (old_stats->lonelf && !new_stats->lonelf ) {
212 * CRLFs would be added by checkout
214 if (checksafe == SAFE_CRLF_WARN)
215 warning(_("LF will be replaced by CRLF in %s.\n"
216 "The file will have its original line"
217 " endings in your working directory."), path);
218 else /* i.e. SAFE_CRLF_FAIL */
219 die(_("LF would be replaced by CRLF in %s"), path);
223 static int has_cr_in_index(const struct index_state *istate, const char *path)
225 unsigned long sz;
226 void *data;
227 int has_cr;
229 data = read_blob_data_from_index(istate, path, &sz);
230 if (!data)
231 return 0;
232 has_cr = memchr(data, '\r', sz) != NULL;
233 free(data);
234 return has_cr;
237 static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
238 enum crlf_action crlf_action)
240 if (output_eol(crlf_action) != EOL_CRLF)
241 return 0;
242 /* No "naked" LF? Nothing to convert, regardless. */
243 if (!stats->lonelf)
244 return 0;
246 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
247 /* If we have any CR or CRLF line endings, we do not touch it */
248 /* This is the new safer autocrlf-handling */
249 if (stats->lonecr || stats->crlf)
250 return 0;
252 if (convert_is_binary(len, stats))
253 return 0;
255 return 1;
259 static int crlf_to_git(const struct index_state *istate,
260 const char *path, const char *src, size_t len,
261 struct strbuf *buf,
262 enum crlf_action crlf_action, enum safe_crlf checksafe)
264 struct text_stat stats;
265 char *dst;
266 int convert_crlf_into_lf;
268 if (crlf_action == CRLF_BINARY ||
269 (src && !len))
270 return 0;
273 * If we are doing a dry-run and have no source buffer, there is
274 * nothing to analyze; we must assume we would convert.
276 if (!buf && !src)
277 return 1;
279 gather_stats(src, len, &stats);
280 /* Optimization: No CRLF? Nothing to convert, regardless. */
281 convert_crlf_into_lf = !!stats.crlf;
283 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
284 if (convert_is_binary(len, &stats))
285 return 0;
287 * If the file in the index has any CR in it, do not
288 * convert. This is the new safer autocrlf handling,
289 * unless we want to renormalize in a merge or
290 * cherry-pick.
292 if ((checksafe != SAFE_CRLF_RENORMALIZE) &&
293 has_cr_in_index(istate, path))
294 convert_crlf_into_lf = 0;
296 if ((checksafe == SAFE_CRLF_WARN ||
297 (checksafe == SAFE_CRLF_FAIL)) && len) {
298 struct text_stat new_stats;
299 memcpy(&new_stats, &stats, sizeof(new_stats));
300 /* simulate "git add" */
301 if (convert_crlf_into_lf) {
302 new_stats.lonelf += new_stats.crlf;
303 new_stats.crlf = 0;
305 /* simulate "git checkout" */
306 if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) {
307 new_stats.crlf += new_stats.lonelf;
308 new_stats.lonelf = 0;
310 check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe);
312 if (!convert_crlf_into_lf)
313 return 0;
316 * At this point all of our source analysis is done, and we are sure we
317 * would convert. If we are in dry-run mode, we can give an answer.
319 if (!buf)
320 return 1;
322 /* only grow if not in place */
323 if (strbuf_avail(buf) + buf->len < len)
324 strbuf_grow(buf, len - buf->len);
325 dst = buf->buf;
326 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
328 * If we guessed, we already know we rejected a file with
329 * lone CR, and we can strip a CR without looking at what
330 * follow it.
332 do {
333 unsigned char c = *src++;
334 if (c != '\r')
335 *dst++ = c;
336 } while (--len);
337 } else {
338 do {
339 unsigned char c = *src++;
340 if (! (c == '\r' && (1 < len && *src == '\n')))
341 *dst++ = c;
342 } while (--len);
344 strbuf_setlen(buf, dst - buf->buf);
345 return 1;
348 static int crlf_to_worktree(const char *path, const char *src, size_t len,
349 struct strbuf *buf, enum crlf_action crlf_action)
351 char *to_free = NULL;
352 struct text_stat stats;
354 if (!len || output_eol(crlf_action) != EOL_CRLF)
355 return 0;
357 gather_stats(src, len, &stats);
358 if (!will_convert_lf_to_crlf(len, &stats, crlf_action))
359 return 0;
361 /* are we "faking" in place editing ? */
362 if (src == buf->buf)
363 to_free = strbuf_detach(buf, NULL);
365 strbuf_grow(buf, len + stats.lonelf);
366 for (;;) {
367 const char *nl = memchr(src, '\n', len);
368 if (!nl)
369 break;
370 if (nl > src && nl[-1] == '\r') {
371 strbuf_add(buf, src, nl + 1 - src);
372 } else {
373 strbuf_add(buf, src, nl - src);
374 strbuf_addstr(buf, "\r\n");
376 len -= nl + 1 - src;
377 src = nl + 1;
379 strbuf_add(buf, src, len);
381 free(to_free);
382 return 1;
385 struct filter_params {
386 const char *src;
387 unsigned long size;
388 int fd;
389 const char *cmd;
390 const char *path;
393 static int filter_buffer_or_fd(int in, int out, void *data)
396 * Spawn cmd and feed the buffer contents through its stdin.
398 struct child_process child_process = CHILD_PROCESS_INIT;
399 struct filter_params *params = (struct filter_params *)data;
400 int write_err, status;
401 const char *argv[] = { NULL, NULL };
403 /* apply % substitution to cmd */
404 struct strbuf cmd = STRBUF_INIT;
405 struct strbuf path = STRBUF_INIT;
406 struct strbuf_expand_dict_entry dict[] = {
407 { "f", NULL, },
408 { NULL, NULL, },
411 /* quote the path to preserve spaces, etc. */
412 sq_quote_buf(&path, params->path);
413 dict[0].value = path.buf;
415 /* expand all %f with the quoted path */
416 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
417 strbuf_release(&path);
419 argv[0] = cmd.buf;
421 child_process.argv = argv;
422 child_process.use_shell = 1;
423 child_process.in = -1;
424 child_process.out = out;
426 if (start_command(&child_process))
427 return error("cannot fork to run external filter '%s'", params->cmd);
429 sigchain_push(SIGPIPE, SIG_IGN);
431 if (params->src) {
432 write_err = (write_in_full(child_process.in,
433 params->src, params->size) < 0);
434 if (errno == EPIPE)
435 write_err = 0;
436 } else {
437 write_err = copy_fd(params->fd, child_process.in);
438 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
439 write_err = 0;
442 if (close(child_process.in))
443 write_err = 1;
444 if (write_err)
445 error("cannot feed the input to external filter '%s'", params->cmd);
447 sigchain_pop(SIGPIPE);
449 status = finish_command(&child_process);
450 if (status)
451 error("external filter '%s' failed %d", params->cmd, status);
453 strbuf_release(&cmd);
454 return (write_err || status);
457 static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
458 struct strbuf *dst, const char *cmd)
461 * Create a pipeline to have the command filter the buffer's
462 * contents.
464 * (child --> cmd) --> us
466 int err = 0;
467 struct strbuf nbuf = STRBUF_INIT;
468 struct async async;
469 struct filter_params params;
471 memset(&async, 0, sizeof(async));
472 async.proc = filter_buffer_or_fd;
473 async.data = &params;
474 async.out = -1;
475 params.src = src;
476 params.size = len;
477 params.fd = fd;
478 params.cmd = cmd;
479 params.path = path;
481 fflush(NULL);
482 if (start_async(&async))
483 return 0; /* error was already reported */
485 if (strbuf_read(&nbuf, async.out, len) < 0) {
486 err = error("read from external filter '%s' failed", cmd);
488 if (close(async.out)) {
489 err = error("read from external filter '%s' failed", cmd);
491 if (finish_async(&async)) {
492 err = error("external filter '%s' failed", cmd);
495 if (!err) {
496 strbuf_swap(dst, &nbuf);
498 strbuf_release(&nbuf);
499 return !err;
502 #define CAP_CLEAN (1u<<0)
503 #define CAP_SMUDGE (1u<<1)
504 #define CAP_DELAY (1u<<2)
506 struct cmd2process {
507 struct subprocess_entry subprocess; /* must be the first member! */
508 unsigned int supported_capabilities;
511 static int subprocess_map_initialized;
512 static struct hashmap subprocess_map;
514 static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
516 int err, i;
517 struct cmd2process *entry = (struct cmd2process *)subprocess;
518 struct string_list cap_list = STRING_LIST_INIT_NODUP;
519 char *cap_buf;
520 const char *cap_name;
521 struct child_process *process = &subprocess->process;
522 const char *cmd = subprocess->cmd;
524 static const struct {
525 const char *name;
526 unsigned int cap;
527 } known_caps[] = {
528 { "clean", CAP_CLEAN },
529 { "smudge", CAP_SMUDGE },
530 { "delay", CAP_DELAY },
533 sigchain_push(SIGPIPE, SIG_IGN);
535 err = packet_writel(process->in, "git-filter-client", "version=2", NULL);
536 if (err)
537 goto done;
539 err = strcmp(packet_read_line(process->out, NULL), "git-filter-server");
540 if (err) {
541 error("external filter '%s' does not support filter protocol version 2", cmd);
542 goto done;
544 err = strcmp(packet_read_line(process->out, NULL), "version=2");
545 if (err)
546 goto done;
547 err = packet_read_line(process->out, NULL) != NULL;
548 if (err)
549 goto done;
551 for (i = 0; i < ARRAY_SIZE(known_caps); ++i) {
552 err = packet_write_fmt_gently(
553 process->in, "capability=%s\n", known_caps[i].name);
554 if (err)
555 goto done;
557 err = packet_flush_gently(process->in);
558 if (err)
559 goto done;
561 for (;;) {
562 cap_buf = packet_read_line(process->out, NULL);
563 if (!cap_buf)
564 break;
565 string_list_split_in_place(&cap_list, cap_buf, '=', 1);
567 if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability"))
568 continue;
570 cap_name = cap_list.items[1].string;
571 i = ARRAY_SIZE(known_caps) - 1;
572 while (i >= 0 && strcmp(cap_name, known_caps[i].name))
573 i--;
575 if (i >= 0)
576 entry->supported_capabilities |= known_caps[i].cap;
577 else
578 warning("external filter '%s' requested unsupported filter capability '%s'",
579 cmd, cap_name);
581 string_list_clear(&cap_list, 0);
584 done:
585 sigchain_pop(SIGPIPE);
587 return err;
590 static void handle_filter_error(const struct strbuf *filter_status,
591 struct cmd2process *entry,
592 const unsigned int wanted_capability) {
593 if (!strcmp(filter_status->buf, "error"))
594 ; /* The filter signaled a problem with the file. */
595 else if (!strcmp(filter_status->buf, "abort") && wanted_capability) {
597 * The filter signaled a permanent problem. Don't try to filter
598 * files with the same command for the lifetime of the current
599 * Git process.
601 entry->supported_capabilities &= ~wanted_capability;
602 } else {
604 * Something went wrong with the protocol filter.
605 * Force shutdown and restart if another blob requires filtering.
607 error("external filter '%s' failed", entry->subprocess.cmd);
608 subprocess_stop(&subprocess_map, &entry->subprocess);
609 free(entry);
613 static int apply_multi_file_filter(const char *path, const char *src, size_t len,
614 int fd, struct strbuf *dst, const char *cmd,
615 const unsigned int wanted_capability,
616 struct delayed_checkout *dco)
618 int err;
619 int can_delay = 0;
620 struct cmd2process *entry;
621 struct child_process *process;
622 struct strbuf nbuf = STRBUF_INIT;
623 struct strbuf filter_status = STRBUF_INIT;
624 const char *filter_type;
626 if (!subprocess_map_initialized) {
627 subprocess_map_initialized = 1;
628 hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
629 entry = NULL;
630 } else {
631 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
634 fflush(NULL);
636 if (!entry) {
637 entry = xmalloc(sizeof(*entry));
638 entry->supported_capabilities = 0;
640 if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
641 free(entry);
642 return 0;
645 process = &entry->subprocess.process;
647 if (!(entry->supported_capabilities & wanted_capability))
648 return 0;
650 if (wanted_capability & CAP_CLEAN)
651 filter_type = "clean";
652 else if (wanted_capability & CAP_SMUDGE)
653 filter_type = "smudge";
654 else
655 die("unexpected filter type");
657 sigchain_push(SIGPIPE, SIG_IGN);
659 assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
660 err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
661 if (err)
662 goto done;
664 err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
665 if (err) {
666 error("path name too long for external filter");
667 goto done;
670 err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
671 if (err)
672 goto done;
674 if ((entry->supported_capabilities & CAP_DELAY) &&
675 dco && dco->state == CE_CAN_DELAY) {
676 can_delay = 1;
677 err = packet_write_fmt_gently(process->in, "can-delay=1\n");
678 if (err)
679 goto done;
682 err = packet_flush_gently(process->in);
683 if (err)
684 goto done;
686 if (fd >= 0)
687 err = write_packetized_from_fd(fd, process->in);
688 else
689 err = write_packetized_from_buf(src, len, process->in);
690 if (err)
691 goto done;
693 err = subprocess_read_status(process->out, &filter_status);
694 if (err)
695 goto done;
697 if (can_delay && !strcmp(filter_status.buf, "delayed")) {
698 string_list_insert(&dco->filters, cmd);
699 string_list_insert(&dco->paths, path);
700 } else {
701 /* The filter got the blob and wants to send us a response. */
702 err = strcmp(filter_status.buf, "success");
703 if (err)
704 goto done;
706 err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
707 if (err)
708 goto done;
710 err = subprocess_read_status(process->out, &filter_status);
711 if (err)
712 goto done;
714 err = strcmp(filter_status.buf, "success");
717 done:
718 sigchain_pop(SIGPIPE);
720 if (err)
721 handle_filter_error(&filter_status, entry, wanted_capability);
722 else
723 strbuf_swap(dst, &nbuf);
724 strbuf_release(&nbuf);
725 return !err;
729 int async_query_available_blobs(const char *cmd, struct string_list *available_paths)
731 int err;
732 char *line;
733 struct cmd2process *entry;
734 struct child_process *process;
735 struct strbuf filter_status = STRBUF_INIT;
737 assert(subprocess_map_initialized);
738 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
739 if (!entry) {
740 error("external filter '%s' is not available anymore although "
741 "not all paths have been filtered", cmd);
742 return 0;
744 process = &entry->subprocess.process;
745 sigchain_push(SIGPIPE, SIG_IGN);
747 err = packet_write_fmt_gently(
748 process->in, "command=list_available_blobs\n");
749 if (err)
750 goto done;
752 err = packet_flush_gently(process->in);
753 if (err)
754 goto done;
756 while ((line = packet_read_line(process->out, NULL))) {
757 const char *path;
758 if (skip_prefix(line, "pathname=", &path))
759 string_list_insert(available_paths, xstrdup(path));
760 else
761 ; /* ignore unknown keys */
764 err = subprocess_read_status(process->out, &filter_status);
765 if (err)
766 goto done;
768 err = strcmp(filter_status.buf, "success");
770 done:
771 sigchain_pop(SIGPIPE);
773 if (err)
774 handle_filter_error(&filter_status, entry, 0);
775 return !err;
778 static struct convert_driver {
779 const char *name;
780 struct convert_driver *next;
781 const char *smudge;
782 const char *clean;
783 const char *process;
784 int required;
785 } *user_convert, **user_convert_tail;
787 static int apply_filter(const char *path, const char *src, size_t len,
788 int fd, struct strbuf *dst, struct convert_driver *drv,
789 const unsigned int wanted_capability,
790 struct delayed_checkout *dco)
792 const char *cmd = NULL;
794 if (!drv)
795 return 0;
797 if (!dst)
798 return 1;
800 if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean)
801 cmd = drv->clean;
802 else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge)
803 cmd = drv->smudge;
805 if (cmd && *cmd)
806 return apply_single_file_filter(path, src, len, fd, dst, cmd);
807 else if (drv->process && *drv->process)
808 return apply_multi_file_filter(path, src, len, fd, dst,
809 drv->process, wanted_capability, dco);
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, NULL);
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 struct index_state *istate,
1180 const char *path, const char *src, size_t len,
1181 struct strbuf *dst, enum safe_crlf checksafe)
1183 int ret = 0;
1184 struct conv_attrs ca;
1186 convert_attrs(&ca, path);
1188 ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL);
1189 if (!ret && ca.drv && ca.drv->required)
1190 die("%s: clean filter '%s' failed", path, ca.drv->name);
1192 if (ret && dst) {
1193 src = dst->buf;
1194 len = dst->len;
1196 ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe);
1197 if (ret && dst) {
1198 src = dst->buf;
1199 len = dst->len;
1201 return ret | ident_to_git(path, src, len, dst, ca.ident);
1204 void convert_to_git_filter_fd(const struct index_state *istate,
1205 const char *path, int fd, struct strbuf *dst,
1206 enum safe_crlf checksafe)
1208 struct conv_attrs ca;
1209 convert_attrs(&ca, path);
1211 assert(ca.drv);
1212 assert(ca.drv->clean || ca.drv->process);
1214 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL))
1215 die("%s: clean filter '%s' failed", path, ca.drv->name);
1217 crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
1218 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
1221 static int convert_to_working_tree_internal(const char *path, const char *src,
1222 size_t len, struct strbuf *dst,
1223 int normalizing, struct delayed_checkout *dco)
1225 int ret = 0, ret_filter = 0;
1226 struct conv_attrs ca;
1228 convert_attrs(&ca, path);
1230 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
1231 if (ret) {
1232 src = dst->buf;
1233 len = dst->len;
1236 * CRLF conversion can be skipped if normalizing, unless there
1237 * is a smudge or process filter (even if the process filter doesn't
1238 * support smudge). The filters might expect CRLFs.
1240 if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
1241 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
1242 if (ret) {
1243 src = dst->buf;
1244 len = dst->len;
1248 ret_filter = apply_filter(
1249 path, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco);
1250 if (!ret_filter && ca.drv && ca.drv->required)
1251 die("%s: smudge filter %s failed", path, ca.drv->name);
1253 return ret | ret_filter;
1256 int async_convert_to_working_tree(const char *path, const char *src,
1257 size_t len, struct strbuf *dst,
1258 void *dco)
1260 return convert_to_working_tree_internal(path, src, len, dst, 0, dco);
1263 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
1265 return convert_to_working_tree_internal(path, src, len, dst, 0, NULL);
1268 int renormalize_buffer(const struct index_state *istate, const char *path,
1269 const char *src, size_t len, struct strbuf *dst)
1271 int ret = convert_to_working_tree_internal(path, src, len, dst, 1, NULL);
1272 if (ret) {
1273 src = dst->buf;
1274 len = dst->len;
1276 return ret | convert_to_git(istate, path, src, len, dst, SAFE_CRLF_RENORMALIZE);
1279 /*****************************************************************
1281 * Streaming conversion support
1283 *****************************************************************/
1285 typedef int (*filter_fn)(struct stream_filter *,
1286 const char *input, size_t *isize_p,
1287 char *output, size_t *osize_p);
1288 typedef void (*free_fn)(struct stream_filter *);
1290 struct stream_filter_vtbl {
1291 filter_fn filter;
1292 free_fn free;
1295 struct stream_filter {
1296 struct stream_filter_vtbl *vtbl;
1299 static int null_filter_fn(struct stream_filter *filter,
1300 const char *input, size_t *isize_p,
1301 char *output, size_t *osize_p)
1303 size_t count;
1305 if (!input)
1306 return 0; /* we do not keep any states */
1307 count = *isize_p;
1308 if (*osize_p < count)
1309 count = *osize_p;
1310 if (count) {
1311 memmove(output, input, count);
1312 *isize_p -= count;
1313 *osize_p -= count;
1315 return 0;
1318 static void null_free_fn(struct stream_filter *filter)
1320 ; /* nothing -- null instances are shared */
1323 static struct stream_filter_vtbl null_vtbl = {
1324 null_filter_fn,
1325 null_free_fn,
1328 static struct stream_filter null_filter_singleton = {
1329 &null_vtbl,
1332 int is_null_stream_filter(struct stream_filter *filter)
1334 return filter == &null_filter_singleton;
1339 * LF-to-CRLF filter
1342 struct lf_to_crlf_filter {
1343 struct stream_filter filter;
1344 unsigned has_held:1;
1345 char held;
1348 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1349 const char *input, size_t *isize_p,
1350 char *output, size_t *osize_p)
1352 size_t count, o = 0;
1353 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1356 * We may be holding onto the CR to see if it is followed by a
1357 * LF, in which case we would need to go to the main loop.
1358 * Otherwise, just emit it to the output stream.
1360 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1361 output[o++] = lf_to_crlf->held;
1362 lf_to_crlf->has_held = 0;
1365 /* We are told to drain */
1366 if (!input) {
1367 *osize_p -= o;
1368 return 0;
1371 count = *isize_p;
1372 if (count || lf_to_crlf->has_held) {
1373 size_t i;
1374 int was_cr = 0;
1376 if (lf_to_crlf->has_held) {
1377 was_cr = 1;
1378 lf_to_crlf->has_held = 0;
1381 for (i = 0; o < *osize_p && i < count; i++) {
1382 char ch = input[i];
1384 if (ch == '\n') {
1385 output[o++] = '\r';
1386 } else if (was_cr) {
1388 * Previous round saw CR and it is not followed
1389 * by a LF; emit the CR before processing the
1390 * current character.
1392 output[o++] = '\r';
1396 * We may have consumed the last output slot,
1397 * in which case we need to break out of this
1398 * loop; hold the current character before
1399 * returning.
1401 if (*osize_p <= o) {
1402 lf_to_crlf->has_held = 1;
1403 lf_to_crlf->held = ch;
1404 continue; /* break but increment i */
1407 if (ch == '\r') {
1408 was_cr = 1;
1409 continue;
1412 was_cr = 0;
1413 output[o++] = ch;
1416 *osize_p -= o;
1417 *isize_p -= i;
1419 if (!lf_to_crlf->has_held && was_cr) {
1420 lf_to_crlf->has_held = 1;
1421 lf_to_crlf->held = '\r';
1424 return 0;
1427 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1429 free(filter);
1432 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1433 lf_to_crlf_filter_fn,
1434 lf_to_crlf_free_fn,
1437 static struct stream_filter *lf_to_crlf_filter(void)
1439 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1441 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1442 return (struct stream_filter *)lf_to_crlf;
1446 * Cascade filter
1448 #define FILTER_BUFFER 1024
1449 struct cascade_filter {
1450 struct stream_filter filter;
1451 struct stream_filter *one;
1452 struct stream_filter *two;
1453 char buf[FILTER_BUFFER];
1454 int end, ptr;
1457 static int cascade_filter_fn(struct stream_filter *filter,
1458 const char *input, size_t *isize_p,
1459 char *output, size_t *osize_p)
1461 struct cascade_filter *cas = (struct cascade_filter *) filter;
1462 size_t filled = 0;
1463 size_t sz = *osize_p;
1464 size_t to_feed, remaining;
1467 * input -- (one) --> buf -- (two) --> output
1469 while (filled < sz) {
1470 remaining = sz - filled;
1472 /* do we already have something to feed two with? */
1473 if (cas->ptr < cas->end) {
1474 to_feed = cas->end - cas->ptr;
1475 if (stream_filter(cas->two,
1476 cas->buf + cas->ptr, &to_feed,
1477 output + filled, &remaining))
1478 return -1;
1479 cas->ptr += (cas->end - cas->ptr) - to_feed;
1480 filled = sz - remaining;
1481 continue;
1484 /* feed one from upstream and have it emit into our buffer */
1485 to_feed = input ? *isize_p : 0;
1486 if (input && !to_feed)
1487 break;
1488 remaining = sizeof(cas->buf);
1489 if (stream_filter(cas->one,
1490 input, &to_feed,
1491 cas->buf, &remaining))
1492 return -1;
1493 cas->end = sizeof(cas->buf) - remaining;
1494 cas->ptr = 0;
1495 if (input) {
1496 size_t fed = *isize_p - to_feed;
1497 *isize_p -= fed;
1498 input += fed;
1501 /* do we know that we drained one completely? */
1502 if (input || cas->end)
1503 continue;
1505 /* tell two to drain; we have nothing more to give it */
1506 to_feed = 0;
1507 remaining = sz - filled;
1508 if (stream_filter(cas->two,
1509 NULL, &to_feed,
1510 output + filled, &remaining))
1511 return -1;
1512 if (remaining == (sz - filled))
1513 break; /* completely drained two */
1514 filled = sz - remaining;
1516 *osize_p -= filled;
1517 return 0;
1520 static void cascade_free_fn(struct stream_filter *filter)
1522 struct cascade_filter *cas = (struct cascade_filter *)filter;
1523 free_stream_filter(cas->one);
1524 free_stream_filter(cas->two);
1525 free(filter);
1528 static struct stream_filter_vtbl cascade_vtbl = {
1529 cascade_filter_fn,
1530 cascade_free_fn,
1533 static struct stream_filter *cascade_filter(struct stream_filter *one,
1534 struct stream_filter *two)
1536 struct cascade_filter *cascade;
1538 if (!one || is_null_stream_filter(one))
1539 return two;
1540 if (!two || is_null_stream_filter(two))
1541 return one;
1543 cascade = xmalloc(sizeof(*cascade));
1544 cascade->one = one;
1545 cascade->two = two;
1546 cascade->end = cascade->ptr = 0;
1547 cascade->filter.vtbl = &cascade_vtbl;
1548 return (struct stream_filter *)cascade;
1552 * ident filter
1554 #define IDENT_DRAINING (-1)
1555 #define IDENT_SKIPPING (-2)
1556 struct ident_filter {
1557 struct stream_filter filter;
1558 struct strbuf left;
1559 int state;
1560 char ident[45]; /* ": x40 $" */
1563 static int is_foreign_ident(const char *str)
1565 int i;
1567 if (!skip_prefix(str, "$Id: ", &str))
1568 return 0;
1569 for (i = 0; str[i]; i++) {
1570 if (isspace(str[i]) && str[i+1] != '$')
1571 return 1;
1573 return 0;
1576 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1578 size_t to_drain = ident->left.len;
1580 if (*osize_p < to_drain)
1581 to_drain = *osize_p;
1582 if (to_drain) {
1583 memcpy(*output_p, ident->left.buf, to_drain);
1584 strbuf_remove(&ident->left, 0, to_drain);
1585 *output_p += to_drain;
1586 *osize_p -= to_drain;
1588 if (!ident->left.len)
1589 ident->state = 0;
1592 static int ident_filter_fn(struct stream_filter *filter,
1593 const char *input, size_t *isize_p,
1594 char *output, size_t *osize_p)
1596 struct ident_filter *ident = (struct ident_filter *)filter;
1597 static const char head[] = "$Id";
1599 if (!input) {
1600 /* drain upon eof */
1601 switch (ident->state) {
1602 default:
1603 strbuf_add(&ident->left, head, ident->state);
1604 case IDENT_SKIPPING:
1605 /* fallthru */
1606 case IDENT_DRAINING:
1607 ident_drain(ident, &output, osize_p);
1609 return 0;
1612 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1613 int ch;
1615 if (ident->state == IDENT_DRAINING) {
1616 ident_drain(ident, &output, osize_p);
1617 if (!*osize_p)
1618 break;
1619 continue;
1622 ch = *(input++);
1623 (*isize_p)--;
1625 if (ident->state == IDENT_SKIPPING) {
1627 * Skipping until '$' or LF, but keeping them
1628 * in case it is a foreign ident.
1630 strbuf_addch(&ident->left, ch);
1631 if (ch != '\n' && ch != '$')
1632 continue;
1633 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1634 strbuf_setlen(&ident->left, sizeof(head) - 1);
1635 strbuf_addstr(&ident->left, ident->ident);
1637 ident->state = IDENT_DRAINING;
1638 continue;
1641 if (ident->state < sizeof(head) &&
1642 head[ident->state] == ch) {
1643 ident->state++;
1644 continue;
1647 if (ident->state)
1648 strbuf_add(&ident->left, head, ident->state);
1649 if (ident->state == sizeof(head) - 1) {
1650 if (ch != ':' && ch != '$') {
1651 strbuf_addch(&ident->left, ch);
1652 ident->state = 0;
1653 continue;
1656 if (ch == ':') {
1657 strbuf_addch(&ident->left, ch);
1658 ident->state = IDENT_SKIPPING;
1659 } else {
1660 strbuf_addstr(&ident->left, ident->ident);
1661 ident->state = IDENT_DRAINING;
1663 continue;
1666 strbuf_addch(&ident->left, ch);
1667 ident->state = IDENT_DRAINING;
1669 return 0;
1672 static void ident_free_fn(struct stream_filter *filter)
1674 struct ident_filter *ident = (struct ident_filter *)filter;
1675 strbuf_release(&ident->left);
1676 free(filter);
1679 static struct stream_filter_vtbl ident_vtbl = {
1680 ident_filter_fn,
1681 ident_free_fn,
1684 static struct stream_filter *ident_filter(const unsigned char *sha1)
1686 struct ident_filter *ident = xmalloc(sizeof(*ident));
1688 xsnprintf(ident->ident, sizeof(ident->ident),
1689 ": %s $", sha1_to_hex(sha1));
1690 strbuf_init(&ident->left, 0);
1691 ident->filter.vtbl = &ident_vtbl;
1692 ident->state = 0;
1693 return (struct stream_filter *)ident;
1697 * Return an appropriately constructed filter for the path, or NULL if
1698 * the contents cannot be filtered without reading the whole thing
1699 * in-core.
1701 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1702 * large binary blob you would want us not to slurp into the memory!
1704 struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1706 struct conv_attrs ca;
1707 struct stream_filter *filter = NULL;
1709 convert_attrs(&ca, path);
1710 if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
1711 return NULL;
1713 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1714 return NULL;
1716 if (ca.ident)
1717 filter = ident_filter(sha1);
1719 if (output_eol(ca.crlf_action) == EOL_CRLF)
1720 filter = cascade_filter(filter, lf_to_crlf_filter());
1721 else
1722 filter = cascade_filter(filter, &null_filter_singleton);
1724 return filter;
1727 void free_stream_filter(struct stream_filter *filter)
1729 filter->vtbl->free(filter);
1732 int stream_filter(struct stream_filter *filter,
1733 const char *input, size_t *isize_p,
1734 char *output, size_t *osize_p)
1736 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);