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