gitweb: Add git_merge sub
[git/gsoc2010-gitweb.git] / convert.c
blobc2ae5af72d0dcf7b5bac7e4f82c3a596e0eff159
1 #include "cache.h"
2 #include "attr.h"
3 #include "run-command.h"
5 /*
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.
14 enum action {
15 CRLF_GUESS = -1,
16 CRLF_BINARY = 0,
17 CRLF_TEXT,
18 CRLF_INPUT,
19 CRLF_CRLF,
20 CRLF_AUTO,
23 struct text_stat {
24 /* NUL, CR, LF and CRLF counts */
25 unsigned nul, cr, lf, crlf;
27 /* These are just approximations! */
28 unsigned printable, nonprintable;
31 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
33 unsigned long i;
35 memset(stats, 0, sizeof(*stats));
37 for (i = 0; i < size; i++) {
38 unsigned char c = buf[i];
39 if (c == '\r') {
40 stats->cr++;
41 if (i+1 < size && buf[i+1] == '\n')
42 stats->crlf++;
43 continue;
45 if (c == '\n') {
46 stats->lf++;
47 continue;
49 if (c == 127)
50 /* DEL */
51 stats->nonprintable++;
52 else if (c < 32) {
53 switch (c) {
54 /* BS, HT, ESC and FF */
55 case '\b': case '\t': case '\033': case '\014':
56 stats->printable++;
57 break;
58 case 0:
59 stats->nul++;
60 /* fall through */
61 default:
62 stats->nonprintable++;
65 else
66 stats->printable++;
69 /* If file ends with EOF then don't count this EOF as non-printable. */
70 if (size >= 1 && buf[size-1] == '\032')
71 stats->nonprintable--;
75 * The same heuristics as diff.c::mmfile_is_binary()
77 static int is_binary(unsigned long size, struct text_stat *stats)
80 if (stats->nul)
81 return 1;
82 if ((stats->printable >> 7) < stats->nonprintable)
83 return 1;
85 * Other heuristics? Average line length might be relevant,
86 * as might LF vs CR vs CRLF counts..
88 * NOTE! It might be normal to have a low ratio of CRLF to LF
89 * (somebody starts with a LF-only file and edits it with an editor
90 * that adds CRLF only to lines that are added..). But do we
91 * want to support CR-only? Probably not.
93 return 0;
96 static enum eol get_output_conversion(enum action action) {
97 switch (action) {
98 case CRLF_BINARY:
99 return EOL_UNSET;
100 case CRLF_CRLF:
101 return EOL_CRLF;
102 case CRLF_INPUT:
103 return EOL_LF;
104 case CRLF_GUESS:
105 if (auto_crlf == AUTO_CRLF_FALSE)
106 return EOL_UNSET;
107 /* fall through */
108 case CRLF_TEXT:
109 case CRLF_AUTO:
110 if (eol == EOL_UNSET) {
111 if (auto_crlf == AUTO_CRLF_FALSE)
112 return EOL_NATIVE;
113 else if (auto_crlf == AUTO_CRLF_TRUE)
114 return EOL_CRLF;
115 else
116 return EOL_LF;
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 (get_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.", path);
136 else /* i.e. SAFE_CRLF_FAIL */
137 die("CRLF would be replaced by LF in %s.", path);
139 } else if (get_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", 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)
266 return 0;
268 if (get_output_conversion(action) != EOL_CRLF)
269 return 0;
271 gather_stats(src, len, &stats);
273 /* No LF? Nothing to convert, regardless. */
274 if (!stats.lf)
275 return 0;
277 /* Was it already in CRLF format? */
278 if (stats.lf == stats.crlf)
279 return 0;
281 if (action == CRLF_AUTO || action == CRLF_GUESS) {
282 if (action == CRLF_GUESS) {
283 /* If we have any CR or CRLF line endings, we do not touch it */
284 /* This is the new safer autocrlf-handling */
285 if (stats.cr > 0 || stats.crlf > 0)
286 return 0;
289 /* If we have any bare CR characters, we're not going to touch it */
290 if (stats.cr != stats.crlf)
291 return 0;
293 if (is_binary(len, &stats))
294 return 0;
297 /* are we "faking" in place editing ? */
298 if (src == buf->buf)
299 to_free = strbuf_detach(buf, NULL);
301 strbuf_grow(buf, len + stats.lf - stats.crlf);
302 for (;;) {
303 const char *nl = memchr(src, '\n', len);
304 if (!nl)
305 break;
306 if (nl > src && nl[-1] == '\r') {
307 strbuf_add(buf, src, nl + 1 - src);
308 } else {
309 strbuf_add(buf, src, nl - src);
310 strbuf_addstr(buf, "\r\n");
312 len -= nl + 1 - src;
313 src = nl + 1;
315 strbuf_add(buf, src, len);
317 free(to_free);
318 return 1;
321 struct filter_params {
322 const char *src;
323 unsigned long size;
324 const char *cmd;
327 static int filter_buffer(int in, int out, void *data)
330 * Spawn cmd and feed the buffer contents through its stdin.
332 struct child_process child_process;
333 struct filter_params *params = (struct filter_params *)data;
334 int write_err, status;
335 const char *argv[] = { NULL, NULL };
337 argv[0] = params->cmd;
339 memset(&child_process, 0, sizeof(child_process));
340 child_process.argv = argv;
341 child_process.use_shell = 1;
342 child_process.in = -1;
343 child_process.out = out;
345 if (start_command(&child_process))
346 return error("cannot fork to run external filter %s", params->cmd);
348 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
349 if (close(child_process.in))
350 write_err = 1;
351 if (write_err)
352 error("cannot feed the input to external filter %s", params->cmd);
354 status = finish_command(&child_process);
355 if (status)
356 error("external filter %s failed %d", params->cmd, status);
357 return (write_err || status);
360 static int apply_filter(const char *path, const char *src, size_t len,
361 struct strbuf *dst, const char *cmd)
364 * Create a pipeline to have the command filter the buffer's
365 * contents.
367 * (child --> cmd) --> us
369 int ret = 1;
370 struct strbuf nbuf = STRBUF_INIT;
371 struct async async;
372 struct filter_params params;
374 if (!cmd)
375 return 0;
377 memset(&async, 0, sizeof(async));
378 async.proc = filter_buffer;
379 async.data = &params;
380 async.out = -1;
381 params.src = src;
382 params.size = len;
383 params.cmd = cmd;
385 fflush(NULL);
386 if (start_async(&async))
387 return 0; /* error was already reported */
389 if (strbuf_read(&nbuf, async.out, len) < 0) {
390 error("read from external filter %s failed", cmd);
391 ret = 0;
393 if (close(async.out)) {
394 error("read from external filter %s failed", cmd);
395 ret = 0;
397 if (finish_async(&async)) {
398 error("external filter %s failed", cmd);
399 ret = 0;
402 if (ret) {
403 strbuf_swap(dst, &nbuf);
405 strbuf_release(&nbuf);
406 return ret;
409 static struct convert_driver {
410 const char *name;
411 struct convert_driver *next;
412 const char *smudge;
413 const char *clean;
414 } *user_convert, **user_convert_tail;
416 static int read_convert_config(const char *var, const char *value, void *cb)
418 const char *ep, *name;
419 int namelen;
420 struct convert_driver *drv;
423 * External conversion drivers are configured using
424 * "filter.<name>.variable".
426 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
427 return 0;
428 name = var + 7;
429 namelen = ep - name;
430 for (drv = user_convert; drv; drv = drv->next)
431 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
432 break;
433 if (!drv) {
434 drv = xcalloc(1, sizeof(struct convert_driver));
435 drv->name = xmemdupz(name, namelen);
436 *user_convert_tail = drv;
437 user_convert_tail = &(drv->next);
440 ep++;
443 * filter.<name>.smudge and filter.<name>.clean specifies
444 * the command line:
446 * command-line
448 * The command-line will not be interpolated in any way.
451 if (!strcmp("smudge", ep))
452 return git_config_string(&drv->smudge, var, value);
454 if (!strcmp("clean", ep))
455 return git_config_string(&drv->clean, var, value);
457 return 0;
460 static void setup_convert_check(struct git_attr_check *check)
462 static struct git_attr *attr_text;
463 static struct git_attr *attr_crlf;
464 static struct git_attr *attr_eol;
465 static struct git_attr *attr_ident;
466 static struct git_attr *attr_filter;
468 if (!attr_text) {
469 attr_text = git_attr("text");
470 attr_crlf = git_attr("crlf");
471 attr_eol = git_attr("eol");
472 attr_ident = git_attr("ident");
473 attr_filter = git_attr("filter");
474 user_convert_tail = &user_convert;
475 git_config(read_convert_config, NULL);
477 check[0].attr = attr_crlf;
478 check[1].attr = attr_ident;
479 check[2].attr = attr_filter;
480 check[3].attr = attr_eol;
481 check[4].attr = attr_text;
484 static int count_ident(const char *cp, unsigned long size)
487 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
489 int cnt = 0;
490 char ch;
492 while (size) {
493 ch = *cp++;
494 size--;
495 if (ch != '$')
496 continue;
497 if (size < 3)
498 break;
499 if (memcmp("Id", cp, 2))
500 continue;
501 ch = cp[2];
502 cp += 3;
503 size -= 3;
504 if (ch == '$')
505 cnt++; /* $Id$ */
506 if (ch != ':')
507 continue;
510 * "$Id: ... "; scan up to the closing dollar sign and discard.
512 while (size) {
513 ch = *cp++;
514 size--;
515 if (ch == '$') {
516 cnt++;
517 break;
519 if (ch == '\n')
520 break;
523 return cnt;
526 static int ident_to_git(const char *path, const char *src, size_t len,
527 struct strbuf *buf, int ident)
529 char *dst, *dollar;
531 if (!ident || !count_ident(src, len))
532 return 0;
534 /* only grow if not in place */
535 if (strbuf_avail(buf) + buf->len < len)
536 strbuf_grow(buf, len - buf->len);
537 dst = buf->buf;
538 for (;;) {
539 dollar = memchr(src, '$', len);
540 if (!dollar)
541 break;
542 memcpy(dst, src, dollar + 1 - src);
543 dst += dollar + 1 - src;
544 len -= dollar + 1 - src;
545 src = dollar + 1;
547 if (len > 3 && !memcmp(src, "Id:", 3)) {
548 dollar = memchr(src + 3, '$', len - 3);
549 if (!dollar)
550 break;
551 if (memchr(src + 3, '\n', dollar - src - 3)) {
552 /* Line break before the next dollar. */
553 continue;
556 memcpy(dst, "Id$", 3);
557 dst += 3;
558 len -= dollar + 1 - src;
559 src = dollar + 1;
562 memcpy(dst, src, len);
563 strbuf_setlen(buf, dst + len - buf->buf);
564 return 1;
567 static int ident_to_worktree(const char *path, const char *src, size_t len,
568 struct strbuf *buf, int ident)
570 unsigned char sha1[20];
571 char *to_free = NULL, *dollar, *spc;
572 int cnt;
574 if (!ident)
575 return 0;
577 cnt = count_ident(src, len);
578 if (!cnt)
579 return 0;
581 /* are we "faking" in place editing ? */
582 if (src == buf->buf)
583 to_free = strbuf_detach(buf, NULL);
584 hash_sha1_file(src, len, "blob", sha1);
586 strbuf_grow(buf, len + cnt * 43);
587 for (;;) {
588 /* step 1: run to the next '$' */
589 dollar = memchr(src, '$', len);
590 if (!dollar)
591 break;
592 strbuf_add(buf, src, dollar + 1 - src);
593 len -= dollar + 1 - src;
594 src = dollar + 1;
596 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
597 if (len < 3 || memcmp("Id", src, 2))
598 continue;
600 /* step 3: skip over Id$ or Id:xxxxx$ */
601 if (src[2] == '$') {
602 src += 3;
603 len -= 3;
604 } else if (src[2] == ':') {
606 * It's possible that an expanded Id has crept its way into the
607 * repository, we cope with that by stripping the expansion out.
608 * This is probably not a good idea, since it will cause changes
609 * on checkout, which won't go away by stash, but let's keep it
610 * for git-style ids.
612 dollar = memchr(src + 3, '$', len - 3);
613 if (!dollar) {
614 /* incomplete keyword, no more '$', so just quit the loop */
615 break;
618 if (memchr(src + 3, '\n', dollar - src - 3)) {
619 /* Line break before the next dollar. */
620 continue;
623 spc = memchr(src + 4, ' ', dollar - src - 4);
624 if (spc && spc < dollar-1) {
625 /* There are spaces in unexpected places.
626 * This is probably an id from some other
627 * versioning system. Keep it for now.
629 continue;
632 len -= dollar + 1 - src;
633 src = dollar + 1;
634 } else {
635 /* it wasn't a "Id$" or "Id:xxxx$" */
636 continue;
639 /* step 4: substitute */
640 strbuf_addstr(buf, "Id: ");
641 strbuf_add(buf, sha1_to_hex(sha1), 40);
642 strbuf_addstr(buf, " $");
644 strbuf_add(buf, src, len);
646 free(to_free);
647 return 1;
650 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
652 const char *value = check->value;
654 if (ATTR_TRUE(value))
655 return CRLF_TEXT;
656 else if (ATTR_FALSE(value))
657 return CRLF_BINARY;
658 else if (ATTR_UNSET(value))
660 else if (!strcmp(value, "input"))
661 return CRLF_INPUT;
662 else if (!strcmp(value, "auto"))
663 return CRLF_AUTO;
664 return CRLF_GUESS;
667 static int git_path_check_eol(const char *path, struct git_attr_check *check)
669 const char *value = check->value;
671 if (ATTR_UNSET(value))
673 else if (!strcmp(value, "lf"))
674 return EOL_LF;
675 else if (!strcmp(value, "crlf"))
676 return EOL_CRLF;
677 return EOL_UNSET;
680 static struct convert_driver *git_path_check_convert(const char *path,
681 struct git_attr_check *check)
683 const char *value = check->value;
684 struct convert_driver *drv;
686 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
687 return NULL;
688 for (drv = user_convert; drv; drv = drv->next)
689 if (!strcmp(value, drv->name))
690 return drv;
691 return NULL;
694 static int git_path_check_ident(const char *path, struct git_attr_check *check)
696 const char *value = check->value;
698 return !!ATTR_TRUE(value);
701 enum action determine_action(enum action text_attr, enum eol eol_attr) {
702 if (text_attr == CRLF_BINARY)
703 return CRLF_BINARY;
704 if (eol_attr == EOL_LF)
705 return CRLF_INPUT;
706 if (eol_attr == EOL_CRLF)
707 return CRLF_CRLF;
708 return text_attr;
711 int convert_to_git(const char *path, const char *src, size_t len,
712 struct strbuf *dst, enum safe_crlf checksafe)
714 struct git_attr_check check[5];
715 enum action action = CRLF_GUESS;
716 enum eol eol_attr = EOL_UNSET;
717 int ident = 0, ret = 0;
718 const char *filter = NULL;
720 setup_convert_check(check);
721 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
722 struct convert_driver *drv;
723 action = git_path_check_crlf(path, check + 4);
724 if (action == CRLF_GUESS)
725 action = git_path_check_crlf(path, check + 0);
726 ident = git_path_check_ident(path, check + 1);
727 drv = git_path_check_convert(path, check + 2);
728 eol_attr = git_path_check_eol(path, check + 3);
729 if (drv && drv->clean)
730 filter = drv->clean;
733 ret |= apply_filter(path, src, len, dst, filter);
734 if (ret) {
735 src = dst->buf;
736 len = dst->len;
738 action = determine_action(action, eol_attr);
739 ret |= crlf_to_git(path, src, len, dst, action, checksafe);
740 if (ret) {
741 src = dst->buf;
742 len = dst->len;
744 return ret | ident_to_git(path, src, len, dst, ident);
747 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
749 struct git_attr_check check[5];
750 enum action action = CRLF_GUESS;
751 enum eol eol_attr = EOL_UNSET;
752 int ident = 0, ret = 0;
753 const char *filter = NULL;
755 setup_convert_check(check);
756 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
757 struct convert_driver *drv;
758 action = git_path_check_crlf(path, check + 4);
759 if (action == CRLF_GUESS)
760 action = git_path_check_crlf(path, check + 0);
761 ident = git_path_check_ident(path, check + 1);
762 drv = git_path_check_convert(path, check + 2);
763 eol_attr = git_path_check_eol(path, check + 3);
764 if (drv && drv->smudge)
765 filter = drv->smudge;
768 ret |= ident_to_worktree(path, src, len, dst, ident);
769 if (ret) {
770 src = dst->buf;
771 len = dst->len;
773 action = determine_action(action, eol_attr);
774 ret |= crlf_to_worktree(path, src, len, dst, action);
775 if (ret) {
776 src = dst->buf;
777 len = dst->len;
779 return ret | apply_filter(path, src, len, dst, filter);