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 "auto_crlf" option is set.
14 #define CRLF_GUESS (-1)
20 /* CR, LF and CRLF counts */
21 unsigned cr
, lf
, crlf
;
23 /* These are just approximations! */
24 unsigned printable
, nonprintable
;
27 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
31 memset(stats
, 0, sizeof(*stats
));
33 for (i
= 0; i
< size
; i
++) {
34 unsigned char c
= buf
[i
];
37 if (i
+1 < size
&& buf
[i
+1] == '\n')
47 stats
->nonprintable
++;
50 /* BS, HT, ESC and FF */
51 case '\b': case '\t': case '\033': case '\014':
55 stats
->nonprintable
++;
62 // If file ends with EOF then don't count this EOF as non-printable
63 if ( size
>= 1 && buf
[size
-1] == '\032' )
64 stats
->nonprintable
--;
68 * The same heuristics as diff.c::mmfile_is_binary()
70 static int is_binary(unsigned long size
, struct text_stat
*stats
)
73 if ((stats
->printable
>> 7) < stats
->nonprintable
)
76 * Other heuristics? Average line length might be relevant,
77 * as might LF vs CR vs CRLF counts..
79 * NOTE! It might be normal to have a low ratio of CRLF to LF
80 * (somebody starts with a LF-only file and edits it with an editor
81 * that adds CRLF only to lines that are added..). But do we
82 * want to support CR-only? Probably not.
87 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
88 struct strbuf
*buf
, int action
)
90 struct text_stat stats
;
93 if ((action
== CRLF_BINARY
) || !auto_crlf
|| !len
)
96 gather_stats(src
, len
, &stats
);
97 /* No CR? Nothing to convert, regardless. */
101 if (action
== CRLF_GUESS
) {
103 * We're currently not going to even try to convert stuff
104 * that has bare CR characters. Does anybody do that crazy
107 if (stats
.cr
!= stats
.crlf
)
111 * And add some heuristics for binary vs text, of course...
113 if (is_binary(len
, &stats
))
117 /* only grow if not in place */
118 if (strbuf_avail(buf
) + buf
->len
< len
)
119 strbuf_grow(buf
, len
- buf
->len
);
121 if (action
== CRLF_GUESS
) {
123 * If we guessed, we already know we rejected a file with
124 * lone CR, and we can strip a CR without looking at what
128 unsigned char c
= *src
++;
134 unsigned char c
= *src
++;
135 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
139 strbuf_setlen(buf
, dst
- buf
->buf
);
143 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
144 struct strbuf
*buf
, int action
)
146 char *to_free
= NULL
;
147 struct text_stat stats
;
149 if ((action
== CRLF_BINARY
) || (action
== CRLF_INPUT
) ||
156 gather_stats(src
, len
, &stats
);
158 /* No LF? Nothing to convert, regardless. */
162 /* Was it already in CRLF format? */
163 if (stats
.lf
== stats
.crlf
)
166 if (action
== CRLF_GUESS
) {
167 /* If we have any bare CR characters, we're not going to touch it */
168 if (stats
.cr
!= stats
.crlf
)
171 if (is_binary(len
, &stats
))
175 /* are we "faking" in place editing ? */
177 to_free
= strbuf_detach(buf
, NULL
);
179 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
181 const char *nl
= memchr(src
, '\n', len
);
184 if (nl
> src
&& nl
[-1] == '\r') {
185 strbuf_add(buf
, src
, nl
+ 1 - src
);
187 strbuf_add(buf
, src
, nl
- src
);
188 strbuf_addstr(buf
, "\r\n");
193 strbuf_add(buf
, src
, len
);
199 struct filter_params
{
205 static int filter_buffer(int fd
, void *data
)
208 * Spawn cmd and feed the buffer contents through its stdin.
210 struct child_process child_process
;
211 struct filter_params
*params
= (struct filter_params
*)data
;
212 int write_err
, status
;
213 const char *argv
[] = { "sh", "-c", params
->cmd
, NULL
};
215 memset(&child_process
, 0, sizeof(child_process
));
216 child_process
.argv
= argv
;
217 child_process
.in
= -1;
218 child_process
.out
= fd
;
220 if (start_command(&child_process
))
221 return error("cannot fork to run external filter %s", params
->cmd
);
223 write_err
= (write_in_full(child_process
.in
, params
->src
, params
->size
) < 0);
224 if (close(child_process
.in
))
227 error("cannot feed the input to external filter %s", params
->cmd
);
229 status
= finish_command(&child_process
);
231 error("external filter %s failed %d", params
->cmd
, -status
);
232 return (write_err
|| status
);
235 static int apply_filter(const char *path
, const char *src
, size_t len
,
236 struct strbuf
*dst
, const char *cmd
)
239 * Create a pipeline to have the command filter the buffer's
242 * (child --> cmd) --> us
247 struct filter_params params
;
252 memset(&async
, 0, sizeof(async
));
253 async
.proc
= filter_buffer
;
254 async
.data
= ¶ms
;
260 if (start_async(&async
))
261 return 0; /* error was already reported */
263 strbuf_init(&nbuf
, 0);
264 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
265 error("read from external filter %s failed", cmd
);
268 if (close(async
.out
)) {
269 error("read from external filter %s failed", cmd
);
272 if (finish_async(&async
)) {
273 error("external filter %s failed", cmd
);
278 strbuf_swap(dst
, &nbuf
);
280 strbuf_release(&nbuf
);
284 static struct convert_driver
{
286 struct convert_driver
*next
;
289 } *user_convert
, **user_convert_tail
;
291 static int read_convert_config(const char *var
, const char *value
)
293 const char *ep
, *name
;
295 struct convert_driver
*drv
;
298 * External conversion drivers are configured using
299 * "filter.<name>.variable".
301 if (prefixcmp(var
, "filter.") || (ep
= strrchr(var
, '.')) == var
+ 6)
305 for (drv
= user_convert
; drv
; drv
= drv
->next
)
306 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
309 drv
= xcalloc(1, sizeof(struct convert_driver
));
310 drv
->name
= xmemdupz(name
, namelen
);
311 *user_convert_tail
= drv
;
312 user_convert_tail
= &(drv
->next
);
318 * filter.<name>.smudge and filter.<name>.clean specifies
323 * The command-line will not be interpolated in any way.
326 if (!strcmp("smudge", ep
)) {
328 return error("%s: lacks value", var
);
329 drv
->smudge
= strdup(value
);
333 if (!strcmp("clean", ep
)) {
335 return error("%s: lacks value", var
);
336 drv
->clean
= strdup(value
);
342 static void setup_convert_check(struct git_attr_check
*check
)
344 static struct git_attr
*attr_crlf
;
345 static struct git_attr
*attr_ident
;
346 static struct git_attr
*attr_filter
;
349 attr_crlf
= git_attr("crlf", 4);
350 attr_ident
= git_attr("ident", 5);
351 attr_filter
= git_attr("filter", 6);
352 user_convert_tail
= &user_convert
;
353 git_config(read_convert_config
);
355 check
[0].attr
= attr_crlf
;
356 check
[1].attr
= attr_ident
;
357 check
[2].attr
= attr_filter
;
360 static int count_ident(const char *cp
, unsigned long size
)
363 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
375 if (memcmp("Id", cp
, 2))
386 * "$Id: ... "; scan up to the closing dollar sign and discard.
400 static int ident_to_git(const char *path
, const char *src
, size_t len
,
401 struct strbuf
*buf
, int ident
)
405 if (!ident
|| !count_ident(src
, len
))
408 /* only grow if not in place */
409 if (strbuf_avail(buf
) + buf
->len
< len
)
410 strbuf_grow(buf
, len
- buf
->len
);
413 dollar
= memchr(src
, '$', len
);
416 memcpy(dst
, src
, dollar
+ 1 - src
);
417 dst
+= dollar
+ 1 - src
;
418 len
-= dollar
+ 1 - src
;
421 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
422 dollar
= memchr(src
+ 3, '$', len
- 3);
425 memcpy(dst
, "Id$", 3);
427 len
-= dollar
+ 1 - src
;
431 memcpy(dst
, src
, len
);
432 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
436 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
437 struct strbuf
*buf
, int ident
)
439 unsigned char sha1
[20];
440 char *to_free
= NULL
, *dollar
;
446 cnt
= count_ident(src
, len
);
450 /* are we "faking" in place editing ? */
452 to_free
= strbuf_detach(buf
, NULL
);
453 hash_sha1_file(src
, len
, "blob", sha1
);
455 strbuf_grow(buf
, len
+ cnt
* 43);
457 /* step 1: run to the next '$' */
458 dollar
= memchr(src
, '$', len
);
461 strbuf_add(buf
, src
, dollar
+ 1 - src
);
462 len
-= dollar
+ 1 - src
;
465 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
466 if (len
< 3 || memcmp("Id", src
, 2))
469 /* step 3: skip over Id$ or Id:xxxxx$ */
473 } else if (src
[2] == ':') {
475 * It's possible that an expanded Id has crept its way into the
476 * repository, we cope with that by stripping the expansion out
478 dollar
= memchr(src
+ 3, '$', len
- 3);
480 /* incomplete keyword, no more '$', so just quit the loop */
484 len
-= dollar
+ 1 - src
;
487 /* it wasn't a "Id$" or "Id:xxxx$" */
491 /* step 4: substitute */
492 strbuf_addstr(buf
, "Id: ");
493 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
494 strbuf_addstr(buf
, " $");
496 strbuf_add(buf
, src
, len
);
502 static int git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
504 const char *value
= check
->value
;
506 if (ATTR_TRUE(value
))
508 else if (ATTR_FALSE(value
))
510 else if (ATTR_UNSET(value
))
512 else if (!strcmp(value
, "input"))
517 static struct convert_driver
*git_path_check_convert(const char *path
,
518 struct git_attr_check
*check
)
520 const char *value
= check
->value
;
521 struct convert_driver
*drv
;
523 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
525 for (drv
= user_convert
; drv
; drv
= drv
->next
)
526 if (!strcmp(value
, drv
->name
))
531 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
533 const char *value
= check
->value
;
535 return !!ATTR_TRUE(value
);
538 int convert_to_git(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
540 struct git_attr_check check
[3];
541 int crlf
= CRLF_GUESS
;
542 int ident
= 0, ret
= 0;
545 setup_convert_check(check
);
546 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
547 struct convert_driver
*drv
;
548 crlf
= git_path_check_crlf(path
, check
+ 0);
549 ident
= git_path_check_ident(path
, check
+ 1);
550 drv
= git_path_check_convert(path
, check
+ 2);
551 if (drv
&& drv
->clean
)
555 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
560 ret
|= crlf_to_git(path
, src
, len
, dst
, crlf
);
565 return ret
| ident_to_git(path
, src
, len
, dst
, ident
);
568 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
570 struct git_attr_check check
[3];
571 int crlf
= CRLF_GUESS
;
572 int ident
= 0, ret
= 0;
575 setup_convert_check(check
);
576 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
577 struct convert_driver
*drv
;
578 crlf
= git_path_check_crlf(path
, check
+ 0);
579 ident
= git_path_check_ident(path
, check
+ 1);
580 drv
= git_path_check_convert(path
, check
+ 2);
581 if (drv
&& drv
->smudge
)
582 filter
= drv
->smudge
;
585 ret
|= ident_to_worktree(path
, src
, len
, dst
, ident
);
590 ret
|= crlf_to_worktree(path
, src
, len
, dst
, crlf
);
595 return ret
| apply_filter(path
, src
, len
, dst
, filter
);