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 "crlf" 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
get_output_conversion(enum action action
) {
105 if (auto_crlf
== AUTO_CRLF_FALSE
)
110 if (eol
== EOL_UNSET
) {
111 if (auto_crlf
== AUTO_CRLF_FALSE
)
113 else if (auto_crlf
== AUTO_CRLF_TRUE
)
122 static void check_safe_crlf(const char *path
, enum action action
,
123 struct text_stat
*stats
, enum safe_crlf checksafe
)
128 if (get_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.", 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
)
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
;
268 if (get_output_conversion(action
) != EOL_CRLF
)
271 gather_stats(src
, len
, &stats
);
273 /* No LF? Nothing to convert, regardless. */
277 /* Was it already in CRLF format? */
278 if (stats
.lf
== stats
.crlf
)
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)
289 /* If we have any bare CR characters, we're not going to touch it */
290 if (stats
.cr
!= stats
.crlf
)
293 if (is_binary(len
, &stats
))
297 /* are we "faking" in place editing ? */
299 to_free
= strbuf_detach(buf
, NULL
);
301 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
303 const char *nl
= memchr(src
, '\n', len
);
306 if (nl
> src
&& nl
[-1] == '\r') {
307 strbuf_add(buf
, src
, nl
+ 1 - src
);
309 strbuf_add(buf
, src
, nl
- src
);
310 strbuf_addstr(buf
, "\r\n");
315 strbuf_add(buf
, src
, len
);
321 struct filter_params
{
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
))
352 error("cannot feed the input to external filter %s", params
->cmd
);
354 status
= finish_command(&child_process
);
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
367 * (child --> cmd) --> us
370 struct strbuf nbuf
= STRBUF_INIT
;
372 struct filter_params params
;
377 memset(&async
, 0, sizeof(async
));
378 async
.proc
= filter_buffer
;
379 async
.data
= ¶ms
;
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
);
393 if (close(async
.out
)) {
394 error("read from external filter %s failed", cmd
);
397 if (finish_async(&async
)) {
398 error("external filter %s failed", cmd
);
403 strbuf_swap(dst
, &nbuf
);
405 strbuf_release(&nbuf
);
409 static struct convert_driver
{
411 struct convert_driver
*next
;
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
;
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)
430 for (drv
= user_convert
; drv
; drv
= drv
->next
)
431 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
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
);
443 * filter.<name>.smudge and filter.<name>.clean specifies
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
);
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
;
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$"
499 if (memcmp("Id", cp
, 2))
510 * "$Id: ... "; scan up to the closing dollar sign and discard.
526 static int ident_to_git(const char *path
, const char *src
, size_t len
,
527 struct strbuf
*buf
, int ident
)
531 if (!ident
|| !count_ident(src
, len
))
534 /* only grow if not in place */
535 if (strbuf_avail(buf
) + buf
->len
< len
)
536 strbuf_grow(buf
, len
- buf
->len
);
539 dollar
= memchr(src
, '$', len
);
542 memcpy(dst
, src
, dollar
+ 1 - src
);
543 dst
+= dollar
+ 1 - src
;
544 len
-= dollar
+ 1 - src
;
547 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
548 dollar
= memchr(src
+ 3, '$', len
- 3);
551 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
552 /* Line break before the next dollar. */
556 memcpy(dst
, "Id$", 3);
558 len
-= dollar
+ 1 - src
;
562 memcpy(dst
, src
, len
);
563 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
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
;
577 cnt
= count_ident(src
, len
);
581 /* are we "faking" in place editing ? */
583 to_free
= strbuf_detach(buf
, NULL
);
584 hash_sha1_file(src
, len
, "blob", sha1
);
586 strbuf_grow(buf
, len
+ cnt
* 43);
588 /* step 1: run to the next '$' */
589 dollar
= memchr(src
, '$', len
);
592 strbuf_add(buf
, src
, dollar
+ 1 - src
);
593 len
-= dollar
+ 1 - src
;
596 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
597 if (len
< 3 || memcmp("Id", src
, 2))
600 /* step 3: skip over Id$ or Id:xxxxx$ */
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
612 dollar
= memchr(src
+ 3, '$', len
- 3);
614 /* incomplete keyword, no more '$', so just quit the loop */
618 if (memchr(src
+ 3, '\n', dollar
- src
- 3)) {
619 /* Line break before the next dollar. */
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.
632 len
-= dollar
+ 1 - src
;
635 /* it wasn't a "Id$" or "Id:xxxx$" */
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
);
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
))
656 else if (ATTR_FALSE(value
))
658 else if (ATTR_UNSET(value
))
660 else if (!strcmp(value
, "input"))
662 else if (!strcmp(value
, "auto"))
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"))
675 else if (!strcmp(value
, "crlf"))
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
))
688 for (drv
= user_convert
; drv
; drv
= drv
->next
)
689 if (!strcmp(value
, drv
->name
))
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
)
704 if (eol_attr
== EOL_LF
)
706 if (eol_attr
== EOL_CRLF
)
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
)
733 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
738 action
= determine_action(action
, eol_attr
);
739 ret
|= crlf_to_git(path
, src
, len
, dst
, action
, checksafe
);
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
);
773 action
= determine_action(action
, eol_attr
);
774 ret
|= crlf_to_worktree(path
, src
, len
, dst
, action
);
779 return ret
| apply_filter(path
, src
, len
, dst
, filter
);