]>
Commit | Line | Data |
---|---|---|
a33e0b2a | 1 | #define NO_THE_INDEX_COMPATIBILITY_MACROS |
6c510bee | 2 | #include "cache.h" |
b2141fc1 | 3 | #include "config.h" |
35ebfd6a | 4 | #include "attr.h" |
3fed15f5 | 5 | #include "run-command.h" |
a2b665de | 6 | #include "quote.h" |
6424c2ad | 7 | #include "sigchain.h" |
edcc8581 | 8 | #include "pkt-line.h" |
99605d62 | 9 | #include "sub-process.h" |
35ebfd6a | 10 | |
6c510bee LT |
11 | /* |
12 | * convert.c - convert a file when checking it out and checking it in. | |
13 | * | |
14 | * This should use the pathname to decide on whether it wants to do some | |
15 | * more interesting conversions (automatic gzip/unzip, general format | |
16 | * conversions etc etc), but by default it just does automatic CRLF<->LF | |
942e7747 | 17 | * translation when the "text" attribute or "auto_crlf" option is set. |
6c510bee LT |
18 | */ |
19 | ||
a7630bd4 TB |
20 | /* Stat bits: When BIN is set, the txt bits are unset */ |
21 | #define CONVERT_STAT_BITS_TXT_LF 0x1 | |
22 | #define CONVERT_STAT_BITS_TXT_CRLF 0x2 | |
23 | #define CONVERT_STAT_BITS_BIN 0x4 | |
24 | ||
c61dcff9 | 25 | enum crlf_action { |
df747b81 TB |
26 | CRLF_UNDEFINED, |
27 | CRLF_BINARY, | |
fd6cce9e | 28 | CRLF_TEXT, |
df747b81 TB |
29 | CRLF_TEXT_INPUT, |
30 | CRLF_TEXT_CRLF, | |
31 | CRLF_AUTO, | |
32 | CRLF_AUTO_INPUT, | |
33 | CRLF_AUTO_CRLF | |
fd6cce9e | 34 | }; |
163b9591 | 35 | |
6c510bee | 36 | struct text_stat { |
28624193 | 37 | /* NUL, CR, LF and CRLF counts */ |
6e336a53 | 38 | unsigned nul, lonecr, lonelf, crlf; |
6c510bee LT |
39 | |
40 | /* These are just approximations! */ | |
41 | unsigned printable, nonprintable; | |
42 | }; | |
43 | ||
44 | static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) | |
45 | { | |
46 | unsigned long i; | |
47 | ||
48 | memset(stats, 0, sizeof(*stats)); | |
49 | ||
50 | for (i = 0; i < size; i++) { | |
51 | unsigned char c = buf[i]; | |
52 | if (c == '\r') { | |
6e336a53 | 53 | if (i+1 < size && buf[i+1] == '\n') { |
6c510bee | 54 | stats->crlf++; |
6e336a53 TB |
55 | i++; |
56 | } else | |
57 | stats->lonecr++; | |
6c510bee LT |
58 | continue; |
59 | } | |
60 | if (c == '\n') { | |
6e336a53 | 61 | stats->lonelf++; |
6c510bee LT |
62 | continue; |
63 | } | |
64 | if (c == 127) | |
65 | /* DEL */ | |
66 | stats->nonprintable++; | |
67 | else if (c < 32) { | |
68 | switch (c) { | |
69 | /* BS, HT, ESC and FF */ | |
70 | case '\b': case '\t': case '\033': case '\014': | |
71 | stats->printable++; | |
72 | break; | |
28624193 DP |
73 | case 0: |
74 | stats->nul++; | |
75 | /* fall through */ | |
6c510bee LT |
76 | default: |
77 | stats->nonprintable++; | |
78 | } | |
79 | } | |
80 | else | |
81 | stats->printable++; | |
82 | } | |
f9dd4bf4 DK |
83 | |
84 | /* If file ends with EOF then don't count this EOF as non-printable. */ | |
85 | if (size >= 1 && buf[size-1] == '\032') | |
86 | stats->nonprintable--; | |
6c510bee LT |
87 | } |
88 | ||
89 | /* | |
90 | * The same heuristics as diff.c::mmfile_is_binary() | |
a7630bd4 | 91 | * We treat files with bare CR as binary |
6c510bee | 92 | */ |
a7630bd4 | 93 | static int convert_is_binary(unsigned long size, const struct text_stat *stats) |
6c510bee | 94 | { |
6e336a53 | 95 | if (stats->lonecr) |
a7630bd4 | 96 | return 1; |
28624193 DP |
97 | if (stats->nul) |
98 | return 1; | |
6c510bee LT |
99 | if ((stats->printable >> 7) < stats->nonprintable) |
100 | return 1; | |
6c510bee LT |
101 | return 0; |
102 | } | |
103 | ||
a7630bd4 TB |
104 | static unsigned int gather_convert_stats(const char *data, unsigned long size) |
105 | { | |
106 | struct text_stat stats; | |
6e336a53 | 107 | int ret = 0; |
a7630bd4 TB |
108 | if (!data || !size) |
109 | return 0; | |
110 | gather_stats(data, size, &stats); | |
111 | if (convert_is_binary(size, &stats)) | |
6e336a53 TB |
112 | ret |= CONVERT_STAT_BITS_BIN; |
113 | if (stats.crlf) | |
114 | ret |= CONVERT_STAT_BITS_TXT_CRLF; | |
115 | if (stats.lonelf) | |
116 | ret |= CONVERT_STAT_BITS_TXT_LF; | |
117 | ||
118 | return ret; | |
a7630bd4 TB |
119 | } |
120 | ||
121 | static const char *gather_convert_stats_ascii(const char *data, unsigned long size) | |
122 | { | |
123 | unsigned int convert_stats = gather_convert_stats(data, size); | |
124 | ||
125 | if (convert_stats & CONVERT_STAT_BITS_BIN) | |
126 | return "-text"; | |
127 | switch (convert_stats) { | |
128 | case CONVERT_STAT_BITS_TXT_LF: | |
129 | return "lf"; | |
130 | case CONVERT_STAT_BITS_TXT_CRLF: | |
131 | return "crlf"; | |
132 | case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF: | |
133 | return "mixed"; | |
134 | default: | |
135 | return "none"; | |
136 | } | |
137 | } | |
138 | ||
a7609c54 BW |
139 | const char *get_cached_convert_stats_ascii(const struct index_state *istate, |
140 | const char *path) | |
a7630bd4 TB |
141 | { |
142 | const char *ret; | |
143 | unsigned long sz; | |
a7609c54 | 144 | void *data = read_blob_data_from_index(istate, path, &sz); |
a7630bd4 TB |
145 | ret = gather_convert_stats_ascii(data, sz); |
146 | free(data); | |
147 | return ret; | |
148 | } | |
149 | ||
150 | const char *get_wt_convert_stats_ascii(const char *path) | |
151 | { | |
152 | const char *ret = ""; | |
153 | struct strbuf sb = STRBUF_INIT; | |
154 | if (strbuf_read_file(&sb, path, 0) >= 0) | |
155 | ret = gather_convert_stats_ascii(sb.buf, sb.len); | |
156 | strbuf_release(&sb); | |
157 | return ret; | |
158 | } | |
159 | ||
4b4024f5 TB |
160 | static int text_eol_is_crlf(void) |
161 | { | |
162 | if (auto_crlf == AUTO_CRLF_TRUE) | |
163 | return 1; | |
164 | else if (auto_crlf == AUTO_CRLF_INPUT) | |
165 | return 0; | |
166 | if (core_eol == EOL_CRLF) | |
167 | return 1; | |
168 | if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF) | |
169 | return 1; | |
170 | return 0; | |
171 | } | |
172 | ||
c61dcff9 | 173 | static enum eol output_eol(enum crlf_action crlf_action) |
f217f0e8 | 174 | { |
c61dcff9 | 175 | switch (crlf_action) { |
942e7747 EB |
176 | case CRLF_BINARY: |
177 | return EOL_UNSET; | |
df747b81 | 178 | case CRLF_TEXT_CRLF: |
942e7747 | 179 | return EOL_CRLF; |
df747b81 | 180 | case CRLF_TEXT_INPUT: |
942e7747 | 181 | return EOL_LF; |
df747b81 TB |
182 | case CRLF_UNDEFINED: |
183 | case CRLF_AUTO_CRLF: | |
65237284 | 184 | return EOL_CRLF; |
df747b81 | 185 | case CRLF_AUTO_INPUT: |
65237284 | 186 | return EOL_LF; |
942e7747 EB |
187 | case CRLF_TEXT: |
188 | case CRLF_AUTO: | |
df747b81 | 189 | /* fall through */ |
4b4024f5 | 190 | return text_eol_is_crlf() ? EOL_CRLF : EOL_LF; |
942e7747 | 191 | } |
df747b81 | 192 | warning("Illegal crlf_action %d\n", (int)crlf_action); |
ec70f52f | 193 | return core_eol; |
942e7747 EB |
194 | } |
195 | ||
c61dcff9 | 196 | static void check_safe_crlf(const char *path, enum crlf_action crlf_action, |
a0ad53c1 TB |
197 | struct text_stat *old_stats, struct text_stat *new_stats, |
198 | enum safe_crlf checksafe) | |
21e5ad50 | 199 | { |
a0ad53c1 | 200 | if (old_stats->crlf && !new_stats->crlf ) { |
21e5ad50 | 201 | /* |
a0ad53c1 | 202 | * CRLFs would not be restored by checkout |
21e5ad50 | 203 | */ |
a0ad53c1 | 204 | if (checksafe == SAFE_CRLF_WARN) |
87cb7845 VA |
205 | warning(_("CRLF will be replaced by LF in %s.\n" |
206 | "The file will have its original line" | |
207 | " endings in your working directory."), path); | |
a0ad53c1 | 208 | else /* i.e. SAFE_CRLF_FAIL */ |
87cb7845 | 209 | die(_("CRLF would be replaced by LF in %s."), path); |
a0ad53c1 | 210 | } else if (old_stats->lonelf && !new_stats->lonelf ) { |
21e5ad50 | 211 | /* |
a0ad53c1 | 212 | * CRLFs would be added by checkout |
21e5ad50 | 213 | */ |
a0ad53c1 | 214 | if (checksafe == SAFE_CRLF_WARN) |
87cb7845 VA |
215 | warning(_("LF will be replaced by CRLF in %s.\n" |
216 | "The file will have its original line" | |
217 | " endings in your working directory."), path); | |
a0ad53c1 | 218 | else /* i.e. SAFE_CRLF_FAIL */ |
87cb7845 | 219 | die(_("LF would be replaced by CRLF in %s"), path); |
21e5ad50 SP |
220 | } |
221 | } | |
222 | ||
49a6d31f | 223 | static int has_cr_in_index(const struct index_state *istate, const char *path) |
c4805393 | 224 | { |
c4805393 | 225 | unsigned long sz; |
c4805393 FAG |
226 | void *data; |
227 | int has_cr; | |
c4805393 | 228 | |
49a6d31f | 229 | data = read_blob_data_from_index(istate, path, &sz); |
4982fd78 | 230 | if (!data) |
c4805393 | 231 | return 0; |
c4805393 FAG |
232 | has_cr = memchr(data, '\r', sz) != NULL; |
233 | free(data); | |
234 | return has_cr; | |
235 | } | |
236 | ||
a0ad53c1 TB |
237 | static int will_convert_lf_to_crlf(size_t len, struct text_stat *stats, |
238 | enum crlf_action crlf_action) | |
239 | { | |
240 | if (output_eol(crlf_action) != EOL_CRLF) | |
241 | return 0; | |
242 | /* No "naked" LF? Nothing to convert, regardless. */ | |
243 | if (!stats->lonelf) | |
244 | return 0; | |
245 | ||
246 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { | |
247 | /* If we have any CR or CRLF line endings, we do not touch it */ | |
248 | /* This is the new safer autocrlf-handling */ | |
249 | if (stats->lonecr || stats->crlf) | |
250 | return 0; | |
251 | ||
252 | if (convert_is_binary(len, stats)) | |
253 | return 0; | |
254 | } | |
255 | return 1; | |
256 | ||
257 | } | |
258 | ||
49a6d31f BW |
259 | static int crlf_to_git(const struct index_state *istate, |
260 | const char *path, const char *src, size_t len, | |
c61dcff9 JH |
261 | struct strbuf *buf, |
262 | enum crlf_action crlf_action, enum safe_crlf checksafe) | |
6c510bee | 263 | { |
6c510bee | 264 | struct text_stat stats; |
5ecd293d | 265 | char *dst; |
a0ad53c1 | 266 | int convert_crlf_into_lf; |
6c510bee | 267 | |
c61dcff9 | 268 | if (crlf_action == CRLF_BINARY || |
4c3b57b9 | 269 | (src && !len)) |
5ecd293d | 270 | return 0; |
6c510bee | 271 | |
4c3b57b9 JK |
272 | /* |
273 | * If we are doing a dry-run and have no source buffer, there is | |
274 | * nothing to analyze; we must assume we would convert. | |
275 | */ | |
276 | if (!buf && !src) | |
277 | return 1; | |
278 | ||
5ecd293d | 279 | gather_stats(src, len, &stats); |
a0ad53c1 TB |
280 | /* Optimization: No CRLF? Nothing to convert, regardless. */ |
281 | convert_crlf_into_lf = !!stats.crlf; | |
6c510bee | 282 | |
df747b81 | 283 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
a7630bd4 | 284 | if (convert_is_binary(len, &stats)) |
5ecd293d | 285 | return 0; |
65237284 | 286 | /* |
1c25d2d8 TB |
287 | * If the file in the index has any CR in it, do not |
288 | * convert. This is the new safer autocrlf handling, | |
289 | * unless we want to renormalize in a merge or | |
290 | * cherry-pick. | |
65237284 | 291 | */ |
49a6d31f BW |
292 | if ((checksafe != SAFE_CRLF_RENORMALIZE) && |
293 | has_cr_in_index(istate, path)) | |
a0ad53c1 | 294 | convert_crlf_into_lf = 0; |
201ac8ef | 295 | } |
1c25d2d8 TB |
296 | if ((checksafe == SAFE_CRLF_WARN || |
297 | (checksafe == SAFE_CRLF_FAIL)) && len) { | |
a0ad53c1 TB |
298 | struct text_stat new_stats; |
299 | memcpy(&new_stats, &stats, sizeof(new_stats)); | |
300 | /* simulate "git add" */ | |
301 | if (convert_crlf_into_lf) { | |
302 | new_stats.lonelf += new_stats.crlf; | |
303 | new_stats.crlf = 0; | |
304 | } | |
305 | /* simulate "git checkout" */ | |
306 | if (will_convert_lf_to_crlf(len, &new_stats, crlf_action)) { | |
307 | new_stats.crlf += new_stats.lonelf; | |
308 | new_stats.lonelf = 0; | |
309 | } | |
310 | check_safe_crlf(path, crlf_action, &stats, &new_stats, checksafe); | |
311 | } | |
312 | if (!convert_crlf_into_lf) | |
21e5ad50 SP |
313 | return 0; |
314 | ||
92ac3197 JK |
315 | /* |
316 | * At this point all of our source analysis is done, and we are sure we | |
317 | * would convert. If we are in dry-run mode, we can give an answer. | |
318 | */ | |
319 | if (!buf) | |
320 | return 1; | |
321 | ||
90d16ec0 PH |
322 | /* only grow if not in place */ |
323 | if (strbuf_avail(buf) + buf->len < len) | |
324 | strbuf_grow(buf, len - buf->len); | |
5ecd293d | 325 | dst = buf->buf; |
df747b81 | 326 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
163b9591 JH |
327 | /* |
328 | * If we guessed, we already know we rejected a file with | |
329 | * lone CR, and we can strip a CR without looking at what | |
330 | * follow it. | |
331 | */ | |
201ac8ef | 332 | do { |
ac78e548 | 333 | unsigned char c = *src++; |
201ac8ef | 334 | if (c != '\r') |
ac78e548 | 335 | *dst++ = c; |
5ecd293d | 336 | } while (--len); |
201ac8ef JH |
337 | } else { |
338 | do { | |
ac78e548 | 339 | unsigned char c = *src++; |
5ecd293d | 340 | if (! (c == '\r' && (1 < len && *src == '\n'))) |
ac78e548 | 341 | *dst++ = c; |
5ecd293d | 342 | } while (--len); |
201ac8ef | 343 | } |
5ecd293d PH |
344 | strbuf_setlen(buf, dst - buf->buf); |
345 | return 1; | |
6c510bee LT |
346 | } |
347 | ||
5ecd293d | 348 | static int crlf_to_worktree(const char *path, const char *src, size_t len, |
c61dcff9 | 349 | struct strbuf *buf, enum crlf_action crlf_action) |
6c510bee | 350 | { |
5ecd293d | 351 | char *to_free = NULL; |
6c510bee | 352 | struct text_stat stats; |
6c510bee | 353 | |
c61dcff9 | 354 | if (!len || output_eol(crlf_action) != EOL_CRLF) |
5ecd293d | 355 | return 0; |
6c510bee | 356 | |
5ecd293d | 357 | gather_stats(src, len, &stats); |
a0ad53c1 | 358 | if (!will_convert_lf_to_crlf(len, &stats, crlf_action)) |
5ecd293d | 359 | return 0; |
6c510bee | 360 | |
5ecd293d PH |
361 | /* are we "faking" in place editing ? */ |
362 | if (src == buf->buf) | |
b315c5c0 | 363 | to_free = strbuf_detach(buf, NULL); |
5ecd293d | 364 | |
6e336a53 | 365 | strbuf_grow(buf, len + stats.lonelf); |
5ecd293d PH |
366 | for (;;) { |
367 | const char *nl = memchr(src, '\n', len); | |
368 | if (!nl) | |
369 | break; | |
370 | if (nl > src && nl[-1] == '\r') { | |
371 | strbuf_add(buf, src, nl + 1 - src); | |
372 | } else { | |
373 | strbuf_add(buf, src, nl - src); | |
374 | strbuf_addstr(buf, "\r\n"); | |
375 | } | |
376 | len -= nl + 1 - src; | |
377 | src = nl + 1; | |
378 | } | |
379 | strbuf_add(buf, src, len); | |
380 | ||
381 | free(to_free); | |
382 | return 1; | |
6c510bee | 383 | } |
35ebfd6a | 384 | |
546bb582 JS |
385 | struct filter_params { |
386 | const char *src; | |
387 | unsigned long size; | |
9035d75a | 388 | int fd; |
546bb582 | 389 | const char *cmd; |
a2b665de | 390 | const char *path; |
546bb582 JS |
391 | }; |
392 | ||
9035d75a | 393 | static int filter_buffer_or_fd(int in, int out, void *data) |
aa4ed402 JH |
394 | { |
395 | /* | |
396 | * Spawn cmd and feed the buffer contents through its stdin. | |
397 | */ | |
d3180279 | 398 | struct child_process child_process = CHILD_PROCESS_INIT; |
546bb582 | 399 | struct filter_params *params = (struct filter_params *)data; |
aa4ed402 | 400 | int write_err, status; |
66dbfd55 GV |
401 | const char *argv[] = { NULL, NULL }; |
402 | ||
a2b665de PW |
403 | /* apply % substitution to cmd */ |
404 | struct strbuf cmd = STRBUF_INIT; | |
405 | struct strbuf path = STRBUF_INIT; | |
406 | struct strbuf_expand_dict_entry dict[] = { | |
407 | { "f", NULL, }, | |
408 | { NULL, NULL, }, | |
409 | }; | |
410 | ||
411 | /* quote the path to preserve spaces, etc. */ | |
412 | sq_quote_buf(&path, params->path); | |
413 | dict[0].value = path.buf; | |
414 | ||
415 | /* expand all %f with the quoted path */ | |
416 | strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict); | |
417 | strbuf_release(&path); | |
418 | ||
419 | argv[0] = cmd.buf; | |
aa4ed402 | 420 | |
dc1bfdcd | 421 | child_process.argv = argv; |
ac0ba18d | 422 | child_process.use_shell = 1; |
dc1bfdcd | 423 | child_process.in = -1; |
ae6a5609 | 424 | child_process.out = out; |
aa4ed402 | 425 | |
f31f1d39 RS |
426 | if (start_command(&child_process)) { |
427 | strbuf_release(&cmd); | |
255f04d6 | 428 | return error("cannot fork to run external filter '%s'", params->cmd); |
f31f1d39 | 429 | } |
aa4ed402 | 430 | |
6424c2ad JB |
431 | sigchain_push(SIGPIPE, SIG_IGN); |
432 | ||
9035d75a | 433 | if (params->src) { |
0c4dd67a JH |
434 | write_err = (write_in_full(child_process.in, |
435 | params->src, params->size) < 0); | |
436 | if (errno == EPIPE) | |
437 | write_err = 0; | |
9035d75a SP |
438 | } else { |
439 | write_err = copy_fd(params->fd, child_process.in); | |
0c4dd67a JH |
440 | if (write_err == COPY_WRITE_ERROR && errno == EPIPE) |
441 | write_err = 0; | |
9035d75a SP |
442 | } |
443 | ||
dc1bfdcd | 444 | if (close(child_process.in)) |
aa4ed402 JH |
445 | write_err = 1; |
446 | if (write_err) | |
255f04d6 | 447 | error("cannot feed the input to external filter '%s'", params->cmd); |
aa4ed402 | 448 | |
6424c2ad JB |
449 | sigchain_pop(SIGPIPE); |
450 | ||
aa4ed402 JH |
451 | status = finish_command(&child_process); |
452 | if (status) | |
255f04d6 | 453 | error("external filter '%s' failed %d", params->cmd, status); |
a2b665de PW |
454 | |
455 | strbuf_release(&cmd); | |
aa4ed402 JH |
456 | return (write_err || status); |
457 | } | |
458 | ||
234fa07e | 459 | static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd, |
5ecd293d | 460 | struct strbuf *dst, const char *cmd) |
aa4ed402 JH |
461 | { |
462 | /* | |
463 | * Create a pipeline to have the command filter the buffer's | |
464 | * contents. | |
465 | * | |
466 | * (child --> cmd) --> us | |
467 | */ | |
b84be553 | 468 | int err = 0; |
f285a2d7 | 469 | struct strbuf nbuf = STRBUF_INIT; |
546bb582 JS |
470 | struct async async; |
471 | struct filter_params params; | |
aa4ed402 | 472 | |
546bb582 | 473 | memset(&async, 0, sizeof(async)); |
9035d75a | 474 | async.proc = filter_buffer_or_fd; |
546bb582 | 475 | async.data = ¶ms; |
ae6a5609 | 476 | async.out = -1; |
546bb582 JS |
477 | params.src = src; |
478 | params.size = len; | |
9035d75a | 479 | params.fd = fd; |
546bb582 | 480 | params.cmd = cmd; |
a2b665de | 481 | params.path = path; |
aa4ed402 JH |
482 | |
483 | fflush(NULL); | |
546bb582 JS |
484 | if (start_async(&async)) |
485 | return 0; /* error was already reported */ | |
aa4ed402 | 486 | |
546bb582 | 487 | if (strbuf_read(&nbuf, async.out, len) < 0) { |
b84be553 | 488 | err = error("read from external filter '%s' failed", cmd); |
aa4ed402 | 489 | } |
546bb582 | 490 | if (close(async.out)) { |
b84be553 | 491 | err = error("read from external filter '%s' failed", cmd); |
aa4ed402 | 492 | } |
546bb582 | 493 | if (finish_async(&async)) { |
b84be553 | 494 | err = error("external filter '%s' failed", cmd); |
aa4ed402 JH |
495 | } |
496 | ||
b84be553 | 497 | if (!err) { |
90d16ec0 | 498 | strbuf_swap(dst, &nbuf); |
5ecd293d | 499 | } |
90d16ec0 | 500 | strbuf_release(&nbuf); |
b84be553 | 501 | return !err; |
aa4ed402 JH |
502 | } |
503 | ||
234fa07e LS |
504 | #define CAP_CLEAN (1u<<0) |
505 | #define CAP_SMUDGE (1u<<1) | |
2841e8f8 | 506 | #define CAP_DELAY (1u<<2) |
234fa07e | 507 | |
1b0b46ee BP |
508 | struct cmd2process { |
509 | struct subprocess_entry subprocess; /* must be the first member! */ | |
510 | unsigned int supported_capabilities; | |
511 | }; | |
512 | ||
f514d7d1 BP |
513 | static int subprocess_map_initialized; |
514 | static struct hashmap subprocess_map; | |
edcc8581 | 515 | |
7ddb9b2c | 516 | static int start_multi_file_filter_fn(struct subprocess_entry *subprocess) |
edcc8581 | 517 | { |
fa64a2fd JT |
518 | static int versions[] = {2, 0}; |
519 | static struct subprocess_capability capabilities[] = { | |
1514c8ed LS |
520 | { "clean", CAP_CLEAN }, |
521 | { "smudge", CAP_SMUDGE }, | |
2841e8f8 | 522 | { "delay", CAP_DELAY }, |
fa64a2fd | 523 | { NULL, 0 } |
1514c8ed | 524 | }; |
fa64a2fd JT |
525 | struct cmd2process *entry = (struct cmd2process *)subprocess; |
526 | return subprocess_handshake(subprocess, "git-filter", versions, NULL, | |
527 | capabilities, | |
528 | &entry->supported_capabilities); | |
a810ea99 BP |
529 | } |
530 | ||
9364fc29 LS |
531 | static void handle_filter_error(const struct strbuf *filter_status, |
532 | struct cmd2process *entry, | |
533 | const unsigned int wanted_capability) { | |
534 | if (!strcmp(filter_status->buf, "error")) | |
535 | ; /* The filter signaled a problem with the file. */ | |
536 | else if (!strcmp(filter_status->buf, "abort") && wanted_capability) { | |
537 | /* | |
538 | * The filter signaled a permanent problem. Don't try to filter | |
539 | * files with the same command for the lifetime of the current | |
540 | * Git process. | |
541 | */ | |
542 | entry->supported_capabilities &= ~wanted_capability; | |
543 | } else { | |
544 | /* | |
545 | * Something went wrong with the protocol filter. | |
546 | * Force shutdown and restart if another blob requires filtering. | |
547 | */ | |
548 | error("external filter '%s' failed", entry->subprocess.cmd); | |
549 | subprocess_stop(&subprocess_map, &entry->subprocess); | |
550 | free(entry); | |
551 | } | |
552 | } | |
553 | ||
edcc8581 LS |
554 | static int apply_multi_file_filter(const char *path, const char *src, size_t len, |
555 | int fd, struct strbuf *dst, const char *cmd, | |
2841e8f8 LS |
556 | const unsigned int wanted_capability, |
557 | struct delayed_checkout *dco) | |
edcc8581 LS |
558 | { |
559 | int err; | |
2841e8f8 | 560 | int can_delay = 0; |
edcc8581 LS |
561 | struct cmd2process *entry; |
562 | struct child_process *process; | |
563 | struct strbuf nbuf = STRBUF_INIT; | |
564 | struct strbuf filter_status = STRBUF_INIT; | |
565 | const char *filter_type; | |
566 | ||
f514d7d1 BP |
567 | if (!subprocess_map_initialized) { |
568 | subprocess_map_initialized = 1; | |
9ab42958 | 569 | hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0); |
edcc8581 LS |
570 | entry = NULL; |
571 | } else { | |
f514d7d1 | 572 | entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd); |
edcc8581 LS |
573 | } |
574 | ||
575 | fflush(NULL); | |
576 | ||
577 | if (!entry) { | |
7ddb9b2c BP |
578 | entry = xmalloc(sizeof(*entry)); |
579 | entry->supported_capabilities = 0; | |
580 | ||
f514d7d1 | 581 | if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) { |
7ddb9b2c | 582 | free(entry); |
edcc8581 | 583 | return 0; |
7ddb9b2c | 584 | } |
edcc8581 | 585 | } |
1b0b46ee | 586 | process = &entry->subprocess.process; |
edcc8581 | 587 | |
42b0a86c | 588 | if (!(entry->supported_capabilities & wanted_capability)) |
edcc8581 LS |
589 | return 0; |
590 | ||
42b0a86c | 591 | if (wanted_capability & CAP_CLEAN) |
edcc8581 | 592 | filter_type = "clean"; |
42b0a86c | 593 | else if (wanted_capability & CAP_SMUDGE) |
edcc8581 LS |
594 | filter_type = "smudge"; |
595 | else | |
596 | die("unexpected filter type"); | |
597 | ||
598 | sigchain_push(SIGPIPE, SIG_IGN); | |
599 | ||
600 | assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n")); | |
601 | err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type); | |
602 | if (err) | |
603 | goto done; | |
604 | ||
605 | err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n"); | |
606 | if (err) { | |
607 | error("path name too long for external filter"); | |
608 | goto done; | |
609 | } | |
610 | ||
611 | err = packet_write_fmt_gently(process->in, "pathname=%s\n", path); | |
612 | if (err) | |
613 | goto done; | |
614 | ||
2841e8f8 LS |
615 | if ((entry->supported_capabilities & CAP_DELAY) && |
616 | dco && dco->state == CE_CAN_DELAY) { | |
617 | can_delay = 1; | |
618 | err = packet_write_fmt_gently(process->in, "can-delay=1\n"); | |
619 | if (err) | |
620 | goto done; | |
621 | } | |
622 | ||
edcc8581 LS |
623 | err = packet_flush_gently(process->in); |
624 | if (err) | |
625 | goto done; | |
626 | ||
627 | if (fd >= 0) | |
628 | err = write_packetized_from_fd(fd, process->in); | |
629 | else | |
630 | err = write_packetized_from_buf(src, len, process->in); | |
631 | if (err) | |
632 | goto done; | |
633 | ||
4f2a2e9f BP |
634 | err = subprocess_read_status(process->out, &filter_status); |
635 | if (err) | |
636 | goto done; | |
637 | ||
2841e8f8 LS |
638 | if (can_delay && !strcmp(filter_status.buf, "delayed")) { |
639 | string_list_insert(&dco->filters, cmd); | |
640 | string_list_insert(&dco->paths, path); | |
641 | } else { | |
642 | /* The filter got the blob and wants to send us a response. */ | |
643 | err = strcmp(filter_status.buf, "success"); | |
644 | if (err) | |
645 | goto done; | |
646 | ||
647 | err = read_packetized_to_strbuf(process->out, &nbuf) < 0; | |
648 | if (err) | |
649 | goto done; | |
650 | ||
651 | err = subprocess_read_status(process->out, &filter_status); | |
652 | if (err) | |
653 | goto done; | |
654 | ||
655 | err = strcmp(filter_status.buf, "success"); | |
656 | } | |
657 | ||
658 | done: | |
659 | sigchain_pop(SIGPIPE); | |
660 | ||
661 | if (err) | |
662 | handle_filter_error(&filter_status, entry, wanted_capability); | |
663 | else | |
664 | strbuf_swap(dst, &nbuf); | |
665 | strbuf_release(&nbuf); | |
666 | return !err; | |
667 | } | |
668 | ||
669 | ||
670 | int async_query_available_blobs(const char *cmd, struct string_list *available_paths) | |
671 | { | |
672 | int err; | |
673 | char *line; | |
674 | struct cmd2process *entry; | |
675 | struct child_process *process; | |
676 | struct strbuf filter_status = STRBUF_INIT; | |
677 | ||
678 | assert(subprocess_map_initialized); | |
679 | entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd); | |
680 | if (!entry) { | |
681 | error("external filter '%s' is not available anymore although " | |
682 | "not all paths have been filtered", cmd); | |
683 | return 0; | |
684 | } | |
685 | process = &entry->subprocess.process; | |
686 | sigchain_push(SIGPIPE, SIG_IGN); | |
687 | ||
688 | err = packet_write_fmt_gently( | |
689 | process->in, "command=list_available_blobs\n"); | |
edcc8581 LS |
690 | if (err) |
691 | goto done; | |
692 | ||
2841e8f8 | 693 | err = packet_flush_gently(process->in); |
edcc8581 LS |
694 | if (err) |
695 | goto done; | |
696 | ||
2841e8f8 LS |
697 | while ((line = packet_read_line(process->out, NULL))) { |
698 | const char *path; | |
699 | if (skip_prefix(line, "pathname=", &path)) | |
700 | string_list_insert(available_paths, xstrdup(path)); | |
701 | else | |
702 | ; /* ignore unknown keys */ | |
703 | } | |
704 | ||
4f2a2e9f BP |
705 | err = subprocess_read_status(process->out, &filter_status); |
706 | if (err) | |
707 | goto done; | |
708 | ||
edcc8581 LS |
709 | err = strcmp(filter_status.buf, "success"); |
710 | ||
711 | done: | |
712 | sigchain_pop(SIGPIPE); | |
713 | ||
9364fc29 | 714 | if (err) |
2841e8f8 | 715 | handle_filter_error(&filter_status, entry, 0); |
edcc8581 | 716 | return !err; |
aa4ed402 JH |
717 | } |
718 | ||
719 | static struct convert_driver { | |
720 | const char *name; | |
721 | struct convert_driver *next; | |
cd8be6c9 BH |
722 | const char *smudge; |
723 | const char *clean; | |
edcc8581 | 724 | const char *process; |
36daaaca | 725 | int required; |
aa4ed402 JH |
726 | } *user_convert, **user_convert_tail; |
727 | ||
234fa07e LS |
728 | static int apply_filter(const char *path, const char *src, size_t len, |
729 | int fd, struct strbuf *dst, struct convert_driver *drv, | |
2841e8f8 LS |
730 | const unsigned int wanted_capability, |
731 | struct delayed_checkout *dco) | |
234fa07e LS |
732 | { |
733 | const char *cmd = NULL; | |
734 | ||
735 | if (!drv) | |
736 | return 0; | |
737 | ||
738 | if (!dst) | |
739 | return 1; | |
740 | ||
42b0a86c | 741 | if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean) |
234fa07e | 742 | cmd = drv->clean; |
42b0a86c | 743 | else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge) |
234fa07e LS |
744 | cmd = drv->smudge; |
745 | ||
746 | if (cmd && *cmd) | |
747 | return apply_single_file_filter(path, src, len, fd, dst, cmd); | |
edcc8581 | 748 | else if (drv->process && *drv->process) |
2841e8f8 LS |
749 | return apply_multi_file_filter(path, src, len, fd, dst, |
750 | drv->process, wanted_capability, dco); | |
234fa07e LS |
751 | |
752 | return 0; | |
753 | } | |
754 | ||
ef90d6d4 | 755 | static int read_convert_config(const char *var, const char *value, void *cb) |
aa4ed402 | 756 | { |
d731f0ad | 757 | const char *key, *name; |
aa4ed402 JH |
758 | int namelen; |
759 | struct convert_driver *drv; | |
760 | ||
761 | /* | |
762 | * External conversion drivers are configured using | |
763 | * "filter.<name>.variable". | |
764 | */ | |
d731f0ad | 765 | if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name) |
aa4ed402 | 766 | return 0; |
aa4ed402 JH |
767 | for (drv = user_convert; drv; drv = drv->next) |
768 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) | |
769 | break; | |
770 | if (!drv) { | |
aa4ed402 | 771 | drv = xcalloc(1, sizeof(struct convert_driver)); |
182af834 | 772 | drv->name = xmemdupz(name, namelen); |
aa4ed402 JH |
773 | *user_convert_tail = drv; |
774 | user_convert_tail = &(drv->next); | |
775 | } | |
776 | ||
aa4ed402 JH |
777 | /* |
778 | * filter.<name>.smudge and filter.<name>.clean specifies | |
779 | * the command line: | |
780 | * | |
781 | * command-line | |
782 | * | |
783 | * The command-line will not be interpolated in any way. | |
784 | */ | |
785 | ||
d731f0ad | 786 | if (!strcmp("smudge", key)) |
cd8be6c9 BH |
787 | return git_config_string(&drv->smudge, var, value); |
788 | ||
d731f0ad | 789 | if (!strcmp("clean", key)) |
cd8be6c9 | 790 | return git_config_string(&drv->clean, var, value); |
aa4ed402 | 791 | |
edcc8581 LS |
792 | if (!strcmp("process", key)) |
793 | return git_config_string(&drv->process, var, value); | |
794 | ||
d731f0ad | 795 | if (!strcmp("required", key)) { |
36daaaca JB |
796 | drv->required = git_config_bool(var, value); |
797 | return 0; | |
798 | } | |
799 | ||
aa4ed402 JH |
800 | return 0; |
801 | } | |
802 | ||
3fed15f5 JH |
803 | static int count_ident(const char *cp, unsigned long size) |
804 | { | |
805 | /* | |
af9b54bb | 806 | * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" |
3fed15f5 JH |
807 | */ |
808 | int cnt = 0; | |
809 | char ch; | |
810 | ||
811 | while (size) { | |
812 | ch = *cp++; | |
813 | size--; | |
814 | if (ch != '$') | |
815 | continue; | |
af9b54bb | 816 | if (size < 3) |
3fed15f5 | 817 | break; |
af9b54bb | 818 | if (memcmp("Id", cp, 2)) |
3fed15f5 | 819 | continue; |
af9b54bb AP |
820 | ch = cp[2]; |
821 | cp += 3; | |
822 | size -= 3; | |
3fed15f5 | 823 | if (ch == '$') |
af9b54bb | 824 | cnt++; /* $Id$ */ |
3fed15f5 JH |
825 | if (ch != ':') |
826 | continue; | |
827 | ||
828 | /* | |
af9b54bb | 829 | * "$Id: ... "; scan up to the closing dollar sign and discard. |
3fed15f5 JH |
830 | */ |
831 | while (size) { | |
832 | ch = *cp++; | |
833 | size--; | |
834 | if (ch == '$') { | |
835 | cnt++; | |
836 | break; | |
837 | } | |
a9f3049f HG |
838 | if (ch == '\n') |
839 | break; | |
3fed15f5 JH |
840 | } |
841 | } | |
842 | return cnt; | |
843 | } | |
844 | ||
5ecd293d PH |
845 | static int ident_to_git(const char *path, const char *src, size_t len, |
846 | struct strbuf *buf, int ident) | |
3fed15f5 | 847 | { |
5ecd293d | 848 | char *dst, *dollar; |
3fed15f5 | 849 | |
4c3b57b9 | 850 | if (!ident || (src && !count_ident(src, len))) |
5ecd293d PH |
851 | return 0; |
852 | ||
92ac3197 JK |
853 | if (!buf) |
854 | return 1; | |
855 | ||
90d16ec0 PH |
856 | /* only grow if not in place */ |
857 | if (strbuf_avail(buf) + buf->len < len) | |
858 | strbuf_grow(buf, len - buf->len); | |
5ecd293d PH |
859 | dst = buf->buf; |
860 | for (;;) { | |
861 | dollar = memchr(src, '$', len); | |
862 | if (!dollar) | |
863 | break; | |
77321184 | 864 | memmove(dst, src, dollar + 1 - src); |
5ecd293d PH |
865 | dst += dollar + 1 - src; |
866 | len -= dollar + 1 - src; | |
867 | src = dollar + 1; | |
868 | ||
869 | if (len > 3 && !memcmp(src, "Id:", 3)) { | |
870 | dollar = memchr(src + 3, '$', len - 3); | |
871 | if (!dollar) | |
872 | break; | |
a9f3049f HG |
873 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
874 | /* Line break before the next dollar. */ | |
875 | continue; | |
876 | } | |
877 | ||
af9b54bb AP |
878 | memcpy(dst, "Id$", 3); |
879 | dst += 3; | |
5ecd293d PH |
880 | len -= dollar + 1 - src; |
881 | src = dollar + 1; | |
3fed15f5 JH |
882 | } |
883 | } | |
77321184 | 884 | memmove(dst, src, len); |
5ecd293d PH |
885 | strbuf_setlen(buf, dst + len - buf->buf); |
886 | return 1; | |
3fed15f5 JH |
887 | } |
888 | ||
5ecd293d PH |
889 | static int ident_to_worktree(const char *path, const char *src, size_t len, |
890 | struct strbuf *buf, int ident) | |
3fed15f5 | 891 | { |
3fed15f5 | 892 | unsigned char sha1[20]; |
07814d90 | 893 | char *to_free = NULL, *dollar, *spc; |
5ecd293d | 894 | int cnt; |
3fed15f5 JH |
895 | |
896 | if (!ident) | |
5ecd293d | 897 | return 0; |
3fed15f5 | 898 | |
5ecd293d | 899 | cnt = count_ident(src, len); |
3fed15f5 | 900 | if (!cnt) |
5ecd293d | 901 | return 0; |
3fed15f5 | 902 | |
5ecd293d PH |
903 | /* are we "faking" in place editing ? */ |
904 | if (src == buf->buf) | |
b315c5c0 | 905 | to_free = strbuf_detach(buf, NULL); |
5ecd293d | 906 | hash_sha1_file(src, len, "blob", sha1); |
3fed15f5 | 907 | |
5ecd293d PH |
908 | strbuf_grow(buf, len + cnt * 43); |
909 | for (;;) { | |
910 | /* step 1: run to the next '$' */ | |
911 | dollar = memchr(src, '$', len); | |
912 | if (!dollar) | |
913 | break; | |
914 | strbuf_add(buf, src, dollar + 1 - src); | |
915 | len -= dollar + 1 - src; | |
916 | src = dollar + 1; | |
c23290d5 | 917 | |
5ecd293d PH |
918 | /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */ |
919 | if (len < 3 || memcmp("Id", src, 2)) | |
3fed15f5 JH |
920 | continue; |
921 | ||
5ecd293d PH |
922 | /* step 3: skip over Id$ or Id:xxxxx$ */ |
923 | if (src[2] == '$') { | |
924 | src += 3; | |
925 | len -= 3; | |
926 | } else if (src[2] == ':') { | |
927 | /* | |
928 | * It's possible that an expanded Id has crept its way into the | |
07814d90 HG |
929 | * repository, we cope with that by stripping the expansion out. |
930 | * This is probably not a good idea, since it will cause changes | |
931 | * on checkout, which won't go away by stash, but let's keep it | |
932 | * for git-style ids. | |
5ecd293d PH |
933 | */ |
934 | dollar = memchr(src + 3, '$', len - 3); | |
935 | if (!dollar) { | |
936 | /* incomplete keyword, no more '$', so just quit the loop */ | |
937 | break; | |
938 | } | |
c23290d5 | 939 | |
a9f3049f HG |
940 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
941 | /* Line break before the next dollar. */ | |
942 | continue; | |
943 | } | |
944 | ||
07814d90 HG |
945 | spc = memchr(src + 4, ' ', dollar - src - 4); |
946 | if (spc && spc < dollar-1) { | |
947 | /* There are spaces in unexpected places. | |
948 | * This is probably an id from some other | |
949 | * versioning system. Keep it for now. | |
950 | */ | |
951 | continue; | |
952 | } | |
953 | ||
5ecd293d PH |
954 | len -= dollar + 1 - src; |
955 | src = dollar + 1; | |
956 | } else { | |
957 | /* it wasn't a "Id$" or "Id:xxxx$" */ | |
958 | continue; | |
959 | } | |
c23290d5 | 960 | |
5ecd293d PH |
961 | /* step 4: substitute */ |
962 | strbuf_addstr(buf, "Id: "); | |
963 | strbuf_add(buf, sha1_to_hex(sha1), 40); | |
964 | strbuf_addstr(buf, " $"); | |
3fed15f5 | 965 | } |
5ecd293d | 966 | strbuf_add(buf, src, len); |
3fed15f5 | 967 | |
5ecd293d PH |
968 | free(to_free); |
969 | return 1; | |
35ebfd6a JH |
970 | } |
971 | ||
7bd18054 | 972 | static enum crlf_action git_path_check_crlf(struct attr_check_item *check) |
35ebfd6a | 973 | { |
6073ee85 JH |
974 | const char *value = check->value; |
975 | ||
976 | if (ATTR_TRUE(value)) | |
977 | return CRLF_TEXT; | |
978 | else if (ATTR_FALSE(value)) | |
979 | return CRLF_BINARY; | |
980 | else if (ATTR_UNSET(value)) | |
981 | ; | |
982 | else if (!strcmp(value, "input")) | |
df747b81 | 983 | return CRLF_TEXT_INPUT; |
fd6cce9e EB |
984 | else if (!strcmp(value, "auto")) |
985 | return CRLF_AUTO; | |
df747b81 | 986 | return CRLF_UNDEFINED; |
35ebfd6a JH |
987 | } |
988 | ||
7bd18054 | 989 | static enum eol git_path_check_eol(struct attr_check_item *check) |
fd6cce9e EB |
990 | { |
991 | const char *value = check->value; | |
992 | ||
993 | if (ATTR_UNSET(value)) | |
994 | ; | |
995 | else if (!strcmp(value, "lf")) | |
996 | return EOL_LF; | |
997 | else if (!strcmp(value, "crlf")) | |
998 | return EOL_CRLF; | |
999 | return EOL_UNSET; | |
1000 | } | |
1001 | ||
7bd18054 | 1002 | static struct convert_driver *git_path_check_convert(struct attr_check_item *check) |
aa4ed402 JH |
1003 | { |
1004 | const char *value = check->value; | |
1005 | struct convert_driver *drv; | |
1006 | ||
1007 | if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value)) | |
1008 | return NULL; | |
1009 | for (drv = user_convert; drv; drv = drv->next) | |
1010 | if (!strcmp(value, drv->name)) | |
1011 | return drv; | |
1012 | return NULL; | |
1013 | } | |
1014 | ||
7bd18054 | 1015 | static int git_path_check_ident(struct attr_check_item *check) |
3fed15f5 JH |
1016 | { |
1017 | const char *value = check->value; | |
1018 | ||
1019 | return !!ATTR_TRUE(value); | |
1020 | } | |
1021 | ||
3bfba20d JH |
1022 | struct conv_attrs { |
1023 | struct convert_driver *drv; | |
bb211b4d TB |
1024 | enum crlf_action attr_action; /* What attr says */ |
1025 | enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */ | |
3bfba20d JH |
1026 | int ident; |
1027 | }; | |
1028 | ||
3bfba20d | 1029 | static void convert_attrs(struct conv_attrs *ca, const char *path) |
83295964 | 1030 | { |
2aef63d3 | 1031 | static struct attr_check *check; |
83295964 | 1032 | |
2aef63d3 JH |
1033 | if (!check) { |
1034 | check = attr_check_initl("crlf", "ident", "filter", | |
1035 | "eol", "text", NULL); | |
83295964 JH |
1036 | user_convert_tail = &user_convert; |
1037 | git_config(read_convert_config, NULL); | |
1038 | } | |
3bfba20d | 1039 | |
2aef63d3 JH |
1040 | if (!git_check_attr(path, check)) { |
1041 | struct attr_check_item *ccheck = check->items; | |
bb211b4d | 1042 | ca->crlf_action = git_path_check_crlf(ccheck + 4); |
df747b81 | 1043 | if (ca->crlf_action == CRLF_UNDEFINED) |
92cce135 TB |
1044 | ca->crlf_action = git_path_check_crlf(ccheck + 0); |
1045 | ca->ident = git_path_check_ident(ccheck + 1); | |
1046 | ca->drv = git_path_check_convert(ccheck + 2); | |
817a0c79 TB |
1047 | if (ca->crlf_action != CRLF_BINARY) { |
1048 | enum eol eol_attr = git_path_check_eol(ccheck + 3); | |
65237284 TB |
1049 | if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF) |
1050 | ca->crlf_action = CRLF_AUTO_INPUT; | |
1051 | else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF) | |
1052 | ca->crlf_action = CRLF_AUTO_CRLF; | |
1053 | else if (eol_attr == EOL_LF) | |
817a0c79 TB |
1054 | ca->crlf_action = CRLF_TEXT_INPUT; |
1055 | else if (eol_attr == EOL_CRLF) | |
1056 | ca->crlf_action = CRLF_TEXT_CRLF; | |
1057 | } | |
3bfba20d JH |
1058 | } else { |
1059 | ca->drv = NULL; | |
df747b81 | 1060 | ca->crlf_action = CRLF_UNDEFINED; |
3bfba20d JH |
1061 | ca->ident = 0; |
1062 | } | |
5c94c93d MÅ |
1063 | |
1064 | /* Save attr and make a decision for action */ | |
1065 | ca->attr_action = ca->crlf_action; | |
df747b81 TB |
1066 | if (ca->crlf_action == CRLF_TEXT) |
1067 | ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT; | |
1068 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE) | |
1069 | ca->crlf_action = CRLF_BINARY; | |
1070 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE) | |
1071 | ca->crlf_action = CRLF_AUTO_CRLF; | |
1072 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT) | |
1073 | ca->crlf_action = CRLF_AUTO_INPUT; | |
83295964 JH |
1074 | } |
1075 | ||
9035d75a SP |
1076 | int would_convert_to_git_filter_fd(const char *path) |
1077 | { | |
1078 | struct conv_attrs ca; | |
1079 | ||
1080 | convert_attrs(&ca, path); | |
1081 | if (!ca.drv) | |
1082 | return 0; | |
1083 | ||
1084 | /* | |
1085 | * Apply a filter to an fd only if the filter is required to succeed. | |
1086 | * We must die if the filter fails, because the original data before | |
1087 | * filtering is not available. | |
1088 | */ | |
1089 | if (!ca.drv->required) | |
1090 | return 0; | |
1091 | ||
2841e8f8 | 1092 | return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL); |
9035d75a SP |
1093 | } |
1094 | ||
a7630bd4 TB |
1095 | const char *get_convert_attr_ascii(const char *path) |
1096 | { | |
1097 | struct conv_attrs ca; | |
a7630bd4 TB |
1098 | |
1099 | convert_attrs(&ca, path); | |
bb211b4d | 1100 | switch (ca.attr_action) { |
df747b81 | 1101 | case CRLF_UNDEFINED: |
a7630bd4 TB |
1102 | return ""; |
1103 | case CRLF_BINARY: | |
1104 | return "-text"; | |
1105 | case CRLF_TEXT: | |
1106 | return "text"; | |
df747b81 | 1107 | case CRLF_TEXT_INPUT: |
a7630bd4 | 1108 | return "text eol=lf"; |
df747b81 TB |
1109 | case CRLF_TEXT_CRLF: |
1110 | return "text eol=crlf"; | |
a7630bd4 TB |
1111 | case CRLF_AUTO: |
1112 | return "text=auto"; | |
df747b81 | 1113 | case CRLF_AUTO_CRLF: |
65237284 | 1114 | return "text=auto eol=crlf"; |
df747b81 | 1115 | case CRLF_AUTO_INPUT: |
65237284 | 1116 | return "text=auto eol=lf"; |
a7630bd4 TB |
1117 | } |
1118 | return ""; | |
1119 | } | |
1120 | ||
82b474e0 BW |
1121 | int convert_to_git(const struct index_state *istate, |
1122 | const char *path, const char *src, size_t len, | |
21e5ad50 | 1123 | struct strbuf *dst, enum safe_crlf checksafe) |
35ebfd6a | 1124 | { |
3bfba20d | 1125 | int ret = 0; |
3bfba20d | 1126 | struct conv_attrs ca; |
6073ee85 | 1127 | |
3bfba20d | 1128 | convert_attrs(&ca, path); |
3fed15f5 | 1129 | |
2841e8f8 | 1130 | ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL); |
234fa07e | 1131 | if (!ret && ca.drv && ca.drv->required) |
36daaaca JB |
1132 | die("%s: clean filter '%s' failed", path, ca.drv->name); |
1133 | ||
92ac3197 | 1134 | if (ret && dst) { |
5ecd293d PH |
1135 | src = dst->buf; |
1136 | len = dst->len; | |
aa4ed402 | 1137 | } |
2fea9de6 TB |
1138 | if (checksafe != SAFE_CRLF_KEEP_CRLF) { |
1139 | ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, checksafe); | |
1140 | if (ret && dst) { | |
1141 | src = dst->buf; | |
1142 | len = dst->len; | |
1143 | } | |
6073ee85 | 1144 | } |
3bfba20d | 1145 | return ret | ident_to_git(path, src, len, dst, ca.ident); |
35ebfd6a JH |
1146 | } |
1147 | ||
d6c41c20 BW |
1148 | void convert_to_git_filter_fd(const struct index_state *istate, |
1149 | const char *path, int fd, struct strbuf *dst, | |
9035d75a SP |
1150 | enum safe_crlf checksafe) |
1151 | { | |
1152 | struct conv_attrs ca; | |
1153 | convert_attrs(&ca, path); | |
1154 | ||
1155 | assert(ca.drv); | |
edcc8581 | 1156 | assert(ca.drv->clean || ca.drv->process); |
9035d75a | 1157 | |
2841e8f8 | 1158 | if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL)) |
9035d75a SP |
1159 | die("%s: clean filter '%s' failed", path, ca.drv->name); |
1160 | ||
d6c41c20 | 1161 | crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, checksafe); |
9035d75a SP |
1162 | ident_to_git(path, dst->buf, dst->len, dst, ca.ident); |
1163 | } | |
1164 | ||
43dd2332 EB |
1165 | static int convert_to_working_tree_internal(const char *path, const char *src, |
1166 | size_t len, struct strbuf *dst, | |
2841e8f8 | 1167 | int normalizing, struct delayed_checkout *dco) |
35ebfd6a | 1168 | { |
36daaaca | 1169 | int ret = 0, ret_filter = 0; |
3bfba20d | 1170 | struct conv_attrs ca; |
6073ee85 | 1171 | |
3bfba20d | 1172 | convert_attrs(&ca, path); |
3fed15f5 | 1173 | |
3bfba20d | 1174 | ret |= ident_to_worktree(path, src, len, dst, ca.ident); |
5ecd293d PH |
1175 | if (ret) { |
1176 | src = dst->buf; | |
1177 | len = dst->len; | |
3fed15f5 | 1178 | } |
43dd2332 EB |
1179 | /* |
1180 | * CRLF conversion can be skipped if normalizing, unless there | |
edcc8581 LS |
1181 | * is a smudge or process filter (even if the process filter doesn't |
1182 | * support smudge). The filters might expect CRLFs. | |
43dd2332 | 1183 | */ |
edcc8581 | 1184 | if ((ca.drv && (ca.drv->smudge || ca.drv->process)) || !normalizing) { |
3bfba20d | 1185 | ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action); |
43dd2332 EB |
1186 | if (ret) { |
1187 | src = dst->buf; | |
1188 | len = dst->len; | |
1189 | } | |
aa4ed402 | 1190 | } |
36daaaca | 1191 | |
2841e8f8 LS |
1192 | ret_filter = apply_filter( |
1193 | path, src, len, -1, dst, ca.drv, CAP_SMUDGE, dco); | |
234fa07e | 1194 | if (!ret_filter && ca.drv && ca.drv->required) |
36daaaca JB |
1195 | die("%s: smudge filter %s failed", path, ca.drv->name); |
1196 | ||
1197 | return ret | ret_filter; | |
35ebfd6a | 1198 | } |
f217f0e8 | 1199 | |
2841e8f8 LS |
1200 | int async_convert_to_working_tree(const char *path, const char *src, |
1201 | size_t len, struct strbuf *dst, | |
1202 | void *dco) | |
1203 | { | |
1204 | return convert_to_working_tree_internal(path, src, len, dst, 0, dco); | |
1205 | } | |
1206 | ||
43dd2332 EB |
1207 | int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst) |
1208 | { | |
2841e8f8 | 1209 | return convert_to_working_tree_internal(path, src, len, dst, 0, NULL); |
43dd2332 EB |
1210 | } |
1211 | ||
a33e0b2a BW |
1212 | int renormalize_buffer(const struct index_state *istate, const char *path, |
1213 | const char *src, size_t len, struct strbuf *dst) | |
f217f0e8 | 1214 | { |
2841e8f8 | 1215 | int ret = convert_to_working_tree_internal(path, src, len, dst, 1, NULL); |
f217f0e8 EB |
1216 | if (ret) { |
1217 | src = dst->buf; | |
1218 | len = dst->len; | |
1219 | } | |
a33e0b2a | 1220 | return ret | convert_to_git(istate, path, src, len, dst, SAFE_CRLF_RENORMALIZE); |
f217f0e8 | 1221 | } |
dd8e9121 | 1222 | |
b6691092 JH |
1223 | /***************************************************************** |
1224 | * | |
749f763d | 1225 | * Streaming conversion support |
b6691092 JH |
1226 | * |
1227 | *****************************************************************/ | |
1228 | ||
1229 | typedef int (*filter_fn)(struct stream_filter *, | |
1230 | const char *input, size_t *isize_p, | |
1231 | char *output, size_t *osize_p); | |
1232 | typedef void (*free_fn)(struct stream_filter *); | |
1233 | ||
1234 | struct stream_filter_vtbl { | |
1235 | filter_fn filter; | |
1236 | free_fn free; | |
1237 | }; | |
1238 | ||
1239 | struct stream_filter { | |
1240 | struct stream_filter_vtbl *vtbl; | |
1241 | }; | |
1242 | ||
1243 | static int null_filter_fn(struct stream_filter *filter, | |
1244 | const char *input, size_t *isize_p, | |
1245 | char *output, size_t *osize_p) | |
1246 | { | |
4ae66704 JH |
1247 | size_t count; |
1248 | ||
1249 | if (!input) | |
1250 | return 0; /* we do not keep any states */ | |
1251 | count = *isize_p; | |
b6691092 JH |
1252 | if (*osize_p < count) |
1253 | count = *osize_p; | |
1254 | if (count) { | |
1255 | memmove(output, input, count); | |
1256 | *isize_p -= count; | |
1257 | *osize_p -= count; | |
1258 | } | |
1259 | return 0; | |
1260 | } | |
1261 | ||
1262 | static void null_free_fn(struct stream_filter *filter) | |
1263 | { | |
1264 | ; /* nothing -- null instances are shared */ | |
1265 | } | |
1266 | ||
1267 | static struct stream_filter_vtbl null_vtbl = { | |
1268 | null_filter_fn, | |
1269 | null_free_fn, | |
1270 | }; | |
1271 | ||
1272 | static struct stream_filter null_filter_singleton = { | |
1273 | &null_vtbl, | |
1274 | }; | |
1275 | ||
1276 | int is_null_stream_filter(struct stream_filter *filter) | |
1277 | { | |
1278 | return filter == &null_filter_singleton; | |
1279 | } | |
1280 | ||
b84c7839 JH |
1281 | |
1282 | /* | |
1283 | * LF-to-CRLF filter | |
1284 | */ | |
284e3d28 CMN |
1285 | |
1286 | struct lf_to_crlf_filter { | |
1287 | struct stream_filter filter; | |
8496f568 JH |
1288 | unsigned has_held:1; |
1289 | char held; | |
284e3d28 CMN |
1290 | }; |
1291 | ||
e322ee38 JH |
1292 | static int lf_to_crlf_filter_fn(struct stream_filter *filter, |
1293 | const char *input, size_t *isize_p, | |
1294 | char *output, size_t *osize_p) | |
1295 | { | |
284e3d28 CMN |
1296 | size_t count, o = 0; |
1297 | struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter; | |
1298 | ||
8496f568 JH |
1299 | /* |
1300 | * We may be holding onto the CR to see if it is followed by a | |
1301 | * LF, in which case we would need to go to the main loop. | |
1302 | * Otherwise, just emit it to the output stream. | |
1303 | */ | |
1304 | if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) { | |
1305 | output[o++] = lf_to_crlf->held; | |
1306 | lf_to_crlf->has_held = 0; | |
284e3d28 | 1307 | } |
e322ee38 | 1308 | |
87afe9a5 JH |
1309 | /* We are told to drain */ |
1310 | if (!input) { | |
1311 | *osize_p -= o; | |
1312 | return 0; | |
1313 | } | |
e322ee38 | 1314 | |
e322ee38 | 1315 | count = *isize_p; |
8496f568 | 1316 | if (count || lf_to_crlf->has_held) { |
284e3d28 | 1317 | size_t i; |
8496f568 JH |
1318 | int was_cr = 0; |
1319 | ||
1320 | if (lf_to_crlf->has_held) { | |
1321 | was_cr = 1; | |
1322 | lf_to_crlf->has_held = 0; | |
1323 | } | |
1324 | ||
284e3d28 | 1325 | for (i = 0; o < *osize_p && i < count; i++) { |
e322ee38 | 1326 | char ch = input[i]; |
8496f568 | 1327 | |
e322ee38 | 1328 | if (ch == '\n') { |
284e3d28 | 1329 | output[o++] = '\r'; |
8496f568 JH |
1330 | } else if (was_cr) { |
1331 | /* | |
1332 | * Previous round saw CR and it is not followed | |
1333 | * by a LF; emit the CR before processing the | |
1334 | * current character. | |
1335 | */ | |
1336 | output[o++] = '\r'; | |
e322ee38 | 1337 | } |
8496f568 JH |
1338 | |
1339 | /* | |
1340 | * We may have consumed the last output slot, | |
1341 | * in which case we need to break out of this | |
1342 | * loop; hold the current character before | |
1343 | * returning. | |
1344 | */ | |
1345 | if (*osize_p <= o) { | |
1346 | lf_to_crlf->has_held = 1; | |
1347 | lf_to_crlf->held = ch; | |
1348 | continue; /* break but increment i */ | |
1349 | } | |
1350 | ||
1351 | if (ch == '\r') { | |
1352 | was_cr = 1; | |
1353 | continue; | |
1354 | } | |
1355 | ||
1356 | was_cr = 0; | |
e322ee38 JH |
1357 | output[o++] = ch; |
1358 | } | |
1359 | ||
1360 | *osize_p -= o; | |
1361 | *isize_p -= i; | |
8496f568 JH |
1362 | |
1363 | if (!lf_to_crlf->has_held && was_cr) { | |
1364 | lf_to_crlf->has_held = 1; | |
1365 | lf_to_crlf->held = '\r'; | |
1366 | } | |
e322ee38 JH |
1367 | } |
1368 | return 0; | |
1369 | } | |
1370 | ||
284e3d28 CMN |
1371 | static void lf_to_crlf_free_fn(struct stream_filter *filter) |
1372 | { | |
1373 | free(filter); | |
1374 | } | |
1375 | ||
e322ee38 JH |
1376 | static struct stream_filter_vtbl lf_to_crlf_vtbl = { |
1377 | lf_to_crlf_filter_fn, | |
284e3d28 | 1378 | lf_to_crlf_free_fn, |
e322ee38 JH |
1379 | }; |
1380 | ||
284e3d28 CMN |
1381 | static struct stream_filter *lf_to_crlf_filter(void) |
1382 | { | |
87afe9a5 | 1383 | struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf)); |
e322ee38 | 1384 | |
284e3d28 | 1385 | lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl; |
284e3d28 CMN |
1386 | return (struct stream_filter *)lf_to_crlf; |
1387 | } | |
b84c7839 | 1388 | |
a265a7f9 JH |
1389 | /* |
1390 | * Cascade filter | |
1391 | */ | |
1392 | #define FILTER_BUFFER 1024 | |
1393 | struct cascade_filter { | |
1394 | struct stream_filter filter; | |
1395 | struct stream_filter *one; | |
1396 | struct stream_filter *two; | |
1397 | char buf[FILTER_BUFFER]; | |
1398 | int end, ptr; | |
1399 | }; | |
1400 | ||
1401 | static int cascade_filter_fn(struct stream_filter *filter, | |
1402 | const char *input, size_t *isize_p, | |
1403 | char *output, size_t *osize_p) | |
1404 | { | |
1405 | struct cascade_filter *cas = (struct cascade_filter *) filter; | |
1406 | size_t filled = 0; | |
1407 | size_t sz = *osize_p; | |
1408 | size_t to_feed, remaining; | |
1409 | ||
1410 | /* | |
1411 | * input -- (one) --> buf -- (two) --> output | |
1412 | */ | |
1413 | while (filled < sz) { | |
1414 | remaining = sz - filled; | |
1415 | ||
1416 | /* do we already have something to feed two with? */ | |
1417 | if (cas->ptr < cas->end) { | |
1418 | to_feed = cas->end - cas->ptr; | |
1419 | if (stream_filter(cas->two, | |
1420 | cas->buf + cas->ptr, &to_feed, | |
1421 | output + filled, &remaining)) | |
1422 | return -1; | |
1423 | cas->ptr += (cas->end - cas->ptr) - to_feed; | |
1424 | filled = sz - remaining; | |
1425 | continue; | |
1426 | } | |
1427 | ||
1428 | /* feed one from upstream and have it emit into our buffer */ | |
1429 | to_feed = input ? *isize_p : 0; | |
1430 | if (input && !to_feed) | |
1431 | break; | |
1432 | remaining = sizeof(cas->buf); | |
1433 | if (stream_filter(cas->one, | |
1434 | input, &to_feed, | |
1435 | cas->buf, &remaining)) | |
1436 | return -1; | |
1437 | cas->end = sizeof(cas->buf) - remaining; | |
1438 | cas->ptr = 0; | |
1439 | if (input) { | |
1440 | size_t fed = *isize_p - to_feed; | |
1441 | *isize_p -= fed; | |
1442 | input += fed; | |
1443 | } | |
1444 | ||
1445 | /* do we know that we drained one completely? */ | |
1446 | if (input || cas->end) | |
1447 | continue; | |
1448 | ||
1449 | /* tell two to drain; we have nothing more to give it */ | |
1450 | to_feed = 0; | |
1451 | remaining = sz - filled; | |
1452 | if (stream_filter(cas->two, | |
1453 | NULL, &to_feed, | |
1454 | output + filled, &remaining)) | |
1455 | return -1; | |
1456 | if (remaining == (sz - filled)) | |
1457 | break; /* completely drained two */ | |
1458 | filled = sz - remaining; | |
1459 | } | |
1460 | *osize_p -= filled; | |
1461 | return 0; | |
1462 | } | |
1463 | ||
1464 | static void cascade_free_fn(struct stream_filter *filter) | |
1465 | { | |
1466 | struct cascade_filter *cas = (struct cascade_filter *)filter; | |
1467 | free_stream_filter(cas->one); | |
1468 | free_stream_filter(cas->two); | |
1469 | free(filter); | |
1470 | } | |
1471 | ||
1472 | static struct stream_filter_vtbl cascade_vtbl = { | |
1473 | cascade_filter_fn, | |
1474 | cascade_free_fn, | |
1475 | }; | |
1476 | ||
1477 | static struct stream_filter *cascade_filter(struct stream_filter *one, | |
1478 | struct stream_filter *two) | |
1479 | { | |
1480 | struct cascade_filter *cascade; | |
1481 | ||
1482 | if (!one || is_null_stream_filter(one)) | |
1483 | return two; | |
1484 | if (!two || is_null_stream_filter(two)) | |
1485 | return one; | |
1486 | ||
1487 | cascade = xmalloc(sizeof(*cascade)); | |
1488 | cascade->one = one; | |
1489 | cascade->two = two; | |
1490 | cascade->end = cascade->ptr = 0; | |
1491 | cascade->filter.vtbl = &cascade_vtbl; | |
1492 | return (struct stream_filter *)cascade; | |
1493 | } | |
1494 | ||
b84c7839 JH |
1495 | /* |
1496 | * ident filter | |
1497 | */ | |
1498 | #define IDENT_DRAINING (-1) | |
1499 | #define IDENT_SKIPPING (-2) | |
1500 | struct ident_filter { | |
1501 | struct stream_filter filter; | |
1502 | struct strbuf left; | |
1503 | int state; | |
1504 | char ident[45]; /* ": x40 $" */ | |
1505 | }; | |
1506 | ||
1507 | static int is_foreign_ident(const char *str) | |
1508 | { | |
1509 | int i; | |
1510 | ||
ae021d87 | 1511 | if (!skip_prefix(str, "$Id: ", &str)) |
b84c7839 | 1512 | return 0; |
ae021d87 | 1513 | for (i = 0; str[i]; i++) { |
b84c7839 JH |
1514 | if (isspace(str[i]) && str[i+1] != '$') |
1515 | return 1; | |
1516 | } | |
1517 | return 0; | |
1518 | } | |
1519 | ||
1520 | static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p) | |
1521 | { | |
1522 | size_t to_drain = ident->left.len; | |
1523 | ||
1524 | if (*osize_p < to_drain) | |
1525 | to_drain = *osize_p; | |
1526 | if (to_drain) { | |
1527 | memcpy(*output_p, ident->left.buf, to_drain); | |
1528 | strbuf_remove(&ident->left, 0, to_drain); | |
1529 | *output_p += to_drain; | |
1530 | *osize_p -= to_drain; | |
1531 | } | |
1532 | if (!ident->left.len) | |
1533 | ident->state = 0; | |
1534 | } | |
1535 | ||
1536 | static int ident_filter_fn(struct stream_filter *filter, | |
1537 | const char *input, size_t *isize_p, | |
1538 | char *output, size_t *osize_p) | |
1539 | { | |
1540 | struct ident_filter *ident = (struct ident_filter *)filter; | |
1541 | static const char head[] = "$Id"; | |
1542 | ||
1543 | if (!input) { | |
1544 | /* drain upon eof */ | |
1545 | switch (ident->state) { | |
1546 | default: | |
1547 | strbuf_add(&ident->left, head, ident->state); | |
1cf01a34 | 1548 | /* fallthrough */ |
b84c7839 | 1549 | case IDENT_SKIPPING: |
1cf01a34 | 1550 | /* fallthrough */ |
b84c7839 JH |
1551 | case IDENT_DRAINING: |
1552 | ident_drain(ident, &output, osize_p); | |
1553 | } | |
1554 | return 0; | |
1555 | } | |
1556 | ||
1557 | while (*isize_p || (ident->state == IDENT_DRAINING)) { | |
1558 | int ch; | |
1559 | ||
1560 | if (ident->state == IDENT_DRAINING) { | |
1561 | ident_drain(ident, &output, osize_p); | |
1562 | if (!*osize_p) | |
1563 | break; | |
1564 | continue; | |
1565 | } | |
1566 | ||
1567 | ch = *(input++); | |
1568 | (*isize_p)--; | |
1569 | ||
1570 | if (ident->state == IDENT_SKIPPING) { | |
1571 | /* | |
1572 | * Skipping until '$' or LF, but keeping them | |
1573 | * in case it is a foreign ident. | |
1574 | */ | |
1575 | strbuf_addch(&ident->left, ch); | |
1576 | if (ch != '\n' && ch != '$') | |
1577 | continue; | |
1578 | if (ch == '$' && !is_foreign_ident(ident->left.buf)) { | |
1579 | strbuf_setlen(&ident->left, sizeof(head) - 1); | |
1580 | strbuf_addstr(&ident->left, ident->ident); | |
1581 | } | |
1582 | ident->state = IDENT_DRAINING; | |
1583 | continue; | |
1584 | } | |
1585 | ||
1586 | if (ident->state < sizeof(head) && | |
1587 | head[ident->state] == ch) { | |
1588 | ident->state++; | |
1589 | continue; | |
1590 | } | |
1591 | ||
1592 | if (ident->state) | |
1593 | strbuf_add(&ident->left, head, ident->state); | |
1594 | if (ident->state == sizeof(head) - 1) { | |
1595 | if (ch != ':' && ch != '$') { | |
1596 | strbuf_addch(&ident->left, ch); | |
1597 | ident->state = 0; | |
1598 | continue; | |
1599 | } | |
1600 | ||
1601 | if (ch == ':') { | |
1602 | strbuf_addch(&ident->left, ch); | |
1603 | ident->state = IDENT_SKIPPING; | |
1604 | } else { | |
1605 | strbuf_addstr(&ident->left, ident->ident); | |
1606 | ident->state = IDENT_DRAINING; | |
1607 | } | |
1608 | continue; | |
1609 | } | |
1610 | ||
1611 | strbuf_addch(&ident->left, ch); | |
1612 | ident->state = IDENT_DRAINING; | |
1613 | } | |
1614 | return 0; | |
1615 | } | |
1616 | ||
1617 | static void ident_free_fn(struct stream_filter *filter) | |
1618 | { | |
1619 | struct ident_filter *ident = (struct ident_filter *)filter; | |
1620 | strbuf_release(&ident->left); | |
1621 | free(filter); | |
1622 | } | |
1623 | ||
1624 | static struct stream_filter_vtbl ident_vtbl = { | |
1625 | ident_filter_fn, | |
1626 | ident_free_fn, | |
1627 | }; | |
1628 | ||
1629 | static struct stream_filter *ident_filter(const unsigned char *sha1) | |
1630 | { | |
1631 | struct ident_filter *ident = xmalloc(sizeof(*ident)); | |
1632 | ||
5096d490 JK |
1633 | xsnprintf(ident->ident, sizeof(ident->ident), |
1634 | ": %s $", sha1_to_hex(sha1)); | |
b84c7839 JH |
1635 | strbuf_init(&ident->left, 0); |
1636 | ident->filter.vtbl = &ident_vtbl; | |
1637 | ident->state = 0; | |
1638 | return (struct stream_filter *)ident; | |
1639 | } | |
1640 | ||
dd8e9121 | 1641 | /* |
b6691092 JH |
1642 | * Return an appropriately constructed filter for the path, or NULL if |
1643 | * the contents cannot be filtered without reading the whole thing | |
1644 | * in-core. | |
1645 | * | |
1646 | * Note that you would be crazy to set CRLF, smuge/clean or ident to a | |
1647 | * large binary blob you would want us not to slurp into the memory! | |
dd8e9121 | 1648 | */ |
b6691092 | 1649 | struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1) |
dd8e9121 JH |
1650 | { |
1651 | struct conv_attrs ca; | |
b84c7839 | 1652 | struct stream_filter *filter = NULL; |
dd8e9121 JH |
1653 | |
1654 | convert_attrs(&ca, path); | |
edcc8581 | 1655 | if (ca.drv && (ca.drv->process || ca.drv->smudge || ca.drv->clean)) |
caa47adc TB |
1656 | return NULL; |
1657 | ||
1658 | if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF) | |
1659 | return NULL; | |
b84c7839 JH |
1660 | |
1661 | if (ca.ident) | |
1662 | filter = ident_filter(sha1); | |
dd8e9121 | 1663 | |
caa47adc | 1664 | if (output_eol(ca.crlf_action) == EOL_CRLF) |
284e3d28 | 1665 | filter = cascade_filter(filter, lf_to_crlf_filter()); |
caa47adc TB |
1666 | else |
1667 | filter = cascade_filter(filter, &null_filter_singleton); | |
e322ee38 | 1668 | |
b84c7839 | 1669 | return filter; |
b6691092 JH |
1670 | } |
1671 | ||
1672 | void free_stream_filter(struct stream_filter *filter) | |
1673 | { | |
1674 | filter->vtbl->free(filter); | |
1675 | } | |
1676 | ||
1677 | int stream_filter(struct stream_filter *filter, | |
1678 | const char *input, size_t *isize_p, | |
1679 | char *output, size_t *osize_p) | |
1680 | { | |
1681 | return filter->vtbl->filter(filter, input, isize_p, output, osize_p); | |
dd8e9121 | 1682 | } |