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