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