Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 592 lines (516 sloc) 13.271 kb
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
1 #include "cache.h"
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
2 #include "attr.h"
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
3 #include "run-command.h"
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
4
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
5 /*
6 * convert.c - convert a file when checking it out and checking it in.
7 *
8 * This should use the pathname to decide on whether it wants to do some
9 * more interesting conversions (automatic gzip/unzip, general format
10 * conversions etc etc), but by default it just does automatic CRLF<->LF
11 * translation when the "auto_crlf" option is set.
12 */
13
163b9591 » Junio C Hamano
2007-04-19 Update 'crlf' attribute semantics.
14 #define CRLF_GUESS (-1)
15 #define CRLF_BINARY 0
16 #define CRLF_TEXT 1
17 #define CRLF_INPUT 2
18
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
19 struct text_stat {
20 /* CR, LF and CRLF counts */
21 unsigned cr, lf, crlf;
22
23 /* These are just approximations! */
24 unsigned printable, nonprintable;
25 };
26
27 static void gather_stats(const char *buf, unsigned long size, struct text_stat *stats)
28 {
29 unsigned long i;
30
31 memset(stats, 0, sizeof(*stats));
32
33 for (i = 0; i < size; i++) {
34 unsigned char c = buf[i];
35 if (c == '\r') {
36 stats->cr++;
37 if (i+1 < size && buf[i+1] == '\n')
38 stats->crlf++;
39 continue;
40 }
41 if (c == '\n') {
42 stats->lf++;
43 continue;
44 }
45 if (c == 127)
46 /* DEL */
47 stats->nonprintable++;
48 else if (c < 32) {
49 switch (c) {
50 /* BS, HT, ESC and FF */
51 case '\b': case '\t': case '\033': case '\014':
52 stats->printable++;
53 break;
54 default:
55 stats->nonprintable++;
56 }
57 }
58 else
59 stats->printable++;
60 }
61 }
62
63 /*
64 * The same heuristics as diff.c::mmfile_is_binary()
65 */
66 static int is_binary(unsigned long size, struct text_stat *stats)
67 {
68
69 if ((stats->printable >> 7) < stats->nonprintable)
70 return 1;
71 /*
72 * Other heuristics? Average line length might be relevant,
73 * as might LF vs CR vs CRLF counts..
74 *
75 * NOTE! It might be normal to have a low ratio of CRLF to LF
76 * (somebody starts with a LF-only file and edits it with an editor
77 * that adds CRLF only to lines that are added..). But do we
78 * want to support CR-only? Probably not.
79 */
80 return 0;
81 }
82
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
83 static int crlf_to_git(const char *path, const char *src, size_t len,
84 struct strbuf *buf, int action)
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
85 {
86 struct text_stat stats;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
87 char *dst;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
88
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
89 if ((action == CRLF_BINARY) || !auto_crlf || !len)
90 return 0;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
91
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
92 gather_stats(src, len, &stats);
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
93 /* No CR? Nothing to convert, regardless. */
94 if (!stats.cr)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
95 return 0;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
96
163b9591 » Junio C Hamano
2007-04-19 Update 'crlf' attribute semantics.
97 if (action == CRLF_GUESS) {
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
98 /*
99 * We're currently not going to even try to convert stuff
100 * that has bare CR characters. Does anybody do that crazy
101 * stuff?
102 */
103 if (stats.cr != stats.crlf)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
104 return 0;
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
105
106 /*
107 * And add some heuristics for binary vs text, of course...
108 */
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
109 if (is_binary(len, &stats))
110 return 0;
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
111 }
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
112
90d16ec0 » MadCoder
2007-10-05 Fix in-place editing functions in convert.c
113 /* only grow if not in place */
114 if (strbuf_avail(buf) + buf->len < len)
115 strbuf_grow(buf, len - buf->len);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
116 dst = buf->buf;
163b9591 » Junio C Hamano
2007-04-19 Update 'crlf' attribute semantics.
117 if (action == CRLF_GUESS) {
118 /*
119 * If we guessed, we already know we rejected a file with
120 * lone CR, and we can strip a CR without looking at what
121 * follow it.
122 */
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
123 do {
ac78e548 » raalkml
2007-04-19 Simplify calling of CR/LF conversion routines
124 unsigned char c = *src++;
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
125 if (c != '\r')
ac78e548 » raalkml
2007-04-19 Simplify calling of CR/LF conversion routines
126 *dst++ = c;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
127 } while (--len);
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
128 } else {
129 do {
ac78e548 » raalkml
2007-04-19 Simplify calling of CR/LF conversion routines
130 unsigned char c = *src++;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
131 if (! (c == '\r' && (1 < len && *src == '\n')))
ac78e548 » raalkml
2007-04-19 Simplify calling of CR/LF conversion routines
132 *dst++ = c;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
133 } while (--len);
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
134 }
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
135 strbuf_setlen(buf, dst - buf->buf);
136 return 1;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
137 }
138
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
139 static int crlf_to_worktree(const char *path, const char *src, size_t len,
140 struct strbuf *buf, int action)
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
141 {
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
142 char *to_free = NULL;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
143 struct text_stat stats;
144
163b9591 » Junio C Hamano
2007-04-19 Update 'crlf' attribute semantics.
145 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
760f0c62 » andyparkins
2007-05-18 Fix crlf attribute handling to match documentation
146 auto_crlf <= 0)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
147 return 0;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
148
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
149 if (!len)
150 return 0;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
151
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
152 gather_stats(src, len, &stats);
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
153
154 /* No LF? Nothing to convert, regardless. */
155 if (!stats.lf)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
156 return 0;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
157
158 /* Was it already in CRLF format? */
159 if (stats.lf == stats.crlf)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
160 return 0;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
161
163b9591 » Junio C Hamano
2007-04-19 Update 'crlf' attribute semantics.
162 if (action == CRLF_GUESS) {
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
163 /* If we have any bare CR characters, we're not going to touch it */
164 if (stats.cr != stats.crlf)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
165 return 0;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
166
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
167 if (is_binary(len, &stats))
168 return 0;
201ac8ef » Junio C Hamano
2007-04-15 Fix 'crlf' attribute semantics.
169 }
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
170
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
171 /* are we "faking" in place editing ? */
172 if (src == buf->buf)
b315c5c0 » MadCoder
2007-09-27 strbuf change: be sure ->buf is never ever NULL.
173 to_free = strbuf_detach(buf, NULL);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
174
175 strbuf_grow(buf, len + stats.lf - stats.crlf);
176 for (;;) {
177 const char *nl = memchr(src, '\n', len);
178 if (!nl)
179 break;
180 if (nl > src && nl[-1] == '\r') {
181 strbuf_add(buf, src, nl + 1 - src);
182 } else {
183 strbuf_add(buf, src, nl - src);
184 strbuf_addstr(buf, "\r\n");
185 }
186 len -= nl + 1 - src;
187 src = nl + 1;
188 }
189 strbuf_add(buf, src, len);
190
191 free(to_free);
192 return 1;
6c510bee » torvalds
2007-02-13 Lazy man's auto-CRLF
193 }
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
194
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
195 struct filter_params {
196 const char *src;
197 unsigned long size;
198 const char *cmd;
199 };
200
201 static int filter_buffer(int fd, void *data)
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
202 {
203 /*
204 * Spawn cmd and feed the buffer contents through its stdin.
205 */
206 struct child_process child_process;
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
207 struct filter_params *params = (struct filter_params *)data;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
208 int write_err, status;
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
209 const char *argv[] = { "sh", "-c", params->cmd, NULL };
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
210
211 memset(&child_process, 0, sizeof(child_process));
dc1bfdcd » Johannes Sixt
2007-10-19 Use start_command() to run content filters instead of explicit fork/e…
212 child_process.argv = argv;
213 child_process.in = -1;
7683b6e8 » Johannes Sixt
2007-10-19 Avoid a dup2(2) in apply_filter() - start_command() can do it for us.
214 child_process.out = fd;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
215
dc1bfdcd » Johannes Sixt
2007-10-19 Use start_command() to run content filters instead of explicit fork/e…
216 if (start_command(&child_process))
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
217 return error("cannot fork to run external filter %s", params->cmd);
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
218
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
219 write_err = (write_in_full(child_process.in, params->src, params->size) < 0);
dc1bfdcd » Johannes Sixt
2007-10-19 Use start_command() to run content filters instead of explicit fork/e…
220 if (close(child_process.in))
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
221 write_err = 1;
222 if (write_err)
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
223 error("cannot feed the input to external filter %s", params->cmd);
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
224
225 status = finish_command(&child_process);
226 if (status)
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
227 error("external filter %s failed %d", params->cmd, -status);
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
228 return (write_err || status);
229 }
230
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
231 static int apply_filter(const char *path, const char *src, size_t len,
232 struct strbuf *dst, const char *cmd)
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
233 {
234 /*
235 * Create a pipeline to have the command filter the buffer's
236 * contents.
237 *
238 * (child --> cmd) --> us
239 */
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
240 int ret = 1;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
241 struct strbuf nbuf;
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
242 struct async async;
243 struct filter_params params;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
244
245 if (!cmd)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
246 return 0;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
247
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
248 memset(&async, 0, sizeof(async));
249 async.proc = filter_buffer;
250 async.data = &params;
251 params.src = src;
252 params.size = len;
253 params.cmd = cmd;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
254
255 fflush(NULL);
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
256 if (start_async(&async))
257 return 0; /* error was already reported */
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
258
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
259 strbuf_init(&nbuf, 0);
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
260 if (strbuf_read(&nbuf, async.out, len) < 0) {
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
261 error("read from external filter %s failed", cmd);
262 ret = 0;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
263 }
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
264 if (close(async.out)) {
90d16ec0 » MadCoder
2007-10-05 Fix in-place editing functions in convert.c
265 error("read from external filter %s failed", cmd);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
266 ret = 0;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
267 }
546bb582 » Johannes Sixt
2007-10-19 Use the asyncronous function infrastructure to run the content filter.
268 if (finish_async(&async)) {
269 error("external filter %s failed", cmd);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
270 ret = 0;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
271 }
272
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
273 if (ret) {
90d16ec0 » MadCoder
2007-10-05 Fix in-place editing functions in convert.c
274 strbuf_swap(dst, &nbuf);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
275 }
90d16ec0 » MadCoder
2007-10-05 Fix in-place editing functions in convert.c
276 strbuf_release(&nbuf);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
277 return ret;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
278 }
279
280 static struct convert_driver {
281 const char *name;
282 struct convert_driver *next;
283 char *smudge;
284 char *clean;
285 } *user_convert, **user_convert_tail;
286
287 static int read_convert_config(const char *var, const char *value)
288 {
289 const char *ep, *name;
290 int namelen;
291 struct convert_driver *drv;
292
293 /*
294 * External conversion drivers are configured using
295 * "filter.<name>.variable".
296 */
297 if (prefixcmp(var, "filter.") || (ep = strrchr(var, '.')) == var + 6)
298 return 0;
299 name = var + 7;
300 namelen = ep - name;
301 for (drv = user_convert; drv; drv = drv->next)
302 if (!strncmp(drv->name, name, namelen) && !drv->name[namelen])
303 break;
304 if (!drv) {
305 drv = xcalloc(1, sizeof(struct convert_driver));
182af834 » MadCoder
2007-09-16 Use xmemdupz() in many places.
306 drv->name = xmemdupz(name, namelen);
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
307 *user_convert_tail = drv;
308 user_convert_tail = &(drv->next);
309 }
310
311 ep++;
312
313 /*
314 * filter.<name>.smudge and filter.<name>.clean specifies
315 * the command line:
316 *
317 * command-line
318 *
319 * The command-line will not be interpolated in any way.
320 */
321
322 if (!strcmp("smudge", ep)) {
323 if (!value)
324 return error("%s: lacks value", var);
325 drv->smudge = strdup(value);
326 return 0;
327 }
328
329 if (!strcmp("clean", ep)) {
330 if (!value)
331 return error("%s: lacks value", var);
332 drv->clean = strdup(value);
333 return 0;
334 }
335 return 0;
336 }
337
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
338 static void setup_convert_check(struct git_attr_check *check)
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
339 {
340 static struct git_attr *attr_crlf;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
341 static struct git_attr *attr_ident;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
342 static struct git_attr *attr_filter;
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
343
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
344 if (!attr_crlf) {
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
345 attr_crlf = git_attr("crlf", 4);
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
346 attr_ident = git_attr("ident", 5);
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
347 attr_filter = git_attr("filter", 6);
348 user_convert_tail = &user_convert;
349 git_config(read_convert_config);
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
350 }
351 check[0].attr = attr_crlf;
352 check[1].attr = attr_ident;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
353 check[2].attr = attr_filter;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
354 }
355
356 static int count_ident(const char *cp, unsigned long size)
357 {
358 /*
af9b54bb » andyparkins
2007-05-14 Use $Id$ as the ident attribute keyword rather than $ident$ to be con…
359 * "$Id: 0000000000000000000000000000000000000000 $" <=> "$Id$"
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
360 */
361 int cnt = 0;
362 char ch;
363
364 while (size) {
365 ch = *cp++;
366 size--;
367 if (ch != '$')
368 continue;
af9b54bb » andyparkins
2007-05-14 Use $Id$ as the ident attribute keyword rather than $ident$ to be con…
369 if (size < 3)
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
370 break;
af9b54bb » andyparkins
2007-05-14 Use $Id$ as the ident attribute keyword rather than $ident$ to be con…
371 if (memcmp("Id", cp, 2))
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
372 continue;
af9b54bb » andyparkins
2007-05-14 Use $Id$ as the ident attribute keyword rather than $ident$ to be con…
373 ch = cp[2];
374 cp += 3;
375 size -= 3;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
376 if (ch == '$')
af9b54bb » andyparkins
2007-05-14 Use $Id$ as the ident attribute keyword rather than $ident$ to be con…
377 cnt++; /* $Id$ */
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
378 if (ch != ':')
379 continue;
380
381 /*
af9b54bb » andyparkins
2007-05-14 Use $Id$ as the ident attribute keyword rather than $ident$ to be con…
382 * "$Id: ... "; scan up to the closing dollar sign and discard.
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
383 */
384 while (size) {
385 ch = *cp++;
386 size--;
387 if (ch == '$') {
388 cnt++;
389 break;
390 }
391 }
392 }
393 return cnt;
394 }
395
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
396 static int ident_to_git(const char *path, const char *src, size_t len,
397 struct strbuf *buf, int ident)
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
398 {
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
399 char *dst, *dollar;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
400
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
401 if (!ident || !count_ident(src, len))
402 return 0;
403
90d16ec0 » MadCoder
2007-10-05 Fix in-place editing functions in convert.c
404 /* only grow if not in place */
405 if (strbuf_avail(buf) + buf->len < len)
406 strbuf_grow(buf, len - buf->len);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
407 dst = buf->buf;
408 for (;;) {
409 dollar = memchr(src, '$', len);
410 if (!dollar)
411 break;
412 memcpy(dst, src, dollar + 1 - src);
413 dst += dollar + 1 - src;
414 len -= dollar + 1 - src;
415 src = dollar + 1;
416
417 if (len > 3 && !memcmp(src, "Id:", 3)) {
418 dollar = memchr(src + 3, '$', len - 3);
419 if (!dollar)
420 break;
af9b54bb » andyparkins
2007-05-14 Use $Id$ as the ident attribute keyword rather than $ident$ to be con…
421 memcpy(dst, "Id$", 3);
422 dst += 3;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
423 len -= dollar + 1 - src;
424 src = dollar + 1;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
425 }
426 }
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
427 memcpy(dst, src, len);
428 strbuf_setlen(buf, dst + len - buf->buf);
429 return 1;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
430 }
431
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
432 static int ident_to_worktree(const char *path, const char *src, size_t len,
433 struct strbuf *buf, int ident)
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
434 {
435 unsigned char sha1[20];
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
436 char *to_free = NULL, *dollar;
437 int cnt;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
438
439 if (!ident)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
440 return 0;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
441
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
442 cnt = count_ident(src, len);
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
443 if (!cnt)
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
444 return 0;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
445
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
446 /* are we "faking" in place editing ? */
447 if (src == buf->buf)
b315c5c0 » MadCoder
2007-09-27 strbuf change: be sure ->buf is never ever NULL.
448 to_free = strbuf_detach(buf, NULL);
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
449 hash_sha1_file(src, len, "blob", sha1);
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
450
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
451 strbuf_grow(buf, len + cnt * 43);
452 for (;;) {
453 /* step 1: run to the next '$' */
454 dollar = memchr(src, '$', len);
455 if (!dollar)
456 break;
457 strbuf_add(buf, src, dollar + 1 - src);
458 len -= dollar + 1 - src;
459 src = dollar + 1;
c23290d5 » andyparkins
2007-05-25 Fix mishandling of $Id$ expanded in the repository copy in convert.c
460
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
461 /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
462 if (len < 3 || memcmp("Id", src, 2))
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
463 continue;
464
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
465 /* step 3: skip over Id$ or Id:xxxxx$ */
466 if (src[2] == '$') {
467 src += 3;
468 len -= 3;
469 } else if (src[2] == ':') {
470 /*
471 * It's possible that an expanded Id has crept its way into the
472 * repository, we cope with that by stripping the expansion out
473 */
474 dollar = memchr(src + 3, '$', len - 3);
475 if (!dollar) {
476 /* incomplete keyword, no more '$', so just quit the loop */
477 break;
478 }
c23290d5 » andyparkins
2007-05-25 Fix mishandling of $Id$ expanded in the repository copy in convert.c
479
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
480 len -= dollar + 1 - src;
481 src = dollar + 1;
482 } else {
483 /* it wasn't a "Id$" or "Id:xxxx$" */
484 continue;
485 }
c23290d5 » andyparkins
2007-05-25 Fix mishandling of $Id$ expanded in the repository copy in convert.c
486
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
487 /* step 4: substitute */
488 strbuf_addstr(buf, "Id: ");
489 strbuf_add(buf, sha1_to_hex(sha1), 40);
490 strbuf_addstr(buf, " $");
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
491 }
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
492 strbuf_add(buf, src, len);
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
493
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
494 free(to_free);
495 return 1;
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
496 }
497
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
498 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
499 {
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
500 const char *value = check->value;
501
502 if (ATTR_TRUE(value))
503 return CRLF_TEXT;
504 else if (ATTR_FALSE(value))
505 return CRLF_BINARY;
506 else if (ATTR_UNSET(value))
507 ;
508 else if (!strcmp(value, "input"))
509 return CRLF_INPUT;
163b9591 » Junio C Hamano
2007-04-19 Update 'crlf' attribute semantics.
510 return CRLF_GUESS;
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
511 }
512
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
513 static struct convert_driver *git_path_check_convert(const char *path,
514 struct git_attr_check *check)
515 {
516 const char *value = check->value;
517 struct convert_driver *drv;
518
519 if (ATTR_TRUE(value) || ATTR_FALSE(value) || ATTR_UNSET(value))
520 return NULL;
521 for (drv = user_convert; drv; drv = drv->next)
522 if (!strcmp(value, drv->name))
523 return drv;
524 return NULL;
525 }
526
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
527 static int git_path_check_ident(const char *path, struct git_attr_check *check)
528 {
529 const char *value = check->value;
530
531 return !!ATTR_TRUE(value);
532 }
533
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
534 int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
535 {
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
536 struct git_attr_check check[3];
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
537 int crlf = CRLF_GUESS;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
538 int ident = 0, ret = 0;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
539 char *filter = NULL;
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
540
541 setup_convert_check(check);
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
542 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
543 struct convert_driver *drv;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
544 crlf = git_path_check_crlf(path, check + 0);
545 ident = git_path_check_ident(path, check + 1);
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
546 drv = git_path_check_convert(path, check + 2);
547 if (drv && drv->clean)
548 filter = drv->clean;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
549 }
550
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
551 ret |= apply_filter(path, src, len, dst, filter);
552 if (ret) {
553 src = dst->buf;
554 len = dst->len;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
555 }
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
556 ret |= crlf_to_git(path, src, len, dst, crlf);
557 if (ret) {
558 src = dst->buf;
559 len = dst->len;
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
560 }
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
561 return ret | ident_to_git(path, src, len, dst, ident);
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
562 }
563
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
564 int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
565 {
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
566 struct git_attr_check check[3];
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
567 int crlf = CRLF_GUESS;
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
568 int ident = 0, ret = 0;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
569 char *filter = NULL;
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
570
571 setup_convert_check(check);
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
572 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
573 struct convert_driver *drv;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
574 crlf = git_path_check_crlf(path, check + 0);
575 ident = git_path_check_ident(path, check + 1);
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
576 drv = git_path_check_convert(path, check + 2);
577 if (drv && drv->smudge)
578 filter = drv->smudge;
6073ee85 » Junio C Hamano
2007-04-20 convert.c: restructure the attribute checking part.
579 }
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
580
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
581 ret |= ident_to_worktree(path, src, len, dst, ident);
582 if (ret) {
583 src = dst->buf;
584 len = dst->len;
3fed15f5 » Junio C Hamano
2007-04-21 Add 'ident' conversion.
585 }
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
586 ret |= crlf_to_worktree(path, src, len, dst, crlf);
587 if (ret) {
588 src = dst->buf;
589 len = dst->len;
aa4ed402 » Junio C Hamano
2007-04-21 Add 'filter' attribute and external filter driver definition.
590 }
5ecd293d » MadCoder
2007-09-16 Rewrite convert_to_{git,working_tree} to use strbuf's.
591 return ret | apply_filter(path, src, len, dst, filter);
35ebfd6a » Junio C Hamano
2007-04-12 Define 'crlf' attribute.
592 }
Something went wrong with that request. Please try again.