@matheusbsbs / matheus-bagliano / commits / 5ecd293d14
Fork 0

Rewrite convert_to_{git,working_tree} to use strbuf's.

* Now, those functions take an "out" strbuf argument, where they store their result if any. In that case, it also returns 1, else it returns 0. * those functions support "in place" editing, in the sense that it's OK to call them this way: convert_to_git(path, sb->buf, sb->len, sb); When doable, conversions are done in place for real, else the strbuf content is just replaced with the new one, transparentely for the caller. If you want to create a new filter working this way, being the accumulation of filter1, filter2, ... filtern, then your meta_filter would be: int meta_filter(..., const char *src, size_t len, struct strbuf *sb) { int ret = 0; ret |= filter1(...., src, len, sb); if (ret) { src = sb->buf; len = sb->len; } ret |= filter2(...., src, len, sb); if (ret) { src = sb->buf; len = sb->len; } .... return ret | filtern(..., src, len, sb); } That's why subfilters the convert_to_* functions called were also rewritten to work this way. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Acked-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 16, 2007 at 15:51 UTC 5ecd293d1420bf641a927a015877950f4d79c067
7 files changed +240 -317
builtin-apply.c
+13 -14
@@ -1446,8 +1446,7 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign
1446 {
1447 int fd;
1448 unsigned long got;
1449 - unsigned long nsize;
1450 - char *nbuf;
1449 + struct strbuf nbuf;
1450 unsigned long size = *size_p;
1451 char *buf = *buf_p;
1452
@@ -1466,13 +1465,12 @@ static int read_old_data(struct stat *st, const char *path, char **buf_p, unsign
1465 got += ret;
1466 }
1467 close(fd);
1469 - nsize = got;
1470 - nbuf = convert_to_git(path, buf, &nsize);
1471 - if (nbuf) {
1468 + strbuf_init(&nbuf, 0);
1469 + if (convert_to_git(path, buf, size, &nbuf)) {
1470 free(buf);
1473 - *buf_p = nbuf;
1474 - *alloc_p = nsize;
1475 - *size_p = nsize;
1471 + *buf_p = nbuf.buf;
1472 + *alloc_p = nbuf.alloc;
1473 + *size_p = nbuf.len;
1474 }
1475 return got != size;
1476 default:
@@ -2439,7 +2437,7 @@ static void add_index_file(const char *path, unsigned mode, void *buf, unsigned
2437 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
2438 {
2439 int fd;
2442 - char *nbuf;
2440 + struct strbuf nbuf;
2441
2442 if (S_ISGITLINK(mode)) {
2443 struct stat st;
@@ -2458,9 +2456,11 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
2456 if (fd < 0)
2457 return -1;
2458
2461 - nbuf = convert_to_working_tree(path, buf, &size);
2462 - if (nbuf)
2463 - buf = nbuf;
2459 + strbuf_init(&nbuf, 0);
2460 + if (convert_to_working_tree(path, buf, size, &nbuf)) {
2461 + size = nbuf.len;
2462 + buf = nbuf.buf;
2463 + }
2464
2465 while (size) {
2466 int written = xwrite(fd, buf, size);
@@ -2473,8 +2473,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
2473 }
2474 if (close(fd) < 0)
2475 die("closing file %s: %s", path, strerror(errno));
2476 - if (nbuf)
2477 - free(nbuf);
2476 + strbuf_release(&nbuf);
2477 return 0;
2478 }
2479
builtin-archive.c
+36 -42
@@ -81,22 +81,21 @@ static int run_remote_archiver(const char *remote, int argc,
81 return !!rv;
82 }
83
84 -static void *format_subst(const struct commit *commit, const char *format,
85 - unsigned long *sizep)
84 +static void format_subst(const struct commit *commit,
85 + const char *src, size_t len,
86 + struct strbuf *buf)
87 {
87 - unsigned long len = *sizep;
88 - const char *a = format;
89 - struct strbuf result;
88 + char *to_free = NULL;
89 struct strbuf fmt;
90
92 - strbuf_init(&result, 0);
91 + if (src == buf->buf)
92 + to_free = strbuf_detach(buf);
93 strbuf_init(&fmt, 0);
94 -
94 for (;;) {
95 const char *b, *c;
96
98 - b = memmem(a, len, "$Format:", 8);
99 - if (!b || a + len < b + 9)
97 + b = memmem(src, len, "$Format:", 8);
98 + if (!b || src + len < b + 9)
99 break;
100 c = memchr(b + 8, '$', len - 8);
101 if (!c)
@@ -105,62 +104,57 @@ static void *format_subst(const struct commit *commit, const char *format,
104 strbuf_reset(&fmt);
105 strbuf_add(&fmt, b + 8, c - b - 8);
106
108 - strbuf_add(&result, a, b - a);
109 - format_commit_message(commit, fmt.buf, &result);
110 - len -= c + 1 - a;
111 - a = c + 1;
107 + strbuf_add(buf, src, b - src);
108 + format_commit_message(commit, fmt.buf, buf);
109 + len -= c + 1 - src;
110 + src = c + 1;
111 }
113 -
114 - strbuf_add(&result, a, len);
115 -
116 - *sizep = result.len;
117 -
112 + strbuf_add(buf, src, len);
113 strbuf_release(&fmt);
119 - return strbuf_detach(&result);
114 + free(to_free);
115 }
116
122 -static void *convert_to_archive(const char *path,
123 - const void *src, unsigned long *sizep,
124 - const struct commit *commit)
117 +static int convert_to_archive(const char *path,
118 + const void *src, size_t len,
119 + struct strbuf *buf,
120 + const struct commit *commit)
121 {
122 static struct git_attr *attr_export_subst;
123 struct git_attr_check check[1];
124
125 if (!commit)
130 - return NULL;
126 + return 0;
127
132 - if (!attr_export_subst)
133 - attr_export_subst = git_attr("export-subst", 12);
128 + if (!attr_export_subst)
129 + attr_export_subst = git_attr("export-subst", 12);
130
131 check[0].attr = attr_export_subst;
132 if (git_checkattr(path, ARRAY_SIZE(check), check))
137 - return NULL;
133 + return 0;
134 if (!ATTR_TRUE(check[0].value))
139 - return NULL;
135 + return 0;
136
141 - return format_subst(commit, src, sizep);
137 + format_subst(commit, src, len, buf);
138 + return 1;
139 }
140
141 void *sha1_file_to_archive(const char *path, const unsigned char *sha1,
142 unsigned int mode, enum object_type *type,
146 - unsigned long *size,
143 + unsigned long *sizep,
144 const struct commit *commit)
145 {
149 - void *buffer, *converted;
146 + void *buffer;
147
151 - buffer = read_sha1_file(sha1, type, size);
148 + buffer = read_sha1_file(sha1, type, sizep);
149 if (buffer && S_ISREG(mode)) {
153 - converted = convert_to_working_tree(path, buffer, size);
154 - if (converted) {
155 - free(buffer);
156 - buffer = converted;
157 - }
158 -
159 - converted = convert_to_archive(path, buffer, size, commit);
160 - if (converted) {
161 - free(buffer);
162 - buffer = converted;
163 - }
150 + struct strbuf buf;
151 +
152 + strbuf_init(&buf, 0);
153 + strbuf_attach(&buf, buffer, *sizep, *sizep + 1);
154 + convert_to_working_tree(path, buf.buf, buf.len, &buf);
155 + convert_to_archive(path, buf.buf, buf.len, &buf, commit);
156 + *sizep = buf.len;
157 + buffer = buf.buf;
158 }
159
160 return buffer;
cache.h
+4 -2
@@ -2,6 +2,7 @@
2 #define CACHE_H
3
4 #include "git-compat-util.h"
5 +#include "strbuf.h"
6
7 #include SHA1_HEADER
8 #include <zlib.h>
@@ -589,8 +590,9 @@ extern void trace_printf(const char *format, ...);
590 extern void trace_argv_printf(const char **argv, int count, const char *format, ...);
591
592 /* convert.c */
592 -extern char *convert_to_git(const char *path, const char *src, unsigned long *sizep);
593 -extern char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep);
593 +/* returns 1 if *dst was used */
594 +extern int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst);
595 +extern int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst);
596
597 /* diff.c */
598 extern int diff_auto_refresh_index;
convert.c
+171 -243
@@ -1,6 +1,7 @@
1 #include "cache.h"
2 #include "attr.h"
3 #include "run-command.h"
4 +#include "strbuf.h"
5
6 /*
7 * convert.c - convert a file when checking it out and checking it in.
@@ -80,24 +81,19 @@ static int is_binary(unsigned long size, struct text_stat *stats)
81 return 0;
82 }
83
83 -static char *crlf_to_git(const char *path, const char *src, unsigned long *sizep, int action)
84 +static int crlf_to_git(const char *path, const char *src, size_t len,
85 + struct strbuf *buf, int action)
86 {
85 - char *buffer, *dst;
86 - unsigned long size, nsize;
87 struct text_stat stats;
88 + char *dst;
89
89 - if ((action == CRLF_BINARY) || !auto_crlf)
90 - return NULL;
91 -
92 - size = *sizep;
93 - if (!size)
94 - return NULL;
95 -
96 - gather_stats(src, size, &stats);
90 + if ((action == CRLF_BINARY) || !auto_crlf || !len)
91 + return 0;
92
93 + gather_stats(src, len, &stats);
94 /* No CR? Nothing to convert, regardless. */
95 if (!stats.cr)
100 - return NULL;
96 + return 0;
97
98 if (action == CRLF_GUESS) {
99 /*
@@ -106,24 +102,17 @@ static char *crlf_to_git(const char *path, const char *src, unsigned long *sizep
102 * stuff?
103 */
104 if (stats.cr != stats.crlf)
109 - return NULL;
105 + return 0;
106
107 /*
108 * And add some heuristics for binary vs text, of course...
109 */
114 - if (is_binary(size, &stats))
115 - return NULL;
110 + if (is_binary(len, &stats))
111 + return 0;
112 }
113
118 - /*
119 - * Ok, allocate a new buffer, fill it in, and return it
120 - * to let the caller know that we switched buffers.
121 - */
122 - nsize = size - stats.crlf;
123 - buffer = xmalloc(nsize);
124 - *sizep = nsize;
125 -
126 - dst = buffer;
114 + strbuf_grow(buf, len);
115 + dst = buf->buf;
116 if (action == CRLF_GUESS) {
117 /*
118 * If we guessed, we already know we rejected a file with
@@ -134,71 +123,72 @@ static char *crlf_to_git(const char *path, const char *src, unsigned long *sizep
123 unsigned char c = *src++;
124 if (c != '\r')
125 *dst++ = c;
137 - } while (--size);
126 + } while (--len);
127 } else {
128 do {
129 unsigned char c = *src++;
141 - if (! (c == '\r' && (1 < size && *src == '\n')))
130 + if (! (c == '\r' && (1 < len && *src == '\n')))
131 *dst++ = c;
143 - } while (--size);
132 + } while (--len);
133 }
145 -
146 - return buffer;
134 + strbuf_setlen(buf, dst - buf->buf);
135 + return 1;
136 }
137
149 -static char *crlf_to_worktree(const char *path, const char *src, unsigned long *sizep, int action)
138 +static int crlf_to_worktree(const char *path, const char *src, size_t len,
139 + struct strbuf *buf, int action)
140 {
151 - char *buffer, *dst;
152 - unsigned long size, nsize;
141 + char *to_free = NULL;
142 struct text_stat stats;
154 - unsigned char last;
143
144 if ((action == CRLF_BINARY) || (action == CRLF_INPUT) ||
145 auto_crlf <= 0)
158 - return NULL;
146 + return 0;
147
160 - size = *sizep;
161 - if (!size)
162 - return NULL;
148 + if (!len)
149 + return 0;
150
164 - gather_stats(src, size, &stats);
151 + gather_stats(src, len, &stats);
152
153 /* No LF? Nothing to convert, regardless. */
154 if (!stats.lf)
168 - return NULL;
155 + return 0;
156
157 /* Was it already in CRLF format? */
158 if (stats.lf == stats.crlf)
172 - return NULL;
159 + return 0;
160
161 if (action == CRLF_GUESS) {
162 /* If we have any bare CR characters, we're not going to touch it */
163 if (stats.cr != stats.crlf)
177 - return NULL;
164 + return 0;
165
179 - if (is_binary(size, &stats))
180 - return NULL;
166 + if (is_binary(len, &stats))
167 + return 0;
168 }
169
183 - /*
184 - * Ok, allocate a new buffer, fill it in, and return it
185 - * to let the caller know that we switched buffers.
186 - */
187 - nsize = size + stats.lf - stats.crlf;
188 - buffer = xmalloc(nsize);
189 - *sizep = nsize;
190 - last = 0;
191 -
192 - dst = buffer;
193 - do {
194 - unsigned char c = *src++;
195 - if (c == '\n' && last != '\r')
196 - *dst++ = '\r';
197 - *dst++ = c;
198 - last = c;
199 - } while (--size);
200 -
201 - return buffer;
170 + /* are we "faking" in place editing ? */
171 + if (src == buf->buf)
172 + to_free = strbuf_detach(buf);
173 +
174 + strbuf_grow(buf, len + stats.lf - stats.crlf);
175 + for (;;) {
176 + const char *nl = memchr(src, '\n', len);
177 + if (!nl)
178 + break;
179 + if (nl > src && nl[-1] == '\r') {
180 + strbuf_add(buf, src, nl + 1 - src);
181 + } else {
182 + strbuf_add(buf, src, nl - src);
183 + strbuf_addstr(buf, "\r\n");
184 + }
185 + len -= nl + 1 - src;
186 + src = nl + 1;
187 + }
188 + strbuf_add(buf, src, len);
189 +
190 + free(to_free);
191 + return 1;
192 }
193
194 static int filter_buffer(const char *path, const char *src,
@@ -246,8 +236,8 @@ static int filter_buffer(const char *path, const char *src,
236 return (write_err || status);
237 }
238
249 -static char *apply_filter(const char *path, const char *src,
250 - unsigned long *sizep, const char *cmd)
239 +static int apply_filter(const char *path, const char *src, size_t len,
240 + struct strbuf *dst, const char *cmd)
241 {
242 /*
243 * Create a pipeline to have the command filter the buffer's
@@ -255,21 +245,19 @@ static char *apply_filter(const char *path, const char *src,
245 *
246 * (child --> cmd) --> us
247 */
258 - const int SLOP = 4096;
248 int pipe_feed[2];
260 - int status;
261 - char *dst;
262 - unsigned long dstsize, dstalloc;
249 + int status, ret = 1;
250 struct child_process child_process;
251 + struct strbuf nbuf;
252
253 if (!cmd)
266 - return NULL;
254 + return 0;
255
256 memset(&child_process, 0, sizeof(child_process));
257
258 if (pipe(pipe_feed) < 0) {
259 error("cannot create pipe to run external filter %s", cmd);
272 - return NULL;
260 + return 0;
261 }
262
263 fflush(NULL);
@@ -278,54 +266,37 @@ static char *apply_filter(const char *path, const char *src,
266 error("cannot fork to run external filter %s", cmd);
267 close(pipe_feed[0]);
268 close(pipe_feed[1]);
281 - return NULL;
269 + return 0;
270 }
271 if (!child_process.pid) {
272 dup2(pipe_feed[1], 1);
273 close(pipe_feed[0]);
274 close(pipe_feed[1]);
287 - exit(filter_buffer(path, src, *sizep, cmd));
275 + exit(filter_buffer(path, src, len, cmd));
276 }
277 close(pipe_feed[1]);
278
291 - dstalloc = *sizep;
292 - dst = xmalloc(dstalloc);
293 - dstsize = 0;
294 -
295 - while (1) {
296 - ssize_t numread = xread(pipe_feed[0], dst + dstsize,
297 - dstalloc - dstsize);
298 -
299 - if (numread <= 0) {
300 - if (!numread)
301 - break;
302 - error("read from external filter %s failed", cmd);
303 - free(dst);
304 - dst = NULL;
305 - break;
306 - }
307 - dstsize += numread;
308 - if (dstalloc <= dstsize + SLOP) {
309 - dstalloc = dstsize + SLOP;
310 - dst = xrealloc(dst, dstalloc);
311 - }
279 + strbuf_init(&nbuf, 0);
280 + if (strbuf_read(&nbuf, pipe_feed[0], len) < 0) {
281 + error("read from external filter %s failed", cmd);
282 + ret = 0;
283 }
284 if (close(pipe_feed[0])) {
314 - error("read from external filter %s failed", cmd);
315 - free(dst);
316 - dst = NULL;
285 + ret = error("read from external filter %s failed", cmd);
286 + ret = 0;
287 }
318 -
288 status = finish_command(&child_process);
289 if (status) {
321 - error("external filter %s failed %d", cmd, -status);
322 - free(dst);
323 - dst = NULL;
290 + ret = error("external filter %s failed %d", cmd, -status);
291 + ret = 0;
292 }
293
326 - if (dst)
327 - *sizep = dstsize;
328 - return dst;
294 + if (ret) {
295 + *dst = nbuf;
296 + } else {
297 + strbuf_release(&nbuf);
298 + }
299 + return ret;
300 }
301
302 static struct convert_driver {
@@ -449,137 +420,104 @@ static int count_ident(const char *cp, unsigned long size)
420 return cnt;
421 }
422
452 -static char *ident_to_git(const char *path, const char *src, unsigned long *sizep, int ident)
423 +static int ident_to_git(const char *path, const char *src, size_t len,
424 + struct strbuf *buf, int ident)
425 {
454 - int cnt;
455 - unsigned long size;
456 - char *dst, *buf;
426 + char *dst, *dollar;
427
458 - if (!ident)
459 - return NULL;
460 - size = *sizep;
461 - cnt = count_ident(src, size);
462 - if (!cnt)
463 - return NULL;
464 - buf = xmalloc(size);
465 -
466 - for (dst = buf; size; size--) {
467 - char ch = *src++;
468 - *dst++ = ch;
469 - if ((ch == '$') && (3 <= size) &&
470 - !memcmp("Id:", src, 3)) {
471 - unsigned long rem = size - 3;
472 - const char *cp = src + 3;
473 - do {
474 - ch = *cp++;
475 - if (ch == '$')
476 - break;
477 - rem--;
478 - } while (rem);
479 - if (!rem)
480 - continue;
428 + if (!ident || !count_ident(src, len))
429 + return 0;
430 +
431 + strbuf_grow(buf, len);
432 + dst = buf->buf;
433 + for (;;) {
434 + dollar = memchr(src, '$', len);
435 + if (!dollar)
436 + break;
437 + memcpy(dst, src, dollar + 1 - src);
438 + dst += dollar + 1 - src;
439 + len -= dollar + 1 - src;
440 + src = dollar + 1;
441 +
442 + if (len > 3 && !memcmp(src, "Id:", 3)) {
443 + dollar = memchr(src + 3, '$', len - 3);
444 + if (!dollar)
445 + break;
446 memcpy(dst, "Id$", 3);
447 dst += 3;
483 - size -= (cp - src);
484 - src = cp;
448 + len -= dollar + 1 - src;
449 + src = dollar + 1;
450 }
451 }
487 -
488 - *sizep = dst - buf;
489 - return buf;
452 + memcpy(dst, src, len);
453 + strbuf_setlen(buf, dst + len - buf->buf);
454 + return 1;
455 }
456
492 -static char *ident_to_worktree(const char *path, const char *src, unsigned long *sizep, int ident)
457 +static int ident_to_worktree(const char *path, const char *src, size_t len,
458 + struct strbuf *buf, int ident)
459 {
494 - int cnt;
495 - unsigned long size;
496 - char *dst, *buf;
460 unsigned char sha1[20];
461 + char *to_free = NULL, *dollar;
462 + int cnt;
463
464 if (!ident)
500 - return NULL;
465 + return 0;
466
502 - size = *sizep;
503 - cnt = count_ident(src, size);
467 + cnt = count_ident(src, len);
468 if (!cnt)
505 - return NULL;
469 + return 0;
470
507 - hash_sha1_file(src, size, "blob", sha1);
508 - buf = xmalloc(size + cnt * 43);
509 -
510 - for (dst = buf; size; size--) {
511 - const char *cp;
512 - /* Fetch next source character, move the pointer on */
513 - char ch = *src++;
514 - /* Copy the current character to the destination */
515 - *dst++ = ch;
516 - /* If the current character is "$" or there are less than three
517 - * remaining bytes or the two bytes following this one are not
518 - * "Id", then simply read the next character */
519 - if ((ch != '$') || (size < 3) || memcmp("Id", src, 2))
520 - continue;
521 - /*
522 - * Here when
523 - * - There are more than 2 bytes remaining
524 - * - The current three bytes are "$Id"
525 - * with
526 - * - ch == "$"
527 - * - src[0] == "I"
528 - */
471 + /* are we "faking" in place editing ? */
472 + if (src == buf->buf)
473 + to_free = strbuf_detach(buf);
474 + hash_sha1_file(src, len, "blob", sha1);
475
530 - /*
531 - * It's possible that an expanded Id has crept its way into the
532 - * repository, we cope with that by stripping the expansion out
533 - */
534 - if (src[2] == ':') {
535 - /* Expanded keywords have "$Id:" at the front */
476 + strbuf_grow(buf, len + cnt * 43);
477 + for (;;) {
478 + /* step 1: run to the next '$' */
479 + dollar = memchr(src, '$', len);
480 + if (!dollar)
481 + break;
482 + strbuf_add(buf, src, dollar + 1 - src);
483 + len -= dollar + 1 - src;
484 + src = dollar + 1;
485
537 - /* discard up to but not including the closing $ */
538 - unsigned long rem = size - 3;
539 - /* Point at first byte after the ":" */
540 - cp = src + 3;
541 - /*
542 - * Throw away characters until either
543 - * - we reach a "$"
544 - * - we run out of bytes (rem == 0)
545 - */
546 - do {
547 - ch = *cp;
548 - if (ch == '$')
549 - break;
550 - cp++;
551 - rem--;
552 - } while (rem);
553 - /* If the above finished because it ran out of characters, then
554 - * this is an incomplete keyword, so don't run the expansion */
555 - if (!rem)
556 - continue;
557 - } else if (src[2] == '$')
558 - cp = src + 2;
559 - else
560 - /* Anything other than "$Id:XXX$" or $Id$ and we skip the
561 - * expansion */
486 + /* step 2: does it looks like a bit like Id:xxx$ or Id$ ? */
487 + if (len < 3 || memcmp("Id", src, 2))
488 continue;
489
564 - /* cp is now pointing at the last $ of the keyword */
565 -
566 - memcpy(dst, "Id: ", 4);
567 - dst += 4;
568 - memcpy(dst, sha1_to_hex(sha1), 40);
569 - dst += 40;
570 - *dst++ = ' ';
490 + /* step 3: skip over Id$ or Id:xxxxx$ */
491 + if (src[2] == '$') {
492 + src += 3;
493 + len -= 3;
494 + } else if (src[2] == ':') {
495 + /*
496 + * It's possible that an expanded Id has crept its way into the
497 + * repository, we cope with that by stripping the expansion out
498 + */
499 + dollar = memchr(src + 3, '$', len - 3);
500 + if (!dollar) {
501 + /* incomplete keyword, no more '$', so just quit the loop */
502 + break;
503 + }
504
572 - /* Adjust for the characters we've discarded */
573 - size -= (cp - src);
574 - src = cp;
505 + len -= dollar + 1 - src;
506 + src = dollar + 1;
507 + } else {
508 + /* it wasn't a "Id$" or "Id:xxxx$" */
509 + continue;
510 + }
511
576 - /* Copy the final "$" */
577 - *dst++ = *src++;
578 - size--;
512 + /* step 4: substitute */
513 + strbuf_addstr(buf, "Id: ");
514 + strbuf_add(buf, sha1_to_hex(sha1), 40);
515 + strbuf_addstr(buf, " $");
516 }
517 + strbuf_add(buf, src, len);
518
581 - *sizep = dst - buf;
582 - return buf;
519 + free(to_free);
520 + return 1;
521 }
522
523 static int git_path_check_crlf(const char *path, struct git_attr_check *check)
@@ -618,13 +556,12 @@ static int git_path_check_ident(const char *path, struct git_attr_check *check)
556 return !!ATTR_TRUE(value);
557 }
558
621 -char *convert_to_git(const char *path, const char *src, unsigned long *sizep)
559 +int convert_to_git(const char *path, const char *src, size_t len, struct strbuf *dst)
560 {
561 struct git_attr_check check[3];
562 int crlf = CRLF_GUESS;
625 - int ident = 0;
563 + int ident = 0, ret = 0;
564 char *filter = NULL;
627 - char *buf, *buf2;
565
566 setup_convert_check(check);
567 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
@@ -636,30 +573,25 @@ char *convert_to_git(const char *path, const char *src, unsigned long *sizep)
573 filter = drv->clean;
574 }
575
639 - buf = apply_filter(path, src, sizep, filter);
640 -
641 - buf2 = crlf_to_git(path, buf ? buf : src, sizep, crlf);
642 - if (buf2) {
643 - free(buf);
644 - buf = buf2;
576 + ret |= apply_filter(path, src, len, dst, filter);
577 + if (ret) {
578 + src = dst->buf;
579 + len = dst->len;
580 }
646 -
647 - buf2 = ident_to_git(path, buf ? buf : src, sizep, ident);
648 - if (buf2) {
649 - free(buf);
650 - buf = buf2;
581 + ret |= crlf_to_git(path, src, len, dst, crlf);
582 + if (ret) {
583 + src = dst->buf;
584 + len = dst->len;
585 }
652 -
653 - return buf;
586 + return ret | ident_to_git(path, src, len, dst, ident);
587 }
588
656 -char *convert_to_working_tree(const char *path, const char *src, unsigned long *sizep)
589 +int convert_to_working_tree(const char *path, const char *src, size_t len, struct strbuf *dst)
590 {
591 struct git_attr_check check[3];
592 int crlf = CRLF_GUESS;
660 - int ident = 0;
593 + int ident = 0, ret = 0;
594 char *filter = NULL;
662 - char *buf, *buf2;
595
596 setup_convert_check(check);
597 if (!git_checkattr(path, ARRAY_SIZE(check), check)) {
@@ -671,19 +603,15 @@ char *convert_to_working_tree(const char *path, const char *src, unsigned long *
603 filter = drv->smudge;
604 }
605
674 - buf = ident_to_worktree(path, src, sizep, ident);
675 -
676 - buf2 = crlf_to_worktree(path, buf ? buf : src, sizep, crlf);
677 - if (buf2) {
678 - free(buf);
679 - buf = buf2;
606 + ret |= ident_to_worktree(path, src, len, dst, ident);
607 + if (ret) {
608 + src = dst->buf;
609 + len = dst->len;
610 }
681 -
682 - buf2 = apply_filter(path, buf ? buf : src, sizep, filter);
683 - if (buf2) {
684 - free(buf);
685 - buf = buf2;
611 + ret |= crlf_to_worktree(path, src, len, dst, crlf);
612 + if (ret) {
613 + src = dst->buf;
614 + len = dst->len;
615 }
687 -
688 - return buf;
616 + return ret | apply_filter(path, src, len, dst, filter);
617 }
diff.c
+5 -7
@@ -1600,10 +1600,9 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1600
1601 if (!s->sha1_valid ||
1602 reuse_worktree_file(s->path, s->sha1, 0)) {
1603 + struct strbuf buf;
1604 struct stat st;
1605 int fd;
1605 - char *buf;
1606 - unsigned long size;
1606
1607 if (!strcmp(s->path, "-"))
1608 return populate_from_stdin(s);
@@ -1644,13 +1643,12 @@ int diff_populate_filespec(struct diff_filespec *s, int size_only)
1643 /*
1644 * Convert from working tree format to canonical git format
1645 */
1647 - size = s->size;
1648 - buf = convert_to_git(s->path, s->data, &size);
1649 - if (buf) {
1646 + strbuf_init(&buf, 0);
1647 + if (convert_to_git(s->path, s->data, s->size, &buf)) {
1648 munmap(s->data, s->size);
1649 s->should_munmap = 0;
1652 - s->data = buf;
1653 - s->size = size;
1650 + s->data = buf.buf;
1651 + s->size = buf.len;
1652 s->should_free = 1;
1653 }
1654 }
entry.c
+6 -4
@@ -104,7 +104,8 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
104 long wrote;
105
106 switch (ntohl(ce->ce_mode) & S_IFMT) {
107 - char *buf, *new;
107 + char *new;
108 + struct strbuf buf;
109 unsigned long size;
110
111 case S_IFREG:
@@ -116,10 +117,11 @@ static int write_entry(struct cache_entry *ce, char *path, const struct checkout
117 /*
118 * Convert from git internal format to working tree format
119 */
119 - buf = convert_to_working_tree(ce->name, new, &size);
120 - if (buf) {
120 + strbuf_init(&buf, 0);
121 + if (convert_to_working_tree(ce->name, new, size, &buf)) {
122 free(new);
122 - new = buf;
123 + new = buf.buf;
124 + size = buf.len;
125 }
126
127 if (to_tempfile) {
sha1_file.c
+5 -5
@@ -2343,12 +2343,12 @@ int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object,
2343 * Convert blobs to git internal format
2344 */
2345 if ((type == OBJ_BLOB) && S_ISREG(st->st_mode)) {
2346 - unsigned long nsize = size;
2347 - char *nbuf = convert_to_git(path, buf, &nsize);
2348 - if (nbuf) {
2346 + struct strbuf nbuf;
2347 + strbuf_init(&nbuf, 0);
2348 + if (convert_to_git(path, buf, size, &nbuf)) {
2349 munmap(buf, size);
2350 - size = nsize;
2351 - buf = nbuf;
2350 + size = nbuf.len;
2351 + buf = nbuf.buf;
2352 re_allocated = 1;
2353 }
2354 }