index 16b0df20aba44d1cb208d2937807810d96446617..2a3ab3afc1f1f3b292cec1a8f5c2b7f76cdfae2b 100644 (file)
--- a/tig.c
+++ b/tig.c
} while (1);
}
+DEFINE_ALLOCATOR(realloc_io_buf, char, BUFSIZ)
+
static char *
io_get(struct io *io, int c, bool can_read)
{
char *eol;
ssize_t readsize;
- if (!io->buf) {
- io->buf = io->bufpos = malloc(BUFSIZ);
- if (!io->buf)
- return NULL;
- io->bufalloc = BUFSIZ;
- io->bufsize = 0;
- }
-
while (TRUE) {
if (io->bufsize > 0) {
eol = memchr(io->bufpos, c, io->bufsize);
if (io->bufsize > 0 && io->bufpos > io->buf)
memmove(io->buf, io->bufpos, io->bufsize);
+ if (io->bufalloc == io->bufsize) {
+ if (!realloc_io_buf(&io->buf, io->bufalloc, BUFSIZ))
+ return NULL;
+ io->bufalloc += BUFSIZ;
+ }
+
io->bufpos = io->buf;
readsize = io_read(io, io->buf + io->bufsize, io->bufalloc - io->bufsize);
if (io_error(io))
static bool
io_read_buf(struct io *io, char buf[], size_t bufsize)
{
- bool error;
+ char *result = io_get(io, '\n', TRUE);
- io->buf = io->bufpos = buf;
- io->bufalloc = bufsize;
- error = !io_get(io, '\n', TRUE) && io_error(io);
- io->buf = NULL;
+ if (result) {
+ result = chomp_string(result);
+ string_ncopy_do(buf, bufsize, result, strlen(result));
+ }
- return done_io(io) || error;
+ return done_io(io) && result;
}
static bool
static struct run_request *run_request;
static size_t run_requests;
+DEFINE_ALLOCATOR(realloc_run_requests, struct run_request, 8)
+
static enum request
add_run_request(enum keymap keymap, int key, int argc, const char **argv)
{
if (argc >= ARRAY_SIZE(req->argv) - 1)
return REQ_NONE;
- req = realloc(run_request, (run_requests + 1) * sizeof(*run_request));
- if (!req)
+ if (!realloc_run_requests(&run_request, run_requests, 1))
return REQ_NONE;
- run_request = req;
req = &run_request[run_requests];
req->keymap = keymap;
req->key = key;
int parents;
if (!run_io_buf(revlist_argv, buf, sizeof(buf)) ||
- !*chomp_string(buf) ||
(parents = (strlen(buf) / 40) - 1) < 0) {
report("Failed to get parent information");
return FALSE;
add_describe_ref(char *buf, size_t *bufpos, const char *commit_id, const char *sep)
{
const char *describe_argv[] = { "git", "describe", commit_id, NULL };
- char refbuf[SIZEOF_STR];
- char *ref = NULL;
+ char ref[SIZEOF_STR];
- if (run_io_buf(describe_argv, refbuf, sizeof(refbuf)))
- ref = chomp_string(refbuf);
-
- if (!ref || !*ref)
+ if (!run_io_buf(describe_argv, ref, sizeof(ref)) || !*ref)
return TRUE;
/* This is the only fatal call, since it can "corrupt" the buffer. */
commit ? commit->id : "",
commit && opt_author ? commit->author : "",
commit && opt_date ? mkdate(&commit->time) : "",
+ NULL
};
return grep_text(view, text);
if (string_format(buf, "%s/rebase-merge/head-name", opt_git_dir) &&
io_open(&io, buf) &&
io_read_buf(&io, buf, sizeof(buf))) {
- head = chomp_string(buf);
+ head = buf;
if (!prefixcmp(head, "refs/heads/"))
head += STRING_SIZE("refs/heads/");
}
static int
load_refs(void)
{
+ const char *head_argv[] = {
+ "git", "symbolic-ref", "HEAD", NULL
+ };
static const char *ls_remote_argv[SIZEOF_ARG] = {
"git", "ls-remote", opt_git_dir, NULL
};
if (!*opt_git_dir)
return OK;
+ if (run_io_buf(head_argv, opt_head, sizeof(opt_head)) &&
+ !prefixcmp(opt_head, "refs/heads/")) {
+ char *offset = opt_head + STRING_SIZE("refs/heads/");
+
+ memmove(opt_head, offset, strlen(offset) + 1);
+ }
+
while (refs_size > 0)
free(refs[--refs_size].name);
while (id_refs_size > 0)
static int
load_repo_info(void)
{
- const char *head_argv[] = {
- "git", "symbolic-ref", "HEAD", NULL
- };
const char *rev_parse_argv[] = {
"git", "rev-parse", "--git-dir", "--is-inside-work-tree",
"--show-cdup", "--show-prefix", NULL
};
- if (run_io_buf(head_argv, opt_head, sizeof(opt_head))) {
- chomp_string(opt_head);
- if (!prefixcmp(opt_head, "refs/heads/")) {
- char *offset = opt_head + STRING_SIZE("refs/heads/");
-
- memmove(opt_head, offset, strlen(offset) + 1);
- }
- }
-
return run_io_load(rev_parse_argv, "=", read_repo_info);
}