8dc9965081320c52152fb635354ee26a0ad693f7
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(int fd, 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 child_process.out = fd;
209
210 if (start_command(&child_process))
211 return error("cannot fork to run external filter %s", cmd);
212
213 write_err = (write_in_full(child_process.in, src, size) < 0);
214 if (close(child_process.in))
215 write_err = 1;
216 if (write_err)
217 error("cannot feed the input to external filter %s", cmd);
218
219 status = finish_command(&child_process);
220 if (status)
221 error("external filter %s failed %d", cmd, -status);
222 return (write_err || status);
223}
224
225static int apply_filter(const char *path, const char *src, size_t len,
226 struct strbuf *dst, const char *cmd)
227{
228 /*
229 * Create a pipeline to have the command filter the buffer's
230 * contents.
231 *
232 * (child --> cmd) --> us
233 */
234 int pipe_feed[2];
235 int status, ret = 1;
236 struct child_process child_process;
237 struct strbuf nbuf;
238
239 if (!cmd)
240 return 0;
241
242 memset(&child_process, 0, sizeof(child_process));
243
244 if (pipe(pipe_feed) < 0) {
245 error("cannot create pipe to run external filter %s", cmd);
246 return 0;
247 }
248
249 fflush(NULL);
250 child_process.pid = fork();
251 if (child_process.pid < 0) {
252 error("cannot fork to run external filter %s", cmd);
253 close(pipe_feed[0]);
254 close(pipe_feed[1]);
255 return 0;
256 }
257 if (!child_process.pid) {
258 close(pipe_feed[0]);
259 exit(filter_buffer(pipe_feed[1], src, len, cmd));
260 }
261 close(pipe_feed[1]);
262
263 strbuf_init(&nbuf, 0);
264 if (strbuf_read(&nbuf, pipe_feed[0], len) < 0) {
265 error("read from external filter %s failed", cmd);
266 ret = 0;
267 }
268 if (close(pipe_feed[0])) {
269 error("read from external filter %s failed", cmd);
270 ret = 0;
271 }
272 status = finish_command(&child_process);
273 if (status) {
274 error("external filter %s failed %d", cmd, -status);
275 ret = 0;
276 }
277
278 if (ret) {
279 strbuf_swap(dst, &nbuf);
280 }
281 strbuf_release(&nbuf);
282 return ret;
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) {
310 drv = xcalloc(1, sizeof(struct convert_driver));
311 drv->name = xmemdupz(name, namelen);
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
343static void setup_convert_check(struct git_attr_check *check)
344{
345 static struct git_attr *attr_crlf;
346 static struct git_attr *attr_ident;
347 static struct git_attr *attr_filter;
348
349 if (!attr_crlf) {
350 attr_crlf = git_attr("crlf", 4);
351 attr_ident = git_attr("ident", 5);
352 attr_filter = git_attr("filter", 6);
353 user_convert_tail = &user_convert;
354 git_config(read_convert_config);
355 }
356 check[0].attr = attr_crlf;
357 check[1].attr = attr_ident;
358 check[2].attr = attr_filter;
359}
360
361static int count_ident(const char *cp, unsigned long size)
362{
363 /*
364 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
365 */
366 int cnt = 0;
367 char ch;
368
369 while (size) {
370 ch = *cp++;
371 size--;
372 if (ch != '$')
373 continue;
374 if (size < 3)
375 break;
376 if (memcmp("Id", cp, 2))
377 continue;
378 ch = cp[2];
379 cp += 3;
380 size -= 3;
381 if (ch == '$')
382 cnt++; /* $Id$ */
383 if (ch != ':')
384 continue;
385
386 /*
387 * "$Id: ... "; scan up to the closing dollar sign and discard.
388 */
389 while (size) {
390 ch = *cp++;
391 size--;
392 if (ch == '$') {
393 cnt++;
394 break;
395 }
396 }
397 }
398 return cnt;
399}
400
401static int ident_to_git(const char *path, const char *src, size_t len,
402 struct strbuf *buf, int ident)
403{
404 char *dst, *dollar;
405
406 if (!ident || !count_ident(src, len))
407 return 0;
408
409 /* only grow if not in place */
410 if (strbuf_avail(buf) + buf->len < len)
411 strbuf_grow(buf, len - buf->len);
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;
426 memcpy(dst, "Id$", 3);
427 dst += 3;
428 len -= dollar + 1 - src;
429 src = dollar + 1;
430 }
431 }
432 memcpy(dst, src, len);
433 strbuf_setlen(buf, dst + len - buf->buf);
434 return 1;
435}
436
437static int ident_to_worktree(const char *path, const char *src, size_t len,
438 struct strbuf *buf, int ident)
439{
440 unsigned char sha1[20];
441 char *to_free = NULL, *dollar;
442 int cnt;
443
444 if (!ident)
445 return 0;
446
447 cnt = count_ident(src, len);
448 if (!cnt)
449 return 0;
450
451 /* are we "faking" in place editing ? */
452 if (src == buf->buf)
453 to_free = strbuf_detach(buf, NULL);
454 hash_sha1_file(src, len, "blob", sha1);
455
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;
465
466 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
467 if (len < 3 || memcmp("Id", src, 2))
468 continue;
469
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 }
484
485 len -= dollar + 1 - src;
486 src = dollar + 1;
487 } else {
488 /* it wasn't a "Id$" or "Id:xxxx$" */
489 continue;
490 }
491
492 /* step 4: substitute */
493 strbuf_addstr(buf, "Id: ");
494 strbuf_add(buf, sha1_to_hex(sha1), 40);
495 strbuf_addstr(buf, " $");
496 }
497 strbuf_add(buf, src, len);
498
499 free(to_free);
500 return 1;
501}
502
503static int git_path_check_crlf(const char *path, struct git_attr_check *check)
504{
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;
515 return CRLF_GUESS;
516}
517
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
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
539int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
540{
541 struct git_attr_check check[3];
542 int crlf = CRLF_GUESS;
543 int ident = 0, ret = 0;
544 char *filter = NULL;
545
546 setup_convert_check(check);
547 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
548 struct convert_driver *drv;
549 crlf = git_path_check_crlf(path, check + 0);
550 ident = git_path_check_ident(path, check + 1);
551 drv = git_path_check_convert(path, check + 2);
552 if (drv && drv->clean)
553 filter = drv->clean;
554 }
555
556 ret |= apply_filter(path, src, len, dst, filter);
557 if (ret) {
558 src = dst->buf;
559 len = dst->len;
560 }
561 ret |= crlf_to_git(path, src, len, dst, crlf);
562 if (ret) {
563 src = dst->buf;
564 len = dst->len;
565 }
566 return ret | ident_to_git(path, src, len, dst, ident);
567}
568
569int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
570{
571 struct git_attr_check check[3];
572 int crlf = CRLF_GUESS;
573 int ident = 0, ret = 0;
574 char *filter = NULL;
575
576 setup_convert_check(check);
577 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
578 struct convert_driver *drv;
579 crlf = git_path_check_crlf(path, check + 0);
580 ident = git_path_check_ident(path, check + 1);
581 drv = git_path_check_convert(path, check + 2);
582 if (drv && drv->smudge)
583 filter = drv->smudge;
584 }
585
586 ret |= ident_to_worktree(path, src, len, dst, ident);
587 if (ret) {
588 src = dst->buf;
589 len = dst->len;
590 }
591 ret |= crlf_to_worktree(path, src, len, dst, crlf);
592 if (ret) {
593 src = dst->buf;
594 len = dst->len;
595 }
596 return ret | apply_filter(path, src, len, dst, filter);
597}