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 "auto_crlf" option is set.
15 #define CRLF_GUESS (-1)
21 /* CR, LF and CRLF counts */
22 unsigned cr
, lf
, crlf
;
24 /* These are just approximations! */
25 unsigned printable
, nonprintable
;
28 static void gather_stats(const char *buf
, unsigned long size
, struct text_stat
*stats
)
32 memset(stats
, 0, sizeof(*stats
));
34 for (i
= 0; i
< size
; i
++) {
35 unsigned char c
= buf
[i
];
38 if (i
+1 < size
&& buf
[i
+1] == '\n')
48 stats
->nonprintable
++;
51 /* BS, HT, ESC and FF */
52 case '\b': case '\t': case '\033': case '\014':
56 stats
->nonprintable
++;
65 * The same heuristics as diff.c::mmfile_is_binary()
67 static int is_binary(unsigned long size
, struct text_stat
*stats
)
70 if ((stats
->printable
>> 7) < stats
->nonprintable
)
73 * Other heuristics? Average line length might be relevant,
74 * as might LF vs CR vs CRLF counts..
76 * NOTE! It might be normal to have a low ratio of CRLF to LF
77 * (somebody starts with a LF-only file and edits it with an editor
78 * that adds CRLF only to lines that are added..). But do we
79 * want to support CR-only? Probably not.
84 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
85 struct strbuf
*buf
, int action
)
87 struct text_stat stats
;
90 if ((action
== CRLF_BINARY
) || !auto_crlf
|| !len
)
93 gather_stats(src
, len
, &stats
);
94 /* No CR? Nothing to convert, regardless. */
98 if (action
== CRLF_GUESS
) {
100 * We're currently not going to even try to convert stuff
101 * that has bare CR characters. Does anybody do that crazy
104 if (stats
.cr
!= stats
.crlf
)
108 * And add some heuristics for binary vs text, of course...
110 if (is_binary(len
, &stats
))
114 strbuf_grow(buf
, len
);
116 if (action
== CRLF_GUESS
) {
118 * If we guessed, we already know we rejected a file with
119 * lone CR, and we can strip a CR without looking at what
123 unsigned char c
= *src
++;
129 unsigned char c
= *src
++;
130 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
134 strbuf_setlen(buf
, dst
- buf
->buf
);
138 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
139 struct strbuf
*buf
, int action
)
141 char *to_free
= NULL
;
142 struct text_stat stats
;
144 if ((action
== CRLF_BINARY
) || (action
== CRLF_INPUT
) ||
151 gather_stats(src
, len
, &stats
);
153 /* No LF? Nothing to convert, regardless. */
157 /* Was it already in CRLF format? */
158 if (stats
.lf
== stats
.crlf
)
161 if (action
== CRLF_GUESS
) {
162 /* If we have any bare CR characters, we're not going to touch it */
163 if (stats
.cr
!= stats
.crlf
)
166 if (is_binary(len
, &stats
))
170 /* are we "faking" in place editing ? */
172 to_free
= strbuf_detach(buf
);
174 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
176 const char *nl
= memchr(src
, '\n', len
);
179 if (nl
> src
&& nl
[-1] == '\r') {
180 strbuf_add(buf
, src
, nl
+ 1 - src
);
182 strbuf_add(buf
, src
, nl
- src
);
183 strbuf_addstr(buf
, "\r\n");
188 strbuf_add(buf
, src
, len
);
194 static int filter_buffer(const char *path
, const char *src
,
195 unsigned long size
, const char *cmd
)
198 * Spawn cmd and feed the buffer contents through its stdin.
200 struct child_process child_process
;
202 int write_err
, status
;
204 memset(&child_process
, 0, sizeof(child_process
));
206 if (pipe(pipe_feed
) < 0) {
207 error("cannot create pipe to run external filter %s", cmd
);
211 child_process
.pid
= fork();
212 if (child_process
.pid
< 0) {
213 error("cannot fork to run external filter %s", cmd
);
218 if (!child_process
.pid
) {
219 dup2(pipe_feed
[0], 0);
222 execlp("sh", "sh", "-c", cmd
, NULL
);
227 write_err
= (write_in_full(pipe_feed
[1], src
, size
) < 0);
228 if (close(pipe_feed
[1]))
231 error("cannot feed the input to external filter %s", cmd
);
233 status
= finish_command(&child_process
);
235 error("external filter %s failed %d", cmd
, -status
);
236 return (write_err
|| status
);
239 static int apply_filter(const char *path
, const char *src
, size_t len
,
240 struct strbuf
*dst
, const char *cmd
)
243 * Create a pipeline to have the command filter the buffer's
246 * (child --> cmd) --> us
250 struct child_process child_process
;
256 memset(&child_process
, 0, sizeof(child_process
));
258 if (pipe(pipe_feed
) < 0) {
259 error("cannot create pipe to run external filter %s", cmd
);
264 child_process
.pid
= fork();
265 if (child_process
.pid
< 0) {
266 error("cannot fork to run external filter %s", cmd
);
271 if (!child_process
.pid
) {
272 dup2(pipe_feed
[1], 1);
275 exit(filter_buffer(path
, src
, len
, cmd
));
279 strbuf_init(&nbuf
, 0);
280 if (strbuf_read(&nbuf
, pipe_feed
[0], len
) < 0) {
281 error("read from external filter %s failed", cmd
);
284 if (close(pipe_feed
[0])) {
285 ret
= error("read from external filter %s failed", cmd
);
288 status
= finish_command(&child_process
);
290 ret
= error("external filter %s failed %d", cmd
, -status
);
297 strbuf_release(&nbuf
);
302 static struct convert_driver
{
304 struct convert_driver
*next
;
307 } *user_convert
, **user_convert_tail
;
309 static int read_convert_config(const char *var
, const char *value
)
311 const char *ep
, *name
;
313 struct convert_driver
*drv
;
316 * External conversion drivers are configured using
317 * "filter.<name>.variable".
319 if (prefixcmp(var
, "filter.") || (ep
= strrchr(var
, '.')) == var
+ 6)
323 for (drv
= user_convert
; drv
; drv
= drv
->next
)
324 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
328 drv
= xcalloc(1, sizeof(struct convert_driver
));
329 namebuf
= xmalloc(namelen
+ 1);
330 memcpy(namebuf
, name
, namelen
);
331 namebuf
[namelen
] = 0;
334 *user_convert_tail
= drv
;
335 user_convert_tail
= &(drv
->next
);
341 * filter.<name>.smudge and filter.<name>.clean specifies
346 * The command-line will not be interpolated in any way.
349 if (!strcmp("smudge", ep
)) {
351 return error("%s: lacks value", var
);
352 drv
->smudge
= strdup(value
);
356 if (!strcmp("clean", ep
)) {
358 return error("%s: lacks value", var
);
359 drv
->clean
= strdup(value
);
365 static void setup_convert_check(struct git_attr_check
*check
)
367 static struct git_attr
*attr_crlf
;
368 static struct git_attr
*attr_ident
;
369 static struct git_attr
*attr_filter
;
372 attr_crlf
= git_attr("crlf", 4);
373 attr_ident
= git_attr("ident", 5);
374 attr_filter
= git_attr("filter", 6);
375 user_convert_tail
= &user_convert
;
376 git_config(read_convert_config
);
378 check
[0].attr
= attr_crlf
;
379 check
[1].attr
= attr_ident
;
380 check
[2].attr
= attr_filter
;
383 static int count_ident(const char *cp
, unsigned long size
)
386 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
398 if (memcmp("Id", cp
, 2))
409 * "$Id: ... "; scan up to the closing dollar sign and discard.
423 static int ident_to_git(const char *path
, const char *src
, size_t len
,
424 struct strbuf
*buf
, int ident
)
428 if (!ident
|| !count_ident(src
, len
))
431 strbuf_grow(buf
, len
);
434 dollar
= memchr(src
, '$', len
);
437 memcpy(dst
, src
, dollar
+ 1 - src
);
438 dst
+= dollar
+ 1 - src
;
439 len
-= dollar
+ 1 - src
;
442 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
443 dollar
= memchr(src
+ 3, '$', len
- 3);
446 memcpy(dst
, "Id$", 3);
448 len
-= dollar
+ 1 - src
;
452 memcpy(dst
, src
, len
);
453 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
457 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
458 struct strbuf
*buf
, int ident
)
460 unsigned char sha1
[20];
461 char *to_free
= NULL
, *dollar
;
467 cnt
= count_ident(src
, len
);
471 /* are we "faking" in place editing ? */
473 to_free
= strbuf_detach(buf
);
474 hash_sha1_file(src
, len
, "blob", sha1
);
476 strbuf_grow(buf
, len
+ cnt
* 43);
478 /* step 1: run to the next '$' */
479 dollar
= memchr(src
, '$', len
);
482 strbuf_add(buf
, src
, dollar
+ 1 - src
);
483 len
-= dollar
+ 1 - src
;
486 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
487 if (len
< 3 || memcmp("Id", src
, 2))
490 /* step 3: skip over Id$ or Id:xxxxx$ */
494 } else if (src
[2] == ':') {
496 * It's possible that an expanded Id has crept its way into the
497 * repository, we cope with that by stripping the expansion out
499 dollar
= memchr(src
+ 3, '$', len
- 3);
501 /* incomplete keyword, no more '$', so just quit the loop */
505 len
-= dollar
+ 1 - src
;
508 /* it wasn't a "Id$" or "Id:xxxx$" */
512 /* step 4: substitute */
513 strbuf_addstr(buf
, "Id: ");
514 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
515 strbuf_addstr(buf
, " $");
517 strbuf_add(buf
, src
, len
);
523 static int git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
525 const char *value
= check
->value
;
527 if (ATTR_TRUE(value
))
529 else if (ATTR_FALSE(value
))
531 else if (ATTR_UNSET(value
))
533 else if (!strcmp(value
, "input"))
538 static struct convert_driver
*git_path_check_convert(const char *path
,
539 struct git_attr_check
*check
)
541 const char *value
= check
->value
;
542 struct convert_driver
*drv
;
544 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
546 for (drv
= user_convert
; drv
; drv
= drv
->next
)
547 if (!strcmp(value
, drv
->name
))
552 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
554 const char *value
= check
->value
;
556 return !!ATTR_TRUE(value
);
559 int convert_to_git(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
561 struct git_attr_check check
[3];
562 int crlf
= CRLF_GUESS
;
563 int ident
= 0, ret
= 0;
566 setup_convert_check(check
);
567 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
568 struct convert_driver
*drv
;
569 crlf
= git_path_check_crlf(path
, check
+ 0);
570 ident
= git_path_check_ident(path
, check
+ 1);
571 drv
= git_path_check_convert(path
, check
+ 2);
572 if (drv
&& drv
->clean
)
576 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
581 ret
|= crlf_to_git(path
, src
, len
, dst
, crlf
);
586 return ret
| ident_to_git(path
, src
, len
, dst
, ident
);
589 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
591 struct git_attr_check check
[3];
592 int crlf
= CRLF_GUESS
;
593 int ident
= 0, ret
= 0;
596 setup_convert_check(check
);
597 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
598 struct convert_driver
*drv
;
599 crlf
= git_path_check_crlf(path
, check
+ 0);
600 ident
= git_path_check_ident(path
, check
+ 1);
601 drv
= git_path_check_convert(path
, check
+ 2);
602 if (drv
&& drv
->smudge
)
603 filter
= drv
->smudge
;
606 ret
|= ident_to_worktree(path
, src
, len
, dst
, ident
);
611 ret
|= crlf_to_worktree(path
, src
, len
, dst
, crlf
);
616 return ret
| apply_filter(path
, src
, len
, dst
, filter
);