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 /* NUL, CR, LF and CRLF counts */
21 unsigned nul
, 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':
58 stats
->nonprintable
++;
67 * The same heuristics as diff.c::mmfile_is_binary()
69 static int is_binary(unsigned long size
, struct text_stat
*stats
)
74 if ((stats
->printable
>> 7) < stats
->nonprintable
)
77 * Other heuristics? Average line length might be relevant,
78 * as might LF vs CR vs CRLF counts..
80 * NOTE! It might be normal to have a low ratio of CRLF to LF
81 * (somebody starts with a LF-only file and edits it with an editor
82 * that adds CRLF only to lines that are added..). But do we
83 * want to support CR-only? Probably not.
88 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
89 struct strbuf
*buf
, int action
)
91 struct text_stat stats
;
94 if ((action
== CRLF_BINARY
) || !auto_crlf
|| !len
)
97 gather_stats(src
, len
, &stats
);
98 /* No CR? Nothing to convert, regardless. */
102 if (action
== CRLF_GUESS
) {
104 * We're currently not going to even try to convert stuff
105 * that has bare CR characters. Does anybody do that crazy
108 if (stats
.cr
!= stats
.crlf
)
112 * And add some heuristics for binary vs text, of course...
114 if (is_binary(len
, &stats
))
118 /* only grow if not in place */
119 if (strbuf_avail(buf
) + buf
->len
< len
)
120 strbuf_grow(buf
, len
- buf
->len
);
122 if (action
== CRLF_GUESS
) {
124 * If we guessed, we already know we rejected a file with
125 * lone CR, and we can strip a CR without looking at what
129 unsigned char c
= *src
++;
135 unsigned char c
= *src
++;
136 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
140 strbuf_setlen(buf
, dst
- buf
->buf
);
144 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
145 struct strbuf
*buf
, int action
)
147 char *to_free
= NULL
;
148 struct text_stat stats
;
150 if ((action
== CRLF_BINARY
) || (action
== CRLF_INPUT
) ||
157 gather_stats(src
, len
, &stats
);
159 /* No LF? Nothing to convert, regardless. */
163 /* Was it already in CRLF format? */
164 if (stats
.lf
== stats
.crlf
)
167 if (action
== CRLF_GUESS
) {
168 /* If we have any bare CR characters, we're not going to touch it */
169 if (stats
.cr
!= stats
.crlf
)
172 if (is_binary(len
, &stats
))
176 /* are we "faking" in place editing ? */
178 to_free
= strbuf_detach(buf
, NULL
);
180 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
182 const char *nl
= memchr(src
, '\n', len
);
185 if (nl
> src
&& nl
[-1] == '\r') {
186 strbuf_add(buf
, src
, nl
+ 1 - src
);
188 strbuf_add(buf
, src
, nl
- src
);
189 strbuf_addstr(buf
, "\r\n");
194 strbuf_add(buf
, src
, len
);
200 struct filter_params
{
206 static int filter_buffer(int fd
, void *data
)
209 * Spawn cmd and feed the buffer contents through its stdin.
211 struct child_process child_process
;
212 struct filter_params
*params
= (struct filter_params
*)data
;
213 int write_err
, status
;
214 const char *argv
[] = { "sh", "-c", params
->cmd
, NULL
};
216 memset(&child_process
, 0, sizeof(child_process
));
217 child_process
.argv
= argv
;
218 child_process
.in
= -1;
219 child_process
.out
= fd
;
221 if (start_command(&child_process
))
222 return error("cannot fork to run external filter %s", params
->cmd
);
224 write_err
= (write_in_full(child_process
.in
, params
->src
, params
->size
) < 0);
225 if (close(child_process
.in
))
228 error("cannot feed the input to external filter %s", params
->cmd
);
230 status
= finish_command(&child_process
);
232 error("external filter %s failed %d", params
->cmd
, -status
);
233 return (write_err
|| status
);
236 static int apply_filter(const char *path
, const char *src
, size_t len
,
237 struct strbuf
*dst
, const char *cmd
)
240 * Create a pipeline to have the command filter the buffer's
243 * (child --> cmd) --> us
248 struct filter_params params
;
253 memset(&async
, 0, sizeof(async
));
254 async
.proc
= filter_buffer
;
255 async
.data
= ¶ms
;
261 if (start_async(&async
))
262 return 0; /* error was already reported */
264 strbuf_init(&nbuf
, 0);
265 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
266 error("read from external filter %s failed", cmd
);
269 if (close(async
.out
)) {
270 error("read from external filter %s failed", cmd
);
273 if (finish_async(&async
)) {
274 error("external filter %s failed", cmd
);
279 strbuf_swap(dst
, &nbuf
);
281 strbuf_release(&nbuf
);
285 static struct convert_driver
{
287 struct convert_driver
*next
;
290 } *user_convert
, **user_convert_tail
;
292 static int read_convert_config(const char *var
, const char *value
)
294 const char *ep
, *name
;
296 struct convert_driver
*drv
;
299 * External conversion drivers are configured using
300 * "filter.<name>.variable".
302 if (prefixcmp(var
, "filter.") || (ep
= strrchr(var
, '.')) == var
+ 6)
306 for (drv
= user_convert
; drv
; drv
= drv
->next
)
307 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
310 drv
= xcalloc(1, sizeof(struct convert_driver
));
311 drv
->name
= xmemdupz(name
, namelen
);
312 *user_convert_tail
= drv
;
313 user_convert_tail
= &(drv
->next
);
319 * filter.<name>.smudge and filter.<name>.clean specifies
324 * The command-line will not be interpolated in any way.
327 if (!strcmp("smudge", ep
)) {
329 return error("%s: lacks value", var
);
330 drv
->smudge
= strdup(value
);
334 if (!strcmp("clean", ep
)) {
336 return error("%s: lacks value", var
);
337 drv
->clean
= strdup(value
);
343 static void setup_convert_check(struct git_attr_check
*check
)
345 static struct git_attr
*attr_crlf
;
346 static struct git_attr
*attr_ident
;
347 static struct git_attr
*attr_filter
;
350 attr_crlf
= git_attr("crlf", 4);
351 attr_ident
= git_attr("ident", 5);
352 attr_filter
= git_attr("filter", 6);
353 user_convert_tail
= &user_convert
;
354 git_config(read_convert_config
);
356 check
[0].attr
= attr_crlf
;
357 check
[1].attr
= attr_ident
;
358 check
[2].attr
= attr_filter
;
361 static int count_ident(const char *cp
, unsigned long size
)
364 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
376 if (memcmp("Id", cp
, 2))
387 * "$Id: ... "; scan up to the closing dollar sign and discard.
401 static int ident_to_git(const char *path
, const char *src
, size_t len
,
402 struct strbuf
*buf
, int ident
)
406 if (!ident
|| !count_ident(src
, len
))
409 /* only grow if not in place */
410 if (strbuf_avail(buf
) + buf
->len
< len
)
411 strbuf_grow(buf
, len
- buf
->len
);
414 dollar
= memchr(src
, '$', len
);
417 memcpy(dst
, src
, dollar
+ 1 - src
);
418 dst
+= dollar
+ 1 - src
;
419 len
-= dollar
+ 1 - src
;
422 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
423 dollar
= memchr(src
+ 3, '$', len
- 3);
426 memcpy(dst
, "Id$", 3);
428 len
-= dollar
+ 1 - src
;
432 memcpy(dst
, src
, len
);
433 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
437 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
438 struct strbuf
*buf
, int ident
)
440 unsigned char sha1
[20];
441 char *to_free
= NULL
, *dollar
;
447 cnt
= count_ident(src
, len
);
451 /* are we "faking" in place editing ? */
453 to_free
= strbuf_detach(buf
, NULL
);
454 hash_sha1_file(src
, len
, "blob", sha1
);
456 strbuf_grow(buf
, len
+ cnt
* 43);
458 /* step 1: run to the next '$' */
459 dollar
= memchr(src
, '$', len
);
462 strbuf_add(buf
, src
, dollar
+ 1 - src
);
463 len
-= dollar
+ 1 - src
;
466 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
467 if (len
< 3 || memcmp("Id", src
, 2))
470 /* step 3: skip over Id$ or Id:xxxxx$ */
474 } else if (src
[2] == ':') {
476 * It's possible that an expanded Id has crept its way into the
477 * repository, we cope with that by stripping the expansion out
479 dollar
= memchr(src
+ 3, '$', len
- 3);
481 /* incomplete keyword, no more '$', so just quit the loop */
485 len
-= dollar
+ 1 - src
;
488 /* it wasn't a "Id$" or "Id:xxxx$" */
492 /* step 4: substitute */
493 strbuf_addstr(buf
, "Id: ");
494 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
495 strbuf_addstr(buf
, " $");
497 strbuf_add(buf
, src
, len
);
503 static int git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
505 const char *value
= check
->value
;
507 if (ATTR_TRUE(value
))
509 else if (ATTR_FALSE(value
))
511 else if (ATTR_UNSET(value
))
513 else if (!strcmp(value
, "input"))
518 static struct convert_driver
*git_path_check_convert(const char *path
,
519 struct git_attr_check
*check
)
521 const char *value
= check
->value
;
522 struct convert_driver
*drv
;
524 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
526 for (drv
= user_convert
; drv
; drv
= drv
->next
)
527 if (!strcmp(value
, drv
->name
))
532 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
534 const char *value
= check
->value
;
536 return !!ATTR_TRUE(value
);
539 int convert_to_git(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
541 struct git_attr_check check
[3];
542 int crlf
= CRLF_GUESS
;
543 int ident
= 0, ret
= 0;
546 setup_convert_check(check
);
547 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
548 struct convert_driver
*drv
;
549 crlf
= git_path_check_crlf(path
, check
+ 0);
550 ident
= git_path_check_ident(path
, check
+ 1);
551 drv
= git_path_check_convert(path
, check
+ 2);
552 if (drv
&& drv
->clean
)
556 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
561 ret
|= crlf_to_git(path
, src
, len
, dst
, crlf
);
566 return ret
| ident_to_git(path
, src
, len
, dst
, ident
);
569 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
571 struct git_attr_check check
[3];
572 int crlf
= CRLF_GUESS
;
573 int ident
= 0, ret
= 0;
576 setup_convert_check(check
);
577 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
578 struct convert_driver
*drv
;
579 crlf
= git_path_check_crlf(path
, check
+ 0);
580 ident
= git_path_check_ident(path
, check
+ 1);
581 drv
= git_path_check_convert(path
, check
+ 2);
582 if (drv
&& drv
->smudge
)
583 filter
= drv
->smudge
;
586 ret
|= ident_to_worktree(path
, src
, len
, dst
, ident
);
591 ret
|= crlf_to_worktree(path
, src
, len
, dst
, crlf
);
596 return ret
| apply_filter(path
, src
, len
, dst
, filter
);