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