blob: 67d69b5c0e22a74e6dc46764beaff9978a78b85a [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
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010016/* 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
Junio C Hamanoc61dcff2011-05-09 13:12:57 -070021enum crlf_action {
Torsten Bögershausendf747b82016-02-10 17:24:41 +010022 CRLF_UNDEFINED,
23 CRLF_BINARY,
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +020024 CRLF_TEXT,
Torsten Bögershausendf747b82016-02-10 17:24:41 +010025 CRLF_TEXT_INPUT,
26 CRLF_TEXT_CRLF,
27 CRLF_AUTO,
28 CRLF_AUTO_INPUT,
29 CRLF_AUTO_CRLF
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +020030};
Junio C Hamano163b9592007-04-19 22:37:19 -070031
Linus Torvalds6c510be2007-02-13 11:07:23 -080032struct text_stat {
Dmitry Potapov28624192008-01-16 04:59:12 +030033 /* NUL, CR, LF and CRLF counts */
Torsten Bögershausen6e336a52016-02-10 17:24:43 +010034 unsigned nul, lonecr, lonelf, crlf;
Linus Torvalds6c510be2007-02-13 11:07:23 -080035
36 /* These are just approximations! */
37 unsigned printable, nonprintable;
38};
39
40static 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') {
Torsten Bögershausen6e336a52016-02-10 17:24:43 +010049 if (i+1 < size && buf[i+1] == '\n') {
Linus Torvalds6c510be2007-02-13 11:07:23 -080050 stats->crlf++;
Torsten Bögershausen6e336a52016-02-10 17:24:43 +010051 i++;
52 } else
53 stats->lonecr++;
Linus Torvalds6c510be2007-02-13 11:07:23 -080054 continue;
55 }
56 if (c == '\n') {
Torsten Bögershausen6e336a52016-02-10 17:24:43 +010057 stats->lonelf++;
Linus Torvalds6c510be2007-02-13 11:07:23 -080058 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;
Dmitry Potapov28624192008-01-16 04:59:12 +030069 case 0:
70 stats->nul++;
71 /* fall through */
Linus Torvalds6c510be2007-02-13 11:07:23 -080072 default:
73 stats->nonprintable++;
74 }
75 }
76 else
77 stats->printable++;
78 }
Dmitry Kakurinf9dd4bf2008-07-11 18:48:16 +020079
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--;
Linus Torvalds6c510be2007-02-13 11:07:23 -080083}
84
85/*
86 * The same heuristics as diff.c::mmfile_is_binary()
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010087 * We treat files with bare CR as binary
Linus Torvalds6c510be2007-02-13 11:07:23 -080088 */
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010089static int convert_is_binary(unsigned long size, const struct text_stat *stats)
Linus Torvalds6c510be2007-02-13 11:07:23 -080090{
Torsten Bögershausen6e336a52016-02-10 17:24:43 +010091 if (stats->lonecr)
Torsten Bögershausena7630bd2016-01-16 07:50:02 +010092 return 1;
Dmitry Potapov28624192008-01-16 04:59:12 +030093 if (stats->nul)
94 return 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -080095 if ((stats->printable >> 7) < stats->nonprintable)
96 return 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -080097 return 0;
98}
99
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100100static unsigned int gather_convert_stats(const char *data, unsigned long size)
101{
102 struct text_stat stats;
Torsten Bögershausen6e336a52016-02-10 17:24:43 +0100103 int ret = 0;
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100104 if (!data || !size)
105 return 0;
106 gather_stats(data, size, &stats);
107 if (convert_is_binary(size, &stats))
Torsten Bögershausen6e336a52016-02-10 17:24:43 +0100108 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;
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100115}
116
117static 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
135const 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
145const 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
Torsten Bögershausen4b4024f2016-02-05 17:13:25 +0100155static 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
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700168static enum eol output_eol(enum crlf_action crlf_action)
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200169{
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700170 switch (crlf_action) {
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200171 case CRLF_BINARY:
172 return EOL_UNSET;
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100173 case CRLF_TEXT_CRLF:
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200174 return EOL_CRLF;
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100175 case CRLF_TEXT_INPUT:
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200176 return EOL_LF;
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100177 case CRLF_UNDEFINED:
178 case CRLF_AUTO_CRLF:
Torsten Bögershausen65237282016-06-28 10:01:13 +0200179 return EOL_CRLF;
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100180 case CRLF_AUTO_INPUT:
Torsten Bögershausen65237282016-06-28 10:01:13 +0200181 return EOL_LF;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200182 case CRLF_TEXT:
183 case CRLF_AUTO:
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100184 /* fall through */
Torsten Bögershausen4b4024f2016-02-05 17:13:25 +0100185 return text_eol_is_crlf() ? EOL_CRLF : EOL_LF;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200186 }
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100187 warning("Illegal crlf_action %d\n", (int)crlf_action);
Junio C Hamanoec70f522011-05-09 12:52:12 -0700188 return core_eol;
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200189}
190
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700191static void check_safe_crlf(const char *path, enum crlf_action crlf_action,
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100192 struct text_stat *stats, enum safe_crlf checksafe)
193{
194 if (!checksafe)
195 return;
196
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700197 if (output_eol(crlf_action) == EOL_LF) {
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100198 /*
199 * CRLFs would not be restored by checkout:
200 * check if we'd remove CRLFs
201 */
202 if (stats->crlf) {
203 if (checksafe == SAFE_CRLF_WARN)
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200204 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 +0100205 else /* i.e. SAFE_CRLF_FAIL */
206 die("CRLF would be replaced by LF in %s.", path);
207 }
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700208 } else if (output_eol(crlf_action) == EOL_CRLF) {
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100209 /*
210 * CRLFs would be added by checkout:
211 * check if we have "naked" LFs
212 */
Torsten Bögershausen6e336a52016-02-10 17:24:43 +0100213 if (stats->lonelf) {
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100214 if (checksafe == SAFE_CRLF_WARN)
Eyvind Bernhardsen942e7742010-06-04 21:29:08 +0200215 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 +0100216 else /* i.e. SAFE_CRLF_FAIL */
217 die("LF would be replaced by CRLF in %s", path);
218 }
219 }
220}
221
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200222static int has_cr_in_index(const char *path)
223{
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200224 unsigned long sz;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200225 void *data;
226 int has_cr;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200227
Lukas Fleischer4982fd72013-04-13 15:28:32 +0200228 data = read_blob_data_from_cache(path, &sz);
229 if (!data)
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200230 return 0;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200231 has_cr = memchr(data, '\r', sz) != NULL;
232 free(data);
233 return has_cr;
234}
235
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200236static int crlf_to_git(const char *path, const char *src, size_t len,
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700237 struct strbuf *buf,
238 enum crlf_action crlf_action, enum safe_crlf checksafe)
Linus Torvalds6c510be2007-02-13 11:07:23 -0800239{
Linus Torvalds6c510be2007-02-13 11:07:23 -0800240 struct text_stat stats;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200241 char *dst;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800242
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700243 if (crlf_action == CRLF_BINARY ||
Jeff King4c3b57b2012-02-24 17:05:03 -0500244 (src && !len))
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200245 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800246
Jeff King4c3b57b2012-02-24 17:05:03 -0500247 /*
248 * If we are doing a dry-run and have no source buffer, there is
249 * nothing to analyze; we must assume we would convert.
250 */
251 if (!buf && !src)
252 return 1;
253
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200254 gather_stats(src, len, &stats);
Linus Torvalds6c510be2007-02-13 11:07:23 -0800255
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100256 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100257 if (convert_is_binary(len, &stats))
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200258 return 0;
Torsten Bögershausen65237282016-06-28 10:01:13 +0200259 /*
260 * If the file in the index has any CR in it, do not convert.
261 * This is the new safer autocrlf handling.
262 */
263 if (checksafe == SAFE_CRLF_RENORMALIZE)
264 checksafe = SAFE_CRLF_FALSE;
265 else if (has_cr_in_index(path))
266 return 0;
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700267 }
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700268 check_safe_crlf(path, crlf_action, &stats, checksafe);
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100269
Torsten Bögershausen6e336a52016-02-10 17:24:43 +0100270 /* Optimization: No CRLF? Nothing to convert, regardless. */
271 if (!stats.crlf)
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100272 return 0;
273
Jeff King92ac3192012-02-24 17:02:37 -0500274 /*
275 * At this point all of our source analysis is done, and we are sure we
276 * would convert. If we are in dry-run mode, we can give an answer.
277 */
278 if (!buf)
279 return 1;
280
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200281 /* only grow if not in place */
282 if (strbuf_avail(buf) + buf->len < len)
283 strbuf_grow(buf, len - buf->len);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200284 dst = buf->buf;
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100285 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
Junio C Hamano163b9592007-04-19 22:37:19 -0700286 /*
287 * If we guessed, we already know we rejected a file with
288 * lone CR, and we can strip a CR without looking at what
289 * follow it.
290 */
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700291 do {
Alex Riesenac78e542007-04-19 02:05:03 +0200292 unsigned char c = *src++;
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700293 if (c != '\r')
Alex Riesenac78e542007-04-19 02:05:03 +0200294 *dst++ = c;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200295 } while (--len);
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700296 } else {
297 do {
Alex Riesenac78e542007-04-19 02:05:03 +0200298 unsigned char c = *src++;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200299 if (! (c == '\r' && (1 < len && *src == '\n')))
Alex Riesenac78e542007-04-19 02:05:03 +0200300 *dst++ = c;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200301 } while (--len);
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700302 }
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200303 strbuf_setlen(buf, dst - buf->buf);
304 return 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800305}
306
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200307static int crlf_to_worktree(const char *path, const char *src, size_t len,
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700308 struct strbuf *buf, enum crlf_action crlf_action)
Linus Torvalds6c510be2007-02-13 11:07:23 -0800309{
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200310 char *to_free = NULL;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800311 struct text_stat stats;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800312
Junio C Hamanoc61dcff2011-05-09 13:12:57 -0700313 if (!len || output_eol(crlf_action) != EOL_CRLF)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200314 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800315
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200316 gather_stats(src, len, &stats);
Linus Torvalds6c510be2007-02-13 11:07:23 -0800317
Torsten Bögershausen6e336a52016-02-10 17:24:43 +0100318 /* No "naked" LF? Nothing to convert, regardless. */
319 if (!stats.lonelf)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200320 return 0;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800321
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100322 if (crlf_action == CRLF_AUTO || crlf_action == CRLF_AUTO_INPUT || crlf_action == CRLF_AUTO_CRLF) {
Torsten Bögershausen65237282016-06-28 10:01:13 +0200323 /* If we have any CR or CRLF line endings, we do not touch it */
324 /* This is the new safer autocrlf-handling */
325 if (stats.lonecr || stats.crlf )
326 return 0;
Finn Arne Gangstadc4805392010-05-12 00:37:57 +0200327
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100328 if (convert_is_binary(len, &stats))
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200329 return 0;
Junio C Hamano201ac8e2007-04-15 13:35:45 -0700330 }
Linus Torvalds6c510be2007-02-13 11:07:23 -0800331
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200332 /* are we "faking" in place editing ? */
333 if (src == buf->buf)
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200334 to_free = strbuf_detach(buf, NULL);
Alex Riesenac78e542007-04-19 02:05:03 +0200335
Torsten Bögershausen6e336a52016-02-10 17:24:43 +0100336 strbuf_grow(buf, len + stats.lonelf);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200337 for (;;) {
338 const char *nl = memchr(src, '\n', len);
339 if (!nl)
340 break;
341 if (nl > src && nl[-1] == '\r') {
342 strbuf_add(buf, src, nl + 1 - src);
343 } else {
344 strbuf_add(buf, src, nl - src);
345 strbuf_addstr(buf, "\r\n");
346 }
347 len -= nl + 1 - src;
348 src = nl + 1;
349 }
350 strbuf_add(buf, src, len);
Linus Torvalds6c510be2007-02-13 11:07:23 -0800351
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200352 free(to_free);
353 return 1;
Linus Torvalds6c510be2007-02-13 11:07:23 -0800354}
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700355
Johannes Sixt546bb582007-10-19 21:48:06 +0200356struct filter_params {
357 const char *src;
358 unsigned long size;
Steffen Prohaska9035d752014-08-26 17:23:25 +0200359 int fd;
Johannes Sixt546bb582007-10-19 21:48:06 +0200360 const char *cmd;
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800361 const char *path;
Johannes Sixt546bb582007-10-19 21:48:06 +0200362};
363
Steffen Prohaska9035d752014-08-26 17:23:25 +0200364static int filter_buffer_or_fd(int in, int out, void *data)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700365{
366 /*
367 * Spawn cmd and feed the buffer contents through its stdin.
368 */
René Scharfed3180272014-08-19 21:09:35 +0200369 struct child_process child_process = CHILD_PROCESS_INIT;
Johannes Sixt546bb582007-10-19 21:48:06 +0200370 struct filter_params *params = (struct filter_params *)data;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700371 int write_err, status;
Gary V. Vaughan66dbfd52010-05-14 09:31:33 +0000372 const char *argv[] = { NULL, NULL };
373
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800374 /* apply % substitution to cmd */
375 struct strbuf cmd = STRBUF_INIT;
376 struct strbuf path = STRBUF_INIT;
377 struct strbuf_expand_dict_entry dict[] = {
378 { "f", NULL, },
379 { NULL, NULL, },
380 };
381
382 /* quote the path to preserve spaces, etc. */
383 sq_quote_buf(&path, params->path);
384 dict[0].value = path.buf;
385
386 /* expand all %f with the quoted path */
387 strbuf_expand(&cmd, params->cmd, strbuf_expand_dict_cb, &dict);
388 strbuf_release(&path);
389
390 argv[0] = cmd.buf;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700391
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200392 child_process.argv = argv;
Jeff Kingac0ba182009-12-30 05:53:57 -0500393 child_process.use_shell = 1;
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200394 child_process.in = -1;
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800395 child_process.out = out;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700396
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200397 if (start_command(&child_process))
Johannes Sixt546bb582007-10-19 21:48:06 +0200398 return error("cannot fork to run external filter %s", params->cmd);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700399
Jehan Bing6424c2a2012-02-20 12:53:37 -0800400 sigchain_push(SIGPIPE, SIG_IGN);
401
Steffen Prohaska9035d752014-08-26 17:23:25 +0200402 if (params->src) {
Junio C Hamano0c4dd672015-05-19 11:08:23 -0700403 write_err = (write_in_full(child_process.in,
404 params->src, params->size) < 0);
405 if (errno == EPIPE)
406 write_err = 0;
Steffen Prohaska9035d752014-08-26 17:23:25 +0200407 } else {
408 write_err = copy_fd(params->fd, child_process.in);
Junio C Hamano0c4dd672015-05-19 11:08:23 -0700409 if (write_err == COPY_WRITE_ERROR && errno == EPIPE)
410 write_err = 0;
Steffen Prohaska9035d752014-08-26 17:23:25 +0200411 }
412
Johannes Sixtdc1bfdc2007-10-19 21:47:55 +0200413 if (close(child_process.in))
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700414 write_err = 1;
415 if (write_err)
Johannes Sixt546bb582007-10-19 21:48:06 +0200416 error("cannot feed the input to external filter %s", params->cmd);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700417
Jehan Bing6424c2a2012-02-20 12:53:37 -0800418 sigchain_pop(SIGPIPE);
419
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700420 status = finish_command(&child_process);
421 if (status)
Johannes Sixt5709e032009-07-04 21:26:39 +0200422 error("external filter %s failed %d", params->cmd, status);
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800423
424 strbuf_release(&cmd);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700425 return (write_err || status);
426}
427
Steffen Prohaska9035d752014-08-26 17:23:25 +0200428static int apply_filter(const char *path, const char *src, size_t len, int fd,
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200429 struct strbuf *dst, const char *cmd)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700430{
431 /*
432 * Create a pipeline to have the command filter the buffer's
433 * contents.
434 *
435 * (child --> cmd) --> us
436 */
Johannes Sixt546bb582007-10-19 21:48:06 +0200437 int ret = 1;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500438 struct strbuf nbuf = STRBUF_INIT;
Johannes Sixt546bb582007-10-19 21:48:06 +0200439 struct async async;
440 struct filter_params params;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700441
Lars Schneider1a8630d2016-01-29 09:21:37 +0100442 if (!cmd || !*cmd)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200443 return 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700444
Jeff King92ac3192012-02-24 17:02:37 -0500445 if (!dst)
446 return 1;
447
Johannes Sixt546bb582007-10-19 21:48:06 +0200448 memset(&async, 0, sizeof(async));
Steffen Prohaska9035d752014-08-26 17:23:25 +0200449 async.proc = filter_buffer_or_fd;
Johannes Sixt546bb582007-10-19 21:48:06 +0200450 async.data = &params;
Erik Faye-Lundae6a5602010-02-05 12:57:38 -0800451 async.out = -1;
Johannes Sixt546bb582007-10-19 21:48:06 +0200452 params.src = src;
453 params.size = len;
Steffen Prohaska9035d752014-08-26 17:23:25 +0200454 params.fd = fd;
Johannes Sixt546bb582007-10-19 21:48:06 +0200455 params.cmd = cmd;
Pete Wyckoffa2b665d2010-12-22 06:40:13 -0800456 params.path = path;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700457
458 fflush(NULL);
Johannes Sixt546bb582007-10-19 21:48:06 +0200459 if (start_async(&async))
460 return 0; /* error was already reported */
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700461
Johannes Sixt546bb582007-10-19 21:48:06 +0200462 if (strbuf_read(&nbuf, async.out, len) < 0) {
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200463 error("read from external filter %s failed", cmd);
464 ret = 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700465 }
Johannes Sixt546bb582007-10-19 21:48:06 +0200466 if (close(async.out)) {
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200467 error("read from external filter %s failed", cmd);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200468 ret = 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700469 }
Johannes Sixt546bb582007-10-19 21:48:06 +0200470 if (finish_async(&async)) {
471 error("external filter %s failed", cmd);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200472 ret = 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700473 }
474
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200475 if (ret) {
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200476 strbuf_swap(dst, &nbuf);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200477 }
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200478 strbuf_release(&nbuf);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200479 return ret;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700480}
481
482static struct convert_driver {
483 const char *name;
484 struct convert_driver *next;
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400485 const char *smudge;
486 const char *clean;
Jehan Bing36daaac2012-02-16 17:19:03 -0800487 int required;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700488} *user_convert, **user_convert_tail;
489
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100490static int read_convert_config(const char *var, const char *value, void *cb)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700491{
Jeff Kingd731f0a2013-01-23 01:24:23 -0500492 const char *key, *name;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700493 int namelen;
494 struct convert_driver *drv;
495
496 /*
497 * External conversion drivers are configured using
498 * "filter.<name>.variable".
499 */
Jeff Kingd731f0a2013-01-23 01:24:23 -0500500 if (parse_config_key(var, "filter", &name, &namelen, &key) < 0 || !name)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700501 return 0;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700502 for (drv = user_convert; drv; drv = drv->next)
503 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
504 break;
505 if (!drv) {
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700506 drv = xcalloc(1, sizeof(struct convert_driver));
Pierre Habouzit182af832007-09-16 00:32:36 +0200507 drv->name = xmemdupz(name, namelen);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700508 *user_convert_tail = drv;
509 user_convert_tail = &(drv->next);
510 }
511
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700512 /*
513 * filter.<name>.smudge and filter.<name>.clean specifies
514 * the command line:
515 *
516 * command-line
517 *
518 * The command-line will not be interpolated in any way.
519 */
520
Jeff Kingd731f0a2013-01-23 01:24:23 -0500521 if (!strcmp("smudge", key))
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400522 return git_config_string(&drv->smudge, var, value);
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700523
Jeff Kingd731f0a2013-01-23 01:24:23 -0500524 if (!strcmp("clean", key))
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400525 return git_config_string(&drv->clean, var, value);
526
Jeff Kingd731f0a2013-01-23 01:24:23 -0500527 if (!strcmp("required", key)) {
Jehan Bing36daaac2012-02-16 17:19:03 -0800528 drv->required = git_config_bool(var, value);
529 return 0;
530 }
531
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700532 return 0;
533}
534
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700535static int count_ident(const char *cp, unsigned long size)
536{
537 /*
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100538 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700539 */
540 int cnt = 0;
541 char ch;
542
543 while (size) {
544 ch = *cp++;
545 size--;
546 if (ch != '$')
547 continue;
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100548 if (size < 3)
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700549 break;
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100550 if (memcmp("Id", cp, 2))
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700551 continue;
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100552 ch = cp[2];
553 cp += 3;
554 size -= 3;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700555 if (ch == '$')
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100556 cnt++; /* $Id$ */
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700557 if (ch != ':')
558 continue;
559
560 /*
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100561 * "$Id: ... "; scan up to the closing dollar sign and discard.
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700562 */
563 while (size) {
564 ch = *cp++;
565 size--;
566 if (ch == '$') {
567 cnt++;
568 break;
569 }
Henrik Grubbströma9f30492010-04-06 14:46:37 +0200570 if (ch == '\n')
571 break;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700572 }
573 }
574 return cnt;
575}
576
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200577static int ident_to_git(const char *path, const char *src, size_t len,
578 struct strbuf *buf, int ident)
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700579{
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200580 char *dst, *dollar;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700581
Jeff King4c3b57b2012-02-24 17:05:03 -0500582 if (!ident || (src && !count_ident(src, len)))
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200583 return 0;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700584
Jeff King92ac3192012-02-24 17:02:37 -0500585 if (!buf)
586 return 1;
587
Pierre Habouzit90d16ec2007-10-05 10:11:59 +0200588 /* only grow if not in place */
589 if (strbuf_avail(buf) + buf->len < len)
590 strbuf_grow(buf, len - buf->len);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200591 dst = buf->buf;
592 for (;;) {
593 dollar = memchr(src, '$', len);
594 if (!dollar)
595 break;
Thomas Rast77321182011-08-29 22:06:04 +0200596 memmove(dst, src, dollar + 1 - src);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200597 dst += dollar + 1 - src;
598 len -= dollar + 1 - src;
599 src = dollar + 1;
600
601 if (len > 3 && !memcmp(src, "Id:", 3)) {
602 dollar = memchr(src + 3, '$', len - 3);
603 if (!dollar)
604 break;
Henrik Grubbströma9f30492010-04-06 14:46:37 +0200605 if (memchr(src + 3, '\n', dollar - src - 3)) {
606 /* Line break before the next dollar. */
607 continue;
608 }
609
Andy Parkinsaf9b54b2007-05-14 14:37:25 +0100610 memcpy(dst, "Id$", 3);
611 dst += 3;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200612 len -= dollar + 1 - src;
613 src = dollar + 1;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700614 }
615 }
Thomas Rast77321182011-08-29 22:06:04 +0200616 memmove(dst, src, len);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200617 strbuf_setlen(buf, dst + len - buf->buf);
618 return 1;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700619}
620
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200621static int ident_to_worktree(const char *path, const char *src, size_t len,
622 struct strbuf *buf, int ident)
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700623{
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700624 unsigned char sha1[20];
Henrik Grubbström07814d92010-04-06 14:46:38 +0200625 char *to_free = NULL, *dollar, *spc;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200626 int cnt;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700627
628 if (!ident)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200629 return 0;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700630
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200631 cnt = count_ident(src, len);
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700632 if (!cnt)
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200633 return 0;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700634
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200635 /* are we "faking" in place editing ? */
636 if (src == buf->buf)
Pierre Habouzitb315c5c2007-09-27 12:58:23 +0200637 to_free = strbuf_detach(buf, NULL);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200638 hash_sha1_file(src, len, "blob", sha1);
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700639
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200640 strbuf_grow(buf, len + cnt * 43);
641 for (;;) {
642 /* step 1: run to the next '$' */
643 dollar = memchr(src, '$', len);
644 if (!dollar)
645 break;
646 strbuf_add(buf, src, dollar + 1 - src);
647 len -= dollar + 1 - src;
648 src = dollar + 1;
649
650 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
651 if (len < 3 || memcmp("Id", src, 2))
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700652 continue;
653
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200654 /* step 3: skip over Id$ or Id:xxxxx$ */
655 if (src[2] == '$') {
656 src += 3;
657 len -= 3;
658 } else if (src[2] == ':') {
Andy Parkinsc23290d2007-05-25 11:50:08 +0100659 /*
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200660 * It's possible that an expanded Id has crept its way into the
Henrik Grubbström07814d92010-04-06 14:46:38 +0200661 * repository, we cope with that by stripping the expansion out.
662 * This is probably not a good idea, since it will cause changes
663 * on checkout, which won't go away by stash, but let's keep it
664 * for git-style ids.
Andy Parkinsc23290d2007-05-25 11:50:08 +0100665 */
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200666 dollar = memchr(src + 3, '$', len - 3);
667 if (!dollar) {
668 /* incomplete keyword, no more '$', so just quit the loop */
669 break;
670 }
671
Henrik Grubbströma9f30492010-04-06 14:46:37 +0200672 if (memchr(src + 3, '\n', dollar - src - 3)) {
673 /* Line break before the next dollar. */
674 continue;
675 }
676
Henrik Grubbström07814d92010-04-06 14:46:38 +0200677 spc = memchr(src + 4, ' ', dollar - src - 4);
678 if (spc && spc < dollar-1) {
679 /* There are spaces in unexpected places.
680 * This is probably an id from some other
681 * versioning system. Keep it for now.
682 */
683 continue;
684 }
685
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200686 len -= dollar + 1 - src;
687 src = dollar + 1;
688 } else {
689 /* it wasn't a "Id$" or "Id:xxxx$" */
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700690 continue;
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200691 }
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700692
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200693 /* step 4: substitute */
694 strbuf_addstr(buf, "Id: ");
695 strbuf_add(buf, sha1_to_hex(sha1), 40);
696 strbuf_addstr(buf, " $");
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700697 }
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200698 strbuf_add(buf, src, len);
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700699
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200700 free(to_free);
701 return 1;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700702}
703
Torsten Bögershausen92cce132016-02-05 17:13:22 +0100704static enum crlf_action git_path_check_crlf(struct git_attr_check *check)
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700705{
Junio C Hamano6073ee82007-04-20 23:44:02 -0700706 const char *value = check->value;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700707
Junio C Hamano6073ee82007-04-20 23:44:02 -0700708 if (ATTR_TRUE(value))
709 return CRLF_TEXT;
710 else if (ATTR_FALSE(value))
711 return CRLF_BINARY;
712 else if (ATTR_UNSET(value))
713 ;
714 else if (!strcmp(value, "input"))
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100715 return CRLF_TEXT_INPUT;
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200716 else if (!strcmp(value, "auto"))
717 return CRLF_AUTO;
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100718 return CRLF_UNDEFINED;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700719}
720
Torsten Bögershausen92cce132016-02-05 17:13:22 +0100721static enum eol git_path_check_eol(struct git_attr_check *check)
Eyvind Bernhardsenfd6cce92010-05-19 22:43:10 +0200722{
723 const char *value = check->value;
724
725 if (ATTR_UNSET(value))
726 ;
727 else if (!strcmp(value, "lf"))
728 return EOL_LF;
729 else if (!strcmp(value, "crlf"))
730 return EOL_CRLF;
731 return EOL_UNSET;
732}
733
Torsten Bögershausen92cce132016-02-05 17:13:22 +0100734static struct convert_driver *git_path_check_convert(struct git_attr_check *check)
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700735{
736 const char *value = check->value;
737 struct convert_driver *drv;
738
739 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
740 return NULL;
741 for (drv = user_convert; drv; drv = drv->next)
742 if (!strcmp(value, drv->name))
743 return drv;
744 return NULL;
745}
746
Torsten Bögershausen92cce132016-02-05 17:13:22 +0100747static int git_path_check_ident(struct git_attr_check *check)
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700748{
749 const char *value = check->value;
750
751 return !!ATTR_TRUE(value);
752}
753
Junio C Hamano3bfba202011-05-09 13:58:31 -0700754struct conv_attrs {
755 struct convert_driver *drv;
Torsten Bögershausenbb211b42016-02-05 17:13:23 +0100756 enum crlf_action attr_action; /* What attr says */
757 enum crlf_action crlf_action; /* When no attr is set, use core.autocrlf */
Junio C Hamano3bfba202011-05-09 13:58:31 -0700758 int ident;
759};
760
Junio C Hamano83295962011-05-09 11:23:04 -0700761static const char *conv_attr_name[] = {
762 "crlf", "ident", "filter", "eol", "text",
763};
764#define NUM_CONV_ATTRS ARRAY_SIZE(conv_attr_name)
765
Junio C Hamano3bfba202011-05-09 13:58:31 -0700766static void convert_attrs(struct conv_attrs *ca, const char *path)
Junio C Hamano83295962011-05-09 11:23:04 -0700767{
768 int i;
769 static struct git_attr_check ccheck[NUM_CONV_ATTRS];
770
771 if (!ccheck[0].attr) {
772 for (i = 0; i < NUM_CONV_ATTRS; i++)
773 ccheck[i].attr = git_attr(conv_attr_name[i]);
774 user_convert_tail = &user_convert;
775 git_config(read_convert_config, NULL);
776 }
Junio C Hamano3bfba202011-05-09 13:58:31 -0700777
Michael Haggertyd932f4e2011-08-04 06:36:33 +0200778 if (!git_check_attr(path, NUM_CONV_ATTRS, ccheck)) {
Torsten Bögershausenbb211b42016-02-05 17:13:23 +0100779 ca->crlf_action = git_path_check_crlf(ccheck + 4);
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100780 if (ca->crlf_action == CRLF_UNDEFINED)
Torsten Bögershausen92cce132016-02-05 17:13:22 +0100781 ca->crlf_action = git_path_check_crlf(ccheck + 0);
Torsten Bögershausenbb211b42016-02-05 17:13:23 +0100782 ca->attr_action = ca->crlf_action;
Torsten Bögershausen92cce132016-02-05 17:13:22 +0100783 ca->ident = git_path_check_ident(ccheck + 1);
784 ca->drv = git_path_check_convert(ccheck + 2);
Torsten Bögershausen817a0c72016-02-23 18:07:19 +0100785 if (ca->crlf_action != CRLF_BINARY) {
786 enum eol eol_attr = git_path_check_eol(ccheck + 3);
Torsten Bögershausen65237282016-06-28 10:01:13 +0200787 if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_LF)
788 ca->crlf_action = CRLF_AUTO_INPUT;
789 else if (ca->crlf_action == CRLF_AUTO && eol_attr == EOL_CRLF)
790 ca->crlf_action = CRLF_AUTO_CRLF;
791 else if (eol_attr == EOL_LF)
Torsten Bögershausen817a0c72016-02-23 18:07:19 +0100792 ca->crlf_action = CRLF_TEXT_INPUT;
793 else if (eol_attr == EOL_CRLF)
794 ca->crlf_action = CRLF_TEXT_CRLF;
795 }
796 ca->attr_action = ca->crlf_action;
Junio C Hamano3bfba202011-05-09 13:58:31 -0700797 } else {
798 ca->drv = NULL;
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100799 ca->crlf_action = CRLF_UNDEFINED;
Junio C Hamano3bfba202011-05-09 13:58:31 -0700800 ca->ident = 0;
801 }
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100802 if (ca->crlf_action == CRLF_TEXT)
803 ca->crlf_action = text_eol_is_crlf() ? CRLF_TEXT_CRLF : CRLF_TEXT_INPUT;
804 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_FALSE)
805 ca->crlf_action = CRLF_BINARY;
806 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_TRUE)
807 ca->crlf_action = CRLF_AUTO_CRLF;
808 if (ca->crlf_action == CRLF_UNDEFINED && auto_crlf == AUTO_CRLF_INPUT)
809 ca->crlf_action = CRLF_AUTO_INPUT;
Junio C Hamano83295962011-05-09 11:23:04 -0700810}
811
Steffen Prohaska9035d752014-08-26 17:23:25 +0200812int would_convert_to_git_filter_fd(const char *path)
813{
814 struct conv_attrs ca;
815
816 convert_attrs(&ca, path);
817 if (!ca.drv)
818 return 0;
819
820 /*
821 * Apply a filter to an fd only if the filter is required to succeed.
822 * We must die if the filter fails, because the original data before
823 * filtering is not available.
824 */
825 if (!ca.drv->required)
826 return 0;
827
828 return apply_filter(path, NULL, 0, -1, NULL, ca.drv->clean);
829}
830
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100831const char *get_convert_attr_ascii(const char *path)
832{
833 struct conv_attrs ca;
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100834
835 convert_attrs(&ca, path);
Torsten Bögershausenbb211b42016-02-05 17:13:23 +0100836 switch (ca.attr_action) {
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100837 case CRLF_UNDEFINED:
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100838 return "";
839 case CRLF_BINARY:
840 return "-text";
841 case CRLF_TEXT:
842 return "text";
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100843 case CRLF_TEXT_INPUT:
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100844 return "text eol=lf";
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100845 case CRLF_TEXT_CRLF:
846 return "text eol=crlf";
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100847 case CRLF_AUTO:
848 return "text=auto";
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100849 case CRLF_AUTO_CRLF:
Torsten Bögershausen65237282016-06-28 10:01:13 +0200850 return "text=auto eol=crlf";
Torsten Bögershausendf747b82016-02-10 17:24:41 +0100851 case CRLF_AUTO_INPUT:
Torsten Bögershausen65237282016-06-28 10:01:13 +0200852 return "text=auto eol=lf";
Torsten Bögershausena7630bd2016-01-16 07:50:02 +0100853 }
854 return "";
855}
856
Steffen Prohaska21e5ad52008-02-06 12:25:58 +0100857int convert_to_git(const char *path, const char *src, size_t len,
858 struct strbuf *dst, enum safe_crlf checksafe)
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700859{
Junio C Hamano3bfba202011-05-09 13:58:31 -0700860 int ret = 0;
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400861 const char *filter = NULL;
Jehan Bing36daaac2012-02-16 17:19:03 -0800862 int required = 0;
Junio C Hamano3bfba202011-05-09 13:58:31 -0700863 struct conv_attrs ca;
Junio C Hamano6073ee82007-04-20 23:44:02 -0700864
Junio C Hamano3bfba202011-05-09 13:58:31 -0700865 convert_attrs(&ca, path);
Jehan Bing36daaac2012-02-16 17:19:03 -0800866 if (ca.drv) {
Junio C Hamano3bfba202011-05-09 13:58:31 -0700867 filter = ca.drv->clean;
Jehan Bing36daaac2012-02-16 17:19:03 -0800868 required = ca.drv->required;
869 }
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700870
Steffen Prohaska9035d752014-08-26 17:23:25 +0200871 ret |= apply_filter(path, src, len, -1, dst, filter);
Jehan Bing36daaac2012-02-16 17:19:03 -0800872 if (!ret && required)
873 die("%s: clean filter '%s' failed", path, ca.drv->name);
874
Jeff King92ac3192012-02-24 17:02:37 -0500875 if (ret && dst) {
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200876 src = dst->buf;
877 len = dst->len;
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700878 }
Junio C Hamano3bfba202011-05-09 13:58:31 -0700879 ret |= crlf_to_git(path, src, len, dst, ca.crlf_action, checksafe);
Jeff King92ac3192012-02-24 17:02:37 -0500880 if (ret && dst) {
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200881 src = dst->buf;
882 len = dst->len;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700883 }
Junio C Hamano3bfba202011-05-09 13:58:31 -0700884 return ret | ident_to_git(path, src, len, dst, ca.ident);
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700885}
886
Steffen Prohaska9035d752014-08-26 17:23:25 +0200887void convert_to_git_filter_fd(const char *path, int fd, struct strbuf *dst,
888 enum safe_crlf checksafe)
889{
890 struct conv_attrs ca;
891 convert_attrs(&ca, path);
892
893 assert(ca.drv);
894 assert(ca.drv->clean);
895
896 if (!apply_filter(path, NULL, 0, fd, dst, ca.drv->clean))
897 die("%s: clean filter '%s' failed", path, ca.drv->name);
898
Steffen Prohaska9035d752014-08-26 17:23:25 +0200899 crlf_to_git(path, dst->buf, dst->len, dst, ca.crlf_action, checksafe);
900 ident_to_git(path, dst->buf, dst->len, dst, ca.ident);
901}
902
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200903static int convert_to_working_tree_internal(const char *path, const char *src,
904 size_t len, struct strbuf *dst,
905 int normalizing)
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700906{
Jehan Bing36daaac2012-02-16 17:19:03 -0800907 int ret = 0, ret_filter = 0;
Brian Hetrocd8be6c2008-07-05 01:24:42 -0400908 const char *filter = NULL;
Jehan Bing36daaac2012-02-16 17:19:03 -0800909 int required = 0;
Junio C Hamano3bfba202011-05-09 13:58:31 -0700910 struct conv_attrs ca;
Junio C Hamano6073ee82007-04-20 23:44:02 -0700911
Junio C Hamano3bfba202011-05-09 13:58:31 -0700912 convert_attrs(&ca, path);
Jehan Bing36daaac2012-02-16 17:19:03 -0800913 if (ca.drv) {
Junio C Hamano3bfba202011-05-09 13:58:31 -0700914 filter = ca.drv->smudge;
Jehan Bing36daaac2012-02-16 17:19:03 -0800915 required = ca.drv->required;
916 }
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700917
Junio C Hamano3bfba202011-05-09 13:58:31 -0700918 ret |= ident_to_worktree(path, src, len, dst, ca.ident);
Pierre Habouzit5ecd2932007-09-16 15:51:04 +0200919 if (ret) {
920 src = dst->buf;
921 len = dst->len;
Junio C Hamano3fed15f2007-04-21 19:09:02 -0700922 }
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200923 /*
924 * CRLF conversion can be skipped if normalizing, unless there
925 * is a smudge filter. The filter might expect CRLFs.
926 */
927 if (filter || !normalizing) {
Junio C Hamano3bfba202011-05-09 13:58:31 -0700928 ret |= crlf_to_worktree(path, src, len, dst, ca.crlf_action);
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200929 if (ret) {
930 src = dst->buf;
931 len = dst->len;
932 }
Junio C Hamanoaa4ed402007-04-21 03:14:13 -0700933 }
Jehan Bing36daaac2012-02-16 17:19:03 -0800934
Steffen Prohaska9035d752014-08-26 17:23:25 +0200935 ret_filter = apply_filter(path, src, len, -1, dst, filter);
Jehan Bing36daaac2012-02-16 17:19:03 -0800936 if (!ret_filter && required)
937 die("%s: smudge filter %s failed", path, ca.drv->name);
938
939 return ret | ret_filter;
Junio C Hamano35ebfd62007-04-12 22:30:05 -0700940}
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200941
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200942int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
943{
944 return convert_to_working_tree_internal(path, src, len, dst, 0);
945}
946
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200947int renormalize_buffer(const char *path, const char *src, size_t len, struct strbuf *dst)
948{
Eyvind Bernhardsen43dd2332010-07-02 21:20:49 +0200949 int ret = convert_to_working_tree_internal(path, src, len, dst, 1);
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200950 if (ret) {
951 src = dst->buf;
952 len = dst->len;
953 }
Torsten Bögershausen65237282016-06-28 10:01:13 +0200954 return ret | convert_to_git(path, src, len, dst, SAFE_CRLF_RENORMALIZE);
Eyvind Bernhardsenf217f0e2010-07-02 21:20:47 +0200955}
Junio C Hamanodd8e9122011-05-12 14:31:08 -0700956
Junio C Hamanob6691092011-05-20 14:33:31 -0700957/*****************************************************************
958 *
Ondřej Bílka749f7632013-07-22 23:02:23 +0200959 * Streaming conversion support
Junio C Hamanob6691092011-05-20 14:33:31 -0700960 *
961 *****************************************************************/
962
963typedef int (*filter_fn)(struct stream_filter *,
964 const char *input, size_t *isize_p,
965 char *output, size_t *osize_p);
966typedef void (*free_fn)(struct stream_filter *);
967
968struct stream_filter_vtbl {
969 filter_fn filter;
970 free_fn free;
971};
972
973struct stream_filter {
974 struct stream_filter_vtbl *vtbl;
975};
976
977static int null_filter_fn(struct stream_filter *filter,
978 const char *input, size_t *isize_p,
979 char *output, size_t *osize_p)
980{
Junio C Hamano4ae66702011-05-21 14:05:51 -0700981 size_t count;
982
983 if (!input)
984 return 0; /* we do not keep any states */
985 count = *isize_p;
Junio C Hamanob6691092011-05-20 14:33:31 -0700986 if (*osize_p < count)
987 count = *osize_p;
988 if (count) {
989 memmove(output, input, count);
990 *isize_p -= count;
991 *osize_p -= count;
992 }
993 return 0;
994}
995
996static void null_free_fn(struct stream_filter *filter)
997{
998 ; /* nothing -- null instances are shared */
999}
1000
1001static struct stream_filter_vtbl null_vtbl = {
1002 null_filter_fn,
1003 null_free_fn,
1004};
1005
1006static struct stream_filter null_filter_singleton = {
1007 &null_vtbl,
1008};
1009
1010int is_null_stream_filter(struct stream_filter *filter)
1011{
1012 return filter == &null_filter_singleton;
1013}
1014
Junio C Hamanob84c78392011-05-20 18:28:00 -07001015
1016/*
1017 * LF-to-CRLF filter
1018 */
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001019
1020struct lf_to_crlf_filter {
1021 struct stream_filter filter;
Junio C Hamano8496f562011-12-16 15:44:18 -08001022 unsigned has_held:1;
1023 char held;
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001024};
1025
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001026static int lf_to_crlf_filter_fn(struct stream_filter *filter,
1027 const char *input, size_t *isize_p,
1028 char *output, size_t *osize_p)
1029{
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001030 size_t count, o = 0;
1031 struct lf_to_crlf_filter *lf_to_crlf = (struct lf_to_crlf_filter *)filter;
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001032
Junio C Hamano8496f562011-12-16 15:44:18 -08001033 /*
1034 * We may be holding onto the CR to see if it is followed by a
1035 * LF, in which case we would need to go to the main loop.
1036 * Otherwise, just emit it to the output stream.
1037 */
1038 if (lf_to_crlf->has_held && (lf_to_crlf->held != '\r' || !input)) {
1039 output[o++] = lf_to_crlf->held;
1040 lf_to_crlf->has_held = 0;
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001041 }
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001042
Junio C Hamano87afe9a2011-12-16 14:39:37 -08001043 /* We are told to drain */
1044 if (!input) {
1045 *osize_p -= o;
1046 return 0;
1047 }
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001048
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001049 count = *isize_p;
Junio C Hamano8496f562011-12-16 15:44:18 -08001050 if (count || lf_to_crlf->has_held) {
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001051 size_t i;
Junio C Hamano8496f562011-12-16 15:44:18 -08001052 int was_cr = 0;
1053
1054 if (lf_to_crlf->has_held) {
1055 was_cr = 1;
1056 lf_to_crlf->has_held = 0;
1057 }
1058
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001059 for (i = 0; o < *osize_p && i < count; i++) {
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001060 char ch = input[i];
Junio C Hamano8496f562011-12-16 15:44:18 -08001061
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001062 if (ch == '\n') {
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001063 output[o++] = '\r';
Junio C Hamano8496f562011-12-16 15:44:18 -08001064 } else if (was_cr) {
1065 /*
1066 * Previous round saw CR and it is not followed
1067 * by a LF; emit the CR before processing the
1068 * current character.
1069 */
1070 output[o++] = '\r';
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001071 }
Junio C Hamano8496f562011-12-16 15:44:18 -08001072
1073 /*
1074 * We may have consumed the last output slot,
1075 * in which case we need to break out of this
1076 * loop; hold the current character before
1077 * returning.
1078 */
1079 if (*osize_p <= o) {
1080 lf_to_crlf->has_held = 1;
1081 lf_to_crlf->held = ch;
1082 continue; /* break but increment i */
1083 }
1084
1085 if (ch == '\r') {
1086 was_cr = 1;
1087 continue;
1088 }
1089
1090 was_cr = 0;
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001091 output[o++] = ch;
1092 }
1093
1094 *osize_p -= o;
1095 *isize_p -= i;
Junio C Hamano8496f562011-12-16 15:44:18 -08001096
1097 if (!lf_to_crlf->has_held && was_cr) {
1098 lf_to_crlf->has_held = 1;
1099 lf_to_crlf->held = '\r';
1100 }
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001101 }
1102 return 0;
1103}
1104
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001105static void lf_to_crlf_free_fn(struct stream_filter *filter)
1106{
1107 free(filter);
1108}
1109
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001110static struct stream_filter_vtbl lf_to_crlf_vtbl = {
1111 lf_to_crlf_filter_fn,
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001112 lf_to_crlf_free_fn,
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001113};
1114
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001115static struct stream_filter *lf_to_crlf_filter(void)
1116{
Junio C Hamano87afe9a2011-12-16 14:39:37 -08001117 struct lf_to_crlf_filter *lf_to_crlf = xcalloc(1, sizeof(*lf_to_crlf));
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001118
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001119 lf_to_crlf->filter.vtbl = &lf_to_crlf_vtbl;
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001120 return (struct stream_filter *)lf_to_crlf;
1121}
Junio C Hamanob84c78392011-05-20 18:28:00 -07001122
1123/*
Junio C Hamanoa265a7f2011-05-21 18:28:41 -07001124 * Cascade filter
1125 */
1126#define FILTER_BUFFER 1024
1127struct cascade_filter {
1128 struct stream_filter filter;
1129 struct stream_filter *one;
1130 struct stream_filter *two;
1131 char buf[FILTER_BUFFER];
1132 int end, ptr;
1133};
1134
1135static int cascade_filter_fn(struct stream_filter *filter,
1136 const char *input, size_t *isize_p,
1137 char *output, size_t *osize_p)
1138{
1139 struct cascade_filter *cas = (struct cascade_filter *) filter;
1140 size_t filled = 0;
1141 size_t sz = *osize_p;
1142 size_t to_feed, remaining;
1143
1144 /*
1145 * input -- (one) --> buf -- (two) --> output
1146 */
1147 while (filled < sz) {
1148 remaining = sz - filled;
1149
1150 /* do we already have something to feed two with? */
1151 if (cas->ptr < cas->end) {
1152 to_feed = cas->end - cas->ptr;
1153 if (stream_filter(cas->two,
1154 cas->buf + cas->ptr, &to_feed,
1155 output + filled, &remaining))
1156 return -1;
1157 cas->ptr += (cas->end - cas->ptr) - to_feed;
1158 filled = sz - remaining;
1159 continue;
1160 }
1161
1162 /* feed one from upstream and have it emit into our buffer */
1163 to_feed = input ? *isize_p : 0;
1164 if (input && !to_feed)
1165 break;
1166 remaining = sizeof(cas->buf);
1167 if (stream_filter(cas->one,
1168 input, &to_feed,
1169 cas->buf, &remaining))
1170 return -1;
1171 cas->end = sizeof(cas->buf) - remaining;
1172 cas->ptr = 0;
1173 if (input) {
1174 size_t fed = *isize_p - to_feed;
1175 *isize_p -= fed;
1176 input += fed;
1177 }
1178
1179 /* do we know that we drained one completely? */
1180 if (input || cas->end)
1181 continue;
1182
1183 /* tell two to drain; we have nothing more to give it */
1184 to_feed = 0;
1185 remaining = sz - filled;
1186 if (stream_filter(cas->two,
1187 NULL, &to_feed,
1188 output + filled, &remaining))
1189 return -1;
1190 if (remaining == (sz - filled))
1191 break; /* completely drained two */
1192 filled = sz - remaining;
1193 }
1194 *osize_p -= filled;
1195 return 0;
1196}
1197
1198static void cascade_free_fn(struct stream_filter *filter)
1199{
1200 struct cascade_filter *cas = (struct cascade_filter *)filter;
1201 free_stream_filter(cas->one);
1202 free_stream_filter(cas->two);
1203 free(filter);
1204}
1205
1206static struct stream_filter_vtbl cascade_vtbl = {
1207 cascade_filter_fn,
1208 cascade_free_fn,
1209};
1210
1211static struct stream_filter *cascade_filter(struct stream_filter *one,
1212 struct stream_filter *two)
1213{
1214 struct cascade_filter *cascade;
1215
1216 if (!one || is_null_stream_filter(one))
1217 return two;
1218 if (!two || is_null_stream_filter(two))
1219 return one;
1220
1221 cascade = xmalloc(sizeof(*cascade));
1222 cascade->one = one;
1223 cascade->two = two;
1224 cascade->end = cascade->ptr = 0;
1225 cascade->filter.vtbl = &cascade_vtbl;
1226 return (struct stream_filter *)cascade;
1227}
1228
1229/*
Junio C Hamanob84c78392011-05-20 18:28:00 -07001230 * ident filter
1231 */
1232#define IDENT_DRAINING (-1)
1233#define IDENT_SKIPPING (-2)
1234struct ident_filter {
1235 struct stream_filter filter;
1236 struct strbuf left;
1237 int state;
1238 char ident[45]; /* ": x40 $" */
1239};
1240
1241static int is_foreign_ident(const char *str)
1242{
1243 int i;
1244
Jeff Kingae021d82014-06-18 15:47:50 -04001245 if (!skip_prefix(str, "$Id: ", &str))
Junio C Hamanob84c78392011-05-20 18:28:00 -07001246 return 0;
Jeff Kingae021d82014-06-18 15:47:50 -04001247 for (i = 0; str[i]; i++) {
Junio C Hamanob84c78392011-05-20 18:28:00 -07001248 if (isspace(str[i]) && str[i+1] != '$')
1249 return 1;
1250 }
1251 return 0;
1252}
1253
1254static void ident_drain(struct ident_filter *ident, char **output_p, size_t *osize_p)
1255{
1256 size_t to_drain = ident->left.len;
1257
1258 if (*osize_p < to_drain)
1259 to_drain = *osize_p;
1260 if (to_drain) {
1261 memcpy(*output_p, ident->left.buf, to_drain);
1262 strbuf_remove(&ident->left, 0, to_drain);
1263 *output_p += to_drain;
1264 *osize_p -= to_drain;
1265 }
1266 if (!ident->left.len)
1267 ident->state = 0;
1268}
1269
1270static int ident_filter_fn(struct stream_filter *filter,
1271 const char *input, size_t *isize_p,
1272 char *output, size_t *osize_p)
1273{
1274 struct ident_filter *ident = (struct ident_filter *)filter;
1275 static const char head[] = "$Id";
1276
1277 if (!input) {
1278 /* drain upon eof */
1279 switch (ident->state) {
1280 default:
1281 strbuf_add(&ident->left, head, ident->state);
1282 case IDENT_SKIPPING:
1283 /* fallthru */
1284 case IDENT_DRAINING:
1285 ident_drain(ident, &output, osize_p);
1286 }
1287 return 0;
1288 }
1289
1290 while (*isize_p || (ident->state == IDENT_DRAINING)) {
1291 int ch;
1292
1293 if (ident->state == IDENT_DRAINING) {
1294 ident_drain(ident, &output, osize_p);
1295 if (!*osize_p)
1296 break;
1297 continue;
1298 }
1299
1300 ch = *(input++);
1301 (*isize_p)--;
1302
1303 if (ident->state == IDENT_SKIPPING) {
1304 /*
1305 * Skipping until '$' or LF, but keeping them
1306 * in case it is a foreign ident.
1307 */
1308 strbuf_addch(&ident->left, ch);
1309 if (ch != '\n' && ch != '$')
1310 continue;
1311 if (ch == '$' && !is_foreign_ident(ident->left.buf)) {
1312 strbuf_setlen(&ident->left, sizeof(head) - 1);
1313 strbuf_addstr(&ident->left, ident->ident);
1314 }
1315 ident->state = IDENT_DRAINING;
1316 continue;
1317 }
1318
1319 if (ident->state < sizeof(head) &&
1320 head[ident->state] == ch) {
1321 ident->state++;
1322 continue;
1323 }
1324
1325 if (ident->state)
1326 strbuf_add(&ident->left, head, ident->state);
1327 if (ident->state == sizeof(head) - 1) {
1328 if (ch != ':' && ch != '$') {
1329 strbuf_addch(&ident->left, ch);
1330 ident->state = 0;
1331 continue;
1332 }
1333
1334 if (ch == ':') {
1335 strbuf_addch(&ident->left, ch);
1336 ident->state = IDENT_SKIPPING;
1337 } else {
1338 strbuf_addstr(&ident->left, ident->ident);
1339 ident->state = IDENT_DRAINING;
1340 }
1341 continue;
1342 }
1343
1344 strbuf_addch(&ident->left, ch);
1345 ident->state = IDENT_DRAINING;
1346 }
1347 return 0;
1348}
1349
1350static void ident_free_fn(struct stream_filter *filter)
1351{
1352 struct ident_filter *ident = (struct ident_filter *)filter;
1353 strbuf_release(&ident->left);
1354 free(filter);
1355}
1356
1357static struct stream_filter_vtbl ident_vtbl = {
1358 ident_filter_fn,
1359 ident_free_fn,
1360};
1361
1362static struct stream_filter *ident_filter(const unsigned char *sha1)
1363{
1364 struct ident_filter *ident = xmalloc(sizeof(*ident));
1365
Jeff King5096d492015-09-24 17:06:08 -04001366 xsnprintf(ident->ident, sizeof(ident->ident),
1367 ": %s $", sha1_to_hex(sha1));
Junio C Hamanob84c78392011-05-20 18:28:00 -07001368 strbuf_init(&ident->left, 0);
1369 ident->filter.vtbl = &ident_vtbl;
1370 ident->state = 0;
1371 return (struct stream_filter *)ident;
1372}
1373
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001374/*
Junio C Hamanob6691092011-05-20 14:33:31 -07001375 * Return an appropriately constructed filter for the path, or NULL if
1376 * the contents cannot be filtered without reading the whole thing
1377 * in-core.
1378 *
1379 * Note that you would be crazy to set CRLF, smuge/clean or ident to a
1380 * large binary blob you would want us not to slurp into the memory!
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001381 */
Junio C Hamanob6691092011-05-20 14:33:31 -07001382struct stream_filter *get_stream_filter(const char *path, const unsigned char *sha1)
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001383{
1384 struct conv_attrs ca;
Junio C Hamanob84c78392011-05-20 18:28:00 -07001385 struct stream_filter *filter = NULL;
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001386
1387 convert_attrs(&ca, path);
Junio C Hamanob84c78392011-05-20 18:28:00 -07001388 if (ca.drv && (ca.drv->smudge || ca.drv->clean))
Torsten Bögershausencaa47ad2016-04-25 18:56:32 +02001389 return NULL;
1390
1391 if (ca.crlf_action == CRLF_AUTO || ca.crlf_action == CRLF_AUTO_CRLF)
1392 return NULL;
Junio C Hamanob84c78392011-05-20 18:28:00 -07001393
1394 if (ca.ident)
1395 filter = ident_filter(sha1);
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001396
Torsten Bögershausencaa47ad2016-04-25 18:56:32 +02001397 if (output_eol(ca.crlf_action) == EOL_CRLF)
Carlos Martín Nieto284e3d22011-11-28 11:48:12 +01001398 filter = cascade_filter(filter, lf_to_crlf_filter());
Torsten Bögershausencaa47ad2016-04-25 18:56:32 +02001399 else
1400 filter = cascade_filter(filter, &null_filter_singleton);
Junio C Hamanoe322ee32011-05-20 16:47:56 -07001401
Junio C Hamanob84c78392011-05-20 18:28:00 -07001402 return filter;
Junio C Hamanob6691092011-05-20 14:33:31 -07001403}
1404
1405void free_stream_filter(struct stream_filter *filter)
1406{
1407 filter->vtbl->free(filter);
1408}
1409
1410int stream_filter(struct stream_filter *filter,
1411 const char *input, size_t *isize_p,
1412 char *output, size_t *osize_p)
1413{
1414 return filter->vtbl->filter(filter, input, isize_p, output, osize_p);
Junio C Hamanodd8e9122011-05-12 14:31:08 -07001415}