blob: cb5fbb45ea04cead65316ecf6b44730b17108122 [file] [log] [blame]
Linus Torvalds6c510be2007-02-13 11:07:23 -08001#include "cache.h"
Junio C Hamano35ebfd62007-04-12 22:30:05 -07002#include "attr.h"
Junio C Hamano3fed15f2007-04-21 19:09:02 -07003#include "run-command.h"
Pete Wyckoffa2b665d2010-12-22 06:40:13 -08004#include "quote.h"
Jehan Bing6424c2a2012-02-20 12:53:37 -08005#include "sigchain.h"
Junio C Hamano35ebfd62007-04-12 22:30:05 -07006
Linus Torvalds6c510be2007-02-13 11:07:23 -08007/*
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
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +020013 * translation when the "text" attribute or "auto_crlf" option is set.
Linus Torvalds6c510be2007-02-13 11:07:23 -080014 */
15
Junio C Hamanoc61dcff2011-05-09 13:12:57 -070016enum crlf_action {
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +020017 CRLF_GUESS = -1,
18 CRLF_BINARY = 0,
19 CRLF_TEXT,
20 CRLF_INPUT,
21 CRLF_CRLF,
Jonathan Niederc9b67822011-03-16 01:59:10 -050022 CRLF_AUTO
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +020023};
Junio C Hamano163b9592007-04-19 22:37:19 -070024
Linus Torvalds6c510be2007-02-13 11:07:23 -080025struct text_stat {
Dmitry Potapov28624192008-01-16 04:59:12 +030026 /* NUL, CR, LF and CRLF counts */
27 unsigned nul, cr, lf, crlf;
Linus Torvalds6c510be2007-02-13 11:07:23 -080028
29 /* These are just approximations! */
30 unsigned printable, nonprintable;
31};
32
33static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
34{
35 unsigned long i;
36
37 memset(stats, 0, sizeof(*stats));
38
39 for (i = 0; i < size; i++) {
40 unsigned char c = buf[i];
41 if (c == '\r') {
42 stats->cr++;
43 if (i+1 < size && buf[i+1] == '\n')
44 stats->crlf++;
45 continue;
46 }
47 if (c == '\n') {
48 stats->lf++;
49 continue;
50 }
51 if (c == 127)
52 /* DEL */
53 stats->nonprintable++;
54 else if (c < 32) {
55 switch (c) {
56 /* BS, HT, ESC and FF */
57 case '\b': case '\t': case '\033': case '\014':
58 stats->printable++;
59 break;
Dmitry Potapov28624192008-01-16 04:59:12 +030060 case 0:
61 stats->nul++;
62 /* fall through */
Linus Torvalds6c510be2007-02-13 11:07:23 -080063 default:
64 stats->nonprintable++;
65 }
66 }
67 else
68 stats->printable++;
69 }
Dmitry Kakurinf9dd4bf2008-07-11 18:48:16 +020070
71 /* If file ends with EOF then don't count this EOF as non-printable. */
72 if (size >= 1 && buf[size-1] == '\032')
73 stats->nonprintable--;
Linus Torvalds6c510be2007-02-13 11:07:23 -080074}
75
76/*
77 * The same heuristics as diff.c::mmfile_is_binary()
78 */
79static int is_binary(unsigned long size, struct text_stat *stats)
80{
81
Dmitry Potapov28624192008-01-16 04:59:12 +030082 if (stats->nul)
83 return 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -080084 if ((stats->printable >> 7) < stats->nonprintable)
85 return 1;
86 /*
87 * Other heuristics? Average line length might be relevant,
88 * as might LF vs CR vs CRLF counts..
89 *
90 * NOTE! It might be normal to have a low ratio of CRLF to LF
91 * (somebody starts with a LF-only file and edits it with an editor
92 * that adds CRLF only to lines that are added..). But do we
93 * want to support CR-only? Probably not.
94 */
95 return 0;
96}
97
Junio C Hamanoc61dcff2011-05-09 13:12:57 -070098static enum eol output_eol(enum crlf_action crlf_action)
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +020099{
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700100 switch (crlf_action) {
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200101 case CRLF_BINARY:
102 return EOL_UNSET;
103 case CRLF_CRLF:
104 return EOL_CRLF;
105 case CRLF_INPUT:
106 return EOL_LF;
107 case CRLF_GUESS:
108 if (!auto_crlf)
109 return EOL_UNSET;
110 /* fall through */
111 case CRLF_TEXT:
112 case CRLF_AUTO:
113 if (auto_crlf == AUTO_CRLF_TRUE)
114 return EOL_CRLF;
115 else if (auto_crlf == AUTO_CRLF_INPUT)
116 return EOL_LF;
Junio C Hamanoec70f522011-05-09 12:52:12 -0700117 else if (core_eol == EOL_UNSET)
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200118 return EOL_NATIVE;
119 }
Junio C Hamanoec70f522011-05-09 12:52:12 -0700120 return core_eol;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200121}
122
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700123static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100124 struct text_stat *stats, enum safe_crlf checksafe)
125{
126 if (!checksafe)
127 return;
128
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700129 if (output_eol(crlf_action) == EOL_LF) {
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100130 /*
131 * CRLFs would not be restored by checkout:
132 * check if we'd remove CRLFs
133 */
134 if (stats->crlf) {
135 if (checksafe == SAFE_CRLF_WARN)
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200136 warning("CRLF will be replaced by LF in %s.\nThe file will have its original line endings in your working directory.", path);
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100137 else /* i.e. SAFE_CRLF_FAIL */
138 die("CRLF would be replaced by LF in %s.", path);
139 }
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700140 } else if (output_eol(crlf_action) == EOL_CRLF) {
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100141 /*
142 * CRLFs would be added by checkout:
143 * check if we have "naked" LFs
144 */
145 if (stats->lf != stats->crlf) {
146 if (checksafe == SAFE_CRLF_WARN)
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200147 warning("LF will be replaced by CRLF in %s.\nThe file will have its original line endings in your working directory.", path);
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100148 else /* i.e. SAFE_CRLF_FAIL */
149 die("LF would be replaced by CRLF in %s", path);
150 }
151 }
152}
153
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200154static int has_cr_in_index(const char *path)
155{
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200156 unsigned long sz;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200157 void *data;
158 int has_cr;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200159
Lukas Fleischer4982fd72013-04-13 15:28:32 +0200160 data = read_blob_data_from_cache(path, &sz);
161 if (!data)
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200162 return 0;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200163 has_cr = memchr(data, '\r', sz) != NULL;
164 free(data);
165 return has_cr;
166}
167
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200168static int crlf_to_git(const char *path, const char *src, size_t len,
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700169 struct strbuf *buf,
170 enum crlf_action crlf_action, enum safe_crlf checksafe)
Linus Torvalds6c510be2007-02-13 11:07:23 -0800171{
Linus Torvalds6c510be2007-02-13 11:07:23 -0800172 struct text_stat stats;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200173 char *dst;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800174
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700175 if (crlf_action == CRLF_BINARY ||
Jeff King4c3b57b2012-02-24 17:05:03 -0500176 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE) ||
177 (src && !len))
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200178 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800179
Jeff King4c3b57b2012-02-24 17:05:03 -0500180 /*
181 * If we are doing a dry-run and have no source buffer, there is
182 * nothing to analyze; we must assume we would convert.
183 */
184 if (!buf && !src)
185 return 1;
186
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200187 gather_stats(src, len, &stats);
Linus Torvalds6c510be2007-02-13 11:07:23 -0800188
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700189 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700190 /*
191 * We're currently not going to even try to convert stuff
192 * that has bare CR characters. Does anybody do that crazy
193 * stuff?
194 */
195 if (stats.cr != stats.crlf)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200196 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800197
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700198 /*
199 * And add some heuristics for binary vs text, of course...
200 */
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200201 if (is_binary(len, &stats))
202 return 0;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200203
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700204 if (crlf_action == CRLF_GUESS) {
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200205 /*
206 * If the file in the index has any CR in it, do not convert.
207 * This is the new safer autocrlf handling.
208 */
209 if (has_cr_in_index(path))
210 return 0;
211 }
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700212 }
Linus Torvalds6c510be2007-02-13 11:07:23 -0800213
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700214 check_safe_crlf(path, crlf_action, &stats, checksafe);
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100215
216 /* Optimization: No CR? Nothing to convert, regardless. */
217 if (!stats.cr)
218 return 0;
219
Jeff King92ac3192012-02-24 17:02:37 -0500220 /*
221 * At this point all of our source analysis is done, and we are sure we
222 * would convert. If we are in dry-run mode, we can give an answer.
223 */
224 if (!buf)
225 return 1;
226
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200227 /* only grow if not in place */
228 if (strbuf_avail(buf) + buf->len < len)
229 strbuf_grow(buf, len - buf->len);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200230 dst = buf->buf;
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700231 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
Junio C Hamano163b9592007-04-19 22:37:19 -0700232 /*
233 * If we guessed, we already know we rejected a file with
234 * lone CR, and we can strip a CR without looking at what
235 * follow it.
236 */
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700237 do {
Alex Riesenac78e542007-04-19 02:05:03 +0200238 unsigned char c = *src++;
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700239 if (c != '\r')
Alex Riesenac78e542007-04-19 02:05:03 +0200240 *dst++ = c;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200241 } while (--len);
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700242 } else {
243 do {
Alex Riesenac78e542007-04-19 02:05:03 +0200244 unsigned char c = *src++;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200245 if (! (c == '\r' && (1 < len && *src == '\n')))
Alex Riesenac78e542007-04-19 02:05:03 +0200246 *dst++ = c;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200247 } while (--len);
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700248 }
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200249 strbuf_setlen(buf, dst - buf->buf);
250 return 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800251}
252
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200253static int crlf_to_worktree(const char *path, const char *src, size_t len,
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700254 struct strbuf *buf, enum crlf_action crlf_action)
Linus Torvalds6c510be2007-02-13 11:07:23 -0800255{
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200256 char *to_free = NULL;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800257 struct text_stat stats;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800258
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700259 if (!len || output_eol(crlf_action) != EOL_CRLF)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200260 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800261
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200262 gather_stats(src, len, &stats);
Linus Torvalds6c510be2007-02-13 11:07:23 -0800263
264 /* No LF? Nothing to convert, regardless. */
265 if (!stats.lf)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200266 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800267
268 /* Was it already in CRLF format? */
269 if (stats.lf == stats.crlf)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200270 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800271
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700272 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS) {
273 if (crlf_action == CRLF_GUESS) {
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200274 /* If we have any CR or CRLF line endings, we do not touch it */
275 /* This is the new safer autocrlf-handling */
276 if (stats.cr > 0 || stats.crlf > 0)
277 return 0;
278 }
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200279
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700280 /* If we have any bare CR characters, we're not going to touch it */
281 if (stats.cr != stats.crlf)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200282 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800283
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200284 if (is_binary(len, &stats))
285 return 0;
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700286 }
Linus Torvalds6c510be2007-02-13 11:07:23 -0800287
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200288 /* are we "faking" in place editing ? */
289 if (src == buf->buf)
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200290 to_free = strbuf_detach(buf, NULL);
Alex Riesenac78e542007-04-19 02:05:03 +0200291
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200292 strbuf_grow(buf, len + stats.lf - stats.crlf);
293 for (;;) {
294 const char *nl = memchr(src, '\n', len);
295 if (!nl)
296 break;
297 if (nl > src && nl[-1] == '\r') {
298 strbuf_add(buf, src, nl + 1 - src);
299 } else {
300 strbuf_add(buf, src, nl - src);
301 strbuf_addstr(buf, "\r\n");
302 }
303 len -= nl + 1 - src;
304 src = nl + 1;
305 }
306 strbuf_add(buf, src, len);
Linus Torvalds6c510be2007-02-13 11:07:23 -0800307
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200308 free(to_free);
309 return 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800310}
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700311
Johannes Sixt546bb582007-10-19 21:48:06 +0200312struct filter_params {
313 const char *src;
314 unsigned long size;
315 const char *cmd;
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800316 const char *path;
Johannes Sixt546bb582007-10-19 21:48:06 +0200317};
318
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800319static int filter_buffer(int in, int out, void *data)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700320{
321 /*
322 * Spawn cmd and feed the buffer contents through its stdin.
323 */
324 struct child_process child_process;
Johannes Sixt546bb582007-10-19 21:48:06 +0200325 struct filter_params *params = (struct filter_params *)data;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700326 int write_err, status;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000327 const char *argv[] = { NULL, NULL };
328
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800329 /* apply % substitution to cmd */
330 struct strbuf cmd = STRBUF_INIT;
331 struct strbuf path = STRBUF_INIT;
332 struct strbuf_expand_dict_entry dict[] = {
333 { "f", NULL, },
334 { NULL, NULL, },
335 };
336
337 /* quote the path to preserve spaces, etc. */
338 sq_quote_buf(&path, params->path);
339 dict[0].value = path.buf;
340
341 /* expand all %f with the quoted path */
342 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
343 strbuf_release(&path);
344
345 argv[0] = cmd.buf;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700346
347 memset(&child_process, 0, sizeof(child_process));
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200348 child_process.argv = argv;
Jeff Kingac0ba182009-12-30 05:53:57 -0500349 child_process.use_shell = 1;
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200350 child_process.in = -1;
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800351 child_process.out = out;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700352
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200353 if (start_command(&child_process))
Johannes Sixt546bb582007-10-19 21:48:06 +0200354 return error("cannot fork to run external filter %s", params->cmd);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700355
Jehan Bing6424c2a2012-02-20 12:53:37 -0800356 sigchain_push(SIGPIPE, SIG_IGN);
357
Johannes Sixt546bb582007-10-19 21:48:06 +0200358 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200359 if (close(child_process.in))
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700360 write_err = 1;
361 if (write_err)
Johannes Sixt546bb582007-10-19 21:48:06 +0200362 error("cannot feed the input to external filter %s", params->cmd);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700363
Jehan Bing6424c2a2012-02-20 12:53:37 -0800364 sigchain_pop(SIGPIPE);
365
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700366 status = finish_command(&child_process);
367 if (status)
Johannes Sixt5709e032009-07-04 21:26:39 +0200368 error("external filter %s failed %d", params->cmd, status);
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800369
370 strbuf_release(&cmd);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700371 return (write_err || status);
372}
373
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200374static int apply_filter(const char *path, const char *src, size_t len,
375 struct strbuf *dst, const char *cmd)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700376{
377 /*
378 * Create a pipeline to have the command filter the buffer's
379 * contents.
380 *
381 * (child --> cmd) --> us
382 */
Johannes Sixt546bb582007-10-19 21:48:06 +0200383 int ret = 1;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500384 struct strbuf nbuf = STRBUF_INIT;
Johannes Sixt546bb582007-10-19 21:48:06 +0200385 struct async async;
386 struct filter_params params;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700387
388 if (!cmd)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200389 return 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700390
Jeff King92ac3192012-02-24 17:02:37 -0500391 if (!dst)
392 return 1;
393
Johannes Sixt546bb582007-10-19 21:48:06 +0200394 memset(&async, 0, sizeof(async));
395 async.proc = filter_buffer;
396 async.data = &params;
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800397 async.out = -1;
Johannes Sixt546bb582007-10-19 21:48:06 +0200398 params.src = src;
399 params.size = len;
400 params.cmd = cmd;
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800401 params.path = path;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700402
403 fflush(NULL);
Johannes Sixt546bb582007-10-19 21:48:06 +0200404 if (start_async(&async))
405 return 0; /* error was already reported */
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700406
Johannes Sixt546bb582007-10-19 21:48:06 +0200407 if (strbuf_read(&nbuf, async.out, len) < 0) {
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200408 error("read from external filter %s failed", cmd);
409 ret = 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700410 }
Johannes Sixt546bb582007-10-19 21:48:06 +0200411 if (close(async.out)) {
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200412 error("read from external filter %s failed", cmd);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200413 ret = 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700414 }
Johannes Sixt546bb582007-10-19 21:48:06 +0200415 if (finish_async(&async)) {
416 error("external filter %s failed", cmd);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200417 ret = 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700418 }
419
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200420 if (ret) {
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200421 strbuf_swap(dst, &nbuf);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200422 }
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200423 strbuf_release(&nbuf);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200424 return ret;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700425}
426
427static struct convert_driver {
428 const char *name;
429 struct convert_driver *next;
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400430 const char *smudge;
431 const char *clean;
Jehan Bing36daaac2012-02-16 17:19:03 -0800432 int required;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700433} *user_convert, **user_convert_tail;
434
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100435static int read_convert_config(const char *var, const char *value, void *cb)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700436{
Jeff Kingd731f0a2013-01-23 01:24:23 -0500437 const char *key, *name;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700438 int namelen;
439 struct convert_driver *drv;
440
441 /*
442 * External conversion drivers are configured using
443 * "filter.<name>.variable".
444 */
Jeff Kingd731f0a2013-01-23 01:24:23 -0500445 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700446 return 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700447 for (drv = user_convert; drv; drv = drv->next)
448 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
449 break;
450 if (!drv) {
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700451 drv = xcalloc(1, sizeof(struct convert_driver));
Pierre Habouzit182af832007-09-16 00:32:36 +0200452 drv->name = xmemdupz(name, namelen);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700453 *user_convert_tail = drv;
454 user_convert_tail = &(drv->next);
455 }
456
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700457 /*
458 * filter.<name>.smudge and filter.<name>.clean specifies
459 * the command line:
460 *
461 * command-line
462 *
463 * The command-line will not be interpolated in any way.
464 */
465
Jeff Kingd731f0a2013-01-23 01:24:23 -0500466 if (!strcmp("smudge", key))
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400467 return git_config_string(&drv->smudge, var, value);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700468
Jeff Kingd731f0a2013-01-23 01:24:23 -0500469 if (!strcmp("clean", key))
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400470 return git_config_string(&drv->clean, var, value);
471
Jeff Kingd731f0a2013-01-23 01:24:23 -0500472 if (!strcmp("required", key)) {
Jehan Bing36daaac2012-02-16 17:19:03 -0800473 drv->required = git_config_bool(var, value);
474 return 0;
475 }
476
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700477 return 0;
478}
479
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700480static int count_ident(const char *cp, unsigned long size)
481{
482 /*
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100483 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700484 */
485 int cnt = 0;
486 char ch;
487
488 while (size) {
489 ch = *cp++;
490 size--;
491 if (ch != '$')
492 continue;
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100493 if (size < 3)
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700494 break;
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100495 if (memcmp("Id", cp, 2))
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700496 continue;
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100497 ch = cp[2];
498 cp += 3;
499 size -= 3;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700500 if (ch == '$')
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100501 cnt++; /* $Id$ */
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700502 if (ch != ':')
503 continue;
504
505 /*
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100506 * "$Id: ... "; scan up to the closing dollar sign and discard.
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700507 */
508 while (size) {
509 ch = *cp++;
510 size--;
511 if (ch == '$') {
512 cnt++;
513 break;
514 }
Henrik Grubbströma9f30492010-04-06 14:46:37 +0200515 if (ch == '\n')
516 break;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700517 }
518 }
519 return cnt;
520}
521
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200522static int ident_to_git(const char *path, const char *src, size_t len,
523 struct strbuf *buf, int ident)
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700524{
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200525 char *dst, *dollar;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700526
Jeff King4c3b57b2012-02-24 17:05:03 -0500527 if (!ident || (src && !count_ident(src, len)))
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200528 return 0;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700529
Jeff King92ac3192012-02-24 17:02:37 -0500530 if (!buf)
531 return 1;
532
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200533 /* only grow if not in place */
534 if (strbuf_avail(buf) + buf->len < len)
535 strbuf_grow(buf, len - buf->len);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200536 dst = buf->buf;
537 for (;;) {
538 dollar = memchr(src, '$', len);
539 if (!dollar)
540 break;
Thomas Rast77321182011-08-29 22:06:04 +0200541 memmove(dst, src, dollar + 1 - src);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200542 dst += dollar + 1 - src;
543 len -= dollar + 1 - src;
544 src = dollar + 1;
545
546 if (len > 3 && !memcmp(src, "Id:", 3)) {
547 dollar = memchr(src + 3, '$', len - 3);
548 if (!dollar)
549 break;
Henrik Grubbströma9f30492010-04-06 14:46:37 +0200550 if (memchr(src + 3, '\n', dollar - src - 3)) {
551 /* Line break before the next dollar. */
552 continue;
553 }
554
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100555 memcpy(dst, "Id$", 3);
556 dst += 3;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200557 len -= dollar + 1 - src;
558 src = dollar + 1;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700559 }
560 }
Thomas Rast77321182011-08-29 22:06:04 +0200561 memmove(dst, src, len);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200562 strbuf_setlen(buf, dst + len - buf->buf);
563 return 1;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700564}
565
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200566static int ident_to_worktree(const char *path, const char *src, size_t len,
567 struct strbuf *buf, int ident)
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700568{
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700569 unsigned char sha1[20];
Henrik Grubbström07814d92010-04-06 14:46:38 +0200570 char *to_free = NULL, *dollar, *spc;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200571 int cnt;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700572
573 if (!ident)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200574 return 0;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700575
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200576 cnt = count_ident(src, len);
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700577 if (!cnt)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200578 return 0;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700579
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200580 /* are we "faking" in place editing ? */
581 if (src == buf->buf)
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200582 to_free = strbuf_detach(buf, NULL);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200583 hash_sha1_file(src, len, "blob", sha1);
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700584
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200585 strbuf_grow(buf, len + cnt * 43);
586 for (;;) {
587 /* step 1: run to the next '$' */
588 dollar = memchr(src, '$', len);
589 if (!dollar)
590 break;
591 strbuf_add(buf, src, dollar + 1 - src);
592 len -= dollar + 1 - src;
593 src = dollar + 1;
594
595 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
596 if (len < 3 || memcmp("Id", src, 2))
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700597 continue;
598
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200599 /* step 3: skip over Id$ or Id:xxxxx$ */
600 if (src[2] == '$') {
601 src += 3;
602 len -= 3;
603 } else if (src[2] == ':') {
Andy Parkinsc23290d2007-05-25 11:50:08 +0100604 /*
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200605 * It's possible that an expanded Id has crept its way into the
Henrik Grubbström07814d92010-04-06 14:46:38 +0200606 * repository, we cope with that by stripping the expansion out.
607 * This is probably not a good idea, since it will cause changes
608 * on checkout, which won't go away by stash, but let's keep it
609 * for git-style ids.
Andy Parkinsc23290d2007-05-25 11:50:08 +0100610 */
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200611 dollar = memchr(src + 3, '$', len - 3);
612 if (!dollar) {
613 /* incomplete keyword, no more '$', so just quit the loop */
614 break;
615 }
616
Henrik Grubbströma9f30492010-04-06 14:46:37 +0200617 if (memchr(src + 3, '\n', dollar - src - 3)) {
618 /* Line break before the next dollar. */
619 continue;
620 }
621
Henrik Grubbström07814d92010-04-06 14:46:38 +0200622 spc = memchr(src + 4, ' ', dollar - src - 4);
623 if (spc && spc < dollar-1) {
624 /* There are spaces in unexpected places.
625 * This is probably an id from some other
626 * versioning system. Keep it for now.
627 */
628 continue;
629 }
630
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200631 len -= dollar + 1 - src;
632 src = dollar + 1;
633 } else {
634 /* it wasn't a "Id$" or "Id:xxxx$" */
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700635 continue;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200636 }
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700637
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200638 /* step 4: substitute */
639 strbuf_addstr(buf, "Id: ");
640 strbuf_add(buf, sha1_to_hex(sha1), 40);
641 strbuf_addstr(buf, " $");
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700642 }
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200643 strbuf_add(buf, src, len);
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700644
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200645 free(to_free);
646 return 1;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700647}
648
Ramkumar Ramachandra7356b512011-11-15 22:29:37 +0530649static enum crlf_action git_path_check_crlf(const char *path, struct git_attr_check *check)
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700650{
Junio C Hamano6073ee82007-04-20 23:44:02 -0700651 const char *value = check->value;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700652
Junio C Hamano6073ee82007-04-20 23:44:02 -0700653 if (ATTR_TRUE(value))
654 return CRLF_TEXT;
655 else if (ATTR_FALSE(value))
656 return CRLF_BINARY;
657 else if (ATTR_UNSET(value))
658 ;
659 else if (!strcmp(value, "input"))
660 return CRLF_INPUT;
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200661 else if (!strcmp(value, "auto"))
662 return CRLF_AUTO;
Junio C Hamano163b9592007-04-19 22:37:19 -0700663 return CRLF_GUESS;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700664}
665
Ramsay Jonesef563de2011-11-21 18:42:09 +0000666static enum eol git_path_check_eol(const char *path, struct git_attr_check *check)
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200667{
668 const char *value = check->value;
669
670 if (ATTR_UNSET(value))
671 ;
672 else if (!strcmp(value, "lf"))
673 return EOL_LF;
674 else if (!strcmp(value, "crlf"))
675 return EOL_CRLF;
676 return EOL_UNSET;
677}
678
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700679static struct convert_driver *git_path_check_convert(const char *path,
680 struct git_attr_check *check)
681{
682 const char *value = check->value;
683 struct convert_driver *drv;
684
685 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
686 return NULL;
687 for (drv = user_convert; drv; drv = drv->next)
688 if (!strcmp(value, drv->name))
689 return drv;
690 return NULL;
691}
692
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700693static int git_path_check_ident(const char *path, struct git_attr_check *check)
694{
695 const char *value = check->value;
696
697 return !!ATTR_TRUE(value);
698}
699
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700700static enum crlf_action input_crlf_action(enum crlf_action text_attr, enum eol eol_attr)
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200701{
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200702 if (text_attr == CRLF_BINARY)
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200703 return CRLF_BINARY;
704 if (eol_attr == EOL_LF)
705 return CRLF_INPUT;
706 if (eol_attr == EOL_CRLF)
707 return CRLF_CRLF;
Eyvind Bernhardsen5ec3e672010-05-19 22:43:11 +0200708 return text_attr;
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200709}
710
Junio C Hamano3bfba202011-05-09 13:58:31 -0700711struct conv_attrs {
712 struct convert_driver *drv;
713 enum crlf_action crlf_action;
714 enum eol eol_attr;
715 int ident;
716};
717
Junio C Hamano83295962011-05-09 11:23:04 -0700718static const char *conv_attr_name[] = {
719 "crlf", "ident", "filter", "eol", "text",
720};
721#define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
722
Junio C Hamano3bfba202011-05-09 13:58:31 -0700723static void convert_attrs(struct conv_attrs *ca, const char *path)
Junio C Hamano83295962011-05-09 11:23:04 -0700724{
725 int i;
726 static struct git_attr_check ccheck[NUM_CONV_ATTRS];
727
728 if (!ccheck[0].attr) {
729 for (i = 0; i < NUM_CONV_ATTRS; i++)
730 ccheck[i].attr = git_attr(conv_attr_name[i]);
731 user_convert_tail = &user_convert;
732 git_config(read_convert_config, NULL);
733 }
Junio C Hamano3bfba202011-05-09 13:58:31 -0700734
Michael Haggertyd932f4e2011-08-04 06:36:33 +0200735 if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
Junio C Hamano3bfba202011-05-09 13:58:31 -0700736 ca->crlf_action = git_path_check_crlf(path, ccheck + 4);
737 if (ca->crlf_action == CRLF_GUESS)
738 ca->crlf_action = git_path_check_crlf(path, ccheck + 0);
739 ca->ident = git_path_check_ident(path, ccheck + 1);
740 ca->drv = git_path_check_convert(path, ccheck + 2);
741 ca->eol_attr = git_path_check_eol(path, ccheck + 3);
742 } else {
743 ca->drv = NULL;
744 ca->crlf_action = CRLF_GUESS;
745 ca->eol_attr = EOL_UNSET;
746 ca->ident = 0;
747 }
Junio C Hamano83295962011-05-09 11:23:04 -0700748}
749
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100750int convert_to_git(const char *path, const char *src, size_t len,
751 struct strbuf *dst, enum safe_crlf checksafe)
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700752{
Junio C Hamano3bfba202011-05-09 13:58:31 -0700753 int ret = 0;
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400754 const char *filter = NULL;
Jehan Bing36daaac2012-02-16 17:19:03 -0800755 int required = 0;
Junio C Hamano3bfba202011-05-09 13:58:31 -0700756 struct conv_attrs ca;
Junio C Hamano6073ee82007-04-20 23:44:02 -0700757
Junio C Hamano3bfba202011-05-09 13:58:31 -0700758 convert_attrs(&ca, path);
Jehan Bing36daaac2012-02-16 17:19:03 -0800759 if (ca.drv) {
Junio C Hamano3bfba202011-05-09 13:58:31 -0700760 filter = ca.drv->clean;
Jehan Bing36daaac2012-02-16 17:19:03 -0800761 required = ca.drv->required;
762 }
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700763
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200764 ret |= apply_filter(path, src, len, dst, filter);
Jehan Bing36daaac2012-02-16 17:19:03 -0800765 if (!ret && required)
766 die("%s: clean filter '%s' failed", path, ca.drv->name);
767
Jeff King92ac3192012-02-24 17:02:37 -0500768 if (ret && dst) {
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200769 src = dst->buf;
770 len = dst->len;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700771 }
Junio C Hamano3bfba202011-05-09 13:58:31 -0700772 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
773 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
Jeff King92ac3192012-02-24 17:02:37 -0500774 if (ret && dst) {
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200775 src = dst->buf;
776 len = dst->len;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700777 }
Junio C Hamano3bfba202011-05-09 13:58:31 -0700778 return ret | ident_to_git(path, src, len, dst, ca.ident);
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700779}
780
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200781static int convert_to_working_tree_internal(const char *path, const char *src,
782 size_t len, struct strbuf *dst,
783 int normalizing)
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700784{
Jehan Bing36daaac2012-02-16 17:19:03 -0800785 int ret = 0, ret_filter = 0;
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400786 const char *filter = NULL;
Jehan Bing36daaac2012-02-16 17:19:03 -0800787 int required = 0;
Junio C Hamano3bfba202011-05-09 13:58:31 -0700788 struct conv_attrs ca;
Junio C Hamano6073ee82007-04-20 23:44:02 -0700789
Junio C Hamano3bfba202011-05-09 13:58:31 -0700790 convert_attrs(&ca, path);
Jehan Bing36daaac2012-02-16 17:19:03 -0800791 if (ca.drv) {
Junio C Hamano3bfba202011-05-09 13:58:31 -0700792 filter = ca.drv->smudge;
Jehan Bing36daaac2012-02-16 17:19:03 -0800793 required = ca.drv->required;
794 }
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700795
Junio C Hamano3bfba202011-05-09 13:58:31 -0700796 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200797 if (ret) {
798 src = dst->buf;
799 len = dst->len;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700800 }
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200801 /*
802 * CRLF conversion can be skipped if normalizing, unless there
803 * is a smudge filter. The filter might expect CRLFs.
804 */
805 if (filter || !normalizing) {
Junio C Hamano3bfba202011-05-09 13:58:31 -0700806 ca.crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
807 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200808 if (ret) {
809 src = dst->buf;
810 len = dst->len;
811 }
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700812 }
Jehan Bing36daaac2012-02-16 17:19:03 -0800813
814 ret_filter = apply_filter(path, src, len, dst, filter);
815 if (!ret_filter && required)
816 die("%s: smudge filter %s failed", path, ca.drv->name);
817
818 return ret | ret_filter;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700819}
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200820
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200821int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
822{
823 return convert_to_working_tree_internal(path, src, len, dst, 0);
824}
825
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200826int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
827{
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200828 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200829 if (ret) {
830 src = dst->buf;
831 len = dst->len;
832 }
Ramkumar Ramachandra7356b512011-11-15 22:29:37 +0530833 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_FALSE);
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200834}
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700835
Junio C Hamanob6691092011-05-20 14:33:31 -0700836/*****************************************************************
837 *
Ondřej Bílka749f7632013-07-22 23:02:23 +0200838 * Streaming conversion support
Junio C Hamanob6691092011-05-20 14:33:31 -0700839 *
840 *****************************************************************/
841
842typedef int (*filter_fn)(struct stream_filter *,
843 const char *input, size_t *isize_p,
844 char *output, size_t *osize_p);
845typedef void (*free_fn)(struct stream_filter *);
846
847struct stream_filter_vtbl {
848 filter_fn filter;
849 free_fn free;
850};
851
852struct stream_filter {
853 struct stream_filter_vtbl *vtbl;
854};
855
856static int null_filter_fn(struct stream_filter *filter,
857 const char *input, size_t *isize_p,
858 char *output, size_t *osize_p)
859{
Junio C Hamano4ae66702011-05-21 14:05:51 -0700860 size_t count;
861
862 if (!input)
863 return 0; /* we do not keep any states */
864 count = *isize_p;
Junio C Hamanob6691092011-05-20 14:33:31 -0700865 if (*osize_p < count)
866 count = *osize_p;
867 if (count) {
868 memmove(output, input, count);
869 *isize_p -= count;
870 *osize_p -= count;
871 }
872 return 0;
873}
874
875static void null_free_fn(struct stream_filter *filter)
876{
877 ; /* nothing -- null instances are shared */
878}
879
880static struct stream_filter_vtbl null_vtbl = {
881 null_filter_fn,
882 null_free_fn,
883};
884
885static struct stream_filter null_filter_singleton = {
886 &null_vtbl,
887};
888
889int is_null_stream_filter(struct stream_filter *filter)
890{
891 return filter == &null_filter_singleton;
892}
893
Junio C Hamanob84c78392011-05-20 18:28:00 -0700894
895/*
896 * LF-to-CRLF filter
897 */
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100898
899struct lf_to_crlf_filter {
900 struct stream_filter filter;
Junio C Hamano8496f562011-12-16 15:44:18 -0800901 unsigned has_held:1;
902 char held;
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100903};
904
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700905static int lf_to_crlf_filter_fn(struct stream_filter *filter,
906 const char *input, size_t *isize_p,
907 char *output, size_t *osize_p)
908{
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100909 size_t count, o = 0;
910 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700911
Junio C Hamano8496f562011-12-16 15:44:18 -0800912 /*
913 * We may be holding onto the CR to see if it is followed by a
914 * LF, in which case we would need to go to the main loop.
915 * Otherwise, just emit it to the output stream.
916 */
917 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
918 output[o++] = lf_to_crlf->held;
919 lf_to_crlf->has_held = 0;
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100920 }
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700921
Junio C Hamano87afe9a2011-12-16 14:39:37 -0800922 /* We are told to drain */
923 if (!input) {
924 *osize_p -= o;
925 return 0;
926 }
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100927
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700928 count = *isize_p;
Junio C Hamano8496f562011-12-16 15:44:18 -0800929 if (count || lf_to_crlf->has_held) {
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100930 size_t i;
Junio C Hamano8496f562011-12-16 15:44:18 -0800931 int was_cr = 0;
932
933 if (lf_to_crlf->has_held) {
934 was_cr = 1;
935 lf_to_crlf->has_held = 0;
936 }
937
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100938 for (i = 0; o < *osize_p && i < count; i++) {
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700939 char ch = input[i];
Junio C Hamano8496f562011-12-16 15:44:18 -0800940
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700941 if (ch == '\n') {
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100942 output[o++] = '\r';
Junio C Hamano8496f562011-12-16 15:44:18 -0800943 } else if (was_cr) {
944 /*
945 * Previous round saw CR and it is not followed
946 * by a LF; emit the CR before processing the
947 * current character.
948 */
949 output[o++] = '\r';
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700950 }
Junio C Hamano8496f562011-12-16 15:44:18 -0800951
952 /*
953 * We may have consumed the last output slot,
954 * in which case we need to break out of this
955 * loop; hold the current character before
956 * returning.
957 */
958 if (*osize_p <= o) {
959 lf_to_crlf->has_held = 1;
960 lf_to_crlf->held = ch;
961 continue; /* break but increment i */
962 }
963
964 if (ch == '\r') {
965 was_cr = 1;
966 continue;
967 }
968
969 was_cr = 0;
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700970 output[o++] = ch;
971 }
972
973 *osize_p -= o;
974 *isize_p -= i;
Junio C Hamano8496f562011-12-16 15:44:18 -0800975
976 if (!lf_to_crlf->has_held && was_cr) {
977 lf_to_crlf->has_held = 1;
978 lf_to_crlf->held = '\r';
979 }
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700980 }
981 return 0;
982}
983
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100984static void lf_to_crlf_free_fn(struct stream_filter *filter)
985{
986 free(filter);
987}
988
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700989static struct stream_filter_vtbl lf_to_crlf_vtbl = {
990 lf_to_crlf_filter_fn,
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100991 lf_to_crlf_free_fn,
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700992};
993
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100994static struct stream_filter *lf_to_crlf_filter(void)
995{
Junio C Hamano87afe9a2011-12-16 14:39:37 -0800996 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
Junio C Hamanoe322ee32011-05-20 16:47:56 -0700997
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100998 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +0100999 return (struct stream_filter *)lf_to_crlf;
1000}
Junio C Hamanob84c78392011-05-20 18:28:00 -07001001
1002/*
Junio C Hamanoa265a7f2011-05-21 18:28:41 -07001003 * Cascade filter
1004 */
1005#define FILTER_BUFFER 1024
1006struct cascade_filter {
1007 struct stream_filter filter;
1008 struct stream_filter *one;
1009 struct stream_filter *two;
1010 char buf[FILTER_BUFFER];
1011 int end, ptr;
1012};
1013
1014static int cascade_filter_fn(struct stream_filter *filter,
1015 const char *input, size_t *isize_p,
1016 char *output, size_t *osize_p)
1017{
1018 struct cascade_filter *cas = (struct cascade_filter *) filter;
1019 size_t filled = 0;
1020 size_t sz = *osize_p;
1021 size_t to_feed, remaining;
1022
1023 /*
1024 * input -- (one) --> buf -- (two) --> output
1025 */
1026 while (filled < sz) {
1027 remaining = sz - filled;
1028
1029 /* do we already have something to feed two with? */
1030 if (cas->ptr < cas->end) {
1031 to_feed = cas->end - cas->ptr;
1032 if (stream_filter(cas->two,
1033 cas->buf + cas->ptr, &to_feed,
1034 output + filled, &remaining))
1035 return -1;
1036 cas->ptr += (cas->end - cas->ptr) - to_feed;
1037 filled = sz - remaining;
1038 continue;
1039 }
1040
1041 /* feed one from upstream and have it emit into our buffer */
1042 to_feed = input ? *isize_p : 0;
1043 if (input && !to_feed)
1044 break;
1045 remaining = sizeof(cas->buf);
1046 if (stream_filter(cas->one,
1047 input, &to_feed,
1048 cas->buf, &remaining))
1049 return -1;
1050 cas->end = sizeof(cas->buf) - remaining;
1051 cas->ptr = 0;
1052 if (input) {
1053 size_t fed = *isize_p - to_feed;
1054 *isize_p -= fed;
1055 input += fed;
1056 }
1057
1058 /* do we know that we drained one completely? */
1059 if (input || cas->end)
1060 continue;
1061
1062 /* tell two to drain; we have nothing more to give it */
1063 to_feed = 0;
1064 remaining = sz - filled;
1065 if (stream_filter(cas->two,
1066 NULL, &to_feed,
1067 output + filled, &remaining))
1068 return -1;
1069 if (remaining == (sz - filled))
1070 break; /* completely drained two */
1071 filled = sz - remaining;
1072 }
1073 *osize_p -= filled;
1074 return 0;
1075}
1076
1077static void cascade_free_fn(struct stream_filter *filter)
1078{
1079 struct cascade_filter *cas = (struct cascade_filter *)filter;
1080 free_stream_filter(cas->one);
1081 free_stream_filter(cas->two);
1082 free(filter);
1083}
1084
1085static struct stream_filter_vtbl cascade_vtbl = {
1086 cascade_filter_fn,
1087 cascade_free_fn,
1088};
1089
1090static struct stream_filter *cascade_filter(struct stream_filter *one,
1091 struct stream_filter *two)
1092{
1093 struct cascade_filter *cascade;
1094
1095 if (!one || is_null_stream_filter(one))
1096 return two;
1097 if (!two || is_null_stream_filter(two))
1098 return one;
1099
1100 cascade = xmalloc(sizeof(*cascade));
1101 cascade->one = one;
1102 cascade->two = two;
1103 cascade->end = cascade->ptr = 0;
1104 cascade->filter.vtbl = &cascade_vtbl;
1105 return (struct stream_filter *)cascade;
1106}
1107
1108/*
Junio C Hamanob84c78392011-05-20 18:28:00 -07001109 * ident filter
1110 */
1111#define IDENT_DRAINING (-1)
1112#define IDENT_SKIPPING (-2)
1113struct ident_filter {
1114 struct stream_filter filter;
1115 struct strbuf left;
1116 int state;
1117 char ident[45]; /* ": x40 $" */
1118};
1119
1120static int is_foreign_ident(const char *str)
1121{
1122 int i;
1123
Jeff Kingae021d82014-06-18 15:47:50 -04001124 if (!skip_prefix(str, "$Id: ", &str))
Junio C Hamanob84c78392011-05-20 18:28:00 -07001125 return 0;
Jeff Kingae021d82014-06-18 15:47:50 -04001126 for (i = 0; str[i]; i++) {
Junio C Hamanob84c78392011-05-20 18:28:00 -07001127 if (isspace(str[i]) && str[i+1] != '$')
1128 return 1;
1129 }
1130 return 0;
1131}
1132
1133static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1134{
1135 size_t to_drain = ident->left.len;
1136
1137 if (*osize_p < to_drain)
1138 to_drain = *osize_p;
1139 if (to_drain) {
1140 memcpy(*output_p, ident->left.buf, to_drain);
1141 strbuf_remove(&ident->left, 0, to_drain);
1142 *output_p += to_drain;
1143 *osize_p -= to_drain;
1144 }
1145 if (!ident->left.len)
1146 ident->state = 0;
1147}
1148
1149static int ident_filter_fn(struct stream_filter *filter,
1150 const char *input, size_t *isize_p,
1151 char *output, size_t *osize_p)
1152{
1153 struct ident_filter *ident = (struct ident_filter *)filter;
1154 static const char head[] = "$Id";
1155
1156 if (!input) {
1157 /* drain upon eof */
1158 switch (ident->state) {
1159 default:
1160 strbuf_add(&ident->left, head, ident->state);
1161 case IDENT_SKIPPING:
1162 /* fallthru */
1163 case IDENT_DRAINING:
1164 ident_drain(ident, &output, osize_p);
1165 }
1166 return 0;
1167 }
1168
1169 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1170 int ch;
1171
1172 if (ident->state == IDENT_DRAINING) {
1173 ident_drain(ident, &output, osize_p);
1174 if (!*osize_p)
1175 break;
1176 continue;
1177 }
1178
1179 ch = *(input++);
1180 (*isize_p)--;
1181
1182 if (ident->state == IDENT_SKIPPING) {
1183 /*
1184 * Skipping until '$' or LF, but keeping them
1185 * in case it is a foreign ident.
1186 */
1187 strbuf_addch(&ident->left, ch);
1188 if (ch != '\n' && ch != '$')
1189 continue;
1190 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1191 strbuf_setlen(&ident->left, sizeof(head) - 1);
1192 strbuf_addstr(&ident->left, ident->ident);
1193 }
1194 ident->state = IDENT_DRAINING;
1195 continue;
1196 }
1197
1198 if (ident->state < sizeof(head) &&
1199 head[ident->state] == ch) {
1200 ident->state++;
1201 continue;
1202 }
1203
1204 if (ident->state)
1205 strbuf_add(&ident->left, head, ident->state);
1206 if (ident->state == sizeof(head) - 1) {
1207 if (ch != ':' && ch != '$') {
1208 strbuf_addch(&ident->left, ch);
1209 ident->state = 0;
1210 continue;
1211 }
1212
1213 if (ch == ':') {
1214 strbuf_addch(&ident->left, ch);
1215 ident->state = IDENT_SKIPPING;
1216 } else {
1217 strbuf_addstr(&ident->left, ident->ident);
1218 ident->state = IDENT_DRAINING;
1219 }
1220 continue;
1221 }
1222
1223 strbuf_addch(&ident->left, ch);
1224 ident->state = IDENT_DRAINING;
1225 }
1226 return 0;
1227}
1228
1229static void ident_free_fn(struct stream_filter *filter)
1230{
1231 struct ident_filter *ident = (struct ident_filter *)filter;
1232 strbuf_release(&ident->left);
1233 free(filter);
1234}
1235
1236static struct stream_filter_vtbl ident_vtbl = {
1237 ident_filter_fn,
1238 ident_free_fn,
1239};
1240
1241static struct stream_filter *ident_filter(const unsigned char *sha1)
1242{
1243 struct ident_filter *ident = xmalloc(sizeof(*ident));
1244
1245 sprintf(ident->ident, ": %s $", sha1_to_hex(sha1));
1246 strbuf_init(&ident->left, 0);
1247 ident->filter.vtbl = &ident_vtbl;
1248 ident->state = 0;
1249 return (struct stream_filter *)ident;
1250}
1251
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001252/*
Junio C Hamanob6691092011-05-20 14:33:31 -07001253 * Return an appropriately constructed filter for the path, or NULL if
1254 * the contents cannot be filtered without reading the whole thing
1255 * in-core.
1256 *
1257 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1258 * large binary blob you would want us not to slurp into the memory!
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001259 */
Junio C Hamanob6691092011-05-20 14:33:31 -07001260struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001261{
1262 struct conv_attrs ca;
1263 enum crlf_action crlf_action;
Junio C Hamanob84c78392011-05-20 18:28:00 -07001264 struct stream_filter *filter = NULL;
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001265
1266 convert_attrs(&ca, path);
1267
Junio C Hamanob84c78392011-05-20 18:28:00 -07001268 if (ca.drv && (ca.drv->smudge || ca.drv->clean))
1269 return filter;
1270
1271 if (ca.ident)
1272 filter = ident_filter(sha1);
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001273
1274 crlf_action = input_crlf_action(ca.crlf_action, ca.eol_attr);
Junio C Hamanob84c78392011-05-20 18:28:00 -07001275
Junio C Hamanob0d9c692011-05-20 16:14:32 -07001276 if ((crlf_action == CRLF_BINARY) || (crlf_action == CRLF_INPUT) ||
Junio C Hamanoa265a7f2011-05-21 18:28:41 -07001277 (crlf_action == CRLF_GUESS && auto_crlf == AUTO_CRLF_FALSE))
1278 filter = cascade_filter(filter, &null_filter_singleton);
1279
1280 else if (output_eol(crlf_action) == EOL_CRLF &&
1281 !(crlf_action == CRLF_AUTO || crlf_action == CRLF_GUESS))
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001282 filter = cascade_filter(filter, lf_to_crlf_filter());
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001283
Junio C Hamanob84c78392011-05-20 18:28:00 -07001284 return filter;
Junio C Hamanob6691092011-05-20 14:33:31 -07001285}
1286
1287void free_stream_filter(struct stream_filter *filter)
1288{
1289 filter->vtbl->free(filter);
1290}
1291
1292int stream_filter(struct stream_filter *filter,
1293 const char *input, size_t *isize_p,
1294 char *output, size_t *osize_p)
1295{
1296 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001297}