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