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