Skip to content

Commit af6eb82

Browse files
MadCodergitster
authored andcommitted
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>
1 parent d52bc66 commit af6eb82

File tree

4 files changed

+44
-108
lines changed

4 files changed

+44
-108
lines changed

builtin-apply.c

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "blob.h"
1313
#include "delta.h"
1414
#include "builtin.h"
15+
#include "strbuf.h"
1516

1617
/*
1718
* --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
181182

182183
static void *read_patch_file(int fd, unsigned long *sizep)
183184
{
184-
unsigned long size = 0, alloc = CHUNKSIZE;
185-
void *buffer = xmalloc(alloc);
185+
struct strbuf buf;
186186

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;
202191

203192
/*
204193
* Make sure that we have some slop in the buffer
205194
* so that we can do speculative "memcmp" etc, and
206195
* see to it that it is NUL-filled.
207196
*/
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);
212200
}
213201

214202
static unsigned long linelen(const char *buffer, unsigned long size)

builtin-blame.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "cache-tree.h"
1919
#include "path-list.h"
2020
#include "mailmap.h"
21+
#include "strbuf.h"
2122

2223
static char blame_usage[] =
2324
"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
20012002
struct commit *commit;
20022003
struct origin *origin;
20032004
unsigned char head_sha1[20];
2004-
char *buf;
2005+
struct strbuf buf;
20052006
const char *ident;
20062007
int fd;
20072008
time_t now;
2008-
unsigned long fin_size;
20092009
int size, len;
20102010
struct cache_entry *ce;
20112011
unsigned mode;
@@ -2023,9 +2023,11 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20232023

20242024
origin = make_origin(commit, path);
20252025

2026+
strbuf_init(&buf);
20262027
if (!contents_from || strcmp("-", contents_from)) {
20272028
struct stat st;
20282029
const char *read_from;
2030+
unsigned long fin_size;
20292031

20302032
if (contents_from) {
20312033
if (stat(contents_from, &st) < 0)
@@ -2038,19 +2040,19 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20382040
read_from = path;
20392041
}
20402042
fin_size = xsize_t(st.st_size);
2041-
buf = xmalloc(fin_size+1);
20422043
mode = canon_mode(st.st_mode);
20432044
switch (st.st_mode & S_IFMT) {
20442045
case S_IFREG:
20452046
fd = open(read_from, O_RDONLY);
20462047
if (fd < 0)
20472048
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))
20492050
die("cannot read %s", read_from);
20502051
break;
20512052
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)
20532054
die("cannot readlink %s", read_from);
2055+
buf.len = fin_size;
20542056
break;
20552057
default:
20562058
die("unsupported file type %s", read_from);
@@ -2059,26 +2061,13 @@ static struct commit *fake_working_tree_commit(const char *path, const char *con
20592061
else {
20602062
/* Reading from stdin */
20612063
contents_from = "standard input";
2062-
buf = NULL;
2063-
fin_size = 0;
20642064
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));
20772067
}
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);
20822071
commit->util = origin;
20832072

20842073
/*

builtin-commit-tree.c

Lines changed: 14 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,13 @@
88
#include "tree.h"
99
#include "builtin.h"
1010
#include "utf8.h"
11+
#include "strbuf.h"
1112

1213
#define BLOCKING (1ul << 14)
1314

1415
/*
1516
* FIXME! Share the code with "write-tree.c"
1617
*/
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-
4718
static void check_valid(unsigned char *sha1, enum object_type expect)
4819
{
4920
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)
8758
int parents = 0;
8859
unsigned char tree_sha1[20];
8960
unsigned char commit_sha1[20];
90-
char comment[1000];
91-
char *buffer;
92-
unsigned int size;
61+
struct strbuf buffer;
9362
int encoding_is_utf8;
9463

9564
git_config(git_default_config);
@@ -118,35 +87,34 @@ int cmd_commit_tree(int argc, const char **argv, const char *prefix)
11887
/* Not having i18n.commitencoding is the same as having utf-8 */
11988
encoding_is_utf8 = is_encoding_utf8(git_commit_encoding);
12089

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));
12393

12494
/*
12595
* NOTE! This ordering means that the same exact tree merged with a
12696
* different order of parents will be a _different_ changeset even
12797
* if everything else stays the same.
12898
*/
12999
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]));
131101

132102
/* 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));
135105
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');
139108

140109
/* 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));
143112

144113
/* 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))
147115
fprintf(stderr, commit_utf8_warn);
148116

149-
if (!write_sha1_file(buffer, size, commit_type, commit_sha1)) {
117+
if (!write_sha1_file(buffer.buf, buffer.len, commit_type, commit_sha1)) {
150118
printf("%s\n", sha1_to_hex(commit_sha1));
151119
return 0;
152120
}

diff.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "xdiff-interface.h"
1010
#include "color.h"
1111
#include "attr.h"
12+
#include "strbuf.h"
1213

1314
#ifdef NO_FAST_WORKING_DIRECTORY
1415
#define FAST_WORKING_DIRECTORY 0
@@ -1545,26 +1546,16 @@ static int reuse_worktree_file(const char *name, const unsigned char *sha1, int
15451546

15461547
static int populate_from_stdin(struct diff_filespec *s)
15471548
{
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",
15621554
strerror(errno));
1563-
size += got;
1564-
}
1555+
15651556
s->should_munmap = 0;
1566-
s->data = buf;
1567-
s->size = size;
1557+
s->size = buf.len;
1558+
s->data = strbuf_detach(&buf);
15681559
s->should_free = 1;
15691560
return 0;
15701561
}

0 commit comments

Comments
 (0)