]>
Commit | Line | Data |
---|---|---|
6c510bee | 1 | #include "cache.h" |
35ebfd6a | 2 | #include "attr.h" |
3fed15f5 | 3 | #include "run-command.h" |
35ebfd6a | 4 | |
6c510bee LT |
5 | /* |
6 | * convert.c - convert a file when checking it out and checking it in. | |
7 | * | |
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 | |
fd6cce9e | 11 | * translation when the "crlf" attribute or "auto_crlf" option is set. |
6c510bee LT |
12 | */ |
13 | ||
fd6cce9e EB |
14 | enum action { |
15 | CRLF_GUESS = -1, | |
16 | CRLF_BINARY = 0, | |
17 | CRLF_TEXT, | |
18 | CRLF_INPUT, | |
19 | CRLF_CRLF, | |
20 | CRLF_AUTO, | |
21 | }; | |
22 | ||
23 | enum eol { | |
24 | EOL_UNSET, | |
25 | EOL_LF, | |
26 | EOL_CRLF, | |
27 | }; | |
163b9591 | 28 | |
6c510bee | 29 | struct text_stat { |
28624193 DP |
30 | /* NUL, CR, LF and CRLF counts */ |
31 | unsigned nul, cr, lf, crlf; | |
6c510bee LT |
32 | |
33 | /* These are just approximations! */ | |
34 | unsigned printable, nonprintable; | |
35 | }; | |
36 | ||
37 | static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) | |
38 | { | |
39 | unsigned long i; | |
40 | ||
41 | memset(stats, 0, sizeof(*stats)); | |
42 | ||
43 | for (i = 0; i < size; i++) { | |
44 | unsigned char c = buf[i]; | |
45 | if (c == '\r') { | |
46 | stats->cr++; | |
47 | if (i+1 < size && buf[i+1] == '\n') | |
48 | stats->crlf++; | |
49 | continue; | |
50 | } | |
51 | if (c == '\n') { | |
52 | stats->lf++; | |
53 | continue; | |
54 | } | |
55 | if (c == 127) | |
56 | /* DEL */ | |
57 | stats->nonprintable++; | |
58 | else if (c < 32) { | |
59 | switch (c) { | |
60 | /* BS, HT, ESC and FF */ | |
61 | case '\b': case '\t': case '\033': case '\014': | |
62 | stats->printable++; | |
63 | break; | |
28624193 DP |
64 | case 0: |
65 | stats->nul++; | |
66 | /* fall through */ | |
6c510bee LT |
67 | default: |
68 | stats->nonprintable++; | |
69 | } | |
70 | } | |
71 | else | |
72 | stats->printable++; | |
73 | } | |
f9dd4bf4 DK |
74 | |
75 | /* If file ends with EOF then don't count this EOF as non-printable. */ | |
76 | if (size >= 1 && buf[size-1] == '\032') | |
77 | stats->nonprintable--; | |
6c510bee LT |
78 | } |
79 | ||
80 | /* | |
81 | * The same heuristics as diff.c::mmfile_is_binary() | |
82 | */ | |
83 | static int is_binary(unsigned long size, struct text_stat *stats) | |
84 | { | |
85 | ||
28624193 DP |
86 | if (stats->nul) |
87 | return 1; | |
6c510bee LT |
88 | if ((stats->printable >> 7) < stats->nonprintable) |
89 | return 1; | |
90 | /* | |
91 | * Other heuristics? Average line length might be relevant, | |
92 | * as might LF vs CR vs CRLF counts.. | |
93 | * | |
94 | * NOTE! It might be normal to have a low ratio of CRLF to LF | |
95 | * (somebody starts with a LF-only file and edits it with an editor | |
96 | * that adds CRLF only to lines that are added..). But do we | |
97 | * want to support CR-only? Probably not. | |
98 | */ | |
99 | return 0; | |
100 | } | |
101 | ||
fd6cce9e | 102 | static void check_safe_crlf(const char *path, enum action action, |
21e5ad50 SP |
103 | struct text_stat *stats, enum safe_crlf checksafe) |
104 | { | |
105 | if (!checksafe) | |
106 | return; | |
107 | ||
fd6cce9e EB |
108 | if (action == CRLF_INPUT || |
109 | (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_INPUT)) { | |
21e5ad50 SP |
110 | /* |
111 | * CRLFs would not be restored by checkout: | |
112 | * check if we'd remove CRLFs | |
113 | */ | |
114 | if (stats->crlf) { | |
115 | if (checksafe == SAFE_CRLF_WARN) | |
116 | warning("CRLF will be replaced by LF in %s.", path); | |
117 | else /* i.e. SAFE_CRLF_FAIL */ | |
118 | die("CRLF would be replaced by LF in %s.", path); | |
119 | } | |
fd6cce9e EB |
120 | } else if (action == CRLF_CRLF || |
121 | (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_TRUE)) { | |
21e5ad50 SP |
122 | /* |
123 | * CRLFs would be added by checkout: | |
124 | * check if we have "naked" LFs | |
125 | */ | |
126 | if (stats->lf != stats->crlf) { | |
127 | if (checksafe == SAFE_CRLF_WARN) | |
128 | warning("LF will be replaced by CRLF in %s", path); | |
129 | else /* i.e. SAFE_CRLF_FAIL */ | |
130 | die("LF would be replaced by CRLF in %s", path); | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
c4805393 FAG |
135 | static int has_cr_in_index(const char *path) |
136 | { | |
137 | int pos, len; | |
138 | unsigned long sz; | |
139 | enum object_type type; | |
140 | void *data; | |
141 | int has_cr; | |
142 | struct index_state *istate = &the_index; | |
143 | ||
144 | len = strlen(path); | |
145 | pos = index_name_pos(istate, path, len); | |
146 | if (pos < 0) { | |
147 | /* | |
148 | * We might be in the middle of a merge, in which | |
149 | * case we would read stage #2 (ours). | |
150 | */ | |
151 | int i; | |
152 | for (i = -pos - 1; | |
153 | (pos < 0 && i < istate->cache_nr && | |
154 | !strcmp(istate->cache[i]->name, path)); | |
155 | i++) | |
156 | if (ce_stage(istate->cache[i]) == 2) | |
157 | pos = i; | |
158 | } | |
159 | if (pos < 0) | |
160 | return 0; | |
161 | data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz); | |
162 | if (!data || type != OBJ_BLOB) { | |
163 | free(data); | |
164 | return 0; | |
165 | } | |
166 | ||
167 | has_cr = memchr(data, '\r', sz) != NULL; | |
168 | free(data); | |
169 | return has_cr; | |
170 | } | |
171 | ||
5ecd293d | 172 | static int crlf_to_git(const char *path, const char *src, size_t len, |
fd6cce9e | 173 | struct strbuf *buf, enum action action, enum safe_crlf checksafe) |
6c510bee | 174 | { |
6c510bee | 175 | struct text_stat stats; |
5ecd293d | 176 | char *dst; |
6c510bee | 177 | |
fd6cce9e EB |
178 | if (action == CRLF_BINARY || |
179 | (action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) || !len) | |
5ecd293d | 180 | return 0; |
6c510bee | 181 | |
5ecd293d | 182 | gather_stats(src, len, &stats); |
6c510bee | 183 | |
fd6cce9e | 184 | if (action == CRLF_AUTO || action == CRLF_GUESS) { |
201ac8ef JH |
185 | /* |
186 | * We're currently not going to even try to convert stuff | |
187 | * that has bare CR characters. Does anybody do that crazy | |
188 | * stuff? | |
189 | */ | |
190 | if (stats.cr != stats.crlf) | |
5ecd293d | 191 | return 0; |
201ac8ef JH |
192 | |
193 | /* | |
194 | * And add some heuristics for binary vs text, of course... | |
195 | */ | |
5ecd293d PH |
196 | if (is_binary(len, &stats)) |
197 | return 0; | |
c4805393 | 198 | |
fd6cce9e EB |
199 | if (action == CRLF_GUESS) { |
200 | /* | |
201 | * If the file in the index has any CR in it, do not convert. | |
202 | * This is the new safer autocrlf handling. | |
203 | */ | |
204 | if (has_cr_in_index(path)) | |
205 | return 0; | |
206 | } | |
201ac8ef | 207 | } |
6c510bee | 208 | |
21e5ad50 SP |
209 | check_safe_crlf(path, action, &stats, checksafe); |
210 | ||
211 | /* Optimization: No CR? Nothing to convert, regardless. */ | |
212 | if (!stats.cr) | |
213 | return 0; | |
214 | ||
90d16ec0 PH |
215 | /* only grow if not in place */ |
216 | if (strbuf_avail(buf) + buf->len < len) | |
217 | strbuf_grow(buf, len - buf->len); | |
5ecd293d | 218 | dst = buf->buf; |
fd6cce9e | 219 | if (action == CRLF_AUTO || action == CRLF_GUESS) { |
163b9591 JH |
220 | /* |
221 | * If we guessed, we already know we rejected a file with | |
222 | * lone CR, and we can strip a CR without looking at what | |
223 | * follow it. | |
224 | */ | |
201ac8ef | 225 | do { |
ac78e548 | 226 | unsigned char c = *src++; |
201ac8ef | 227 | if (c != '\r') |
ac78e548 | 228 | *dst++ = c; |
5ecd293d | 229 | } while (--len); |
201ac8ef JH |
230 | } else { |
231 | do { | |
ac78e548 | 232 | unsigned char c = *src++; |
5ecd293d | 233 | if (! (c == '\r' && (1 < len && *src == '\n'))) |
ac78e548 | 234 | *dst++ = c; |
5ecd293d | 235 | } while (--len); |
201ac8ef | 236 | } |
5ecd293d PH |
237 | strbuf_setlen(buf, dst - buf->buf); |
238 | return 1; | |
6c510bee LT |
239 | } |
240 | ||
5ecd293d | 241 | static int crlf_to_worktree(const char *path, const char *src, size_t len, |
fd6cce9e | 242 | struct strbuf *buf, enum action action) |
6c510bee | 243 | { |
5ecd293d | 244 | char *to_free = NULL; |
6c510bee | 245 | struct text_stat stats; |
6c510bee | 246 | |
163b9591 | 247 | if ((action == CRLF_BINARY) || (action == CRLF_INPUT) || |
fd6cce9e | 248 | (action != CRLF_CRLF && auto_crlf != AUTO_CRLF_TRUE)) |
5ecd293d | 249 | return 0; |
6c510bee | 250 | |
5ecd293d PH |
251 | if (!len) |
252 | return 0; | |
6c510bee | 253 | |
5ecd293d | 254 | gather_stats(src, len, &stats); |
6c510bee LT |
255 | |
256 | /* No LF? Nothing to convert, regardless. */ | |
257 | if (!stats.lf) | |
5ecd293d | 258 | return 0; |
6c510bee LT |
259 | |
260 | /* Was it already in CRLF format? */ | |
261 | if (stats.lf == stats.crlf) | |
5ecd293d | 262 | return 0; |
6c510bee | 263 | |
fd6cce9e EB |
264 | if (action == CRLF_AUTO || action == CRLF_GUESS) { |
265 | if (action == CRLF_GUESS) { | |
266 | /* If we have any CR or CRLF line endings, we do not touch it */ | |
267 | /* This is the new safer autocrlf-handling */ | |
268 | if (stats.cr > 0 || stats.crlf > 0) | |
269 | return 0; | |
270 | } | |
c4805393 | 271 | |
201ac8ef JH |
272 | /* If we have any bare CR characters, we're not going to touch it */ |
273 | if (stats.cr != stats.crlf) | |
5ecd293d | 274 | return 0; |
6c510bee | 275 | |
5ecd293d PH |
276 | if (is_binary(len, &stats)) |
277 | return 0; | |
201ac8ef | 278 | } |
6c510bee | 279 | |
5ecd293d PH |
280 | /* are we "faking" in place editing ? */ |
281 | if (src == buf->buf) | |
b315c5c0 | 282 | to_free = strbuf_detach(buf, NULL); |
5ecd293d PH |
283 | |
284 | strbuf_grow(buf, len + stats.lf - stats.crlf); | |
285 | for (;;) { | |
286 | const char *nl = memchr(src, '\n', len); | |
287 | if (!nl) | |
288 | break; | |
289 | if (nl > src && nl[-1] == '\r') { | |
290 | strbuf_add(buf, src, nl + 1 - src); | |
291 | } else { | |
292 | strbuf_add(buf, src, nl - src); | |
293 | strbuf_addstr(buf, "\r\n"); | |
294 | } | |
295 | len -= nl + 1 - src; | |
296 | src = nl + 1; | |
297 | } | |
298 | strbuf_add(buf, src, len); | |
299 | ||
300 | free(to_free); | |
301 | return 1; | |
6c510bee | 302 | } |
35ebfd6a | 303 | |
546bb582 JS |
304 | struct filter_params { |
305 | const char *src; | |
306 | unsigned long size; | |
307 | const char *cmd; | |
308 | }; | |
309 | ||
310 | static int filter_buffer(int fd, void *data) | |
aa4ed402 JH |
311 | { |
312 | /* | |
313 | * Spawn cmd and feed the buffer contents through its stdin. | |
314 | */ | |
315 | struct child_process child_process; | |
546bb582 | 316 | struct filter_params *params = (struct filter_params *)data; |
aa4ed402 | 317 | int write_err, status; |
ac0ba18d | 318 | const char *argv[] = { params->cmd, NULL }; |
aa4ed402 JH |
319 | |
320 | memset(&child_process, 0, sizeof(child_process)); | |
dc1bfdcd | 321 | child_process.argv = argv; |
ac0ba18d | 322 | child_process.use_shell = 1; |
dc1bfdcd | 323 | child_process.in = -1; |
7683b6e8 | 324 | child_process.out = fd; |
aa4ed402 | 325 | |
dc1bfdcd | 326 | if (start_command(&child_process)) |
546bb582 | 327 | return error("cannot fork to run external filter %s", params->cmd); |
aa4ed402 | 328 | |
546bb582 | 329 | write_err = (write_in_full(child_process.in, params->src, params->size) < 0); |
dc1bfdcd | 330 | if (close(child_process.in)) |
aa4ed402 JH |
331 | write_err = 1; |
332 | if (write_err) | |
546bb582 | 333 | error("cannot feed the input to external filter %s", params->cmd); |
aa4ed402 JH |
334 | |
335 | status = finish_command(&child_process); | |
336 | if (status) | |
5709e036 | 337 | error("external filter %s failed %d", params->cmd, status); |
aa4ed402 JH |
338 | return (write_err || status); |
339 | } | |
340 | ||
5ecd293d PH |
341 | static int apply_filter(const char *path, const char *src, size_t len, |
342 | struct strbuf *dst, const char *cmd) | |
aa4ed402 JH |
343 | { |
344 | /* | |
345 | * Create a pipeline to have the command filter the buffer's | |
346 | * contents. | |
347 | * | |
348 | * (child --> cmd) --> us | |
349 | */ | |
546bb582 | 350 | int ret = 1; |
f285a2d7 | 351 | struct strbuf nbuf = STRBUF_INIT; |
546bb582 JS |
352 | struct async async; |
353 | struct filter_params params; | |
aa4ed402 JH |
354 | |
355 | if (!cmd) | |
5ecd293d | 356 | return 0; |
aa4ed402 | 357 | |
546bb582 JS |
358 | memset(&async, 0, sizeof(async)); |
359 | async.proc = filter_buffer; | |
360 | async.data = ¶ms; | |
361 | params.src = src; | |
362 | params.size = len; | |
363 | params.cmd = cmd; | |
aa4ed402 JH |
364 | |
365 | fflush(NULL); | |
546bb582 JS |
366 | if (start_async(&async)) |
367 | return 0; /* error was already reported */ | |
aa4ed402 | 368 | |
546bb582 | 369 | if (strbuf_read(&nbuf, async.out, len) < 0) { |
5ecd293d PH |
370 | error("read from external filter %s failed", cmd); |
371 | ret = 0; | |
aa4ed402 | 372 | } |
546bb582 | 373 | if (close(async.out)) { |
90d16ec0 | 374 | error("read from external filter %s failed", cmd); |
5ecd293d | 375 | ret = 0; |
aa4ed402 | 376 | } |
546bb582 JS |
377 | if (finish_async(&async)) { |
378 | error("external filter %s failed", cmd); | |
5ecd293d | 379 | ret = 0; |
aa4ed402 JH |
380 | } |
381 | ||
5ecd293d | 382 | if (ret) { |
90d16ec0 | 383 | strbuf_swap(dst, &nbuf); |
5ecd293d | 384 | } |
90d16ec0 | 385 | strbuf_release(&nbuf); |
5ecd293d | 386 | return ret; |
aa4ed402 JH |
387 | } |
388 | ||
389 | static struct convert_driver { | |
390 | const char *name; | |
391 | struct convert_driver *next; | |
cd8be6c9 BH |
392 | const char *smudge; |
393 | const char *clean; | |
aa4ed402 JH |
394 | } *user_convert, **user_convert_tail; |
395 | ||
ef90d6d4 | 396 | static int read_convert_config(const char *var, const char *value, void *cb) |
aa4ed402 JH |
397 | { |
398 | const char *ep, *name; | |
399 | int namelen; | |
400 | struct convert_driver *drv; | |
401 | ||
402 | /* | |
403 | * External conversion drivers are configured using | |
404 | * "filter.<name>.variable". | |
405 | */ | |
406 | if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6) | |
407 | return 0; | |
408 | name = var + 7; | |
409 | namelen = ep - name; | |
410 | for (drv = user_convert; drv; drv = drv->next) | |
411 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) | |
412 | break; | |
413 | if (!drv) { | |
aa4ed402 | 414 | drv = xcalloc(1, sizeof(struct convert_driver)); |
182af834 | 415 | drv->name = xmemdupz(name, namelen); |
aa4ed402 JH |
416 | *user_convert_tail = drv; |
417 | user_convert_tail = &(drv->next); | |
418 | } | |
419 | ||
420 | ep++; | |
421 | ||
422 | /* | |
423 | * filter.<name>.smudge and filter.<name>.clean specifies | |
424 | * the command line: | |
425 | * | |
426 | * command-line | |
427 | * | |
428 | * The command-line will not be interpolated in any way. | |
429 | */ | |
430 | ||
cd8be6c9 BH |
431 | if (!strcmp("smudge", ep)) |
432 | return git_config_string(&drv->smudge, var, value); | |
433 | ||
434 | if (!strcmp("clean", ep)) | |
435 | return git_config_string(&drv->clean, var, value); | |
aa4ed402 | 436 | |
aa4ed402 JH |
437 | return 0; |
438 | } | |
439 | ||
6073ee85 | 440 | static void setup_convert_check(struct git_attr_check *check) |
35ebfd6a | 441 | { |
5ec3e670 | 442 | static struct git_attr *attr_text; |
35ebfd6a | 443 | static struct git_attr *attr_crlf; |
fd6cce9e | 444 | static struct git_attr *attr_eol; |
3fed15f5 | 445 | static struct git_attr *attr_ident; |
aa4ed402 | 446 | static struct git_attr *attr_filter; |
35ebfd6a | 447 | |
5ec3e670 EB |
448 | if (!attr_text) { |
449 | attr_text = git_attr("text"); | |
7fb0eaa2 | 450 | attr_crlf = git_attr("crlf"); |
fd6cce9e | 451 | attr_eol = git_attr("eol"); |
7fb0eaa2 JH |
452 | attr_ident = git_attr("ident"); |
453 | attr_filter = git_attr("filter"); | |
aa4ed402 | 454 | user_convert_tail = &user_convert; |
ef90d6d4 | 455 | git_config(read_convert_config, NULL); |
3fed15f5 JH |
456 | } |
457 | check[0].attr = attr_crlf; | |
458 | check[1].attr = attr_ident; | |
aa4ed402 | 459 | check[2].attr = attr_filter; |
fd6cce9e | 460 | check[3].attr = attr_eol; |
5ec3e670 | 461 | check[4].attr = attr_text; |
3fed15f5 JH |
462 | } |
463 | ||
464 | static int count_ident(const char *cp, unsigned long size) | |
465 | { | |
466 | /* | |
af9b54bb | 467 | * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" |
3fed15f5 JH |
468 | */ |
469 | int cnt = 0; | |
470 | char ch; | |
471 | ||
472 | while (size) { | |
473 | ch = *cp++; | |
474 | size--; | |
475 | if (ch != '$') | |
476 | continue; | |
af9b54bb | 477 | if (size < 3) |
3fed15f5 | 478 | break; |
af9b54bb | 479 | if (memcmp("Id", cp, 2)) |
3fed15f5 | 480 | continue; |
af9b54bb AP |
481 | ch = cp[2]; |
482 | cp += 3; | |
483 | size -= 3; | |
3fed15f5 | 484 | if (ch == '$') |
af9b54bb | 485 | cnt++; /* $Id$ */ |
3fed15f5 JH |
486 | if (ch != ':') |
487 | continue; | |
488 | ||
489 | /* | |
af9b54bb | 490 | * "$Id: ... "; scan up to the closing dollar sign and discard. |
3fed15f5 JH |
491 | */ |
492 | while (size) { | |
493 | ch = *cp++; | |
494 | size--; | |
495 | if (ch == '$') { | |
496 | cnt++; | |
497 | break; | |
498 | } | |
499 | } | |
500 | } | |
501 | return cnt; | |
502 | } | |
503 | ||
5ecd293d PH |
504 | static int ident_to_git(const char *path, const char *src, size_t len, |
505 | struct strbuf *buf, int ident) | |
3fed15f5 | 506 | { |
5ecd293d | 507 | char *dst, *dollar; |
3fed15f5 | 508 | |
5ecd293d PH |
509 | if (!ident || !count_ident(src, len)) |
510 | return 0; | |
511 | ||
90d16ec0 PH |
512 | /* only grow if not in place */ |
513 | if (strbuf_avail(buf) + buf->len < len) | |
514 | strbuf_grow(buf, len - buf->len); | |
5ecd293d PH |
515 | dst = buf->buf; |
516 | for (;;) { | |
517 | dollar = memchr(src, '$', len); | |
518 | if (!dollar) | |
519 | break; | |
520 | memcpy(dst, src, dollar + 1 - src); | |
521 | dst += dollar + 1 - src; | |
522 | len -= dollar + 1 - src; | |
523 | src = dollar + 1; | |
524 | ||
525 | if (len > 3 && !memcmp(src, "Id:", 3)) { | |
526 | dollar = memchr(src + 3, '$', len - 3); | |
527 | if (!dollar) | |
528 | break; | |
af9b54bb AP |
529 | memcpy(dst, "Id$", 3); |
530 | dst += 3; | |
5ecd293d PH |
531 | len -= dollar + 1 - src; |
532 | src = dollar + 1; | |
3fed15f5 JH |
533 | } |
534 | } | |
5ecd293d PH |
535 | memcpy(dst, src, len); |
536 | strbuf_setlen(buf, dst + len - buf->buf); | |
537 | return 1; | |
3fed15f5 JH |
538 | } |
539 | ||
5ecd293d PH |
540 | static int ident_to_worktree(const char *path, const char *src, size_t len, |
541 | struct strbuf *buf, int ident) | |
3fed15f5 | 542 | { |
3fed15f5 | 543 | unsigned char sha1[20]; |
5ecd293d PH |
544 | char *to_free = NULL, *dollar; |
545 | int cnt; | |
3fed15f5 JH |
546 | |
547 | if (!ident) | |
5ecd293d | 548 | return 0; |
3fed15f5 | 549 | |
5ecd293d | 550 | cnt = count_ident(src, len); |
3fed15f5 | 551 | if (!cnt) |
5ecd293d | 552 | return 0; |
3fed15f5 | 553 | |
5ecd293d PH |
554 | /* are we "faking" in place editing ? */ |
555 | if (src == buf->buf) | |
b315c5c0 | 556 | to_free = strbuf_detach(buf, NULL); |
5ecd293d | 557 | hash_sha1_file(src, len, "blob", sha1); |
3fed15f5 | 558 | |
5ecd293d PH |
559 | strbuf_grow(buf, len + cnt * 43); |
560 | for (;;) { | |
561 | /* step 1: run to the next '$' */ | |
562 | dollar = memchr(src, '$', len); | |
563 | if (!dollar) | |
564 | break; | |
565 | strbuf_add(buf, src, dollar + 1 - src); | |
566 | len -= dollar + 1 - src; | |
567 | src = dollar + 1; | |
c23290d5 | 568 | |
5ecd293d PH |
569 | /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */ |
570 | if (len < 3 || memcmp("Id", src, 2)) | |
3fed15f5 JH |
571 | continue; |
572 | ||
5ecd293d PH |
573 | /* step 3: skip over Id$ or Id:xxxxx$ */ |
574 | if (src[2] == '$') { | |
575 | src += 3; | |
576 | len -= 3; | |
577 | } else if (src[2] == ':') { | |
578 | /* | |
579 | * It's possible that an expanded Id has crept its way into the | |
580 | * repository, we cope with that by stripping the expansion out | |
581 | */ | |
582 | dollar = memchr(src + 3, '$', len - 3); | |
583 | if (!dollar) { | |
584 | /* incomplete keyword, no more '$', so just quit the loop */ | |
585 | break; | |
586 | } | |
c23290d5 | 587 | |
5ecd293d PH |
588 | len -= dollar + 1 - src; |
589 | src = dollar + 1; | |
590 | } else { | |
591 | /* it wasn't a "Id$" or "Id:xxxx$" */ | |
592 | continue; | |
593 | } | |
c23290d5 | 594 | |
5ecd293d PH |
595 | /* step 4: substitute */ |
596 | strbuf_addstr(buf, "Id: "); | |
597 | strbuf_add(buf, sha1_to_hex(sha1), 40); | |
598 | strbuf_addstr(buf, " $"); | |
3fed15f5 | 599 | } |
5ecd293d | 600 | strbuf_add(buf, src, len); |
3fed15f5 | 601 | |
5ecd293d PH |
602 | free(to_free); |
603 | return 1; | |
35ebfd6a JH |
604 | } |
605 | ||
6073ee85 | 606 | static int git_path_check_crlf(const char *path, struct git_attr_check *check) |
35ebfd6a | 607 | { |
6073ee85 JH |
608 | const char *value = check->value; |
609 | ||
610 | if (ATTR_TRUE(value)) | |
611 | return CRLF_TEXT; | |
612 | else if (ATTR_FALSE(value)) | |
613 | return CRLF_BINARY; | |
614 | else if (ATTR_UNSET(value)) | |
615 | ; | |
616 | else if (!strcmp(value, "input")) | |
617 | return CRLF_INPUT; | |
fd6cce9e EB |
618 | else if (!strcmp(value, "auto")) |
619 | return CRLF_AUTO; | |
163b9591 | 620 | return CRLF_GUESS; |
35ebfd6a JH |
621 | } |
622 | ||
fd6cce9e EB |
623 | static int git_path_check_eol(const char *path, struct git_attr_check *check) |
624 | { | |
625 | const char *value = check->value; | |
626 | ||
627 | if (ATTR_UNSET(value)) | |
628 | ; | |
629 | else if (!strcmp(value, "lf")) | |
630 | return EOL_LF; | |
631 | else if (!strcmp(value, "crlf")) | |
632 | return EOL_CRLF; | |
633 | return EOL_UNSET; | |
634 | } | |
635 | ||
aa4ed402 JH |
636 | static struct convert_driver *git_path_check_convert(const char *path, |
637 | struct git_attr_check *check) | |
638 | { | |
639 | const char *value = check->value; | |
640 | struct convert_driver *drv; | |
641 | ||
642 | if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value)) | |
643 | return NULL; | |
644 | for (drv = user_convert; drv; drv = drv->next) | |
645 | if (!strcmp(value, drv->name)) | |
646 | return drv; | |
647 | return NULL; | |
648 | } | |
649 | ||
3fed15f5 JH |
650 | static int git_path_check_ident(const char *path, struct git_attr_check *check) |
651 | { | |
652 | const char *value = check->value; | |
653 | ||
654 | return !!ATTR_TRUE(value); | |
655 | } | |
656 | ||
5ec3e670 EB |
657 | enum action determine_action(enum action text_attr, enum eol eol_attr) { |
658 | if (text_attr == CRLF_BINARY) | |
fd6cce9e EB |
659 | return CRLF_BINARY; |
660 | if (eol_attr == EOL_LF) | |
661 | return CRLF_INPUT; | |
662 | if (eol_attr == EOL_CRLF) | |
663 | return CRLF_CRLF; | |
5ec3e670 | 664 | return text_attr; |
fd6cce9e EB |
665 | } |
666 | ||
21e5ad50 SP |
667 | int convert_to_git(const char *path, const char *src, size_t len, |
668 | struct strbuf *dst, enum safe_crlf checksafe) | |
35ebfd6a | 669 | { |
5ec3e670 | 670 | struct git_attr_check check[5]; |
fd6cce9e EB |
671 | enum action action = CRLF_GUESS; |
672 | enum eol eol = EOL_UNSET; | |
5ecd293d | 673 | int ident = 0, ret = 0; |
cd8be6c9 | 674 | const char *filter = NULL; |
6073ee85 JH |
675 | |
676 | setup_convert_check(check); | |
3fed15f5 | 677 | if (!git_checkattr(path, ARRAY_SIZE(check), check)) { |
aa4ed402 | 678 | struct convert_driver *drv; |
5ec3e670 EB |
679 | action = git_path_check_crlf(path, check + 4); |
680 | if (action == CRLF_GUESS) | |
681 | action = git_path_check_crlf(path, check + 0); | |
3fed15f5 | 682 | ident = git_path_check_ident(path, check + 1); |
aa4ed402 | 683 | drv = git_path_check_convert(path, check + 2); |
fd6cce9e | 684 | eol = git_path_check_eol(path, check + 3); |
aa4ed402 JH |
685 | if (drv && drv->clean) |
686 | filter = drv->clean; | |
3fed15f5 JH |
687 | } |
688 | ||
5ecd293d PH |
689 | ret |= apply_filter(path, src, len, dst, filter); |
690 | if (ret) { | |
691 | src = dst->buf; | |
692 | len = dst->len; | |
aa4ed402 | 693 | } |
fd6cce9e EB |
694 | action = determine_action(action, eol); |
695 | ret |= crlf_to_git(path, src, len, dst, action, checksafe); | |
5ecd293d PH |
696 | if (ret) { |
697 | src = dst->buf; | |
698 | len = dst->len; | |
6073ee85 | 699 | } |
5ecd293d | 700 | return ret | ident_to_git(path, src, len, dst, ident); |
35ebfd6a JH |
701 | } |
702 | ||
5ecd293d | 703 | int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst) |
35ebfd6a | 704 | { |
5ec3e670 | 705 | struct git_attr_check check[5]; |
fd6cce9e EB |
706 | enum action action = CRLF_GUESS; |
707 | enum eol eol = EOL_UNSET; | |
5ecd293d | 708 | int ident = 0, ret = 0; |
cd8be6c9 | 709 | const char *filter = NULL; |
6073ee85 JH |
710 | |
711 | setup_convert_check(check); | |
3fed15f5 | 712 | if (!git_checkattr(path, ARRAY_SIZE(check), check)) { |
aa4ed402 | 713 | struct convert_driver *drv; |
5ec3e670 EB |
714 | action = git_path_check_crlf(path, check + 4); |
715 | if (action == CRLF_GUESS) | |
716 | action = git_path_check_crlf(path, check + 0); | |
3fed15f5 | 717 | ident = git_path_check_ident(path, check + 1); |
aa4ed402 | 718 | drv = git_path_check_convert(path, check + 2); |
fd6cce9e | 719 | eol = git_path_check_eol(path, check + 3); |
aa4ed402 JH |
720 | if (drv && drv->smudge) |
721 | filter = drv->smudge; | |
6073ee85 | 722 | } |
3fed15f5 | 723 | |
5ecd293d PH |
724 | ret |= ident_to_worktree(path, src, len, dst, ident); |
725 | if (ret) { | |
726 | src = dst->buf; | |
727 | len = dst->len; | |
3fed15f5 | 728 | } |
fd6cce9e EB |
729 | action = determine_action(action, eol); |
730 | ret |= crlf_to_worktree(path, src, len, dst, action); | |
5ecd293d PH |
731 | if (ret) { |
732 | src = dst->buf; | |
733 | len = dst->len; | |
aa4ed402 | 734 | } |
5ecd293d | 735 | return ret | apply_filter(path, src, len, dst, filter); |
35ebfd6a | 736 | } |