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 /* only grow if not in place */
114 if (strbuf_avail(buf) + buf->len < len)
115 strbuf_grow(buf, len - buf->len);
117 if (action == CRLF_GUESS) {
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
124 unsigned char c = *src++;
130 unsigned char c = *src++;
131 if (! (c == '\r' && (1 < len && *src == '\n')))
135 strbuf_setlen(buf, dst - buf->buf);
139 static int crlf_to_worktree(const char *path, const char *src, size_t len,
140 struct strbuf *buf, int action)
142 char *to_free = NULL;
143 struct text_stat stats;
145 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
152 gather_stats(src, len, &stats);
154 /* No LF? Nothing to convert, regardless. */
158 /* Was it already in CRLF format? */
159 if (stats.lf == stats.crlf)
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)
167 if (is_binary(len, &stats))
171 /* are we "faking" in place editing ? */
173 to_free = strbuf_detach(buf, NULL);
175 strbuf_grow(buf, len + stats.lf - stats.crlf);
177 const char *nl = memchr(src, '\n', len);
180 if (nl > src && nl[-1] == '\r') {
181 strbuf_add(buf, src, nl + 1 - src);
183 strbuf_add(buf, src, nl - src);
184 strbuf_addstr(buf, "\r\n");
189 strbuf_add(buf, src, len);
195 static int filter_buffer(const char *path, const char *src,
196 unsigned long size, const char *cmd)
199 * Spawn cmd and feed the buffer contents through its stdin.
201 struct child_process child_process;
203 int write_err, status;
205 memset(&child_process, 0, sizeof(child_process));
207 if (pipe(pipe_feed) < 0) {
208 error("cannot create pipe to run external filter %s", cmd);
212 child_process.pid = fork();
213 if (child_process.pid < 0) {
214 error("cannot fork to run external filter %s", cmd);
219 if (!child_process.pid) {
220 dup2(pipe_feed[0], 0);
223 execlp("sh", "sh", "-c", cmd, NULL);
228 write_err = (write_in_full(pipe_feed[1], src, size) < 0);
229 if (close(pipe_feed[1]))
232 error("cannot feed the input to external filter %s", cmd);
234 status = finish_command(&child_process);
236 error("external filter %s failed %d", cmd, -status);
237 return (write_err || status);
240 static int apply_filter(const char *path, const char *src, size_t len,
241 struct strbuf *dst, const char *cmd)
244 * Create a pipeline to have the command filter the buffer's
247 * (child --> cmd) --> us
251 struct child_process child_process;
257 memset(&child_process, 0, sizeof(child_process));
259 if (pipe(pipe_feed) < 0) {
260 error("cannot create pipe to run external filter %s", cmd);
265 child_process.pid = fork();
266 if (child_process.pid < 0) {
267 error("cannot fork to run external filter %s", cmd);
272 if (!child_process.pid) {
273 dup2(pipe_feed[1], 1);
276 exit(filter_buffer(path, src, len, cmd));
280 strbuf_init(&nbuf, 0);
281 if (strbuf_read(&nbuf, pipe_feed[0], len) < 0) {
282 error("read from external filter %s failed", cmd);
285 if (close(pipe_feed[0])) {
286 error("read from external filter %s failed", cmd);
289 status = finish_command(&child_process);
291 error("external filter %s failed %d", cmd, -status);
296 strbuf_swap(dst, &nbuf);
298 strbuf_release(&nbuf);
302 static struct convert_driver {
304 struct convert_driver *next;
307 } *user_convert, **user_convert_tail;
309 static int read_convert_config(const char *var, const char *value)
311 const char *ep, *name;
313 struct convert_driver *drv;
316 * External conversion drivers are configured using
317 * "filter.<name>.variable".
319 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
323 for (drv = user_convert; drv; drv = drv->next)
324 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
327 drv = xcalloc(1, sizeof(struct convert_driver));
328 drv->name = xmemdupz(name, namelen);
329 *user_convert_tail = drv;
330 user_convert_tail = &(drv->next);
336 * filter.<name>.smudge and filter.<name>.clean specifies
341 * The command-line will not be interpolated in any way.
344 if (!strcmp("smudge", ep)) {
346 return error("%s: lacks value", var);
347 drv->smudge = strdup(value);
351 if (!strcmp("clean", ep)) {
353 return error("%s: lacks value", var);
354 drv->clean = strdup(value);
360 static void setup_convert_check(struct git_attr_check *check)
362 static struct git_attr *attr_crlf;
363 static struct git_attr *attr_ident;
364 static struct git_attr *attr_filter;
367 attr_crlf = git_attr("crlf", 4);
368 attr_ident = git_attr("ident", 5);
369 attr_filter = git_attr("filter", 6);
370 user_convert_tail = &user_convert;
371 git_config(read_convert_config);
373 check[0].attr = attr_crlf;
374 check[1].attr = attr_ident;
375 check[2].attr = attr_filter;
378 static int count_ident(const char *cp, unsigned long size)
381 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
393 if (memcmp("Id", cp, 2))
404 * "$Id: ... "; scan up to the closing dollar sign and discard.
418 static int ident_to_git(const char *path, const char *src, size_t len,
419 struct strbuf *buf, int ident)
423 if (!ident || !count_ident(src, len))
426 /* only grow if not in place */
427 if (strbuf_avail(buf) + buf->len < len)
428 strbuf_grow(buf, len - buf->len);
431 dollar = memchr(src, '$', len);
434 memcpy(dst, src, dollar + 1 - src);
435 dst += dollar + 1 - src;
436 len -= dollar + 1 - src;
439 if (len > 3 && !memcmp(src, "Id:", 3)) {
440 dollar = memchr(src + 3, '$', len - 3);
443 memcpy(dst, "Id$", 3);
445 len -= dollar + 1 - src;
449 memcpy(dst, src, len);
450 strbuf_setlen(buf, dst + len - buf->buf);
454 static int ident_to_worktree(const char *path, const char *src, size_t len,
455 struct strbuf *buf, int ident)
457 unsigned char sha1[20];
458 char *to_free = NULL, *dollar;
464 cnt = count_ident(src, len);
468 /* are we "faking" in place editing ? */
470 to_free = strbuf_detach(buf, NULL);
471 hash_sha1_file(src, len, "blob", sha1);
473 strbuf_grow(buf, len + cnt * 43);
475 /* step 1: run to the next '$' */
476 dollar = memchr(src, '$', len);
479 strbuf_add(buf, src, dollar + 1 - src);
480 len -= dollar + 1 - src;
483 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
484 if (len < 3 || memcmp("Id", src, 2))
487 /* step 3: skip over Id$ or Id:xxxxx$ */
491 } else if (src[2] == ':') {
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
496 dollar = memchr(src + 3, '$', len - 3);
498 /* incomplete keyword, no more '$', so just quit the loop */
502 len -= dollar + 1 - src;
505 /* it wasn't a "Id$" or "Id:xxxx$" */
509 /* step 4: substitute */
510 strbuf_addstr(buf, "Id: ");
511 strbuf_add(buf, sha1_to_hex(sha1), 40);
512 strbuf_addstr(buf, " $");
514 strbuf_add(buf, src, len);
520 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
522 const char *value = check->value;
524 if (ATTR_TRUE(value))
526 else if (ATTR_FALSE(value))
528 else if (ATTR_UNSET(value))
530 else if (!strcmp(value, "input"))
535 static struct convert_driver *git_path_check_convert(const char *path,
536 struct git_attr_check *check)
538 const char *value = check->value;
539 struct convert_driver *drv;
541 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
543 for (drv = user_convert; drv; drv = drv->next)
544 if (!strcmp(value, drv->name))
549 static int git_path_check_ident(const char *path, struct git_attr_check *check)
551 const char *value = check->value;
553 return !!ATTR_TRUE(value);
556 int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
558 struct git_attr_check check[3];
559 int crlf = CRLF_GUESS;
560 int ident = 0, ret = 0;
563 setup_convert_check(check);
564 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
565 struct convert_driver *drv;
566 crlf = git_path_check_crlf(path, check + 0);
567 ident = git_path_check_ident(path, check + 1);
568 drv = git_path_check_convert(path, check + 2);
569 if (drv && drv->clean)
573 ret |= apply_filter(path, src, len, dst, filter);
578 ret |= crlf_to_git(path, src, len, dst, crlf);
583 return ret | ident_to_git(path, src, len, dst, ident);
586 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
588 struct git_attr_check check[3];
589 int crlf = CRLF_GUESS;
590 int ident = 0, ret = 0;
593 setup_convert_check(check);
594 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
595 struct convert_driver *drv;
596 crlf = git_path_check_crlf(path, check + 0);
597 ident = git_path_check_ident(path, check + 1);
598 drv = git_path_check_convert(path, check + 2);
599 if (drv && drv->smudge)
600 filter = drv->smudge;
603 ret |= ident_to_worktree(path, src, len, dst, ident);
608 ret |= crlf_to_worktree(path, src, len, dst, crlf);
613 return ret | apply_filter(path, src, len, dst, filter);