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