@matheusbsbs / matheus-bagliano / commits / af6eb82262
Fork 0

Use strbuf API in apply, blame, commit-tree and diff

Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Pierre Habouzit committed Sep 6, 2007 at 13:20 UTC af6eb82262e35687aa8f00d688e327cb845973fa
4 files changed +44 -108
builtin-apply.c
+9 -21
@@ -12,6 +12,7 @@
12 #include "blob.h"
13 #include "delta.h"
14 #include "builtin.h"
15 +#include "strbuf.h"
16
17 /*
18 * --check turns on checking that the working tree matches the
@@ -181,34 +182,21 @@ static void say_patch_name(FILE *output, const char *pre, struct patch *patch, c
182
183 static void *read_patch_file(int fd, unsigned long *sizep)
184 {
184 - unsigned long size = 0, alloc = CHUNKSIZE;
185 - void *buffer = xmalloc(alloc);
185 + struct strbuf buf;
186
187 - for (;;) {
188 - ssize_t nr = alloc - size;
189 - if (nr < 1024) {
190 - alloc += CHUNKSIZE;
191 - buffer = xrealloc(buffer, alloc);
192 - nr = alloc - size;
193 - }
194 - nr = xread(fd, (char *) buffer + size, nr);
195 - if (!nr)
196 - break;
197 - if (nr < 0)
198 - die("git-apply: read returned %s", strerror(errno));
199 - size += nr;
200 - }
201 - *sizep = size;
187 + strbuf_init(&buf);
188 + if (strbuf_read(&buf, fd) < 0)
189 + die("git-apply: read returned %s", strerror(errno));
190 + *sizep = buf.len;
191
192 /*
193 * Make sure that we have some slop in the buffer
194 * so that we can do speculative "memcmp" etc, and
195 * see to it that it is NUL-filled.
196 */
208 - if (alloc < size + SLOP)
209 - buffer = xrealloc(buffer, size + SLOP);
210 - memset((char *) buffer + size, 0, SLOP);
211 - return buffer;
197 + strbuf_grow(&buf, SLOP);
198 + memset(buf.buf + buf.len, 0, SLOP);
199 + return strbuf_detach(&buf);
200 }
201
202 static unsigned long linelen(const char *buffer, unsigned long size)
builtin-blame.c
+12 -23
@@ -18,6 +18,7 @@
18 #include "cache-tree.h"
19 #include "path-list.h"
20 #include "mailmap.h"
21 +#include "strbuf.h"
22
23 static char blame_usage[] =
24 "git-blame [-c] [-b] [-l] [--root] [-t] [-f] [-n] [-s] [-p] [-w] [-L n,m] [-S <revs-file>] [-M] [-C] [-C] [--contents <filename>] [--incremental] [commit] [--] file\n"
@@ -2001,11 +2002,10 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
2002 struct commit *commit;
2003 struct origin *origin;
2004 unsigned char head_sha1[20];
2004 - char *buf;
2005 + struct strbuf buf;
2006 const char *ident;
2007 int fd;
2008 time_t now;
2008 - unsigned long fin_size;
2009 int size, len;
2010 struct cache_entry *ce;
2011 unsigned mode;
@@ -2023,9 +2023,11 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
2023
2024 origin = make_origin(commit, path);
2025
2026 + strbuf_init(&buf);
2027 if (!contents_from || strcmp("-", contents_from)) {
2028 struct stat st;
2029 const char *read_from;
2030 + unsigned long fin_size;
2031
2032 if (contents_from) {
2033 if (stat(contents_from, &st) < 0)
@@ -2038,19 +2040,19 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
2040 read_from = path;
2041 }
2042 fin_size = xsize_t(st.st_size);
2041 - buf = xmalloc(fin_size+1);
2043 mode = canon_mode(st.st_mode);
2044 switch (st.st_mode & S_IFMT) {
2045 case S_IFREG:
2046 fd = open(read_from, O_RDONLY);
2047 if (fd < 0)
2048 die("cannot open %s", read_from);
2048 - if (read_in_full(fd, buf, fin_size) != fin_size)
2049 + if (strbuf_read(&buf, fd) != xsize_t(st.st_size))
2050 die("cannot read %s", read_from);
2051 break;
2052 case S_IFLNK:
2052 - if (readlink(read_from, buf, fin_size+1) != fin_size)
2053 + if (readlink(read_from, buf.buf, buf.alloc) != fin_size)
2054 die("cannot readlink %s", read_from);
2055 + buf.len = fin_size;
2056 break;
2057 default:
2058 die("unsupported file type %s", read_from);
@@ -2059,26 +2061,13 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
2061 else {
2062 /* Reading from stdin */
2063 contents_from = "standard input";
2062 - buf = NULL;
2063 - fin_size = 0;
2064 mode = 0;
2065 - while (1) {
2066 - ssize_t cnt = 8192;
2067 - buf = xrealloc(buf, fin_size + cnt);
2068 - cnt = xread(0, buf + fin_size, cnt);
2069 - if (cnt < 0)
2070 - die("read error %s from stdin",
2071 - strerror(errno));
2072 - if (!cnt)
2073 - break;
2074 - fin_size += cnt;
2075 - }
2076 - buf = xrealloc(buf, fin_size + 1);
2065 + if (strbuf_read(&buf, 0) < 0)
2066 + die("read error %s from stdin", strerror(errno));
2067 }
2078 - buf[fin_size] = 0;
2079 - origin->file.ptr = buf;
2080 - origin->file.size = fin_size;
2081 - pretend_sha1_file(buf, fin_size, OBJ_BLOB, origin->blob_sha1);
2068 + origin->file.ptr = buf.buf;
2069 + origin->file.size = buf.len;
2070 + pretend_sha1_file(buf.buf, buf.len, OBJ_BLOB, origin->blob_sha1);
2071 commit->util = origin;
2072
2073 /*
builtin-commit-tree.c
+14 -46
@@ -8,42 +8,13 @@
8 #include "tree.h"
9 #include "builtin.h"
10 #include "utf8.h"
11 +#include "strbuf.h"
12
13 #define BLOCKING (1ul << 14)
14
15 /*
16 * FIXME! Share the code with "write-tree.c"
17 */
17 -static void init_buffer(char **bufp, unsigned int *sizep)
18 -{
19 - *bufp = xmalloc(BLOCKING);
20 - *sizep = 0;
21 -}
22 -
23 -static void add_buffer(char **bufp, unsigned int *sizep, const char *fmt, ...)
24 -{
25 - char one_line[2048];
26 - va_list args;
27 - int len;
28 - unsigned long alloc, size, newsize;
29 - char *buf;
30 -
31 - va_start(args, fmt);
32 - len = vsnprintf(one_line, sizeof(one_line), fmt, args);
33 - va_end(args);
34 - size = *sizep;
35 - newsize = size + len + 1;
36 - alloc = (size + 32767) & ~32767;
37 - buf = *bufp;
38 - if (newsize > alloc) {
39 - alloc = (newsize + 32767) & ~32767;
40 - buf = xrealloc(buf, alloc);
41 - *bufp = buf;
42 - }
43 - *sizep = newsize - 1;
44 - memcpy(buf + size, one_line, len);
45 -}
46 -
18 static void check_valid(unsigned char *sha1, enum object_type expect)
19 {
20 enum object_type type = sha1_object_info(sha1, NULL);
@@ -87,9 +58,7 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
58 int parents = 0;
59 unsigned char tree_sha1[20];
60 unsigned char commit_sha1[20];
90 - char comment[1000];
91 - char *buffer;
92 - unsigned int size;
61 + struct strbuf buffer;
62 int encoding_is_utf8;
63
64 git_config(git_default_config);
@@ -118,8 +87,9 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
87 /* Not having i18n.commitencoding is the same as having utf-8 */
88 encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
89
121 - init_buffer(&buffer, &size);
122 - add_buffer(&buffer, &size, "tree %s\n", sha1_to_hex(tree_sha1));
90 + strbuf_init(&buffer);
91 + strbuf_grow(&buffer, 8192); /* should avoid reallocs for the headers */
92 + strbuf_addf(&buffer, "tree %s\n", sha1_to_hex(tree_sha1));
93
94 /*
95 * NOTE! This ordering means that the same exact tree merged with a
@@ -127,26 +97,24 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
97 * if everything else stays the same.
98 */
99 for (i = 0; i < parents; i++)
130 - add_buffer(&buffer, &size, "parent %s\n", sha1_to_hex(parent_sha1[i]));
100 + strbuf_addf(&buffer, "parent %s\n", sha1_to_hex(parent_sha1[i]));
101
102 /* Person/date information */
133 - add_buffer(&buffer, &size, "author %s\n", git_author_info(1));
134 - add_buffer(&buffer, &size, "committer %s\n", git_committer_info(1));
103 + strbuf_addf(&buffer, "author %s\n", git_author_info(1));
104 + strbuf_addf(&buffer, "committer %s\n", git_committer_info(1));
105 if (!encoding_is_utf8)
136 - add_buffer(&buffer, &size,
137 - "encoding %s\n", git_commit_encoding);
138 - add_buffer(&buffer, &size, "\n");
106 + strbuf_addf(&buffer, "encoding %s\n", git_commit_encoding);
107 + strbuf_addch(&buffer, '\n');
108
109 /* And add the comment */
141 - while (fgets(comment, sizeof(comment), stdin) != NULL)
142 - add_buffer(&buffer, &size, "%s", comment);
110 + if (strbuf_read(&buffer, 0) < 0)
111 + die("git-commit-tree: read returned %s", strerror(errno));
112
113 /* And check the encoding */
145 - buffer[size] = '\0';
146 - if (encoding_is_utf8 && !is_utf8(buffer))
114 + if (encoding_is_utf8 && !is_utf8(buffer.buf))
115 fprintf(stderr, commit_utf8_warn);
116
149 - if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {
117 + if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) {
118 printf("%s\n", sha1_to_hex(commit_sha1));
119 return 0;
120 }
diff.c
+9 -18
@@ -9,6 +9,7 @@
9 #include "xdiff-interface.h"
10 #include "color.h"
11 #include "attr.h"
12 +#include "strbuf.h"
13
14 #ifdef NO_FAST_WORKING_DIRECTORY
15 #define FAST_WORKING_DIRECTORY 0
@@ -1545,26 +1546,16 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
1546
1547 static int populate_from_stdin(struct diff_filespec *s)
1548 {
1548 -#define INCREMENT 1024
1549 - char *buf;
1550 - unsigned long size;
1551 - ssize_t got;
1552 -
1553 - size = 0;
1554 - buf = NULL;
1555 - while (1) {
1556 - buf = xrealloc(buf, size + INCREMENT);
1557 - got = xread(0, buf + size, INCREMENT);
1558 - if (!got)
1559 - break; /* EOF */
1560 - if (got < 0)
1561 - return error("error while reading from stdin %s",
1549 + struct strbuf buf;
1550 +
1551 + strbuf_init(&buf);
1552 + if (strbuf_read(&buf, 0) < 0)
1553 + return error("error while reading from stdin %s",
1554 strerror(errno));
1563 - size += got;
1564 - }
1555 +
1556 s->should_munmap = 0;
1566 - s->data = buf;
1567 - s->size = size;
1557 + s->size = buf.len;
1558 + s->data = strbuf_detach(&buf);
1559 s->should_free = 1;
1560 return 0;
1561 }