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