@matheusbsbs / matheus-bagliano / commits / 9a76adebd6
Fork 0

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>

Pierre Habouzit committed Sep 27, 2007 at 13:33 UTC 9a76adebd6dfe144c3643e7e877fff2b41be5c8e
1 file changed +11 -16
builtin-apply.c
+11 -16
@@ -178,12 +178,9 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
178 #define CHUNKSIZE (8192)
179 #define SLOP (16)
180
181 -static void *read_patch_file(int fd, size_t *sizep)
181 +static void read_patch_file(struct strbuf *sb, int fd)
182 {
183 - struct strbuf buf;
184 -
185 - strbuf_init(&buf, 0);
186 - if (strbuf_read(&buf, fd, 0) < 0)
183 + if (strbuf_read(sb, fd, 0) < 0)
184 die("git-apply: read returned %s", strerror(errno));
185
186 /*
@@ -191,9 +188,8 @@ static void *read_patch_file(int fd, size_t *sizep)
188 * so that we can do speculative "memcmp" etc, and
189 * see to it that it is NUL-filled.
190 */
194 - strbuf_grow(&buf, SLOP);
195 - memset(buf.buf + buf.len, 0, SLOP);
196 - return strbuf_detach(&buf, sizep);
191 + strbuf_grow(sb, SLOP);
192 + memset(sb->buf + sb->len, 0, SLOP);
193 }
194
195 static unsigned long linelen(const char *buffer, unsigned long size)
@@ -2633,22 +2629,22 @@ static void prefix_patches(struct patch *p)
2629
2630 static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2631 {
2636 - unsigned long offset, size;
2637 - char *buffer = read_patch_file(fd, &size);
2632 + size_t offset;
2633 + struct strbuf buf;
2634 struct patch *list = NULL, **listp = &list;
2635 int skipped_patch = 0;
2636
2637 + strbuf_init(&buf, 0);
2638 patch_input_file = filename;
2642 - if (!buffer)
2643 - return -1;
2639 + read_patch_file(&buf, fd);
2640 offset = 0;
2645 - while (size > 0) {
2641 + while (offset < buf.len) {
2642 struct patch *patch;
2643 int nr;
2644
2645 patch = xcalloc(1, sizeof(*patch));
2646 patch->inaccurate_eof = inaccurate_eof;
2651 - nr = parse_chunk(buffer + offset, size, patch);
2647 + nr = parse_chunk(buf.buf + offset, buf.len, patch);
2648 if (nr < 0)
2649 break;
2650 if (apply_in_reverse)
@@ -2666,7 +2662,6 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2662 skipped_patch++;
2663 }
2664 offset += nr;
2669 - size -= nr;
2665 }
2666
2667 if (whitespace_error && (new_whitespace == error_on_whitespace))
@@ -2701,7 +2696,7 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
2696 if (summary)
2697 summary_patch_list(list);
2698
2704 - free(buffer);
2699 + strbuf_release(&buf);
2700 return 0;
2701 }
2702