t6007: test rev-list --cherry
[git.git] / convert.c
blobd5aebed48df3387e966a23b6ee460dc2244f728e
1 #include "cache.h"
2 #include "attr.h"
3 #include "run-command.h"
4 #include "quote.h"
6 /*
7 * convert.c - convert a file when checking it out and checking it in.
9 * This should use the pathname to decide on whether it wants to do some
10 * more interesting conversions (automatic gzip/unzip, general format
11 * conversions etc etc), but by default it just does automatic CRLF<->LF
12 * translation when the "text" attribute or "auto_crlf" option is set.
15 enum action {
16 CRLF_GUESS = -1,
17 CRLF_BINARY = 0,
18 CRLF_TEXT,
19 CRLF_INPUT,
20 CRLF_CRLF,
21 CRLF_AUTO,
24 struct text_stat {
25 /* NUL, CR, LF and CRLF counts */
26 unsigned nul, cr, lf, crlf;
28 /* These are just approximations! */
29 unsigned printable, nonprintable;
32 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
34 unsigned long i;
36 memset(stats, 0, sizeof(*stats));
38 for (i = 0; i < size; i++) {
39 unsigned char c = buf[i];
40 if (c == '\r') {
41 stats->cr++;
42 if (i+1 < size && buf[i+1] == '\n')
43 stats->crlf++;
44 continue;
46 if (c == '\n') {
47 stats->lf++;
48 continue;
50 if (c == 127)
51 /* DEL */
52 stats->nonprintable++;
53 else if (c < 32) {
54 switch (c) {
55 /* BS, HT, ESC and FF */
56 case '\b': case '\t': case '\033': case '\014':
57 stats->printable++;
58 break;
59 case 0:
60 stats->nul++;
61 /* fall through */
62 default:
63 stats->nonprintable++;
66 else
67 stats->printable++;
70 /* If file ends with EOF then don't count this EOF as non-printable. */
71 if (size >= 1 && buf[size-1] == '\032')
72 stats->nonprintable--;
76 * The same heuristics as diff.c::mmfile_is_binary()
78 static int is_binary(unsigned long size, struct text_stat *stats)
81 if (stats->nul)
82 return 1;
83 if ((stats->printable >> 7) < stats->nonprintable)
84 return 1;
86 * Other heuristics? Average line length might be relevant,
87 * as might LF vs CR vs CRLF counts..
89 * NOTE! It might be normal to have a low ratio of CRLF to LF
90 * (somebody starts with a LF-only file and edits it with an editor
91 * that adds CRLF only to lines that are added..). But do we
92 * want to support CR-only? Probably not.
94 return 0;
97 static enum eol determine_output_conversion(enum action action)
99 switch (action) {
100 case CRLF_BINARY:
101 return EOL_UNSET;
102 case CRLF_CRLF:
103 return EOL_CRLF;
104 case CRLF_INPUT:
105 return EOL_LF;
106 case CRLF_GUESS:
107 if (!auto_crlf)
108 return EOL_UNSET;
109 /* fall through */
110 case CRLF_TEXT:
111 case CRLF_AUTO:
112 if (auto_crlf == AUTO_CRLF_TRUE)
113 return EOL_CRLF;
114 else if (auto_crlf == AUTO_CRLF_INPUT)
115 return EOL_LF;
116 else if (eol == EOL_UNSET)
117 return EOL_NATIVE;
119 return eol;
122 static void check_safe_crlf(const char *path, enum action action,
123 struct text_stat *stats, enum safe_crlf checksafe)
125 if (!checksafe)
126 return;
128 if (determine_output_conversion(action) == EOL_LF) {
130 * CRLFs would not be restored by checkout:
131 * check if we'd remove CRLFs
133 if (stats->crlf) {
134 if (checksafe == SAFE_CRLF_WARN)
135 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
136 else /* i.e. SAFE_CRLF_FAIL */
137 die("CRLF would be replaced by LF in %s.", path);
139 } else if (determine_output_conversion(action) == EOL_CRLF) {
141 * CRLFs would be added by checkout:
142 * check if we have "naked" LFs
144 if (stats->lf != stats->crlf) {
145 if (checksafe == SAFE_CRLF_WARN)
146 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
147 else /* i.e. SAFE_CRLF_FAIL */
148 die("LF would be replaced by CRLF in %s", path);
153 static int has_cr_in_index(const char *path)
155 int pos, len;
156 unsigned long sz;
157 enum object_type type;
158 void *data;
159 int has_cr;
160 struct index_state *istate = &the_index;
162 len = strlen(path);
163 pos = index_name_pos(istate, path, len);
164 if (pos < 0) {
166 * We might be in the middle of a merge, in which
167 * case we would read stage #2 (ours).
169 int i;
170 for (i = -pos - 1;
171 (pos < 0 && i < istate->cache_nr &&
172 !strcmp(istate->cache[i]->name, path));
173 i++)
174 if (ce_stage(istate->cache[i]) == 2)
175 pos = i;
177 if (pos < 0)
178 return 0;
179 data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz);
180 if (!data || type != OBJ_BLOB) {
181 free(data);
182 return 0;
185 has_cr = memchr(data, '\r', sz) != NULL;
186 free(data);
187 return has_cr;
190 static int crlf_to_git(const char *path, const char *src, size_t len,
191 struct strbuf *buf, enum action action, enum safe_crlf checksafe)
193 struct text_stat stats;
194 char *dst;
196 if (action == CRLF_BINARY ||
197 (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) || !len)
198 return 0;
200 gather_stats(src, len, &stats);
202 if (action == CRLF_AUTO || action == CRLF_GUESS) {
204 * We're currently not going to even try to convert stuff
205 * that has bare CR characters. Does anybody do that crazy
206 * stuff?
208 if (stats.cr != stats.crlf)
209 return 0;
212 * And add some heuristics for binary vs text, of course...
214 if (is_binary(len, &stats))
215 return 0;
217 if (action == CRLF_GUESS) {
219 * If the file in the index has any CR in it, do not convert.
220 * This is the new safer autocrlf handling.
222 if (has_cr_in_index(path))
223 return 0;
227 check_safe_crlf(path, action, &stats, checksafe);
229 /* Optimization: No CR? Nothing to convert, regardless. */
230 if (!stats.cr)
231 return 0;
233 /* only grow if not in place */
234 if (strbuf_avail(buf) + buf->len < len)
235 strbuf_grow(buf, len - buf->len);
236 dst = buf->buf;
237 if (action == CRLF_AUTO || action == CRLF_GUESS) {
239 * If we guessed, we already know we rejected a file with
240 * lone CR, and we can strip a CR without looking at what
241 * follow it.
243 do {
244 unsigned char c = *src++;
245 if (c != '\r')
246 *dst++ = c;
247 } while (--len);
248 } else {
249 do {
250 unsigned char c = *src++;
251 if (! (c == '\r' && (1 < len && *src == '\n')))
252 *dst++ = c;
253 } while (--len);
255 strbuf_setlen(buf, dst - buf->buf);
256 return 1;
259 static int crlf_to_worktree(const char *path, const char *src, size_t len,
260 struct strbuf *buf, enum action action)
262 char *to_free = NULL;
263 struct text_stat stats;
265 if (!len || determine_output_conversion(action) != EOL_CRLF)
266 return 0;
268 gather_stats(src, len, &stats);
270 /* No LF? Nothing to convert, regardless. */
271 if (!stats.lf)
272 return 0;
274 /* Was it already in CRLF format? */
275 if (stats.lf == stats.crlf)
276 return 0;
278 if (action == CRLF_AUTO || action == CRLF_GUESS) {
279 if (action == CRLF_GUESS) {
280 /* If we have any CR or CRLF line endings, we do not touch it */
281 /* This is the new safer autocrlf-handling */
282 if (stats.cr > 0 || stats.crlf > 0)
283 return 0;
286 /* If we have any bare CR characters, we're not going to touch it */
287 if (stats.cr != stats.crlf)
288 return 0;
290 if (is_binary(len, &stats))
291 return 0;
294 /* are we "faking" in place editing ? */
295 if (src == buf->buf)
296 to_free = strbuf_detach(buf, NULL);
298 strbuf_grow(buf, len + stats.lf - stats.crlf);
299 for (;;) {
300 const char *nl = memchr(src, '\n', len);
301 if (!nl)
302 break;
303 if (nl > src && nl[-1] == '\r') {
304 strbuf_add(buf, src, nl + 1 - src);
305 } else {
306 strbuf_add(buf, src, nl - src);
307 strbuf_addstr(buf, "\r\n");
309 len -= nl + 1 - src;
310 src = nl + 1;
312 strbuf_add(buf, src, len);
314 free(to_free);
315 return 1;
318 struct filter_params {
319 const char *src;
320 unsigned long size;
321 const char *cmd;
322 const char *path;
325 static int filter_buffer(int in, int out, void *data)
328 * Spawn cmd and feed the buffer contents through its stdin.
330 struct child_process child_process;
331 struct filter_params *params = (struct filter_params *)data;
332 int write_err, status;
333 const char *argv[] = { NULL, NULL };
335 /* apply % substitution to cmd */
336 struct strbuf cmd = STRBUF_INIT;
337 struct strbuf path = STRBUF_INIT;
338 struct strbuf_expand_dict_entry dict[] = {
339 { "f", NULL, },
340 { NULL, NULL, },
343 /* quote the path to preserve spaces, etc. */
344 sq_quote_buf(&path, params->path);
345 dict[0].value = path.buf;
347 /* expand all %f with the quoted path */
348 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
349 strbuf_release(&path);
351 argv[0] = cmd.buf;
353 memset(&child_process, 0, sizeof(child_process));
354 child_process.argv = argv;
355 child_process.use_shell = 1;
356 child_process.in = -1;
357 child_process.out = out;
359 if (start_command(&child_process))
360 return error("cannot fork to run external filter %s", params->cmd);
362 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
363 if (close(child_process.in))
364 write_err = 1;
365 if (write_err)
366 error("cannot feed the input to external filter %s", params->cmd);
368 status = finish_command(&child_process);
369 if (status)
370 error("external filter %s failed %d", params->cmd, status);
372 strbuf_release(&cmd);
373 return (write_err || status);
376 static int apply_filter(const char *path, const char *src, size_t len,
377 struct strbuf *dst, const char *cmd)
380 * Create a pipeline to have the command filter the buffer's
381 * contents.
383 * (child --> cmd) --> us
385 int ret = 1;
386 struct strbuf nbuf = STRBUF_INIT;
387 struct async async;
388 struct filter_params params;
390 if (!cmd)
391 return 0;
393 memset(&async, 0, sizeof(async));
394 async.proc = filter_buffer;
395 async.data = &params;
396 async.out = -1;
397 params.src = src;
398 params.size = len;
399 params.cmd = cmd;
400 params.path = path;
402 fflush(NULL);
403 if (start_async(&async))
404 return 0; /* error was already reported */
406 if (strbuf_read(&nbuf, async.out, len) < 0) {
407 error("read from external filter %s failed", cmd);
408 ret = 0;
410 if (close(async.out)) {
411 error("read from external filter %s failed", cmd);
412 ret = 0;
414 if (finish_async(&async)) {
415 error("external filter %s failed", cmd);
416 ret = 0;
419 if (ret) {
420 strbuf_swap(dst, &nbuf);
422 strbuf_release(&nbuf);
423 return ret;
426 static struct convert_driver {
427 const char *name;
428 struct convert_driver *next;
429 const char *smudge;
430 const char *clean;
431 } *user_convert, **user_convert_tail;
433 static int read_convert_config(const char *var, const char *value, void *cb)
435 const char *ep, *name;
436 int namelen;
437 struct convert_driver *drv;
440 * External conversion drivers are configured using
441 * "filter.<name>.variable".
443 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
444 return 0;
445 name = var + 7;
446 namelen = ep - name;
447 for (drv = user_convert; drv; drv = drv->next)
448 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
449 break;
450 if (!drv) {
451 drv = xcalloc(1, sizeof(struct convert_driver));
452 drv->name = xmemdupz(name, namelen);
453 *user_convert_tail = drv;
454 user_convert_tail = &(drv->next);
457 ep++;
460 * filter.<name>.smudge and filter.<name>.clean specifies
461 * the command line:
463 * command-line
465 * The command-line will not be interpolated in any way.
468 if (!strcmp("smudge", ep))
469 return git_config_string(&drv->smudge, var, value);
471 if (!strcmp("clean", ep))
472 return git_config_string(&drv->clean, var, value);
474 return 0;
477 static void setup_convert_check(struct git_attr_check *check)
479 static struct git_attr *attr_text;
480 static struct git_attr *attr_crlf;
481 static struct git_attr *attr_eol;
482 static struct git_attr *attr_ident;
483 static struct git_attr *attr_filter;
485 if (!attr_text) {
486 attr_text = git_attr("text");
487 attr_crlf = git_attr("crlf");
488 attr_eol = git_attr("eol");
489 attr_ident = git_attr("ident");
490 attr_filter = git_attr("filter");
491 user_convert_tail = &user_convert;
492 git_config(read_convert_config, NULL);
494 check[0].attr = attr_crlf;
495 check[1].attr = attr_ident;
496 check[2].attr = attr_filter;
497 check[3].attr = attr_eol;
498 check[4].attr = attr_text;
501 static int count_ident(const char *cp, unsigned long size)
504 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
506 int cnt = 0;
507 char ch;
509 while (size) {
510 ch = *cp++;
511 size--;
512 if (ch != '$')
513 continue;
514 if (size < 3)
515 break;
516 if (memcmp("Id", cp, 2))
517 continue;
518 ch = cp[2];
519 cp += 3;
520 size -= 3;
521 if (ch == '$')
522 cnt++; /* $Id$ */
523 if (ch != ':')
524 continue;
527 * "$Id: ... "; scan up to the closing dollar sign and discard.
529 while (size) {
530 ch = *cp++;
531 size--;
532 if (ch == '$') {
533 cnt++;
534 break;
536 if (ch == '\n')
537 break;
540 return cnt;
543 static int ident_to_git(const char *path, const char *src, size_t len,
544 struct strbuf *buf, int ident)
546 char *dst, *dollar;
548 if (!ident || !count_ident(src, len))
549 return 0;
551 /* only grow if not in place */
552 if (strbuf_avail(buf) + buf->len < len)
553 strbuf_grow(buf, len - buf->len);
554 dst = buf->buf;
555 for (;;) {
556 dollar = memchr(src, '$', len);
557 if (!dollar)
558 break;
559 memcpy(dst, src, dollar + 1 - src);
560 dst += dollar + 1 - src;
561 len -= dollar + 1 - src;
562 src = dollar + 1;
564 if (len > 3 && !memcmp(src, "Id:", 3)) {
565 dollar = memchr(src + 3, '$', len - 3);
566 if (!dollar)
567 break;
568 if (memchr(src + 3, '\n', dollar - src - 3)) {
569 /* Line break before the next dollar. */
570 continue;
573 memcpy(dst, "Id$", 3);
574 dst += 3;
575 len -= dollar + 1 - src;
576 src = dollar + 1;
579 memcpy(dst, src, len);
580 strbuf_setlen(buf, dst + len - buf->buf);
581 return 1;
584 static int ident_to_worktree(const char *path, const char *src, size_t len,
585 struct strbuf *buf, int ident)
587 unsigned char sha1[20];
588 char *to_free = NULL, *dollar, *spc;
589 int cnt;
591 if (!ident)
592 return 0;
594 cnt = count_ident(src, len);
595 if (!cnt)
596 return 0;
598 /* are we "faking" in place editing ? */
599 if (src == buf->buf)
600 to_free = strbuf_detach(buf, NULL);
601 hash_sha1_file(src, len, "blob", sha1);
603 strbuf_grow(buf, len + cnt * 43);
604 for (;;) {
605 /* step 1: run to the next '$' */
606 dollar = memchr(src, '$', len);
607 if (!dollar)
608 break;
609 strbuf_add(buf, src, dollar + 1 - src);
610 len -= dollar + 1 - src;
611 src = dollar + 1;
613 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
614 if (len < 3 || memcmp("Id", src, 2))
615 continue;
617 /* step 3: skip over Id$ or Id:xxxxx$ */
618 if (src[2] == '$') {
619 src += 3;
620 len -= 3;
621 } else if (src[2] == ':') {
623 * It's possible that an expanded Id has crept its way into the
624 * repository, we cope with that by stripping the expansion out.
625 * This is probably not a good idea, since it will cause changes
626 * on checkout, which won't go away by stash, but let's keep it
627 * for git-style ids.
629 dollar = memchr(src + 3, '$', len - 3);
630 if (!dollar) {
631 /* incomplete keyword, no more '$', so just quit the loop */
632 break;
635 if (memchr(src + 3, '\n', dollar - src - 3)) {
636 /* Line break before the next dollar. */
637 continue;
640 spc = memchr(src + 4, ' ', dollar - src - 4);
641 if (spc && spc < dollar-1) {
642 /* There are spaces in unexpected places.
643 * This is probably an id from some other
644 * versioning system. Keep it for now.
646 continue;
649 len -= dollar + 1 - src;
650 src = dollar + 1;
651 } else {
652 /* it wasn't a "Id$" or "Id:xxxx$" */
653 continue;
656 /* step 4: substitute */
657 strbuf_addstr(buf, "Id: ");
658 strbuf_add(buf, sha1_to_hex(sha1), 40);
659 strbuf_addstr(buf, " $");
661 strbuf_add(buf, src, len);
663 free(to_free);
664 return 1;
667 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
669 const char *value = check->value;
671 if (ATTR_TRUE(value))
672 return CRLF_TEXT;
673 else if (ATTR_FALSE(value))
674 return CRLF_BINARY;
675 else if (ATTR_UNSET(value))
677 else if (!strcmp(value, "input"))
678 return CRLF_INPUT;
679 else if (!strcmp(value, "auto"))
680 return CRLF_AUTO;
681 return CRLF_GUESS;
684 static int git_path_check_eol(const char *path, struct git_attr_check *check)
686 const char *value = check->value;
688 if (ATTR_UNSET(value))
690 else if (!strcmp(value, "lf"))
691 return EOL_LF;
692 else if (!strcmp(value, "crlf"))
693 return EOL_CRLF;
694 return EOL_UNSET;
697 static struct convert_driver *git_path_check_convert(const char *path,
698 struct git_attr_check *check)
700 const char *value = check->value;
701 struct convert_driver *drv;
703 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
704 return NULL;
705 for (drv = user_convert; drv; drv = drv->next)
706 if (!strcmp(value, drv->name))
707 return drv;
708 return NULL;
711 static int git_path_check_ident(const char *path, struct git_attr_check *check)
713 const char *value = check->value;
715 return !!ATTR_TRUE(value);
718 static enum action determine_action(enum action text_attr, enum eol eol_attr)
720 if (text_attr == CRLF_BINARY)
721 return CRLF_BINARY;
722 if (eol_attr == EOL_LF)
723 return CRLF_INPUT;
724 if (eol_attr == EOL_CRLF)
725 return CRLF_CRLF;
726 return text_attr;
729 int convert_to_git(const char *path, const char *src, size_t len,
730 struct strbuf *dst, enum safe_crlf checksafe)
732 struct git_attr_check check[5];
733 enum action action = CRLF_GUESS;
734 enum eol eol_attr = EOL_UNSET;
735 int ident = 0, ret = 0;
736 const char *filter = NULL;
738 setup_convert_check(check);
739 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
740 struct convert_driver *drv;
741 action = git_path_check_crlf(path, check + 4);
742 if (action == CRLF_GUESS)
743 action = git_path_check_crlf(path, check + 0);
744 ident = git_path_check_ident(path, check + 1);
745 drv = git_path_check_convert(path, check + 2);
746 eol_attr = git_path_check_eol(path, check + 3);
747 if (drv && drv->clean)
748 filter = drv->clean;
751 ret |= apply_filter(path, src, len, dst, filter);
752 if (ret) {
753 src = dst->buf;
754 len = dst->len;
756 action = determine_action(action, eol_attr);
757 ret |= crlf_to_git(path, src, len, dst, action, checksafe);
758 if (ret) {
759 src = dst->buf;
760 len = dst->len;
762 return ret | ident_to_git(path, src, len, dst, ident);
765 static int convert_to_working_tree_internal(const char *path, const char *src,
766 size_t len, struct strbuf *dst,
767 int normalizing)
769 struct git_attr_check check[5];
770 enum action action = CRLF_GUESS;
771 enum eol eol_attr = EOL_UNSET;
772 int ident = 0, ret = 0;
773 const char *filter = NULL;
775 setup_convert_check(check);
776 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
777 struct convert_driver *drv;
778 action = git_path_check_crlf(path, check + 4);
779 if (action == CRLF_GUESS)
780 action = git_path_check_crlf(path, check + 0);
781 ident = git_path_check_ident(path, check + 1);
782 drv = git_path_check_convert(path, check + 2);
783 eol_attr = git_path_check_eol(path, check + 3);
784 if (drv && drv->smudge)
785 filter = drv->smudge;
788 ret |= ident_to_worktree(path, src, len, dst, ident);
789 if (ret) {
790 src = dst->buf;
791 len = dst->len;
794 * CRLF conversion can be skipped if normalizing, unless there
795 * is a smudge filter. The filter might expect CRLFs.
797 if (filter || !normalizing) {
798 action = determine_action(action, eol_attr);
799 ret |= crlf_to_worktree(path, src, len, dst, action);
800 if (ret) {
801 src = dst->buf;
802 len = dst->len;
805 return ret | apply_filter(path, src, len, dst, filter);
808 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
810 return convert_to_working_tree_internal(path, src, len, dst, 0);
813 int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
815 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
816 if (ret) {
817 src = dst->buf;
818 len = dst->len;
820 return ret | convert_to_git(path, src, len, dst, 0);