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_crlf;
443 static struct git_attr *attr_eol;
444 static struct git_attr *attr_ident;
445 static struct git_attr *attr_filter;
448 attr_crlf = git_attr("crlf");
449 attr_eol = git_attr("eol");
450 attr_ident = git_attr("ident");
451 attr_filter = git_attr("filter");
452 user_convert_tail = &user_convert;
453 git_config(read_convert_config, NULL);
455 check[0].attr = attr_crlf;
456 check[1].attr = attr_ident;
457 check[2].attr = attr_filter;
458 check[3].attr = attr_eol;
461 static int count_ident(const char *cp, unsigned long size)
464 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
476 if (memcmp("Id", cp, 2))
487 * "$Id: ... "; scan up to the closing dollar sign and discard.
501 static int ident_to_git(const char *path, const char *src, size_t len,
502 struct strbuf *buf, int ident)
506 if (!ident || !count_ident(src, len))
509 /* only grow if not in place */
510 if (strbuf_avail(buf) + buf->len < len)
511 strbuf_grow(buf, len - buf->len);
514 dollar = memchr(src, '$', len);
517 memcpy(dst, src, dollar + 1 - src);
518 dst += dollar + 1 - src;
519 len -= dollar + 1 - src;
522 if (len > 3 && !memcmp(src, "Id:", 3)) {
523 dollar = memchr(src + 3, '$', len - 3);
526 memcpy(dst, "Id$", 3);
528 len -= dollar + 1 - src;
532 memcpy(dst, src, len);
533 strbuf_setlen(buf, dst + len - buf->buf);
537 static int ident_to_worktree(const char *path, const char *src, size_t len,
538 struct strbuf *buf, int ident)
540 unsigned char sha1[20];
541 char *to_free = NULL, *dollar;
547 cnt = count_ident(src, len);
551 /* are we "faking" in place editing ? */
553 to_free = strbuf_detach(buf, NULL);
554 hash_sha1_file(src, len, "blob", sha1);
556 strbuf_grow(buf, len + cnt * 43);
558 /* step 1: run to the next '$' */
559 dollar = memchr(src, '$', len);
562 strbuf_add(buf, src, dollar + 1 - src);
563 len -= dollar + 1 - src;
566 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
567 if (len < 3 || memcmp("Id", src, 2))
570 /* step 3: skip over Id$ or Id:xxxxx$ */
574 } else if (src[2] == ':') {
576 * It's possible that an expanded Id has crept its way into the
577 * repository, we cope with that by stripping the expansion out
579 dollar = memchr(src + 3, '$', len - 3);
581 /* incomplete keyword, no more '$', so just quit the loop */
585 len -= dollar + 1 - src;
588 /* it wasn't a "Id$" or "Id:xxxx$" */
592 /* step 4: substitute */
593 strbuf_addstr(buf, "Id: ");
594 strbuf_add(buf, sha1_to_hex(sha1), 40);
595 strbuf_addstr(buf, " $");
597 strbuf_add(buf, src, len);
603 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
605 const char *value = check->value;
607 if (ATTR_TRUE(value))
609 else if (ATTR_FALSE(value))
611 else if (ATTR_UNSET(value))
613 else if (!strcmp(value, "input"))
615 else if (!strcmp(value, "auto"))
620 static int git_path_check_eol(const char *path, struct git_attr_check *check)
622 const char *value = check->value;
624 if (ATTR_UNSET(value))
626 else if (!strcmp(value, "lf"))
628 else if (!strcmp(value, "crlf"))
633 static struct convert_driver *git_path_check_convert(const char *path,
634 struct git_attr_check *check)
636 const char *value = check->value;
637 struct convert_driver *drv;
639 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
641 for (drv = user_convert; drv; drv = drv->next)
642 if (!strcmp(value, drv->name))
647 static int git_path_check_ident(const char *path, struct git_attr_check *check)
649 const char *value = check->value;
651 return !!ATTR_TRUE(value);
654 enum action determine_action(enum action crlf_attr, enum eol eol_attr) {
655 if (crlf_attr == CRLF_BINARY)
657 if (eol_attr == EOL_LF)
659 if (eol_attr == EOL_CRLF)
664 int convert_to_git(const char *path, const char *src, size_t len,
665 struct strbuf *dst, enum safe_crlf checksafe)
667 struct git_attr_check check[4];
668 enum action action = CRLF_GUESS;
669 enum eol eol = EOL_UNSET;
670 int ident = 0, ret = 0;
671 const char *filter = NULL;
673 setup_convert_check(check);
674 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
675 struct convert_driver *drv;
676 action = git_path_check_crlf(path, check + 0);
677 ident = git_path_check_ident(path, check + 1);
678 drv = git_path_check_convert(path, check + 2);
679 eol = git_path_check_eol(path, check + 3);
680 if (drv && drv->clean)
684 ret |= apply_filter(path, src, len, dst, filter);
689 action = determine_action(action, eol);
690 ret |= crlf_to_git(path, src, len, dst, action, checksafe);
695 return ret | ident_to_git(path, src, len, dst, ident);
698 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
700 struct git_attr_check check[4];
701 enum action action = CRLF_GUESS;
702 enum eol eol = EOL_UNSET;
703 int ident = 0, ret = 0;
704 const char *filter = NULL;
706 setup_convert_check(check);
707 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
708 struct convert_driver *drv;
709 action = git_path_check_crlf(path, check + 0);
710 ident = git_path_check_ident(path, check + 1);
711 drv = git_path_check_convert(path, check + 2);
712 eol = git_path_check_eol(path, check + 3);
713 if (drv && drv->smudge)
714 filter = drv->smudge;
717 ret |= ident_to_worktree(path, src, len, dst, ident);
722 action = determine_action(action, eol);
723 ret |= crlf_to_worktree(path, src, len, dst, action);
728 return ret | apply_filter(path, src, len, dst, filter);