Replace all read_fd use with strbuf_read, and get rid of it.
This brings builtin-stripspace, builtin-tag and mktag to use strbufs. Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Pierre Habouzit committed
Sep 10, 2007 at 12:35 UTC
fd17f5b5f77716bf90098c6e49e3cf7fd9f56306
5 files changed
+56
-120
builtin-stripspace.c
+9
-12
@@ -1,5 +1,6 @@
1
#include "builtin.h"
2
#include "cache.h"
3
+#include "strbuf.h"
4
5
/*
6
* Returns the length of a line, without trailing spaces.
@@ -74,26 +75,22 @@ size_t stripspace(char *buffer, size_t length, int skip_comments)
75
76
int cmd_stripspace(int argc, const char **argv, const char *prefix)
77
{
77
- char *buffer;
78
- unsigned long size;
78
+ struct strbuf buf;
79
int strip_comments = 0;
80
81
if (argc > 1 && (!strcmp(argv[1], "-s") ||
82
!strcmp(argv[1], "--strip-comments")))
83
strip_comments = 1;
84
85
- size = 1024;
86
- buffer = xmalloc(size);
87
- if (read_fd(0, &buffer, &size)) {
88
- free(buffer);
85
+ strbuf_init(&buf, 0);
86
+ if (strbuf_read(&buf, 0, 1024) < 0)
87
die("could not read the input");
90
- }
88
92
- size = stripspace(buffer, size, strip_comments);
93
- write_or_die(1, buffer, size);
94
- if (size)
95
- putc('\n', stdout);
89
+ strbuf_setlen(&buf, stripspace(buf.buf, buf.len, strip_comments));
90
+ if (buf.len)
91
+ strbuf_addch(&buf, '\n');
92
97
- free(buffer);
93
+ write_or_die(1, buf.buf, buf.len);
94
+ strbuf_release(&buf);
95
return 0;
96
}
builtin-tag.c
+31
-48
@@ -8,6 +8,7 @@
8
9
#include "cache.h"
10
#include "builtin.h"
11
+#include "strbuf.h"
12
#include "refs.h"
13
#include "tag.h"
14
#include "run-command.h"
@@ -17,7 +18,7 @@ static const char builtin_tag_usage[] =
18
19
static char signingkey[1000];
20
20
-static void launch_editor(const char *path, char **buffer, unsigned long *len)
21
+static void launch_editor(const char *path, struct strbuf *buffer)
22
{
23
const char *editor, *terminal;
24
struct child_process child;
@@ -55,10 +56,8 @@ static void launch_editor(const char *path, char **buffer, unsigned long *len)
56
fd = open(path, O_RDONLY);
57
if (fd < 0)
58
die("could not open '%s': %s", path, strerror(errno));
58
- if (read_fd(fd, buffer, len)) {
59
- free(*buffer);
60
- die("could not read message file '%s': %s",
61
- path, strerror(errno));
59
+ if (strbuf_read(buffer, fd, 0) < 0) {
60
+ die("could not read message file '%s': %s", path, strerror(errno));
61
}
62
close(fd);
63
}
@@ -184,7 +183,7 @@ static int verify_tag(const char *name, const char *ref,
183
return 0;
184
}
185
187
-static ssize_t do_sign(char *buffer, size_t size, size_t max)
186
+static int do_sign(struct strbuf *buffer)
187
{
188
struct child_process gpg;
189
const char *args[4];
@@ -216,22 +215,22 @@ static ssize_t do_sign(char *buffer, size_t size, size_t max)
215
if (start_command(&gpg))
216
return error("could not run gpg.");
217
219
- if (write_in_full(gpg.in, buffer, size) != size) {
218
+ if (write_in_full(gpg.in, buffer->buf, buffer->len) != buffer->len) {
219
close(gpg.in);
220
finish_command(&gpg);
221
return error("gpg did not accept the tag data");
222
}
223
close(gpg.in);
224
gpg.close_in = 0;
226
- len = read_in_full(gpg.out, buffer + size, max - size);
225
+ len = strbuf_read(buffer, gpg.out, 1024);
226
227
if (finish_command(&gpg) || !len || len < 0)
228
return error("gpg failed to sign the tag");
229
231
- if (len == max - size)
230
+ if (len < 0)
231
return error("could not read the entire signature from gpg.");
232
234
- return size + len;
233
+ return 0;
234
}
235
236
static const char tag_template[] =
@@ -254,15 +253,13 @@ static int git_tag_config(const char *var, const char *value)
253
return git_default_config(var, value);
254
}
255
257
-#define MAX_SIGNATURE_LENGTH 1024
258
-/* message must be NULL or allocated, it will be reallocated and freed */
256
static void create_tag(const unsigned char *object, const char *tag,
260
- char *message, int sign, unsigned char *result)
257
+ struct strbuf *buf, int message, int sign,
258
+ unsigned char *result)
259
{
260
enum object_type type;
263
- char header_buf[1024], *buffer = NULL;
264
- int header_len, max_size;
265
- unsigned long size = 0;
261
+ char header_buf[1024];
262
+ int header_len;
263
264
type = sha1_object_info(object, NULL);
265
if (type <= OBJ_NONE)
@@ -294,53 +291,40 @@ static void create_tag(const unsigned char *object, const char *tag,
291
write_or_die(fd, tag_template, strlen(tag_template));
292
close(fd);
293
297
- launch_editor(path, &buffer, &size);
294
+ launch_editor(path, buf);
295
296
unlink(path);
297
free(path);
298
}
302
- else {
303
- buffer = message;
304
- size = strlen(message);
305
- }
299
307
- size = stripspace(buffer, size, 1);
300
+ strbuf_setlen(buf, stripspace(buf->buf, buf->len, 1));
301
309
- if (!message && !size)
302
+ if (!message && !buf->len)
303
die("no tag message?");
304
305
/* insert the header and add the '\n' if needed: */
313
- max_size = header_len + size + (sign ? MAX_SIGNATURE_LENGTH : 0) + 1;
314
- buffer = xrealloc(buffer, max_size);
315
- if (size)
316
- buffer[size++] = '\n';
317
- memmove(buffer + header_len, buffer, size);
318
- memcpy(buffer, header_buf, header_len);
319
- size += header_len;
320
-
321
- if (sign) {
322
- ssize_t r = do_sign(buffer, size, max_size);
323
- if (r < 0)
324
- die("unable to sign the tag");
325
- size = r;
326
- }
306
+ if (buf->len)
307
+ strbuf_addch(buf, '\n');
308
+ strbuf_insert(buf, 0, header_buf, header_len);
309
328
- if (write_sha1_file(buffer, size, tag_type, result) < 0)
310
+ if (sign && do_sign(buf) < 0)
311
+ die("unable to sign the tag");
312
+ if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
313
die("unable to write tag file");
330
- free(buffer);
314
}
315
316
int cmd_tag(int argc, const char **argv, const char *prefix)
317
{
318
+ struct strbuf buf;
319
unsigned char object[20], prev[20];
336
- int annotate = 0, sign = 0, force = 0, lines = 0;
337
- char *message = NULL;
320
+ int annotate = 0, sign = 0, force = 0, lines = 0, message = 0;
321
char ref[PATH_MAX];
322
const char *object_ref, *tag;
323
int i;
324
struct ref_lock *lock;
325
326
git_config(git_tag_config);
327
+ strbuf_init(&buf, 0);
328
329
for (i = 1; i < argc; i++) {
330
const char *arg = argv[i];
@@ -376,11 +360,11 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
360
die("option -m needs an argument.");
361
if (message)
362
die("only one -F or -m option is allowed.");
379
- message = xstrdup(argv[i]);
363
+ strbuf_addstr(&buf, argv[i]);
364
+ message = 1;
365
continue;
366
}
367
if (!strcmp(arg, "-F")) {
383
- unsigned long len;
368
int fd;
369
370
annotate = 1;
@@ -398,12 +382,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
382
die("could not open '%s': %s",
383
argv[i], strerror(errno));
384
}
401
- len = 1024;
402
- message = xmalloc(len);
403
- if (read_fd(fd, &message, &len)) {
404
- free(message);
385
+ if (strbuf_read(&buf, fd, 1024) < 0) {
386
die("cannot read %s", argv[i]);
387
}
388
+ message = 1;
389
continue;
390
}
391
if (!strcmp(arg, "-u")) {
@@ -451,7 +433,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
433
die("tag '%s' already exists", tag);
434
435
if (annotate)
454
- create_tag(object, tag, message, sign, object);
436
+ create_tag(object, tag, &buf, message, sign, object);
437
438
lock = lock_any_ref_for_update(ref, prev, 0);
439
if (!lock)
@@ -459,5 +441,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
441
if (write_ref_sha1(lock, object, NULL) < 0)
442
die("%s: cannot update the ref", ref);
443
444
+ strbuf_release(&buf);
445
return 0;
446
}
cache.h
-1
@@ -269,7 +269,6 @@ extern int ie_match_stat(struct index_state *, struct cache_entry *, struct stat
269
extern int ie_modified(struct index_state *, struct cache_entry *, struct stat *, int);
270
extern int ce_path_match(const struct cache_entry *ce, const char **pathspec);
271
extern int index_fd(unsigned char *sha1, int fd, struct stat *st, int write_object, enum object_type type, const char *path);
272
-extern int read_fd(int fd, char **return_buf, unsigned long *return_size);
272
extern int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object);
273
extern int index_path(unsigned char *sha1, const char *path, struct stat *st, int write_object);
274
extern void fill_stat_cache_info(struct cache_entry *ce, struct stat *st);
mktag.c
+7
-8
@@ -1,4 +1,5 @@
1
#include "cache.h"
2
+#include "strbuf.h"
3
#include "tag.h"
4
5
/*
@@ -111,8 +112,7 @@ static int verify_tag(char *buffer, unsigned long size)
112
113
int main(int argc, char **argv)
114
{
114
- unsigned long size = 4096;
115
- char *buffer = xmalloc(size);
115
+ struct strbuf buf;
116
unsigned char result_sha1[20];
117
118
if (argc != 1)
@@ -120,21 +120,20 @@ int main(int argc, char **argv)
120
121
setup_git_directory();
122
123
- if (read_fd(0, &buffer, &size)) {
124
- free(buffer);
123
+ strbuf_init(&buf, 0);
124
+ if (strbuf_read(&buf, 0, 4096) < 0) {
125
die("could not read from stdin");
126
}
127
128
/* Verify it for some basic sanity: it needs to start with
129
"object <sha1>\ntype\ntagger " */
130
- if (verify_tag(buffer, size) < 0)
130
+ if (verify_tag(buf.buf, buf.len) < 0)
131
die("invalid tag signature file");
132
133
- if (write_sha1_file(buffer, size, tag_type, result_sha1) < 0)
133
+ if (write_sha1_file(buf.buf, buf.len, tag_type, result_sha1) < 0)
134
die("unable to write tag file");
135
136
- free(buffer);
137
-
136
+ strbuf_release(&buf);
137
printf("%s\n", sha1_to_hex(result_sha1));
138
return 0;
139
}
sha1_file.c
+9
-51
@@ -14,6 +14,7 @@
14
#include "tag.h"
15
#include "tree.h"
16
#include "refs.h"
17
+#include "strbuf.h"
18
19
#ifndef O_NOATIME
20
#if defined(__linux__) && (defined(__i386__) || defined(__PPC__))
@@ -2302,68 +2303,25 @@ int has_sha1_file(const unsigned char *sha1)
2303
return find_sha1_file(sha1, &st) ? 1 : 0;
2304
}
2305
2305
-/*
2306
- * reads from fd as long as possible into a supplied buffer of size bytes.
2307
- * If necessary the buffer's size is increased using realloc()
2308
- *
2309
- * returns 0 if anything went fine and -1 otherwise
2310
- *
2311
- * The buffer is always NUL-terminated, not including it in returned size.
2312
- *
2313
- * NOTE: both buf and size may change, but even when -1 is returned
2314
- * you still have to free() it yourself.
2315
- */
2316
-int read_fd(int fd, char **return_buf, unsigned long *return_size)
2317
-{
2318
- char *buf = *return_buf;
2319
- unsigned long size = *return_size;
2320
- ssize_t iret;
2321
- unsigned long off = 0;
2322
-
2323
- if (!buf || size <= 1) {
2324
- size = 1024;
2325
- buf = xrealloc(buf, size);
2326
- }
2327
-
2328
- do {
2329
- iret = xread(fd, buf + off, (size - 1) - off);
2330
- if (iret > 0) {
2331
- off += iret;
2332
- if (off == size - 1) {
2333
- size = alloc_nr(size);
2334
- buf = xrealloc(buf, size);
2335
- }
2336
- }
2337
- } while (iret > 0);
2338
-
2339
- buf[off] = '\0';
2340
-
2341
- *return_buf = buf;
2342
- *return_size = off;
2343
-
2344
- if (iret < 0)
2345
- return -1;
2346
- return 0;
2347
-}
2348
-
2306
int index_pipe(unsigned char *sha1, int fd, const char *type, int write_object)
2307
{
2351
- unsigned long size = 4096;
2352
- char *buf = xmalloc(size);
2308
+ struct strbuf buf;
2309
int ret;
2310
2355
- if (read_fd(fd, &buf, &size)) {
2356
- free(buf);
2311
+ strbuf_init(&buf, 0);
2312
+ if (strbuf_read(&buf, fd, 4096) < 0) {
2313
+ strbuf_release(&buf);
2314
return -1;
2315
}
2316
2317
if (!type)
2318
type = blob_type;
2319
if (write_object)
2363
- ret = write_sha1_file(buf, size, type, sha1);
2320
+ ret = write_sha1_file(buf.buf, buf.len, type, sha1);
2321
else
2365
- ret = hash_sha1_file(buf, size, type, sha1);
2366
- free(buf);
2322
+ ret = hash_sha1_file(buf.buf, buf.len, type, sha1);
2323
+ strbuf_release(&buf);
2324
+
2325
return ret;
2326
}
2327