]> git.ipfire.org Git - thirdparty/git.git/blame - convert.c
Merge branch 'maint'
[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
ae6a5609 244static int filter_buffer(int in, int out, 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;
ac0ba18d 252 const char *argv[] = { params->cmd, NULL };
aa4ed402
JH
253
254 memset(&child_process, 0, sizeof(child_process));
dc1bfdcd 255 child_process.argv = argv;
ac0ba18d 256 child_process.use_shell = 1;
dc1bfdcd 257 child_process.in = -1;
ae6a5609 258 child_process.out = out;
aa4ed402 259
dc1bfdcd 260 if (start_command(&child_process))
546bb582 261 return error("cannot fork to run external filter %s", params->cmd);
aa4ed402 262
546bb582 263 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
dc1bfdcd 264 if (close(child_process.in))
aa4ed402
JH
265 write_err = 1;
266 if (write_err)
546bb582 267 error("cannot feed the input to external filter %s", params->cmd);
aa4ed402
JH
268
269 status = finish_command(&child_process);
270 if (status)
5709e036 271 error("external filter %s failed %d", params->cmd, status);
aa4ed402
JH
272 return (write_err || status);
273}
274
5ecd293d
PH
275static int apply_filter(const char *path, const char *src, size_t len,
276 struct strbuf *dst, const char *cmd)
aa4ed402
JH
277{
278 /*
279 * Create a pipeline to have the command filter the buffer's
280 * contents.
281 *
282 * (child --> cmd) --> us
283 */
546bb582 284 int ret = 1;
f285a2d7 285 struct strbuf nbuf = STRBUF_INIT;
546bb582
JS
286 struct async async;
287 struct filter_params params;
aa4ed402
JH
288
289 if (!cmd)
5ecd293d 290 return 0;
aa4ed402 291
546bb582
JS
292 memset(&async, 0, sizeof(async));
293 async.proc = filter_buffer;
294 async.data = &params;
ae6a5609 295 async.out = -1;
546bb582
JS
296 params.src = src;
297 params.size = len;
298 params.cmd = cmd;
aa4ed402
JH
299
300 fflush(NULL);
546bb582
JS
301 if (start_async(&async))
302 return 0; /* error was already reported */
aa4ed402 303
546bb582 304 if (strbuf_read(&nbuf, async.out, len) < 0) {
5ecd293d
PH
305 error("read from external filter %s failed", cmd);
306 ret = 0;
aa4ed402 307 }
546bb582 308 if (close(async.out)) {
90d16ec0 309 error("read from external filter %s failed", cmd);
5ecd293d 310 ret = 0;
aa4ed402 311 }
546bb582
JS
312 if (finish_async(&async)) {
313 error("external filter %s failed", cmd);
5ecd293d 314 ret = 0;
aa4ed402
JH
315 }
316
5ecd293d 317 if (ret) {
90d16ec0 318 strbuf_swap(dst, &nbuf);
5ecd293d 319 }
90d16ec0 320 strbuf_release(&nbuf);
5ecd293d 321 return ret;
aa4ed402
JH
322}
323
324static struct convert_driver {
325 const char *name;
326 struct convert_driver *next;
cd8be6c9
BH
327 const char *smudge;
328 const char *clean;
aa4ed402
JH
329} *user_convert, **user_convert_tail;
330
ef90d6d4 331static int read_convert_config(const char *var, const char *value, void *cb)
aa4ed402
JH
332{
333 const char *ep, *name;
334 int namelen;
335 struct convert_driver *drv;
336
337 /*
338 * External conversion drivers are configured using
339 * "filter.<name>.variable".
340 */
341 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
342 return 0;
343 name = var + 7;
344 namelen = ep - name;
345 for (drv = user_convert; drv; drv = drv->next)
346 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
347 break;
348 if (!drv) {
aa4ed402 349 drv = xcalloc(1, sizeof(struct convert_driver));
182af834 350 drv->name = xmemdupz(name, namelen);
aa4ed402
JH
351 *user_convert_tail = drv;
352 user_convert_tail = &(drv->next);
353 }
354
355 ep++;
356
357 /*
358 * filter.<name>.smudge and filter.<name>.clean specifies
359 * the command line:
360 *
361 * command-line
362 *
363 * The command-line will not be interpolated in any way.
364 */
365
cd8be6c9
BH
366 if (!strcmp("smudge", ep))
367 return git_config_string(&drv->smudge, var, value);
368
369 if (!strcmp("clean", ep))
370 return git_config_string(&drv->clean, var, value);
aa4ed402 371
aa4ed402
JH
372 return 0;
373}
374
6073ee85 375static void setup_convert_check(struct git_attr_check *check)
35ebfd6a
JH
376{
377 static struct git_attr *attr_crlf;
3fed15f5 378 static struct git_attr *attr_ident;
aa4ed402 379 static struct git_attr *attr_filter;
35ebfd6a 380
3fed15f5 381 if (!attr_crlf) {
7fb0eaa2
JH
382 attr_crlf = git_attr("crlf");
383 attr_ident = git_attr("ident");
384 attr_filter = git_attr("filter");
aa4ed402 385 user_convert_tail = &user_convert;
ef90d6d4 386 git_config(read_convert_config, NULL);
3fed15f5
JH
387 }
388 check[0].attr = attr_crlf;
389 check[1].attr = attr_ident;
aa4ed402 390 check[2].attr = attr_filter;
3fed15f5
JH
391}
392
393static int count_ident(const char *cp, unsigned long size)
394{
395 /*
af9b54bb 396 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
3fed15f5
JH
397 */
398 int cnt = 0;
399 char ch;
400
401 while (size) {
402 ch = *cp++;
403 size--;
404 if (ch != '$')
405 continue;
af9b54bb 406 if (size < 3)
3fed15f5 407 break;
af9b54bb 408 if (memcmp("Id", cp, 2))
3fed15f5 409 continue;
af9b54bb
AP
410 ch = cp[2];
411 cp += 3;
412 size -= 3;
3fed15f5 413 if (ch == '$')
af9b54bb 414 cnt++; /* $Id$ */
3fed15f5
JH
415 if (ch != ':')
416 continue;
417
418 /*
af9b54bb 419 * "$Id: ... "; scan up to the closing dollar sign and discard.
3fed15f5
JH
420 */
421 while (size) {
422 ch = *cp++;
423 size--;
424 if (ch == '$') {
425 cnt++;
426 break;
427 }
a9f3049f
HG
428 if (ch == '\n')
429 break;
3fed15f5
JH
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;
a9f3049f
HG
460 if (memchr(src + 3, '\n', dollar - src - 3)) {
461 /* Line break before the next dollar. */
462 continue;
463 }
464
af9b54bb
AP
465 memcpy(dst, "Id$", 3);
466 dst += 3;
5ecd293d
PH
467 len -= dollar + 1 - src;
468 src = dollar + 1;
3fed15f5
JH
469 }
470 }
5ecd293d
PH
471 memcpy(dst, src, len);
472 strbuf_setlen(buf, dst + len - buf->buf);
473 return 1;
3fed15f5
JH
474}
475
5ecd293d
PH
476static int ident_to_worktree(const char *path, const char *src, size_t len,
477 struct strbuf *buf, int ident)
3fed15f5 478{
3fed15f5 479 unsigned char sha1[20];
07814d90 480 char *to_free = NULL, *dollar, *spc;
5ecd293d 481 int cnt;
3fed15f5
JH
482
483 if (!ident)
5ecd293d 484 return 0;
3fed15f5 485
5ecd293d 486 cnt = count_ident(src, len);
3fed15f5 487 if (!cnt)
5ecd293d 488 return 0;
3fed15f5 489
5ecd293d
PH
490 /* are we "faking" in place editing ? */
491 if (src == buf->buf)
b315c5c0 492 to_free = strbuf_detach(buf, NULL);
5ecd293d 493 hash_sha1_file(src, len, "blob", sha1);
3fed15f5 494
5ecd293d
PH
495 strbuf_grow(buf, len + cnt * 43);
496 for (;;) {
497 /* step 1: run to the next '$' */
498 dollar = memchr(src, '$', len);
499 if (!dollar)
500 break;
501 strbuf_add(buf, src, dollar + 1 - src);
502 len -= dollar + 1 - src;
503 src = dollar + 1;
c23290d5 504
5ecd293d
PH
505 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
506 if (len < 3 || memcmp("Id", src, 2))
3fed15f5
JH
507 continue;
508
5ecd293d
PH
509 /* step 3: skip over Id$ or Id:xxxxx$ */
510 if (src[2] == '$') {
511 src += 3;
512 len -= 3;
513 } else if (src[2] == ':') {
514 /*
515 * It's possible that an expanded Id has crept its way into the
07814d90
HG
516 * repository, we cope with that by stripping the expansion out.
517 * This is probably not a good idea, since it will cause changes
518 * on checkout, which won't go away by stash, but let's keep it
519 * for git-style ids.
5ecd293d
PH
520 */
521 dollar = memchr(src + 3, '$', len - 3);
522 if (!dollar) {
523 /* incomplete keyword, no more '$', so just quit the loop */
524 break;
525 }
c23290d5 526
a9f3049f
HG
527 if (memchr(src + 3, '\n', dollar - src - 3)) {
528 /* Line break before the next dollar. */
529 continue;
530 }
531
07814d90
HG
532 spc = memchr(src + 4, ' ', dollar - src - 4);
533 if (spc && spc < dollar-1) {
534 /* There are spaces in unexpected places.
535 * This is probably an id from some other
536 * versioning system. Keep it for now.
537 */
538 continue;
539 }
540
5ecd293d
PH
541 len -= dollar + 1 - src;
542 src = dollar + 1;
543 } else {
544 /* it wasn't a "Id$" or "Id:xxxx$" */
545 continue;
546 }
c23290d5 547
5ecd293d
PH
548 /* step 4: substitute */
549 strbuf_addstr(buf, "Id: ");
550 strbuf_add(buf, sha1_to_hex(sha1), 40);
551 strbuf_addstr(buf, " $");
3fed15f5 552 }
5ecd293d 553 strbuf_add(buf, src, len);
3fed15f5 554
5ecd293d
PH
555 free(to_free);
556 return 1;
35ebfd6a
JH
557}
558
6073ee85 559static int git_path_check_crlf(const char *path, struct git_attr_check *check)
35ebfd6a 560{
6073ee85
JH
561 const char *value = check->value;
562
563 if (ATTR_TRUE(value))
564 return CRLF_TEXT;
565 else if (ATTR_FALSE(value))
566 return CRLF_BINARY;
567 else if (ATTR_UNSET(value))
568 ;
569 else if (!strcmp(value, "input"))
570 return CRLF_INPUT;
163b9591 571 return CRLF_GUESS;
35ebfd6a
JH
572}
573
aa4ed402
JH
574static struct convert_driver *git_path_check_convert(const char *path,
575 struct git_attr_check *check)
576{
577 const char *value = check->value;
578 struct convert_driver *drv;
579
580 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
581 return NULL;
582 for (drv = user_convert; drv; drv = drv->next)
583 if (!strcmp(value, drv->name))
584 return drv;
585 return NULL;
586}
587
3fed15f5
JH
588static int git_path_check_ident(const char *path, struct git_attr_check *check)
589{
590 const char *value = check->value;
591
592 return !!ATTR_TRUE(value);
593}
594
21e5ad50
SP
595int convert_to_git(const char *path, const char *src, size_t len,
596 struct strbuf *dst, enum safe_crlf checksafe)
35ebfd6a 597{
aa4ed402 598 struct git_attr_check check[3];
6073ee85 599 int crlf = CRLF_GUESS;
5ecd293d 600 int ident = 0, ret = 0;
cd8be6c9 601 const char *filter = NULL;
6073ee85
JH
602
603 setup_convert_check(check);
3fed15f5 604 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 605 struct convert_driver *drv;
3fed15f5
JH
606 crlf = git_path_check_crlf(path, check + 0);
607 ident = git_path_check_ident(path, check + 1);
aa4ed402
JH
608 drv = git_path_check_convert(path, check + 2);
609 if (drv && drv->clean)
610 filter = drv->clean;
3fed15f5
JH
611 }
612
5ecd293d
PH
613 ret |= apply_filter(path, src, len, dst, filter);
614 if (ret) {
615 src = dst->buf;
616 len = dst->len;
aa4ed402 617 }
21e5ad50 618 ret |= crlf_to_git(path, src, len, dst, crlf, checksafe);
5ecd293d
PH
619 if (ret) {
620 src = dst->buf;
621 len = dst->len;
6073ee85 622 }
5ecd293d 623 return ret | ident_to_git(path, src, len, dst, ident);
35ebfd6a
JH
624}
625
5ecd293d 626int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
35ebfd6a 627{
aa4ed402 628 struct git_attr_check check[3];
6073ee85 629 int crlf = CRLF_GUESS;
5ecd293d 630 int ident = 0, ret = 0;
cd8be6c9 631 const char *filter = NULL;
6073ee85
JH
632
633 setup_convert_check(check);
3fed15f5 634 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 635 struct convert_driver *drv;
3fed15f5
JH
636 crlf = git_path_check_crlf(path, check + 0);
637 ident = git_path_check_ident(path, check + 1);
aa4ed402
JH
638 drv = git_path_check_convert(path, check + 2);
639 if (drv && drv->smudge)
640 filter = drv->smudge;
6073ee85 641 }
3fed15f5 642
5ecd293d
PH
643 ret |= ident_to_worktree(path, src, len, dst, ident);
644 if (ret) {
645 src = dst->buf;
646 len = dst->len;
3fed15f5 647 }
5ecd293d
PH
648 ret |= crlf_to_worktree(path, src, len, dst, crlf);
649 if (ret) {
650 src = dst->buf;
651 len = dst->len;
aa4ed402 652 }
5ecd293d 653 return ret | apply_filter(path, src, len, dst, filter);
35ebfd6a 654}