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 struct filter_params {
201 static int filter_buffer(int fd, void *data)
204 * Spawn cmd and feed the buffer contents through its stdin.
206 struct child_process child_process;
207 struct filter_params *params = (struct filter_params *)data;
208 int write_err, status;
209 const char *argv[] = { "sh", "-c", params->cmd, NULL };
211 memset(&child_process, 0, sizeof(child_process));
212 child_process.argv = argv;
213 child_process.in = -1;
214 child_process.out = fd;
216 if (start_command(&child_process))
217 return error("cannot fork to run external filter %s", params->cmd);
219 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
220 if (close(child_process.in))
223 error("cannot feed the input to external filter %s", params->cmd);
225 status = finish_command(&child_process);
227 error("external filter %s failed %d", params->cmd, -status);
228 return (write_err || status);
231 static int apply_filter(const char *path, const char *src, size_t len,
232 struct strbuf *dst, const char *cmd)
235 * Create a pipeline to have the command filter the buffer's
238 * (child --> cmd) --> us
243 struct filter_params params;
248 memset(&async, 0, sizeof(async));
249 async.proc = filter_buffer;
250 async.data = ¶ms;
256 if (start_async(&async))
257 return 0; /* error was already reported */
259 strbuf_init(&nbuf, 0);
260 if (strbuf_read(&nbuf, async.out, len) < 0) {
261 error("read from external filter %s failed", cmd);
264 if (close(async.out)) {
265 error("read from external filter %s failed", cmd);
268 if (finish_async(&async)) {
269 error("external filter %s failed", cmd);
274 strbuf_swap(dst, &nbuf);
276 strbuf_release(&nbuf);
280 static struct convert_driver {
282 struct convert_driver *next;
285 } *user_convert, **user_convert_tail;
287 static int read_convert_config(const char *var, const char *value)
289 const char *ep, *name;
291 struct convert_driver *drv;
294 * External conversion drivers are configured using
295 * "filter.<name>.variable".
297 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
301 for (drv = user_convert; drv; drv = drv->next)
302 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
305 drv = xcalloc(1, sizeof(struct convert_driver));
306 drv->name = xmemdupz(name, namelen);
307 *user_convert_tail = drv;
308 user_convert_tail = &(drv->next);
314 * filter.<name>.smudge and filter.<name>.clean specifies
319 * The command-line will not be interpolated in any way.
322 if (!strcmp("smudge", ep)) {
324 return error("%s: lacks value", var);
325 drv->smudge = strdup(value);
329 if (!strcmp("clean", ep)) {
331 return error("%s: lacks value", var);
332 drv->clean = strdup(value);
338 static void setup_convert_check(struct git_attr_check *check)
340 static struct git_attr *attr_crlf;
341 static struct git_attr *attr_ident;
342 static struct git_attr *attr_filter;
345 attr_crlf = git_attr("crlf", 4);
346 attr_ident = git_attr("ident", 5);
347 attr_filter = git_attr("filter", 6);
348 user_convert_tail = &user_convert;
349 git_config(read_convert_config);
351 check[0].attr = attr_crlf;
352 check[1].attr = attr_ident;
353 check[2].attr = attr_filter;
356 static int count_ident(const char *cp, unsigned long size)
359 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
371 if (memcmp("Id", cp, 2))
382 * "$Id: ... "; scan up to the closing dollar sign and discard.
396 static int ident_to_git(const char *path, const char *src, size_t len,
397 struct strbuf *buf, int ident)
401 if (!ident || !count_ident(src, len))
404 /* only grow if not in place */
405 if (strbuf_avail(buf) + buf->len < len)
406 strbuf_grow(buf, len - buf->len);
409 dollar = memchr(src, '$', len);
412 memcpy(dst, src, dollar + 1 - src);
413 dst += dollar + 1 - src;
414 len -= dollar + 1 - src;
417 if (len > 3 && !memcmp(src, "Id:", 3)) {
418 dollar = memchr(src + 3, '$', len - 3);
421 memcpy(dst, "Id$", 3);
423 len -= dollar + 1 - src;
427 memcpy(dst, src, len);
428 strbuf_setlen(buf, dst + len - buf->buf);
432 static int ident_to_worktree(const char *path, const char *src, size_t len,
433 struct strbuf *buf, int ident)
435 unsigned char sha1[20];
436 char *to_free = NULL, *dollar;
442 cnt = count_ident(src, len);
446 /* are we "faking" in place editing ? */
448 to_free = strbuf_detach(buf, NULL);
449 hash_sha1_file(src, len, "blob", sha1);
451 strbuf_grow(buf, len + cnt * 43);
453 /* step 1: run to the next '$' */
454 dollar = memchr(src, '$', len);
457 strbuf_add(buf, src, dollar + 1 - src);
458 len -= dollar + 1 - src;
461 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
462 if (len < 3 || memcmp("Id", src, 2))
465 /* step 3: skip over Id$ or Id:xxxxx$ */
469 } else if (src[2] == ':') {
471 * It's possible that an expanded Id has crept its way into the
472 * repository, we cope with that by stripping the expansion out
474 dollar = memchr(src + 3, '$', len - 3);
476 /* incomplete keyword, no more '$', so just quit the loop */
480 len -= dollar + 1 - src;
483 /* it wasn't a "Id$" or "Id:xxxx$" */
487 /* step 4: substitute */
488 strbuf_addstr(buf, "Id: ");
489 strbuf_add(buf, sha1_to_hex(sha1), 40);
490 strbuf_addstr(buf, " $");
492 strbuf_add(buf, src, len);
498 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
500 const char *value = check->value;
502 if (ATTR_TRUE(value))
504 else if (ATTR_FALSE(value))
506 else if (ATTR_UNSET(value))
508 else if (!strcmp(value, "input"))
513 static struct convert_driver *git_path_check_convert(const char *path,
514 struct git_attr_check *check)
516 const char *value = check->value;
517 struct convert_driver *drv;
519 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
521 for (drv = user_convert; drv; drv = drv->next)
522 if (!strcmp(value, drv->name))
527 static int git_path_check_ident(const char *path, struct git_attr_check *check)
529 const char *value = check->value;
531 return !!ATTR_TRUE(value);
534 int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
536 struct git_attr_check check[3];
537 int crlf = CRLF_GUESS;
538 int ident = 0, ret = 0;
541 setup_convert_check(check);
542 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
543 struct convert_driver *drv;
544 crlf = git_path_check_crlf(path, check + 0);
545 ident = git_path_check_ident(path, check + 1);
546 drv = git_path_check_convert(path, check + 2);
547 if (drv && drv->clean)
551 ret |= apply_filter(path, src, len, dst, filter);
556 ret |= crlf_to_git(path, src, len, dst, crlf);
561 return ret | ident_to_git(path, src, len, dst, ident);
564 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
566 struct git_attr_check check[3];
567 int crlf = CRLF_GUESS;
568 int ident = 0, ret = 0;
571 setup_convert_check(check);
572 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
573 struct convert_driver *drv;
574 crlf = git_path_check_crlf(path, check + 0);
575 ident = git_path_check_ident(path, check + 1);
576 drv = git_path_check_convert(path, check + 2);
577 if (drv && drv->smudge)
578 filter = drv->smudge;
581 ret |= ident_to_worktree(path, src, len, dst, ident);
586 ret |= crlf_to_worktree(path, src, len, dst, crlf);
591 return ret | apply_filter(path, src, len, dst, filter);