skip t5512 because remote does not yet work
[git/platforms/storm.git] / convert.c
blob6333a1cf63a6140ec4c977b90da372b5dbbb3cbe
1 #include "cache.h"
2 #include "attr.h"
3 #include "run-command.h"
5 /*
6 * convert.c - convert a file when checking it out and checking it in.
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.
14 #define CRLF_GUESS (-1)
15 #define CRLF_BINARY 0
16 #define CRLF_TEXT 1
17 #define CRLF_INPUT 2
19 struct text_stat {
20 /* CR, LF and CRLF counts */
21 unsigned cr, lf, crlf;
23 /* These are just approximations! */
24 unsigned printable, nonprintable;
27 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
29 unsigned long i;
31 memset(stats, 0, sizeof(*stats));
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;
41 if (c == '\n') {
42 stats->lf++;
43 continue;
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++;
58 else
59 stats->printable++;
62 // If file ends with EOF then don't count this EOF as non-printable
63 if ( size >= 1 && buf[size-1] == '\032' )
64 stats->nonprintable--;
68 * The same heuristics as diff.c::mmfile_is_binary()
70 static int is_binary(unsigned long size, struct text_stat *stats)
73 if ((stats->printable >> 7) < stats->nonprintable)
74 return 1;
76 * Other heuristics? Average line length might be relevant,
77 * as might LF vs CR vs CRLF counts..
79 * NOTE! It might be normal to have a low ratio of CRLF to LF
80 * (somebody starts with a LF-only file and edits it with an editor
81 * that adds CRLF only to lines that are added..). But do we
82 * want to support CR-only? Probably not.
84 return 0;
87 static int crlf_to_git(const char *path, const char *src, size_t len,
88 struct strbuf *buf, int action)
90 struct text_stat stats;
91 char *dst;
93 if ((action == CRLF_BINARY) || !auto_crlf || !len)
94 return 0;
96 gather_stats(src, len, &stats);
97 /* No CR? Nothing to convert, regardless. */
98 if (!stats.cr)
99 return 0;
101 if (action == CRLF_GUESS) {
103 * We're currently not going to even try to convert stuff
104 * that has bare CR characters. Does anybody do that crazy
105 * stuff?
107 if (stats.cr != stats.crlf)
108 return 0;
111 * And add some heuristics for binary vs text, of course...
113 if (is_binary(len, &stats))
114 return 0;
117 /* only grow if not in place */
118 if (strbuf_avail(buf) + buf->len < len)
119 strbuf_grow(buf, len - buf->len);
120 dst = buf->buf;
121 if (action == CRLF_GUESS) {
123 * If we guessed, we already know we rejected a file with
124 * lone CR, and we can strip a CR without looking at what
125 * follow it.
127 do {
128 unsigned char c = *src++;
129 if (c != '\r')
130 *dst++ = c;
131 } while (--len);
132 } else {
133 do {
134 unsigned char c = *src++;
135 if (! (c == '\r' && (1 < len && *src == '\n')))
136 *dst++ = c;
137 } while (--len);
139 strbuf_setlen(buf, dst - buf->buf);
140 return 1;
143 static int crlf_to_worktree(const char *path, const char *src, size_t len,
144 struct strbuf *buf, int action)
146 char *to_free = NULL;
147 struct text_stat stats;
149 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
150 auto_crlf <= 0)
151 return 0;
153 if (!len)
154 return 0;
156 gather_stats(src, len, &stats);
158 /* No LF? Nothing to convert, regardless. */
159 if (!stats.lf)
160 return 0;
162 /* Was it already in CRLF format? */
163 if (stats.lf == stats.crlf)
164 return 0;
166 if (action == CRLF_GUESS) {
167 /* If we have any bare CR characters, we're not going to touch it */
168 if (stats.cr != stats.crlf)
169 return 0;
171 if (is_binary(len, &stats))
172 return 0;
175 /* are we "faking" in place editing ? */
176 if (src == buf->buf)
177 to_free = strbuf_detach(buf, NULL);
179 strbuf_grow(buf, len + stats.lf - stats.crlf);
180 for (;;) {
181 const char *nl = memchr(src, '\n', len);
182 if (!nl)
183 break;
184 if (nl > src && nl[-1] == '\r') {
185 strbuf_add(buf, src, nl + 1 - src);
186 } else {
187 strbuf_add(buf, src, nl - src);
188 strbuf_addstr(buf, "\r\n");
190 len -= nl + 1 - src;
191 src = nl + 1;
193 strbuf_add(buf, src, len);
195 free(to_free);
196 return 1;
199 struct filter_params {
200 const char *src;
201 unsigned long size;
202 const char *cmd;
205 static int filter_buffer(int fd, void *data)
208 * Spawn cmd and feed the buffer contents through its stdin.
210 struct child_process child_process;
211 struct filter_params *params = (struct filter_params *)data;
212 int write_err, status;
213 const char *argv[] = { "sh", "-c", params->cmd, NULL };
215 memset(&child_process, 0, sizeof(child_process));
216 child_process.argv = argv;
217 child_process.in = -1;
218 child_process.out = fd;
220 if (start_command(&child_process))
221 return error("cannot fork to run external filter %s", params->cmd);
223 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
224 if (close(child_process.in))
225 write_err = 1;
226 if (write_err)
227 error("cannot feed the input to external filter %s", params->cmd);
229 status = finish_command(&child_process);
230 if (status)
231 error("external filter %s failed %d", params->cmd, -status);
232 return (write_err || status);
235 static int apply_filter(const char *path, const char *src, size_t len,
236 struct strbuf *dst, const char *cmd)
239 * Create a pipeline to have the command filter the buffer's
240 * contents.
242 * (child --> cmd) --> us
244 int ret = 1;
245 struct strbuf nbuf;
246 struct async async;
247 struct filter_params params;
249 if (!cmd)
250 return 0;
252 memset(&async, 0, sizeof(async));
253 async.proc = filter_buffer;
254 async.data = &params;
255 params.src = src;
256 params.size = len;
257 params.cmd = cmd;
259 fflush(NULL);
260 if (start_async(&async))
261 return 0; /* error was already reported */
263 strbuf_init(&nbuf, 0);
264 if (strbuf_read(&nbuf, async.out, len) < 0) {
265 error("read from external filter %s failed", cmd);
266 ret = 0;
268 if (close(async.out)) {
269 error("read from external filter %s failed", cmd);
270 ret = 0;
272 if (finish_async(&async)) {
273 error("external filter %s failed", cmd);
274 ret = 0;
277 if (ret) {
278 strbuf_swap(dst, &nbuf);
280 strbuf_release(&nbuf);
281 return ret;
284 static struct convert_driver {
285 const char *name;
286 struct convert_driver *next;
287 char *smudge;
288 char *clean;
289 } *user_convert, **user_convert_tail;
291 static int read_convert_config(const char *var, const char *value)
293 const char *ep, *name;
294 int namelen;
295 struct convert_driver *drv;
298 * External conversion drivers are configured using
299 * "filter.<name>.variable".
301 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
302 return 0;
303 name = var + 7;
304 namelen = ep - name;
305 for (drv = user_convert; drv; drv = drv->next)
306 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
307 break;
308 if (!drv) {
309 drv = xcalloc(1, sizeof(struct convert_driver));
310 drv->name = xmemdupz(name, namelen);
311 *user_convert_tail = drv;
312 user_convert_tail = &(drv->next);
315 ep++;
318 * filter.<name>.smudge and filter.<name>.clean specifies
319 * the command line:
321 * command-line
323 * The command-line will not be interpolated in any way.
326 if (!strcmp("smudge", ep)) {
327 if (!value)
328 return error("%s: lacks value", var);
329 drv->smudge = strdup(value);
330 return 0;
333 if (!strcmp("clean", ep)) {
334 if (!value)
335 return error("%s: lacks value", var);
336 drv->clean = strdup(value);
337 return 0;
339 return 0;
342 static void setup_convert_check(struct git_attr_check *check)
344 static struct git_attr *attr_crlf;
345 static struct git_attr *attr_ident;
346 static struct git_attr *attr_filter;
348 if (!attr_crlf) {
349 attr_crlf = git_attr("crlf", 4);
350 attr_ident = git_attr("ident", 5);
351 attr_filter = git_attr("filter", 6);
352 user_convert_tail = &user_convert;
353 git_config(read_convert_config);
355 check[0].attr = attr_crlf;
356 check[1].attr = attr_ident;
357 check[2].attr = attr_filter;
360 static int count_ident(const char *cp, unsigned long size)
363 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
365 int cnt = 0;
366 char ch;
368 while (size) {
369 ch = *cp++;
370 size--;
371 if (ch != '$')
372 continue;
373 if (size < 3)
374 break;
375 if (memcmp("Id", cp, 2))
376 continue;
377 ch = cp[2];
378 cp += 3;
379 size -= 3;
380 if (ch == '$')
381 cnt++; /* $Id$ */
382 if (ch != ':')
383 continue;
386 * "$Id: ... "; scan up to the closing dollar sign and discard.
388 while (size) {
389 ch = *cp++;
390 size--;
391 if (ch == '$') {
392 cnt++;
393 break;
397 return cnt;
400 static int ident_to_git(const char *path, const char *src, size_t len,
401 struct strbuf *buf, int ident)
403 char *dst, *dollar;
405 if (!ident || !count_ident(src, len))
406 return 0;
408 /* only grow if not in place */
409 if (strbuf_avail(buf) + buf->len < len)
410 strbuf_grow(buf, len - buf->len);
411 dst = buf->buf;
412 for (;;) {
413 dollar = memchr(src, '$', len);
414 if (!dollar)
415 break;
416 memcpy(dst, src, dollar + 1 - src);
417 dst += dollar + 1 - src;
418 len -= dollar + 1 - src;
419 src = dollar + 1;
421 if (len > 3 && !memcmp(src, "Id:", 3)) {
422 dollar = memchr(src + 3, '$', len - 3);
423 if (!dollar)
424 break;
425 memcpy(dst, "Id$", 3);
426 dst += 3;
427 len -= dollar + 1 - src;
428 src = dollar + 1;
431 memcpy(dst, src, len);
432 strbuf_setlen(buf, dst + len - buf->buf);
433 return 1;
436 static int ident_to_worktree(const char *path, const char *src, size_t len,
437 struct strbuf *buf, int ident)
439 unsigned char sha1[20];
440 char *to_free = NULL, *dollar;
441 int cnt;
443 if (!ident)
444 return 0;
446 cnt = count_ident(src, len);
447 if (!cnt)
448 return 0;
450 /* are we "faking" in place editing ? */
451 if (src == buf->buf)
452 to_free = strbuf_detach(buf, NULL);
453 hash_sha1_file(src, len, "blob", sha1);
455 strbuf_grow(buf, len + cnt * 43);
456 for (;;) {
457 /* step 1: run to the next '$' */
458 dollar = memchr(src, '$', len);
459 if (!dollar)
460 break;
461 strbuf_add(buf, src, dollar + 1 - src);
462 len -= dollar + 1 - src;
463 src = dollar + 1;
465 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
466 if (len < 3 || memcmp("Id", src, 2))
467 continue;
469 /* step 3: skip over Id$ or Id:xxxxx$ */
470 if (src[2] == '$') {
471 src += 3;
472 len -= 3;
473 } else if (src[2] == ':') {
475 * It's possible that an expanded Id has crept its way into the
476 * repository, we cope with that by stripping the expansion out
478 dollar = memchr(src + 3, '$', len - 3);
479 if (!dollar) {
480 /* incomplete keyword, no more '$', so just quit the loop */
481 break;
484 len -= dollar + 1 - src;
485 src = dollar + 1;
486 } else {
487 /* it wasn't a "Id$" or "Id:xxxx$" */
488 continue;
491 /* step 4: substitute */
492 strbuf_addstr(buf, "Id: ");
493 strbuf_add(buf, sha1_to_hex(sha1), 40);
494 strbuf_addstr(buf, " $");
496 strbuf_add(buf, src, len);
498 free(to_free);
499 return 1;
502 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
504 const char *value = check->value;
506 if (ATTR_TRUE(value))
507 return CRLF_TEXT;
508 else if (ATTR_FALSE(value))
509 return CRLF_BINARY;
510 else if (ATTR_UNSET(value))
512 else if (!strcmp(value, "input"))
513 return CRLF_INPUT;
514 return CRLF_GUESS;
517 static struct convert_driver *git_path_check_convert(const char *path,
518 struct git_attr_check *check)
520 const char *value = check->value;
521 struct convert_driver *drv;
523 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
524 return NULL;
525 for (drv = user_convert; drv; drv = drv->next)
526 if (!strcmp(value, drv->name))
527 return drv;
528 return NULL;
531 static int git_path_check_ident(const char *path, struct git_attr_check *check)
533 const char *value = check->value;
535 return !!ATTR_TRUE(value);
538 int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
540 struct git_attr_check check[3];
541 int crlf = CRLF_GUESS;
542 int ident = 0, ret = 0;
543 char *filter = NULL;
545 setup_convert_check(check);
546 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
547 struct convert_driver *drv;
548 crlf = git_path_check_crlf(path, check + 0);
549 ident = git_path_check_ident(path, check + 1);
550 drv = git_path_check_convert(path, check + 2);
551 if (drv && drv->clean)
552 filter = drv->clean;
555 ret |= apply_filter(path, src, len, dst, filter);
556 if (ret) {
557 src = dst->buf;
558 len = dst->len;
560 ret |= crlf_to_git(path, src, len, dst, crlf);
561 if (ret) {
562 src = dst->buf;
563 len = dst->len;
565 return ret | ident_to_git(path, src, len, dst, ident);
568 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
570 struct git_attr_check check[3];
571 int crlf = CRLF_GUESS;
572 int ident = 0, ret = 0;
573 char *filter = NULL;
575 setup_convert_check(check);
576 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
577 struct convert_driver *drv;
578 crlf = git_path_check_crlf(path, check + 0);
579 ident = git_path_check_ident(path, check + 1);
580 drv = git_path_check_convert(path, check + 2);
581 if (drv && drv->smudge)
582 filter = drv->smudge;
585 ret |= ident_to_worktree(path, src, len, dst, ident);
586 if (ret) {
587 src = dst->buf;
588 len = dst->len;
590 ret |= crlf_to_worktree(path, src, len, dst, crlf);
591 if (ret) {
592 src = dst->buf;
593 len = dst->len;
595 return ret | apply_filter(path, src, len, dst, filter);