Skip to content

Commit c14d5f9

Browse files
committed
Merge branch 'rs/strbuf-read-file-or-whine'
Code clean-up. * rs/strbuf-read-file-or-whine: sequencer: factor out strbuf_read_file_or_whine()
2 parents 327e524 + 8780560 commit c14d5f9

File tree

1 file changed

+28
-46
lines changed

1 file changed

+28
-46
lines changed

sequencer.c

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,22 +1869,31 @@ static int count_commands(struct todo_list *todo_list)
18691869
return count;
18701870
}
18711871

1872+
static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
1873+
{
1874+
int fd;
1875+
ssize_t len;
1876+
1877+
fd = open(path, O_RDONLY);
1878+
if (fd < 0)
1879+
return error_errno(_("could not open '%s'"), path);
1880+
len = strbuf_read(sb, fd, 0);
1881+
close(fd);
1882+
if (len < 0)
1883+
return error(_("could not read '%s'."), path);
1884+
return len;
1885+
}
1886+
18721887
static int read_populate_todo(struct todo_list *todo_list,
18731888
struct replay_opts *opts)
18741889
{
18751890
struct stat st;
18761891
const char *todo_file = get_todo_path(opts);
1877-
int fd, res;
1892+
int res;
18781893

18791894
strbuf_reset(&todo_list->buf);
1880-
fd = open(todo_file, O_RDONLY);
1881-
if (fd < 0)
1882-
return error_errno(_("could not open '%s'"), todo_file);
1883-
if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
1884-
close(fd);
1885-
return error(_("could not read '%s'."), todo_file);
1886-
}
1887-
close(fd);
1895+
if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0)
1896+
return -1;
18881897

18891898
res = stat(todo_file, &st);
18901899
if (res)
@@ -3155,20 +3164,13 @@ int check_todo_list(void)
31553164
struct strbuf todo_file = STRBUF_INIT;
31563165
struct todo_list todo_list = TODO_LIST_INIT;
31573166
struct strbuf missing = STRBUF_INIT;
3158-
int advise_to_edit_todo = 0, res = 0, fd, i;
3167+
int advise_to_edit_todo = 0, res = 0, i;
31593168

31603169
strbuf_addstr(&todo_file, rebase_path_todo());
3161-
fd = open(todo_file.buf, O_RDONLY);
3162-
if (fd < 0) {
3163-
res = error_errno(_("could not open '%s'"), todo_file.buf);
3164-
goto leave_check;
3165-
}
3166-
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
3167-
close(fd);
3168-
res = error(_("could not read '%s'."), todo_file.buf);
3170+
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
3171+
res = -1;
31693172
goto leave_check;
31703173
}
3171-
close(fd);
31723174
advise_to_edit_todo = res =
31733175
parse_insn_buffer(todo_list.buf.buf, &todo_list);
31743176

@@ -3184,17 +3186,10 @@ int check_todo_list(void)
31843186

31853187
todo_list_release(&todo_list);
31863188
strbuf_addstr(&todo_file, ".backup");
3187-
fd = open(todo_file.buf, O_RDONLY);
3188-
if (fd < 0) {
3189-
res = error_errno(_("could not open '%s'"), todo_file.buf);
3190-
goto leave_check;
3191-
}
3192-
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
3193-
close(fd);
3194-
res = error(_("could not read '%s'."), todo_file.buf);
3189+
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
3190+
res = -1;
31953191
goto leave_check;
31963192
}
3197-
close(fd);
31983193
strbuf_release(&todo_file);
31993194
res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
32003195

@@ -3275,15 +3270,8 @@ int skip_unnecessary_picks(void)
32753270
}
32763271
strbuf_release(&buf);
32773272

3278-
fd = open(todo_file, O_RDONLY);
3279-
if (fd < 0) {
3280-
return error_errno(_("could not open '%s'"), todo_file);
3281-
}
3282-
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
3283-
close(fd);
3284-
return error(_("could not read '%s'."), todo_file);
3285-
}
3286-
close(fd);
3273+
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
3274+
return -1;
32873275
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
32883276
todo_list_release(&todo_list);
32893277
return -1;
@@ -3374,17 +3362,11 @@ int rearrange_squash(void)
33743362
const char *todo_file = rebase_path_todo();
33753363
struct todo_list todo_list = TODO_LIST_INIT;
33763364
struct hashmap subject2item;
3377-
int res = 0, rearranged = 0, *next, *tail, fd, i;
3365+
int res = 0, rearranged = 0, *next, *tail, i;
33783366
char **subjects;
33793367

3380-
fd = open(todo_file, O_RDONLY);
3381-
if (fd < 0)
3382-
return error_errno(_("could not open '%s'"), todo_file);
3383-
if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
3384-
close(fd);
3385-
return error(_("could not read '%s'."), todo_file);
3386-
}
3387-
close(fd);
3368+
if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
3369+
return -1;
33883370
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
33893371
todo_list_release(&todo_list);
33903372
return -1;

0 commit comments

Comments
 (0)