3 #include "run-command.h"
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.
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
)
36 memset(stats
, 0, sizeof(*stats
));
38 for (i
= 0; i
< size
; i
++) {
39 unsigned char c
= buf
[i
];
42 if (i
+1 < size
&& buf
[i
+1] == '\n')
52 stats
->nonprintable
++;
55 /* BS, HT, ESC and FF */
56 case '\b': case '\t': case '\033': case '\014':
63 stats
->nonprintable
++;
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
)
83 if ((stats
->printable
>> 7) < stats
->nonprintable
)
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.
97 static enum eol
determine_output_conversion(enum action action
)
112 if (auto_crlf
== AUTO_CRLF_TRUE
)
114 else if (auto_crlf
== AUTO_CRLF_INPUT
)
116 else if (eol
== EOL_UNSET
)
122 static void check_safe_crlf(const char *path
, enum action action
,
123 struct text_stat
*stats
, enum safe_crlf checksafe
)
128 if (determine_output_conversion(action
) == EOL_LF
) {
130 * CRLFs would not be restored by checkout:
131 * check if we'd remove CRLFs
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
)
157 enum object_type type
;
160 struct index_state
*istate
= &the_index
;
163 pos
= index_name_pos(istate
, path
, len
);
166 * We might be in the middle of a merge, in which
167 * case we would read stage #2 (ours).
171 (pos
< 0 && i
< istate
->cache_nr
&&
172 !strcmp(istate
->cache
[i
]->name
, path
));
174 if (ce_stage(istate
->cache
[i
]) == 2)
179 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
180 if (!data
|| type
!= OBJ_BLOB
) {
185 has_cr
= memchr(data
, '\r', sz
) != NULL
;
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
;
196 if (action
== CRLF_BINARY
||
197 (action
== CRLF_GUESS
&& auto_crlf
== AUTO_CRLF_FALSE
) || !len
)
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
208 if (stats
.cr
!= stats
.crlf
)
212 * And add some heuristics for binary vs text, of course...
214 if (is_binary(len
, &stats
))
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
))
227 check_safe_crlf(path
, action
, &stats
, checksafe
);
229 /* Optimization: No CR? Nothing to convert, regardless. */
233 /* only grow if not in place */
234 if (strbuf_avail(buf
) + buf
->len
< len
)
235 strbuf_grow(buf
, len
- buf
->len
);
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
244 unsigned char c
= *src
++;
250 unsigned char c
= *src
++;
251 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
255 strbuf_setlen(buf
, dst
- buf
->buf
);
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
)
268 gather_stats(src
, len
, &stats
);
270 /* No LF? Nothing to convert, regardless. */
274 /* Was it already in CRLF format? */
275 if (stats
.lf
== stats
.crlf
)
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)
286 /* If we have any bare CR characters, we're not going to touch it */
287 if (stats
.cr
!= stats
.crlf
)
290 if (is_binary(len
, &stats
))
294 /* are we "faking" in place editing ? */
296 to_free
= strbuf_detach(buf
, NULL
);
298 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
300 const char *nl
= memchr(src
, '\n', len
);
303 if (nl
> src
&& nl
[-1] == '\r') {
304 strbuf_add(buf
, src
, nl
+ 1 - src
);
306 strbuf_add(buf
, src
, nl
- src
);
307 strbuf_addstr(buf
, "\r\n");
312 strbuf_add(buf
, src
, len
);
318 struct filter_params
{
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
[] = {
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
);
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
))
366 error("cannot feed the input to external filter %s", params
->cmd
);
368 status
= finish_command(&child_process
);
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
383 * (child --> cmd) --> us
386 struct strbuf nbuf
= STRBUF_INIT
;
388 struct filter_params params
;
393 memset(&async
, 0, sizeof(async
));
394 async
.proc
= filter_buffer
;
395 async
.data
= ¶ms
;
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
);
410 if (close(async
.out
)) {
411 error("read from external filter %s failed", cmd
);
414 if (finish_async(&async
)) {
415 error("external filter %s failed", cmd
);
420 strbuf_swap(dst
, &nbuf
);
422 strbuf_release(&nbuf
);
426 static struct convert_driver
{
428 struct convert_driver
*next
;
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
;
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)
447 for (drv
= user_convert
; drv
; drv
= drv
->next
)
448 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
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
);
460 * filter.<name>.smudge and filter.<name>.clean specifies
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
);
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
;
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$"
516 if (memcmp("Id", cp
, 2))
527 * "$Id: ... "; scan up to the closing dollar sign and discard.
543 static int ident_to_git(const char *path
, const char *src
, size_t len
,
544 struct strbuf
*buf
, int ident
)
548 if (!ident
|| !count_ident(src
, len
))
551 /* only grow if not in place */
552 if (strbuf_avail(buf
) + buf
->len
< len
)
553 strbuf_grow(buf
, len
- buf
->len
);
556 dollar
= memchr(src
, '$', len
);
559 memcpy(dst
, src
, dollar
+ 1 - src
);
560 dst
+= dollar
+ 1 - src
;
561 len
-= dollar
+ 1 - src
;
564 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
565 dollar
= memchr(src
+ 3, '$', len
- 3);
568 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
569 /* Line break before the next dollar. */
573 memcpy(dst
, "Id$", 3);
575 len
-= dollar
+ 1 - src
;
579 memcpy(dst
, src
, len
);
580 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
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
;
594 cnt
= count_ident(src
, len
);
598 /* are we "faking" in place editing ? */
600 to_free
= strbuf_detach(buf
, NULL
);
601 hash_sha1_file(src
, len
, "blob", sha1
);
603 strbuf_grow(buf
, len
+ cnt
* 43);
605 /* step 1: run to the next '$' */
606 dollar
= memchr(src
, '$', len
);
609 strbuf_add(buf
, src
, dollar
+ 1 - src
);
610 len
-= dollar
+ 1 - src
;
613 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
614 if (len
< 3 || memcmp("Id", src
, 2))
617 /* step 3: skip over Id$ or Id:xxxxx$ */
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
629 dollar
= memchr(src
+ 3, '$', len
- 3);
631 /* incomplete keyword, no more '$', so just quit the loop */
635 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
636 /* Line break before the next dollar. */
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.
649 len
-= dollar
+ 1 - src
;
652 /* it wasn't a "Id$" or "Id:xxxx$" */
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
);
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
))
673 else if (ATTR_FALSE(value
))
675 else if (ATTR_UNSET(value
))
677 else if (!strcmp(value
, "input"))
679 else if (!strcmp(value
, "auto"))
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"))
692 else if (!strcmp(value
, "crlf"))
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
))
705 for (drv
= user_convert
; drv
; drv
= drv
->next
)
706 if (!strcmp(value
, drv
->name
))
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
)
722 if (eol_attr
== EOL_LF
)
724 if (eol_attr
== EOL_CRLF
)
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
)
751 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
756 action
= determine_action(action
, eol_attr
);
757 ret
|= crlf_to_git(path
, src
, len
, dst
, action
, checksafe
);
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
,
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
);
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
);
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);
820 return ret
| convert_to_git(path
, src
, len
, dst
, 0);