]>
Commit | Line | Data |
---|---|---|
bc5c5ec0 | 1 | #include "git-compat-util.h" |
6c6ddf92 | 2 | #include "advice.h" |
b2141fc1 | 3 | #include "config.h" |
73359a9b | 4 | #include "convert.h" |
d5fff46f | 5 | #include "copy.h" |
f394e093 | 6 | #include "gettext.h" |
41771fa4 | 7 | #include "hex.h" |
a034e910 | 8 | #include "object-store-ll.h" |
35ebfd6a | 9 | #include "attr.h" |
3fed15f5 | 10 | #include "run-command.h" |
a2b665de | 11 | #include "quote.h" |
08c46a49 | 12 | #include "read-cache-ll.h" |
6424c2ad | 13 | #include "sigchain.h" |
edcc8581 | 14 | #include "pkt-line.h" |
99605d62 | 15 | #include "sub-process.h" |
74ea5c95 | 16 | #include "trace.h" |
107642fe | 17 | #include "utf8.h" |
67238999 | 18 | #include "merge-ll.h" |
35ebfd6a | 19 | |
6c510bee LT |
20 | /* |
21 | * convert.c - convert a file when checking it out and checking it in. | |
22 | * | |
23 | * This should use the pathname to decide on whether it wants to do some | |
24 | * more interesting conversions (automatic gzip/unzip, general format | |
25 | * conversions etc etc), but by default it just does automatic CRLF<->LF | |
942e7747 | 26 | * translation when the "text" attribute or "auto_crlf" option is set. |
6c510bee LT |
27 | */ |
28 | ||
a7630bd4 TB |
29 | /* Stat bits: When BIN is set, the txt bits are unset */ |
30 | #define CONVERT_STAT_BITS_TXT_LF 0x1 | |
31 | #define CONVERT_STAT_BITS_TXT_CRLF 0x2 | |
32 | #define CONVERT_STAT_BITS_BIN 0x4 | |
33 | ||
6c510bee | 34 | struct text_stat { |
28624193 | 35 | /* NUL, CR, LF and CRLF counts */ |
6e336a53 | 36 | unsigned nul, lonecr, lonelf, crlf; |
6c510bee LT |
37 | |
38 | /* These are just approximations! */ | |
39 | unsigned printable, nonprintable; | |
40 | }; | |
41 | ||
42 | static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats) | |
43 | { | |
44 | unsigned long i; | |
45 | ||
46 | memset(stats, 0, sizeof(*stats)); | |
47 | ||
48 | for (i = 0; i < size; i++) { | |
49 | unsigned char c = buf[i]; | |
50 | if (c == '\r') { | |
6e336a53 | 51 | if (i+1 < size && buf[i+1] == '\n') { |
6c510bee | 52 | stats->crlf++; |
6e336a53 TB |
53 | i++; |
54 | } else | |
55 | stats->lonecr++; | |
6c510bee LT |
56 | continue; |
57 | } | |
58 | if (c == '\n') { | |
6e336a53 | 59 | stats->lonelf++; |
6c510bee LT |
60 | continue; |
61 | } | |
62 | if (c == 127) | |
63 | /* DEL */ | |
64 | stats->nonprintable++; | |
65 | else if (c < 32) { | |
66 | switch (c) { | |
67 | /* BS, HT, ESC and FF */ | |
68 | case '\b': case '\t': case '\033': case '\014': | |
69 | stats->printable++; | |
70 | break; | |
28624193 DP |
71 | case 0: |
72 | stats->nul++; | |
73 | /* fall through */ | |
6c510bee LT |
74 | default: |
75 | stats->nonprintable++; | |
76 | } | |
77 | } | |
78 | else | |
79 | stats->printable++; | |
80 | } | |
f9dd4bf4 DK |
81 | |
82 | /* If file ends with EOF then don't count this EOF as non-printable. */ | |
83 | if (size >= 1 && buf[size-1] == '\032') | |
84 | stats->nonprintable--; | |
6c510bee LT |
85 | } |
86 | ||
87 | /* | |
88 | * The same heuristics as diff.c::mmfile_is_binary() | |
a7630bd4 | 89 | * We treat files with bare CR as binary |
6c510bee | 90 | */ |
129beeee | 91 | static int convert_is_binary(const struct text_stat *stats) |
6c510bee | 92 | { |
6e336a53 | 93 | if (stats->lonecr) |
a7630bd4 | 94 | return 1; |
28624193 DP |
95 | if (stats->nul) |
96 | return 1; | |
6c510bee LT |
97 | if ((stats->printable >> 7) < stats->nonprintable) |
98 | return 1; | |
6c510bee LT |
99 | return 0; |
100 | } | |
101 | ||
a7630bd4 TB |
102 | static unsigned int gather_convert_stats(const char *data, unsigned long size) |
103 | { | |
104 | struct text_stat stats; | |
6e336a53 | 105 | int ret = 0; |
a7630bd4 TB |
106 | if (!data || !size) |
107 | return 0; | |
108 | gather_stats(data, size, &stats); | |
129beeee | 109 | if (convert_is_binary(&stats)) |
6e336a53 TB |
110 | ret |= CONVERT_STAT_BITS_BIN; |
111 | if (stats.crlf) | |
112 | ret |= CONVERT_STAT_BITS_TXT_CRLF; | |
113 | if (stats.lonelf) | |
114 | ret |= CONVERT_STAT_BITS_TXT_LF; | |
115 | ||
116 | return ret; | |
a7630bd4 TB |
117 | } |
118 | ||
119 | static const char *gather_convert_stats_ascii(const char *data, unsigned long size) | |
120 | { | |
121 | unsigned int convert_stats = gather_convert_stats(data, size); | |
122 | ||
123 | if (convert_stats & CONVERT_STAT_BITS_BIN) | |
124 | return "-text"; | |
125 | switch (convert_stats) { | |
126 | case CONVERT_STAT_BITS_TXT_LF: | |
127 | return "lf"; | |
128 | case CONVERT_STAT_BITS_TXT_CRLF: | |
129 | return "crlf"; | |
130 | case CONVERT_STAT_BITS_TXT_LF | CONVERT_STAT_BITS_TXT_CRLF: | |
131 | return "mixed"; | |
132 | default: | |
133 | return "none"; | |
134 | } | |
135 | } | |
136 | ||
847a9e5d | 137 | const char *get_cached_convert_stats_ascii(struct index_state *istate, |
a7609c54 | 138 | const char *path) |
a7630bd4 TB |
139 | { |
140 | const char *ret; | |
141 | unsigned long sz; | |
a7609c54 | 142 | void *data = read_blob_data_from_index(istate, path, &sz); |
a7630bd4 TB |
143 | ret = gather_convert_stats_ascii(data, sz); |
144 | free(data); | |
145 | return ret; | |
146 | } | |
147 | ||
148 | const char *get_wt_convert_stats_ascii(const char *path) | |
149 | { | |
150 | const char *ret = ""; | |
151 | struct strbuf sb = STRBUF_INIT; | |
152 | if (strbuf_read_file(&sb, path, 0) >= 0) | |
153 | ret = gather_convert_stats_ascii(sb.buf, sb.len); | |
154 | strbuf_release(&sb); | |
155 | return ret; | |
156 | } | |
157 | ||
4b4024f5 TB |
158 | static int text_eol_is_crlf(void) |
159 | { | |
160 | if (auto_crlf == AUTO_CRLF_TRUE) | |
161 | return 1; | |
162 | else if (auto_crlf == AUTO_CRLF_INPUT) | |
163 | return 0; | |
164 | if (core_eol == EOL_CRLF) | |
165 | return 1; | |
166 | if (core_eol == EOL_UNSET && EOL_NATIVE == EOL_CRLF) | |
167 | return 1; | |
168 | return 0; | |
169 | } | |
170 | ||
38e95844 | 171 | static enum eol output_eol(enum convert_crlf_action crlf_action) |
f217f0e8 | 172 | { |
c61dcff9 | 173 | switch (crlf_action) { |
942e7747 EB |
174 | case CRLF_BINARY: |
175 | return EOL_UNSET; | |
df747b81 | 176 | case CRLF_TEXT_CRLF: |
942e7747 | 177 | return EOL_CRLF; |
df747b81 | 178 | case CRLF_TEXT_INPUT: |
942e7747 | 179 | return EOL_LF; |
df747b81 TB |
180 | case CRLF_UNDEFINED: |
181 | case CRLF_AUTO_CRLF: | |
65237284 | 182 | return EOL_CRLF; |
df747b81 | 183 | case CRLF_AUTO_INPUT: |
65237284 | 184 | return EOL_LF; |
942e7747 EB |
185 | case CRLF_TEXT: |
186 | case CRLF_AUTO: | |
df747b81 | 187 | /* fall through */ |
4b4024f5 | 188 | return text_eol_is_crlf() ? EOL_CRLF : EOL_LF; |
942e7747 | 189 | } |
d26a328e | 190 | warning(_("illegal crlf_action %d"), (int)crlf_action); |
ec70f52f | 191 | return core_eol; |
942e7747 EB |
192 | } |
193 | ||
185e8652 | 194 | static void check_global_conv_flags_eol(const char *path, |
a0ad53c1 | 195 | struct text_stat *old_stats, struct text_stat *new_stats, |
8462ff43 | 196 | int conv_flags) |
21e5ad50 | 197 | { |
a0ad53c1 | 198 | if (old_stats->crlf && !new_stats->crlf ) { |
21e5ad50 | 199 | /* |
a0ad53c1 | 200 | * CRLFs would not be restored by checkout |
21e5ad50 | 201 | */ |
8462ff43 | 202 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
1a07e59c | 203 | die(_("CRLF would be replaced by LF in %s"), path); |
8462ff43 | 204 | else if (conv_flags & CONV_EOL_RNDTRP_WARN) |
c970d30c AH |
205 | warning(_("in the working copy of '%s', CRLF will be" |
206 | " replaced by LF the next time Git touches" | |
207 | " it"), path); | |
a0ad53c1 | 208 | } else if (old_stats->lonelf && !new_stats->lonelf ) { |
21e5ad50 | 209 | /* |
a0ad53c1 | 210 | * CRLFs would be added by checkout |
21e5ad50 | 211 | */ |
8462ff43 TB |
212 | if (conv_flags & CONV_EOL_RNDTRP_DIE) |
213 | die(_("LF would be replaced by CRLF in %s"), path); | |
214 | else if (conv_flags & CONV_EOL_RNDTRP_WARN) | |
c970d30c AH |
215 | warning(_("in the working copy of '%s', LF will be" |
216 | " replaced by CRLF the next time Git touches" | |
217 | " it"), path); | |
21e5ad50 SP |
218 | } |
219 | } | |
220 | ||
847a9e5d | 221 | static int has_crlf_in_index(struct index_state *istate, const char *path) |
c4805393 | 222 | { |
c4805393 | 223 | unsigned long sz; |
c4805393 | 224 | void *data; |
86ff70a0 TB |
225 | const char *crp; |
226 | int has_crlf = 0; | |
c4805393 | 227 | |
49a6d31f | 228 | data = read_blob_data_from_index(istate, path, &sz); |
4982fd78 | 229 | if (!data) |
c4805393 | 230 | return 0; |
86ff70a0 TB |
231 | |
232 | crp = memchr(data, '\r', sz); | |
233 | if (crp) { | |
234 | unsigned int ret_stats; | |
235 | ret_stats = gather_convert_stats(data, sz); | |
236 | if (!(ret_stats & CONVERT_STAT_BITS_BIN) && | |
237 | (ret_stats & CONVERT_STAT_BITS_TXT_CRLF)) | |
238 | has_crlf = 1; | |
239 | } | |
c4805393 | 240 | free(data); |
86ff70a0 | 241 | return has_crlf; |
c4805393 FAG |
242 | } |
243 | ||
129beeee | 244 | static int will_convert_lf_to_crlf(struct text_stat *stats, |
38e95844 | 245 | enum convert_crlf_action crlf_action) |
a0ad53c1 TB |
246 | { |
247 | if (output_eol(crlf_action) != EOL_CRLF) | |
248 | return 0; | |
249 | /* No "naked" LF? Nothing to convert, regardless. */ | |
250 | if (!stats->lonelf) | |
251 | return 0; | |
252 | ||
253 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { | |
254 | /* If we have any CR or CRLF line endings, we do not touch it */ | |
255 | /* This is the new safer autocrlf-handling */ | |
256 | if (stats->lonecr || stats->crlf) | |
257 | return 0; | |
258 | ||
129beeee | 259 | if (convert_is_binary(stats)) |
a0ad53c1 TB |
260 | return 0; |
261 | } | |
262 | return 1; | |
263 | ||
264 | } | |
265 | ||
7a17918c LS |
266 | static int validate_encoding(const char *path, const char *enc, |
267 | const char *data, size_t len, int die_on_error) | |
268 | { | |
ed283588 RS |
269 | const char *stripped; |
270 | ||
7a17918c | 271 | /* We only check for UTF here as UTF?? can be an alias for UTF-?? */ |
ed283588 RS |
272 | if (skip_iprefix(enc, "UTF", &stripped)) { |
273 | skip_prefix(stripped, "-", &stripped); | |
274 | ||
7a17918c LS |
275 | /* |
276 | * Check for detectable errors in UTF encodings | |
277 | */ | |
278 | if (has_prohibited_utf_bom(enc, data, len)) { | |
279 | const char *error_msg = _( | |
280 | "BOM is prohibited in '%s' if encoded as %s"); | |
281 | /* | |
282 | * This advice is shown for UTF-??BE and UTF-??LE encodings. | |
283 | * We cut off the last two characters of the encoding name | |
284 | * to generate the encoding name suitable for BOMs. | |
285 | */ | |
286 | const char *advise_msg = _( | |
287 | "The file '%s' contains a byte order " | |
ed283588 | 288 | "mark (BOM). Please use UTF-%.*s as " |
7a17918c | 289 | "working-tree-encoding."); |
ed283588 RS |
290 | int stripped_len = strlen(stripped) - strlen("BE"); |
291 | advise(advise_msg, path, stripped_len, stripped); | |
7a17918c LS |
292 | if (die_on_error) |
293 | die(error_msg, path, enc); | |
294 | else { | |
295 | return error(error_msg, path, enc); | |
296 | } | |
297 | ||
298 | } else if (is_missing_required_utf_bom(enc, data, len)) { | |
299 | const char *error_msg = _( | |
300 | "BOM is required in '%s' if encoded as %s"); | |
301 | const char *advise_msg = _( | |
302 | "The file '%s' is missing a byte order " | |
303 | "mark (BOM). Please use UTF-%sBE or UTF-%sLE " | |
304 | "(depending on the byte order) as " | |
305 | "working-tree-encoding."); | |
7a17918c | 306 | advise(advise_msg, path, stripped, stripped); |
7a17918c LS |
307 | if (die_on_error) |
308 | die(error_msg, path, enc); | |
309 | else { | |
310 | return error(error_msg, path, enc); | |
311 | } | |
312 | } | |
313 | ||
314 | } | |
315 | return 0; | |
316 | } | |
317 | ||
541d059c LS |
318 | static void trace_encoding(const char *context, const char *path, |
319 | const char *encoding, const char *buf, size_t len) | |
320 | { | |
321 | static struct trace_key coe = TRACE_KEY_INIT(WORKING_TREE_ENCODING); | |
322 | struct strbuf trace = STRBUF_INIT; | |
323 | int i; | |
324 | ||
325 | strbuf_addf(&trace, "%s (%s, considered %s):\n", context, path, encoding); | |
326 | for (i = 0; i < len && buf; ++i) { | |
327 | strbuf_addf( | |
8302f50e | 328 | &trace, "| \033[2m%2i:\033[0m %2x \033[2m%c\033[0m%c", |
541d059c LS |
329 | i, |
330 | (unsigned char) buf[i], | |
331 | (buf[i] > 32 && buf[i] < 127 ? buf[i] : ' '), | |
332 | ((i+1) % 8 && (i+1) < len ? ' ' : '\n') | |
333 | ); | |
334 | } | |
335 | strbuf_addchars(&trace, '\n', 1); | |
336 | ||
337 | trace_strbuf(&coe, &trace); | |
338 | strbuf_release(&trace); | |
339 | } | |
340 | ||
e92d6225 LS |
341 | static int check_roundtrip(const char *enc_name) |
342 | { | |
343 | /* | |
344 | * check_roundtrip_encoding contains a string of comma and/or | |
345 | * space separated encodings (eg. "UTF-16, ASCII, CP1125"). | |
346 | * Search for the given encoding in that string. | |
347 | */ | |
348 | const char *found = strcasestr(check_roundtrip_encoding, enc_name); | |
349 | const char *next; | |
350 | int len; | |
351 | if (!found) | |
352 | return 0; | |
353 | next = found + strlen(enc_name); | |
354 | len = strlen(check_roundtrip_encoding); | |
355 | return (found && ( | |
356 | /* | |
357 | * check that the found encoding is at the | |
358 | * beginning of check_roundtrip_encoding or | |
359 | * that it is prefixed with a space or comma | |
360 | */ | |
361 | found == check_roundtrip_encoding || ( | |
362 | (isspace(found[-1]) || found[-1] == ',') | |
363 | ) | |
364 | ) && ( | |
365 | /* | |
366 | * check that the found encoding is at the | |
367 | * end of check_roundtrip_encoding or | |
368 | * that it is suffixed with a space or comma | |
369 | */ | |
370 | next == check_roundtrip_encoding + len || ( | |
371 | next < check_roundtrip_encoding + len && | |
372 | (isspace(next[0]) || next[0] == ',') | |
373 | ) | |
374 | )); | |
375 | } | |
376 | ||
107642fe LS |
377 | static const char *default_encoding = "UTF-8"; |
378 | ||
379 | static int encode_to_git(const char *path, const char *src, size_t src_len, | |
380 | struct strbuf *buf, const char *enc, int conv_flags) | |
381 | { | |
382 | char *dst; | |
c7d017d7 | 383 | size_t dst_len; |
107642fe LS |
384 | int die_on_error = conv_flags & CONV_WRITE_OBJECT; |
385 | ||
386 | /* | |
387 | * No encoding is specified or there is nothing to encode. | |
388 | * Tell the caller that the content was not modified. | |
389 | */ | |
390 | if (!enc || (src && !src_len)) | |
391 | return 0; | |
392 | ||
393 | /* | |
394 | * Looks like we got called from "would_convert_to_git()". | |
395 | * This means Git wants to know if it would encode (= modify!) | |
396 | * the content. Let's answer with "yes", since an encoding was | |
397 | * specified. | |
398 | */ | |
399 | if (!buf && !src) | |
400 | return 1; | |
401 | ||
7a17918c LS |
402 | if (validate_encoding(path, enc, src, src_len, die_on_error)) |
403 | return 0; | |
404 | ||
541d059c | 405 | trace_encoding("source", path, enc, src, src_len); |
107642fe LS |
406 | dst = reencode_string_len(src, src_len, default_encoding, enc, |
407 | &dst_len); | |
408 | if (!dst) { | |
409 | /* | |
410 | * We could add the blob "as-is" to Git. However, on checkout | |
15beaaa3 | 411 | * we would try to re-encode to the original encoding. This |
107642fe LS |
412 | * would fail and we would leave the user with a messed-up |
413 | * working tree. Let's try to avoid this by screaming loud. | |
414 | */ | |
415 | const char* msg = _("failed to encode '%s' from %s to %s"); | |
416 | if (die_on_error) | |
417 | die(msg, path, enc, default_encoding); | |
418 | else { | |
419 | error(msg, path, enc, default_encoding); | |
420 | return 0; | |
421 | } | |
422 | } | |
541d059c | 423 | trace_encoding("destination", path, default_encoding, dst, dst_len); |
107642fe | 424 | |
e92d6225 LS |
425 | /* |
426 | * UTF supports lossless conversion round tripping [1] and conversions | |
427 | * between UTF and other encodings are mostly round trip safe as | |
428 | * Unicode aims to be a superset of all other character encodings. | |
429 | * However, certain encodings (e.g. SHIFT-JIS) are known to have round | |
430 | * trip issues [2]. Check the round trip conversion for all encodings | |
431 | * listed in core.checkRoundtripEncoding. | |
432 | * | |
433 | * The round trip check is only performed if content is written to Git. | |
434 | * This ensures that no information is lost during conversion to/from | |
435 | * the internal UTF-8 representation. | |
436 | * | |
437 | * Please note, the code below is not tested because I was not able to | |
438 | * generate a faulty round trip without an iconv error. Iconv errors | |
439 | * are already caught above. | |
440 | * | |
441 | * [1] http://unicode.org/faq/utf_bom.html#gen2 | |
442 | * [2] https://support.microsoft.com/en-us/help/170559/prb-conversion-problem-between-shift-jis-and-unicode | |
443 | */ | |
444 | if (die_on_error && check_roundtrip(enc)) { | |
445 | char *re_src; | |
c7d017d7 | 446 | size_t re_src_len; |
e92d6225 LS |
447 | |
448 | re_src = reencode_string_len(dst, dst_len, | |
449 | enc, default_encoding, | |
450 | &re_src_len); | |
451 | ||
452 | trace_printf("Checking roundtrip encoding for %s...\n", enc); | |
453 | trace_encoding("reencoded source", path, enc, | |
454 | re_src, re_src_len); | |
455 | ||
456 | if (!re_src || src_len != re_src_len || | |
457 | memcmp(src, re_src, src_len)) { | |
458 | const char* msg = _("encoding '%s' from %s to %s and " | |
459 | "back is not the same"); | |
460 | die(msg, path, enc, default_encoding); | |
461 | } | |
462 | ||
463 | free(re_src); | |
464 | } | |
465 | ||
107642fe LS |
466 | strbuf_attach(buf, dst, dst_len, dst_len + 1); |
467 | return 1; | |
468 | } | |
469 | ||
470 | static int encode_to_worktree(const char *path, const char *src, size_t src_len, | |
471 | struct strbuf *buf, const char *enc) | |
472 | { | |
473 | char *dst; | |
c7d017d7 | 474 | size_t dst_len; |
107642fe LS |
475 | |
476 | /* | |
477 | * No encoding is specified or there is nothing to encode. | |
478 | * Tell the caller that the content was not modified. | |
479 | */ | |
480 | if (!enc || (src && !src_len)) | |
481 | return 0; | |
482 | ||
483 | dst = reencode_string_len(src, src_len, enc, default_encoding, | |
484 | &dst_len); | |
485 | if (!dst) { | |
d26a328e | 486 | error(_("failed to encode '%s' from %s to %s"), |
1a07e59c | 487 | path, default_encoding, enc); |
107642fe LS |
488 | return 0; |
489 | } | |
490 | ||
491 | strbuf_attach(buf, dst, dst_len, dst_len + 1); | |
492 | return 1; | |
493 | } | |
494 | ||
847a9e5d | 495 | static int crlf_to_git(struct index_state *istate, |
49a6d31f | 496 | const char *path, const char *src, size_t len, |
c61dcff9 | 497 | struct strbuf *buf, |
38e95844 | 498 | enum convert_crlf_action crlf_action, int conv_flags) |
6c510bee | 499 | { |
6c510bee | 500 | struct text_stat stats; |
5ecd293d | 501 | char *dst; |
a0ad53c1 | 502 | int convert_crlf_into_lf; |
6c510bee | 503 | |
c61dcff9 | 504 | if (crlf_action == CRLF_BINARY || |
4c3b57b9 | 505 | (src && !len)) |
5ecd293d | 506 | return 0; |
6c510bee | 507 | |
4c3b57b9 JK |
508 | /* |
509 | * If we are doing a dry-run and have no source buffer, there is | |
510 | * nothing to analyze; we must assume we would convert. | |
511 | */ | |
512 | if (!buf && !src) | |
513 | return 1; | |
514 | ||
5ecd293d | 515 | gather_stats(src, len, &stats); |
a0ad53c1 TB |
516 | /* Optimization: No CRLF? Nothing to convert, regardless. */ |
517 | convert_crlf_into_lf = !!stats.crlf; | |
6c510bee | 518 | |
df747b81 | 519 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
129beeee | 520 | if (convert_is_binary(&stats)) |
5ecd293d | 521 | return 0; |
65237284 | 522 | /* |
1c25d2d8 TB |
523 | * If the file in the index has any CR in it, do not |
524 | * convert. This is the new safer autocrlf handling, | |
525 | * unless we want to renormalize in a merge or | |
526 | * cherry-pick. | |
65237284 | 527 | */ |
8462ff43 | 528 | if ((!(conv_flags & CONV_EOL_RENORMALIZE)) && |
86ff70a0 | 529 | has_crlf_in_index(istate, path)) |
a0ad53c1 | 530 | convert_crlf_into_lf = 0; |
201ac8ef | 531 | } |
8462ff43 TB |
532 | if (((conv_flags & CONV_EOL_RNDTRP_WARN) || |
533 | ((conv_flags & CONV_EOL_RNDTRP_DIE) && len))) { | |
a0ad53c1 TB |
534 | struct text_stat new_stats; |
535 | memcpy(&new_stats, &stats, sizeof(new_stats)); | |
536 | /* simulate "git add" */ | |
537 | if (convert_crlf_into_lf) { | |
538 | new_stats.lonelf += new_stats.crlf; | |
539 | new_stats.crlf = 0; | |
540 | } | |
541 | /* simulate "git checkout" */ | |
129beeee | 542 | if (will_convert_lf_to_crlf(&new_stats, crlf_action)) { |
a0ad53c1 TB |
543 | new_stats.crlf += new_stats.lonelf; |
544 | new_stats.lonelf = 0; | |
545 | } | |
185e8652 | 546 | check_global_conv_flags_eol(path, &stats, &new_stats, conv_flags); |
a0ad53c1 TB |
547 | } |
548 | if (!convert_crlf_into_lf) | |
21e5ad50 SP |
549 | return 0; |
550 | ||
92ac3197 JK |
551 | /* |
552 | * At this point all of our source analysis is done, and we are sure we | |
553 | * would convert. If we are in dry-run mode, we can give an answer. | |
554 | */ | |
555 | if (!buf) | |
556 | return 1; | |
557 | ||
90d16ec0 PH |
558 | /* only grow if not in place */ |
559 | if (strbuf_avail(buf) + buf->len < len) | |
560 | strbuf_grow(buf, len - buf->len); | |
5ecd293d | 561 | dst = buf->buf; |
df747b81 | 562 | if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) { |
163b9591 JH |
563 | /* |
564 | * If we guessed, we already know we rejected a file with | |
565 | * lone CR, and we can strip a CR without looking at what | |
566 | * follow it. | |
567 | */ | |
201ac8ef | 568 | do { |
ac78e548 | 569 | unsigned char c = *src++; |
201ac8ef | 570 | if (c != '\r') |
ac78e548 | 571 | *dst++ = c; |
5ecd293d | 572 | } while (--len); |
201ac8ef JH |
573 | } else { |
574 | do { | |
ac78e548 | 575 | unsigned char c = *src++; |
5ecd293d | 576 | if (! (c == '\r' && (1 < len && *src == '\n'))) |
ac78e548 | 577 | *dst++ = c; |
5ecd293d | 578 | } while (--len); |
201ac8ef | 579 | } |
5ecd293d PH |
580 | strbuf_setlen(buf, dst - buf->buf); |
581 | return 1; | |
6c510bee LT |
582 | } |
583 | ||
38e95844 JH |
584 | static int crlf_to_worktree(const char *src, size_t len, struct strbuf *buf, |
585 | enum convert_crlf_action crlf_action) | |
6c510bee | 586 | { |
5ecd293d | 587 | char *to_free = NULL; |
6c510bee | 588 | struct text_stat stats; |
6c510bee | 589 | |
c61dcff9 | 590 | if (!len || output_eol(crlf_action) != EOL_CRLF) |
5ecd293d | 591 | return 0; |
6c510bee | 592 | |
5ecd293d | 593 | gather_stats(src, len, &stats); |
129beeee | 594 | if (!will_convert_lf_to_crlf(&stats, crlf_action)) |
5ecd293d | 595 | return 0; |
6c510bee | 596 | |
5ecd293d PH |
597 | /* are we "faking" in place editing ? */ |
598 | if (src == buf->buf) | |
b315c5c0 | 599 | to_free = strbuf_detach(buf, NULL); |
5ecd293d | 600 | |
6e336a53 | 601 | strbuf_grow(buf, len + stats.lonelf); |
5ecd293d PH |
602 | for (;;) { |
603 | const char *nl = memchr(src, '\n', len); | |
604 | if (!nl) | |
605 | break; | |
606 | if (nl > src && nl[-1] == '\r') { | |
607 | strbuf_add(buf, src, nl + 1 - src); | |
608 | } else { | |
609 | strbuf_add(buf, src, nl - src); | |
610 | strbuf_addstr(buf, "\r\n"); | |
611 | } | |
612 | len -= nl + 1 - src; | |
613 | src = nl + 1; | |
614 | } | |
615 | strbuf_add(buf, src, len); | |
616 | ||
617 | free(to_free); | |
618 | return 1; | |
6c510bee | 619 | } |
35ebfd6a | 620 | |
546bb582 JS |
621 | struct filter_params { |
622 | const char *src; | |
596b5e77 | 623 | size_t size; |
9035d75a | 624 | int fd; |
546bb582 | 625 | const char *cmd; |
a2b665de | 626 | const char *path; |
546bb582 JS |
627 | }; |
628 | ||
5cf88fd8 | 629 | static int filter_buffer_or_fd(int in UNUSED, int out, void *data) |
aa4ed402 JH |
630 | { |
631 | /* | |
632 | * Spawn cmd and feed the buffer contents through its stdin. | |
633 | */ | |
d3180279 | 634 | struct child_process child_process = CHILD_PROCESS_INIT; |
546bb582 | 635 | struct filter_params *params = (struct filter_params *)data; |
39dbd49b | 636 | const char *format = params->cmd; |
aa4ed402 | 637 | int write_err, status; |
66dbfd55 | 638 | |
a2b665de PW |
639 | /* apply % substitution to cmd */ |
640 | struct strbuf cmd = STRBUF_INIT; | |
39dbd49b RS |
641 | |
642 | /* expand all %f with the quoted path; quote to preserve space, etc. */ | |
643 | while (strbuf_expand_step(&cmd, &format)) { | |
644 | if (skip_prefix(format, "%", &format)) | |
645 | strbuf_addch(&cmd, '%'); | |
646 | else if (skip_prefix(format, "f", &format)) | |
647 | sq_quote_buf(&cmd, params->path); | |
648 | else | |
649 | strbuf_addch(&cmd, '%'); | |
650 | } | |
a2b665de | 651 | |
afbdba39 | 652 | strvec_push(&child_process.args, cmd.buf); |
ac0ba18d | 653 | child_process.use_shell = 1; |
dc1bfdcd | 654 | child_process.in = -1; |
ae6a5609 | 655 | child_process.out = out; |
aa4ed402 | 656 | |
f31f1d39 RS |
657 | if (start_command(&child_process)) { |
658 | strbuf_release(&cmd); | |
d26a328e NTND |
659 | return error(_("cannot fork to run external filter '%s'"), |
660 | params->cmd); | |
f31f1d39 | 661 | } |
aa4ed402 | 662 | |
6424c2ad JB |
663 | sigchain_push(SIGPIPE, SIG_IGN); |
664 | ||
9035d75a | 665 | if (params->src) { |
0c4dd67a JH |
666 | write_err = (write_in_full(child_process.in, |
667 | params->src, params->size) < 0); | |
668 | if (errno == EPIPE) | |
669 | write_err = 0; | |
9035d75a SP |
670 | } else { |
671 | write_err = copy_fd(params->fd, child_process.in); | |
0c4dd67a JH |
672 | if (write_err == COPY_WRITE_ERROR && errno == EPIPE) |
673 | write_err = 0; | |
9035d75a SP |
674 | } |
675 | ||
dc1bfdcd | 676 | if (close(child_process.in)) |
aa4ed402 JH |
677 | write_err = 1; |
678 | if (write_err) | |
d26a328e NTND |
679 | error(_("cannot feed the input to external filter '%s'"), |
680 | params->cmd); | |
aa4ed402 | 681 | |
6424c2ad JB |
682 | sigchain_pop(SIGPIPE); |
683 | ||
aa4ed402 JH |
684 | status = finish_command(&child_process); |
685 | if (status) | |
d26a328e | 686 | error(_("external filter '%s' failed %d"), params->cmd, status); |
a2b665de PW |
687 | |
688 | strbuf_release(&cmd); | |
aa4ed402 JH |
689 | return (write_err || status); |
690 | } | |
691 | ||
234fa07e | 692 | static int apply_single_file_filter(const char *path, const char *src, size_t len, int fd, |
ec36c42a | 693 | struct strbuf *dst, const char *cmd) |
aa4ed402 JH |
694 | { |
695 | /* | |
696 | * Create a pipeline to have the command filter the buffer's | |
697 | * contents. | |
698 | * | |
699 | * (child --> cmd) --> us | |
700 | */ | |
b84be553 | 701 | int err = 0; |
f285a2d7 | 702 | struct strbuf nbuf = STRBUF_INIT; |
546bb582 JS |
703 | struct async async; |
704 | struct filter_params params; | |
aa4ed402 | 705 | |
546bb582 | 706 | memset(&async, 0, sizeof(async)); |
9035d75a | 707 | async.proc = filter_buffer_or_fd; |
546bb582 | 708 | async.data = ¶ms; |
ae6a5609 | 709 | async.out = -1; |
546bb582 JS |
710 | params.src = src; |
711 | params.size = len; | |
9035d75a | 712 | params.fd = fd; |
546bb582 | 713 | params.cmd = cmd; |
a2b665de | 714 | params.path = path; |
aa4ed402 JH |
715 | |
716 | fflush(NULL); | |
546bb582 JS |
717 | if (start_async(&async)) |
718 | return 0; /* error was already reported */ | |
aa4ed402 | 719 | |
02156ab0 | 720 | if (strbuf_read(&nbuf, async.out, 0) < 0) { |
d26a328e | 721 | err = error(_("read from external filter '%s' failed"), cmd); |
aa4ed402 | 722 | } |
546bb582 | 723 | if (close(async.out)) { |
d26a328e | 724 | err = error(_("read from external filter '%s' failed"), cmd); |
aa4ed402 | 725 | } |
546bb582 | 726 | if (finish_async(&async)) { |
d26a328e | 727 | err = error(_("external filter '%s' failed"), cmd); |
aa4ed402 JH |
728 | } |
729 | ||
b84be553 | 730 | if (!err) { |
90d16ec0 | 731 | strbuf_swap(dst, &nbuf); |
5ecd293d | 732 | } |
90d16ec0 | 733 | strbuf_release(&nbuf); |
b84be553 | 734 | return !err; |
aa4ed402 JH |
735 | } |
736 | ||
234fa07e LS |
737 | #define CAP_CLEAN (1u<<0) |
738 | #define CAP_SMUDGE (1u<<1) | |
2841e8f8 | 739 | #define CAP_DELAY (1u<<2) |
234fa07e | 740 | |
1b0b46ee BP |
741 | struct cmd2process { |
742 | struct subprocess_entry subprocess; /* must be the first member! */ | |
743 | unsigned int supported_capabilities; | |
744 | }; | |
745 | ||
f514d7d1 BP |
746 | static int subprocess_map_initialized; |
747 | static struct hashmap subprocess_map; | |
edcc8581 | 748 | |
7ddb9b2c | 749 | static int start_multi_file_filter_fn(struct subprocess_entry *subprocess) |
edcc8581 | 750 | { |
fa64a2fd JT |
751 | static int versions[] = {2, 0}; |
752 | static struct subprocess_capability capabilities[] = { | |
1514c8ed LS |
753 | { "clean", CAP_CLEAN }, |
754 | { "smudge", CAP_SMUDGE }, | |
2841e8f8 | 755 | { "delay", CAP_DELAY }, |
fa64a2fd | 756 | { NULL, 0 } |
1514c8ed | 757 | }; |
fa64a2fd JT |
758 | struct cmd2process *entry = (struct cmd2process *)subprocess; |
759 | return subprocess_handshake(subprocess, "git-filter", versions, NULL, | |
760 | capabilities, | |
761 | &entry->supported_capabilities); | |
a810ea99 BP |
762 | } |
763 | ||
9364fc29 LS |
764 | static void handle_filter_error(const struct strbuf *filter_status, |
765 | struct cmd2process *entry, | |
3b335762 NTND |
766 | const unsigned int wanted_capability) |
767 | { | |
9364fc29 LS |
768 | if (!strcmp(filter_status->buf, "error")) |
769 | ; /* The filter signaled a problem with the file. */ | |
770 | else if (!strcmp(filter_status->buf, "abort") && wanted_capability) { | |
771 | /* | |
772 | * The filter signaled a permanent problem. Don't try to filter | |
773 | * files with the same command for the lifetime of the current | |
774 | * Git process. | |
775 | */ | |
776 | entry->supported_capabilities &= ~wanted_capability; | |
777 | } else { | |
778 | /* | |
779 | * Something went wrong with the protocol filter. | |
780 | * Force shutdown and restart if another blob requires filtering. | |
781 | */ | |
d26a328e | 782 | error(_("external filter '%s' failed"), entry->subprocess.cmd); |
9364fc29 LS |
783 | subprocess_stop(&subprocess_map, &entry->subprocess); |
784 | free(entry); | |
785 | } | |
786 | } | |
787 | ||
edcc8581 LS |
788 | static int apply_multi_file_filter(const char *path, const char *src, size_t len, |
789 | int fd, struct strbuf *dst, const char *cmd, | |
2841e8f8 | 790 | const unsigned int wanted_capability, |
ab90ecae | 791 | const struct checkout_metadata *meta, |
2841e8f8 | 792 | struct delayed_checkout *dco) |
edcc8581 LS |
793 | { |
794 | int err; | |
2841e8f8 | 795 | int can_delay = 0; |
edcc8581 LS |
796 | struct cmd2process *entry; |
797 | struct child_process *process; | |
798 | struct strbuf nbuf = STRBUF_INIT; | |
799 | struct strbuf filter_status = STRBUF_INIT; | |
800 | const char *filter_type; | |
801 | ||
f514d7d1 BP |
802 | if (!subprocess_map_initialized) { |
803 | subprocess_map_initialized = 1; | |
9ab42958 | 804 | hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0); |
edcc8581 LS |
805 | entry = NULL; |
806 | } else { | |
f514d7d1 | 807 | entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd); |
edcc8581 LS |
808 | } |
809 | ||
810 | fflush(NULL); | |
811 | ||
812 | if (!entry) { | |
7ddb9b2c BP |
813 | entry = xmalloc(sizeof(*entry)); |
814 | entry->supported_capabilities = 0; | |
815 | ||
f514d7d1 | 816 | if (subprocess_start(&subprocess_map, &entry->subprocess, cmd, start_multi_file_filter_fn)) { |
7ddb9b2c | 817 | free(entry); |
edcc8581 | 818 | return 0; |
7ddb9b2c | 819 | } |
edcc8581 | 820 | } |
1b0b46ee | 821 | process = &entry->subprocess.process; |
edcc8581 | 822 | |
42b0a86c | 823 | if (!(entry->supported_capabilities & wanted_capability)) |
edcc8581 LS |
824 | return 0; |
825 | ||
42b0a86c | 826 | if (wanted_capability & CAP_CLEAN) |
edcc8581 | 827 | filter_type = "clean"; |
42b0a86c | 828 | else if (wanted_capability & CAP_SMUDGE) |
edcc8581 LS |
829 | filter_type = "smudge"; |
830 | else | |
d26a328e | 831 | die(_("unexpected filter type")); |
edcc8581 LS |
832 | |
833 | sigchain_push(SIGPIPE, SIG_IGN); | |
834 | ||
835 | assert(strlen(filter_type) < LARGE_PACKET_DATA_MAX - strlen("command=\n")); | |
836 | err = packet_write_fmt_gently(process->in, "command=%s\n", filter_type); | |
837 | if (err) | |
838 | goto done; | |
839 | ||
840 | err = strlen(path) > LARGE_PACKET_DATA_MAX - strlen("pathname=\n"); | |
841 | if (err) { | |
d26a328e | 842 | error(_("path name too long for external filter")); |
edcc8581 LS |
843 | goto done; |
844 | } | |
845 | ||
846 | err = packet_write_fmt_gently(process->in, "pathname=%s\n", path); | |
847 | if (err) | |
848 | goto done; | |
849 | ||
ab90ecae | 850 | if (meta && meta->refname) { |
851 | err = packet_write_fmt_gently(process->in, "ref=%s\n", meta->refname); | |
852 | if (err) | |
853 | goto done; | |
854 | } | |
855 | ||
856 | if (meta && !is_null_oid(&meta->treeish)) { | |
857 | err = packet_write_fmt_gently(process->in, "treeish=%s\n", oid_to_hex(&meta->treeish)); | |
858 | if (err) | |
859 | goto done; | |
860 | } | |
861 | ||
862 | if (meta && !is_null_oid(&meta->blob)) { | |
863 | err = packet_write_fmt_gently(process->in, "blob=%s\n", oid_to_hex(&meta->blob)); | |
864 | if (err) | |
865 | goto done; | |
866 | } | |
867 | ||
2841e8f8 LS |
868 | if ((entry->supported_capabilities & CAP_DELAY) && |
869 | dco && dco->state == CE_CAN_DELAY) { | |
870 | can_delay = 1; | |
871 | err = packet_write_fmt_gently(process->in, "can-delay=1\n"); | |
872 | if (err) | |
873 | goto done; | |
874 | } | |
875 | ||
edcc8581 LS |
876 | err = packet_flush_gently(process->in); |
877 | if (err) | |
878 | goto done; | |
879 | ||
880 | if (fd >= 0) | |
3a63c6a4 | 881 | err = write_packetized_from_fd_no_flush(fd, process->in); |
edcc8581 | 882 | else |
3a63c6a4 JS |
883 | err = write_packetized_from_buf_no_flush(src, len, process->in); |
884 | if (err) | |
885 | goto done; | |
886 | ||
887 | err = packet_flush_gently(process->in); | |
edcc8581 LS |
888 | if (err) |
889 | goto done; | |
890 | ||
4f2a2e9f BP |
891 | err = subprocess_read_status(process->out, &filter_status); |
892 | if (err) | |
893 | goto done; | |
894 | ||
2841e8f8 LS |
895 | if (can_delay && !strcmp(filter_status.buf, "delayed")) { |
896 | string_list_insert(&dco->filters, cmd); | |
897 | string_list_insert(&dco->paths, path); | |
898 | } else { | |
899 | /* The filter got the blob and wants to send us a response. */ | |
900 | err = strcmp(filter_status.buf, "success"); | |
901 | if (err) | |
902 | goto done; | |
903 | ||
8c2efa5d JS |
904 | err = read_packetized_to_strbuf(process->out, &nbuf, |
905 | PACKET_READ_GENTLE_ON_EOF) < 0; | |
2841e8f8 LS |
906 | if (err) |
907 | goto done; | |
908 | ||
909 | err = subprocess_read_status(process->out, &filter_status); | |
910 | if (err) | |
911 | goto done; | |
912 | ||
913 | err = strcmp(filter_status.buf, "success"); | |
914 | } | |
915 | ||
916 | done: | |
917 | sigchain_pop(SIGPIPE); | |
918 | ||
919 | if (err) | |
920 | handle_filter_error(&filter_status, entry, wanted_capability); | |
921 | else | |
922 | strbuf_swap(dst, &nbuf); | |
923 | strbuf_release(&nbuf); | |
675ea4ef | 924 | strbuf_release(&filter_status); |
2841e8f8 LS |
925 | return !err; |
926 | } | |
927 | ||
928 | ||
929 | int async_query_available_blobs(const char *cmd, struct string_list *available_paths) | |
930 | { | |
931 | int err; | |
932 | char *line; | |
933 | struct cmd2process *entry; | |
934 | struct child_process *process; | |
935 | struct strbuf filter_status = STRBUF_INIT; | |
936 | ||
937 | assert(subprocess_map_initialized); | |
938 | entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd); | |
939 | if (!entry) { | |
d26a328e NTND |
940 | error(_("external filter '%s' is not available anymore although " |
941 | "not all paths have been filtered"), cmd); | |
2841e8f8 LS |
942 | return 0; |
943 | } | |
944 | process = &entry->subprocess.process; | |
945 | sigchain_push(SIGPIPE, SIG_IGN); | |
946 | ||
947 | err = packet_write_fmt_gently( | |
948 | process->in, "command=list_available_blobs\n"); | |
edcc8581 LS |
949 | if (err) |
950 | goto done; | |
951 | ||
2841e8f8 | 952 | err = packet_flush_gently(process->in); |
edcc8581 LS |
953 | if (err) |
954 | goto done; | |
955 | ||
2841e8f8 LS |
956 | while ((line = packet_read_line(process->out, NULL))) { |
957 | const char *path; | |
958 | if (skip_prefix(line, "pathname=", &path)) | |
959 | string_list_insert(available_paths, xstrdup(path)); | |
960 | else | |
961 | ; /* ignore unknown keys */ | |
962 | } | |
963 | ||
4f2a2e9f BP |
964 | err = subprocess_read_status(process->out, &filter_status); |
965 | if (err) | |
966 | goto done; | |
967 | ||
edcc8581 LS |
968 | err = strcmp(filter_status.buf, "success"); |
969 | ||
970 | done: | |
971 | sigchain_pop(SIGPIPE); | |
972 | ||
9364fc29 | 973 | if (err) |
2841e8f8 | 974 | handle_filter_error(&filter_status, entry, 0); |
675ea4ef | 975 | strbuf_release(&filter_status); |
edcc8581 | 976 | return !err; |
aa4ed402 JH |
977 | } |
978 | ||
979 | static struct convert_driver { | |
980 | const char *name; | |
981 | struct convert_driver *next; | |
cd8be6c9 BH |
982 | const char *smudge; |
983 | const char *clean; | |
edcc8581 | 984 | const char *process; |
36daaaca | 985 | int required; |
aa4ed402 JH |
986 | } *user_convert, **user_convert_tail; |
987 | ||
234fa07e LS |
988 | static int apply_filter(const char *path, const char *src, size_t len, |
989 | int fd, struct strbuf *dst, struct convert_driver *drv, | |
2841e8f8 | 990 | const unsigned int wanted_capability, |
ab90ecae | 991 | const struct checkout_metadata *meta, |
2841e8f8 | 992 | struct delayed_checkout *dco) |
234fa07e LS |
993 | { |
994 | const char *cmd = NULL; | |
995 | ||
996 | if (!drv) | |
997 | return 0; | |
998 | ||
999 | if (!dst) | |
1000 | return 1; | |
1001 | ||
42b0a86c | 1002 | if ((wanted_capability & CAP_CLEAN) && !drv->process && drv->clean) |
234fa07e | 1003 | cmd = drv->clean; |
42b0a86c | 1004 | else if ((wanted_capability & CAP_SMUDGE) && !drv->process && drv->smudge) |
234fa07e LS |
1005 | cmd = drv->smudge; |
1006 | ||
1007 | if (cmd && *cmd) | |
1008 | return apply_single_file_filter(path, src, len, fd, dst, cmd); | |
edcc8581 | 1009 | else if (drv->process && *drv->process) |
2841e8f8 | 1010 | return apply_multi_file_filter(path, src, len, fd, dst, |
ab90ecae | 1011 | drv->process, wanted_capability, meta, dco); |
234fa07e LS |
1012 | |
1013 | return 0; | |
1014 | } | |
1015 | ||
a4e7e317 GC |
1016 | static int read_convert_config(const char *var, const char *value, |
1017 | const struct config_context *ctx UNUSED, | |
1018 | void *cb UNUSED) | |
aa4ed402 | 1019 | { |
d731f0ad | 1020 | const char *key, *name; |
f5914f4b | 1021 | size_t namelen; |
aa4ed402 JH |
1022 | struct convert_driver *drv; |
1023 | ||
1024 | /* | |
1025 | * External conversion drivers are configured using | |
1026 | * "filter.<name>.variable". | |
1027 | */ | |
d731f0ad | 1028 | if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name) |
aa4ed402 | 1029 | return 0; |
aa4ed402 JH |
1030 | for (drv = user_convert; drv; drv = drv->next) |
1031 | if (!strncmp(drv->name, name, namelen) && !drv->name[namelen]) | |
1032 | break; | |
1033 | if (!drv) { | |
ca56dadb | 1034 | CALLOC_ARRAY(drv, 1); |
182af834 | 1035 | drv->name = xmemdupz(name, namelen); |
aa4ed402 JH |
1036 | *user_convert_tail = drv; |
1037 | user_convert_tail = &(drv->next); | |
1038 | } | |
1039 | ||
aa4ed402 JH |
1040 | /* |
1041 | * filter.<name>.smudge and filter.<name>.clean specifies | |
1042 | * the command line: | |
1043 | * | |
1044 | * command-line | |
1045 | * | |
1046 | * The command-line will not be interpolated in any way. | |
1047 | */ | |
1048 | ||
d731f0ad | 1049 | if (!strcmp("smudge", key)) |
cd8be6c9 BH |
1050 | return git_config_string(&drv->smudge, var, value); |
1051 | ||
d731f0ad | 1052 | if (!strcmp("clean", key)) |
cd8be6c9 | 1053 | return git_config_string(&drv->clean, var, value); |
aa4ed402 | 1054 | |
edcc8581 LS |
1055 | if (!strcmp("process", key)) |
1056 | return git_config_string(&drv->process, var, value); | |
1057 | ||
d731f0ad | 1058 | if (!strcmp("required", key)) { |
36daaaca JB |
1059 | drv->required = git_config_bool(var, value); |
1060 | return 0; | |
1061 | } | |
1062 | ||
aa4ed402 JH |
1063 | return 0; |
1064 | } | |
1065 | ||
3fed15f5 JH |
1066 | static int count_ident(const char *cp, unsigned long size) |
1067 | { | |
1068 | /* | |
af9b54bb | 1069 | * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$" |
3fed15f5 JH |
1070 | */ |
1071 | int cnt = 0; | |
1072 | char ch; | |
1073 | ||
1074 | while (size) { | |
1075 | ch = *cp++; | |
1076 | size--; | |
1077 | if (ch != '$') | |
1078 | continue; | |
af9b54bb | 1079 | if (size < 3) |
3fed15f5 | 1080 | break; |
af9b54bb | 1081 | if (memcmp("Id", cp, 2)) |
3fed15f5 | 1082 | continue; |
af9b54bb AP |
1083 | ch = cp[2]; |
1084 | cp += 3; | |
1085 | size -= 3; | |
3fed15f5 | 1086 | if (ch == '$') |
af9b54bb | 1087 | cnt++; /* $Id$ */ |
3fed15f5 JH |
1088 | if (ch != ':') |
1089 | continue; | |
1090 | ||
1091 | /* | |
af9b54bb | 1092 | * "$Id: ... "; scan up to the closing dollar sign and discard. |
3fed15f5 JH |
1093 | */ |
1094 | while (size) { | |
1095 | ch = *cp++; | |
1096 | size--; | |
1097 | if (ch == '$') { | |
1098 | cnt++; | |
1099 | break; | |
1100 | } | |
a9f3049f HG |
1101 | if (ch == '\n') |
1102 | break; | |
3fed15f5 JH |
1103 | } |
1104 | } | |
1105 | return cnt; | |
1106 | } | |
1107 | ||
55ad152c | 1108 | static int ident_to_git(const char *src, size_t len, |
ec36c42a | 1109 | struct strbuf *buf, int ident) |
3fed15f5 | 1110 | { |
5ecd293d | 1111 | char *dst, *dollar; |
3fed15f5 | 1112 | |
4c3b57b9 | 1113 | if (!ident || (src && !count_ident(src, len))) |
5ecd293d PH |
1114 | return 0; |
1115 | ||
92ac3197 JK |
1116 | if (!buf) |
1117 | return 1; | |
1118 | ||
90d16ec0 PH |
1119 | /* only grow if not in place */ |
1120 | if (strbuf_avail(buf) + buf->len < len) | |
1121 | strbuf_grow(buf, len - buf->len); | |
5ecd293d PH |
1122 | dst = buf->buf; |
1123 | for (;;) { | |
1124 | dollar = memchr(src, '$', len); | |
1125 | if (!dollar) | |
1126 | break; | |
77321184 | 1127 | memmove(dst, src, dollar + 1 - src); |
5ecd293d PH |
1128 | dst += dollar + 1 - src; |
1129 | len -= dollar + 1 - src; | |
1130 | src = dollar + 1; | |
1131 | ||
1132 | if (len > 3 && !memcmp(src, "Id:", 3)) { | |
1133 | dollar = memchr(src + 3, '$', len - 3); | |
1134 | if (!dollar) | |
1135 | break; | |
a9f3049f HG |
1136 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
1137 | /* Line break before the next dollar. */ | |
1138 | continue; | |
1139 | } | |
1140 | ||
af9b54bb AP |
1141 | memcpy(dst, "Id$", 3); |
1142 | dst += 3; | |
5ecd293d PH |
1143 | len -= dollar + 1 - src; |
1144 | src = dollar + 1; | |
3fed15f5 JH |
1145 | } |
1146 | } | |
77321184 | 1147 | memmove(dst, src, len); |
5ecd293d PH |
1148 | strbuf_setlen(buf, dst + len - buf->buf); |
1149 | return 1; | |
3fed15f5 JH |
1150 | } |
1151 | ||
55ad152c | 1152 | static int ident_to_worktree(const char *src, size_t len, |
ec36c42a | 1153 | struct strbuf *buf, int ident) |
3fed15f5 | 1154 | { |
f070facc | 1155 | struct object_id oid; |
07814d90 | 1156 | char *to_free = NULL, *dollar, *spc; |
5ecd293d | 1157 | int cnt; |
3fed15f5 JH |
1158 | |
1159 | if (!ident) | |
5ecd293d | 1160 | return 0; |
3fed15f5 | 1161 | |
5ecd293d | 1162 | cnt = count_ident(src, len); |
3fed15f5 | 1163 | if (!cnt) |
5ecd293d | 1164 | return 0; |
3fed15f5 | 1165 | |
5ecd293d PH |
1166 | /* are we "faking" in place editing ? */ |
1167 | if (src == buf->buf) | |
b315c5c0 | 1168 | to_free = strbuf_detach(buf, NULL); |
44439c1c | 1169 | hash_object_file(the_hash_algo, src, len, OBJ_BLOB, &oid); |
3fed15f5 | 1170 | |
1a750441 | 1171 | strbuf_grow(buf, len + cnt * (the_hash_algo->hexsz + 3)); |
5ecd293d PH |
1172 | for (;;) { |
1173 | /* step 1: run to the next '$' */ | |
1174 | dollar = memchr(src, '$', len); | |
1175 | if (!dollar) | |
1176 | break; | |
1177 | strbuf_add(buf, src, dollar + 1 - src); | |
1178 | len -= dollar + 1 - src; | |
1179 | src = dollar + 1; | |
c23290d5 | 1180 | |
5ecd293d PH |
1181 | /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */ |
1182 | if (len < 3 || memcmp("Id", src, 2)) | |
3fed15f5 JH |
1183 | continue; |
1184 | ||
5ecd293d PH |
1185 | /* step 3: skip over Id$ or Id:xxxxx$ */ |
1186 | if (src[2] == '$') { | |
1187 | src += 3; | |
1188 | len -= 3; | |
1189 | } else if (src[2] == ':') { | |
1190 | /* | |
1191 | * It's possible that an expanded Id has crept its way into the | |
07814d90 HG |
1192 | * repository, we cope with that by stripping the expansion out. |
1193 | * This is probably not a good idea, since it will cause changes | |
1194 | * on checkout, which won't go away by stash, but let's keep it | |
1195 | * for git-style ids. | |
5ecd293d PH |
1196 | */ |
1197 | dollar = memchr(src + 3, '$', len - 3); | |
1198 | if (!dollar) { | |
1199 | /* incomplete keyword, no more '$', so just quit the loop */ | |
1200 | break; | |
1201 | } | |
c23290d5 | 1202 | |
a9f3049f HG |
1203 | if (memchr(src + 3, '\n', dollar - src - 3)) { |
1204 | /* Line break before the next dollar. */ | |
1205 | continue; | |
1206 | } | |
1207 | ||
07814d90 HG |
1208 | spc = memchr(src + 4, ' ', dollar - src - 4); |
1209 | if (spc && spc < dollar-1) { | |
1210 | /* There are spaces in unexpected places. | |
1211 | * This is probably an id from some other | |
1212 | * versioning system. Keep it for now. | |
1213 | */ | |
1214 | continue; | |
1215 | } | |
1216 | ||
5ecd293d PH |
1217 | len -= dollar + 1 - src; |
1218 | src = dollar + 1; | |
1219 | } else { | |
1220 | /* it wasn't a "Id$" or "Id:xxxx$" */ | |
1221 | continue; | |
1222 | } | |
c23290d5 | 1223 | |
5ecd293d PH |
1224 | /* step 4: substitute */ |
1225 | strbuf_addstr(buf, "Id: "); | |
f070facc | 1226 | strbuf_addstr(buf, oid_to_hex(&oid)); |
5ecd293d | 1227 | strbuf_addstr(buf, " $"); |
3fed15f5 | 1228 | } |
5ecd293d | 1229 | strbuf_add(buf, src, len); |
3fed15f5 | 1230 | |
5ecd293d PH |
1231 | free(to_free); |
1232 | return 1; | |
35ebfd6a JH |
1233 | } |
1234 | ||
107642fe LS |
1235 | static const char *git_path_check_encoding(struct attr_check_item *check) |
1236 | { | |
1237 | const char *value = check->value; | |
1238 | ||
1239 | if (ATTR_UNSET(value) || !strlen(value)) | |
1240 | return NULL; | |
1241 | ||
1242 | if (ATTR_TRUE(value) || ATTR_FALSE(value)) { | |
1243 | die(_("true/false are no valid working-tree-encodings")); | |
1244 | } | |
1245 | ||
1246 | /* Don't encode to the default encoding */ | |
1247 | if (same_encoding(value, default_encoding)) | |
1248 | return NULL; | |
1249 | ||
1250 | return value; | |
1251 | } | |
1252 | ||
38e95844 | 1253 | static enum convert_crlf_action git_path_check_crlf(struct attr_check_item *check) |
35ebfd6a | 1254 | { |
6073ee85 JH |
1255 | const char *value = check->value; |
1256 | ||
1257 | if (ATTR_TRUE(value)) | |
1258 | return CRLF_TEXT; | |
1259 | else if (ATTR_FALSE(value)) | |
1260 | return CRLF_BINARY; | |
1261 | else if (ATTR_UNSET(value)) | |
1262 | ; | |
1263 | else if (!strcmp(value, "input")) | |
df747b81 | 1264 | return CRLF_TEXT_INPUT; |
fd6cce9e EB |
1265 | else if (!strcmp(value, "auto")) |
1266 | return CRLF_AUTO; | |
df747b81 | 1267 | return CRLF_UNDEFINED; |
35ebfd6a JH |
1268 | } |
1269 | ||
7bd18054 | 1270 | static enum eol git_path_check_eol(struct attr_check_item *check) |
fd6cce9e EB |
1271 | { |
1272 | const char *value = check->value; | |
1273 | ||
1274 | if (ATTR_UNSET(value)) | |
1275 | ; | |
1276 | else if (!strcmp(value, "lf")) | |
1277 | return EOL_LF; | |
1278 | else if (!strcmp(value, "crlf")) | |
1279 | return EOL_CRLF; | |
1280 | return EOL_UNSET; | |
1281 | } | |
1282 | ||
7bd18054 | 1283 | static struct convert_driver *git_path_check_convert(struct attr_check_item *check) |
aa4ed402 JH |
1284 | { |
1285 | const char *value = check->value; | |
1286 | struct convert_driver *drv; | |
1287 | ||
1288 | if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value)) | |
1289 | return NULL; | |
1290 | for (drv = user_convert; drv; drv = drv->next) | |
1291 | if (!strcmp(value, drv->name)) | |
1292 | return drv; | |
1293 | return NULL; | |
1294 | } | |
1295 | ||
7bd18054 | 1296 | static int git_path_check_ident(struct attr_check_item *check) |
3fed15f5 JH |
1297 | { |
1298 | const char *value = check->value; | |
1299 | ||
1300 | return !!ATTR_TRUE(value); | |
1301 | } | |
1302 | ||
2c65d90f | 1303 | static struct attr_check *check; |
1304 | ||
8e978529 | 1305 | void convert_attrs(struct index_state *istate, |
38e95844 | 1306 | struct conv_attrs *ca, const char *path) |
83295964 | 1307 | { |
d64324cb | 1308 | struct attr_check_item *ccheck = NULL; |
83295964 | 1309 | |
2aef63d3 JH |
1310 | if (!check) { |
1311 | check = attr_check_initl("crlf", "ident", "filter", | |
107642fe LS |
1312 | "eol", "text", "working-tree-encoding", |
1313 | NULL); | |
83295964 JH |
1314 | user_convert_tail = &user_convert; |
1315 | git_config(read_convert_config, NULL); | |
1316 | } | |
3bfba20d | 1317 | |
44451a2e | 1318 | git_check_attr(istate, path, check); |
d64324cb TB |
1319 | ccheck = check->items; |
1320 | ca->crlf_action = git_path_check_crlf(ccheck + 4); | |
1321 | if (ca->crlf_action == CRLF_UNDEFINED) | |
1322 | ca->crlf_action = git_path_check_crlf(ccheck + 0); | |
1323 | ca->ident = git_path_check_ident(ccheck + 1); | |
1324 | ca->drv = git_path_check_convert(ccheck + 2); | |
1325 | if (ca->crlf_action != CRLF_BINARY) { | |
1326 | enum eol eol_attr = git_path_check_eol(ccheck + 3); | |
1327 | if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF) | |
1328 | ca->crlf_action = CRLF_AUTO_INPUT; | |
1329 | else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF) | |
1330 | ca->crlf_action = CRLF_AUTO_CRLF; | |
1331 | else if (eol_attr == EOL_LF) | |
1332 | ca->crlf_action = CRLF_TEXT_INPUT; | |
1333 | else if (eol_attr == EOL_CRLF) | |
1334 | ca->crlf_action = CRLF_TEXT_CRLF; | |
3bfba20d | 1335 | } |
d64324cb | 1336 | ca->working_tree_encoding = git_path_check_encoding(ccheck + 5); |
5c94c93d MÅ |
1337 | |
1338 | /* Save attr and make a decision for action */ | |
1339 | ca->attr_action = ca->crlf_action; | |
df747b81 TB |
1340 | if (ca->crlf_action == CRLF_TEXT) |
1341 | ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT; | |
1342 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE) | |
1343 | ca->crlf_action = CRLF_BINARY; | |
1344 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE) | |
1345 | ca->crlf_action = CRLF_AUTO_CRLF; | |
1346 | if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT) | |
1347 | ca->crlf_action = CRLF_AUTO_INPUT; | |
83295964 JH |
1348 | } |
1349 | ||
2c65d90f | 1350 | void reset_parsed_attributes(void) |
1351 | { | |
1352 | struct convert_driver *drv, *next; | |
1353 | ||
1354 | attr_check_free(check); | |
1355 | check = NULL; | |
1356 | reset_merge_attributes(); | |
1357 | ||
1358 | for (drv = user_convert; drv; drv = next) { | |
1359 | next = drv->next; | |
1360 | free((void *)drv->name); | |
1361 | free(drv); | |
1362 | } | |
1363 | user_convert = NULL; | |
1364 | user_convert_tail = NULL; | |
1365 | } | |
1366 | ||
847a9e5d | 1367 | int would_convert_to_git_filter_fd(struct index_state *istate, const char *path) |
9035d75a SP |
1368 | { |
1369 | struct conv_attrs ca; | |
1370 | ||
7f944e26 | 1371 | convert_attrs(istate, &ca, path); |
9035d75a SP |
1372 | if (!ca.drv) |
1373 | return 0; | |
1374 | ||
1375 | /* | |
1376 | * Apply a filter to an fd only if the filter is required to succeed. | |
1377 | * We must die if the filter fails, because the original data before | |
1378 | * filtering is not available. | |
1379 | */ | |
1380 | if (!ca.drv->required) | |
1381 | return 0; | |
1382 | ||
ab90ecae | 1383 | return apply_filter(path, NULL, 0, -1, NULL, ca.drv, CAP_CLEAN, NULL, NULL); |
9035d75a SP |
1384 | } |
1385 | ||
847a9e5d | 1386 | const char *get_convert_attr_ascii(struct index_state *istate, const char *path) |
a7630bd4 TB |
1387 | { |
1388 | struct conv_attrs ca; | |
a7630bd4 | 1389 | |
7f944e26 | 1390 | convert_attrs(istate, &ca, path); |
bb211b4d | 1391 | switch (ca.attr_action) { |
df747b81 | 1392 | case CRLF_UNDEFINED: |
a7630bd4 TB |
1393 | return ""; |
1394 | case CRLF_BINARY: | |
1395 | return "-text"; | |
1396 | case CRLF_TEXT: | |
1397 | return "text"; | |
df747b81 | 1398 | case CRLF_TEXT_INPUT: |
a7630bd4 | 1399 | return "text eol=lf"; |
df747b81 TB |
1400 | case CRLF_TEXT_CRLF: |
1401 | return "text eol=crlf"; | |
a7630bd4 TB |
1402 | case CRLF_AUTO: |
1403 | return "text=auto"; | |
df747b81 | 1404 | case CRLF_AUTO_CRLF: |
65237284 | 1405 | return "text=auto eol=crlf"; |
df747b81 | 1406 | case CRLF_AUTO_INPUT: |
65237284 | 1407 | return "text=auto eol=lf"; |
a7630bd4 TB |
1408 | } |
1409 | return ""; | |
1410 | } | |
1411 | ||
847a9e5d | 1412 | int convert_to_git(struct index_state *istate, |
82b474e0 | 1413 | const char *path, const char *src, size_t len, |
8462ff43 | 1414 | struct strbuf *dst, int conv_flags) |
35ebfd6a | 1415 | { |
3bfba20d | 1416 | int ret = 0; |
3bfba20d | 1417 | struct conv_attrs ca; |
6073ee85 | 1418 | |
7f944e26 | 1419 | convert_attrs(istate, &ca, path); |
3fed15f5 | 1420 | |
ab90ecae | 1421 | ret |= apply_filter(path, src, len, -1, dst, ca.drv, CAP_CLEAN, NULL, NULL); |
234fa07e | 1422 | if (!ret && ca.drv && ca.drv->required) |
d26a328e | 1423 | die(_("%s: clean filter '%s' failed"), path, ca.drv->name); |
36daaaca | 1424 | |
92ac3197 | 1425 | if (ret && dst) { |
5ecd293d PH |
1426 | src = dst->buf; |
1427 | len = dst->len; | |
aa4ed402 | 1428 | } |
107642fe LS |
1429 | |
1430 | ret |= encode_to_git(path, src, len, dst, ca.working_tree_encoding, conv_flags); | |
1431 | if (ret && dst) { | |
1432 | src = dst->buf; | |
1433 | len = dst->len; | |
1434 | } | |
1435 | ||
8462ff43 TB |
1436 | if (!(conv_flags & CONV_EOL_KEEP_CRLF)) { |
1437 | ret |= crlf_to_git(istate, path, src, len, dst, ca.crlf_action, conv_flags); | |
2fea9de6 TB |
1438 | if (ret && dst) { |
1439 | src = dst->buf; | |
1440 | len = dst->len; | |
1441 | } | |
6073ee85 | 1442 | } |
55ad152c | 1443 | return ret | ident_to_git(src, len, dst, ca.ident); |
35ebfd6a JH |
1444 | } |
1445 | ||
847a9e5d | 1446 | void convert_to_git_filter_fd(struct index_state *istate, |
d6c41c20 | 1447 | const char *path, int fd, struct strbuf *dst, |
8462ff43 | 1448 | int conv_flags) |
9035d75a SP |
1449 | { |
1450 | struct conv_attrs ca; | |
7f944e26 | 1451 | convert_attrs(istate, &ca, path); |
9035d75a SP |
1452 | |
1453 | assert(ca.drv); | |
9035d75a | 1454 | |
ab90ecae | 1455 | if (!apply_filter(path, NULL, 0, fd, dst, ca.drv, CAP_CLEAN, NULL, NULL)) |
d26a328e | 1456 | die(_("%s: clean filter '%s' failed"), path, ca.drv->name); |
9035d75a | 1457 | |
107642fe | 1458 | encode_to_git(path, dst->buf, dst->len, dst, ca.working_tree_encoding, conv_flags); |
8462ff43 | 1459 | crlf_to_git(istate, path, dst->buf, dst->len, dst, ca.crlf_action, conv_flags); |
55ad152c | 1460 | ident_to_git(dst->buf, dst->len, dst, ca.ident); |
9035d75a SP |
1461 | } |
1462 | ||
55b4ad0e JH |
1463 | static int convert_to_working_tree_ca_internal(const struct conv_attrs *ca, |
1464 | const char *path, const char *src, | |
1465 | size_t len, struct strbuf *dst, | |
1466 | int normalizing, | |
1467 | const struct checkout_metadata *meta, | |
1468 | struct delayed_checkout *dco) | |
35ebfd6a | 1469 | { |
36daaaca | 1470 | int ret = 0, ret_filter = 0; |
3fed15f5 | 1471 | |
55b4ad0e | 1472 | ret |= ident_to_worktree(src, len, dst, ca->ident); |
5ecd293d PH |
1473 | if (ret) { |
1474 | src = dst->buf; | |
1475 | len = dst->len; | |
3fed15f5 | 1476 | } |
43dd2332 EB |
1477 | /* |
1478 | * CRLF conversion can be skipped if normalizing, unless there | |
edcc8581 LS |
1479 | * is a smudge or process filter (even if the process filter doesn't |
1480 | * support smudge). The filters might expect CRLFs. | |
43dd2332 | 1481 | */ |
55b4ad0e JH |
1482 | if ((ca->drv && (ca->drv->smudge || ca->drv->process)) || !normalizing) { |
1483 | ret |= crlf_to_worktree(src, len, dst, ca->crlf_action); | |
43dd2332 EB |
1484 | if (ret) { |
1485 | src = dst->buf; | |
1486 | len = dst->len; | |
1487 | } | |
aa4ed402 | 1488 | } |
36daaaca | 1489 | |
55b4ad0e | 1490 | ret |= encode_to_worktree(path, src, len, dst, ca->working_tree_encoding); |
107642fe LS |
1491 | if (ret) { |
1492 | src = dst->buf; | |
1493 | len = dst->len; | |
1494 | } | |
1495 | ||
2841e8f8 | 1496 | ret_filter = apply_filter( |
55b4ad0e JH |
1497 | path, src, len, -1, dst, ca->drv, CAP_SMUDGE, meta, dco); |
1498 | if (!ret_filter && ca->drv && ca->drv->required) | |
1499 | die(_("%s: smudge filter %s failed"), path, ca->drv->name); | |
36daaaca JB |
1500 | |
1501 | return ret | ret_filter; | |
35ebfd6a | 1502 | } |
f217f0e8 | 1503 | |
55b4ad0e JH |
1504 | int async_convert_to_working_tree_ca(const struct conv_attrs *ca, |
1505 | const char *path, const char *src, | |
1506 | size_t len, struct strbuf *dst, | |
1507 | const struct checkout_metadata *meta, | |
1508 | void *dco) | |
2841e8f8 | 1509 | { |
55b4ad0e JH |
1510 | return convert_to_working_tree_ca_internal(ca, path, src, len, dst, 0, |
1511 | meta, dco); | |
2841e8f8 LS |
1512 | } |
1513 | ||
55b4ad0e JH |
1514 | int convert_to_working_tree_ca(const struct conv_attrs *ca, |
1515 | const char *path, const char *src, | |
1516 | size_t len, struct strbuf *dst, | |
1517 | const struct checkout_metadata *meta) | |
43dd2332 | 1518 | { |
55b4ad0e JH |
1519 | return convert_to_working_tree_ca_internal(ca, path, src, len, dst, 0, |
1520 | meta, NULL); | |
43dd2332 EB |
1521 | } |
1522 | ||
847a9e5d | 1523 | int renormalize_buffer(struct index_state *istate, const char *path, |
a33e0b2a | 1524 | const char *src, size_t len, struct strbuf *dst) |
f217f0e8 | 1525 | { |
55b4ad0e JH |
1526 | struct conv_attrs ca; |
1527 | int ret; | |
1528 | ||
1529 | convert_attrs(istate, &ca, path); | |
1530 | ret = convert_to_working_tree_ca_internal(&ca, path, src, len, dst, 1, | |
1531 | NULL, NULL); | |
f217f0e8 EB |
1532 | if (ret) { |
1533 | src = dst->buf; | |
1534 | len = dst->len; | |
1535 | } | |
8462ff43 | 1536 | return ret | convert_to_git(istate, path, src, len, dst, CONV_EOL_RENORMALIZE); |
f217f0e8 | 1537 | } |
dd8e9121 | 1538 | |
b6691092 JH |
1539 | /***************************************************************** |
1540 | * | |
749f763d | 1541 | * Streaming conversion support |
b6691092 JH |
1542 | * |
1543 | *****************************************************************/ | |
1544 | ||
1545 | typedef int (*filter_fn)(struct stream_filter *, | |
1546 | const char *input, size_t *isize_p, | |
1547 | char *output, size_t *osize_p); | |
1548 | typedef void (*free_fn)(struct stream_filter *); | |
1549 | ||
1550 | struct stream_filter_vtbl { | |
1551 | filter_fn filter; | |
1552 | free_fn free; | |
1553 | }; | |
1554 | ||
1555 | struct stream_filter { | |
1556 | struct stream_filter_vtbl *vtbl; | |
1557 | }; | |
1558 | ||
dfd2a238 | 1559 | static int null_filter_fn(struct stream_filter *filter UNUSED, |
b6691092 JH |
1560 | const char *input, size_t *isize_p, |
1561 | char *output, size_t *osize_p) | |
1562 | { | |
4ae66704 JH |
1563 | size_t count; |
1564 | ||
1565 | if (!input) | |
1566 | return 0; /* we do not keep any states */ | |
1567 | count = *isize_p; | |
b6691092 JH |
1568 | if (*osize_p < count) |
1569 | count = *osize_p; | |
1570 | if (count) { | |
1571 | memmove(output, input, count); | |
1572 | *isize_p -= count; | |
1573 | *osize_p -= count; | |
1574 | } | |
1575 | return 0; | |
1576 | } | |
1577 | ||
dfd2a238 | 1578 | static void null_free_fn(struct stream_filter *filter UNUSED) |
b6691092 JH |
1579 | { |
1580 | ; /* nothing -- null instances are shared */ | |
1581 | } | |
1582 | ||
1583 | static struct stream_filter_vtbl null_vtbl = { | |
a9f6274f ÆAB |
1584 | .filter = null_filter_fn, |
1585 | .free = null_free_fn, | |
b6691092 JH |
1586 | }; |
1587 | ||
1588 | static struct stream_filter null_filter_singleton = { | |
a9f6274f | 1589 | .vtbl = &null_vtbl, |
b6691092 JH |
1590 | }; |
1591 | ||
1592 | int is_null_stream_filter(struct stream_filter *filter) | |
1593 | { | |
1594 | return filter == &null_filter_singleton; | |
1595 | } | |
1596 | ||
b84c7839 JH |
1597 | |
1598 | /* | |
1599 | * LF-to-CRLF filter | |
1600 | */ | |
284e3d28 CMN |
1601 | |
1602 | struct lf_to_crlf_filter { | |
1603 | struct stream_filter filter; | |
8496f568 JH |
1604 | unsigned has_held:1; |
1605 | char held; | |
284e3d28 CMN |
1606 | }; |
1607 | ||
e322ee38 JH |
1608 | static int lf_to_crlf_filter_fn(struct stream_filter *filter, |
1609 | const char *input, size_t *isize_p, | |
1610 | char *output, size_t *osize_p) | |
1611 | { | |
284e3d28 CMN |
1612 | size_t count, o = 0; |
1613 | struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter; | |
1614 | ||
8496f568 JH |
1615 | /* |
1616 | * We may be holding onto the CR to see if it is followed by a | |
1617 | * LF, in which case we would need to go to the main loop. | |
1618 | * Otherwise, just emit it to the output stream. | |
1619 | */ | |
1620 | if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) { | |
1621 | output[o++] = lf_to_crlf->held; | |
1622 | lf_to_crlf->has_held = 0; | |
284e3d28 | 1623 | } |
e322ee38 | 1624 | |
87afe9a5 JH |
1625 | /* We are told to drain */ |
1626 | if (!input) { | |
1627 | *osize_p -= o; | |
1628 | return 0; | |
1629 | } | |
e322ee38 | 1630 | |
e322ee38 | 1631 | count = *isize_p; |
8496f568 | 1632 | if (count || lf_to_crlf->has_held) { |
284e3d28 | 1633 | size_t i; |
8496f568 JH |
1634 | int was_cr = 0; |
1635 | ||
1636 | if (lf_to_crlf->has_held) { | |
1637 | was_cr = 1; | |
1638 | lf_to_crlf->has_held = 0; | |
1639 | } | |
1640 | ||
284e3d28 | 1641 | for (i = 0; o < *osize_p && i < count; i++) { |
e322ee38 | 1642 | char ch = input[i]; |
8496f568 | 1643 | |
e322ee38 | 1644 | if (ch == '\n') { |
284e3d28 | 1645 | output[o++] = '\r'; |
8496f568 JH |
1646 | } else if (was_cr) { |
1647 | /* | |
1648 | * Previous round saw CR and it is not followed | |
1649 | * by a LF; emit the CR before processing the | |
1650 | * current character. | |
1651 | */ | |
1652 | output[o++] = '\r'; | |
e322ee38 | 1653 | } |
8496f568 JH |
1654 | |
1655 | /* | |
1656 | * We may have consumed the last output slot, | |
1657 | * in which case we need to break out of this | |
1658 | * loop; hold the current character before | |
1659 | * returning. | |
1660 | */ | |
1661 | if (*osize_p <= o) { | |
1662 | lf_to_crlf->has_held = 1; | |
1663 | lf_to_crlf->held = ch; | |
1664 | continue; /* break but increment i */ | |
1665 | } | |
1666 | ||
1667 | if (ch == '\r') { | |
1668 | was_cr = 1; | |
1669 | continue; | |
1670 | } | |
1671 | ||
1672 | was_cr = 0; | |
e322ee38 JH |
1673 | output[o++] = ch; |
1674 | } | |
1675 | ||
1676 | *osize_p -= o; | |
1677 | *isize_p -= i; | |
8496f568 JH |
1678 | |
1679 | if (!lf_to_crlf->has_held && was_cr) { | |
1680 | lf_to_crlf->has_held = 1; | |
1681 | lf_to_crlf->held = '\r'; | |
1682 | } | |
e322ee38 JH |
1683 | } |
1684 | return 0; | |
1685 | } | |
1686 | ||
284e3d28 CMN |
1687 | static void lf_to_crlf_free_fn(struct stream_filter *filter) |
1688 | { | |
1689 | free(filter); | |
1690 | } | |
1691 | ||
e322ee38 | 1692 | static struct stream_filter_vtbl lf_to_crlf_vtbl = { |
a9f6274f ÆAB |
1693 | .filter = lf_to_crlf_filter_fn, |
1694 | .free = lf_to_crlf_free_fn, | |
e322ee38 JH |
1695 | }; |
1696 | ||
284e3d28 CMN |
1697 | static struct stream_filter *lf_to_crlf_filter(void) |
1698 | { | |
87afe9a5 | 1699 | struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf)); |
e322ee38 | 1700 | |
284e3d28 | 1701 | lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl; |
284e3d28 CMN |
1702 | return (struct stream_filter *)lf_to_crlf; |
1703 | } | |
b84c7839 | 1704 | |
a265a7f9 JH |
1705 | /* |
1706 | * Cascade filter | |
1707 | */ | |
1708 | #define FILTER_BUFFER 1024 | |
1709 | struct cascade_filter { | |
1710 | struct stream_filter filter; | |
1711 | struct stream_filter *one; | |
1712 | struct stream_filter *two; | |
1713 | char buf[FILTER_BUFFER]; | |
1714 | int end, ptr; | |
1715 | }; | |
1716 | ||
1717 | static int cascade_filter_fn(struct stream_filter *filter, | |
1718 | const char *input, size_t *isize_p, | |
1719 | char *output, size_t *osize_p) | |
1720 | { | |
1721 | struct cascade_filter *cas = (struct cascade_filter *) filter; | |
1722 | size_t filled = 0; | |
1723 | size_t sz = *osize_p; | |
1724 | size_t to_feed, remaining; | |
1725 | ||
1726 | /* | |
1727 | * input -- (one) --> buf -- (two) --> output | |
1728 | */ | |
1729 | while (filled < sz) { | |
1730 | remaining = sz - filled; | |
1731 | ||
1732 | /* do we already have something to feed two with? */ | |
1733 | if (cas->ptr < cas->end) { | |
1734 | to_feed = cas->end - cas->ptr; | |
1735 | if (stream_filter(cas->two, | |
1736 | cas->buf + cas->ptr, &to_feed, | |
1737 | output + filled, &remaining)) | |
1738 | return -1; | |
1739 | cas->ptr += (cas->end - cas->ptr) - to_feed; | |
1740 | filled = sz - remaining; | |
1741 | continue; | |
1742 | } | |
1743 | ||
1744 | /* feed one from upstream and have it emit into our buffer */ | |
1745 | to_feed = input ? *isize_p : 0; | |
1746 | if (input && !to_feed) | |
1747 | break; | |
1748 | remaining = sizeof(cas->buf); | |
1749 | if (stream_filter(cas->one, | |
1750 | input, &to_feed, | |
1751 | cas->buf, &remaining)) | |
1752 | return -1; | |
1753 | cas->end = sizeof(cas->buf) - remaining; | |
1754 | cas->ptr = 0; | |
1755 | if (input) { | |
1756 | size_t fed = *isize_p - to_feed; | |
1757 | *isize_p -= fed; | |
1758 | input += fed; | |
1759 | } | |
1760 | ||
1761 | /* do we know that we drained one completely? */ | |
1762 | if (input || cas->end) | |
1763 | continue; | |
1764 | ||
1765 | /* tell two to drain; we have nothing more to give it */ | |
1766 | to_feed = 0; | |
1767 | remaining = sz - filled; | |
1768 | if (stream_filter(cas->two, | |
1769 | NULL, &to_feed, | |
1770 | output + filled, &remaining)) | |
1771 | return -1; | |
1772 | if (remaining == (sz - filled)) | |
1773 | break; /* completely drained two */ | |
1774 | filled = sz - remaining; | |
1775 | } | |
1776 | *osize_p -= filled; | |
1777 | return 0; | |
1778 | } | |
1779 | ||
1780 | static void cascade_free_fn(struct stream_filter *filter) | |
1781 | { | |
1782 | struct cascade_filter *cas = (struct cascade_filter *)filter; | |
1783 | free_stream_filter(cas->one); | |
1784 | free_stream_filter(cas->two); | |
1785 | free(filter); | |
1786 | } | |
1787 | ||
1788 | static struct stream_filter_vtbl cascade_vtbl = { | |
a9f6274f ÆAB |
1789 | .filter = cascade_filter_fn, |
1790 | .free = cascade_free_fn, | |
a265a7f9 JH |
1791 | }; |
1792 | ||
1793 | static struct stream_filter *cascade_filter(struct stream_filter *one, | |
1794 | struct stream_filter *two) | |
1795 | { | |
1796 | struct cascade_filter *cascade; | |
1797 | ||
1798 | if (!one || is_null_stream_filter(one)) | |
1799 | return two; | |
1800 | if (!two || is_null_stream_filter(two)) | |
1801 | return one; | |
1802 | ||
1803 | cascade = xmalloc(sizeof(*cascade)); | |
1804 | cascade->one = one; | |
1805 | cascade->two = two; | |
1806 | cascade->end = cascade->ptr = 0; | |
1807 | cascade->filter.vtbl = &cascade_vtbl; | |
1808 | return (struct stream_filter *)cascade; | |
1809 | } | |
1810 | ||
b84c7839 JH |
1811 | /* |
1812 | * ident filter | |
1813 | */ | |
1814 | #define IDENT_DRAINING (-1) | |
1815 | #define IDENT_SKIPPING (-2) | |
1816 | struct ident_filter { | |
1817 | struct stream_filter filter; | |
1818 | struct strbuf left; | |
1819 | int state; | |
1a750441 | 1820 | char ident[GIT_MAX_HEXSZ + 5]; /* ": x40 $" */ |
b84c7839 JH |
1821 | }; |
1822 | ||
1823 | static int is_foreign_ident(const char *str) | |
1824 | { | |
1825 | int i; | |
1826 | ||
ae021d87 | 1827 | if (!skip_prefix(str, "$Id: ", &str)) |
b84c7839 | 1828 | return 0; |
ae021d87 | 1829 | for (i = 0; str[i]; i++) { |
b84c7839 JH |
1830 | if (isspace(str[i]) && str[i+1] != '$') |
1831 | return 1; | |
1832 | } | |
1833 | return 0; | |
1834 | } | |
1835 | ||
1836 | static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p) | |
1837 | { | |
1838 | size_t to_drain = ident->left.len; | |
1839 | ||
1840 | if (*osize_p < to_drain) | |
1841 | to_drain = *osize_p; | |
1842 | if (to_drain) { | |
1843 | memcpy(*output_p, ident->left.buf, to_drain); | |
1844 | strbuf_remove(&ident->left, 0, to_drain); | |
1845 | *output_p += to_drain; | |
1846 | *osize_p -= to_drain; | |
1847 | } | |
1848 | if (!ident->left.len) | |
1849 | ident->state = 0; | |
1850 | } | |
1851 | ||
1852 | static int ident_filter_fn(struct stream_filter *filter, | |
1853 | const char *input, size_t *isize_p, | |
1854 | char *output, size_t *osize_p) | |
1855 | { | |
1856 | struct ident_filter *ident = (struct ident_filter *)filter; | |
1857 | static const char head[] = "$Id"; | |
1858 | ||
1859 | if (!input) { | |
1860 | /* drain upon eof */ | |
1861 | switch (ident->state) { | |
1862 | default: | |
1863 | strbuf_add(&ident->left, head, ident->state); | |
1cf01a34 | 1864 | /* fallthrough */ |
b84c7839 | 1865 | case IDENT_SKIPPING: |
1cf01a34 | 1866 | /* fallthrough */ |
b84c7839 JH |
1867 | case IDENT_DRAINING: |
1868 | ident_drain(ident, &output, osize_p); | |
1869 | } | |
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | while (*isize_p || (ident->state == IDENT_DRAINING)) { | |
1874 | int ch; | |
1875 | ||
1876 | if (ident->state == IDENT_DRAINING) { | |
1877 | ident_drain(ident, &output, osize_p); | |
1878 | if (!*osize_p) | |
1879 | break; | |
1880 | continue; | |
1881 | } | |
1882 | ||
1883 | ch = *(input++); | |
1884 | (*isize_p)--; | |
1885 | ||
1886 | if (ident->state == IDENT_SKIPPING) { | |
1887 | /* | |
1888 | * Skipping until '$' or LF, but keeping them | |
1889 | * in case it is a foreign ident. | |
1890 | */ | |
1891 | strbuf_addch(&ident->left, ch); | |
1892 | if (ch != '\n' && ch != '$') | |
1893 | continue; | |
1894 | if (ch == '$' && !is_foreign_ident(ident->left.buf)) { | |
1895 | strbuf_setlen(&ident->left, sizeof(head) - 1); | |
1896 | strbuf_addstr(&ident->left, ident->ident); | |
1897 | } | |
1898 | ident->state = IDENT_DRAINING; | |
1899 | continue; | |
1900 | } | |
1901 | ||
1902 | if (ident->state < sizeof(head) && | |
1903 | head[ident->state] == ch) { | |
1904 | ident->state++; | |
1905 | continue; | |
1906 | } | |
1907 | ||
1908 | if (ident->state) | |
1909 | strbuf_add(&ident->left, head, ident->state); | |
1910 | if (ident->state == sizeof(head) - 1) { | |
1911 | if (ch != ':' && ch != '$') { | |
1912 | strbuf_addch(&ident->left, ch); | |
1913 | ident->state = 0; | |
1914 | continue; | |
1915 | } | |
1916 | ||
1917 | if (ch == ':') { | |
1918 | strbuf_addch(&ident->left, ch); | |
1919 | ident->state = IDENT_SKIPPING; | |
1920 | } else { | |
1921 | strbuf_addstr(&ident->left, ident->ident); | |
1922 | ident->state = IDENT_DRAINING; | |
1923 | } | |
1924 | continue; | |
1925 | } | |
1926 | ||
1927 | strbuf_addch(&ident->left, ch); | |
1928 | ident->state = IDENT_DRAINING; | |
1929 | } | |
1930 | return 0; | |
1931 | } | |
1932 | ||
1933 | static void ident_free_fn(struct stream_filter *filter) | |
1934 | { | |
1935 | struct ident_filter *ident = (struct ident_filter *)filter; | |
1936 | strbuf_release(&ident->left); | |
1937 | free(filter); | |
1938 | } | |
1939 | ||
1940 | static struct stream_filter_vtbl ident_vtbl = { | |
a9f6274f ÆAB |
1941 | .filter = ident_filter_fn, |
1942 | .free = ident_free_fn, | |
b84c7839 JH |
1943 | }; |
1944 | ||
1a750441 | 1945 | static struct stream_filter *ident_filter(const struct object_id *oid) |
b84c7839 JH |
1946 | { |
1947 | struct ident_filter *ident = xmalloc(sizeof(*ident)); | |
1948 | ||
5096d490 | 1949 | xsnprintf(ident->ident, sizeof(ident->ident), |
1a750441 | 1950 | ": %s $", oid_to_hex(oid)); |
b84c7839 JH |
1951 | strbuf_init(&ident->left, 0); |
1952 | ident->filter.vtbl = &ident_vtbl; | |
1953 | ident->state = 0; | |
1954 | return (struct stream_filter *)ident; | |
1955 | } | |
1956 | ||
dd8e9121 | 1957 | /* |
3e9e82c0 | 1958 | * Return an appropriately constructed filter for the given ca, or NULL if |
b6691092 JH |
1959 | * the contents cannot be filtered without reading the whole thing |
1960 | * in-core. | |
1961 | * | |
2b0f19fa | 1962 | * Note that you would be crazy to set CRLF, smudge/clean or ident to a |
b6691092 | 1963 | * large binary blob you would want us not to slurp into the memory! |
dd8e9121 | 1964 | */ |
3e9e82c0 JH |
1965 | struct stream_filter *get_stream_filter_ca(const struct conv_attrs *ca, |
1966 | const struct object_id *oid) | |
dd8e9121 | 1967 | { |
b84c7839 | 1968 | struct stream_filter *filter = NULL; |
dd8e9121 | 1969 | |
f59d15bb | 1970 | if (classify_conv_attrs(ca) != CA_CLASS_STREAMABLE) |
107642fe LS |
1971 | return NULL; |
1972 | ||
3e9e82c0 | 1973 | if (ca->ident) |
1a750441 | 1974 | filter = ident_filter(oid); |
dd8e9121 | 1975 | |
3e9e82c0 | 1976 | if (output_eol(ca->crlf_action) == EOL_CRLF) |
284e3d28 | 1977 | filter = cascade_filter(filter, lf_to_crlf_filter()); |
caa47adc TB |
1978 | else |
1979 | filter = cascade_filter(filter, &null_filter_singleton); | |
e322ee38 | 1980 | |
b84c7839 | 1981 | return filter; |
b6691092 JH |
1982 | } |
1983 | ||
8e978529 | 1984 | struct stream_filter *get_stream_filter(struct index_state *istate, |
3e9e82c0 JH |
1985 | const char *path, |
1986 | const struct object_id *oid) | |
1987 | { | |
1988 | struct conv_attrs ca; | |
1989 | convert_attrs(istate, &ca, path); | |
1990 | return get_stream_filter_ca(&ca, oid); | |
1991 | } | |
1992 | ||
b6691092 JH |
1993 | void free_stream_filter(struct stream_filter *filter) |
1994 | { | |
1995 | filter->vtbl->free(filter); | |
1996 | } | |
1997 | ||
1998 | int stream_filter(struct stream_filter *filter, | |
1999 | const char *input, size_t *isize_p, | |
2000 | char *output, size_t *osize_p) | |
2001 | { | |
2002 | return filter->vtbl->filter(filter, input, isize_p, output, osize_p); | |
dd8e9121 | 2003 | } |
c397aac0 | 2004 | |
2005 | void init_checkout_metadata(struct checkout_metadata *meta, const char *refname, | |
2006 | const struct object_id *treeish, | |
2007 | const struct object_id *blob) | |
2008 | { | |
2009 | memset(meta, 0, sizeof(*meta)); | |
2010 | if (refname) | |
2011 | meta->refname = refname; | |
2012 | if (treeish) | |
2013 | oidcpy(&meta->treeish, treeish); | |
2014 | if (blob) | |
2015 | oidcpy(&meta->blob, blob); | |
2016 | } | |
2017 | ||
2018 | void clone_checkout_metadata(struct checkout_metadata *dst, | |
2019 | const struct checkout_metadata *src, | |
2020 | const struct object_id *blob) | |
2021 | { | |
2022 | memcpy(dst, src, sizeof(*dst)); | |
2023 | if (blob) | |
2024 | oidcpy(&dst->blob, blob); | |
2025 | } | |
f59d15bb JH |
2026 | |
2027 | enum conv_attrs_classification classify_conv_attrs(const struct conv_attrs *ca) | |
2028 | { | |
2029 | if (ca->drv) { | |
2030 | if (ca->drv->process) | |
2031 | return CA_CLASS_INCORE_PROCESS; | |
2032 | if (ca->drv->smudge || ca->drv->clean) | |
2033 | return CA_CLASS_INCORE_FILTER; | |
2034 | } | |
2035 | ||
2036 | if (ca->working_tree_encoding) | |
2037 | return CA_CLASS_INCORE; | |
2038 | ||
2039 | if (ca->crlf_action == CRLF_AUTO || ca->crlf_action == CRLF_AUTO_CRLF) | |
2040 | return CA_CLASS_INCORE; | |
2041 | ||
2042 | return CA_CLASS_STREAMABLE; | |
2043 | } |