Skip to content

Commit 73646bf

Browse files
rscharfegitster
authored andcommitted
sequencer: factor out rewrite_file()
Reduce code duplication by extracting a function for rewriting an existing file. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb5918a commit 73646bf

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

sequencer.c

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2665,6 +2665,20 @@ int check_todo_list(void)
26652665
return res;
26662666
}
26672667

2668+
static int rewrite_file(const char *path, const char *buf, size_t len)
2669+
{
2670+
int rc = 0;
2671+
int fd = open(path, O_WRONLY);
2672+
if (fd < 0)
2673+
return error_errno(_("could not open '%s' for writing"), path);
2674+
if (write_in_full(fd, buf, len) < 0)
2675+
rc = error_errno(_("could not write to '%s'"), path);
2676+
if (!rc && ftruncate(fd, len) < 0)
2677+
rc = error_errno(_("could not truncate '%s'"), path);
2678+
close(fd);
2679+
return rc;
2680+
}
2681+
26682682
/* skip picking commits whose parents are unchanged */
26692683
int skip_unnecessary_picks(void)
26702684
{
@@ -2737,29 +2751,11 @@ int skip_unnecessary_picks(void)
27372751
}
27382752
close(fd);
27392753

2740-
fd = open(rebase_path_todo(), O_WRONLY, 0666);
2741-
if (fd < 0) {
2742-
error_errno(_("could not open '%s' for writing"),
2743-
rebase_path_todo());
2754+
if (rewrite_file(rebase_path_todo(), todo_list.buf.buf + offset,
2755+
todo_list.buf.len - offset) < 0) {
27442756
todo_list_release(&todo_list);
27452757
return -1;
27462758
}
2747-
if (write_in_full(fd, todo_list.buf.buf + offset,
2748-
todo_list.buf.len - offset) < 0) {
2749-
error_errno(_("could not write to '%s'"),
2750-
rebase_path_todo());
2751-
close(fd);
2752-
todo_list_release(&todo_list);
2753-
return -1;
2754-
}
2755-
if (ftruncate(fd, todo_list.buf.len - offset) < 0) {
2756-
error_errno(_("could not truncate '%s'"),
2757-
rebase_path_todo());
2758-
todo_list_release(&todo_list);
2759-
close(fd);
2760-
return -1;
2761-
}
2762-
close(fd);
27632759

27642760
todo_list.current = i;
27652761
if (is_fixup(peek_command(&todo_list, 0)))
@@ -2944,15 +2940,7 @@ int rearrange_squash(void)
29442940
}
29452941
}
29462942

2947-
fd = open(todo_file, O_WRONLY);
2948-
if (fd < 0)
2949-
res = error_errno(_("could not open '%s'"), todo_file);
2950-
else if (write(fd, buf.buf, buf.len) < 0)
2951-
res = error_errno(_("could not write to '%s'"), todo_file);
2952-
else if (ftruncate(fd, buf.len) < 0)
2953-
res = error_errno(_("could not truncate '%s'"),
2954-
todo_file);
2955-
close(fd);
2943+
res = rewrite_file(todo_file, buf.buf, buf.len);
29562944
strbuf_release(&buf);
29572945
}
29582946

0 commit comments

Comments
 (0)