Merge branch 'jk/write-file'
General code clean-up around a helper function to write a single-liner to a file. * jk/write-file: branch: use write_file_buf instead of write_file use write_file_buf where applicable write_file: add format attribute write_file: add pointer+len variant write_file: use xopen write_file: drop "gently" form branch: use non-gentle write_file for branch description am: ignore return value of write_file() config: fix bogus fd check when setting up default config
This commit is contained in:
19
builtin/am.c
19
builtin/am.c
@ -184,22 +184,22 @@ static inline const char *am_path(const struct am_state *state, const char *path
|
|||||||
/**
|
/**
|
||||||
* For convenience to call write_file()
|
* For convenience to call write_file()
|
||||||
*/
|
*/
|
||||||
static int write_state_text(const struct am_state *state,
|
static void write_state_text(const struct am_state *state,
|
||||||
const char *name, const char *string)
|
const char *name, const char *string)
|
||||||
{
|
{
|
||||||
return write_file(am_path(state, name), "%s", string);
|
write_file(am_path(state, name), "%s", string);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_state_count(const struct am_state *state,
|
static void write_state_count(const struct am_state *state,
|
||||||
const char *name, int value)
|
const char *name, int value)
|
||||||
{
|
{
|
||||||
return write_file(am_path(state, name), "%d", value);
|
write_file(am_path(state, name), "%d", value);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_state_bool(const struct am_state *state,
|
static void write_state_bool(const struct am_state *state,
|
||||||
const char *name, int value)
|
const char *name, int value)
|
||||||
{
|
{
|
||||||
return write_state_text(state, name, value ? "t" : "f");
|
write_state_text(state, name, value ? "t" : "f");
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -403,13 +403,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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -618,10 +618,7 @@ static int edit_branch_description(const char *branch_name)
|
|||||||
" %s\n"
|
" %s\n"
|
||||||
"Lines starting with '%c' will be stripped.\n"),
|
"Lines starting with '%c' will be stripped.\n"),
|
||||||
branch_name, comment_line_char);
|
branch_name, comment_line_char);
|
||||||
if (write_file_gently(git_path(edit_description), "%s", buf.buf)) {
|
write_file_buf(git_path(edit_description), buf.buf, buf.len);
|
||||||
strbuf_release(&buf);
|
|
||||||
return error_errno(_("could not write branch description template"));
|
|
||||||
}
|
|
||||||
strbuf_reset(&buf);
|
strbuf_reset(&buf);
|
||||||
if (launch_editor(git_path(edit_description), &buf, NULL)) {
|
if (launch_editor(git_path(edit_description), &buf, NULL)) {
|
||||||
strbuf_release(&buf);
|
strbuf_release(&buf);
|
||||||
|
@ -604,7 +604,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
|
|||||||
given_config_source.file : git_path("config"));
|
given_config_source.file : git_path("config"));
|
||||||
if (use_global_config) {
|
if (use_global_config) {
|
||||||
int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
|
int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
|
||||||
if (fd) {
|
if (fd >= 0) {
|
||||||
char *content = default_user_config();
|
char *content = default_user_config();
|
||||||
write_str_in_full(fd, content);
|
write_str_in_full(fd, content);
|
||||||
free(content);
|
free(content);
|
||||||
|
@ -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)
|
||||||
|
17
cache.h
17
cache.h
@ -1746,8 +1746,21 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
|
|||||||
return write_in_full(fd, str, strlen(str));
|
return write_in_full(fd, str, strlen(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
extern int write_file(const char *path, const char *fmt, ...);
|
/**
|
||||||
extern int write_file_gently(const char *path, const char *fmt, ...);
|
* Open (and truncate) the file at path, write the contents of buf to it,
|
||||||
|
* and close it. Dies if any errors are encountered.
|
||||||
|
*/
|
||||||
|
extern void write_file_buf(const char *path, const char *buf, size_t len);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Like write_file_buf(), but format the contents into a buffer first.
|
||||||
|
* Additionally, write_file() will append a newline if one is not already
|
||||||
|
* present, making it convenient to write text files:
|
||||||
|
*
|
||||||
|
* write_file(path, "counter: %d", ctr);
|
||||||
|
*/
|
||||||
|
__attribute__((format (printf, 2, 3)))
|
||||||
|
extern void write_file(const char *path, const char *fmt, ...);
|
||||||
|
|
||||||
/* pager.c */
|
/* pager.c */
|
||||||
extern void setup_pager(void);
|
extern void setup_pager(void);
|
||||||
|
48
wrapper.c
48
wrapper.c
@ -651,56 +651,28 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...)
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int write_file_v(const char *path, int fatal,
|
void write_file_buf(const char *path, const char *buf, size_t len)
|
||||||
const char *fmt, va_list params)
|
|
||||||
{
|
{
|
||||||
struct strbuf sb = STRBUF_INIT;
|
int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||||
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
|
if (write_in_full(fd, buf, len) != len)
|
||||||
if (fd < 0) {
|
|
||||||
if (fatal)
|
|
||||||
die_errno(_("could not open %s for writing"), path);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
strbuf_vaddf(&sb, fmt, params);
|
|
||||||
strbuf_complete_line(&sb);
|
|
||||||
if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
|
|
||||||
int err = errno;
|
|
||||||
close(fd);
|
|
||||||
strbuf_release(&sb);
|
|
||||||
errno = err;
|
|
||||||
if (fatal)
|
|
||||||
die_errno(_("could not write to %s"), path);
|
die_errno(_("could not write to %s"), path);
|
||||||
return -1;
|
if (close(fd))
|
||||||
}
|
|
||||||
strbuf_release(&sb);
|
|
||||||
if (close(fd)) {
|
|
||||||
if (fatal)
|
|
||||||
die_errno(_("could not close %s"), path);
|
die_errno(_("could not close %s"), path);
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int write_file(const char *path, const char *fmt, ...)
|
void write_file(const char *path, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
int status;
|
|
||||||
va_list params;
|
va_list params;
|
||||||
|
struct strbuf sb = STRBUF_INIT;
|
||||||
|
|
||||||
va_start(params, fmt);
|
va_start(params, fmt);
|
||||||
status = write_file_v(path, 1, fmt, params);
|
strbuf_vaddf(&sb, fmt, params);
|
||||||
va_end(params);
|
va_end(params);
|
||||||
return status;
|
|
||||||
}
|
|
||||||
|
|
||||||
int write_file_gently(const char *path, const char *fmt, ...)
|
strbuf_complete_line(&sb);
|
||||||
{
|
|
||||||
int status;
|
|
||||||
va_list params;
|
|
||||||
|
|
||||||
va_start(params, fmt);
|
write_file_buf(path, sb.buf, sb.len);
|
||||||
status = write_file_v(path, 0, fmt, params);
|
strbuf_release(&sb);
|
||||||
va_end(params);
|
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sleep_millisec(int millisec)
|
void sleep_millisec(int millisec)
|
||||||
|
Reference in New Issue
Block a user