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 /* NUL, CR, LF and CRLF counts */
21 unsigned nul, 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':
58 stats->nonprintable++;
67 * The same heuristics as diff.c::mmfile_is_binary()
69 static int is_binary(unsigned long size, struct text_stat *stats)
74 if ((stats->printable >> 7) < stats->nonprintable)
77 * Other heuristics? Average line length might be relevant,
78 * as might LF vs CR vs CRLF counts..
80 * NOTE! It might be normal to have a low ratio of CRLF to LF
81 * (somebody starts with a LF-only file and edits it with an editor
82 * that adds CRLF only to lines that are added..). But do we
83 * want to support CR-only? Probably not.
88 static void check_safe_crlf(const char *path, int action,
89 struct text_stat *stats, enum safe_crlf checksafe)
94 if (action == CRLF_INPUT || auto_crlf <= 0) {
96 * CRLFs would not be restored by checkout:
97 * check if we'd remove CRLFs
100 if (checksafe == SAFE_CRLF_WARN)
101 warning("CRLF will be replaced by LF in %s.", path);
102 else /* i.e. SAFE_CRLF_FAIL */
103 die("CRLF would be replaced by LF in %s.", path);
105 } else if (auto_crlf > 0) {
107 * CRLFs would be added by checkout:
108 * check if we have "naked" LFs
110 if (stats->lf != stats->crlf) {
111 if (checksafe == SAFE_CRLF_WARN)
112 warning("LF will be replaced by CRLF in %s", path);
113 else /* i.e. SAFE_CRLF_FAIL */
114 die("LF would be replaced by CRLF in %s", path);
119 static int crlf_to_git(const char *path, const char *src, size_t len,
120 struct strbuf *buf, int action, enum safe_crlf checksafe)
122 struct text_stat stats;
125 if ((action == CRLF_BINARY) || !auto_crlf || !len)
128 gather_stats(src, len, &stats);
130 if (action == CRLF_GUESS) {
132 * We're currently not going to even try to convert stuff
133 * that has bare CR characters. Does anybody do that crazy
136 if (stats.cr != stats.crlf)
140 * And add some heuristics for binary vs text, of course...
142 if (is_binary(len, &stats))
146 check_safe_crlf(path, action, &stats, checksafe);
148 /* Optimization: No CR? Nothing to convert, regardless. */
152 /* only grow if not in place */
153 if (strbuf_avail(buf) + buf->len < len)
154 strbuf_grow(buf, len - buf->len);
156 if (action == CRLF_GUESS) {
158 * If we guessed, we already know we rejected a file with
159 * lone CR, and we can strip a CR without looking at what
163 unsigned char c = *src++;
169 unsigned char c = *src++;
170 if (! (c == '\r' && (1 < len && *src == '\n')))
174 strbuf_setlen(buf, dst - buf->buf);
178 static int crlf_to_worktree(const char *path, const char *src, size_t len,
179 struct strbuf *buf, int action)
181 char *to_free = NULL;
182 struct text_stat stats;
184 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
191 gather_stats(src, len, &stats);
193 /* No LF? Nothing to convert, regardless. */
197 /* Was it already in CRLF format? */
198 if (stats.lf == stats.crlf)
201 if (action == CRLF_GUESS) {
202 /* If we have any bare CR characters, we're not going to touch it */
203 if (stats.cr != stats.crlf)
206 if (is_binary(len, &stats))
210 /* are we "faking" in place editing ? */
212 to_free = strbuf_detach(buf, NULL);
214 strbuf_grow(buf, len + stats.lf - stats.crlf);
216 const char *nl = memchr(src, '\n', len);
219 if (nl > src && nl[-1] == '\r') {
220 strbuf_add(buf, src, nl + 1 - src);
222 strbuf_add(buf, src, nl - src);
223 strbuf_addstr(buf, "\r\n");
228 strbuf_add(buf, src, len);
234 struct filter_params {
240 static int filter_buffer(int fd, void *data)
243 * Spawn cmd and feed the buffer contents through its stdin.
245 struct child_process child_process;
246 struct filter_params *params = (struct filter_params *)data;
247 int write_err, status;
248 const char *argv[] = { "sh", "-c", params->cmd, NULL };
250 memset(&child_process, 0, sizeof(child_process));
251 child_process.argv = argv;
252 child_process.in = -1;
253 child_process.out = fd;
255 if (start_command(&child_process))
256 return error("cannot fork to run external filter %s", params->cmd);
258 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
259 if (close(child_process.in))
262 error("cannot feed the input to external filter %s", params->cmd);
264 status = finish_command(&child_process);
266 error("external filter %s failed %d", params->cmd, -status);
267 return (write_err || status);
270 static int apply_filter(const char *path, const char *src, size_t len,
271 struct strbuf *dst, const char *cmd)
274 * Create a pipeline to have the command filter the buffer's
277 * (child --> cmd) --> us
282 struct filter_params params;
287 memset(&async, 0, sizeof(async));
288 async.proc = filter_buffer;
289 async.data = ¶ms;
295 if (start_async(&async))
296 return 0; /* error was already reported */
298 strbuf_init(&nbuf, 0);
299 if (strbuf_read(&nbuf, async.out, len) < 0) {
300 error("read from external filter %s failed", cmd);
303 if (close(async.out)) {
304 error("read from external filter %s failed", cmd);
307 if (finish_async(&async)) {
308 error("external filter %s failed", cmd);
313 strbuf_swap(dst, &nbuf);
315 strbuf_release(&nbuf);
319 static struct convert_driver {
321 struct convert_driver *next;
324 } *user_convert, **user_convert_tail;
326 static int read_convert_config(const char *var, const char *value, void *cb)
328 const char *ep, *name;
330 struct convert_driver *drv;
333 * External conversion drivers are configured using
334 * "filter.<name>.variable".
336 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
340 for (drv = user_convert; drv; drv = drv->next)
341 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
344 drv = xcalloc(1, sizeof(struct convert_driver));
345 drv->name = xmemdupz(name, namelen);
346 *user_convert_tail = drv;
347 user_convert_tail = &(drv->next);
353 * filter.<name>.smudge and filter.<name>.clean specifies
358 * The command-line will not be interpolated in any way.
361 if (!strcmp("smudge", ep))
362 return git_config_string(&drv->smudge, var, value);
364 if (!strcmp("clean", ep))
365 return git_config_string(&drv->clean, var, value);
370 static void setup_convert_check(struct git_attr_check *check)
372 static struct git_attr *attr_crlf;
373 static struct git_attr *attr_ident;
374 static struct git_attr *attr_filter;
377 attr_crlf = git_attr("crlf", 4);
378 attr_ident = git_attr("ident", 5);
379 attr_filter = git_attr("filter", 6);
380 user_convert_tail = &user_convert;
381 git_config(read_convert_config, NULL);
383 check[0].attr = attr_crlf;
384 check[1].attr = attr_ident;
385 check[2].attr = attr_filter;
388 static int count_ident(const char *cp, unsigned long size)
391 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
403 if (memcmp("Id", cp, 2))
414 * "$Id: ... "; scan up to the closing dollar sign and discard.
428 static int ident_to_git(const char *path, const char *src, size_t len,
429 struct strbuf *buf, int ident)
433 if (!ident || !count_ident(src, len))
436 /* only grow if not in place */
437 if (strbuf_avail(buf) + buf->len < len)
438 strbuf_grow(buf, len - buf->len);
441 dollar = memchr(src, '$', len);
444 memcpy(dst, src, dollar + 1 - src);
445 dst += dollar + 1 - src;
446 len -= dollar + 1 - src;
449 if (len > 3 && !memcmp(src, "Id:", 3)) {
450 dollar = memchr(src + 3, '$', len - 3);
453 memcpy(dst, "Id$", 3);
455 len -= dollar + 1 - src;
459 memcpy(dst, src, len);
460 strbuf_setlen(buf, dst + len - buf->buf);
464 static int ident_to_worktree(const char *path, const char *src, size_t len,
465 struct strbuf *buf, int ident)
467 unsigned char sha1[20];
468 char *to_free = NULL, *dollar;
474 cnt = count_ident(src, len);
478 /* are we "faking" in place editing ? */
480 to_free = strbuf_detach(buf, NULL);
481 hash_sha1_file(src, len, "blob", sha1);
483 strbuf_grow(buf, len + cnt * 43);
485 /* step 1: run to the next '$' */
486 dollar = memchr(src, '$', len);
489 strbuf_add(buf, src, dollar + 1 - src);
490 len -= dollar + 1 - src;
493 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
494 if (len < 3 || memcmp("Id", src, 2))
497 /* step 3: skip over Id$ or Id:xxxxx$ */
501 } else if (src[2] == ':') {
503 * It's possible that an expanded Id has crept its way into the
504 * repository, we cope with that by stripping the expansion out
506 dollar = memchr(src + 3, '$', len - 3);
508 /* incomplete keyword, no more '$', so just quit the loop */
512 len -= dollar + 1 - src;
515 /* it wasn't a "Id$" or "Id:xxxx$" */
519 /* step 4: substitute */
520 strbuf_addstr(buf, "Id: ");
521 strbuf_add(buf, sha1_to_hex(sha1), 40);
522 strbuf_addstr(buf, " $");
524 strbuf_add(buf, src, len);
530 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
532 const char *value = check->value;
534 if (ATTR_TRUE(value))
536 else if (ATTR_FALSE(value))
538 else if (ATTR_UNSET(value))
540 else if (!strcmp(value, "input"))
545 static struct convert_driver *git_path_check_convert(const char *path,
546 struct git_attr_check *check)
548 const char *value = check->value;
549 struct convert_driver *drv;
551 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
553 for (drv = user_convert; drv; drv = drv->next)
554 if (!strcmp(value, drv->name))
559 static int git_path_check_ident(const char *path, struct git_attr_check *check)
561 const char *value = check->value;
563 return !!ATTR_TRUE(value);
566 int convert_to_git(const char *path, const char *src, size_t len,
567 struct strbuf *dst, enum safe_crlf checksafe)
569 struct git_attr_check check[3];
570 int crlf = CRLF_GUESS;
571 int ident = 0, ret = 0;
572 const char *filter = NULL;
574 setup_convert_check(check);
575 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
576 struct convert_driver *drv;
577 crlf = git_path_check_crlf(path, check + 0);
578 ident = git_path_check_ident(path, check + 1);
579 drv = git_path_check_convert(path, check + 2);
580 if (drv && drv->clean)
584 ret |= apply_filter(path, src, len, dst, filter);
589 ret |= crlf_to_git(path, src, len, dst, crlf, checksafe);
594 return ret | ident_to_git(path, src, len, dst, ident);
597 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
599 struct git_attr_check check[3];
600 int crlf = CRLF_GUESS;
601 int ident = 0, ret = 0;
602 const char *filter = NULL;
604 setup_convert_check(check);
605 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
606 struct convert_driver *drv;
607 crlf = git_path_check_crlf(path, check + 0);
608 ident = git_path_check_ident(path, check + 1);
609 drv = git_path_check_convert(path, check + 2);
610 if (drv && drv->smudge)
611 filter = drv->smudge;
614 ret |= ident_to_worktree(path, src, len, dst, ident);
619 ret |= crlf_to_worktree(path, src, len, dst, crlf);
624 return ret | apply_filter(path, src, len, dst, filter);