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
) {
110 if (auto_crlf
== AUTO_CRLF_TRUE
)
112 else if (auto_crlf
== AUTO_CRLF_INPUT
)
114 else if (eol
== EOL_UNSET
)
120 static void check_safe_crlf(const char *path
, enum action action
,
121 struct text_stat
*stats
, enum safe_crlf checksafe
)
126 if (determine_output_conversion(action
) == EOL_LF
) {
128 * CRLFs would not be restored by checkout:
129 * check if we'd remove CRLFs
132 if (checksafe
== SAFE_CRLF_WARN
)
133 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path
);
134 else /* i.e. SAFE_CRLF_FAIL */
135 die("CRLF would be replaced by LF in %s.", path
);
137 } else if (determine_output_conversion(action
) == EOL_CRLF
) {
139 * CRLFs would be added by checkout:
140 * check if we have "naked" LFs
142 if (stats
->lf
!= stats
->crlf
) {
143 if (checksafe
== SAFE_CRLF_WARN
)
144 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path
);
145 else /* i.e. SAFE_CRLF_FAIL */
146 die("LF would be replaced by CRLF in %s", path
);
151 static int has_cr_in_index(const char *path
)
155 enum object_type type
;
158 struct index_state
*istate
= &the_index
;
161 pos
= index_name_pos(istate
, path
, len
);
164 * We might be in the middle of a merge, in which
165 * case we would read stage #2 (ours).
169 (pos
< 0 && i
< istate
->cache_nr
&&
170 !strcmp(istate
->cache
[i
]->name
, path
));
172 if (ce_stage(istate
->cache
[i
]) == 2)
177 data
= read_sha1_file(istate
->cache
[pos
]->sha1
, &type
, &sz
);
178 if (!data
|| type
!= OBJ_BLOB
) {
183 has_cr
= memchr(data
, '\r', sz
) != NULL
;
188 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
189 struct strbuf
*buf
, enum action action
, enum safe_crlf checksafe
)
191 struct text_stat stats
;
194 if (action
== CRLF_BINARY
||
195 (action
== CRLF_GUESS
&& auto_crlf
== AUTO_CRLF_FALSE
) || !len
)
198 gather_stats(src
, len
, &stats
);
200 if (action
== CRLF_AUTO
|| action
== CRLF_GUESS
) {
202 * We're currently not going to even try to convert stuff
203 * that has bare CR characters. Does anybody do that crazy
206 if (stats
.cr
!= stats
.crlf
)
210 * And add some heuristics for binary vs text, of course...
212 if (is_binary(len
, &stats
))
215 if (action
== CRLF_GUESS
) {
217 * If the file in the index has any CR in it, do not convert.
218 * This is the new safer autocrlf handling.
220 if (has_cr_in_index(path
))
225 check_safe_crlf(path
, action
, &stats
, checksafe
);
227 /* Optimization: No CR? Nothing to convert, regardless. */
231 /* only grow if not in place */
232 if (strbuf_avail(buf
) + buf
->len
< len
)
233 strbuf_grow(buf
, len
- buf
->len
);
235 if (action
== CRLF_AUTO
|| action
== CRLF_GUESS
) {
237 * If we guessed, we already know we rejected a file with
238 * lone CR, and we can strip a CR without looking at what
242 unsigned char c
= *src
++;
248 unsigned char c
= *src
++;
249 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
253 strbuf_setlen(buf
, dst
- buf
->buf
);
257 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
258 struct strbuf
*buf
, enum action action
)
260 char *to_free
= NULL
;
261 struct text_stat stats
;
263 if (!len
|| determine_output_conversion(action
) != EOL_CRLF
)
266 gather_stats(src
, len
, &stats
);
268 /* No LF? Nothing to convert, regardless. */
272 /* Was it already in CRLF format? */
273 if (stats
.lf
== stats
.crlf
)
276 if (action
== CRLF_AUTO
|| action
== CRLF_GUESS
) {
277 if (action
== CRLF_GUESS
) {
278 /* If we have any CR or CRLF line endings, we do not touch it */
279 /* This is the new safer autocrlf-handling */
280 if (stats
.cr
> 0 || stats
.crlf
> 0)
284 /* If we have any bare CR characters, we're not going to touch it */
285 if (stats
.cr
!= stats
.crlf
)
288 if (is_binary(len
, &stats
))
292 /* are we "faking" in place editing ? */
294 to_free
= strbuf_detach(buf
, NULL
);
296 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
298 const char *nl
= memchr(src
, '\n', len
);
301 if (nl
> src
&& nl
[-1] == '\r') {
302 strbuf_add(buf
, src
, nl
+ 1 - src
);
304 strbuf_add(buf
, src
, nl
- src
);
305 strbuf_addstr(buf
, "\r\n");
310 strbuf_add(buf
, src
, len
);
316 struct filter_params
{
322 static int filter_buffer(int in
, int out
, void *data
)
325 * Spawn cmd and feed the buffer contents through its stdin.
327 struct child_process child_process
;
328 struct filter_params
*params
= (struct filter_params
*)data
;
329 int write_err
, status
;
330 const char *argv
[] = { NULL
, NULL
};
332 argv
[0] = params
->cmd
;
334 memset(&child_process
, 0, sizeof(child_process
));
335 child_process
.argv
= argv
;
336 child_process
.use_shell
= 1;
337 child_process
.in
= -1;
338 child_process
.out
= out
;
340 if (start_command(&child_process
))
341 return error("cannot fork to run external filter %s", params
->cmd
);
343 write_err
= (write_in_full(child_process
.in
, params
->src
, params
->size
) < 0);
344 if (close(child_process
.in
))
347 error("cannot feed the input to external filter %s", params
->cmd
);
349 status
= finish_command(&child_process
);
351 error("external filter %s failed %d", params
->cmd
, status
);
352 return (write_err
|| status
);
355 static int apply_filter(const char *path
, const char *src
, size_t len
,
356 struct strbuf
*dst
, const char *cmd
)
359 * Create a pipeline to have the command filter the buffer's
362 * (child --> cmd) --> us
365 struct strbuf nbuf
= STRBUF_INIT
;
367 struct filter_params params
;
372 memset(&async
, 0, sizeof(async
));
373 async
.proc
= filter_buffer
;
374 async
.data
= ¶ms
;
381 if (start_async(&async
))
382 return 0; /* error was already reported */
384 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
385 error("read from external filter %s failed", cmd
);
388 if (close(async
.out
)) {
389 error("read from external filter %s failed", cmd
);
392 if (finish_async(&async
)) {
393 error("external filter %s failed", cmd
);
398 strbuf_swap(dst
, &nbuf
);
400 strbuf_release(&nbuf
);
404 static struct convert_driver
{
406 struct convert_driver
*next
;
409 } *user_convert
, **user_convert_tail
;
411 static int read_convert_config(const char *var
, const char *value
, void *cb
)
413 const char *ep
, *name
;
415 struct convert_driver
*drv
;
418 * External conversion drivers are configured using
419 * "filter.<name>.variable".
421 if (prefixcmp(var
, "filter.") || (ep
= strrchr(var
, '.')) == var
+ 6)
425 for (drv
= user_convert
; drv
; drv
= drv
->next
)
426 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
429 drv
= xcalloc(1, sizeof(struct convert_driver
));
430 drv
->name
= xmemdupz(name
, namelen
);
431 *user_convert_tail
= drv
;
432 user_convert_tail
= &(drv
->next
);
438 * filter.<name>.smudge and filter.<name>.clean specifies
443 * The command-line will not be interpolated in any way.
446 if (!strcmp("smudge", ep
))
447 return git_config_string(&drv
->smudge
, var
, value
);
449 if (!strcmp("clean", ep
))
450 return git_config_string(&drv
->clean
, var
, value
);
455 static void setup_convert_check(struct git_attr_check
*check
)
457 static struct git_attr
*attr_text
;
458 static struct git_attr
*attr_crlf
;
459 static struct git_attr
*attr_eol
;
460 static struct git_attr
*attr_ident
;
461 static struct git_attr
*attr_filter
;
464 attr_text
= git_attr("text");
465 attr_crlf
= git_attr("crlf");
466 attr_eol
= git_attr("eol");
467 attr_ident
= git_attr("ident");
468 attr_filter
= git_attr("filter");
469 user_convert_tail
= &user_convert
;
470 git_config(read_convert_config
, NULL
);
472 check
[0].attr
= attr_crlf
;
473 check
[1].attr
= attr_ident
;
474 check
[2].attr
= attr_filter
;
475 check
[3].attr
= attr_eol
;
476 check
[4].attr
= attr_text
;
479 static int count_ident(const char *cp
, unsigned long size
)
482 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
494 if (memcmp("Id", cp
, 2))
505 * "$Id: ... "; scan up to the closing dollar sign and discard.
521 static int ident_to_git(const char *path
, const char *src
, size_t len
,
522 struct strbuf
*buf
, int ident
)
526 if (!ident
|| !count_ident(src
, len
))
529 /* only grow if not in place */
530 if (strbuf_avail(buf
) + buf
->len
< len
)
531 strbuf_grow(buf
, len
- buf
->len
);
534 dollar
= memchr(src
, '$', len
);
537 memcpy(dst
, src
, dollar
+ 1 - src
);
538 dst
+= dollar
+ 1 - src
;
539 len
-= dollar
+ 1 - src
;
542 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
543 dollar
= memchr(src
+ 3, '$', len
- 3);
546 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
547 /* Line break before the next dollar. */
551 memcpy(dst
, "Id$", 3);
553 len
-= dollar
+ 1 - src
;
557 memcpy(dst
, src
, len
);
558 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
562 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
563 struct strbuf
*buf
, int ident
)
565 unsigned char sha1
[20];
566 char *to_free
= NULL
, *dollar
, *spc
;
572 cnt
= count_ident(src
, len
);
576 /* are we "faking" in place editing ? */
578 to_free
= strbuf_detach(buf
, NULL
);
579 hash_sha1_file(src
, len
, "blob", sha1
);
581 strbuf_grow(buf
, len
+ cnt
* 43);
583 /* step 1: run to the next '$' */
584 dollar
= memchr(src
, '$', len
);
587 strbuf_add(buf
, src
, dollar
+ 1 - src
);
588 len
-= dollar
+ 1 - src
;
591 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
592 if (len
< 3 || memcmp("Id", src
, 2))
595 /* step 3: skip over Id$ or Id:xxxxx$ */
599 } else if (src
[2] == ':') {
601 * It's possible that an expanded Id has crept its way into the
602 * repository, we cope with that by stripping the expansion out.
603 * This is probably not a good idea, since it will cause changes
604 * on checkout, which won't go away by stash, but let's keep it
607 dollar
= memchr(src
+ 3, '$', len
- 3);
609 /* incomplete keyword, no more '$', so just quit the loop */
613 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
614 /* Line break before the next dollar. */
618 spc
= memchr(src
+ 4, ' ', dollar
- src
- 4);
619 if (spc
&& spc
< dollar
-1) {
620 /* There are spaces in unexpected places.
621 * This is probably an id from some other
622 * versioning system. Keep it for now.
627 len
-= dollar
+ 1 - src
;
630 /* it wasn't a "Id$" or "Id:xxxx$" */
634 /* step 4: substitute */
635 strbuf_addstr(buf
, "Id: ");
636 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
637 strbuf_addstr(buf
, " $");
639 strbuf_add(buf
, src
, len
);
645 static int git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
647 const char *value
= check
->value
;
649 if (ATTR_TRUE(value
))
651 else if (ATTR_FALSE(value
))
653 else if (ATTR_UNSET(value
))
655 else if (!strcmp(value
, "input"))
657 else if (!strcmp(value
, "auto"))
662 static int git_path_check_eol(const char *path
, struct git_attr_check
*check
)
664 const char *value
= check
->value
;
666 if (ATTR_UNSET(value
))
668 else if (!strcmp(value
, "lf"))
670 else if (!strcmp(value
, "crlf"))
675 static struct convert_driver
*git_path_check_convert(const char *path
,
676 struct git_attr_check
*check
)
678 const char *value
= check
->value
;
679 struct convert_driver
*drv
;
681 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
683 for (drv
= user_convert
; drv
; drv
= drv
->next
)
684 if (!strcmp(value
, drv
->name
))
689 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
691 const char *value
= check
->value
;
693 return !!ATTR_TRUE(value
);
696 enum action
determine_action(enum action text_attr
, enum eol eol_attr
) {
697 if (text_attr
== CRLF_BINARY
)
699 if (eol_attr
== EOL_LF
)
701 if (eol_attr
== EOL_CRLF
)
706 int convert_to_git(const char *path
, const char *src
, size_t len
,
707 struct strbuf
*dst
, enum safe_crlf checksafe
)
709 struct git_attr_check check
[5];
710 enum action action
= CRLF_GUESS
;
711 enum eol eol_attr
= EOL_UNSET
;
712 int ident
= 0, ret
= 0;
713 const char *filter
= NULL
;
715 setup_convert_check(check
);
716 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
717 struct convert_driver
*drv
;
718 action
= git_path_check_crlf(path
, check
+ 4);
719 if (action
== CRLF_GUESS
)
720 action
= git_path_check_crlf(path
, check
+ 0);
721 ident
= git_path_check_ident(path
, check
+ 1);
722 drv
= git_path_check_convert(path
, check
+ 2);
723 eol_attr
= git_path_check_eol(path
, check
+ 3);
724 if (drv
&& drv
->clean
)
728 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
733 action
= determine_action(action
, eol_attr
);
734 ret
|= crlf_to_git(path
, src
, len
, dst
, action
, checksafe
);
739 return ret
| ident_to_git(path
, src
, len
, dst
, ident
);
742 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
744 struct git_attr_check check
[5];
745 enum action action
= CRLF_GUESS
;
746 enum eol eol_attr
= EOL_UNSET
;
747 int ident
= 0, ret
= 0;
748 const char *filter
= NULL
;
750 setup_convert_check(check
);
751 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
752 struct convert_driver
*drv
;
753 action
= git_path_check_crlf(path
, check
+ 4);
754 if (action
== CRLF_GUESS
)
755 action
= git_path_check_crlf(path
, check
+ 0);
756 ident
= git_path_check_ident(path
, check
+ 1);
757 drv
= git_path_check_convert(path
, check
+ 2);
758 eol_attr
= git_path_check_eol(path
, check
+ 3);
759 if (drv
&& drv
->smudge
)
760 filter
= drv
->smudge
;
763 ret
|= ident_to_worktree(path
, src
, len
, dst
, ident
);
768 action
= determine_action(action
, eol_attr
);
769 ret
|= crlf_to_worktree(path
, src
, len
, dst
, action
);
774 return ret
| apply_filter(path
, src
, len
, dst
, filter
);