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 "text" attribute or "auto_crlf" option is set.
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
)
35 memset(stats
, 0, sizeof(*stats
));
37 for (i
= 0; i
< size
; i
++) {
38 unsigned char c
= buf
[i
];
41 if (i
+1 < size
&& buf
[i
+1] == '\n')
51 stats
->nonprintable
++;
54 /* BS, HT, ESC and FF */
55 case '\b': case '\t': case '\033': case '\014':
62 stats
->nonprintable
++;
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
)
82 if ((stats
->printable
>> 7) < stats
->nonprintable
)
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.
96 static enum eol
determine_output_conversion(enum action action
)
111 if (auto_crlf
== AUTO_CRLF_TRUE
)
113 else if (auto_crlf
== AUTO_CRLF_INPUT
)
115 else if (eol
== EOL_UNSET
)
121 static void check_safe_crlf(const char *path
, enum action action
,
122 struct text_stat
*stats
, enum safe_crlf checksafe
)
127 if (determine_output_conversion(action
) == EOL_LF
) {
129 * CRLFs would not be restored by checkout:
130 * check if we'd remove CRLFs
133 if (checksafe
== SAFE_CRLF_WARN
)
134 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path
);
135 else /* i.e. SAFE_CRLF_FAIL */
136 die("CRLF would be replaced by LF in %s.", path
);
138 } else if (determine_output_conversion(action
) == EOL_CRLF
) {
140 * CRLFs would be added by checkout:
141 * check if we have "naked" LFs
143 if (stats
->lf
!= stats
->crlf
) {
144 if (checksafe
== SAFE_CRLF_WARN
)
145 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path
);
146 else /* i.e. SAFE_CRLF_FAIL */
147 die("LF would be replaced by CRLF in %s", path
);
152 static int has_cr_in_index(const char *path
)
156 enum object_type type
;
159 struct index_state
*istate
= &the_index
;
162 pos
= index_name_pos(istate
, path
, len
);
165 * We might be in the middle of a merge, in which
166 * case we would read stage #2 (ours).
170 (pos
< 0 && i
< istate
->cache_nr
&&
171 !strcmp(istate
->cache
[i
]->name
, path
));
173 if (ce_stage(istate
->cache
[i
]) == 2)
178 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
179 if (!data
|| type
!= OBJ_BLOB
) {
184 has_cr
= memchr(data
, '\r', sz
) != NULL
;
189 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
190 struct strbuf
*buf
, enum action action
, enum safe_crlf checksafe
)
192 struct text_stat stats
;
195 if (action
== CRLF_BINARY
||
196 (action
== CRLF_GUESS
&& auto_crlf
== AUTO_CRLF_FALSE
) || !len
)
199 gather_stats(src
, len
, &stats
);
201 if (action
== CRLF_AUTO
|| action
== CRLF_GUESS
) {
203 * We're currently not going to even try to convert stuff
204 * that has bare CR characters. Does anybody do that crazy
207 if (stats
.cr
!= stats
.crlf
)
211 * And add some heuristics for binary vs text, of course...
213 if (is_binary(len
, &stats
))
216 if (action
== CRLF_GUESS
) {
218 * If the file in the index has any CR in it, do not convert.
219 * This is the new safer autocrlf handling.
221 if (has_cr_in_index(path
))
226 check_safe_crlf(path
, action
, &stats
, checksafe
);
228 /* Optimization: No CR? Nothing to convert, regardless. */
232 /* only grow if not in place */
233 if (strbuf_avail(buf
) + buf
->len
< len
)
234 strbuf_grow(buf
, len
- buf
->len
);
236 if (action
== CRLF_AUTO
|| action
== CRLF_GUESS
) {
238 * If we guessed, we already know we rejected a file with
239 * lone CR, and we can strip a CR without looking at what
243 unsigned char c
= *src
++;
249 unsigned char c
= *src
++;
250 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
254 strbuf_setlen(buf
, dst
- buf
->buf
);
258 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
259 struct strbuf
*buf
, enum action action
)
261 char *to_free
= NULL
;
262 struct text_stat stats
;
264 if (!len
|| determine_output_conversion(action
) != EOL_CRLF
)
267 gather_stats(src
, len
, &stats
);
269 /* No LF? Nothing to convert, regardless. */
273 /* Was it already in CRLF format? */
274 if (stats
.lf
== stats
.crlf
)
277 if (action
== CRLF_AUTO
|| action
== CRLF_GUESS
) {
278 if (action
== CRLF_GUESS
) {
279 /* If we have any CR or CRLF line endings, we do not touch it */
280 /* This is the new safer autocrlf-handling */
281 if (stats
.cr
> 0 || stats
.crlf
> 0)
285 /* If we have any bare CR characters, we're not going to touch it */
286 if (stats
.cr
!= stats
.crlf
)
289 if (is_binary(len
, &stats
))
293 /* are we "faking" in place editing ? */
295 to_free
= strbuf_detach(buf
, NULL
);
297 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
299 const char *nl
= memchr(src
, '\n', len
);
302 if (nl
> src
&& nl
[-1] == '\r') {
303 strbuf_add(buf
, src
, nl
+ 1 - src
);
305 strbuf_add(buf
, src
, nl
- src
);
306 strbuf_addstr(buf
, "\r\n");
311 strbuf_add(buf
, src
, len
);
317 struct filter_params
{
323 static int filter_buffer(int in
, int out
, void *data
)
326 * Spawn cmd and feed the buffer contents through its stdin.
328 struct child_process child_process
;
329 struct filter_params
*params
= (struct filter_params
*)data
;
330 int write_err
, status
;
331 const char *argv
[] = { NULL
, NULL
};
333 argv
[0] = params
->cmd
;
335 memset(&child_process
, 0, sizeof(child_process
));
336 child_process
.argv
= argv
;
337 child_process
.use_shell
= 1;
338 child_process
.in
= -1;
339 child_process
.out
= out
;
341 if (start_command(&child_process
))
342 return error("cannot fork to run external filter %s", params
->cmd
);
344 write_err
= (write_in_full(child_process
.in
, params
->src
, params
->size
) < 0);
345 if (close(child_process
.in
))
348 error("cannot feed the input to external filter %s", params
->cmd
);
350 status
= finish_command(&child_process
);
352 error("external filter %s failed %d", params
->cmd
, status
);
353 return (write_err
|| status
);
356 static int apply_filter(const char *path
, const char *src
, size_t len
,
357 struct strbuf
*dst
, const char *cmd
)
360 * Create a pipeline to have the command filter the buffer's
363 * (child --> cmd) --> us
366 struct strbuf nbuf
= STRBUF_INIT
;
368 struct filter_params params
;
373 memset(&async
, 0, sizeof(async
));
374 async
.proc
= filter_buffer
;
375 async
.data
= ¶ms
;
382 if (start_async(&async
))
383 return 0; /* error was already reported */
385 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
386 error("read from external filter %s failed", cmd
);
389 if (close(async
.out
)) {
390 error("read from external filter %s failed", cmd
);
393 if (finish_async(&async
)) {
394 error("external filter %s failed", cmd
);
399 strbuf_swap(dst
, &nbuf
);
401 strbuf_release(&nbuf
);
405 static struct convert_driver
{
407 struct convert_driver
*next
;
410 } *user_convert
, **user_convert_tail
;
412 static int read_convert_config(const char *var
, const char *value
, void *cb
)
414 const char *ep
, *name
;
416 struct convert_driver
*drv
;
419 * External conversion drivers are configured using
420 * "filter.<name>.variable".
422 if (prefixcmp(var
, "filter.") || (ep
= strrchr(var
, '.')) == var
+ 6)
426 for (drv
= user_convert
; drv
; drv
= drv
->next
)
427 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
430 drv
= xcalloc(1, sizeof(struct convert_driver
));
431 drv
->name
= xmemdupz(name
, namelen
);
432 *user_convert_tail
= drv
;
433 user_convert_tail
= &(drv
->next
);
439 * filter.<name>.smudge and filter.<name>.clean specifies
444 * The command-line will not be interpolated in any way.
447 if (!strcmp("smudge", ep
))
448 return git_config_string(&drv
->smudge
, var
, value
);
450 if (!strcmp("clean", ep
))
451 return git_config_string(&drv
->clean
, var
, value
);
456 static void setup_convert_check(struct git_attr_check
*check
)
458 static struct git_attr
*attr_text
;
459 static struct git_attr
*attr_crlf
;
460 static struct git_attr
*attr_eol
;
461 static struct git_attr
*attr_ident
;
462 static struct git_attr
*attr_filter
;
465 attr_text
= git_attr("text");
466 attr_crlf
= git_attr("crlf");
467 attr_eol
= git_attr("eol");
468 attr_ident
= git_attr("ident");
469 attr_filter
= git_attr("filter");
470 user_convert_tail
= &user_convert
;
471 git_config(read_convert_config
, NULL
);
473 check
[0].attr
= attr_crlf
;
474 check
[1].attr
= attr_ident
;
475 check
[2].attr
= attr_filter
;
476 check
[3].attr
= attr_eol
;
477 check
[4].attr
= attr_text
;
480 static int count_ident(const char *cp
, unsigned long size
)
483 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
495 if (memcmp("Id", cp
, 2))
506 * "$Id: ... "; scan up to the closing dollar sign and discard.
522 static int ident_to_git(const char *path
, const char *src
, size_t len
,
523 struct strbuf
*buf
, int ident
)
527 if (!ident
|| !count_ident(src
, len
))
530 /* only grow if not in place */
531 if (strbuf_avail(buf
) + buf
->len
< len
)
532 strbuf_grow(buf
, len
- buf
->len
);
535 dollar
= memchr(src
, '$', len
);
538 memcpy(dst
, src
, dollar
+ 1 - src
);
539 dst
+= dollar
+ 1 - src
;
540 len
-= dollar
+ 1 - src
;
543 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
544 dollar
= memchr(src
+ 3, '$', len
- 3);
547 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
548 /* Line break before the next dollar. */
552 memcpy(dst
, "Id$", 3);
554 len
-= dollar
+ 1 - src
;
558 memcpy(dst
, src
, len
);
559 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
563 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
564 struct strbuf
*buf
, int ident
)
566 unsigned char sha1
[20];
567 char *to_free
= NULL
, *dollar
, *spc
;
573 cnt
= count_ident(src
, len
);
577 /* are we "faking" in place editing ? */
579 to_free
= strbuf_detach(buf
, NULL
);
580 hash_sha1_file(src
, len
, "blob", sha1
);
582 strbuf_grow(buf
, len
+ cnt
* 43);
584 /* step 1: run to the next '$' */
585 dollar
= memchr(src
, '$', len
);
588 strbuf_add(buf
, src
, dollar
+ 1 - src
);
589 len
-= dollar
+ 1 - src
;
592 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
593 if (len
< 3 || memcmp("Id", src
, 2))
596 /* step 3: skip over Id$ or Id:xxxxx$ */
600 } else if (src
[2] == ':') {
602 * It's possible that an expanded Id has crept its way into the
603 * repository, we cope with that by stripping the expansion out.
604 * This is probably not a good idea, since it will cause changes
605 * on checkout, which won't go away by stash, but let's keep it
608 dollar
= memchr(src
+ 3, '$', len
- 3);
610 /* incomplete keyword, no more '$', so just quit the loop */
614 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
615 /* Line break before the next dollar. */
619 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
620 if (spc
&& spc
< dollar
-1) {
621 /* There are spaces in unexpected places.
622 * This is probably an id from some other
623 * versioning system. Keep it for now.
628 len
-= dollar
+ 1 - src
;
631 /* it wasn't a "Id$" or "Id:xxxx$" */
635 /* step 4: substitute */
636 strbuf_addstr(buf
, "Id: ");
637 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
638 strbuf_addstr(buf
, " $");
640 strbuf_add(buf
, src
, len
);
646 static int git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
648 const char *value
= check
->value
;
650 if (ATTR_TRUE(value
))
652 else if (ATTR_FALSE(value
))
654 else if (ATTR_UNSET(value
))
656 else if (!strcmp(value
, "input"))
658 else if (!strcmp(value
, "auto"))
663 static int git_path_check_eol(const char *path
, struct git_attr_check
*check
)
665 const char *value
= check
->value
;
667 if (ATTR_UNSET(value
))
669 else if (!strcmp(value
, "lf"))
671 else if (!strcmp(value
, "crlf"))
676 static struct convert_driver
*git_path_check_convert(const char *path
,
677 struct git_attr_check
*check
)
679 const char *value
= check
->value
;
680 struct convert_driver
*drv
;
682 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
684 for (drv
= user_convert
; drv
; drv
= drv
->next
)
685 if (!strcmp(value
, drv
->name
))
690 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
692 const char *value
= check
->value
;
694 return !!ATTR_TRUE(value
);
697 static enum action
determine_action(enum action text_attr
, enum eol eol_attr
)
699 if (text_attr
== CRLF_BINARY
)
701 if (eol_attr
== EOL_LF
)
703 if (eol_attr
== EOL_CRLF
)
708 int convert_to_git(const char *path
, const char *src
, size_t len
,
709 struct strbuf
*dst
, enum safe_crlf checksafe
)
711 struct git_attr_check check
[5];
712 enum action action
= CRLF_GUESS
;
713 enum eol eol_attr
= EOL_UNSET
;
714 int ident
= 0, ret
= 0;
715 const char *filter
= NULL
;
717 setup_convert_check(check
);
718 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
719 struct convert_driver
*drv
;
720 action
= git_path_check_crlf(path
, check
+ 4);
721 if (action
== CRLF_GUESS
)
722 action
= git_path_check_crlf(path
, check
+ 0);
723 ident
= git_path_check_ident(path
, check
+ 1);
724 drv
= git_path_check_convert(path
, check
+ 2);
725 eol_attr
= git_path_check_eol(path
, check
+ 3);
726 if (drv
&& drv
->clean
)
730 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
735 action
= determine_action(action
, eol_attr
);
736 ret
|= crlf_to_git(path
, src
, len
, dst
, action
, checksafe
);
741 return ret
| ident_to_git(path
, src
, len
, dst
, ident
);
744 static int convert_to_working_tree_internal(const char *path
, const char *src
,
745 size_t len
, struct strbuf
*dst
,
748 struct git_attr_check check
[5];
749 enum action action
= CRLF_GUESS
;
750 enum eol eol_attr
= EOL_UNSET
;
751 int ident
= 0, ret
= 0;
752 const char *filter
= NULL
;
754 setup_convert_check(check
);
755 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
756 struct convert_driver
*drv
;
757 action
= git_path_check_crlf(path
, check
+ 4);
758 if (action
== CRLF_GUESS
)
759 action
= git_path_check_crlf(path
, check
+ 0);
760 ident
= git_path_check_ident(path
, check
+ 1);
761 drv
= git_path_check_convert(path
, check
+ 2);
762 eol_attr
= git_path_check_eol(path
, check
+ 3);
763 if (drv
&& drv
->smudge
)
764 filter
= drv
->smudge
;
767 ret
|= ident_to_worktree(path
, src
, len
, dst
, ident
);
773 * CRLF conversion can be skipped if normalizing, unless there
774 * is a smudge filter. The filter might expect CRLFs.
776 if (filter
|| !normalizing
) {
777 action
= determine_action(action
, eol_attr
);
778 ret
|= crlf_to_worktree(path
, src
, len
, dst
, action
);
784 return ret
| apply_filter(path
, src
, len
, dst
, filter
);
787 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
789 return convert_to_working_tree_internal(path
, src
, len
, dst
, 0);
792 int renormalize_buffer(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
794 int ret
= convert_to_working_tree_internal(path
, src
, len
, dst
, 1);
799 return ret
| convert_to_git(path
, src
, len
, dst
, 0);