pager.h: move declarations for pager.c functions from cache.h
[alt-git.git] / convert.c
blob5a2ea5308d6b1467b1ac1925507b1dacc5d809e4
1 #include "cache.h"
2 #include "advice.h"
3 #include "config.h"
4 #include "convert.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "object-store.h"
8 #include "attr.h"
9 #include "run-command.h"
10 #include "quote.h"
11 #include "sigchain.h"
12 #include "pkt-line.h"
13 #include "sub-process.h"
14 #include "trace.h"
15 #include "utf8.h"
16 #include "ll-merge.h"
17 #include "wrapper.h"
20 * convert.c - convert a file when checking it out and checking it in.
22 * This should use the pathname to decide on whether it wants to do some
23 * more interesting conversions (automatic gzip/unzip, general format
24 * conversions etc etc), but by default it just does automatic CRLF<->LF
25 * translation when the "text" attribute or "auto_crlf" option is set.
28 /* Stat bits: When BIN is set, the txt bits are unset */
29 #define CONVERT_STAT_BITS_TXT_LF 0x1
30 #define CONVERT_STAT_BITS_TXT_CRLF 0x2
31 #define CONVERT_STAT_BITS_BIN 0x4
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(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(&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(struct index_state *istate,
137 const char *path)
139 const char *ret;
140 unsigned long sz;
141 void *data = read_blob_data_from_index(istate, path, &sz);
142 ret = gather_convert_stats_ascii(data, sz);
143 free(data);
144 return ret;
147 const char *get_wt_convert_stats_ascii(const char *path)
149 const char *ret = "";
150 struct strbuf sb = STRBUF_INIT;
151 if (strbuf_read_file(&sb, path, 0) >= 0)
152 ret = gather_convert_stats_ascii(sb.buf, sb.len);
153 strbuf_release(&sb);
154 return ret;
157 static int text_eol_is_crlf(void)
159 if (auto_crlf == AUTO_CRLF_TRUE)
160 return 1;
161 else if (auto_crlf == AUTO_CRLF_INPUT)
162 return 0;
163 if (core_eol == EOL_CRLF)
164 return 1;
165 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
166 return 1;
167 return 0;
170 static enum eol output_eol(enum convert_crlf_action crlf_action)
172 switch (crlf_action) {
173 case CRLF_BINARY:
174 return EOL_UNSET;
175 case CRLF_TEXT_CRLF:
176 return EOL_CRLF;
177 case CRLF_TEXT_INPUT:
178 return EOL_LF;
179 case CRLF_UNDEFINED:
180 case CRLF_AUTO_CRLF:
181 return EOL_CRLF;
182 case CRLF_AUTO_INPUT:
183 return EOL_LF;
184 case CRLF_TEXT:
185 case CRLF_AUTO:
186 /* fall through */
187 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
189 warning(_("illegal crlf_action %d"), (int)crlf_action);
190 return core_eol;
193 static void check_global_conv_flags_eol(const char *path,
194 struct text_stat *old_stats, struct text_stat *new_stats,
195 int conv_flags)
197 if (old_stats->crlf && !new_stats->crlf ) {
199 * CRLFs would not be restored by checkout
201 if (conv_flags & CONV_EOL_RNDTRP_DIE)
202 die(_("CRLF would be replaced by LF in %s"), path);
203 else if (conv_flags & CONV_EOL_RNDTRP_WARN)
204 warning(_("in the working copy of '%s', CRLF will be"
205 " replaced by LF the next time Git touches"
206 " it"), path);
207 } else if (old_stats->lonelf && !new_stats->lonelf ) {
209 * CRLFs would be added by checkout
211 if (conv_flags & CONV_EOL_RNDTRP_DIE)
212 die(_("LF would be replaced by CRLF in %s"), path);
213 else if (conv_flags & CONV_EOL_RNDTRP_WARN)
214 warning(_("in the working copy of '%s', LF will be"
215 " replaced by CRLF the next time Git touches"
216 " it"), path);
220 static int has_crlf_in_index(struct index_state *istate, const char *path)
222 unsigned long sz;
223 void *data;
224 const char *crp;
225 int has_crlf = 0;
227 data = read_blob_data_from_index(istate, path, &sz);
228 if (!data)
229 return 0;
231 crp = memchr(data, '\r', sz);
232 if (crp) {
233 unsigned int ret_stats;
234 ret_stats = gather_convert_stats(data, sz);
235 if (!(ret_stats & CONVERT_STAT_BITS_BIN) &&
236 (ret_stats & CONVERT_STAT_BITS_TXT_CRLF))
237 has_crlf = 1;
239 free(data);
240 return has_crlf;
243 static int will_convert_lf_to_crlf(struct text_stat *stats,
244 enum convert_crlf_action crlf_action)
246 if (output_eol(crlf_action) != EOL_CRLF)
247 return 0;
248 /* No "naked" LF? Nothing to convert, regardless. */
249 if (!stats->lonelf)
250 return 0;
252 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
253 /* If we have any CR or CRLF line endings, we do not touch it */
254 /* This is the new safer autocrlf-handling */
255 if (stats->lonecr || stats->crlf)
256 return 0;
258 if (convert_is_binary(stats))
259 return 0;
261 return 1;
265 static int validate_encoding(const char *path, const char *enc,
266 const char *data, size_t len, int die_on_error)
268 const char *stripped;
270 /* We only check for UTF here as UTF?? can be an alias for UTF-?? */
271 if (skip_iprefix(enc, "UTF", &stripped)) {
272 skip_prefix(stripped, "-", &stripped);
275 * Check for detectable errors in UTF encodings
277 if (has_prohibited_utf_bom(enc, data, len)) {
278 const char *error_msg = _(
279 "BOM is prohibited in '%s' if encoded as %s");
281 * This advice is shown for UTF-??BE and UTF-??LE encodings.
282 * We cut off the last two characters of the encoding name
283 * to generate the encoding name suitable for BOMs.
285 const char *advise_msg = _(
286 "The file '%s' contains a byte order "
287 "mark (BOM). Please use UTF-%.*s as "
288 "working-tree-encoding.");
289 int stripped_len = strlen(stripped) - strlen("BE");
290 advise(advise_msg, path, stripped_len, stripped);
291 if (die_on_error)
292 die(error_msg, path, enc);
293 else {
294 return error(error_msg, path, enc);
297 } else if (is_missing_required_utf_bom(enc, data, len)) {
298 const char *error_msg = _(
299 "BOM is required in '%s' if encoded as %s");
300 const char *advise_msg = _(
301 "The file '%s' is missing a byte order "
302 "mark (BOM). Please use UTF-%sBE or UTF-%sLE "
303 "(depending on the byte order) as "
304 "working-tree-encoding.");
305 advise(advise_msg, path, stripped, stripped);
306 if (die_on_error)
307 die(error_msg, path, enc);
308 else {
309 return error(error_msg, path, enc);
314 return 0;
317 static void trace_encoding(const char *context, const char *path,
318 const char *encoding, const char *buf, size_t len)
320 static struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING);
321 struct strbuf trace = STRBUF_INIT;
322 int i;
324 strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding);
325 for (i = 0; i < len && buf; ++i) {
326 strbuf_addf(
327 &trace, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c",
329 (unsigned char) buf[i],
330 (buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '),
331 ((i+1) % 8 && (i+1) < len ? ' ' : '\n')
334 strbuf_addchars(&trace, '\n', 1);
336 trace_strbuf(&coe, &trace);
337 strbuf_release(&trace);
340 static int check_roundtrip(const char *enc_name)
343 * check_roundtrip_encoding contains a string of comma and/or
344 * space separated encodings (eg. "UTF-16, ASCII, CP1125").
345 * Search for the given encoding in that string.
347 const char *found = strcasestr(check_roundtrip_encoding, enc_name);
348 const char *next;
349 int len;
350 if (!found)
351 return 0;
352 next = found + strlen(enc_name);
353 len = strlen(check_roundtrip_encoding);
354 return (found && (
356 * check that the found encoding is at the
357 * beginning of check_roundtrip_encoding or
358 * that it is prefixed with a space or comma
360 found == check_roundtrip_encoding || (
361 (isspace(found[-1]) || found[-1] == ',')
363 ) && (
365 * check that the found encoding is at the
366 * end of check_roundtrip_encoding or
367 * that it is suffixed with a space or comma
369 next == check_roundtrip_encoding + len || (
370 next < check_roundtrip_encoding + len &&
371 (isspace(next[0]) || next[0] == ',')
376 static const char *default_encoding = "UTF-8";
378 static int encode_to_git(const char *path, const char *src, size_t src_len,
379 struct strbuf *buf, const char *enc, int conv_flags)
381 char *dst;
382 size_t dst_len;
383 int die_on_error = conv_flags & CONV_WRITE_OBJECT;
386 * No encoding is specified or there is nothing to encode.
387 * Tell the caller that the content was not modified.
389 if (!enc || (src && !src_len))
390 return 0;
393 * Looks like we got called from "would_convert_to_git()".
394 * This means Git wants to know if it would encode (= modify!)
395 * the content. Let's answer with "yes", since an encoding was
396 * specified.
398 if (!buf && !src)
399 return 1;
401 if (validate_encoding(path, enc, src, src_len, die_on_error))
402 return 0;
404 trace_encoding("source", path, enc, src, src_len);
405 dst = reencode_string_len(src, src_len, default_encoding, enc,
406 &dst_len);
407 if (!dst) {
409 * We could add the blob "as-is" to Git. However, on checkout
410 * we would try to re-encode to the original encoding. This
411 * would fail and we would leave the user with a messed-up
412 * working tree. Let's try to avoid this by screaming loud.
414 const char* msg = _("failed to encode '%s' from %s to %s");
415 if (die_on_error)
416 die(msg, path, enc, default_encoding);
417 else {
418 error(msg, path, enc, default_encoding);
419 return 0;
422 trace_encoding("destination", path, default_encoding, dst, dst_len);
425 * UTF supports lossless conversion round tripping [1] and conversions
426 * between UTF and other encodings are mostly round trip safe as
427 * Unicode aims to be a superset of all other character encodings.
428 * However, certain encodings (e.g. SHIFT-JIS) are known to have round
429 * trip issues [2]. Check the round trip conversion for all encodings
430 * listed in core.checkRoundtripEncoding.
432 * The round trip check is only performed if content is written to Git.
433 * This ensures that no information is lost during conversion to/from
434 * the internal UTF-8 representation.
436 * Please note, the code below is not tested because I was not able to
437 * generate a faulty round trip without an iconv error. Iconv errors
438 * are already caught above.
440 * [1] http://unicode.org/faq/utf_bom.html#gen2
441 * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode
443 if (die_on_error && check_roundtrip(enc)) {
444 char *re_src;
445 size_t re_src_len;
447 re_src = reencode_string_len(dst, dst_len,
448 enc, default_encoding,
449 &re_src_len);
451 trace_printf("Checking roundtrip encoding for %s...\n", enc);
452 trace_encoding("reencoded source", path, enc,
453 re_src, re_src_len);
455 if (!re_src || src_len != re_src_len ||
456 memcmp(src, re_src, src_len)) {
457 const char* msg = _("encoding '%s' from %s to %s and "
458 "back is not the same");
459 die(msg, path, enc, default_encoding);
462 free(re_src);
465 strbuf_attach(buf, dst, dst_len, dst_len + 1);
466 return 1;
469 static int encode_to_worktree(const char *path, const char *src, size_t src_len,
470 struct strbuf *buf, const char *enc)
472 char *dst;
473 size_t dst_len;
476 * No encoding is specified or there is nothing to encode.
477 * Tell the caller that the content was not modified.
479 if (!enc || (src && !src_len))
480 return 0;
482 dst = reencode_string_len(src, src_len, enc, default_encoding,
483 &dst_len);
484 if (!dst) {
485 error(_("failed to encode '%s' from %s to %s"),
486 path, default_encoding, enc);
487 return 0;
490 strbuf_attach(buf, dst, dst_len, dst_len + 1);
491 return 1;
494 static int crlf_to_git(struct index_state *istate,
495 const char *path, const char *src, size_t len,
496 struct strbuf *buf,
497 enum convert_crlf_action crlf_action, int conv_flags)
499 struct text_stat stats;
500 char *dst;
501 int convert_crlf_into_lf;
503 if (crlf_action == CRLF_BINARY ||
504 (src && !len))
505 return 0;
508 * If we are doing a dry-run and have no source buffer, there is
509 * nothing to analyze; we must assume we would convert.
511 if (!buf && !src)
512 return 1;
514 gather_stats(src, len, &stats);
515 /* Optimization: No CRLF? Nothing to convert, regardless. */
516 convert_crlf_into_lf = !!stats.crlf;
518 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
519 if (convert_is_binary(&stats))
520 return 0;
522 * If the file in the index has any CR in it, do not
523 * convert. This is the new safer autocrlf handling,
524 * unless we want to renormalize in a merge or
525 * cherry-pick.
527 if ((!(conv_flags & CONV_EOL_RENORMALIZE)) &&
528 has_crlf_in_index(istate, path))
529 convert_crlf_into_lf = 0;
531 if (((conv_flags & CONV_EOL_RNDTRP_WARN) ||
532 ((conv_flags & CONV_EOL_RNDTRP_DIE) && len))) {
533 struct text_stat new_stats;
534 memcpy(&new_stats, &stats, sizeof(new_stats));
535 /* simulate "git add" */
536 if (convert_crlf_into_lf) {
537 new_stats.lonelf += new_stats.crlf;
538 new_stats.crlf = 0;
540 /* simulate "git checkout" */
541 if (will_convert_lf_to_crlf(&new_stats, crlf_action)) {
542 new_stats.crlf += new_stats.lonelf;
543 new_stats.lonelf = 0;
545 check_global_conv_flags_eol(path, &stats, &new_stats, conv_flags);
547 if (!convert_crlf_into_lf)
548 return 0;
551 * At this point all of our source analysis is done, and we are sure we
552 * would convert. If we are in dry-run mode, we can give an answer.
554 if (!buf)
555 return 1;
557 /* only grow if not in place */
558 if (strbuf_avail(buf) + buf->len < len)
559 strbuf_grow(buf, len - buf->len);
560 dst = buf->buf;
561 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
563 * If we guessed, we already know we rejected a file with
564 * lone CR, and we can strip a CR without looking at what
565 * follow it.
567 do {
568 unsigned char c = *src++;
569 if (c != '\r')
570 *dst++ = c;
571 } while (--len);
572 } else {
573 do {
574 unsigned char c = *src++;
575 if (! (c == '\r' && (1 < len && *src == '\n')))
576 *dst++ = c;
577 } while (--len);
579 strbuf_setlen(buf, dst - buf->buf);
580 return 1;
583 static int crlf_to_worktree(const char *src, size_t len, struct strbuf *buf,
584 enum convert_crlf_action crlf_action)
586 char *to_free = NULL;
587 struct text_stat stats;
589 if (!len || output_eol(crlf_action) != EOL_CRLF)
590 return 0;
592 gather_stats(src, len, &stats);
593 if (!will_convert_lf_to_crlf(&stats, crlf_action))
594 return 0;
596 /* are we "faking" in place editing ? */
597 if (src == buf->buf)
598 to_free = strbuf_detach(buf, NULL);
600 strbuf_grow(buf, len + stats.lonelf);
601 for (;;) {
602 const char *nl = memchr(src, '\n', len);
603 if (!nl)
604 break;
605 if (nl > src && nl[-1] == '\r') {
606 strbuf_add(buf, src, nl + 1 - src);
607 } else {
608 strbuf_add(buf, src, nl - src);
609 strbuf_addstr(buf, "\r\n");
611 len -= nl + 1 - src;
612 src = nl + 1;
614 strbuf_add(buf, src, len);
616 free(to_free);
617 return 1;
620 struct filter_params {
621 const char *src;
622 size_t size;
623 int fd;
624 const char *cmd;
625 const char *path;
628 static int filter_buffer_or_fd(int in UNUSED, int out, void *data)
631 * Spawn cmd and feed the buffer contents through its stdin.
633 struct child_process child_process = CHILD_PROCESS_INIT;
634 struct filter_params *params = (struct filter_params *)data;
635 int write_err, status;
637 /* apply % substitution to cmd */
638 struct strbuf cmd = STRBUF_INIT;
639 struct strbuf path = STRBUF_INIT;
640 struct strbuf_expand_dict_entry dict[] = {
641 { "f", NULL, },
642 { NULL, NULL, },
645 /* quote the path to preserve spaces, etc. */
646 sq_quote_buf(&path, params->path);
647 dict[0].value = path.buf;
649 /* expand all %f with the quoted path */
650 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
651 strbuf_release(&path);
653 strvec_push(&child_process.args, cmd.buf);
654 child_process.use_shell = 1;
655 child_process.in = -1;
656 child_process.out = out;
658 if (start_command(&child_process)) {
659 strbuf_release(&cmd);
660 return error(_("cannot fork to run external filter '%s'"),
661 params->cmd);
664 sigchain_push(SIGPIPE, SIG_IGN);
666 if (params->src) {
667 write_err = (write_in_full(child_process.in,
668 params->src, params->size) < 0);
669 if (errno == EPIPE)
670 write_err = 0;
671 } else {
672 write_err = copy_fd(params->fd, child_process.in);
673 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
674 write_err = 0;
677 if (close(child_process.in))
678 write_err = 1;
679 if (write_err)
680 error(_("cannot feed the input to external filter '%s'"),
681 params->cmd);
683 sigchain_pop(SIGPIPE);
685 status = finish_command(&child_process);
686 if (status)
687 error(_("external filter '%s' failed %d"), params->cmd, status);
689 strbuf_release(&cmd);
690 return (write_err || status);
693 static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
694 struct strbuf *dst, const char *cmd)
697 * Create a pipeline to have the command filter the buffer's
698 * contents.
700 * (child --> cmd) --> us
702 int err = 0;
703 struct strbuf nbuf = STRBUF_INIT;
704 struct async async;
705 struct filter_params params;
707 memset(&async, 0, sizeof(async));
708 async.proc = filter_buffer_or_fd;
709 async.data = &params;
710 async.out = -1;
711 params.src = src;
712 params.size = len;
713 params.fd = fd;
714 params.cmd = cmd;
715 params.path = path;
717 fflush(NULL);
718 if (start_async(&async))
719 return 0; /* error was already reported */
721 if (strbuf_read(&nbuf, async.out, 0) < 0) {
722 err = error(_("read from external filter '%s' failed"), cmd);
724 if (close(async.out)) {
725 err = error(_("read from external filter '%s' failed"), cmd);
727 if (finish_async(&async)) {
728 err = error(_("external filter '%s' failed"), cmd);
731 if (!err) {
732 strbuf_swap(dst, &nbuf);
734 strbuf_release(&nbuf);
735 return !err;
738 #define CAP_CLEAN (1u<<0)
739 #define CAP_SMUDGE (1u<<1)
740 #define CAP_DELAY (1u<<2)
742 struct cmd2process {
743 struct subprocess_entry subprocess; /* must be the first member! */
744 unsigned int supported_capabilities;
747 static int subprocess_map_initialized;
748 static struct hashmap subprocess_map;
750 static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
752 static int versions[] = {2, 0};
753 static struct subprocess_capability capabilities[] = {
754 { "clean", CAP_CLEAN },
755 { "smudge", CAP_SMUDGE },
756 { "delay", CAP_DELAY },
757 { NULL, 0 }
759 struct cmd2process *entry = (struct cmd2process *)subprocess;
760 return subprocess_handshake(subprocess, "git-filter", versions, NULL,
761 capabilities,
762 &entry->supported_capabilities);
765 static void handle_filter_error(const struct strbuf *filter_status,
766 struct cmd2process *entry,
767 const unsigned int wanted_capability)
769 if (!strcmp(filter_status->buf, "error"))
770 ; /* The filter signaled a problem with the file. */
771 else if (!strcmp(filter_status->buf, "abort") && wanted_capability) {
773 * The filter signaled a permanent problem. Don't try to filter
774 * files with the same command for the lifetime of the current
775 * Git process.
777 entry->supported_capabilities &= ~wanted_capability;
778 } else {
780 * Something went wrong with the protocol filter.
781 * Force shutdown and restart if another blob requires filtering.
783 error(_("external filter '%s' failed"), entry->subprocess.cmd);
784 subprocess_stop(&subprocess_map, &entry->subprocess);
785 free(entry);
789 static int apply_multi_file_filter(const char *path, const char *src, size_t len,
790 int fd, struct strbuf *dst, const char *cmd,
791 const unsigned int wanted_capability,
792 const struct checkout_metadata *meta,
793 struct delayed_checkout *dco)
795 int err;
796 int can_delay = 0;
797 struct cmd2process *entry;
798 struct child_process *process;
799 struct strbuf nbuf = STRBUF_INIT;
800 struct strbuf filter_status = STRBUF_INIT;
801 const char *filter_type;
803 if (!subprocess_map_initialized) {
804 subprocess_map_initialized = 1;
805 hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
806 entry = NULL;
807 } else {
808 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
811 fflush(NULL);
813 if (!entry) {
814 entry = xmalloc(sizeof(*entry));
815 entry->supported_capabilities = 0;
817 if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
818 free(entry);
819 return 0;
822 process = &entry->subprocess.process;
824 if (!(entry->supported_capabilities & wanted_capability))
825 return 0;
827 if (wanted_capability & CAP_CLEAN)
828 filter_type = "clean";
829 else if (wanted_capability & CAP_SMUDGE)
830 filter_type = "smudge";
831 else
832 die(_("unexpected filter type"));
834 sigchain_push(SIGPIPE, SIG_IGN);
836 assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
837 err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
838 if (err)
839 goto done;
841 err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
842 if (err) {
843 error(_("path name too long for external filter"));
844 goto done;
847 err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
848 if (err)
849 goto done;
851 if (meta && meta->refname) {
852 err = packet_write_fmt_gently(process->in, "ref=%s\n", meta->refname);
853 if (err)
854 goto done;
857 if (meta && !is_null_oid(&meta->treeish)) {
858 err = packet_write_fmt_gently(process->in, "treeish=%s\n", oid_to_hex(&meta->treeish));
859 if (err)
860 goto done;
863 if (meta && !is_null_oid(&meta->blob)) {
864 err = packet_write_fmt_gently(process->in, "blob=%s\n", oid_to_hex(&meta->blob));
865 if (err)
866 goto done;
869 if ((entry->supported_capabilities & CAP_DELAY) &&
870 dco && dco->state == CE_CAN_DELAY) {
871 can_delay = 1;
872 err = packet_write_fmt_gently(process->in, "can-delay=1\n");
873 if (err)
874 goto done;
877 err = packet_flush_gently(process->in);
878 if (err)
879 goto done;
881 if (fd >= 0)
882 err = write_packetized_from_fd_no_flush(fd, process->in);
883 else
884 err = write_packetized_from_buf_no_flush(src, len, process->in);
885 if (err)
886 goto done;
888 err = packet_flush_gently(process->in);
889 if (err)
890 goto done;
892 err = subprocess_read_status(process->out, &filter_status);
893 if (err)
894 goto done;
896 if (can_delay && !strcmp(filter_status.buf, "delayed")) {
897 string_list_insert(&dco->filters, cmd);
898 string_list_insert(&dco->paths, path);
899 } else {
900 /* The filter got the blob and wants to send us a response. */
901 err = strcmp(filter_status.buf, "success");
902 if (err)
903 goto done;
905 err = read_packetized_to_strbuf(process->out, &nbuf,
906 PACKET_READ_GENTLE_ON_EOF) < 0;
907 if (err)
908 goto done;
910 err = subprocess_read_status(process->out, &filter_status);
911 if (err)
912 goto done;
914 err = strcmp(filter_status.buf, "success");
917 done:
918 sigchain_pop(SIGPIPE);
920 if (err)
921 handle_filter_error(&filter_status, entry, wanted_capability);
922 else
923 strbuf_swap(dst, &nbuf);
924 strbuf_release(&nbuf);
925 strbuf_release(&filter_status);
926 return !err;
930 int async_query_available_blobs(const char *cmd, struct string_list *available_paths)
932 int err;
933 char *line;
934 struct cmd2process *entry;
935 struct child_process *process;
936 struct strbuf filter_status = STRBUF_INIT;
938 assert(subprocess_map_initialized);
939 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
940 if (!entry) {
941 error(_("external filter '%s' is not available anymore although "
942 "not all paths have been filtered"), cmd);
943 return 0;
945 process = &entry->subprocess.process;
946 sigchain_push(SIGPIPE, SIG_IGN);
948 err = packet_write_fmt_gently(
949 process->in, "command=list_available_blobs\n");
950 if (err)
951 goto done;
953 err = packet_flush_gently(process->in);
954 if (err)
955 goto done;
957 while ((line = packet_read_line(process->out, NULL))) {
958 const char *path;
959 if (skip_prefix(line, "pathname=", &path))
960 string_list_insert(available_paths, xstrdup(path));
961 else
962 ; /* ignore unknown keys */
965 err = subprocess_read_status(process->out, &filter_status);
966 if (err)
967 goto done;
969 err = strcmp(filter_status.buf, "success");
971 done:
972 sigchain_pop(SIGPIPE);
974 if (err)
975 handle_filter_error(&filter_status, entry, 0);
976 strbuf_release(&filter_status);
977 return !err;
980 static struct convert_driver {
981 const char *name;
982 struct convert_driver *next;
983 const char *smudge;
984 const char *clean;
985 const char *process;
986 int required;
987 } *user_convert, **user_convert_tail;
989 static int apply_filter(const char *path, const char *src, size_t len,
990 int fd, struct strbuf *dst, struct convert_driver *drv,
991 const unsigned int wanted_capability,
992 const struct checkout_metadata *meta,
993 struct delayed_checkout *dco)
995 const char *cmd = NULL;
997 if (!drv)
998 return 0;
1000 if (!dst)
1001 return 1;
1003 if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean)
1004 cmd = drv->clean;
1005 else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge)
1006 cmd = drv->smudge;
1008 if (cmd && *cmd)
1009 return apply_single_file_filter(path, src, len, fd, dst, cmd);
1010 else if (drv->process && *drv->process)
1011 return apply_multi_file_filter(path, src, len, fd, dst,
1012 drv->process, wanted_capability, meta, dco);
1014 return 0;
1017 static int read_convert_config(const char *var, const char *value, void *cb UNUSED)
1019 const char *key, *name;
1020 size_t namelen;
1021 struct convert_driver *drv;
1024 * External conversion drivers are configured using
1025 * "filter.<name>.variable".
1027 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
1028 return 0;
1029 for (drv = user_convert; drv; drv = drv->next)
1030 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
1031 break;
1032 if (!drv) {
1033 CALLOC_ARRAY(drv, 1);
1034 drv->name = xmemdupz(name, namelen);
1035 *user_convert_tail = drv;
1036 user_convert_tail = &(drv->next);
1040 * filter.<name>.smudge and filter.<name>.clean specifies
1041 * the command line:
1043 * command-line
1045 * The command-line will not be interpolated in any way.
1048 if (!strcmp("smudge", key))
1049 return git_config_string(&drv->smudge, var, value);
1051 if (!strcmp("clean", key))
1052 return git_config_string(&drv->clean, var, value);
1054 if (!strcmp("process", key))
1055 return git_config_string(&drv->process, var, value);
1057 if (!strcmp("required", key)) {
1058 drv->required = git_config_bool(var, value);
1059 return 0;
1062 return 0;
1065 static int count_ident(const char *cp, unsigned long size)
1068 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
1070 int cnt = 0;
1071 char ch;
1073 while (size) {
1074 ch = *cp++;
1075 size--;
1076 if (ch != '$')
1077 continue;
1078 if (size < 3)
1079 break;
1080 if (memcmp("Id", cp, 2))
1081 continue;
1082 ch = cp[2];
1083 cp += 3;
1084 size -= 3;
1085 if (ch == '$')
1086 cnt++; /* $Id$ */
1087 if (ch != ':')
1088 continue;
1091 * "$Id: ... "; scan up to the closing dollar sign and discard.
1093 while (size) {
1094 ch = *cp++;
1095 size--;
1096 if (ch == '$') {
1097 cnt++;
1098 break;
1100 if (ch == '\n')
1101 break;
1104 return cnt;
1107 static int ident_to_git(const char *src, size_t len,
1108 struct strbuf *buf, int ident)
1110 char *dst, *dollar;
1112 if (!ident || (src && !count_ident(src, len)))
1113 return 0;
1115 if (!buf)
1116 return 1;
1118 /* only grow if not in place */
1119 if (strbuf_avail(buf) + buf->len < len)
1120 strbuf_grow(buf, len - buf->len);
1121 dst = buf->buf;
1122 for (;;) {
1123 dollar = memchr(src, '$', len);
1124 if (!dollar)
1125 break;
1126 memmove(dst, src, dollar + 1 - src);
1127 dst += dollar + 1 - src;
1128 len -= dollar + 1 - src;
1129 src = dollar + 1;
1131 if (len > 3 && !memcmp(src, "Id:", 3)) {
1132 dollar = memchr(src + 3, '$', len - 3);
1133 if (!dollar)
1134 break;
1135 if (memchr(src + 3, '\n', dollar - src - 3)) {
1136 /* Line break before the next dollar. */
1137 continue;
1140 memcpy(dst, "Id$", 3);
1141 dst += 3;
1142 len -= dollar + 1 - src;
1143 src = dollar + 1;
1146 memmove(dst, src, len);
1147 strbuf_setlen(buf, dst + len - buf->buf);
1148 return 1;
1151 static int ident_to_worktree(const char *src, size_t len,
1152 struct strbuf *buf, int ident)
1154 struct object_id oid;
1155 char *to_free = NULL, *dollar, *spc;
1156 int cnt;
1158 if (!ident)
1159 return 0;
1161 cnt = count_ident(src, len);
1162 if (!cnt)
1163 return 0;
1165 /* are we "faking" in place editing ? */
1166 if (src == buf->buf)
1167 to_free = strbuf_detach(buf, NULL);
1168 hash_object_file(the_hash_algo, src, len, OBJ_BLOB, &oid);
1170 strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3));
1171 for (;;) {
1172 /* step 1: run to the next '$' */
1173 dollar = memchr(src, '$', len);
1174 if (!dollar)
1175 break;
1176 strbuf_add(buf, src, dollar + 1 - src);
1177 len -= dollar + 1 - src;
1178 src = dollar + 1;
1180 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
1181 if (len < 3 || memcmp("Id", src, 2))
1182 continue;
1184 /* step 3: skip over Id$ or Id:xxxxx$ */
1185 if (src[2] == '$') {
1186 src += 3;
1187 len -= 3;
1188 } else if (src[2] == ':') {
1190 * It's possible that an expanded Id has crept its way into the
1191 * repository, we cope with that by stripping the expansion out.
1192 * This is probably not a good idea, since it will cause changes
1193 * on checkout, which won't go away by stash, but let's keep it
1194 * for git-style ids.
1196 dollar = memchr(src + 3, '$', len - 3);
1197 if (!dollar) {
1198 /* incomplete keyword, no more '$', so just quit the loop */
1199 break;
1202 if (memchr(src + 3, '\n', dollar - src - 3)) {
1203 /* Line break before the next dollar. */
1204 continue;
1207 spc = memchr(src + 4, ' ', dollar - src - 4);
1208 if (spc && spc < dollar-1) {
1209 /* There are spaces in unexpected places.
1210 * This is probably an id from some other
1211 * versioning system. Keep it for now.
1213 continue;
1216 len -= dollar + 1 - src;
1217 src = dollar + 1;
1218 } else {
1219 /* it wasn't a "Id$" or "Id:xxxx$" */
1220 continue;
1223 /* step 4: substitute */
1224 strbuf_addstr(buf, "Id: ");
1225 strbuf_addstr(buf, oid_to_hex(&oid));
1226 strbuf_addstr(buf, " $");
1228 strbuf_add(buf, src, len);
1230 free(to_free);
1231 return 1;
1234 static const char *git_path_check_encoding(struct attr_check_item *check)
1236 const char *value = check->value;
1238 if (ATTR_UNSET(value) || !strlen(value))
1239 return NULL;
1241 if (ATTR_TRUE(value) || ATTR_FALSE(value)) {
1242 die(_("true/false are no valid working-tree-encodings"));
1245 /* Don't encode to the default encoding */
1246 if (same_encoding(value, default_encoding))
1247 return NULL;
1249 return value;
1252 static enum convert_crlf_action git_path_check_crlf(struct attr_check_item *check)
1254 const char *value = check->value;
1256 if (ATTR_TRUE(value))
1257 return CRLF_TEXT;
1258 else if (ATTR_FALSE(value))
1259 return CRLF_BINARY;
1260 else if (ATTR_UNSET(value))
1262 else if (!strcmp(value, "input"))
1263 return CRLF_TEXT_INPUT;
1264 else if (!strcmp(value, "auto"))
1265 return CRLF_AUTO;
1266 return CRLF_UNDEFINED;
1269 static enum eol git_path_check_eol(struct attr_check_item *check)
1271 const char *value = check->value;
1273 if (ATTR_UNSET(value))
1275 else if (!strcmp(value, "lf"))
1276 return EOL_LF;
1277 else if (!strcmp(value, "crlf"))
1278 return EOL_CRLF;
1279 return EOL_UNSET;
1282 static struct convert_driver *git_path_check_convert(struct attr_check_item *check)
1284 const char *value = check->value;
1285 struct convert_driver *drv;
1287 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
1288 return NULL;
1289 for (drv = user_convert; drv; drv = drv->next)
1290 if (!strcmp(value, drv->name))
1291 return drv;
1292 return NULL;
1295 static int git_path_check_ident(struct attr_check_item *check)
1297 const char *value = check->value;
1299 return !!ATTR_TRUE(value);
1302 static struct attr_check *check;
1304 void convert_attrs(struct index_state *istate,
1305 struct conv_attrs *ca, const char *path)
1307 struct attr_check_item *ccheck = NULL;
1309 if (!check) {
1310 check = attr_check_initl("crlf", "ident", "filter",
1311 "eol", "text", "working-tree-encoding",
1312 NULL);
1313 user_convert_tail = &user_convert;
1314 git_config(read_convert_config, NULL);
1317 git_check_attr(istate, NULL, path, check);
1318 ccheck = check->items;
1319 ca->crlf_action = git_path_check_crlf(ccheck + 4);
1320 if (ca->crlf_action == CRLF_UNDEFINED)
1321 ca->crlf_action = git_path_check_crlf(ccheck + 0);
1322 ca->ident = git_path_check_ident(ccheck + 1);
1323 ca->drv = git_path_check_convert(ccheck + 2);
1324 if (ca->crlf_action != CRLF_BINARY) {
1325 enum eol eol_attr = git_path_check_eol(ccheck + 3);
1326 if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
1327 ca->crlf_action = CRLF_AUTO_INPUT;
1328 else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
1329 ca->crlf_action = CRLF_AUTO_CRLF;
1330 else if (eol_attr == EOL_LF)
1331 ca->crlf_action = CRLF_TEXT_INPUT;
1332 else if (eol_attr == EOL_CRLF)
1333 ca->crlf_action = CRLF_TEXT_CRLF;
1335 ca->working_tree_encoding = git_path_check_encoding(ccheck + 5);
1337 /* Save attr and make a decision for action */
1338 ca->attr_action = ca->crlf_action;
1339 if (ca->crlf_action == CRLF_TEXT)
1340 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
1341 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
1342 ca->crlf_action = CRLF_BINARY;
1343 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
1344 ca->crlf_action = CRLF_AUTO_CRLF;
1345 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
1346 ca->crlf_action = CRLF_AUTO_INPUT;
1349 void reset_parsed_attributes(void)
1351 struct convert_driver *drv, *next;
1353 attr_check_free(check);
1354 check = NULL;
1355 reset_merge_attributes();
1357 for (drv = user_convert; drv; drv = next) {
1358 next = drv->next;
1359 free((void *)drv->name);
1360 free(drv);
1362 user_convert = NULL;
1363 user_convert_tail = NULL;
1366 int would_convert_to_git_filter_fd(struct index_state *istate, const char *path)
1368 struct conv_attrs ca;
1370 convert_attrs(istate, &ca, path);
1371 if (!ca.drv)
1372 return 0;
1375 * Apply a filter to an fd only if the filter is required to succeed.
1376 * We must die if the filter fails, because the original data before
1377 * filtering is not available.
1379 if (!ca.drv->required)
1380 return 0;
1382 return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL, NULL);
1385 const char *get_convert_attr_ascii(struct index_state *istate, const char *path)
1387 struct conv_attrs ca;
1389 convert_attrs(istate, &ca, path);
1390 switch (ca.attr_action) {
1391 case CRLF_UNDEFINED:
1392 return "";
1393 case CRLF_BINARY:
1394 return "-text";
1395 case CRLF_TEXT:
1396 return "text";
1397 case CRLF_TEXT_INPUT:
1398 return "text eol=lf";
1399 case CRLF_TEXT_CRLF:
1400 return "text eol=crlf";
1401 case CRLF_AUTO:
1402 return "text=auto";
1403 case CRLF_AUTO_CRLF:
1404 return "text=auto eol=crlf";
1405 case CRLF_AUTO_INPUT:
1406 return "text=auto eol=lf";
1408 return "";
1411 int convert_to_git(struct index_state *istate,
1412 const char *path, const char *src, size_t len,
1413 struct strbuf *dst, int conv_flags)
1415 int ret = 0;
1416 struct conv_attrs ca;
1418 convert_attrs(istate, &ca, path);
1420 ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL, NULL);
1421 if (!ret && ca.drv && ca.drv->required)
1422 die(_("%s: clean filter '%s' failed"), path, ca.drv->name);
1424 if (ret && dst) {
1425 src = dst->buf;
1426 len = dst->len;
1429 ret |= encode_to_git(path, src, len, dst, ca.working_tree_encoding, conv_flags);
1430 if (ret && dst) {
1431 src = dst->buf;
1432 len = dst->len;
1435 if (!(conv_flags & CONV_EOL_KEEP_CRLF)) {
1436 ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, conv_flags);
1437 if (ret && dst) {
1438 src = dst->buf;
1439 len = dst->len;
1442 return ret | ident_to_git(src, len, dst, ca.ident);
1445 void convert_to_git_filter_fd(struct index_state *istate,
1446 const char *path, int fd, struct strbuf *dst,
1447 int conv_flags)
1449 struct conv_attrs ca;
1450 convert_attrs(istate, &ca, path);
1452 assert(ca.drv);
1454 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL, NULL))
1455 die(_("%s: clean filter '%s' failed"), path, ca.drv->name);
1457 encode_to_git(path, dst->buf, dst->len, dst, ca.working_tree_encoding, conv_flags);
1458 crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags);
1459 ident_to_git(dst->buf, dst->len, dst, ca.ident);
1462 static int convert_to_working_tree_ca_internal(const struct conv_attrs *ca,
1463 const char *path, const char *src,
1464 size_t len, struct strbuf *dst,
1465 int normalizing,
1466 const struct checkout_metadata *meta,
1467 struct delayed_checkout *dco)
1469 int ret = 0, ret_filter = 0;
1471 ret |= ident_to_worktree(src, len, dst, ca->ident);
1472 if (ret) {
1473 src = dst->buf;
1474 len = dst->len;
1477 * CRLF conversion can be skipped if normalizing, unless there
1478 * is a smudge or process filter (even if the process filter doesn't
1479 * support smudge). The filters might expect CRLFs.
1481 if ((ca->drv && (ca->drv->smudge || ca->drv->process)) || !normalizing) {
1482 ret |= crlf_to_worktree(src, len, dst, ca->crlf_action);
1483 if (ret) {
1484 src = dst->buf;
1485 len = dst->len;
1489 ret |= encode_to_worktree(path, src, len, dst, ca->working_tree_encoding);
1490 if (ret) {
1491 src = dst->buf;
1492 len = dst->len;
1495 ret_filter = apply_filter(
1496 path, src, len, -1, dst, ca->drv, CAP_SMUDGE, meta, dco);
1497 if (!ret_filter && ca->drv && ca->drv->required)
1498 die(_("%s: smudge filter %s failed"), path, ca->drv->name);
1500 return ret | ret_filter;
1503 int async_convert_to_working_tree_ca(const struct conv_attrs *ca,
1504 const char *path, const char *src,
1505 size_t len, struct strbuf *dst,
1506 const struct checkout_metadata *meta,
1507 void *dco)
1509 return convert_to_working_tree_ca_internal(ca, path, src, len, dst, 0,
1510 meta, dco);
1513 int convert_to_working_tree_ca(const struct conv_attrs *ca,
1514 const char *path, const char *src,
1515 size_t len, struct strbuf *dst,
1516 const struct checkout_metadata *meta)
1518 return convert_to_working_tree_ca_internal(ca, path, src, len, dst, 0,
1519 meta, NULL);
1522 int renormalize_buffer(struct index_state *istate, const char *path,
1523 const char *src, size_t len, struct strbuf *dst)
1525 struct conv_attrs ca;
1526 int ret;
1528 convert_attrs(istate, &ca, path);
1529 ret = convert_to_working_tree_ca_internal(&ca, path, src, len, dst, 1,
1530 NULL, NULL);
1531 if (ret) {
1532 src = dst->buf;
1533 len = dst->len;
1535 return ret | convert_to_git(istate, path, src, len, dst, CONV_EOL_RENORMALIZE);
1538 /*****************************************************************
1540 * Streaming conversion support
1542 *****************************************************************/
1544 typedef int (*filter_fn)(struct stream_filter *,
1545 const char *input, size_t *isize_p,
1546 char *output, size_t *osize_p);
1547 typedef void (*free_fn)(struct stream_filter *);
1549 struct stream_filter_vtbl {
1550 filter_fn filter;
1551 free_fn free;
1554 struct stream_filter {
1555 struct stream_filter_vtbl *vtbl;
1558 static int null_filter_fn(struct stream_filter *filter UNUSED,
1559 const char *input, size_t *isize_p,
1560 char *output, size_t *osize_p)
1562 size_t count;
1564 if (!input)
1565 return 0; /* we do not keep any states */
1566 count = *isize_p;
1567 if (*osize_p < count)
1568 count = *osize_p;
1569 if (count) {
1570 memmove(output, input, count);
1571 *isize_p -= count;
1572 *osize_p -= count;
1574 return 0;
1577 static void null_free_fn(struct stream_filter *filter UNUSED)
1579 ; /* nothing -- null instances are shared */
1582 static struct stream_filter_vtbl null_vtbl = {
1583 .filter = null_filter_fn,
1584 .free = null_free_fn,
1587 static struct stream_filter null_filter_singleton = {
1588 .vtbl = &null_vtbl,
1591 int is_null_stream_filter(struct stream_filter *filter)
1593 return filter == &null_filter_singleton;
1598 * LF-to-CRLF filter
1601 struct lf_to_crlf_filter {
1602 struct stream_filter filter;
1603 unsigned has_held:1;
1604 char held;
1607 static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1608 const char *input, size_t *isize_p,
1609 char *output, size_t *osize_p)
1611 size_t count, o = 0;
1612 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1615 * We may be holding onto the CR to see if it is followed by a
1616 * LF, in which case we would need to go to the main loop.
1617 * Otherwise, just emit it to the output stream.
1619 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1620 output[o++] = lf_to_crlf->held;
1621 lf_to_crlf->has_held = 0;
1624 /* We are told to drain */
1625 if (!input) {
1626 *osize_p -= o;
1627 return 0;
1630 count = *isize_p;
1631 if (count || lf_to_crlf->has_held) {
1632 size_t i;
1633 int was_cr = 0;
1635 if (lf_to_crlf->has_held) {
1636 was_cr = 1;
1637 lf_to_crlf->has_held = 0;
1640 for (i = 0; o < *osize_p && i < count; i++) {
1641 char ch = input[i];
1643 if (ch == '\n') {
1644 output[o++] = '\r';
1645 } else if (was_cr) {
1647 * Previous round saw CR and it is not followed
1648 * by a LF; emit the CR before processing the
1649 * current character.
1651 output[o++] = '\r';
1655 * We may have consumed the last output slot,
1656 * in which case we need to break out of this
1657 * loop; hold the current character before
1658 * returning.
1660 if (*osize_p <= o) {
1661 lf_to_crlf->has_held = 1;
1662 lf_to_crlf->held = ch;
1663 continue; /* break but increment i */
1666 if (ch == '\r') {
1667 was_cr = 1;
1668 continue;
1671 was_cr = 0;
1672 output[o++] = ch;
1675 *osize_p -= o;
1676 *isize_p -= i;
1678 if (!lf_to_crlf->has_held && was_cr) {
1679 lf_to_crlf->has_held = 1;
1680 lf_to_crlf->held = '\r';
1683 return 0;
1686 static void lf_to_crlf_free_fn(struct stream_filter *filter)
1688 free(filter);
1691 static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1692 .filter = lf_to_crlf_filter_fn,
1693 .free = lf_to_crlf_free_fn,
1696 static struct stream_filter *lf_to_crlf_filter(void)
1698 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1700 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1701 return (struct stream_filter *)lf_to_crlf;
1705 * Cascade filter
1707 #define FILTER_BUFFER 1024
1708 struct cascade_filter {
1709 struct stream_filter filter;
1710 struct stream_filter *one;
1711 struct stream_filter *two;
1712 char buf[FILTER_BUFFER];
1713 int end, ptr;
1716 static int cascade_filter_fn(struct stream_filter *filter,
1717 const char *input, size_t *isize_p,
1718 char *output, size_t *osize_p)
1720 struct cascade_filter *cas = (struct cascade_filter *) filter;
1721 size_t filled = 0;
1722 size_t sz = *osize_p;
1723 size_t to_feed, remaining;
1726 * input -- (one) --> buf -- (two) --> output
1728 while (filled < sz) {
1729 remaining = sz - filled;
1731 /* do we already have something to feed two with? */
1732 if (cas->ptr < cas->end) {
1733 to_feed = cas->end - cas->ptr;
1734 if (stream_filter(cas->two,
1735 cas->buf + cas->ptr, &to_feed,
1736 output + filled, &remaining))
1737 return -1;
1738 cas->ptr += (cas->end - cas->ptr) - to_feed;
1739 filled = sz - remaining;
1740 continue;
1743 /* feed one from upstream and have it emit into our buffer */
1744 to_feed = input ? *isize_p : 0;
1745 if (input && !to_feed)
1746 break;
1747 remaining = sizeof(cas->buf);
1748 if (stream_filter(cas->one,
1749 input, &to_feed,
1750 cas->buf, &remaining))
1751 return -1;
1752 cas->end = sizeof(cas->buf) - remaining;
1753 cas->ptr = 0;
1754 if (input) {
1755 size_t fed = *isize_p - to_feed;
1756 *isize_p -= fed;
1757 input += fed;
1760 /* do we know that we drained one completely? */
1761 if (input || cas->end)
1762 continue;
1764 /* tell two to drain; we have nothing more to give it */
1765 to_feed = 0;
1766 remaining = sz - filled;
1767 if (stream_filter(cas->two,
1768 NULL, &to_feed,
1769 output + filled, &remaining))
1770 return -1;
1771 if (remaining == (sz - filled))
1772 break; /* completely drained two */
1773 filled = sz - remaining;
1775 *osize_p -= filled;
1776 return 0;
1779 static void cascade_free_fn(struct stream_filter *filter)
1781 struct cascade_filter *cas = (struct cascade_filter *)filter;
1782 free_stream_filter(cas->one);
1783 free_stream_filter(cas->two);
1784 free(filter);
1787 static struct stream_filter_vtbl cascade_vtbl = {
1788 .filter = cascade_filter_fn,
1789 .free = cascade_free_fn,
1792 static struct stream_filter *cascade_filter(struct stream_filter *one,
1793 struct stream_filter *two)
1795 struct cascade_filter *cascade;
1797 if (!one || is_null_stream_filter(one))
1798 return two;
1799 if (!two || is_null_stream_filter(two))
1800 return one;
1802 cascade = xmalloc(sizeof(*cascade));
1803 cascade->one = one;
1804 cascade->two = two;
1805 cascade->end = cascade->ptr = 0;
1806 cascade->filter.vtbl = &cascade_vtbl;
1807 return (struct stream_filter *)cascade;
1811 * ident filter
1813 #define IDENT_DRAINING (-1)
1814 #define IDENT_SKIPPING (-2)
1815 struct ident_filter {
1816 struct stream_filter filter;
1817 struct strbuf left;
1818 int state;
1819 char ident[GIT_MAX_HEXSZ + 5]; /* ": x40 $" */
1822 static int is_foreign_ident(const char *str)
1824 int i;
1826 if (!skip_prefix(str, "$Id: ", &str))
1827 return 0;
1828 for (i = 0; str[i]; i++) {
1829 if (isspace(str[i]) && str[i+1] != '$')
1830 return 1;
1832 return 0;
1835 static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1837 size_t to_drain = ident->left.len;
1839 if (*osize_p < to_drain)
1840 to_drain = *osize_p;
1841 if (to_drain) {
1842 memcpy(*output_p, ident->left.buf, to_drain);
1843 strbuf_remove(&ident->left, 0, to_drain);
1844 *output_p += to_drain;
1845 *osize_p -= to_drain;
1847 if (!ident->left.len)
1848 ident->state = 0;
1851 static int ident_filter_fn(struct stream_filter *filter,
1852 const char *input, size_t *isize_p,
1853 char *output, size_t *osize_p)
1855 struct ident_filter *ident = (struct ident_filter *)filter;
1856 static const char head[] = "$Id";
1858 if (!input) {
1859 /* drain upon eof */
1860 switch (ident->state) {
1861 default:
1862 strbuf_add(&ident->left, head, ident->state);
1863 /* fallthrough */
1864 case IDENT_SKIPPING:
1865 /* fallthrough */
1866 case IDENT_DRAINING:
1867 ident_drain(ident, &output, osize_p);
1869 return 0;
1872 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1873 int ch;
1875 if (ident->state == IDENT_DRAINING) {
1876 ident_drain(ident, &output, osize_p);
1877 if (!*osize_p)
1878 break;
1879 continue;
1882 ch = *(input++);
1883 (*isize_p)--;
1885 if (ident->state == IDENT_SKIPPING) {
1887 * Skipping until '$' or LF, but keeping them
1888 * in case it is a foreign ident.
1890 strbuf_addch(&ident->left, ch);
1891 if (ch != '\n' && ch != '$')
1892 continue;
1893 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1894 strbuf_setlen(&ident->left, sizeof(head) - 1);
1895 strbuf_addstr(&ident->left, ident->ident);
1897 ident->state = IDENT_DRAINING;
1898 continue;
1901 if (ident->state < sizeof(head) &&
1902 head[ident->state] == ch) {
1903 ident->state++;
1904 continue;
1907 if (ident->state)
1908 strbuf_add(&ident->left, head, ident->state);
1909 if (ident->state == sizeof(head) - 1) {
1910 if (ch != ':' && ch != '$') {
1911 strbuf_addch(&ident->left, ch);
1912 ident->state = 0;
1913 continue;
1916 if (ch == ':') {
1917 strbuf_addch(&ident->left, ch);
1918 ident->state = IDENT_SKIPPING;
1919 } else {
1920 strbuf_addstr(&ident->left, ident->ident);
1921 ident->state = IDENT_DRAINING;
1923 continue;
1926 strbuf_addch(&ident->left, ch);
1927 ident->state = IDENT_DRAINING;
1929 return 0;
1932 static void ident_free_fn(struct stream_filter *filter)
1934 struct ident_filter *ident = (struct ident_filter *)filter;
1935 strbuf_release(&ident->left);
1936 free(filter);
1939 static struct stream_filter_vtbl ident_vtbl = {
1940 .filter = ident_filter_fn,
1941 .free = ident_free_fn,
1944 static struct stream_filter *ident_filter(const struct object_id *oid)
1946 struct ident_filter *ident = xmalloc(sizeof(*ident));
1948 xsnprintf(ident->ident, sizeof(ident->ident),
1949 ": %s $", oid_to_hex(oid));
1950 strbuf_init(&ident->left, 0);
1951 ident->filter.vtbl = &ident_vtbl;
1952 ident->state = 0;
1953 return (struct stream_filter *)ident;
1957 * Return an appropriately constructed filter for the given ca, or NULL if
1958 * the contents cannot be filtered without reading the whole thing
1959 * in-core.
1961 * Note that you would be crazy to set CRLF, smudge/clean or ident to a
1962 * large binary blob you would want us not to slurp into the memory!
1964 struct stream_filter *get_stream_filter_ca(const struct conv_attrs *ca,
1965 const struct object_id *oid)
1967 struct stream_filter *filter = NULL;
1969 if (classify_conv_attrs(ca) != CA_CLASS_STREAMABLE)
1970 return NULL;
1972 if (ca->ident)
1973 filter = ident_filter(oid);
1975 if (output_eol(ca->crlf_action) == EOL_CRLF)
1976 filter = cascade_filter(filter, lf_to_crlf_filter());
1977 else
1978 filter = cascade_filter(filter, &null_filter_singleton);
1980 return filter;
1983 struct stream_filter *get_stream_filter(struct index_state *istate,
1984 const char *path,
1985 const struct object_id *oid)
1987 struct conv_attrs ca;
1988 convert_attrs(istate, &ca, path);
1989 return get_stream_filter_ca(&ca, oid);
1992 void free_stream_filter(struct stream_filter *filter)
1994 filter->vtbl->free(filter);
1997 int stream_filter(struct stream_filter *filter,
1998 const char *input, size_t *isize_p,
1999 char *output, size_t *osize_p)
2001 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);
2004 void init_checkout_metadata(struct checkout_metadata *meta, const char *refname,
2005 const struct object_id *treeish,
2006 const struct object_id *blob)
2008 memset(meta, 0, sizeof(*meta));
2009 if (refname)
2010 meta->refname = refname;
2011 if (treeish)
2012 oidcpy(&meta->treeish, treeish);
2013 if (blob)
2014 oidcpy(&meta->blob, blob);
2017 void clone_checkout_metadata(struct checkout_metadata *dst,
2018 const struct checkout_metadata *src,
2019 const struct object_id *blob)
2021 memcpy(dst, src, sizeof(*dst));
2022 if (blob)
2023 oidcpy(&dst->blob, blob);
2026 enum conv_attrs_classification classify_conv_attrs(const struct conv_attrs *ca)
2028 if (ca->drv) {
2029 if (ca->drv->process)
2030 return CA_CLASS_INCORE_PROCESS;
2031 if (ca->drv->smudge || ca->drv->clean)
2032 return CA_CLASS_INCORE_FILTER;
2035 if (ca->working_tree_encoding)
2036 return CA_CLASS_INCORE;
2038 if (ca->crlf_action == CRLF_AUTO || ca->crlf_action == CRLF_AUTO_CRLF)
2039 return CA_CLASS_INCORE;
2041 return CA_CLASS_STREAMABLE;