Provide git_config with a callback-data parameter
[git/git.git] / convert.c
CommitLineData
6c510bee 1#include "cache.h"
35ebfd6a 2#include "attr.h"
3fed15f5 3#include "run-command.h"
35ebfd6a 4
6c510bee
LT
5/*
6 * convert.c - convert a file when checking it out and checking it in.
7 *
8 * This should use the pathname to decide on whether it wants to do some
9 * more interesting conversions (automatic gzip/unzip, general format
10 * conversions etc etc), but by default it just does automatic CRLF<->LF
11 * translation when the "auto_crlf" option is set.
12 */
13
163b9591
JH
14#define CRLF_GUESS (-1)
15#define CRLF_BINARY 0
16#define CRLF_TEXT 1
17#define CRLF_INPUT 2
18
6c510bee 19struct text_stat {
28624193
DP
20 /* NUL, CR, LF and CRLF counts */
21 unsigned nul, cr, lf, crlf;
6c510bee
LT
22
23 /* These are just approximations! */
24 unsigned printable, nonprintable;
25};
26
27static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
28{
29 unsigned long i;
30
31 memset(stats, 0, sizeof(*stats));
32
33 for (i = 0; i < size; i++) {
34 unsigned char c = buf[i];
35 if (c == '\r') {
36 stats->cr++;
37 if (i+1 < size && buf[i+1] == '\n')
38 stats->crlf++;
39 continue;
40 }
41 if (c == '\n') {
42 stats->lf++;
43 continue;
44 }
45 if (c == 127)
46 /* DEL */
47 stats->nonprintable++;
48 else if (c < 32) {
49 switch (c) {
50 /* BS, HT, ESC and FF */
51 case '\b': case '\t': case '\033': case '\014':
52 stats->printable++;
53 break;
28624193
DP
54 case 0:
55 stats->nul++;
56 /* fall through */
6c510bee
LT
57 default:
58 stats->nonprintable++;
59 }
60 }
61 else
62 stats->printable++;
63 }
64}
65
66/*
67 * The same heuristics as diff.c::mmfile_is_binary()
68 */
69static int is_binary(unsigned long size, struct text_stat *stats)
70{
71
28624193
DP
72 if (stats->nul)
73 return 1;
6c510bee
LT
74 if ((stats->printable >> 7) < stats->nonprintable)
75 return 1;
76 /*
77 * Other heuristics? Average line length might be relevant,
78 * as might LF vs CR vs CRLF counts..
79 *
80 * NOTE! It might be normal to have a low ratio of CRLF to LF
81 * (somebody starts with a LF-only file and edits it with an editor
82 * that adds CRLF only to lines that are added..). But do we
83 * want to support CR-only? Probably not.
84 */
85 return 0;
86}
87
21e5ad50
SP
88static void check_safe_crlf(const char *path, int action,
89 struct text_stat *stats, enum safe_crlf checksafe)
90{
91 if (!checksafe)
92 return;
93
94 if (action == CRLF_INPUT || auto_crlf <= 0) {
95 /*
96 * CRLFs would not be restored by checkout:
97 * check if we'd remove CRLFs
98 */
99 if (stats->crlf) {
100 if (checksafe == SAFE_CRLF_WARN)
101 warning("CRLF will be replaced by LF in %s.", path);
102 else /* i.e. SAFE_CRLF_FAIL */
103 die("CRLF would be replaced by LF in %s.", path);
104 }
105 } else if (auto_crlf > 0) {
106 /*
107 * CRLFs would be added by checkout:
108 * check if we have "naked" LFs
109 */
110 if (stats->lf != stats->crlf) {
111 if (checksafe == SAFE_CRLF_WARN)
112 warning("LF will be replaced by CRLF in %s", path);
113 else /* i.e. SAFE_CRLF_FAIL */
114 die("LF would be replaced by CRLF in %s", path);
115 }
116 }
117}
118
5ecd293d 119static int crlf_to_git(const char *path, const char *src, size_t len,
21e5ad50 120 struct strbuf *buf, int action, enum safe_crlf checksafe)
6c510bee 121{
6c510bee 122 struct text_stat stats;
5ecd293d 123 char *dst;
6c510bee 124
5ecd293d
PH
125 if ((action == CRLF_BINARY) || !auto_crlf || !len)
126 return 0;
6c510bee 127
5ecd293d 128 gather_stats(src, len, &stats);
6c510bee 129
163b9591 130 if (action == CRLF_GUESS) {
201ac8ef
JH
131 /*
132 * We're currently not going to even try to convert stuff
133 * that has bare CR characters. Does anybody do that crazy
134 * stuff?
135 */
136 if (stats.cr != stats.crlf)
5ecd293d 137 return 0;
201ac8ef
JH
138
139 /*
140 * And add some heuristics for binary vs text, of course...
141 */
5ecd293d
PH
142 if (is_binary(len, &stats))
143 return 0;
201ac8ef 144 }
6c510bee 145
21e5ad50
SP
146 check_safe_crlf(path, action, &stats, checksafe);
147
148 /* Optimization: No CR? Nothing to convert, regardless. */
149 if (!stats.cr)
150 return 0;
151
90d16ec0
PH
152 /* only grow if not in place */
153 if (strbuf_avail(buf) + buf->len < len)
154 strbuf_grow(buf, len - buf->len);
5ecd293d 155 dst = buf->buf;
163b9591
JH
156 if (action == CRLF_GUESS) {
157 /*
158 * If we guessed, we already know we rejected a file with
159 * lone CR, and we can strip a CR without looking at what
160 * follow it.
161 */
201ac8ef 162 do {
ac78e548 163 unsigned char c = *src++;
201ac8ef 164 if (c != '\r')
ac78e548 165 *dst++ = c;
5ecd293d 166 } while (--len);
201ac8ef
JH
167 } else {
168 do {
ac78e548 169 unsigned char c = *src++;
5ecd293d 170 if (! (c == '\r' && (1 < len && *src == '\n')))
ac78e548 171 *dst++ = c;
5ecd293d 172 } while (--len);
201ac8ef 173 }
5ecd293d
PH
174 strbuf_setlen(buf, dst - buf->buf);
175 return 1;
6c510bee
LT
176}
177
5ecd293d
PH
178static int crlf_to_worktree(const char *path, const char *src, size_t len,
179 struct strbuf *buf, int action)
6c510bee 180{
5ecd293d 181 char *to_free = NULL;
6c510bee 182 struct text_stat stats;
6c510bee 183
163b9591 184 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
760f0c62 185 auto_crlf <= 0)
5ecd293d 186 return 0;
6c510bee 187
5ecd293d
PH
188 if (!len)
189 return 0;
6c510bee 190
5ecd293d 191 gather_stats(src, len, &stats);
6c510bee
LT
192
193 /* No LF? Nothing to convert, regardless. */
194 if (!stats.lf)
5ecd293d 195 return 0;
6c510bee
LT
196
197 /* Was it already in CRLF format? */
198 if (stats.lf == stats.crlf)
5ecd293d 199 return 0;
6c510bee 200
163b9591 201 if (action == CRLF_GUESS) {
201ac8ef
JH
202 /* If we have any bare CR characters, we're not going to touch it */
203 if (stats.cr != stats.crlf)
5ecd293d 204 return 0;
6c510bee 205
5ecd293d
PH
206 if (is_binary(len, &stats))
207 return 0;
201ac8ef 208 }
6c510bee 209
5ecd293d
PH
210 /* are we "faking" in place editing ? */
211 if (src == buf->buf)
b315c5c0 212 to_free = strbuf_detach(buf, NULL);
5ecd293d
PH
213
214 strbuf_grow(buf, len + stats.lf - stats.crlf);
215 for (;;) {
216 const char *nl = memchr(src, '\n', len);
217 if (!nl)
218 break;
219 if (nl > src && nl[-1] == '\r') {
220 strbuf_add(buf, src, nl + 1 - src);
221 } else {
222 strbuf_add(buf, src, nl - src);
223 strbuf_addstr(buf, "\r\n");
224 }
225 len -= nl + 1 - src;
226 src = nl + 1;
227 }
228 strbuf_add(buf, src, len);
229
230 free(to_free);
231 return 1;
6c510bee 232}
35ebfd6a 233
546bb582
JS
234struct filter_params {
235 const char *src;
236 unsigned long size;
237 const char *cmd;
238};
239
240static int filter_buffer(int fd, void *data)
aa4ed402
JH
241{
242 /*
243 * Spawn cmd and feed the buffer contents through its stdin.
244 */
245 struct child_process child_process;
546bb582 246 struct filter_params *params = (struct filter_params *)data;
aa4ed402 247 int write_err, status;
546bb582 248 const char *argv[] = { "sh", "-c", params->cmd, NULL };
aa4ed402
JH
249
250 memset(&child_process, 0, sizeof(child_process));
dc1bfdcd
JS
251 child_process.argv = argv;
252 child_process.in = -1;
7683b6e8 253 child_process.out = fd;
aa4ed402 254
dc1bfdcd 255 if (start_command(&child_process))
546bb582 256 return error("cannot fork to run external filter %s", params->cmd);
aa4ed402 257
546bb582 258 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
dc1bfdcd 259 if (close(child_process.in))
aa4ed402
JH
260 write_err = 1;
261 if (write_err)
546bb582 262 error("cannot feed the input to external filter %s", params->cmd);
aa4ed402
JH
263
264 status = finish_command(&child_process);
265 if (status)
546bb582 266 error("external filter %s failed %d", params->cmd, -status);
aa4ed402
JH
267 return (write_err || status);
268}
269
5ecd293d
PH
270static int apply_filter(const char *path, const char *src, size_t len,
271 struct strbuf *dst, const char *cmd)
aa4ed402
JH
272{
273 /*
274 * Create a pipeline to have the command filter the buffer's
275 * contents.
276 *
277 * (child --> cmd) --> us
278 */
546bb582 279 int ret = 1;
5ecd293d 280 struct strbuf nbuf;
546bb582
JS
281 struct async async;
282 struct filter_params params;
aa4ed402
JH
283
284 if (!cmd)
5ecd293d 285 return 0;
aa4ed402 286
546bb582
JS
287 memset(&async, 0, sizeof(async));
288 async.proc = filter_buffer;
289 async.data = &params;
290 params.src = src;
291 params.size = len;
292 params.cmd = cmd;
aa4ed402
JH
293
294 fflush(NULL);
546bb582
JS
295 if (start_async(&async))
296 return 0; /* error was already reported */
aa4ed402 297
5ecd293d 298 strbuf_init(&nbuf, 0);
546bb582 299 if (strbuf_read(&nbuf, async.out, len) < 0) {
5ecd293d
PH
300 error("read from external filter %s failed", cmd);
301 ret = 0;
aa4ed402 302 }
546bb582 303 if (close(async.out)) {
90d16ec0 304 error("read from external filter %s failed", cmd);
5ecd293d 305 ret = 0;
aa4ed402 306 }
546bb582
JS
307 if (finish_async(&async)) {
308 error("external filter %s failed", cmd);
5ecd293d 309 ret = 0;
aa4ed402
JH
310 }
311
5ecd293d 312 if (ret) {
90d16ec0 313 strbuf_swap(dst, &nbuf);
5ecd293d 314 }
90d16ec0 315 strbuf_release(&nbuf);
5ecd293d 316 return ret;
aa4ed402
JH
317}
318
319static struct convert_driver {
320 const char *name;
321 struct convert_driver *next;
322 char *smudge;
323 char *clean;
324} *user_convert, **user_convert_tail;
325
ef90d6d4 326static int read_convert_config(const char *var, const char *value, void *cb)
aa4ed402
JH
327{
328 const char *ep, *name;
329 int namelen;
330 struct convert_driver *drv;
331
332 /*
333 * External conversion drivers are configured using
334 * "filter.<name>.variable".
335 */
336 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
337 return 0;
338 name = var + 7;
339 namelen = ep - name;
340 for (drv = user_convert; drv; drv = drv->next)
341 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
342 break;
343 if (!drv) {
aa4ed402 344 drv = xcalloc(1, sizeof(struct convert_driver));
182af834 345 drv->name = xmemdupz(name, namelen);
aa4ed402
JH
346 *user_convert_tail = drv;
347 user_convert_tail = &(drv->next);
348 }
349
350 ep++;
351
352 /*
353 * filter.<name>.smudge and filter.<name>.clean specifies
354 * the command line:
355 *
356 * command-line
357 *
358 * The command-line will not be interpolated in any way.
359 */
360
361 if (!strcmp("smudge", ep)) {
362 if (!value)
a7269e5c 363 return config_error_nonbool(var);
aa4ed402
JH
364 drv->smudge = strdup(value);
365 return 0;
366 }
367
368 if (!strcmp("clean", ep)) {
369 if (!value)
a7269e5c 370 return config_error_nonbool(var);
aa4ed402
JH
371 drv->clean = strdup(value);
372 return 0;
373 }
374 return 0;
375}
376
6073ee85 377static void setup_convert_check(struct git_attr_check *check)
35ebfd6a
JH
378{
379 static struct git_attr *attr_crlf;
3fed15f5 380 static struct git_attr *attr_ident;
aa4ed402 381 static struct git_attr *attr_filter;
35ebfd6a 382
3fed15f5 383 if (!attr_crlf) {
35ebfd6a 384 attr_crlf = git_attr("crlf", 4);
3fed15f5 385 attr_ident = git_attr("ident", 5);
aa4ed402
JH
386 attr_filter = git_attr("filter", 6);
387 user_convert_tail = &user_convert;
ef90d6d4 388 git_config(read_convert_config, NULL);
3fed15f5
JH
389 }
390 check[0].attr = attr_crlf;
391 check[1].attr = attr_ident;
aa4ed402 392 check[2].attr = attr_filter;
3fed15f5
JH
393}
394
395static int count_ident(const char *cp, unsigned long size)
396{
397 /*
af9b54bb 398 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
3fed15f5
JH
399 */
400 int cnt = 0;
401 char ch;
402
403 while (size) {
404 ch = *cp++;
405 size--;
406 if (ch != '$')
407 continue;
af9b54bb 408 if (size < 3)
3fed15f5 409 break;
af9b54bb 410 if (memcmp("Id", cp, 2))
3fed15f5 411 continue;
af9b54bb
AP
412 ch = cp[2];
413 cp += 3;
414 size -= 3;
3fed15f5 415 if (ch == '$')
af9b54bb 416 cnt++; /* $Id$ */
3fed15f5
JH
417 if (ch != ':')
418 continue;
419
420 /*
af9b54bb 421 * "$Id: ... "; scan up to the closing dollar sign and discard.
3fed15f5
JH
422 */
423 while (size) {
424 ch = *cp++;
425 size--;
426 if (ch == '$') {
427 cnt++;
428 break;
429 }
430 }
431 }
432 return cnt;
433}
434
5ecd293d
PH
435static int ident_to_git(const char *path, const char *src, size_t len,
436 struct strbuf *buf, int ident)
3fed15f5 437{
5ecd293d 438 char *dst, *dollar;
3fed15f5 439
5ecd293d
PH
440 if (!ident || !count_ident(src, len))
441 return 0;
442
90d16ec0
PH
443 /* only grow if not in place */
444 if (strbuf_avail(buf) + buf->len < len)
445 strbuf_grow(buf, len - buf->len);
5ecd293d
PH
446 dst = buf->buf;
447 for (;;) {
448 dollar = memchr(src, '$', len);
449 if (!dollar)
450 break;
451 memcpy(dst, src, dollar + 1 - src);
452 dst += dollar + 1 - src;
453 len -= dollar + 1 - src;
454 src = dollar + 1;
455
456 if (len > 3 && !memcmp(src, "Id:", 3)) {
457 dollar = memchr(src + 3, '$', len - 3);
458 if (!dollar)
459 break;
af9b54bb
AP
460 memcpy(dst, "Id$", 3);
461 dst += 3;
5ecd293d
PH
462 len -= dollar + 1 - src;
463 src = dollar + 1;
3fed15f5
JH
464 }
465 }
5ecd293d
PH
466 memcpy(dst, src, len);
467 strbuf_setlen(buf, dst + len - buf->buf);
468 return 1;
3fed15f5
JH
469}
470
5ecd293d
PH
471static int ident_to_worktree(const char *path, const char *src, size_t len,
472 struct strbuf *buf, int ident)
3fed15f5 473{
3fed15f5 474 unsigned char sha1[20];
5ecd293d
PH
475 char *to_free = NULL, *dollar;
476 int cnt;
3fed15f5
JH
477
478 if (!ident)
5ecd293d 479 return 0;
3fed15f5 480
5ecd293d 481 cnt = count_ident(src, len);
3fed15f5 482 if (!cnt)
5ecd293d 483 return 0;
3fed15f5 484
5ecd293d
PH
485 /* are we "faking" in place editing ? */
486 if (src == buf->buf)
b315c5c0 487 to_free = strbuf_detach(buf, NULL);
5ecd293d 488 hash_sha1_file(src, len, "blob", sha1);
3fed15f5 489
5ecd293d
PH
490 strbuf_grow(buf, len + cnt * 43);
491 for (;;) {
492 /* step 1: run to the next '$' */
493 dollar = memchr(src, '$', len);
494 if (!dollar)
495 break;
496 strbuf_add(buf, src, dollar + 1 - src);
497 len -= dollar + 1 - src;
498 src = dollar + 1;
c23290d5 499
5ecd293d
PH
500 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
501 if (len < 3 || memcmp("Id", src, 2))
3fed15f5
JH
502 continue;
503
5ecd293d
PH
504 /* step 3: skip over Id$ or Id:xxxxx$ */
505 if (src[2] == '$') {
506 src += 3;
507 len -= 3;
508 } else if (src[2] == ':') {
509 /*
510 * It's possible that an expanded Id has crept its way into the
511 * repository, we cope with that by stripping the expansion out
512 */
513 dollar = memchr(src + 3, '$', len - 3);
514 if (!dollar) {
515 /* incomplete keyword, no more '$', so just quit the loop */
516 break;
517 }
c23290d5 518
5ecd293d
PH
519 len -= dollar + 1 - src;
520 src = dollar + 1;
521 } else {
522 /* it wasn't a "Id$" or "Id:xxxx$" */
523 continue;
524 }
c23290d5 525
5ecd293d
PH
526 /* step 4: substitute */
527 strbuf_addstr(buf, "Id: ");
528 strbuf_add(buf, sha1_to_hex(sha1), 40);
529 strbuf_addstr(buf, " $");
3fed15f5 530 }
5ecd293d 531 strbuf_add(buf, src, len);
3fed15f5 532
5ecd293d
PH
533 free(to_free);
534 return 1;
35ebfd6a
JH
535}
536
6073ee85 537static int git_path_check_crlf(const char *path, struct git_attr_check *check)
35ebfd6a 538{
6073ee85
JH
539 const char *value = check->value;
540
541 if (ATTR_TRUE(value))
542 return CRLF_TEXT;
543 else if (ATTR_FALSE(value))
544 return CRLF_BINARY;
545 else if (ATTR_UNSET(value))
546 ;
547 else if (!strcmp(value, "input"))
548 return CRLF_INPUT;
163b9591 549 return CRLF_GUESS;
35ebfd6a
JH
550}
551
aa4ed402
JH
552static struct convert_driver *git_path_check_convert(const char *path,
553 struct git_attr_check *check)
554{
555 const char *value = check->value;
556 struct convert_driver *drv;
557
558 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
559 return NULL;
560 for (drv = user_convert; drv; drv = drv->next)
561 if (!strcmp(value, drv->name))
562 return drv;
563 return NULL;
564}
565
3fed15f5
JH
566static int git_path_check_ident(const char *path, struct git_attr_check *check)
567{
568 const char *value = check->value;
569
570 return !!ATTR_TRUE(value);
571}
572
21e5ad50
SP
573int convert_to_git(const char *path, const char *src, size_t len,
574 struct strbuf *dst, enum safe_crlf checksafe)
35ebfd6a 575{
aa4ed402 576 struct git_attr_check check[3];
6073ee85 577 int crlf = CRLF_GUESS;
5ecd293d 578 int ident = 0, ret = 0;
aa4ed402 579 char *filter = NULL;
6073ee85
JH
580
581 setup_convert_check(check);
3fed15f5 582 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 583 struct convert_driver *drv;
3fed15f5
JH
584 crlf = git_path_check_crlf(path, check + 0);
585 ident = git_path_check_ident(path, check + 1);
aa4ed402
JH
586 drv = git_path_check_convert(path, check + 2);
587 if (drv && drv->clean)
588 filter = drv->clean;
3fed15f5
JH
589 }
590
5ecd293d
PH
591 ret |= apply_filter(path, src, len, dst, filter);
592 if (ret) {
593 src = dst->buf;
594 len = dst->len;
aa4ed402 595 }
21e5ad50 596 ret |= crlf_to_git(path, src, len, dst, crlf, checksafe);
5ecd293d
PH
597 if (ret) {
598 src = dst->buf;
599 len = dst->len;
6073ee85 600 }
5ecd293d 601 return ret | ident_to_git(path, src, len, dst, ident);
35ebfd6a
JH
602}
603
5ecd293d 604int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
35ebfd6a 605{
aa4ed402 606 struct git_attr_check check[3];
6073ee85 607 int crlf = CRLF_GUESS;
5ecd293d 608 int ident = 0, ret = 0;
aa4ed402 609 char *filter = NULL;
6073ee85
JH
610
611 setup_convert_check(check);
3fed15f5 612 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 613 struct convert_driver *drv;
3fed15f5
JH
614 crlf = git_path_check_crlf(path, check + 0);
615 ident = git_path_check_ident(path, check + 1);
aa4ed402
JH
616 drv = git_path_check_convert(path, check + 2);
617 if (drv && drv->smudge)
618 filter = drv->smudge;
6073ee85 619 }
3fed15f5 620
5ecd293d
PH
621 ret |= ident_to_worktree(path, src, len, dst, ident);
622 if (ret) {
623 src = dst->buf;
624 len = dst->len;
3fed15f5 625 }
5ecd293d
PH
626 ret |= crlf_to_worktree(path, src, len, dst, crlf);
627 if (ret) {
628 src = dst->buf;
629 len = dst->len;
aa4ed402 630 }
5ecd293d 631 return ret | apply_filter(path, src, len, dst, filter);
35ebfd6a 632}