use write_file_buf where applicable

There are several places where we open a file, write some content from a strbuf, and close it. These can be simplified with write_file_buf(). As a bonus, many of these did not catch write problems at close() time. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jul 8, 2016 at 05:12 UTC e78d5d49935373dabcc40c5e32aefe4956a70b97
2 files changed +6 -46
builtin/am.c
+1 -6
@@ -402,13 +402,8 @@ static int read_commit_msg(struct am_state *state)
402 */
403 static void write_commit_msg(const struct am_state *state)
404 {
405 - int fd;
405 const char *filename = am_path(state, "final-commit");
407 -
408 - fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
409 - if (write_in_full(fd, state->msg, state->msg_len) < 0)
410 - die_errno(_("could not write to %s"), filename);
411 - close(fd);
406 + write_file_buf(filename, state->msg, state->msg_len);
407 }
408
409 /**
builtin/merge.c
+5 -40
@@ -336,15 +336,9 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
336 struct rev_info rev;
337 struct strbuf out = STRBUF_INIT;
338 struct commit_list *j;
339 - const char *filename;
340 - int fd;
339 struct pretty_print_context ctx = {0};
340
341 printf(_("Squash commit -- not updating HEAD\n"));
344 - filename = git_path_squash_msg();
345 - fd = open(filename, O_WRONLY | O_CREAT, 0666);
346 - if (fd < 0)
347 - die_errno(_("Could not write to '%s'"), filename);
342
343 init_revisions(&rev, NULL);
344 rev.ignore_merges = 1;
@@ -371,10 +365,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
365 oid_to_hex(&commit->object.oid));
366 pretty_print_commit(&ctx, commit, &out);
367 }
374 - if (write_in_full(fd, out.buf, out.len) != out.len)
375 - die_errno(_("Writing SQUASH_MSG"));
376 - if (close(fd))
377 - die_errno(_("Finishing SQUASH_MSG"));
368 + write_file_buf(git_path_squash_msg(), out.buf, out.len);
369 strbuf_release(&out);
370 }
371
@@ -756,18 +747,6 @@ static void add_strategies(const char *string, unsigned attr)
747
748 }
749
759 -static void write_merge_msg(struct strbuf *msg)
760 -{
761 - const char *filename = git_path_merge_msg();
762 - int fd = open(filename, O_WRONLY | O_CREAT, 0666);
763 - if (fd < 0)
764 - die_errno(_("Could not open '%s' for writing"),
765 - filename);
766 - if (write_in_full(fd, msg->buf, msg->len) != msg->len)
767 - die_errno(_("Could not write to '%s'"), filename);
768 - close(fd);
769 -}
770 -
750 static void read_merge_msg(struct strbuf *msg)
751 {
752 const char *filename = git_path_merge_msg();
@@ -801,7 +780,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
780 strbuf_addch(&msg, '\n');
781 if (0 < option_edit)
782 strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
804 - write_merge_msg(&msg);
783 + write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
784 if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
785 git_path_merge_msg(), "merge", NULL))
786 abort_commit(remoteheads, NULL);
@@ -964,8 +943,6 @@ static int setup_with_upstream(const char ***argv)
943
944 static void write_merge_state(struct commit_list *remoteheads)
945 {
967 - const char *filename;
968 - int fd;
946 struct commit_list *j;
947 struct strbuf buf = STRBUF_INIT;
948
@@ -979,26 +956,14 @@ static void write_merge_state(struct commit_list *remoteheads)
956 }
957 strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
958 }
982 - filename = git_path_merge_head();
983 - fd = open(filename, O_WRONLY | O_CREAT, 0666);
984 - if (fd < 0)
985 - die_errno(_("Could not open '%s' for writing"), filename);
986 - if (write_in_full(fd, buf.buf, buf.len) != buf.len)
987 - die_errno(_("Could not write to '%s'"), filename);
988 - close(fd);
959 + write_file_buf(git_path_merge_head(), buf.buf, buf.len);
960 strbuf_addch(&merge_msg, '\n');
990 - write_merge_msg(&merge_msg);
961 + write_file_buf(git_path_merge_msg(), merge_msg.buf, merge_msg.len);
962
992 - filename = git_path_merge_mode();
993 - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
994 - if (fd < 0)
995 - die_errno(_("Could not open '%s' for writing"), filename);
963 strbuf_reset(&buf);
964 if (fast_forward == FF_NO)
965 strbuf_addf(&buf, "no-ff");
999 - if (write_in_full(fd, buf.buf, buf.len) != buf.len)
1000 - die_errno(_("Could not write to '%s'"), filename);
1001 - close(fd);
966 + write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
967 }
968
969 static int default_edit_option(void)