summaryrefslogtreecommitdiff
path: root/builtin-apply.c
diff options
context:
space:
mode:
authorPierre Habouzit <madcoder@debian.org>2007-09-27 13:33:19 +0200
committerJunio C Hamano <gitster@pobox.com>2007-09-29 21:39:29 -0700
commit9a76adebd6dfe144c3643e7e877fff2b41be5c8e (patch)
tree0a66f6a98cb9529c3d1817c2b70424b7dd4532b0 /builtin-apply.c
parent387e7e19d7eb5444be8da8e99ed7491989dc1cbb (diff)
downloadgit-9a76adebd6dfe144c3643e7e877fff2b41be5c8e.tar.gz
Make read_patch_file work on a strbuf.
So that we don't need to use strbuf_detach. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'builtin-apply.c')
-rw-r--r--builtin-apply.c27
1 files changed, 11 insertions, 16 deletions
diff --git a/builtin-apply.c b/builtin-apply.c
index fec96a8d9d..4113d66b56 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -178,12 +178,9 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
#define CHUNKSIZE (8192)
#define SLOP (16)
-static void *read_patch_file(int fd, size_t *sizep)
+static void read_patch_file(struct strbuf *sb, int fd)
{
- struct strbuf buf;
-
- strbuf_init(&buf, 0);
- if (strbuf_read(&buf, fd, 0) < 0)
+ if (strbuf_read(sb, fd, 0) < 0)
die("git-apply: read returned %s", strerror(errno));
/*
@@ -191,9 +188,8 @@ static void *read_patch_file(int fd, size_t *sizep)
* so that we can do speculative "memcmp" etc, and
* see to it that it is NUL-filled.
*/
- strbuf_grow(&buf, SLOP);
- memset(buf.buf + buf.len, 0, SLOP);
- return strbuf_detach(&buf, sizep);
+ strbuf_grow(sb, SLOP);
+ memset(sb->buf + sb->len, 0, SLOP);
}
static unsigned long linelen(const char *buffer, unsigned long size)
@@ -2633,22 +2629,22 @@ static void prefix_patches(struct patch *p)
static int apply_patch(int fd, const char *filename, int inaccurate_eof)
{
- unsigned long offset, size;
- char *buffer = read_patch_file(fd, &size);
+ size_t offset;
+ struct strbuf buf;
struct patch *list = NULL, **listp = &list;
int skipped_patch = 0;
+ strbuf_init(&buf, 0);
patch_input_file = filename;
- if (!buffer)
- return -1;
+ read_patch_file(&buf, fd);
offset = 0;
- while (size > 0) {
+ while (offset < buf.len) {
struct patch *patch;
int nr;
patch = xcalloc(1, sizeof(*patch));
patch->inaccurate_eof = inaccurate_eof;
- nr = parse_chunk(buffer + offset, size, patch);
+ nr = parse_chunk(buf.buf + offset, buf.len, patch);
if (nr < 0)
break;
if (apply_in_reverse)
@@ -2666,7 +2662,6 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
skipped_patch++;
}
offset += nr;
- size -= nr;
}
if (whitespace_error && (new_whitespace == error_on_whitespace))
@@ -2701,7 +2696,7 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
if (summary)
summary_patch_list(list);
- free(buffer);
+ strbuf_release(&buf);
return 0;
}