sequencer: factor out strbuf_read_file_or_whine()
Reduce code duplication by factoring out a function that reads an entire file into a strbuf, or reports errors on stderr if something goes wrong. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
570676e011
commit
878056005e
74
sequencer.c
74
sequencer.c
@ -1333,22 +1333,31 @@ static int count_commands(struct todo_list *todo_list)
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
ssize_t len;
|
||||||
|
|
||||||
|
fd = open(path, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
return error_errno(_("could not open '%s'"), path);
|
||||||
|
len = strbuf_read(sb, fd, 0);
|
||||||
|
close(fd);
|
||||||
|
if (len < 0)
|
||||||
|
return error(_("could not read '%s'."), path);
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
static int read_populate_todo(struct todo_list *todo_list,
|
static int read_populate_todo(struct todo_list *todo_list,
|
||||||
struct replay_opts *opts)
|
struct replay_opts *opts)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
const char *todo_file = get_todo_path(opts);
|
const char *todo_file = get_todo_path(opts);
|
||||||
int fd, res;
|
int res;
|
||||||
|
|
||||||
strbuf_reset(&todo_list->buf);
|
strbuf_reset(&todo_list->buf);
|
||||||
fd = open(todo_file, O_RDONLY);
|
if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0)
|
||||||
if (fd < 0)
|
return -1;
|
||||||
return error_errno(_("could not open '%s'"), todo_file);
|
|
||||||
if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
|
|
||||||
close(fd);
|
|
||||||
return error(_("could not read '%s'."), todo_file);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
|
|
||||||
res = stat(todo_file, &st);
|
res = stat(todo_file, &st);
|
||||||
if (res)
|
if (res)
|
||||||
@ -2575,20 +2584,13 @@ int check_todo_list(void)
|
|||||||
struct strbuf todo_file = STRBUF_INIT;
|
struct strbuf todo_file = STRBUF_INIT;
|
||||||
struct todo_list todo_list = TODO_LIST_INIT;
|
struct todo_list todo_list = TODO_LIST_INIT;
|
||||||
struct strbuf missing = STRBUF_INIT;
|
struct strbuf missing = STRBUF_INIT;
|
||||||
int advise_to_edit_todo = 0, res = 0, fd, i;
|
int advise_to_edit_todo = 0, res = 0, i;
|
||||||
|
|
||||||
strbuf_addstr(&todo_file, rebase_path_todo());
|
strbuf_addstr(&todo_file, rebase_path_todo());
|
||||||
fd = open(todo_file.buf, O_RDONLY);
|
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
|
||||||
if (fd < 0) {
|
res = -1;
|
||||||
res = error_errno(_("could not open '%s'"), todo_file.buf);
|
|
||||||
goto leave_check;
|
goto leave_check;
|
||||||
}
|
}
|
||||||
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
|
|
||||||
close(fd);
|
|
||||||
res = error(_("could not read '%s'."), todo_file.buf);
|
|
||||||
goto leave_check;
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
advise_to_edit_todo = res =
|
advise_to_edit_todo = res =
|
||||||
parse_insn_buffer(todo_list.buf.buf, &todo_list);
|
parse_insn_buffer(todo_list.buf.buf, &todo_list);
|
||||||
|
|
||||||
@ -2604,17 +2606,10 @@ int check_todo_list(void)
|
|||||||
|
|
||||||
todo_list_release(&todo_list);
|
todo_list_release(&todo_list);
|
||||||
strbuf_addstr(&todo_file, ".backup");
|
strbuf_addstr(&todo_file, ".backup");
|
||||||
fd = open(todo_file.buf, O_RDONLY);
|
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
|
||||||
if (fd < 0) {
|
res = -1;
|
||||||
res = error_errno(_("could not open '%s'"), todo_file.buf);
|
|
||||||
goto leave_check;
|
goto leave_check;
|
||||||
}
|
}
|
||||||
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
|
|
||||||
close(fd);
|
|
||||||
res = error(_("could not read '%s'."), todo_file.buf);
|
|
||||||
goto leave_check;
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
strbuf_release(&todo_file);
|
strbuf_release(&todo_file);
|
||||||
res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
|
res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
|
||||||
|
|
||||||
@ -2682,15 +2677,8 @@ int skip_unnecessary_picks(void)
|
|||||||
}
|
}
|
||||||
strbuf_release(&buf);
|
strbuf_release(&buf);
|
||||||
|
|
||||||
fd = open(todo_file, O_RDONLY);
|
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
|
||||||
if (fd < 0) {
|
return -1;
|
||||||
return error_errno(_("could not open '%s'"), todo_file);
|
|
||||||
}
|
|
||||||
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
|
|
||||||
close(fd);
|
|
||||||
return error(_("could not read '%s'."), todo_file);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
|
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
|
||||||
todo_list_release(&todo_list);
|
todo_list_release(&todo_list);
|
||||||
return -1;
|
return -1;
|
||||||
@ -2799,17 +2787,11 @@ int rearrange_squash(void)
|
|||||||
const char *todo_file = rebase_path_todo();
|
const char *todo_file = rebase_path_todo();
|
||||||
struct todo_list todo_list = TODO_LIST_INIT;
|
struct todo_list todo_list = TODO_LIST_INIT;
|
||||||
struct hashmap subject2item;
|
struct hashmap subject2item;
|
||||||
int res = 0, rearranged = 0, *next, *tail, fd, i;
|
int res = 0, rearranged = 0, *next, *tail, i;
|
||||||
char **subjects;
|
char **subjects;
|
||||||
|
|
||||||
fd = open(todo_file, O_RDONLY);
|
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
|
||||||
if (fd < 0)
|
return -1;
|
||||||
return error_errno(_("could not open '%s'"), todo_file);
|
|
||||||
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
|
|
||||||
close(fd);
|
|
||||||
return error(_("could not read '%s'."), todo_file);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
|
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
|
||||||
todo_list_release(&todo_list);
|
todo_list_release(&todo_list);
|
||||||
return -1;
|
return -1;
|
||||||
|
Reference in New Issue
Block a user