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>
René Scharfe committed
Feb 22, 2018 at 20:29 UTC
878056005e94f5fea00c9ff8999f30a99bf10cae
1 file changed
+28
-46
sequencer.c
+28
-46
@@ -1333,22 +1333,31 @@ static int count_commands(struct todo_list *todo_list)
1333
return count;
1334
}
1335
1336
+static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
1337
+{
1338
+ int fd;
1339
+ ssize_t len;
1340
+
1341
+ fd = open(path, O_RDONLY);
1342
+ if (fd < 0)
1343
+ return error_errno(_("could not open '%s'"), path);
1344
+ len = strbuf_read(sb, fd, 0);
1345
+ close(fd);
1346
+ if (len < 0)
1347
+ return error(_("could not read '%s'."), path);
1348
+ return len;
1349
+}
1350
+
1351
static int read_populate_todo(struct todo_list *todo_list,
1352
struct replay_opts *opts)
1353
{
1354
struct stat st;
1355
const char *todo_file = get_todo_path(opts);
1341
- int fd, res;
1356
+ int res;
1357
1358
strbuf_reset(&todo_list->buf);
1344
- fd = open(todo_file, O_RDONLY);
1345
- if (fd < 0)
1346
- return error_errno(_("could not open '%s'"), todo_file);
1347
- if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
1348
- close(fd);
1349
- return error(_("could not read '%s'."), todo_file);
1350
- }
1351
- close(fd);
1359
+ if (strbuf_read_file_or_whine(&todo_list->buf, todo_file) < 0)
1360
+ return -1;
1361
1362
res = stat(todo_file, &st);
1363
if (res)
@@ -2575,20 +2584,13 @@ int check_todo_list(void)
2584
struct strbuf todo_file = STRBUF_INIT;
2585
struct todo_list todo_list = TODO_LIST_INIT;
2586
struct strbuf missing = STRBUF_INIT;
2578
- int advise_to_edit_todo = 0, res = 0, fd, i;
2587
+ int advise_to_edit_todo = 0, res = 0, i;
2588
2589
strbuf_addstr(&todo_file, rebase_path_todo());
2581
- fd = open(todo_file.buf, O_RDONLY);
2582
- if (fd < 0) {
2583
- res = error_errno(_("could not open '%s'"), todo_file.buf);
2584
- goto leave_check;
2585
- }
2586
- if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2587
- close(fd);
2588
- res = error(_("could not read '%s'."), todo_file.buf);
2590
+ if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
2591
+ res = -1;
2592
goto leave_check;
2593
}
2591
- close(fd);
2594
advise_to_edit_todo = res =
2595
parse_insn_buffer(todo_list.buf.buf, &todo_list);
2596
@@ -2604,17 +2606,10 @@ int check_todo_list(void)
2606
2607
todo_list_release(&todo_list);
2608
strbuf_addstr(&todo_file, ".backup");
2607
- fd = open(todo_file.buf, O_RDONLY);
2608
- if (fd < 0) {
2609
- res = error_errno(_("could not open '%s'"), todo_file.buf);
2610
- goto leave_check;
2611
- }
2612
- if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2613
- close(fd);
2614
- res = error(_("could not read '%s'."), todo_file.buf);
2609
+ if (strbuf_read_file_or_whine(&todo_list.buf, todo_file.buf) < 0) {
2610
+ res = -1;
2611
goto leave_check;
2612
}
2617
- close(fd);
2613
strbuf_release(&todo_file);
2614
res = !!parse_insn_buffer(todo_list.buf.buf, &todo_list);
2615
@@ -2682,15 +2677,8 @@ int skip_unnecessary_picks(void)
2677
}
2678
strbuf_release(&buf);
2679
2685
- fd = open(todo_file, O_RDONLY);
2686
- if (fd < 0) {
2687
- return error_errno(_("could not open '%s'"), todo_file);
2688
- }
2689
- if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2690
- close(fd);
2691
- return error(_("could not read '%s'."), todo_file);
2692
- }
2693
- close(fd);
2680
+ if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
2681
+ return -1;
2682
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
2683
todo_list_release(&todo_list);
2684
return -1;
@@ -2799,17 +2787,11 @@ int rearrange_squash(void)
2787
const char *todo_file = rebase_path_todo();
2788
struct todo_list todo_list = TODO_LIST_INIT;
2789
struct hashmap subject2item;
2802
- int res = 0, rearranged = 0, *next, *tail, fd, i;
2790
+ int res = 0, rearranged = 0, *next, *tail, i;
2791
char **subjects;
2792
2805
- fd = open(todo_file, O_RDONLY);
2806
- if (fd < 0)
2807
- return error_errno(_("could not open '%s'"), todo_file);
2808
- if (strbuf_read(&todo_list.buf, fd, 0) < 0) {
2809
- close(fd);
2810
- return error(_("could not read '%s'."), todo_file);
2811
- }
2812
- close(fd);
2793
+ if (strbuf_read_file_or_whine(&todo_list.buf, todo_file) < 0)
2794
+ return -1;
2795
if (parse_insn_buffer(todo_list.buf.buf, &todo_list) < 0) {
2796
todo_list_release(&todo_list);
2797
return -1;