sequencer: factor out rewrite_file()
Reduce code duplication by extracting a function for rewriting an existing file. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Oct 31, 2017 at 10:54 UTC
73646bfdcb38114a4d9242a02fc180a9877e01bf
1 file changed
+17
-29
sequencer.c
+17
-29
@@ -2665,6 +2665,20 @@ leave_check:
2665
return res;
2666
}
2667
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
+
2682
/* skip picking commits whose parents are unchanged */
2683
int skip_unnecessary_picks(void)
2684
{
@@ -2737,29 +2751,11 @@ int skip_unnecessary_picks(void)
2751
}
2752
close(fd);
2753
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) {
2756
todo_list_release(&todo_list);
2757
return -1;
2758
}
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);
2759
2760
todo_list.current = i;
2761
if (is_fixup(peek_command(&todo_list, 0)))
@@ -2944,15 +2940,7 @@ int rearrange_squash(void)
2940
}
2941
}
2942
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);
2944
strbuf_release(&buf);
2945
}
2946