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
++;
65 /* If file ends with EOF then don't count this EOF as non-printable. */
66 if (size
>= 1 && buf
[size
-1] == '\032')
67 stats
->nonprintable
--;
71 * The same heuristics as diff.c::mmfile_is_binary()
73 static int is_binary(unsigned long size
, struct text_stat
*stats
)
78 if ((stats
->printable
>> 7) < stats
->nonprintable
)
81 * Other heuristics? Average line length might be relevant,
82 * as might LF vs CR vs CRLF counts..
84 * NOTE! It might be normal to have a low ratio of CRLF to LF
85 * (somebody starts with a LF-only file and edits it with an editor
86 * that adds CRLF only to lines that are added..). But do we
87 * want to support CR-only? Probably not.
92 static void check_safe_crlf(const char *path
, int action
,
93 struct text_stat
*stats
, enum safe_crlf checksafe
)
98 if (action
== CRLF_INPUT
|| auto_crlf
<= 0) {
100 * CRLFs would not be restored by checkout:
101 * check if we'd remove CRLFs
104 if (checksafe
== SAFE_CRLF_WARN
)
105 warning("CRLF will be replaced by LF in %s.", path
);
106 else /* i.e. SAFE_CRLF_FAIL */
107 die("CRLF would be replaced by LF in %s.", path
);
109 } else if (auto_crlf
> 0) {
111 * CRLFs would be added by checkout:
112 * check if we have "naked" LFs
114 if (stats
->lf
!= stats
->crlf
) {
115 if (checksafe
== SAFE_CRLF_WARN
)
116 warning("LF will be replaced by CRLF in %s", path
);
117 else /* i.e. SAFE_CRLF_FAIL */
118 die("LF would be replaced by CRLF in %s", path
);
123 static int crlf_to_git(const char *path
, const char *src
, size_t len
,
124 struct strbuf
*buf
, int action
, enum safe_crlf checksafe
)
126 struct text_stat stats
;
129 if ((action
== CRLF_BINARY
) || !auto_crlf
|| !len
)
132 gather_stats(src
, len
, &stats
);
134 if (action
== CRLF_GUESS
) {
136 * We're currently not going to even try to convert stuff
137 * that has bare CR characters. Does anybody do that crazy
140 if (stats
.cr
!= stats
.crlf
)
144 * And add some heuristics for binary vs text, of course...
146 if (is_binary(len
, &stats
))
150 check_safe_crlf(path
, action
, &stats
, checksafe
);
152 /* Optimization: No CR? Nothing to convert, regardless. */
156 /* only grow if not in place */
157 if (strbuf_avail(buf
) + buf
->len
< len
)
158 strbuf_grow(buf
, len
- buf
->len
);
160 if (action
== CRLF_GUESS
) {
162 * If we guessed, we already know we rejected a file with
163 * lone CR, and we can strip a CR without looking at what
167 unsigned char c
= *src
++;
173 unsigned char c
= *src
++;
174 if (! (c
== '\r' && (1 < len
&& *src
== '\n')))
178 strbuf_setlen(buf
, dst
- buf
->buf
);
182 static int crlf_to_worktree(const char *path
, const char *src
, size_t len
,
183 struct strbuf
*buf
, int action
)
185 char *to_free
= NULL
;
186 struct text_stat stats
;
188 if ((action
== CRLF_BINARY
) || (action
== CRLF_INPUT
) ||
195 gather_stats(src
, len
, &stats
);
197 /* No LF? Nothing to convert, regardless. */
201 /* Was it already in CRLF format? */
202 if (stats
.lf
== stats
.crlf
)
205 if (action
== CRLF_GUESS
) {
206 /* If we have any bare CR characters, we're not going to touch it */
207 if (stats
.cr
!= stats
.crlf
)
210 if (is_binary(len
, &stats
))
214 /* are we "faking" in place editing ? */
216 to_free
= strbuf_detach(buf
, NULL
);
218 strbuf_grow(buf
, len
+ stats
.lf
- stats
.crlf
);
220 const char *nl
= memchr(src
, '\n', len
);
223 if (nl
> src
&& nl
[-1] == '\r') {
224 strbuf_add(buf
, src
, nl
+ 1 - src
);
226 strbuf_add(buf
, src
, nl
- src
);
227 strbuf_addstr(buf
, "\r\n");
232 strbuf_add(buf
, src
, len
);
238 struct filter_params
{
244 static int filter_buffer(int fd
, void *data
)
247 * Spawn cmd and feed the buffer contents through its stdin.
249 struct child_process child_process
;
250 struct filter_params
*params
= (struct filter_params
*)data
;
251 int write_err
, status
;
252 const char *argv
[] = { "sh", "-c", params
->cmd
, NULL
};
254 memset(&child_process
, 0, sizeof(child_process
));
255 child_process
.argv
= argv
;
256 child_process
.in
= -1;
257 child_process
.out
= fd
;
259 if (start_command(&child_process
))
260 return error("cannot fork to run external filter %s", params
->cmd
);
262 write_err
= (write_in_full(child_process
.in
, params
->src
, params
->size
) < 0);
263 if (close(child_process
.in
))
266 error("cannot feed the input to external filter %s", params
->cmd
);
268 status
= finish_command(&child_process
);
270 error("external filter %s failed %d", params
->cmd
, status
);
271 return (write_err
|| status
);
274 static int apply_filter(const char *path
, const char *src
, size_t len
,
275 struct strbuf
*dst
, const char *cmd
)
278 * Create a pipeline to have the command filter the buffer's
281 * (child --> cmd) --> us
284 struct strbuf nbuf
= STRBUF_INIT
;
286 struct filter_params params
;
291 memset(&async
, 0, sizeof(async
));
292 async
.proc
= filter_buffer
;
293 async
.data
= ¶ms
;
299 if (start_async(&async
))
300 return 0; /* error was already reported */
302 if (strbuf_read(&nbuf
, async
.out
, len
) < 0) {
303 error("read from external filter %s failed", cmd
);
306 if (close(async
.out
)) {
307 error("read from external filter %s failed", cmd
);
310 if (finish_async(&async
)) {
311 error("external filter %s failed", cmd
);
316 strbuf_swap(dst
, &nbuf
);
318 strbuf_release(&nbuf
);
322 static struct convert_driver
{
324 struct convert_driver
*next
;
327 } *user_convert
, **user_convert_tail
;
329 static int read_convert_config(const char *var
, const char *value
, void *cb
)
331 const char *ep
, *name
;
333 struct convert_driver
*drv
;
336 * External conversion drivers are configured using
337 * "filter.<name>.variable".
339 if (prefixcmp(var
, "filter.") || (ep
= strrchr(var
, '.')) == var
+ 6)
343 for (drv
= user_convert
; drv
; drv
= drv
->next
)
344 if (!strncmp(drv
->name
, name
, namelen
) && !drv
->name
[namelen
])
347 drv
= xcalloc(1, sizeof(struct convert_driver
));
348 drv
->name
= xmemdupz(name
, namelen
);
349 *user_convert_tail
= drv
;
350 user_convert_tail
= &(drv
->next
);
356 * filter.<name>.smudge and filter.<name>.clean specifies
361 * The command-line will not be interpolated in any way.
364 if (!strcmp("smudge", ep
))
365 return git_config_string(&drv
->smudge
, var
, value
);
367 if (!strcmp("clean", ep
))
368 return git_config_string(&drv
->clean
, var
, value
);
373 static void setup_convert_check(struct git_attr_check
*check
)
375 static struct git_attr
*attr_crlf
;
376 static struct git_attr
*attr_ident
;
377 static struct git_attr
*attr_filter
;
380 attr_crlf
= git_attr("crlf", 4);
381 attr_ident
= git_attr("ident", 5);
382 attr_filter
= git_attr("filter", 6);
383 user_convert_tail
= &user_convert
;
384 git_config(read_convert_config
, NULL
);
386 check
[0].attr
= attr_crlf
;
387 check
[1].attr
= attr_ident
;
388 check
[2].attr
= attr_filter
;
391 static int count_ident(const char *cp
, unsigned long size
)
394 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
406 if (memcmp("Id", cp
, 2))
417 * "$Id: ... "; scan up to the closing dollar sign and discard.
431 static int ident_to_git(const char *path
, const char *src
, size_t len
,
432 struct strbuf
*buf
, int ident
)
436 if (!ident
|| !count_ident(src
, len
))
439 /* only grow if not in place */
440 if (strbuf_avail(buf
) + buf
->len
< len
)
441 strbuf_grow(buf
, len
- buf
->len
);
444 dollar
= memchr(src
, '$', len
);
447 memcpy(dst
, src
, dollar
+ 1 - src
);
448 dst
+= dollar
+ 1 - src
;
449 len
-= dollar
+ 1 - src
;
452 if (len
> 3 && !memcmp(src
, "Id:", 3)) {
453 dollar
= memchr(src
+ 3, '$', len
- 3);
456 memcpy(dst
, "Id$", 3);
458 len
-= dollar
+ 1 - src
;
462 memcpy(dst
, src
, len
);
463 strbuf_setlen(buf
, dst
+ len
- buf
->buf
);
467 static int ident_to_worktree(const char *path
, const char *src
, size_t len
,
468 struct strbuf
*buf
, int ident
)
470 unsigned char sha1
[20];
471 char *to_free
= NULL
, *dollar
;
477 cnt
= count_ident(src
, len
);
481 /* are we "faking" in place editing ? */
483 to_free
= strbuf_detach(buf
, NULL
);
484 hash_sha1_file(src
, len
, "blob", sha1
);
486 strbuf_grow(buf
, len
+ cnt
* 43);
488 /* step 1: run to the next '$' */
489 dollar
= memchr(src
, '$', len
);
492 strbuf_add(buf
, src
, dollar
+ 1 - src
);
493 len
-= dollar
+ 1 - src
;
496 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
497 if (len
< 3 || memcmp("Id", src
, 2))
500 /* step 3: skip over Id$ or Id:xxxxx$ */
504 } else if (src
[2] == ':') {
506 * It's possible that an expanded Id has crept its way into the
507 * repository, we cope with that by stripping the expansion out
509 dollar
= memchr(src
+ 3, '$', len
- 3);
511 /* incomplete keyword, no more '$', so just quit the loop */
515 len
-= dollar
+ 1 - src
;
518 /* it wasn't a "Id$" or "Id:xxxx$" */
522 /* step 4: substitute */
523 strbuf_addstr(buf
, "Id: ");
524 strbuf_add(buf
, sha1_to_hex(sha1
), 40);
525 strbuf_addstr(buf
, " $");
527 strbuf_add(buf
, src
, len
);
533 static int git_path_check_crlf(const char *path
, struct git_attr_check
*check
)
535 const char *value
= check
->value
;
537 if (ATTR_TRUE(value
))
539 else if (ATTR_FALSE(value
))
541 else if (ATTR_UNSET(value
))
543 else if (!strcmp(value
, "input"))
548 static struct convert_driver
*git_path_check_convert(const char *path
,
549 struct git_attr_check
*check
)
551 const char *value
= check
->value
;
552 struct convert_driver
*drv
;
554 if (ATTR_TRUE(value
) || ATTR_FALSE(value
) || ATTR_UNSET(value
))
556 for (drv
= user_convert
; drv
; drv
= drv
->next
)
557 if (!strcmp(value
, drv
->name
))
562 static int git_path_check_ident(const char *path
, struct git_attr_check
*check
)
564 const char *value
= check
->value
;
566 return !!ATTR_TRUE(value
);
569 int convert_to_git(const char *path
, const char *src
, size_t len
,
570 struct strbuf
*dst
, enum safe_crlf checksafe
)
572 struct git_attr_check check
[3];
573 int crlf
= CRLF_GUESS
;
574 int ident
= 0, ret
= 0;
575 const char *filter
= NULL
;
577 setup_convert_check(check
);
578 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
579 struct convert_driver
*drv
;
580 crlf
= git_path_check_crlf(path
, check
+ 0);
581 ident
= git_path_check_ident(path
, check
+ 1);
582 drv
= git_path_check_convert(path
, check
+ 2);
583 if (drv
&& drv
->clean
)
587 ret
|= apply_filter(path
, src
, len
, dst
, filter
);
592 ret
|= crlf_to_git(path
, src
, len
, dst
, crlf
, checksafe
);
597 return ret
| ident_to_git(path
, src
, len
, dst
, ident
);
600 int convert_to_working_tree(const char *path
, const char *src
, size_t len
, struct strbuf
*dst
)
602 struct git_attr_check check
[3];
603 int crlf
= CRLF_GUESS
;
604 int ident
= 0, ret
= 0;
605 const char *filter
= NULL
;
607 setup_convert_check(check
);
608 if (!git_checkattr(path
, ARRAY_SIZE(check
), check
)) {
609 struct convert_driver
*drv
;
610 crlf
= git_path_check_crlf(path
, check
+ 0);
611 ident
= git_path_check_ident(path
, check
+ 1);
612 drv
= git_path_check_convert(path
, check
+ 2);
613 if (drv
&& drv
->smudge
)
614 filter
= drv
->smudge
;
617 ret
|= ident_to_worktree(path
, src
, len
, dst
, ident
);
622 ret
|= crlf_to_worktree(path
, src
, len
, dst
, crlf
);
627 return ret
| apply_filter(path
, src
, len
, dst
, filter
);