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