3 #include "run-command.h"
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)
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)
31 memset(stats, 0, sizeof(*stats));
33 for (i = 0; i < size; i++) {
34 unsigned char c = buf[i];
37 if (i+1 < size && buf[i+1] == '\n')
47 stats->nonprintable++;
50 /* BS, HT, ESC and FF */
51 case '\b': case '\t': case '\033': case '\014':
55 stats->nonprintable++;
64 * The same heuristics as diff.c::mmfile_is_binary()
66 static int is_binary(unsigned long size, struct text_stat *stats)
69 if ((stats->printable >> 7) < stats->nonprintable)
72 * Other heuristics? Average line length might be relevant,
73 * as might LF vs CR vs CRLF counts..
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.
83 static int crlf_to_git(const char *path, const char *src, size_t len,
84 struct strbuf *buf, int action)
86 struct text_stat stats;
89 if ((action == CRLF_BINARY) || !auto_crlf || !len)
92 gather_stats(src, len, &stats);
93 /* No CR? Nothing to convert, regardless. */
97 if (action == CRLF_GUESS) {
99 * We're currently not going to even try to convert stuff
100 * that has bare CR characters. Does anybody do that crazy
103 if (stats.cr != stats.crlf)
107 * And add some heuristics for binary vs text, of course...
109 if (is_binary(len, &stats))
113 strbuf_grow(buf, len);
115 if (action == CRLF_GUESS) {
117 * If we guessed, we already know we rejected a file with
118 * lone CR, and we can strip a CR without looking at what
122 unsigned char c = *src++;
128 unsigned char c = *src++;
129 if (! (c == '\r' && (1 < len && *src == '\n')))
133 strbuf_setlen(buf, dst - buf->buf);
137 static int crlf_to_worktree(const char *path, const char *src, size_t len,
138 struct strbuf *buf, int action)
140 char *to_free = NULL;
141 struct text_stat stats;
143 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
150 gather_stats(src, len, &stats);
152 /* No LF? Nothing to convert, regardless. */
156 /* Was it already in CRLF format? */
157 if (stats.lf == stats.crlf)
160 if (action == CRLF_GUESS) {
161 /* If we have any bare CR characters, we're not going to touch it */
162 if (stats.cr != stats.crlf)
165 if (is_binary(len, &stats))
169 /* are we "faking" in place editing ? */
171 to_free = strbuf_detach(buf);
173 strbuf_grow(buf, len + stats.lf - stats.crlf);
175 const char *nl = memchr(src, '\n', len);
178 if (nl > src && nl[-1] == '\r') {
179 strbuf_add(buf, src, nl + 1 - src);
181 strbuf_add(buf, src, nl - src);
182 strbuf_addstr(buf, "\r\n");
187 strbuf_add(buf, src, len);
193 static int filter_buffer(const char *path, const char *src,
194 unsigned long size, const char *cmd)
197 * Spawn cmd and feed the buffer contents through its stdin.
199 struct child_process child_process;
201 int write_err, status;
203 memset(&child_process, 0, sizeof(child_process));
205 if (pipe(pipe_feed) < 0) {
206 error("cannot create pipe to run external filter %s", cmd);
210 child_process.pid = fork();
211 if (child_process.pid < 0) {
212 error("cannot fork to run external filter %s", cmd);
217 if (!child_process.pid) {
218 dup2(pipe_feed[0], 0);
221 execlp("sh", "sh", "-c", cmd, NULL);
226 write_err = (write_in_full(pipe_feed[1], src, size) < 0);
227 if (close(pipe_feed[1]))
230 error("cannot feed the input to external filter %s", cmd);
232 status = finish_command(&child_process);
234 error("external filter %s failed %d", cmd, -status);
235 return (write_err || status);
238 static int apply_filter(const char *path, const char *src, size_t len,
239 struct strbuf *dst, const char *cmd)
242 * Create a pipeline to have the command filter the buffer's
245 * (child --> cmd) --> us
249 struct child_process child_process;
255 memset(&child_process, 0, sizeof(child_process));
257 if (pipe(pipe_feed) < 0) {
258 error("cannot create pipe to run external filter %s", cmd);
263 child_process.pid = fork();
264 if (child_process.pid < 0) {
265 error("cannot fork to run external filter %s", cmd);
270 if (!child_process.pid) {
271 dup2(pipe_feed[1], 1);
274 exit(filter_buffer(path, src, len, cmd));
278 strbuf_init(&nbuf, 0);
279 if (strbuf_read(&nbuf, pipe_feed[0], len) < 0) {
280 error("read from external filter %s failed", cmd);
283 if (close(pipe_feed[0])) {
284 ret = error("read from external filter %s failed", cmd);
287 status = finish_command(&child_process);
289 ret = error("external filter %s failed %d", cmd, -status);
296 strbuf_release(&nbuf);
301 static struct convert_driver {
303 struct convert_driver *next;
306 } *user_convert, **user_convert_tail;
308 static int read_convert_config(const char *var, const char *value)
310 const char *ep, *name;
312 struct convert_driver *drv;
315 * External conversion drivers are configured using
316 * "filter.<name>.variable".
318 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
322 for (drv = user_convert; drv; drv = drv->next)
323 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
326 drv = xcalloc(1, sizeof(struct convert_driver));
327 drv->name = xmemdupz(name, namelen);
328 *user_convert_tail = drv;
329 user_convert_tail = &(drv->next);
335 * filter.<name>.smudge and filter.<name>.clean specifies
340 * The command-line will not be interpolated in any way.
343 if (!strcmp("smudge", ep)) {
345 return error("%s: lacks value", var);
346 drv->smudge = strdup(value);
350 if (!strcmp("clean", ep)) {
352 return error("%s: lacks value", var);
353 drv->clean = strdup(value);
359 static void setup_convert_check(struct git_attr_check *check)
361 static struct git_attr *attr_crlf;
362 static struct git_attr *attr_ident;
363 static struct git_attr *attr_filter;
366 attr_crlf = git_attr("crlf", 4);
367 attr_ident = git_attr("ident", 5);
368 attr_filter = git_attr("filter", 6);
369 user_convert_tail = &user_convert;
370 git_config(read_convert_config);
372 check[0].attr = attr_crlf;
373 check[1].attr = attr_ident;
374 check[2].attr = attr_filter;
377 static int count_ident(const char *cp, unsigned long size)
380 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
392 if (memcmp("Id", cp, 2))
403 * "$Id: ... "; scan up to the closing dollar sign and discard.
417 static int ident_to_git(const char *path, const char *src, size_t len,
418 struct strbuf *buf, int ident)
422 if (!ident || !count_ident(src, len))
425 strbuf_grow(buf, len);
428 dollar = memchr(src, '$', len);
431 memcpy(dst, src, dollar + 1 - src);
432 dst += dollar + 1 - src;
433 len -= dollar + 1 - src;
436 if (len > 3 && !memcmp(src, "Id:", 3)) {
437 dollar = memchr(src + 3, '$', len - 3);
440 memcpy(dst, "Id$", 3);
442 len -= dollar + 1 - src;
446 memcpy(dst, src, len);
447 strbuf_setlen(buf, dst + len - buf->buf);
451 static int ident_to_worktree(const char *path, const char *src, size_t len,
452 struct strbuf *buf, int ident)
454 unsigned char sha1[20];
455 char *to_free = NULL, *dollar;
461 cnt = count_ident(src, len);
465 /* are we "faking" in place editing ? */
467 to_free = strbuf_detach(buf);
468 hash_sha1_file(src, len, "blob", sha1);
470 strbuf_grow(buf, len + cnt * 43);
472 /* step 1: run to the next '$' */
473 dollar = memchr(src, '$', len);
476 strbuf_add(buf, src, dollar + 1 - src);
477 len -= dollar + 1 - src;
480 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
481 if (len < 3 || memcmp("Id", src, 2))
484 /* step 3: skip over Id$ or Id:xxxxx$ */
488 } else if (src[2] == ':') {
490 * It's possible that an expanded Id has crept its way into the
491 * repository, we cope with that by stripping the expansion out
493 dollar = memchr(src + 3, '$', len - 3);
495 /* incomplete keyword, no more '$', so just quit the loop */
499 len -= dollar + 1 - src;
502 /* it wasn't a "Id$" or "Id:xxxx$" */
506 /* step 4: substitute */
507 strbuf_addstr(buf, "Id: ");
508 strbuf_add(buf, sha1_to_hex(sha1), 40);
509 strbuf_addstr(buf, " $");
511 strbuf_add(buf, src, len);
517 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
519 const char *value = check->value;
521 if (ATTR_TRUE(value))
523 else if (ATTR_FALSE(value))
525 else if (ATTR_UNSET(value))
527 else if (!strcmp(value, "input"))
532 static struct convert_driver *git_path_check_convert(const char *path,
533 struct git_attr_check *check)
535 const char *value = check->value;
536 struct convert_driver *drv;
538 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
540 for (drv = user_convert; drv; drv = drv->next)
541 if (!strcmp(value, drv->name))
546 static int git_path_check_ident(const char *path, struct git_attr_check *check)
548 const char *value = check->value;
550 return !!ATTR_TRUE(value);
553 int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
555 struct git_attr_check check[3];
556 int crlf = CRLF_GUESS;
557 int ident = 0, ret = 0;
560 setup_convert_check(check);
561 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
562 struct convert_driver *drv;
563 crlf = git_path_check_crlf(path, check + 0);
564 ident = git_path_check_ident(path, check + 1);
565 drv = git_path_check_convert(path, check + 2);
566 if (drv && drv->clean)
570 ret |= apply_filter(path, src, len, dst, filter);
575 ret |= crlf_to_git(path, src, len, dst, crlf);
580 return ret | ident_to_git(path, src, len, dst, ident);
583 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
585 struct git_attr_check check[3];
586 int crlf = CRLF_GUESS;
587 int ident = 0, ret = 0;
590 setup_convert_check(check);
591 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
592 struct convert_driver *drv;
593 crlf = git_path_check_crlf(path, check + 0);
594 ident = git_path_check_ident(path, check + 1);
595 drv = git_path_check_convert(path, check + 2);
596 if (drv && drv->smudge)
597 filter = drv->smudge;
600 ret |= ident_to_worktree(path, src, len, dst, ident);
605 ret |= crlf_to_worktree(path, src, len, dst, crlf);
610 return ret | apply_filter(path, src, len, dst, filter);