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 "crlf" attribute or "auto_crlf" option is set.
30 /* NUL, CR, LF and CRLF counts */
31 unsigned nul, cr, lf, crlf;
33 /* These are just approximations! */
34 unsigned printable, nonprintable;
37 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
41 memset(stats, 0, sizeof(*stats));
43 for (i = 0; i < size; i++) {
44 unsigned char c = buf[i];
47 if (i+1 < size && buf[i+1] == '\n')
57 stats->nonprintable++;
60 /* BS, HT, ESC and FF */
61 case '\b': case '\t': case '\033': case '\014':
68 stats->nonprintable++;
75 /* If file ends with EOF then don't count this EOF as non-printable. */
76 if (size >= 1 && buf[size-1] == '\032')
77 stats->nonprintable--;
81 * The same heuristics as diff.c::mmfile_is_binary()
83 static int is_binary(unsigned long size, struct text_stat *stats)
88 if ((stats->printable >> 7) < stats->nonprintable)
91 * Other heuristics? Average line length might be relevant,
92 * as might LF vs CR vs CRLF counts..
94 * NOTE! It might be normal to have a low ratio of CRLF to LF
95 * (somebody starts with a LF-only file and edits it with an editor
96 * that adds CRLF only to lines that are added..). But do we
97 * want to support CR-only? Probably not.
102 static void check_safe_crlf(const char *path, enum action action,
103 struct text_stat *stats, enum safe_crlf checksafe)
108 if (action == CRLF_INPUT ||
109 (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_INPUT)) {
111 * CRLFs would not be restored by checkout:
112 * check if we'd remove CRLFs
115 if (checksafe == SAFE_CRLF_WARN)
116 warning("CRLF will be replaced by LF in %s.", path);
117 else /* i.e. SAFE_CRLF_FAIL */
118 die("CRLF would be replaced by LF in %s.", path);
120 } else if (action == CRLF_CRLF ||
121 (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_TRUE)) {
123 * CRLFs would be added by checkout:
124 * check if we have "naked" LFs
126 if (stats->lf != stats->crlf) {
127 if (checksafe == SAFE_CRLF_WARN)
128 warning("LF will be replaced by CRLF in %s", path);
129 else /* i.e. SAFE_CRLF_FAIL */
130 die("LF would be replaced by CRLF in %s", path);
135 static int has_cr_in_index(const char *path)
139 enum object_type type;
142 struct index_state *istate = &the_index;
145 pos = index_name_pos(istate, path, len);
148 * We might be in the middle of a merge, in which
149 * case we would read stage #2 (ours).
153 (pos < 0 && i < istate->cache_nr &&
154 !strcmp(istate->cache[i]->name, path));
156 if (ce_stage(istate->cache[i]) == 2)
161 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
162 if (!data || type != OBJ_BLOB) {
167 has_cr = memchr(data, '\r', sz) != NULL;
172 static int crlf_to_git(const char *path, const char *src, size_t len,
173 struct strbuf *buf, enum action action, enum safe_crlf checksafe)
175 struct text_stat stats;
178 if (action == CRLF_BINARY ||
179 (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) || !len)
182 gather_stats(src, len, &stats);
184 if (action == CRLF_AUTO || action == CRLF_GUESS) {
186 * We're currently not going to even try to convert stuff
187 * that has bare CR characters. Does anybody do that crazy
190 if (stats.cr != stats.crlf)
194 * And add some heuristics for binary vs text, of course...
196 if (is_binary(len, &stats))
199 if (action == CRLF_GUESS) {
201 * If the file in the index has any CR in it, do not convert.
202 * This is the new safer autocrlf handling.
204 if (has_cr_in_index(path))
209 check_safe_crlf(path, action, &stats, checksafe);
211 /* Optimization: No CR? Nothing to convert, regardless. */
215 /* only grow if not in place */
216 if (strbuf_avail(buf) + buf->len < len)
217 strbuf_grow(buf, len - buf->len);
219 if (action == CRLF_AUTO || action == CRLF_GUESS) {
221 * If we guessed, we already know we rejected a file with
222 * lone CR, and we can strip a CR without looking at what
226 unsigned char c = *src++;
232 unsigned char c = *src++;
233 if (! (c == '\r' && (1 < len && *src == '\n')))
237 strbuf_setlen(buf, dst - buf->buf);
241 static int crlf_to_worktree(const char *path, const char *src, size_t len,
242 struct strbuf *buf, enum action action)
244 char *to_free = NULL;
245 struct text_stat stats;
247 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
248 (action != CRLF_CRLF && auto_crlf != AUTO_CRLF_TRUE))
254 gather_stats(src, len, &stats);
256 /* No LF? Nothing to convert, regardless. */
260 /* Was it already in CRLF format? */
261 if (stats.lf == stats.crlf)
264 if (action == CRLF_AUTO || action == CRLF_GUESS) {
265 if (action == CRLF_GUESS) {
266 /* If we have any CR or CRLF line endings, we do not touch it */
267 /* This is the new safer autocrlf-handling */
268 if (stats.cr > 0 || stats.crlf > 0)
272 /* If we have any bare CR characters, we're not going to touch it */
273 if (stats.cr != stats.crlf)
276 if (is_binary(len, &stats))
280 /* are we "faking" in place editing ? */
282 to_free = strbuf_detach(buf, NULL);
284 strbuf_grow(buf, len + stats.lf - stats.crlf);
286 const char *nl = memchr(src, '\n', len);
289 if (nl > src && nl[-1] == '\r') {
290 strbuf_add(buf, src, nl + 1 - src);
292 strbuf_add(buf, src, nl - src);
293 strbuf_addstr(buf, "\r\n");
298 strbuf_add(buf, src, len);
304 struct filter_params {
310 static int filter_buffer(int fd, void *data)
313 * Spawn cmd and feed the buffer contents through its stdin.
315 struct child_process child_process;
316 struct filter_params *params = (struct filter_params *)data;
317 int write_err, status;
318 const char *argv[] = { params->cmd, NULL };
320 memset(&child_process, 0, sizeof(child_process));
321 child_process.argv = argv;
322 child_process.use_shell = 1;
323 child_process.in = -1;
324 child_process.out = fd;
326 if (start_command(&child_process))
327 return error("cannot fork to run external filter %s", params->cmd);
329 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
330 if (close(child_process.in))
333 error("cannot feed the input to external filter %s", params->cmd);
335 status = finish_command(&child_process);
337 error("external filter %s failed %d", params->cmd, status);
338 return (write_err || status);
341 static int apply_filter(const char *path, const char *src, size_t len,
342 struct strbuf *dst, const char *cmd)
345 * Create a pipeline to have the command filter the buffer's
348 * (child --> cmd) --> us
351 struct strbuf nbuf = STRBUF_INIT;
353 struct filter_params params;
358 memset(&async, 0, sizeof(async));
359 async.proc = filter_buffer;
360 async.data = ¶ms;
366 if (start_async(&async))
367 return 0; /* error was already reported */
369 if (strbuf_read(&nbuf, async.out, len) < 0) {
370 error("read from external filter %s failed", cmd);
373 if (close(async.out)) {
374 error("read from external filter %s failed", cmd);
377 if (finish_async(&async)) {
378 error("external filter %s failed", cmd);
383 strbuf_swap(dst, &nbuf);
385 strbuf_release(&nbuf);
389 static struct convert_driver {
391 struct convert_driver *next;
394 } *user_convert, **user_convert_tail;
396 static int read_convert_config(const char *var, const char *value, void *cb)
398 const char *ep, *name;
400 struct convert_driver *drv;
403 * External conversion drivers are configured using
404 * "filter.<name>.variable".
406 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
410 for (drv = user_convert; drv; drv = drv->next)
411 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
414 drv = xcalloc(1, sizeof(struct convert_driver));
415 drv->name = xmemdupz(name, namelen);
416 *user_convert_tail = drv;
417 user_convert_tail = &(drv->next);
423 * filter.<name>.smudge and filter.<name>.clean specifies
428 * The command-line will not be interpolated in any way.
431 if (!strcmp("smudge", ep))
432 return git_config_string(&drv->smudge, var, value);
434 if (!strcmp("clean", ep))
435 return git_config_string(&drv->clean, var, value);
440 static void setup_convert_check(struct git_attr_check *check)
442 static struct git_attr *attr_text;
443 static struct git_attr *attr_crlf;
444 static struct git_attr *attr_eol;
445 static struct git_attr *attr_ident;
446 static struct git_attr *attr_filter;
449 attr_text = git_attr("text");
450 attr_crlf = git_attr("crlf");
451 attr_eol = git_attr("eol");
452 attr_ident = git_attr("ident");
453 attr_filter = git_attr("filter");
454 user_convert_tail = &user_convert;
455 git_config(read_convert_config, NULL);
457 check[0].attr = attr_crlf;
458 check[1].attr = attr_ident;
459 check[2].attr = attr_filter;
460 check[3].attr = attr_eol;
461 check[4].attr = attr_text;
464 static int count_ident(const char *cp, unsigned long size)
467 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
479 if (memcmp("Id", cp, 2))
490 * "$Id: ... "; scan up to the closing dollar sign and discard.
504 static int ident_to_git(const char *path, const char *src, size_t len,
505 struct strbuf *buf, int ident)
509 if (!ident || !count_ident(src, len))
512 /* only grow if not in place */
513 if (strbuf_avail(buf) + buf->len < len)
514 strbuf_grow(buf, len - buf->len);
517 dollar = memchr(src, '$', len);
520 memcpy(dst, src, dollar + 1 - src);
521 dst += dollar + 1 - src;
522 len -= dollar + 1 - src;
525 if (len > 3 && !memcmp(src, "Id:", 3)) {
526 dollar = memchr(src + 3, '$', len - 3);
529 memcpy(dst, "Id$", 3);
531 len -= dollar + 1 - src;
535 memcpy(dst, src, len);
536 strbuf_setlen(buf, dst + len - buf->buf);
540 static int ident_to_worktree(const char *path, const char *src, size_t len,
541 struct strbuf *buf, int ident)
543 unsigned char sha1[20];
544 char *to_free = NULL, *dollar;
550 cnt = count_ident(src, len);
554 /* are we "faking" in place editing ? */
556 to_free = strbuf_detach(buf, NULL);
557 hash_sha1_file(src, len, "blob", sha1);
559 strbuf_grow(buf, len + cnt * 43);
561 /* step 1: run to the next '$' */
562 dollar = memchr(src, '$', len);
565 strbuf_add(buf, src, dollar + 1 - src);
566 len -= dollar + 1 - src;
569 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
570 if (len < 3 || memcmp("Id", src, 2))
573 /* step 3: skip over Id$ or Id:xxxxx$ */
577 } else if (src[2] == ':') {
579 * It's possible that an expanded Id has crept its way into the
580 * repository, we cope with that by stripping the expansion out
582 dollar = memchr(src + 3, '$', len - 3);
584 /* incomplete keyword, no more '$', so just quit the loop */
588 len -= dollar + 1 - src;
591 /* it wasn't a "Id$" or "Id:xxxx$" */
595 /* step 4: substitute */
596 strbuf_addstr(buf, "Id: ");
597 strbuf_add(buf, sha1_to_hex(sha1), 40);
598 strbuf_addstr(buf, " $");
600 strbuf_add(buf, src, len);
606 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
608 const char *value = check->value;
610 if (ATTR_TRUE(value))
612 else if (ATTR_FALSE(value))
614 else if (ATTR_UNSET(value))
616 else if (!strcmp(value, "input"))
618 else if (!strcmp(value, "auto"))
623 static int git_path_check_eol(const char *path, struct git_attr_check *check)
625 const char *value = check->value;
627 if (ATTR_UNSET(value))
629 else if (!strcmp(value, "lf"))
631 else if (!strcmp(value, "crlf"))
636 static struct convert_driver *git_path_check_convert(const char *path,
637 struct git_attr_check *check)
639 const char *value = check->value;
640 struct convert_driver *drv;
642 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
644 for (drv = user_convert; drv; drv = drv->next)
645 if (!strcmp(value, drv->name))
650 static int git_path_check_ident(const char *path, struct git_attr_check *check)
652 const char *value = check->value;
654 return !!ATTR_TRUE(value);
657 enum action determine_action(enum action text_attr, enum eol eol_attr) {
658 if (text_attr == CRLF_BINARY)
660 if (eol_attr == EOL_LF)
662 if (eol_attr == EOL_CRLF)
667 int convert_to_git(const char *path, const char *src, size_t len,
668 struct strbuf *dst, enum safe_crlf checksafe)
670 struct git_attr_check check[5];
671 enum action action = CRLF_GUESS;
672 enum eol eol = EOL_UNSET;
673 int ident = 0, ret = 0;
674 const char *filter = NULL;
676 setup_convert_check(check);
677 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
678 struct convert_driver *drv;
679 action = git_path_check_crlf(path, check + 4);
680 if (action == CRLF_GUESS)
681 action = git_path_check_crlf(path, check + 0);
682 ident = git_path_check_ident(path, check + 1);
683 drv = git_path_check_convert(path, check + 2);
684 eol = git_path_check_eol(path, check + 3);
685 if (drv && drv->clean)
689 ret |= apply_filter(path, src, len, dst, filter);
694 action = determine_action(action, eol);
695 ret |= crlf_to_git(path, src, len, dst, action, checksafe);
700 return ret | ident_to_git(path, src, len, dst, ident);
703 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
705 struct git_attr_check check[5];
706 enum action action = CRLF_GUESS;
707 enum eol eol = EOL_UNSET;
708 int ident = 0, ret = 0;
709 const char *filter = NULL;
711 setup_convert_check(check);
712 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
713 struct convert_driver *drv;
714 action = git_path_check_crlf(path, check + 4);
715 if (action == CRLF_GUESS)
716 action = git_path_check_crlf(path, check + 0);
717 ident = git_path_check_ident(path, check + 1);
718 drv = git_path_check_convert(path, check + 2);
719 eol = git_path_check_eol(path, check + 3);
720 if (drv && drv->smudge)
721 filter = drv->smudge;
724 ret |= ident_to_worktree(path, src, len, dst, ident);
729 action = determine_action(action, eol);
730 ret |= crlf_to_worktree(path, src, len, dst, action);
735 return ret | apply_filter(path, src, len, dst, filter);