]>
Commit | Line | Data |
---|---|---|
6c510bee | 1 | #include "cache.h" |
35ebfd6a | 2 | #include "attr.h" |
3fed15f5 | 3 | #include "run-command.h" |
a2b665de | 4 | #include "quote.h" |
35ebfd6a | 5 | |
6c510bee LT |
6 | /* |
7 | * convert.c - convert a file when checking it out and checking it in. | |
8 | * | |
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 | |
942e7747 | 12 | * translation when the "text" attribute or "auto_crlf" option is set. |
6c510bee LT |
13 | */ |
14 | ||
c61dcff9 | 15 | enum crlf_action { |
fd6cce9e EB |
16 | CRLF_GUESS = -1, |
17 | CRLF_BINARY = 0, | |
18 | CRLF_TEXT, | |
19 | CRLF_INPUT, | |
20 | CRLF_CRLF, | |
c9b6782a | 21 | CRLF_AUTO |
fd6cce9e | 22 | }; |
163b9591 | 23 | |
6c510bee | 24 | struct text_stat { |
28624193 DP |
25 | /* NUL, CR, LF and CRLF counts */ |
26 | unsigned nul, cr, lf, crlf; | |
6c510bee LT |
27 | |
28 | /* These are just approximations! */ | |
29 | unsigned printable, nonprintable; | |
30 | }; | |
31 | ||
32 | static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) | |
33 | { | |
34 | unsigned long i; | |
35 | ||
36 | memset(stats, 0, sizeof(*stats)); | |
37 | ||
38 | for (i = 0; i < size; i++) { | |
39 | unsigned char c = buf[i]; | |
40 | if (c == '\r') { | |
41 | stats->cr++; | |
42 | if (i+1 < size && buf[i+1] == '\n') | |
43 | stats->crlf++; | |
44 | continue; | |
45 | } | |
46 | if (c == '\n') { | |
47 | stats->lf++; | |
48 | continue; | |
49 | } | |
50 | if (c == 127) | |
51 | /* DEL */ | |
52 | stats->nonprintable++; | |
53 | else if (c < 32) { | |
54 | switch (c) { | |
55 | /* BS, HT, ESC and FF */ | |
56 | case '\b': case '\t': case '\033': case '\014': | |
57 | stats->printable++; | |
58 | break; | |
28624193 DP |
59 | case 0: |
60 | stats->nul++; | |
61 | /* fall through */ | |
6c510bee LT |
62 | default: |
63 | stats->nonprintable++; | |
64 | } | |
65 | } | |
66 | else | |
67 | stats->printable++; | |
68 | } | |
f9dd4bf4 DK |
69 | |
70 | /* If file ends with EOF then don't count this EOF as non-printable. */ | |
71 | if (size >= 1 && buf[size-1] == '\032') | |
72 | stats->nonprintable--; | |
6c510bee LT |
73 | } |
74 | ||
75 | /* | |
76 | * The same heuristics as diff.c::mmfile_is_binary() | |
77 | */ | |
78 | static int is_binary(unsigned long size, struct text_stat *stats) | |
79 | { | |
80 | ||
28624193 DP |
81 | if (stats->nul) |
82 | return 1; | |
6c510bee LT |
83 | if ((stats->printable >> 7) < stats->nonprintable) |
84 | return 1; | |
85 | /* | |
86 | * Other heuristics? Average line length might be relevant, | |
87 | * as might LF vs CR vs CRLF counts.. | |
88 | * | |
89 | * NOTE! It might be normal to have a low ratio of CRLF to LF | |
90 | * (somebody starts with a LF-only file and edits it with an editor | |
91 | * that adds CRLF only to lines that are added..). But do we | |
92 | * want to support CR-only? Probably not. | |
93 | */ | |
94 | return 0; | |
95 | } | |
96 | ||
c61dcff9 | 97 | static enum eol output_eol(enum crlf_action crlf_action) |
f217f0e8 | 98 | { |
c61dcff9 | 99 | switch (crlf_action) { |
942e7747 EB |
100 | case CRLF_BINARY: |
101 | return EOL_UNSET; | |
102 | case CRLF_CRLF: | |
103 | return EOL_CRLF; | |
104 | case CRLF_INPUT: | |
105 | return EOL_LF; | |
106 | case CRLF_GUESS: | |
107 | if (!auto_crlf) | |
108 | return EOL_UNSET; | |
109 | /* fall through */ | |
110 | case CRLF_TEXT: | |
111 | case CRLF_AUTO: | |
112 | if (auto_crlf == AUTO_CRLF_TRUE) | |
113 | return EOL_CRLF; | |
114 | else if (auto_crlf == AUTO_CRLF_INPUT) | |
115 | return EOL_LF; | |
ec70f52f | 116 | else if (core_eol == EOL_UNSET) |
942e7747 EB |
117 | return EOL_NATIVE; |
118 | } | |
ec70f52f | 119 | return core_eol; |
942e7747 EB |
120 | } |
121 | ||
c61dcff9 | 122 | static void check_safe_crlf(const char *path, enum crlf_action crlf_action, |
21e5ad50 SP |
123 | struct text_stat *stats, enum safe_crlf checksafe) |
124 | { | |
125 | if (!checksafe) | |
126 | return; | |
127 | ||
c61dcff9 | 128 | if (output_eol(crlf_action) == EOL_LF) { |
21e5ad50 SP |
129 | /* |
130 | * CRLFs would not be restored by checkout: | |
131 | * check if we'd remove CRLFs | |
132 | */ | |
133 | if (stats->crlf) { | |
134 | if (checksafe == SAFE_CRLF_WARN) | |
942e7747 | 135 | warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path); |
21e5ad50 SP |
136 | else /* i.e. SAFE_CRLF_FAIL */ |
137 | die("CRLF would be replaced by LF in %s.", path); | |
138 | } | |
c61dcff9 | 139 | } else if (output_eol(crlf_action) == EOL_CRLF) { |
21e5ad50 SP |
140 | /* |
141 | * CRLFs would be added by checkout: | |
142 | * check if we have "naked" LFs | |
143 | */ | |
144 | if (stats->lf != stats->crlf) { | |
145 | if (checksafe == SAFE_CRLF_WARN) | |
942e7747 | 146 | warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path); |
21e5ad50 SP |
147 | else /* i.e. SAFE_CRLF_FAIL */ |
148 | die("LF would be replaced by CRLF in %s", path); | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
c4805393 FAG |
153 | static int has_cr_in_index(const char *path) |
154 | { | |
155 | int pos, len; | |
156 | unsigned long sz; | |
157 | enum object_type type; | |
158 | void *data; | |
159 | int has_cr; | |
160 | struct index_state *istate = &the_index; | |
161 | ||
162 | len = strlen(path); | |
163 | pos = index_name_pos(istate, path, len); | |
164 | if (pos < 0) { | |
165 | /* | |
166 | * We might be in the middle of a merge, in which | |
167 | * case we would read stage #2 (ours). | |
168 | */ | |
169 | int i; | |
170 | for (i = -pos - 1; | |
171 | (pos < 0 && i < istate->cache_nr && | |
172 | !strcmp(istate->cache[i]->name, path)); | |
173 | i++) | |
174 | if (ce_stage(istate->cache[i]) == 2) | |
175 | pos = i; | |
176 | } | |
177 | if (pos < 0) | |
178 | return 0; | |
179 | data = read_sha1_file(istate->cache[pos]->sha1, &type, &sz); | |
180 | if (!data || type != OBJ_BLOB) { | |
181 | free(data); | |
182 | return 0; | |
183 | } | |
184 | ||
185 | has_cr = memchr(data, '\r', sz) != NULL; | |
186 | free(data); | |
187 | return has_cr; | |
188 | } | |
189 | ||
5ecd293d | 190 | static int crlf_to_git(const char *path, const char *src, size_t len, |
c61dcff9 JH |
191 | struct strbuf *buf, |
192 | enum crlf_action crlf_action, enum safe_crlf checksafe) | |
6c510bee | 193 | { |
6c510bee | 194 | struct text_stat stats; |
5ecd293d | 195 | char *dst; |
6c510bee | 196 | |
c61dcff9 JH |
197 | if (crlf_action == CRLF_BINARY || |
198 | (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) || !len) | |
5ecd293d | 199 | return 0; |
6c510bee | 200 | |
5ecd293d | 201 | gather_stats(src, len, &stats); |
6c510bee | 202 | |
c61dcff9 | 203 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) { |
201ac8ef JH |
204 | /* |
205 | * We're currently not going to even try to convert stuff | |
206 | * that has bare CR characters. Does anybody do that crazy | |
207 | * stuff? | |
208 | */ | |
209 | if (stats.cr != stats.crlf) | |
5ecd293d | 210 | return 0; |
201ac8ef JH |
211 | |
212 | /* | |
213 | * And add some heuristics for binary vs text, of course... | |
214 | */ | |
5ecd293d PH |
215 | if (is_binary(len, &stats)) |
216 | return 0; | |
c4805393 | 217 | |
c61dcff9 | 218 | if (crlf_action == CRLF_GUESS) { |
fd6cce9e EB |
219 | /* |
220 | * If the file in the index has any CR in it, do not convert. | |
221 | * This is the new safer autocrlf handling. | |
222 | */ | |
223 | if (has_cr_in_index(path)) | |
224 | return 0; | |
225 | } | |
201ac8ef | 226 | } |
6c510bee | 227 | |
c61dcff9 | 228 | check_safe_crlf(path, crlf_action, &stats, checksafe); |
21e5ad50 SP |
229 | |
230 | /* Optimization: No CR? Nothing to convert, regardless. */ | |
231 | if (!stats.cr) | |
232 | return 0; | |
233 | ||
90d16ec0 PH |
234 | /* only grow if not in place */ |
235 | if (strbuf_avail(buf) + buf->len < len) | |
236 | strbuf_grow(buf, len - buf->len); | |
5ecd293d | 237 | dst = buf->buf; |
c61dcff9 | 238 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) { |
163b9591 JH |
239 | /* |
240 | * If we guessed, we already know we rejected a file with | |
241 | * lone CR, and we can strip a CR without looking at what | |
242 | * follow it. | |
243 | */ | |
201ac8ef | 244 | do { |
ac78e548 | 245 | unsigned char c = *src++; |
201ac8ef | 246 | if (c != '\r') |
ac78e548 | 247 | *dst++ = c; |
5ecd293d | 248 | } while (--len); |
201ac8ef JH |
249 | } else { |
250 | do { | |
ac78e548 | 251 | unsigned char c = *src++; |
5ecd293d | 252 | if (! (c == '\r' && (1 < len && *src == '\n'))) |
ac78e548 | 253 | *dst++ = c; |
5ecd293d | 254 | } while (--len); |
201ac8ef | 255 | } |
5ecd293d PH |
256 | strbuf_setlen(buf, dst - buf->buf); |
257 | return 1; | |
6c510bee LT |
258 | } |
259 | ||
5ecd293d | 260 | static int crlf_to_worktree(const char *path, const char *src, size_t len, |
c61dcff9 | 261 | struct strbuf *buf, enum crlf_action crlf_action) |
6c510bee | 262 | { |
5ecd293d | 263 | char *to_free = NULL; |
6c510bee | 264 | struct text_stat stats; |
6c510bee | 265 | |
c61dcff9 | 266 | if (!len || output_eol(crlf_action) != EOL_CRLF) |
5ecd293d | 267 | return 0; |
6c510bee | 268 | |
5ecd293d | 269 | gather_stats(src, len, &stats); |
6c510bee LT |
270 | |
271 | /* No LF? Nothing to convert, regardless. */ | |
272 | if (!stats.lf) | |
5ecd293d | 273 | return 0; |
6c510bee LT |
274 | |
275 | /* Was it already in CRLF format? */ | |
276 | if (stats.lf == stats.crlf) | |
5ecd293d | 277 | return 0; |
6c510bee | 278 | |
c61dcff9 JH |
279 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) { |
280 | if (crlf_action == CRLF_GUESS) { | |
fd6cce9e EB |
281 | /* If we have any CR or CRLF line endings, we do not touch it */ |
282 | /* This is the new safer autocrlf-handling */ | |
283 | if (stats.cr > 0 || stats.crlf > 0) | |
284 | return 0; | |
285 | } | |
c4805393 | 286 | |
201ac8ef JH |
287 | /* If we have any bare CR characters, we're not going to touch it */ |
288 | if (stats.cr != stats.crlf) | |
5ecd293d | 289 | return 0; |
6c510bee | 290 | |
5ecd293d PH |
291 | if (is_binary(len, &stats)) |
292 | return 0; | |
201ac8ef | 293 | } |
6c510bee | 294 | |
5ecd293d PH |
295 | /* are we "faking" in place editing ? */ |
296 | if (src == buf->buf) | |
b315c5c0 | 297 | to_free = strbuf_detach(buf, NULL); |
5ecd293d PH |
298 | |
299 | strbuf_grow(buf, len + stats.lf - stats.crlf); | |
300 | for (;;) { | |
301 | const char *nl = memchr(src, '\n', len); | |
302 | if (!nl) | |
303 | break; | |
304 | if (nl > src && nl[-1] == '\r') { | |
305 | strbuf_add(buf, src, nl + 1 - src); | |
306 | } else { | |
307 | strbuf_add(buf, src, nl - src); | |
308 | strbuf_addstr(buf, "\r\n"); | |
309 | } | |
310 | len -= nl + 1 - src; | |
311 | src = nl + 1; | |
312 | } | |
313 | strbuf_add(buf, src, len); | |
314 | ||
315 | free(to_free); | |
316 | return 1; | |
6c510bee | 317 | } |
35ebfd6a | 318 | |
546bb582 JS |
319 | struct filter_params { |
320 | const char *src; | |
321 | unsigned long size; | |
322 | const char *cmd; | |
a2b665de | 323 | const char *path; |
546bb582 JS |
324 | }; |
325 | ||
ae6a5609 | 326 | static int filter_buffer(int in, int out, void *data) |
aa4ed402 JH |
327 | { |
328 | /* | |
329 | * Spawn cmd and feed the buffer contents through its stdin. | |
330 | */ | |
331 | struct child_process child_process; | |
546bb582 | 332 | struct filter_params *params = (struct filter_params *)data; |
aa4ed402 | 333 | int write_err, status; |
66dbfd55 GV |
334 | const char *argv[] = { NULL, NULL }; |
335 | ||
a2b665de PW |
336 | /* apply % substitution to cmd */ |
337 | struct strbuf cmd = STRBUF_INIT; | |
338 | struct strbuf path = STRBUF_INIT; | |
339 | struct strbuf_expand_dict_entry dict[] = { | |
340 | { "f", NULL, }, | |
341 | { NULL, NULL, }, | |
342 | }; | |
343 | ||
344 | /* quote the path to preserve spaces, etc. */ | |
345 | sq_quote_buf(&path, params->path); | |
346 | dict[0].value = path.buf; | |
347 | ||
348 | /* expand all %f with the quoted path */ | |
349 | strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict); | |
350 | strbuf_release(&path); | |
351 | ||
352 | argv[0] = cmd.buf; | |
aa4ed402 JH |
353 | |
354 | memset(&child_process, 0, sizeof(child_process)); | |
dc1bfdcd | 355 | child_process.argv = argv; |
ac0ba18d | 356 | child_process.use_shell = 1; |
dc1bfdcd | 357 | child_process.in = -1; |
ae6a5609 | 358 | child_process.out = out; |
aa4ed402 | 359 | |
dc1bfdcd | 360 | if (start_command(&child_process)) |
546bb582 | 361 | return error("cannot fork to run external filter %s", params->cmd); |
aa4ed402 | 362 | |
546bb582 | 363 | write_err = (write_in_full(child_process.in, params->src, params->size) < 0); |
dc1bfdcd | 364 | if (close(child_process.in)) |
aa4ed402 JH |
365 | write_err = 1; |
366 | if (write_err) | |
546bb582 | 367 | error("cannot feed the input to external filter %s", params->cmd); |
aa4ed402 JH |
368 | |
369 | status = finish_command(&child_process); | |
370 | if (status) | |
5709e036 | 371 | error("external filter %s failed %d", params->cmd, status); |
a2b665de PW |
372 | |
373 | strbuf_release(&cmd); | |
aa4ed402 JH |
374 | return (write_err || status); |
375 | } | |
376 | ||
5ecd293d PH |
377 | static int apply_filter(const char *path, const char *src, size_t len, |
378 | struct strbuf *dst, const char *cmd) | |
aa4ed402 JH |
379 | { |
380 | /* | |
381 | * Create a pipeline to have the command filter the buffer's | |
382 | * contents. | |
383 | * | |
384 | * (child --> cmd) --> us | |
385 | */ | |
546bb582 | 386 | int ret = 1; |
f285a2d7 | 387 | struct strbuf nbuf = STRBUF_INIT; |
546bb582 JS |
388 | struct async async; |
389 | struct filter_params params; | |
aa4ed402 JH |
390 | |
391 | if (!cmd) | |
5ecd293d | 392 | return 0; |
aa4ed402 | 393 | |
546bb582 JS |
394 | memset(&async, 0, sizeof(async)); |
395 | async.proc = filter_buffer; | |
396 | async.data = ¶ms; | |
ae6a5609 | 397 | async.out = -1; |
546bb582 JS |
398 | params.src = src; |
399 | params.size = len; | |
400 | params.cmd = cmd; | |
a2b665de | 401 | params.path = path; |
aa4ed402 JH |
402 | |
403 | fflush(NULL); | |
546bb582 JS |
404 | if (start_async(&async)) |
405 | return 0; /* error was already reported */ | |
aa4ed402 | 406 | |
546bb582 | 407 | if (strbuf_read(&nbuf, async.out, len) < 0) { |
5ecd293d PH |
408 | error("read from external filter %s failed", cmd); |
409 | ret = 0; | |
aa4ed402 | 410 | } |
546bb582 | 411 | if (close(async.out)) { |
90d16ec0 | 412 | error("read from external filter %s failed", cmd); |
5ecd293d | 413 | ret = 0; |
aa4ed402 | 414 | } |
546bb582 JS |
415 | if (finish_async(&async)) { |
416 | error("external filter %s failed", cmd); | |
5ecd293d | 417 | ret = 0; |
aa4ed402 JH |
418 | } |
419 | ||
5ecd293d | 420 | if (ret) { |
90d16ec0 | 421 | strbuf_swap(dst, &nbuf); |
5ecd293d | 422 | } |
90d16ec0 | 423 | strbuf_release(&nbuf); |
5ecd293d | 424 | return ret; |
aa4ed402 JH |
425 | } |
426 | ||
427 | static struct convert_driver { | |
428 | const char *name; | |
429 | struct convert_driver *next; | |
cd8be6c9 BH |
430 | const char *smudge; |
431 | const char *clean; | |
aa4ed402 JH |
432 | } *user_convert, **user_convert_tail; |
433 | ||
ef90d6d4 | 434 | static int read_convert_config(const char *var, const char *value, void *cb) |
aa4ed402 JH |
435 | { |
436 | const char *ep, *name; | |
437 | int namelen; | |
438 | struct convert_driver *drv; | |
439 | ||
440 | /* | |
441 | * External conversion drivers are configured using | |
442 | * "filter.<name>.variable". | |
443 | */ | |
444 | if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6) | |
445 | return 0; | |
446 | name = var + 7; | |
447 | namelen = ep - name; | |
448 | for (drv = user_convert; drv; drv = drv->next) | |
449 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) | |
450 | break; | |
451 | if (!drv) { | |
aa4ed402 | 452 | drv = xcalloc(1, sizeof(struct convert_driver)); |
182af834 | 453 | drv->name = xmemdupz(name, namelen); |
aa4ed402 JH |
454 | *user_convert_tail = drv; |
455 | user_convert_tail = &(drv->next); | |
456 | } | |
457 | ||
458 | ep++; | |
459 | ||
460 | /* | |
461 | * filter.<name>.smudge and filter.<name>.clean specifies | |
462 | * the command line: | |
463 | * | |
464 | * command-line | |
465 | * | |
466 | * The command-line will not be interpolated in any way. | |
467 | */ | |
468 | ||
cd8be6c9 BH |
469 | if (!strcmp("smudge", ep)) |
470 | return git_config_string(&drv->smudge, var, value); | |
471 | ||
472 | if (!strcmp("clean", ep)) | |
473 | return git_config_string(&drv->clean, var, value); | |
aa4ed402 | 474 | |
aa4ed402 JH |
475 | return 0; |
476 | } | |
477 | ||
3fed15f5 JH |
478 | static int count_ident(const char *cp, unsigned long size) |
479 | { | |
480 | /* | |
af9b54bb | 481 | * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" |
3fed15f5 JH |
482 | */ |
483 | int cnt = 0; | |
484 | char ch; | |
485 | ||
486 | while (size) { | |
487 | ch = *cp++; | |
488 | size--; | |
489 | if (ch != '$') | |
490 | continue; | |
af9b54bb | 491 | if (size < 3) |
3fed15f5 | 492 | break; |
af9b54bb | 493 | if (memcmp("Id", cp, 2)) |
3fed15f5 | 494 | continue; |
af9b54bb AP |
495 | ch = cp[2]; |
496 | cp += 3; | |
497 | size -= 3; | |
3fed15f5 | 498 | if (ch == '$') |
af9b54bb | 499 | cnt++; /* $Id$ */ |
3fed15f5 JH |
500 | if (ch != ':') |
501 | continue; | |
502 | ||
503 | /* | |
af9b54bb | 504 | * "$Id: ... "; scan up to the closing dollar sign and discard. |
3fed15f5 JH |
505 | */ |
506 | while (size) { | |
507 | ch = *cp++; | |
508 | size--; | |
509 | if (ch == '$') { | |
510 | cnt++; | |
511 | break; | |
512 | } | |
a9f3049f HG |
513 | if (ch == '\n') |
514 | break; | |
3fed15f5 JH |
515 | } |
516 | } | |
517 | return cnt; | |
518 | } | |
519 | ||
5ecd293d PH |
520 | static int ident_to_git(const char *path, const char *src, size_t len, |
521 | struct strbuf *buf, int ident) | |
3fed15f5 | 522 | { |
5ecd293d | 523 | char *dst, *dollar; |
3fed15f5 | 524 | |
5ecd293d PH |
525 | if (!ident || !count_ident(src, len)) |
526 | return 0; | |
527 | ||
90d16ec0 PH |
528 | /* only grow if not in place */ |
529 | if (strbuf_avail(buf) + buf->len < len) | |
530 | strbuf_grow(buf, len - buf->len); | |
5ecd293d PH |
531 | dst = buf->buf; |
532 | for (;;) { | |
533 | dollar = memchr(src, '$', len); | |
534 | if (!dollar) | |
535 | break; | |
77321184 | 536 | memmove(dst, src, dollar + 1 - src); |
5ecd293d PH |
537 | dst += dollar + 1 - src; |
538 | len -= dollar + 1 - src; | |
539 | src = dollar + 1; | |
540 | ||
541 | if (len > 3 && !memcmp(src, "Id:", 3)) { | |
542 | dollar = memchr(src + 3, '$', len - 3); | |
543 | if (!dollar) | |
544 | break; | |
a9f3049f HG |
545 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
546 | /* Line break before the next dollar. */ | |
547 | continue; | |
548 | } | |
549 | ||
af9b54bb AP |
550 | memcpy(dst, "Id$", 3); |
551 | dst += 3; | |
5ecd293d PH |
552 | len -= dollar + 1 - src; |
553 | src = dollar + 1; | |
3fed15f5 JH |
554 | } |
555 | } | |
77321184 | 556 | memmove(dst, src, len); |
5ecd293d PH |
557 | strbuf_setlen(buf, dst + len - buf->buf); |
558 | return 1; | |
3fed15f5 JH |
559 | } |
560 | ||
5ecd293d PH |
561 | static int ident_to_worktree(const char *path, const char *src, size_t len, |
562 | struct strbuf *buf, int ident) | |
3fed15f5 | 563 | { |
3fed15f5 | 564 | unsigned char sha1[20]; |
07814d90 | 565 | char *to_free = NULL, *dollar, *spc; |
5ecd293d | 566 | int cnt; |
3fed15f5 JH |
567 | |
568 | if (!ident) | |
5ecd293d | 569 | return 0; |
3fed15f5 | 570 | |
5ecd293d | 571 | cnt = count_ident(src, len); |
3fed15f5 | 572 | if (!cnt) |
5ecd293d | 573 | return 0; |
3fed15f5 | 574 | |
5ecd293d PH |
575 | /* are we "faking" in place editing ? */ |
576 | if (src == buf->buf) | |
b315c5c0 | 577 | to_free = strbuf_detach(buf, NULL); |
5ecd293d | 578 | hash_sha1_file(src, len, "blob", sha1); |
3fed15f5 | 579 | |
5ecd293d PH |
580 | strbuf_grow(buf, len + cnt * 43); |
581 | for (;;) { | |
582 | /* step 1: run to the next '$' */ | |
583 | dollar = memchr(src, '$', len); | |
584 | if (!dollar) | |
585 | break; | |
586 | strbuf_add(buf, src, dollar + 1 - src); | |
587 | len -= dollar + 1 - src; | |
588 | src = dollar + 1; | |
c23290d5 | 589 | |
5ecd293d PH |
590 | /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */ |
591 | if (len < 3 || memcmp("Id", src, 2)) | |
3fed15f5 JH |
592 | continue; |
593 | ||
5ecd293d PH |
594 | /* step 3: skip over Id$ or Id:xxxxx$ */ |
595 | if (src[2] == '$') { | |
596 | src += 3; | |
597 | len -= 3; | |
598 | } else if (src[2] == ':') { | |
599 | /* | |
600 | * It's possible that an expanded Id has crept its way into the | |
07814d90 HG |
601 | * repository, we cope with that by stripping the expansion out. |
602 | * This is probably not a good idea, since it will cause changes | |
603 | * on checkout, which won't go away by stash, but let's keep it | |
604 | * for git-style ids. | |
5ecd293d PH |
605 | */ |
606 | dollar = memchr(src + 3, '$', len - 3); | |
607 | if (!dollar) { | |
608 | /* incomplete keyword, no more '$', so just quit the loop */ | |
609 | break; | |
610 | } | |
c23290d5 | 611 | |
a9f3049f HG |
612 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
613 | /* Line break before the next dollar. */ | |
614 | continue; | |
615 | } | |
616 | ||
07814d90 HG |
617 | spc = memchr(src + 4, ' ', dollar - src - 4); |
618 | if (spc && spc < dollar-1) { | |
619 | /* There are spaces in unexpected places. | |
620 | * This is probably an id from some other | |
621 | * versioning system. Keep it for now. | |
622 | */ | |
623 | continue; | |
624 | } | |
625 | ||
5ecd293d PH |
626 | len -= dollar + 1 - src; |
627 | src = dollar + 1; | |
628 | } else { | |
629 | /* it wasn't a "Id$" or "Id:xxxx$" */ | |
630 | continue; | |
631 | } | |
c23290d5 | 632 | |
5ecd293d PH |
633 | /* step 4: substitute */ |
634 | strbuf_addstr(buf, "Id: "); | |
635 | strbuf_add(buf, sha1_to_hex(sha1), 40); | |
636 | strbuf_addstr(buf, " $"); | |
3fed15f5 | 637 | } |
5ecd293d | 638 | strbuf_add(buf, src, len); |
3fed15f5 | 639 | |
5ecd293d PH |
640 | free(to_free); |
641 | return 1; | |
35ebfd6a JH |
642 | } |
643 | ||
6073ee85 | 644 | static int git_path_check_crlf(const char *path, struct git_attr_check *check) |
35ebfd6a | 645 | { |
6073ee85 JH |
646 | const char *value = check->value; |
647 | ||
648 | if (ATTR_TRUE(value)) | |
649 | return CRLF_TEXT; | |
650 | else if (ATTR_FALSE(value)) | |
651 | return CRLF_BINARY; | |
652 | else if (ATTR_UNSET(value)) | |
653 | ; | |
654 | else if (!strcmp(value, "input")) | |
655 | return CRLF_INPUT; | |
fd6cce9e EB |
656 | else if (!strcmp(value, "auto")) |
657 | return CRLF_AUTO; | |
163b9591 | 658 | return CRLF_GUESS; |
35ebfd6a JH |
659 | } |
660 | ||
fd6cce9e EB |
661 | static int git_path_check_eol(const char *path, struct git_attr_check *check) |
662 | { | |
663 | const char *value = check->value; | |
664 | ||
665 | if (ATTR_UNSET(value)) | |
666 | ; | |
667 | else if (!strcmp(value, "lf")) | |
668 | return EOL_LF; | |
669 | else if (!strcmp(value, "crlf")) | |
670 | return EOL_CRLF; | |
671 | return EOL_UNSET; | |
672 | } | |
673 | ||
aa4ed402 JH |
674 | static struct convert_driver *git_path_check_convert(const char *path, |
675 | struct git_attr_check *check) | |
676 | { | |
677 | const char *value = check->value; | |
678 | struct convert_driver *drv; | |
679 | ||
680 | if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value)) | |
681 | return NULL; | |
682 | for (drv = user_convert; drv; drv = drv->next) | |
683 | if (!strcmp(value, drv->name)) | |
684 | return drv; | |
685 | return NULL; | |
686 | } | |
687 | ||
3fed15f5 JH |
688 | static int git_path_check_ident(const char *path, struct git_attr_check *check) |
689 | { | |
690 | const char *value = check->value; | |
691 | ||
692 | return !!ATTR_TRUE(value); | |
693 | } | |
694 | ||
c61dcff9 | 695 | static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol eol_attr) |
f217f0e8 | 696 | { |
5ec3e670 | 697 | if (text_attr == CRLF_BINARY) |
fd6cce9e EB |
698 | return CRLF_BINARY; |
699 | if (eol_attr == EOL_LF) | |
700 | return CRLF_INPUT; | |
701 | if (eol_attr == EOL_CRLF) | |
702 | return CRLF_CRLF; | |
5ec3e670 | 703 | return text_attr; |
fd6cce9e EB |
704 | } |
705 | ||
3bfba20d JH |
706 | struct conv_attrs { |
707 | struct convert_driver *drv; | |
708 | enum crlf_action crlf_action; | |
709 | enum eol eol_attr; | |
710 | int ident; | |
711 | }; | |
712 | ||
83295964 JH |
713 | static const char *conv_attr_name[] = { |
714 | "crlf", "ident", "filter", "eol", "text", | |
715 | }; | |
716 | #define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name) | |
717 | ||
3bfba20d | 718 | static void convert_attrs(struct conv_attrs *ca, const char *path) |
83295964 JH |
719 | { |
720 | int i; | |
721 | static struct git_attr_check ccheck[NUM_CONV_ATTRS]; | |
722 | ||
723 | if (!ccheck[0].attr) { | |
724 | for (i = 0; i < NUM_CONV_ATTRS; i++) | |
725 | ccheck[i].attr = git_attr(conv_attr_name[i]); | |
726 | user_convert_tail = &user_convert; | |
727 | git_config(read_convert_config, NULL); | |
728 | } | |
3bfba20d | 729 | |
d932f4eb | 730 | if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) { |
3bfba20d JH |
731 | ca->crlf_action = git_path_check_crlf(path, ccheck + 4); |
732 | if (ca->crlf_action == CRLF_GUESS) | |
733 | ca->crlf_action = git_path_check_crlf(path, ccheck + 0); | |
734 | ca->ident = git_path_check_ident(path, ccheck + 1); | |
735 | ca->drv = git_path_check_convert(path, ccheck + 2); | |
736 | ca->eol_attr = git_path_check_eol(path, ccheck + 3); | |
737 | } else { | |
738 | ca->drv = NULL; | |
739 | ca->crlf_action = CRLF_GUESS; | |
740 | ca->eol_attr = EOL_UNSET; | |
741 | ca->ident = 0; | |
742 | } | |
83295964 JH |
743 | } |
744 | ||
21e5ad50 SP |
745 | int convert_to_git(const char *path, const char *src, size_t len, |
746 | struct strbuf *dst, enum safe_crlf checksafe) | |
35ebfd6a | 747 | { |
3bfba20d | 748 | int ret = 0; |
cd8be6c9 | 749 | const char *filter = NULL; |
3bfba20d | 750 | struct conv_attrs ca; |
6073ee85 | 751 | |
3bfba20d JH |
752 | convert_attrs(&ca, path); |
753 | if (ca.drv) | |
754 | filter = ca.drv->clean; | |
3fed15f5 | 755 | |
5ecd293d PH |
756 | ret |= apply_filter(path, src, len, dst, filter); |
757 | if (ret) { | |
758 | src = dst->buf; | |
759 | len = dst->len; | |
aa4ed402 | 760 | } |
3bfba20d JH |
761 | ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr); |
762 | ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe); | |
5ecd293d PH |
763 | if (ret) { |
764 | src = dst->buf; | |
765 | len = dst->len; | |
6073ee85 | 766 | } |
3bfba20d | 767 | return ret | ident_to_git(path, src, len, dst, ca.ident); |
35ebfd6a JH |
768 | } |
769 | ||
43dd2332 EB |
770 | static int convert_to_working_tree_internal(const char *path, const char *src, |
771 | size_t len, struct strbuf *dst, | |
772 | int normalizing) | |
35ebfd6a | 773 | { |
3bfba20d | 774 | int ret = 0; |
cd8be6c9 | 775 | const char *filter = NULL; |
3bfba20d | 776 | struct conv_attrs ca; |
6073ee85 | 777 | |
3bfba20d JH |
778 | convert_attrs(&ca, path); |
779 | if (ca.drv) | |
780 | filter = ca.drv->smudge; | |
3fed15f5 | 781 | |
3bfba20d | 782 | ret |= ident_to_worktree(path, src, len, dst, ca.ident); |
5ecd293d PH |
783 | if (ret) { |
784 | src = dst->buf; | |
785 | len = dst->len; | |
3fed15f5 | 786 | } |
43dd2332 EB |
787 | /* |
788 | * CRLF conversion can be skipped if normalizing, unless there | |
789 | * is a smudge filter. The filter might expect CRLFs. | |
790 | */ | |
791 | if (filter || !normalizing) { | |
3bfba20d JH |
792 | ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr); |
793 | ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action); | |
43dd2332 EB |
794 | if (ret) { |
795 | src = dst->buf; | |
796 | len = dst->len; | |
797 | } | |
aa4ed402 | 798 | } |
5ecd293d | 799 | return ret | apply_filter(path, src, len, dst, filter); |
35ebfd6a | 800 | } |
f217f0e8 | 801 | |
43dd2332 EB |
802 | int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst) |
803 | { | |
804 | return convert_to_working_tree_internal(path, src, len, dst, 0); | |
805 | } | |
806 | ||
f217f0e8 EB |
807 | int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst) |
808 | { | |
43dd2332 | 809 | int ret = convert_to_working_tree_internal(path, src, len, dst, 1); |
f217f0e8 EB |
810 | if (ret) { |
811 | src = dst->buf; | |
812 | len = dst->len; | |
813 | } | |
814 | return ret | convert_to_git(path, src, len, dst, 0); | |
815 | } | |
dd8e9121 | 816 | |
b6691092 JH |
817 | /***************************************************************** |
818 | * | |
819 | * Streaming converison support | |
820 | * | |
821 | *****************************************************************/ | |
822 | ||
823 | typedef int (*filter_fn)(struct stream_filter *, | |
824 | const char *input, size_t *isize_p, | |
825 | char *output, size_t *osize_p); | |
826 | typedef void (*free_fn)(struct stream_filter *); | |
827 | ||
828 | struct stream_filter_vtbl { | |
829 | filter_fn filter; | |
830 | free_fn free; | |
831 | }; | |
832 | ||
833 | struct stream_filter { | |
834 | struct stream_filter_vtbl *vtbl; | |
835 | }; | |
836 | ||
837 | static int null_filter_fn(struct stream_filter *filter, | |
838 | const char *input, size_t *isize_p, | |
839 | char *output, size_t *osize_p) | |
840 | { | |
4ae66704 JH |
841 | size_t count; |
842 | ||
843 | if (!input) | |
844 | return 0; /* we do not keep any states */ | |
845 | count = *isize_p; | |
b6691092 JH |
846 | if (*osize_p < count) |
847 | count = *osize_p; | |
848 | if (count) { | |
849 | memmove(output, input, count); | |
850 | *isize_p -= count; | |
851 | *osize_p -= count; | |
852 | } | |
853 | return 0; | |
854 | } | |
855 | ||
856 | static void null_free_fn(struct stream_filter *filter) | |
857 | { | |
858 | ; /* nothing -- null instances are shared */ | |
859 | } | |
860 | ||
861 | static struct stream_filter_vtbl null_vtbl = { | |
862 | null_filter_fn, | |
863 | null_free_fn, | |
864 | }; | |
865 | ||
866 | static struct stream_filter null_filter_singleton = { | |
867 | &null_vtbl, | |
868 | }; | |
869 | ||
870 | int is_null_stream_filter(struct stream_filter *filter) | |
871 | { | |
872 | return filter == &null_filter_singleton; | |
873 | } | |
874 | ||
b84c7839 JH |
875 | |
876 | /* | |
877 | * LF-to-CRLF filter | |
878 | */ | |
e322ee38 JH |
879 | static int lf_to_crlf_filter_fn(struct stream_filter *filter, |
880 | const char *input, size_t *isize_p, | |
881 | char *output, size_t *osize_p) | |
882 | { | |
883 | size_t count; | |
884 | ||
885 | if (!input) | |
886 | return 0; /* we do not keep any states */ | |
887 | count = *isize_p; | |
888 | if (count) { | |
889 | size_t i, o; | |
890 | for (i = o = 0; o < *osize_p && i < count; i++) { | |
891 | char ch = input[i]; | |
892 | if (ch == '\n') { | |
893 | if (o + 1 < *osize_p) | |
894 | output[o++] = '\r'; | |
895 | else | |
896 | break; | |
897 | } | |
898 | output[o++] = ch; | |
899 | } | |
900 | ||
901 | *osize_p -= o; | |
902 | *isize_p -= i; | |
903 | } | |
904 | return 0; | |
905 | } | |
906 | ||
907 | static struct stream_filter_vtbl lf_to_crlf_vtbl = { | |
908 | lf_to_crlf_filter_fn, | |
909 | null_free_fn, | |
910 | }; | |
911 | ||
912 | static struct stream_filter lf_to_crlf_filter_singleton = { | |
913 | &lf_to_crlf_vtbl, | |
914 | }; | |
915 | ||
b84c7839 | 916 | |
a265a7f9 JH |
917 | /* |
918 | * Cascade filter | |
919 | */ | |
920 | #define FILTER_BUFFER 1024 | |
921 | struct cascade_filter { | |
922 | struct stream_filter filter; | |
923 | struct stream_filter *one; | |
924 | struct stream_filter *two; | |
925 | char buf[FILTER_BUFFER]; | |
926 | int end, ptr; | |
927 | }; | |
928 | ||
929 | static int cascade_filter_fn(struct stream_filter *filter, | |
930 | const char *input, size_t *isize_p, | |
931 | char *output, size_t *osize_p) | |
932 | { | |
933 | struct cascade_filter *cas = (struct cascade_filter *) filter; | |
934 | size_t filled = 0; | |
935 | size_t sz = *osize_p; | |
936 | size_t to_feed, remaining; | |
937 | ||
938 | /* | |
939 | * input -- (one) --> buf -- (two) --> output | |
940 | */ | |
941 | while (filled < sz) { | |
942 | remaining = sz - filled; | |
943 | ||
944 | /* do we already have something to feed two with? */ | |
945 | if (cas->ptr < cas->end) { | |
946 | to_feed = cas->end - cas->ptr; | |
947 | if (stream_filter(cas->two, | |
948 | cas->buf + cas->ptr, &to_feed, | |
949 | output + filled, &remaining)) | |
950 | return -1; | |
951 | cas->ptr += (cas->end - cas->ptr) - to_feed; | |
952 | filled = sz - remaining; | |
953 | continue; | |
954 | } | |
955 | ||
956 | /* feed one from upstream and have it emit into our buffer */ | |
957 | to_feed = input ? *isize_p : 0; | |
958 | if (input && !to_feed) | |
959 | break; | |
960 | remaining = sizeof(cas->buf); | |
961 | if (stream_filter(cas->one, | |
962 | input, &to_feed, | |
963 | cas->buf, &remaining)) | |
964 | return -1; | |
965 | cas->end = sizeof(cas->buf) - remaining; | |
966 | cas->ptr = 0; | |
967 | if (input) { | |
968 | size_t fed = *isize_p - to_feed; | |
969 | *isize_p -= fed; | |
970 | input += fed; | |
971 | } | |
972 | ||
973 | /* do we know that we drained one completely? */ | |
974 | if (input || cas->end) | |
975 | continue; | |
976 | ||
977 | /* tell two to drain; we have nothing more to give it */ | |
978 | to_feed = 0; | |
979 | remaining = sz - filled; | |
980 | if (stream_filter(cas->two, | |
981 | NULL, &to_feed, | |
982 | output + filled, &remaining)) | |
983 | return -1; | |
984 | if (remaining == (sz - filled)) | |
985 | break; /* completely drained two */ | |
986 | filled = sz - remaining; | |
987 | } | |
988 | *osize_p -= filled; | |
989 | return 0; | |
990 | } | |
991 | ||
992 | static void cascade_free_fn(struct stream_filter *filter) | |
993 | { | |
994 | struct cascade_filter *cas = (struct cascade_filter *)filter; | |
995 | free_stream_filter(cas->one); | |
996 | free_stream_filter(cas->two); | |
997 | free(filter); | |
998 | } | |
999 | ||
1000 | static struct stream_filter_vtbl cascade_vtbl = { | |
1001 | cascade_filter_fn, | |
1002 | cascade_free_fn, | |
1003 | }; | |
1004 | ||
1005 | static struct stream_filter *cascade_filter(struct stream_filter *one, | |
1006 | struct stream_filter *two) | |
1007 | { | |
1008 | struct cascade_filter *cascade; | |
1009 | ||
1010 | if (!one || is_null_stream_filter(one)) | |
1011 | return two; | |
1012 | if (!two || is_null_stream_filter(two)) | |
1013 | return one; | |
1014 | ||
1015 | cascade = xmalloc(sizeof(*cascade)); | |
1016 | cascade->one = one; | |
1017 | cascade->two = two; | |
1018 | cascade->end = cascade->ptr = 0; | |
1019 | cascade->filter.vtbl = &cascade_vtbl; | |
1020 | return (struct stream_filter *)cascade; | |
1021 | } | |
1022 | ||
b84c7839 JH |
1023 | /* |
1024 | * ident filter | |
1025 | */ | |
1026 | #define IDENT_DRAINING (-1) | |
1027 | #define IDENT_SKIPPING (-2) | |
1028 | struct ident_filter { | |
1029 | struct stream_filter filter; | |
1030 | struct strbuf left; | |
1031 | int state; | |
1032 | char ident[45]; /* ": x40 $" */ | |
1033 | }; | |
1034 | ||
1035 | static int is_foreign_ident(const char *str) | |
1036 | { | |
1037 | int i; | |
1038 | ||
1039 | if (prefixcmp(str, "$Id: ")) | |
1040 | return 0; | |
1041 | for (i = 5; str[i]; i++) { | |
1042 | if (isspace(str[i]) && str[i+1] != '$') | |
1043 | return 1; | |
1044 | } | |
1045 | return 0; | |
1046 | } | |
1047 | ||
1048 | static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p) | |
1049 | { | |
1050 | size_t to_drain = ident->left.len; | |
1051 | ||
1052 | if (*osize_p < to_drain) | |
1053 | to_drain = *osize_p; | |
1054 | if (to_drain) { | |
1055 | memcpy(*output_p, ident->left.buf, to_drain); | |
1056 | strbuf_remove(&ident->left, 0, to_drain); | |
1057 | *output_p += to_drain; | |
1058 | *osize_p -= to_drain; | |
1059 | } | |
1060 | if (!ident->left.len) | |
1061 | ident->state = 0; | |
1062 | } | |
1063 | ||
1064 | static int ident_filter_fn(struct stream_filter *filter, | |
1065 | const char *input, size_t *isize_p, | |
1066 | char *output, size_t *osize_p) | |
1067 | { | |
1068 | struct ident_filter *ident = (struct ident_filter *)filter; | |
1069 | static const char head[] = "$Id"; | |
1070 | ||
1071 | if (!input) { | |
1072 | /* drain upon eof */ | |
1073 | switch (ident->state) { | |
1074 | default: | |
1075 | strbuf_add(&ident->left, head, ident->state); | |
1076 | case IDENT_SKIPPING: | |
1077 | /* fallthru */ | |
1078 | case IDENT_DRAINING: | |
1079 | ident_drain(ident, &output, osize_p); | |
1080 | } | |
1081 | return 0; | |
1082 | } | |
1083 | ||
1084 | while (*isize_p || (ident->state == IDENT_DRAINING)) { | |
1085 | int ch; | |
1086 | ||
1087 | if (ident->state == IDENT_DRAINING) { | |
1088 | ident_drain(ident, &output, osize_p); | |
1089 | if (!*osize_p) | |
1090 | break; | |
1091 | continue; | |
1092 | } | |
1093 | ||
1094 | ch = *(input++); | |
1095 | (*isize_p)--; | |
1096 | ||
1097 | if (ident->state == IDENT_SKIPPING) { | |
1098 | /* | |
1099 | * Skipping until '$' or LF, but keeping them | |
1100 | * in case it is a foreign ident. | |
1101 | */ | |
1102 | strbuf_addch(&ident->left, ch); | |
1103 | if (ch != '\n' && ch != '$') | |
1104 | continue; | |
1105 | if (ch == '$' && !is_foreign_ident(ident->left.buf)) { | |
1106 | strbuf_setlen(&ident->left, sizeof(head) - 1); | |
1107 | strbuf_addstr(&ident->left, ident->ident); | |
1108 | } | |
1109 | ident->state = IDENT_DRAINING; | |
1110 | continue; | |
1111 | } | |
1112 | ||
1113 | if (ident->state < sizeof(head) && | |
1114 | head[ident->state] == ch) { | |
1115 | ident->state++; | |
1116 | continue; | |
1117 | } | |
1118 | ||
1119 | if (ident->state) | |
1120 | strbuf_add(&ident->left, head, ident->state); | |
1121 | if (ident->state == sizeof(head) - 1) { | |
1122 | if (ch != ':' && ch != '$') { | |
1123 | strbuf_addch(&ident->left, ch); | |
1124 | ident->state = 0; | |
1125 | continue; | |
1126 | } | |
1127 | ||
1128 | if (ch == ':') { | |
1129 | strbuf_addch(&ident->left, ch); | |
1130 | ident->state = IDENT_SKIPPING; | |
1131 | } else { | |
1132 | strbuf_addstr(&ident->left, ident->ident); | |
1133 | ident->state = IDENT_DRAINING; | |
1134 | } | |
1135 | continue; | |
1136 | } | |
1137 | ||
1138 | strbuf_addch(&ident->left, ch); | |
1139 | ident->state = IDENT_DRAINING; | |
1140 | } | |
1141 | return 0; | |
1142 | } | |
1143 | ||
1144 | static void ident_free_fn(struct stream_filter *filter) | |
1145 | { | |
1146 | struct ident_filter *ident = (struct ident_filter *)filter; | |
1147 | strbuf_release(&ident->left); | |
1148 | free(filter); | |
1149 | } | |
1150 | ||
1151 | static struct stream_filter_vtbl ident_vtbl = { | |
1152 | ident_filter_fn, | |
1153 | ident_free_fn, | |
1154 | }; | |
1155 | ||
1156 | static struct stream_filter *ident_filter(const unsigned char *sha1) | |
1157 | { | |
1158 | struct ident_filter *ident = xmalloc(sizeof(*ident)); | |
1159 | ||
1160 | sprintf(ident->ident, ": %s $", sha1_to_hex(sha1)); | |
1161 | strbuf_init(&ident->left, 0); | |
1162 | ident->filter.vtbl = &ident_vtbl; | |
1163 | ident->state = 0; | |
1164 | return (struct stream_filter *)ident; | |
1165 | } | |
1166 | ||
dd8e9121 | 1167 | /* |
b6691092 JH |
1168 | * Return an appropriately constructed filter for the path, or NULL if |
1169 | * the contents cannot be filtered without reading the whole thing | |
1170 | * in-core. | |
1171 | * | |
1172 | * Note that you would be crazy to set CRLF, smuge/clean or ident to a | |
1173 | * large binary blob you would want us not to slurp into the memory! | |
dd8e9121 | 1174 | */ |
b6691092 | 1175 | struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1) |
dd8e9121 JH |
1176 | { |
1177 | struct conv_attrs ca; | |
1178 | enum crlf_action crlf_action; | |
b84c7839 | 1179 | struct stream_filter *filter = NULL; |
dd8e9121 JH |
1180 | |
1181 | convert_attrs(&ca, path); | |
1182 | ||
b84c7839 JH |
1183 | if (ca.drv && (ca.drv->smudge || ca.drv->clean)) |
1184 | return filter; | |
1185 | ||
1186 | if (ca.ident) | |
1187 | filter = ident_filter(sha1); | |
dd8e9121 JH |
1188 | |
1189 | crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr); | |
b84c7839 | 1190 | |
b0d9c69f | 1191 | if ((crlf_action == CRLF_BINARY) || (crlf_action == CRLF_INPUT) || |
a265a7f9 JH |
1192 | (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE)) |
1193 | filter = cascade_filter(filter, &null_filter_singleton); | |
1194 | ||
1195 | else if (output_eol(crlf_action) == EOL_CRLF && | |
1196 | !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS)) | |
1197 | filter = cascade_filter(filter, &lf_to_crlf_filter_singleton); | |
e322ee38 | 1198 | |
b84c7839 | 1199 | return filter; |
b6691092 JH |
1200 | } |
1201 | ||
1202 | void free_stream_filter(struct stream_filter *filter) | |
1203 | { | |
1204 | filter->vtbl->free(filter); | |
1205 | } | |
1206 | ||
1207 | int stream_filter(struct stream_filter *filter, | |
1208 | const char *input, size_t *isize_p, | |
1209 | char *output, size_t *osize_p) | |
1210 | { | |
1211 | return filter->vtbl->filter(filter, input, isize_p, output, osize_p); | |
dd8e9121 | 1212 | } |