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