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