03160b376a47489b1ccf614cb636702fac993051
1#include "cache.h"
2#include "attr.h"
3#include "run-command.h"
4#include "quote.h"
5#include "sigchain.h"
6#include "pkt-line.h"
7#include "sub-process.h"
8
9/*
10 * convert.c - convert a file when checking it out and checking it in.
11 *
12 * This should use the pathname to decide on whether it wants to do some
13 * more interesting conversions (automatic gzip/unzip, general format
14 * conversions etc etc), but by default it just does automatic CRLF<->LF
15 * translation when the "text" attribute or "auto_crlf" option is set.
16 */
17
18/* Stat bits: When BIN is set, the txt bits are unset */
19#define CONVERT_STAT_BITS_TXT_LF 0x1
20#define CONVERT_STAT_BITS_TXT_CRLF 0x2
21#define CONVERT_STAT_BITS_BIN 0x4
22
23enum crlf_action {
24 CRLF_UNDEFINED,
25 CRLF_BINARY,
26 CRLF_TEXT,
27 CRLF_TEXT_INPUT,
28 CRLF_TEXT_CRLF,
29 CRLF_AUTO,
30 CRLF_AUTO_INPUT,
31 CRLF_AUTO_CRLF
32};
33
34struct text_stat {
35 /* NUL, CR, LF and CRLF counts */
36 unsigned nul, lonecr, lonelf, crlf;
37
38 /* These are just approximations! */
39 unsigned printable, nonprintable;
40};
41
42static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
43{
44 unsigned long i;
45
46 memset(stats, 0, sizeof(*stats));
47
48 for (i = 0; i < size; i++) {
49 unsigned char c = buf[i];
50 if (c == '\r') {
51 if (i+1 < size && buf[i+1] == '\n') {
52 stats->crlf++;
53 i++;
54 } else
55 stats->lonecr++;
56 continue;
57 }
58 if (c == '\n') {
59 stats->lonelf++;
60 continue;
61 }
62 if (c == 127)
63 /* DEL */
64 stats->nonprintable++;
65 else if (c < 32) {
66 switch (c) {
67 /* BS, HT, ESC and FF */
68 case '\b': case '\t': case '\033': case '\014':
69 stats->printable++;
70 break;
71 case 0:
72 stats->nul++;
73 /* fall through */
74 default:
75 stats->nonprintable++;
76 }
77 }
78 else
79 stats->printable++;
80 }
81
82 /* If file ends with EOF then don't count this EOF as non-printable. */
83 if (size >= 1 && buf[size-1] == '\032')
84 stats->nonprintable--;
85}
86
87/*
88 * The same heuristics as diff.c::mmfile_is_binary()
89 * We treat files with bare CR as binary
90 */
91static int convert_is_binary(unsigned long size, const struct text_stat *stats)
92{
93 if (stats->lonecr)
94 return 1;
95 if (stats->nul)
96 return 1;
97 if ((stats->printable >> 7) < stats->nonprintable)
98 return 1;
99 return 0;
100}
101
102static unsigned int gather_convert_stats(const char *data, unsigned long size)
103{
104 struct text_stat stats;
105 int ret = 0;
106 if (!data || !size)
107 return 0;
108 gather_stats(data, size, &stats);
109 if (convert_is_binary(size, &stats))
110 ret |= CONVERT_STAT_BITS_BIN;
111 if (stats.crlf)
112 ret |= CONVERT_STAT_BITS_TXT_CRLF;
113 if (stats.lonelf)
114 ret |= CONVERT_STAT_BITS_TXT_LF;
115
116 return ret;
117}
118
119static const char *gather_convert_stats_ascii(const char *data, unsigned long size)
120{
121 unsigned int convert_stats = gather_convert_stats(data, size);
122
123 if (convert_stats & CONVERT_STAT_BITS_BIN)
124 return "-text";
125 switch (convert_stats) {
126 case CONVERT_STAT_BITS_TXT_LF:
127 return "lf";
128 case CONVERT_STAT_BITS_TXT_CRLF:
129 return "crlf";
130 case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF:
131 return "mixed";
132 default:
133 return "none";
134 }
135}
136
137const char *get_cached_convert_stats_ascii(const struct index_state *istate,
138 const char *path)
139{
140 const char *ret;
141 unsigned long sz;
142 void *data = read_blob_data_from_index(istate, path, &sz);
143 ret = gather_convert_stats_ascii(data, sz);
144 free(data);
145 return ret;
146}
147
148const char *get_wt_convert_stats_ascii(const char *path)
149{
150 const char *ret = "";
151 struct strbuf sb = STRBUF_INIT;
152 if (strbuf_read_file(&sb, path, 0) >= 0)
153 ret = gather_convert_stats_ascii(sb.buf, sb.len);
154 strbuf_release(&sb);
155 return ret;
156}
157
158static int text_eol_is_crlf(void)
159{
160 if (auto_crlf == AUTO_CRLF_TRUE)
161 return 1;
162 else if (auto_crlf == AUTO_CRLF_INPUT)
163 return 0;
164 if (core_eol == EOL_CRLF)
165 return 1;
166 if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF)
167 return 1;
168 return 0;
169}
170
171static enum eol output_eol(enum crlf_action crlf_action)
172{
173 switch (crlf_action) {
174 case CRLF_BINARY:
175 return EOL_UNSET;
176 case CRLF_TEXT_CRLF:
177 return EOL_CRLF;
178 case CRLF_TEXT_INPUT:
179 return EOL_LF;
180 case CRLF_UNDEFINED:
181 case CRLF_AUTO_CRLF:
182 return EOL_CRLF;
183 case CRLF_AUTO_INPUT:
184 return EOL_LF;
185 case CRLF_TEXT:
186 case CRLF_AUTO:
187 /* fall through */
188 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
189 }
190 warning("Illegal crlf_action %d\n", (int)crlf_action);
191 return core_eol;
192}
193
194static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
195 struct text_stat *old_stats, struct text_stat *new_stats,
196 enum safe_crlf checksafe)
197{
198 if (old_stats->crlf && !new_stats->crlf ) {
199 /*
200 * CRLFs would not be restored by checkout
201 */
202 if (checksafe == SAFE_CRLF_WARN)
203 warning(_("CRLF will be replaced by LF in %s.\n"
204 "The file will have its original line"
205 " endings in your working directory."), path);
206 else /* i.e. SAFE_CRLF_FAIL */
207 die(_("CRLF would be replaced by LF in %s."), path);
208 } else if (old_stats->lonelf && !new_stats->lonelf ) {
209 /*
210 * CRLFs would be added by checkout
211 */
212 if (checksafe == SAFE_CRLF_WARN)
213 warning(_("LF will be replaced by CRLF in %s.\n"
214 "The file will have its original line"
215 " endings in your working directory."), path);
216 else /* i.e. SAFE_CRLF_FAIL */
217 die(_("LF would be replaced by CRLF in %s"), path);
218 }
219}
220
221static int has_cr_in_index(const char *path)
222{
223 unsigned long sz;
224 void *data;
225 int has_cr;
226
227 data = read_blob_data_from_cache(path, &sz);
228 if (!data)
229 return 0;
230 has_cr = memchr(data, '\r', sz) != NULL;
231 free(data);
232 return has_cr;
233}
234
235static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats,
236 enum crlf_action crlf_action)
237{
238 if (output_eol(crlf_action) != EOL_CRLF)
239 return 0;
240 /* No "naked" LF? Nothing to convert, regardless. */
241 if (!stats->lonelf)
242 return 0;
243
244 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
245 /* If we have any CR or CRLF line endings, we do not touch it */
246 /* This is the new safer autocrlf-handling */
247 if (stats->lonecr || stats->crlf)
248 return 0;
249
250 if (convert_is_binary(len, stats))
251 return 0;
252 }
253 return 1;
254
255}
256
257static int crlf_to_git(const char *path, const char *src, size_t len,
258 struct strbuf *buf,
259 enum crlf_action crlf_action, enum safe_crlf checksafe)
260{
261 struct text_stat stats;
262 char *dst;
263 int convert_crlf_into_lf;
264
265 if (crlf_action == CRLF_BINARY ||
266 (src && !len))
267 return 0;
268
269 /*
270 * If we are doing a dry-run and have no source buffer, there is
271 * nothing to analyze; we must assume we would convert.
272 */
273 if (!buf && !src)
274 return 1;
275
276 gather_stats(src, len, &stats);
277 /* Optimization: No CRLF? Nothing to convert, regardless. */
278 convert_crlf_into_lf = !!stats.crlf;
279
280 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
281 if (convert_is_binary(len, &stats))
282 return 0;
283 /*
284 * If the file in the index has any CR in it, do not
285 * convert. This is the new safer autocrlf handling,
286 * unless we want to renormalize in a merge or
287 * cherry-pick.
288 */
289 if ((checksafe != SAFE_CRLF_RENORMALIZE) && has_cr_in_index(path))
290 convert_crlf_into_lf = 0;
291 }
292 if ((checksafe == SAFE_CRLF_WARN ||
293 (checksafe == SAFE_CRLF_FAIL)) && len) {
294 struct text_stat new_stats;
295 memcpy(&new_stats, &stats, sizeof(new_stats));
296 /* simulate "git add" */
297 if (convert_crlf_into_lf) {
298 new_stats.lonelf += new_stats.crlf;
299 new_stats.crlf = 0;
300 }
301 /* simulate "git checkout" */
302 if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) {
303 new_stats.crlf += new_stats.lonelf;
304 new_stats.lonelf = 0;
305 }
306 check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe);
307 }
308 if (!convert_crlf_into_lf)
309 return 0;
310
311 /*
312 * At this point all of our source analysis is done, and we are sure we
313 * would convert. If we are in dry-run mode, we can give an answer.
314 */
315 if (!buf)
316 return 1;
317
318 /* only grow if not in place */
319 if (strbuf_avail(buf) + buf->len < len)
320 strbuf_grow(buf, len - buf->len);
321 dst = buf->buf;
322 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
323 /*
324 * If we guessed, we already know we rejected a file with
325 * lone CR, and we can strip a CR without looking at what
326 * follow it.
327 */
328 do {
329 unsigned char c = *src++;
330 if (c != '\r')
331 *dst++ = c;
332 } while (--len);
333 } else {
334 do {
335 unsigned char c = *src++;
336 if (! (c == '\r' && (1 < len && *src == '\n')))
337 *dst++ = c;
338 } while (--len);
339 }
340 strbuf_setlen(buf, dst - buf->buf);
341 return 1;
342}
343
344static int crlf_to_worktree(const char *path, const char *src, size_t len,
345 struct strbuf *buf, enum crlf_action crlf_action)
346{
347 char *to_free = NULL;
348 struct text_stat stats;
349
350 if (!len || output_eol(crlf_action) != EOL_CRLF)
351 return 0;
352
353 gather_stats(src, len, &stats);
354 if (!will_convert_lf_to_crlf(len, &stats, crlf_action))
355 return 0;
356
357 /* are we "faking" in place editing ? */
358 if (src == buf->buf)
359 to_free = strbuf_detach(buf, NULL);
360
361 strbuf_grow(buf, len + stats.lonelf);
362 for (;;) {
363 const char *nl = memchr(src, '\n', len);
364 if (!nl)
365 break;
366 if (nl > src && nl[-1] == '\r') {
367 strbuf_add(buf, src, nl + 1 - src);
368 } else {
369 strbuf_add(buf, src, nl - src);
370 strbuf_addstr(buf, "\r\n");
371 }
372 len -= nl + 1 - src;
373 src = nl + 1;
374 }
375 strbuf_add(buf, src, len);
376
377 free(to_free);
378 return 1;
379}
380
381struct filter_params {
382 const char *src;
383 unsigned long size;
384 int fd;
385 const char *cmd;
386 const char *path;
387};
388
389static int filter_buffer_or_fd(int in, int out, void *data)
390{
391 /*
392 * Spawn cmd and feed the buffer contents through its stdin.
393 */
394 struct child_process child_process = CHILD_PROCESS_INIT;
395 struct filter_params *params = (struct filter_params *)data;
396 int write_err, status;
397 const char *argv[] = { NULL, NULL };
398
399 /* apply % substitution to cmd */
400 struct strbuf cmd = STRBUF_INIT;
401 struct strbuf path = STRBUF_INIT;
402 struct strbuf_expand_dict_entry dict[] = {
403 { "f", NULL, },
404 { NULL, NULL, },
405 };
406
407 /* quote the path to preserve spaces, etc. */
408 sq_quote_buf(&path, params->path);
409 dict[0].value = path.buf;
410
411 /* expand all %f with the quoted path */
412 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
413 strbuf_release(&path);
414
415 argv[0] = cmd.buf;
416
417 child_process.argv = argv;
418 child_process.use_shell = 1;
419 child_process.in = -1;
420 child_process.out = out;
421
422 if (start_command(&child_process))
423 return error("cannot fork to run external filter '%s'", params->cmd);
424
425 sigchain_push(SIGPIPE, SIG_IGN);
426
427 if (params->src) {
428 write_err = (write_in_full(child_process.in,
429 params->src, params->size) < 0);
430 if (errno == EPIPE)
431 write_err = 0;
432 } else {
433 write_err = copy_fd(params->fd, child_process.in);
434 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
435 write_err = 0;
436 }
437
438 if (close(child_process.in))
439 write_err = 1;
440 if (write_err)
441 error("cannot feed the input to external filter '%s'", params->cmd);
442
443 sigchain_pop(SIGPIPE);
444
445 status = finish_command(&child_process);
446 if (status)
447 error("external filter '%s' failed %d", params->cmd, status);
448
449 strbuf_release(&cmd);
450 return (write_err || status);
451}
452
453static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd,
454 struct strbuf *dst, const char *cmd)
455{
456 /*
457 * Create a pipeline to have the command filter the buffer's
458 * contents.
459 *
460 * (child --> cmd) --> us
461 */
462 int err = 0;
463 struct strbuf nbuf = STRBUF_INIT;
464 struct async async;
465 struct filter_params params;
466
467 memset(&async, 0, sizeof(async));
468 async.proc = filter_buffer_or_fd;
469 async.data = ¶ms;
470 async.out = -1;
471 params.src = src;
472 params.size = len;
473 params.fd = fd;
474 params.cmd = cmd;
475 params.path = path;
476
477 fflush(NULL);
478 if (start_async(&async))
479 return 0; /* error was already reported */
480
481 if (strbuf_read(&nbuf, async.out, len) < 0) {
482 err = error("read from external filter '%s' failed", cmd);
483 }
484 if (close(async.out)) {
485 err = error("read from external filter '%s' failed", cmd);
486 }
487 if (finish_async(&async)) {
488 err = error("external filter '%s' failed", cmd);
489 }
490
491 if (!err) {
492 strbuf_swap(dst, &nbuf);
493 }
494 strbuf_release(&nbuf);
495 return !err;
496}
497
498#define CAP_CLEAN (1u<<0)
499#define CAP_SMUDGE (1u<<1)
500
501struct cmd2process {
502 struct subprocess_entry subprocess; /* must be the first member! */
503 unsigned int supported_capabilities;
504};
505
506static int subprocess_map_initialized;
507static struct hashmap subprocess_map;
508
509static int start_multi_file_filter_fn(struct subprocess_entry *subprocess)
510{
511 int err;
512 struct cmd2process *entry = (struct cmd2process *)subprocess;
513 struct string_list cap_list = STRING_LIST_INIT_NODUP;
514 char *cap_buf;
515 const char *cap_name;
516 struct child_process *process = &subprocess->process;
517 const char *cmd = subprocess->cmd;
518
519 sigchain_push(SIGPIPE, SIG_IGN);
520
521 err = packet_writel(process->in, "git-filter-client", "version=2", NULL);
522 if (err)
523 goto done;
524
525 err = strcmp(packet_read_line(process->out, NULL), "git-filter-server");
526 if (err) {
527 error("external filter '%s' does not support filter protocol version 2", cmd);
528 goto done;
529 }
530 err = strcmp(packet_read_line(process->out, NULL), "version=2");
531 if (err)
532 goto done;
533 err = packet_read_line(process->out, NULL) != NULL;
534 if (err)
535 goto done;
536
537 err = packet_writel(process->in, "capability=clean", "capability=smudge", NULL);
538
539 for (;;) {
540 cap_buf = packet_read_line(process->out, NULL);
541 if (!cap_buf)
542 break;
543 string_list_split_in_place(&cap_list, cap_buf, '=', 1);
544
545 if (cap_list.nr != 2 || strcmp(cap_list.items[0].string, "capability"))
546 continue;
547
548 cap_name = cap_list.items[1].string;
549 if (!strcmp(cap_name, "clean")) {
550 entry->supported_capabilities |= CAP_CLEAN;
551 } else if (!strcmp(cap_name, "smudge")) {
552 entry->supported_capabilities |= CAP_SMUDGE;
553 } else {
554 warning(
555 "external filter '%s' requested unsupported filter capability '%s'",
556 cmd, cap_name
557 );
558 }
559
560 string_list_clear(&cap_list, 0);
561 }
562
563done:
564 sigchain_pop(SIGPIPE);
565
566 return err;
567}
568
569static int apply_multi_file_filter(const char *path, const char *src, size_t len,
570 int fd, struct strbuf *dst, const char *cmd,
571 const unsigned int wanted_capability)
572{
573 int err;
574 struct cmd2process *entry;
575 struct child_process *process;
576 struct strbuf nbuf = STRBUF_INIT;
577 struct strbuf filter_status = STRBUF_INIT;
578 const char *filter_type;
579
580 if (!subprocess_map_initialized) {
581 subprocess_map_initialized = 1;
582 hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
583 entry = NULL;
584 } else {
585 entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);
586 }
587
588 fflush(NULL);
589
590 if (!entry) {
591 entry = xmalloc(sizeof(*entry));
592 entry->supported_capabilities = 0;
593
594 if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) {
595 free(entry);
596 return 0;
597 }
598 }
599 process = &entry->subprocess.process;
600
601 if (!(wanted_capability & entry->supported_capabilities))
602 return 0;
603
604 if (CAP_CLEAN & wanted_capability)
605 filter_type = "clean";
606 else if (CAP_SMUDGE & wanted_capability)
607 filter_type = "smudge";
608 else
609 die("unexpected filter type");
610
611 sigchain_push(SIGPIPE, SIG_IGN);
612
613 assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n"));
614 err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type);
615 if (err)
616 goto done;
617
618 err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n");
619 if (err) {
620 error("path name too long for external filter");
621 goto done;
622 }
623
624 err = packet_write_fmt_gently(process->in, "pathname=%s\n", path);
625 if (err)
626 goto done;
627
628 err = packet_flush_gently(process->in);
629 if (err)
630 goto done;
631
632 if (fd >= 0)
633 err = write_packetized_from_fd(fd, process->in);
634 else
635 err = write_packetized_from_buf(src, len, process->in);
636 if (err)
637 goto done;
638
639 err = subprocess_read_status(process->out, &filter_status);
640 if (err)
641 goto done;
642
643 err = strcmp(filter_status.buf, "success");
644 if (err)
645 goto done;
646
647 err = read_packetized_to_strbuf(process->out, &nbuf) < 0;
648 if (err)
649 goto done;
650
651 err = subprocess_read_status(process->out, &filter_status);
652 if (err)
653 goto done;
654
655 err = strcmp(filter_status.buf, "success");
656
657done:
658 sigchain_pop(SIGPIPE);
659
660 if (err) {
661 if (!strcmp(filter_status.buf, "error")) {
662 /* The filter signaled a problem with the file. */
663 } else if (!strcmp(filter_status.buf, "abort")) {
664 /*
665 * The filter signaled a permanent problem. Don't try to filter
666 * files with the same command for the lifetime of the current
667 * Git process.
668 */
669 entry->supported_capabilities &= ~wanted_capability;
670 } else {
671 /*
672 * Something went wrong with the protocol filter.
673 * Force shutdown and restart if another blob requires filtering.
674 */
675 error("external filter '%s' failed", cmd);
676 subprocess_stop(&subprocess_map, &entry->subprocess);
677 free(entry);
678 }
679 } else {
680 strbuf_swap(dst, &nbuf);
681 }
682 strbuf_release(&nbuf);
683 return !err;
684}
685
686static struct convert_driver {
687 const char *name;
688 struct convert_driver *next;
689 const char *smudge;
690 const char *clean;
691 const char *process;
692 int required;
693} *user_convert, **user_convert_tail;
694
695static int apply_filter(const char *path, const char *src, size_t len,
696 int fd, struct strbuf *dst, struct convert_driver *drv,
697 const unsigned int wanted_capability)
698{
699 const char *cmd = NULL;
700
701 if (!drv)
702 return 0;
703
704 if (!dst)
705 return 1;
706
707 if ((CAP_CLEAN & wanted_capability) && !drv->process && drv->clean)
708 cmd = drv->clean;
709 else if ((CAP_SMUDGE & wanted_capability) && !drv->process && drv->smudge)
710 cmd = drv->smudge;
711
712 if (cmd && *cmd)
713 return apply_single_file_filter(path, src, len, fd, dst, cmd);
714 else if (drv->process && *drv->process)
715 return apply_multi_file_filter(path, src, len, fd, dst, drv->process, wanted_capability);
716
717 return 0;
718}
719
720static int read_convert_config(const char *var, const char *value, void *cb)
721{
722 const char *key, *name;
723 int namelen;
724 struct convert_driver *drv;
725
726 /*
727 * External conversion drivers are configured using
728 * "filter.<name>.variable".
729 */
730 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
731 return 0;
732 for (drv = user_convert; drv; drv = drv->next)
733 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
734 break;
735 if (!drv) {
736 drv = xcalloc(1, sizeof(struct convert_driver));
737 drv->name = xmemdupz(name, namelen);
738 *user_convert_tail = drv;
739 user_convert_tail = &(drv->next);
740 }
741
742 /*
743 * filter.<name>.smudge and filter.<name>.clean specifies
744 * the command line:
745 *
746 * command-line
747 *
748 * The command-line will not be interpolated in any way.
749 */
750
751 if (!strcmp("smudge", key))
752 return git_config_string(&drv->smudge, var, value);
753
754 if (!strcmp("clean", key))
755 return git_config_string(&drv->clean, var, value);
756
757 if (!strcmp("process", key))
758 return git_config_string(&drv->process, var, value);
759
760 if (!strcmp("required", key)) {
761 drv->required = git_config_bool(var, value);
762 return 0;
763 }
764
765 return 0;
766}
767
768static int count_ident(const char *cp, unsigned long size)
769{
770 /*
771 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
772 */
773 int cnt = 0;
774 char ch;
775
776 while (size) {
777 ch = *cp++;
778 size--;
779 if (ch != '$')
780 continue;
781 if (size < 3)
782 break;
783 if (memcmp("Id", cp, 2))
784 continue;
785 ch = cp[2];
786 cp += 3;
787 size -= 3;
788 if (ch == '$')
789 cnt++; /* $Id$ */
790 if (ch != ':')
791 continue;
792
793 /*
794 * "$Id: ... "; scan up to the closing dollar sign and discard.
795 */
796 while (size) {
797 ch = *cp++;
798 size--;
799 if (ch == '$') {
800 cnt++;
801 break;
802 }
803 if (ch == '\n')
804 break;
805 }
806 }
807 return cnt;
808}
809
810static int ident_to_git(const char *path, const char *src, size_t len,
811 struct strbuf *buf, int ident)
812{
813 char *dst, *dollar;
814
815 if (!ident || (src && !count_ident(src, len)))
816 return 0;
817
818 if (!buf)
819 return 1;
820
821 /* only grow if not in place */
822 if (strbuf_avail(buf) + buf->len < len)
823 strbuf_grow(buf, len - buf->len);
824 dst = buf->buf;
825 for (;;) {
826 dollar = memchr(src, '$', len);
827 if (!dollar)
828 break;
829 memmove(dst, src, dollar + 1 - src);
830 dst += dollar + 1 - src;
831 len -= dollar + 1 - src;
832 src = dollar + 1;
833
834 if (len > 3 && !memcmp(src, "Id:", 3)) {
835 dollar = memchr(src + 3, '$', len - 3);
836 if (!dollar)
837 break;
838 if (memchr(src + 3, '\n', dollar - src - 3)) {
839 /* Line break before the next dollar. */
840 continue;
841 }
842
843 memcpy(dst, "Id$", 3);
844 dst += 3;
845 len -= dollar + 1 - src;
846 src = dollar + 1;
847 }
848 }
849 memmove(dst, src, len);
850 strbuf_setlen(buf, dst + len - buf->buf);
851 return 1;
852}
853
854static int ident_to_worktree(const char *path, const char *src, size_t len,
855 struct strbuf *buf, int ident)
856{
857 unsigned char sha1[20];
858 char *to_free = NULL, *dollar, *spc;
859 int cnt;
860
861 if (!ident)
862 return 0;
863
864 cnt = count_ident(src, len);
865 if (!cnt)
866 return 0;
867
868 /* are we "faking" in place editing ? */
869 if (src == buf->buf)
870 to_free = strbuf_detach(buf, NULL);
871 hash_sha1_file(src, len, "blob", sha1);
872
873 strbuf_grow(buf, len + cnt * 43);
874 for (;;) {
875 /* step 1: run to the next '$' */
876 dollar = memchr(src, '$', len);
877 if (!dollar)
878 break;
879 strbuf_add(buf, src, dollar + 1 - src);
880 len -= dollar + 1 - src;
881 src = dollar + 1;
882
883 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
884 if (len < 3 || memcmp("Id", src, 2))
885 continue;
886
887 /* step 3: skip over Id$ or Id:xxxxx$ */
888 if (src[2] == '$') {
889 src += 3;
890 len -= 3;
891 } else if (src[2] == ':') {
892 /*
893 * It's possible that an expanded Id has crept its way into the
894 * repository, we cope with that by stripping the expansion out.
895 * This is probably not a good idea, since it will cause changes
896 * on checkout, which won't go away by stash, but let's keep it
897 * for git-style ids.
898 */
899 dollar = memchr(src + 3, '$', len - 3);
900 if (!dollar) {
901 /* incomplete keyword, no more '$', so just quit the loop */
902 break;
903 }
904
905 if (memchr(src + 3, '\n', dollar - src - 3)) {
906 /* Line break before the next dollar. */
907 continue;
908 }
909
910 spc = memchr(src + 4, ' ', dollar - src - 4);
911 if (spc && spc < dollar-1) {
912 /* There are spaces in unexpected places.
913 * This is probably an id from some other
914 * versioning system. Keep it for now.
915 */
916 continue;
917 }
918
919 len -= dollar + 1 - src;
920 src = dollar + 1;
921 } else {
922 /* it wasn't a "Id$" or "Id:xxxx$" */
923 continue;
924 }
925
926 /* step 4: substitute */
927 strbuf_addstr(buf, "Id: ");
928 strbuf_add(buf, sha1_to_hex(sha1), 40);
929 strbuf_addstr(buf, " $");
930 }
931 strbuf_add(buf, src, len);
932
933 free(to_free);
934 return 1;
935}
936
937static enum crlf_action git_path_check_crlf(struct attr_check_item *check)
938{
939 const char *value = check->value;
940
941 if (ATTR_TRUE(value))
942 return CRLF_TEXT;
943 else if (ATTR_FALSE(value))
944 return CRLF_BINARY;
945 else if (ATTR_UNSET(value))
946 ;
947 else if (!strcmp(value, "input"))
948 return CRLF_TEXT_INPUT;
949 else if (!strcmp(value, "auto"))
950 return CRLF_AUTO;
951 return CRLF_UNDEFINED;
952}
953
954static enum eol git_path_check_eol(struct attr_check_item *check)
955{
956 const char *value = check->value;
957
958 if (ATTR_UNSET(value))
959 ;
960 else if (!strcmp(value, "lf"))
961 return EOL_LF;
962 else if (!strcmp(value, "crlf"))
963 return EOL_CRLF;
964 return EOL_UNSET;
965}
966
967static struct convert_driver *git_path_check_convert(struct attr_check_item *check)
968{
969 const char *value = check->value;
970 struct convert_driver *drv;
971
972 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
973 return NULL;
974 for (drv = user_convert; drv; drv = drv->next)
975 if (!strcmp(value, drv->name))
976 return drv;
977 return NULL;
978}
979
980static int git_path_check_ident(struct attr_check_item *check)
981{
982 const char *value = check->value;
983
984 return !!ATTR_TRUE(value);
985}
986
987struct conv_attrs {
988 struct convert_driver *drv;
989 enum crlf_action attr_action; /* What attr says */
990 enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
991 int ident;
992};
993
994static void convert_attrs(struct conv_attrs *ca, const char *path)
995{
996 static struct attr_check *check;
997
998 if (!check) {
999 check = attr_check_initl("crlf", "ident", "filter",
1000 "eol", "text", NULL);
1001 user_convert_tail = &user_convert;
1002 git_config(read_convert_config, NULL);
1003 }
1004
1005 if (!git_check_attr(path, check)) {
1006 struct attr_check_item *ccheck = check->items;
1007 ca->crlf_action = git_path_check_crlf(ccheck + 4);
1008 if (ca->crlf_action == CRLF_UNDEFINED)
1009 ca->crlf_action = git_path_check_crlf(ccheck + 0);
1010 ca->attr_action = ca->crlf_action;
1011 ca->ident = git_path_check_ident(ccheck + 1);
1012 ca->drv = git_path_check_convert(ccheck + 2);
1013 if (ca->crlf_action != CRLF_BINARY) {
1014 enum eol eol_attr = git_path_check_eol(ccheck + 3);
1015 if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
1016 ca->crlf_action = CRLF_AUTO_INPUT;
1017 else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
1018 ca->crlf_action = CRLF_AUTO_CRLF;
1019 else if (eol_attr == EOL_LF)
1020 ca->crlf_action = CRLF_TEXT_INPUT;
1021 else if (eol_attr == EOL_CRLF)
1022 ca->crlf_action = CRLF_TEXT_CRLF;
1023 }
1024 ca->attr_action = ca->crlf_action;
1025 } else {
1026 ca->drv = NULL;
1027 ca->crlf_action = CRLF_UNDEFINED;
1028 ca->ident = 0;
1029 }
1030 if (ca->crlf_action == CRLF_TEXT)
1031 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
1032 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
1033 ca->crlf_action = CRLF_BINARY;
1034 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
1035 ca->crlf_action = CRLF_AUTO_CRLF;
1036 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
1037 ca->crlf_action = CRLF_AUTO_INPUT;
1038}
1039
1040int would_convert_to_git_filter_fd(const char *path)
1041{
1042 struct conv_attrs ca;
1043
1044 convert_attrs(&ca, path);
1045 if (!ca.drv)
1046 return 0;
1047
1048 /*
1049 * Apply a filter to an fd only if the filter is required to succeed.
1050 * We must die if the filter fails, because the original data before
1051 * filtering is not available.
1052 */
1053 if (!ca.drv->required)
1054 return 0;
1055
1056 return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN);
1057}
1058
1059const char *get_convert_attr_ascii(const char *path)
1060{
1061 struct conv_attrs ca;
1062
1063 convert_attrs(&ca, path);
1064 switch (ca.attr_action) {
1065 case CRLF_UNDEFINED:
1066 return "";
1067 case CRLF_BINARY:
1068 return "-text";
1069 case CRLF_TEXT:
1070 return "text";
1071 case CRLF_TEXT_INPUT:
1072 return "text eol=lf";
1073 case CRLF_TEXT_CRLF:
1074 return "text eol=crlf";
1075 case CRLF_AUTO:
1076 return "text=auto";
1077 case CRLF_AUTO_CRLF:
1078 return "text=auto eol=crlf";
1079 case CRLF_AUTO_INPUT:
1080 return "text=auto eol=lf";
1081 }
1082 return "";
1083}
1084
1085int convert_to_git(const char *path, const char *src, size_t len,
1086 struct strbuf *dst, enum safe_crlf checksafe)
1087{
1088 int ret = 0;
1089 struct conv_attrs ca;
1090
1091 convert_attrs(&ca, path);
1092
1093 ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN);
1094 if (!ret && ca.drv && ca.drv->required)
1095 die("%s: clean filter '%s' failed", path, ca.drv->name);
1096
1097 if (ret && dst) {
1098 src = dst->buf;
1099 len = dst->len;
1100 }
1101 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
1102 if (ret && dst) {
1103 src = dst->buf;
1104 len = dst->len;
1105 }
1106 return ret | ident_to_git(path, src, len, dst, ca.ident);
1107}
1108
1109void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
1110 enum safe_crlf checksafe)
1111{
1112 struct conv_attrs ca;
1113 convert_attrs(&ca, path);
1114
1115 assert(ca.drv);
1116 assert(ca.drv->clean || ca.drv->process);
1117
1118 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN))
1119 die("%s: clean filter '%s' failed", path, ca.drv->name);
1120
1121 crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
1122 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
1123}
1124
1125static int convert_to_working_tree_internal(const char *path, const char *src,
1126 size_t len, struct strbuf *dst,
1127 int normalizing)
1128{
1129 int ret = 0, ret_filter = 0;
1130 struct conv_attrs ca;
1131
1132 convert_attrs(&ca, path);
1133
1134 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
1135 if (ret) {
1136 src = dst->buf;
1137 len = dst->len;
1138 }
1139 /*
1140 * CRLF conversion can be skipped if normalizing, unless there
1141 * is a smudge or process filter (even if the process filter doesn't
1142 * support smudge). The filters might expect CRLFs.
1143 */
1144 if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) {
1145 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
1146 if (ret) {
1147 src = dst->buf;
1148 len = dst->len;
1149 }
1150 }
1151
1152 ret_filter = apply_filter(path, src, len, -1, dst, ca.drv, CAP_SMUDGE);
1153 if (!ret_filter && ca.drv && ca.drv->required)
1154 die("%s: smudge filter %s failed", path, ca.drv->name);
1155
1156 return ret | ret_filter;
1157}
1158
1159int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
1160{
1161 return convert_to_working_tree_internal(path, src, len, dst, 0);
1162}
1163
1164int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
1165{
1166 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
1167 if (ret) {
1168 src = dst->buf;
1169 len = dst->len;
1170 }
1171 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE);
1172}
1173
1174/*****************************************************************
1175 *
1176 * Streaming conversion support
1177 *
1178 *****************************************************************/
1179
1180typedef int (*filter_fn)(struct stream_filter *,
1181 const char *input, size_t *isize_p,
1182 char *output, size_t *osize_p);
1183typedef void (*free_fn)(struct stream_filter *);
1184
1185struct stream_filter_vtbl {
1186 filter_fn filter;
1187 free_fn free;
1188};
1189
1190struct stream_filter {
1191 struct stream_filter_vtbl *vtbl;
1192};
1193
1194static int null_filter_fn(struct stream_filter *filter,
1195 const char *input, size_t *isize_p,
1196 char *output, size_t *osize_p)
1197{
1198 size_t count;
1199
1200 if (!input)
1201 return 0; /* we do not keep any states */
1202 count = *isize_p;
1203 if (*osize_p < count)
1204 count = *osize_p;
1205 if (count) {
1206 memmove(output, input, count);
1207 *isize_p -= count;
1208 *osize_p -= count;
1209 }
1210 return 0;
1211}
1212
1213static void null_free_fn(struct stream_filter *filter)
1214{
1215 ; /* nothing -- null instances are shared */
1216}
1217
1218static struct stream_filter_vtbl null_vtbl = {
1219 null_filter_fn,
1220 null_free_fn,
1221};
1222
1223static struct stream_filter null_filter_singleton = {
1224 &null_vtbl,
1225};
1226
1227int is_null_stream_filter(struct stream_filter *filter)
1228{
1229 return filter == &null_filter_singleton;
1230}
1231
1232
1233/*
1234 * LF-to-CRLF filter
1235 */
1236
1237struct lf_to_crlf_filter {
1238 struct stream_filter filter;
1239 unsigned has_held:1;
1240 char held;
1241};
1242
1243static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1244 const char *input, size_t *isize_p,
1245 char *output, size_t *osize_p)
1246{
1247 size_t count, o = 0;
1248 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
1249
1250 /*
1251 * We may be holding onto the CR to see if it is followed by a
1252 * LF, in which case we would need to go to the main loop.
1253 * Otherwise, just emit it to the output stream.
1254 */
1255 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1256 output[o++] = lf_to_crlf->held;
1257 lf_to_crlf->has_held = 0;
1258 }
1259
1260 /* We are told to drain */
1261 if (!input) {
1262 *osize_p -= o;
1263 return 0;
1264 }
1265
1266 count = *isize_p;
1267 if (count || lf_to_crlf->has_held) {
1268 size_t i;
1269 int was_cr = 0;
1270
1271 if (lf_to_crlf->has_held) {
1272 was_cr = 1;
1273 lf_to_crlf->has_held = 0;
1274 }
1275
1276 for (i = 0; o < *osize_p && i < count; i++) {
1277 char ch = input[i];
1278
1279 if (ch == '\n') {
1280 output[o++] = '\r';
1281 } else if (was_cr) {
1282 /*
1283 * Previous round saw CR and it is not followed
1284 * by a LF; emit the CR before processing the
1285 * current character.
1286 */
1287 output[o++] = '\r';
1288 }
1289
1290 /*
1291 * We may have consumed the last output slot,
1292 * in which case we need to break out of this
1293 * loop; hold the current character before
1294 * returning.
1295 */
1296 if (*osize_p <= o) {
1297 lf_to_crlf->has_held = 1;
1298 lf_to_crlf->held = ch;
1299 continue; /* break but increment i */
1300 }
1301
1302 if (ch == '\r') {
1303 was_cr = 1;
1304 continue;
1305 }
1306
1307 was_cr = 0;
1308 output[o++] = ch;
1309 }
1310
1311 *osize_p -= o;
1312 *isize_p -= i;
1313
1314 if (!lf_to_crlf->has_held && was_cr) {
1315 lf_to_crlf->has_held = 1;
1316 lf_to_crlf->held = '\r';
1317 }
1318 }
1319 return 0;
1320}
1321
1322static void lf_to_crlf_free_fn(struct stream_filter *filter)
1323{
1324 free(filter);
1325}
1326
1327static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1328 lf_to_crlf_filter_fn,
1329 lf_to_crlf_free_fn,
1330};
1331
1332static struct stream_filter *lf_to_crlf_filter(void)
1333{
1334 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
1335
1336 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
1337 return (struct stream_filter *)lf_to_crlf;
1338}
1339
1340/*
1341 * Cascade filter
1342 */
1343#define FILTER_BUFFER 1024
1344struct cascade_filter {
1345 struct stream_filter filter;
1346 struct stream_filter *one;
1347 struct stream_filter *two;
1348 char buf[FILTER_BUFFER];
1349 int end, ptr;
1350};
1351
1352static int cascade_filter_fn(struct stream_filter *filter,
1353 const char *input, size_t *isize_p,
1354 char *output, size_t *osize_p)
1355{
1356 struct cascade_filter *cas = (struct cascade_filter *) filter;
1357 size_t filled = 0;
1358 size_t sz = *osize_p;
1359 size_t to_feed, remaining;
1360
1361 /*
1362 * input -- (one) --> buf -- (two) --> output
1363 */
1364 while (filled < sz) {
1365 remaining = sz - filled;
1366
1367 /* do we already have something to feed two with? */
1368 if (cas->ptr < cas->end) {
1369 to_feed = cas->end - cas->ptr;
1370 if (stream_filter(cas->two,
1371 cas->buf + cas->ptr, &to_feed,
1372 output + filled, &remaining))
1373 return -1;
1374 cas->ptr += (cas->end - cas->ptr) - to_feed;
1375 filled = sz - remaining;
1376 continue;
1377 }
1378
1379 /* feed one from upstream and have it emit into our buffer */
1380 to_feed = input ? *isize_p : 0;
1381 if (input && !to_feed)
1382 break;
1383 remaining = sizeof(cas->buf);
1384 if (stream_filter(cas->one,
1385 input, &to_feed,
1386 cas->buf, &remaining))
1387 return -1;
1388 cas->end = sizeof(cas->buf) - remaining;
1389 cas->ptr = 0;
1390 if (input) {
1391 size_t fed = *isize_p - to_feed;
1392 *isize_p -= fed;
1393 input += fed;
1394 }
1395
1396 /* do we know that we drained one completely? */
1397 if (input || cas->end)
1398 continue;
1399
1400 /* tell two to drain; we have nothing more to give it */
1401 to_feed = 0;
1402 remaining = sz - filled;
1403 if (stream_filter(cas->two,
1404 NULL, &to_feed,
1405 output + filled, &remaining))
1406 return -1;
1407 if (remaining == (sz - filled))
1408 break; /* completely drained two */
1409 filled = sz - remaining;
1410 }
1411 *osize_p -= filled;
1412 return 0;
1413}
1414
1415static void cascade_free_fn(struct stream_filter *filter)
1416{
1417 struct cascade_filter *cas = (struct cascade_filter *)filter;
1418 free_stream_filter(cas->one);
1419 free_stream_filter(cas->two);
1420 free(filter);
1421}
1422
1423static struct stream_filter_vtbl cascade_vtbl = {
1424 cascade_filter_fn,
1425 cascade_free_fn,
1426};
1427
1428static struct stream_filter *cascade_filter(struct stream_filter *one,
1429 struct stream_filter *two)
1430{
1431 struct cascade_filter *cascade;
1432
1433 if (!one || is_null_stream_filter(one))
1434 return two;
1435 if (!two || is_null_stream_filter(two))
1436 return one;
1437
1438 cascade = xmalloc(sizeof(*cascade));
1439 cascade->one = one;
1440 cascade->two = two;
1441 cascade->end = cascade->ptr = 0;
1442 cascade->filter.vtbl = &cascade_vtbl;
1443 return (struct stream_filter *)cascade;
1444}
1445
1446/*
1447 * ident filter
1448 */
1449#define IDENT_DRAINING (-1)
1450#define IDENT_SKIPPING (-2)
1451struct ident_filter {
1452 struct stream_filter filter;
1453 struct strbuf left;
1454 int state;
1455 char ident[45]; /* ": x40 $" */
1456};
1457
1458static int is_foreign_ident(const char *str)
1459{
1460 int i;
1461
1462 if (!skip_prefix(str, "$Id: ", &str))
1463 return 0;
1464 for (i = 0; str[i]; i++) {
1465 if (isspace(str[i]) && str[i+1] != '$')
1466 return 1;
1467 }
1468 return 0;
1469}
1470
1471static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1472{
1473 size_t to_drain = ident->left.len;
1474
1475 if (*osize_p < to_drain)
1476 to_drain = *osize_p;
1477 if (to_drain) {
1478 memcpy(*output_p, ident->left.buf, to_drain);
1479 strbuf_remove(&ident->left, 0, to_drain);
1480 *output_p += to_drain;
1481 *osize_p -= to_drain;
1482 }
1483 if (!ident->left.len)
1484 ident->state = 0;
1485}
1486
1487static int ident_filter_fn(struct stream_filter *filter,
1488 const char *input, size_t *isize_p,
1489 char *output, size_t *osize_p)
1490{
1491 struct ident_filter *ident = (struct ident_filter *)filter;
1492 static const char head[] = "$Id";
1493
1494 if (!input) {
1495 /* drain upon eof */
1496 switch (ident->state) {
1497 default:
1498 strbuf_add(&ident->left, head, ident->state);
1499 case IDENT_SKIPPING:
1500 /* fallthru */
1501 case IDENT_DRAINING:
1502 ident_drain(ident, &output, osize_p);
1503 }
1504 return 0;
1505 }
1506
1507 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1508 int ch;
1509
1510 if (ident->state == IDENT_DRAINING) {
1511 ident_drain(ident, &output, osize_p);
1512 if (!*osize_p)
1513 break;
1514 continue;
1515 }
1516
1517 ch = *(input++);
1518 (*isize_p)--;
1519
1520 if (ident->state == IDENT_SKIPPING) {
1521 /*
1522 * Skipping until '$' or LF, but keeping them
1523 * in case it is a foreign ident.
1524 */
1525 strbuf_addch(&ident->left, ch);
1526 if (ch != '\n' && ch != '$')
1527 continue;
1528 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1529 strbuf_setlen(&ident->left, sizeof(head) - 1);
1530 strbuf_addstr(&ident->left, ident->ident);
1531 }
1532 ident->state = IDENT_DRAINING;
1533 continue;
1534 }
1535
1536 if (ident->state < sizeof(head) &&
1537 head[ident->state] == ch) {
1538 ident->state++;
1539 continue;
1540 }
1541
1542 if (ident->state)
1543 strbuf_add(&ident->left, head, ident->state);
1544 if (ident->state == sizeof(head) - 1) {
1545 if (ch != ':' && ch != '$') {
1546 strbuf_addch(&ident->left, ch);
1547 ident->state = 0;
1548 continue;
1549 }
1550
1551 if (ch == ':') {
1552 strbuf_addch(&ident->left, ch);
1553 ident->state = IDENT_SKIPPING;
1554 } else {
1555 strbuf_addstr(&ident->left, ident->ident);
1556 ident->state = IDENT_DRAINING;
1557 }
1558 continue;
1559 }
1560
1561 strbuf_addch(&ident->left, ch);
1562 ident->state = IDENT_DRAINING;
1563 }
1564 return 0;
1565}
1566
1567static void ident_free_fn(struct stream_filter *filter)
1568{
1569 struct ident_filter *ident = (struct ident_filter *)filter;
1570 strbuf_release(&ident->left);
1571 free(filter);
1572}
1573
1574static struct stream_filter_vtbl ident_vtbl = {
1575 ident_filter_fn,
1576 ident_free_fn,
1577};
1578
1579static struct stream_filter *ident_filter(const unsigned char *sha1)
1580{
1581 struct ident_filter *ident = xmalloc(sizeof(*ident));
1582
1583 xsnprintf(ident->ident, sizeof(ident->ident),
1584 ": %s $", sha1_to_hex(sha1));
1585 strbuf_init(&ident->left, 0);
1586 ident->filter.vtbl = &ident_vtbl;
1587 ident->state = 0;
1588 return (struct stream_filter *)ident;
1589}
1590
1591/*
1592 * Return an appropriately constructed filter for the path, or NULL if
1593 * the contents cannot be filtered without reading the whole thing
1594 * in-core.
1595 *
1596 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1597 * large binary blob you would want us not to slurp into the memory!
1598 */
1599struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
1600{
1601 struct conv_attrs ca;
1602 struct stream_filter *filter = NULL;
1603
1604 convert_attrs(&ca, path);
1605 if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean))
1606 return NULL;
1607
1608 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1609 return NULL;
1610
1611 if (ca.ident)
1612 filter = ident_filter(sha1);
1613
1614 if (output_eol(ca.crlf_action) == EOL_CRLF)
1615 filter = cascade_filter(filter, lf_to_crlf_filter());
1616 else
1617 filter = cascade_filter(filter, &null_filter_singleton);
1618
1619 return filter;
1620}
1621
1622void free_stream_filter(struct stream_filter *filter)
1623{
1624 filter->vtbl->free(filter);
1625}
1626
1627int stream_filter(struct stream_filter *filter,
1628 const char *input, size_t *isize_p,
1629 char *output, size_t *osize_p)
1630{
1631 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);
1632}