mirror of
https://github.com/git/git
synced 2024-11-05 18:59:29 +00:00
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>
This commit is contained in:
parent
e04d08a4b3
commit
e78d5d4993
2 changed files with 6 additions and 46 deletions
|
@ -402,13 +402,8 @@ static int read_commit_msg(struct am_state *state)
|
||||||
*/
|
*/
|
||||||
static void write_commit_msg(const struct am_state *state)
|
static void write_commit_msg(const struct am_state *state)
|
||||||
{
|
{
|
||||||
int fd;
|
|
||||||
const char *filename = am_path(state, "final-commit");
|
const char *filename = am_path(state, "final-commit");
|
||||||
|
write_file_buf(filename, state->msg, state->msg_len);
|
||||||
fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
|
|
||||||
if (write_in_full(fd, state->msg, state->msg_len) < 0)
|
|
||||||
die_errno(_("could not write to %s"), filename);
|
|
||||||
close(fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -336,15 +336,9 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
|
||||||
struct rev_info rev;
|
struct rev_info rev;
|
||||||
struct strbuf out = STRBUF_INIT;
|
struct strbuf out = STRBUF_INIT;
|
||||||
struct commit_list *j;
|
struct commit_list *j;
|
||||||
const char *filename;
|
|
||||||
int fd;
|
|
||||||
struct pretty_print_context ctx = {0};
|
struct pretty_print_context ctx = {0};
|
||||||
|
|
||||||
printf(_("Squash commit -- not updating HEAD\n"));
|
printf(_("Squash commit -- not updating HEAD\n"));
|
||||||
filename = git_path_squash_msg();
|
|
||||||
fd = open(filename, O_WRONLY | O_CREAT, 0666);
|
|
||||||
if (fd < 0)
|
|
||||||
die_errno(_("Could not write to '%s'"), filename);
|
|
||||||
|
|
||||||
init_revisions(&rev, NULL);
|
init_revisions(&rev, NULL);
|
||||||
rev.ignore_merges = 1;
|
rev.ignore_merges = 1;
|
||||||
|
@ -371,10 +365,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
|
||||||
oid_to_hex(&commit->object.oid));
|
oid_to_hex(&commit->object.oid));
|
||||||
pretty_print_commit(&ctx, commit, &out);
|
pretty_print_commit(&ctx, commit, &out);
|
||||||
}
|
}
|
||||||
if (write_in_full(fd, out.buf, out.len) != out.len)
|
write_file_buf(git_path_squash_msg(), out.buf, out.len);
|
||||||
die_errno(_("Writing SQUASH_MSG"));
|
|
||||||
if (close(fd))
|
|
||||||
die_errno(_("Finishing SQUASH_MSG"));
|
|
||||||
strbuf_release(&out);
|
strbuf_release(&out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -756,18 +747,6 @@ static void add_strategies(const char *string, unsigned attr)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_merge_msg(struct strbuf *msg)
|
|
||||||
{
|
|
||||||
const char *filename = git_path_merge_msg();
|
|
||||||
int fd = open(filename, O_WRONLY | O_CREAT, 0666);
|
|
||||||
if (fd < 0)
|
|
||||||
die_errno(_("Could not open '%s' for writing"),
|
|
||||||
filename);
|
|
||||||
if (write_in_full(fd, msg->buf, msg->len) != msg->len)
|
|
||||||
die_errno(_("Could not write to '%s'"), filename);
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void read_merge_msg(struct strbuf *msg)
|
static void read_merge_msg(struct strbuf *msg)
|
||||||
{
|
{
|
||||||
const char *filename = git_path_merge_msg();
|
const char *filename = git_path_merge_msg();
|
||||||
|
@ -801,7 +780,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
|
||||||
strbuf_addch(&msg, '\n');
|
strbuf_addch(&msg, '\n');
|
||||||
if (0 < option_edit)
|
if (0 < option_edit)
|
||||||
strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
|
strbuf_commented_addf(&msg, _(merge_editor_comment), comment_line_char);
|
||||||
write_merge_msg(&msg);
|
write_file_buf(git_path_merge_msg(), msg.buf, msg.len);
|
||||||
if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
|
if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
|
||||||
git_path_merge_msg(), "merge", NULL))
|
git_path_merge_msg(), "merge", NULL))
|
||||||
abort_commit(remoteheads, NULL);
|
abort_commit(remoteheads, NULL);
|
||||||
|
@ -964,8 +943,6 @@ static int setup_with_upstream(const char ***argv)
|
||||||
|
|
||||||
static void write_merge_state(struct commit_list *remoteheads)
|
static void write_merge_state(struct commit_list *remoteheads)
|
||||||
{
|
{
|
||||||
const char *filename;
|
|
||||||
int fd;
|
|
||||||
struct commit_list *j;
|
struct commit_list *j;
|
||||||
struct strbuf buf = STRBUF_INIT;
|
struct strbuf buf = STRBUF_INIT;
|
||||||
|
|
||||||
|
@ -979,26 +956,14 @@ static void write_merge_state(struct commit_list *remoteheads)
|
||||||
}
|
}
|
||||||
strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
|
strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
|
||||||
}
|
}
|
||||||
filename = git_path_merge_head();
|
write_file_buf(git_path_merge_head(), buf.buf, buf.len);
|
||||||
fd = open(filename, O_WRONLY | O_CREAT, 0666);
|
|
||||||
if (fd < 0)
|
|
||||||
die_errno(_("Could not open '%s' for writing"), filename);
|
|
||||||
if (write_in_full(fd, buf.buf, buf.len) != buf.len)
|
|
||||||
die_errno(_("Could not write to '%s'"), filename);
|
|
||||||
close(fd);
|
|
||||||
strbuf_addch(&merge_msg, '\n');
|
strbuf_addch(&merge_msg, '\n');
|
||||||
write_merge_msg(&merge_msg);
|
write_file_buf(git_path_merge_msg(), merge_msg.buf, merge_msg.len);
|
||||||
|
|
||||||
filename = git_path_merge_mode();
|
|
||||||
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
|
||||||
if (fd < 0)
|
|
||||||
die_errno(_("Could not open '%s' for writing"), filename);
|
|
||||||
strbuf_reset(&buf);
|
strbuf_reset(&buf);
|
||||||
if (fast_forward == FF_NO)
|
if (fast_forward == FF_NO)
|
||||||
strbuf_addf(&buf, "no-ff");
|
strbuf_addf(&buf, "no-ff");
|
||||||
if (write_in_full(fd, buf.buf, buf.len) != buf.len)
|
write_file_buf(git_path_merge_mode(), buf.buf, buf.len);
|
||||||
die_errno(_("Could not write to '%s'"), filename);
|
|
||||||
close(fd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int default_edit_option(void)
|
static int default_edit_option(void)
|
||||||
|
|
Loading…
Reference in a new issue