Skip to content

Commit 2b6456b

Browse files
committed
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
2 parents 96e0801 + 7eb6e10 commit 2b6456b

File tree

6 files changed

+44
-102
lines changed

6 files changed

+44
-102
lines changed

builtin/am.c

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,22 @@ static inline const char *am_path(const struct am_state *state, const char *path
184184
/**
185185
* For convenience to call write_file()
186186
*/
187-
static int write_state_text(const struct am_state *state,
188-
const char *name, const char *string)
187+
static void write_state_text(const struct am_state *state,
188+
const char *name, const char *string)
189189
{
190-
return write_file(am_path(state, name), "%s", string);
190+
write_file(am_path(state, name), "%s", string);
191191
}
192192

193-
static int write_state_count(const struct am_state *state,
194-
const char *name, int value)
193+
static void write_state_count(const struct am_state *state,
194+
const char *name, int value)
195195
{
196-
return write_file(am_path(state, name), "%d", value);
196+
write_file(am_path(state, name), "%d", value);
197197
}
198198

199-
static int write_state_bool(const struct am_state *state,
200-
const char *name, int value)
199+
static void write_state_bool(const struct am_state *state,
200+
const char *name, int value)
201201
{
202-
return write_state_text(state, name, value ? "t" : "f");
202+
write_state_text(state, name, value ? "t" : "f");
203203
}
204204

205205
/**
@@ -403,13 +403,8 @@ static int read_commit_msg(struct am_state *state)
403403
*/
404404
static void write_commit_msg(const struct am_state *state)
405405
{
406-
int fd;
407406
const char *filename = am_path(state, "final-commit");
408-
409-
fd = xopen(filename, O_WRONLY | O_CREAT, 0666);
410-
if (write_in_full(fd, state->msg, state->msg_len) < 0)
411-
die_errno(_("could not write to %s"), filename);
412-
close(fd);
407+
write_file_buf(filename, state->msg, state->msg_len);
413408
}
414409

415410
/**

builtin/branch.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -618,10 +618,7 @@ static int edit_branch_description(const char *branch_name)
618618
" %s\n"
619619
"Lines starting with '%c' will be stripped.\n"),
620620
branch_name, comment_line_char);
621-
if (write_file_gently(git_path(edit_description), "%s", buf.buf)) {
622-
strbuf_release(&buf);
623-
return error_errno(_("could not write branch description template"));
624-
}
621+
write_file_buf(git_path(edit_description), buf.buf, buf.len);
625622
strbuf_reset(&buf);
626623
if (launch_editor(git_path(edit_description), &buf, NULL)) {
627624
strbuf_release(&buf);

builtin/config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
604604
given_config_source.file : git_path("config"));
605605
if (use_global_config) {
606606
int fd = open(config_file, O_CREAT | O_EXCL | O_WRONLY, 0666);
607-
if (fd) {
607+
if (fd >= 0) {
608608
char *content = default_user_config();
609609
write_str_in_full(fd, content);
610610
free(content);

builtin/merge.c

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,9 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
336336
struct rev_info rev;
337337
struct strbuf out = STRBUF_INIT;
338338
struct commit_list *j;
339-
const char *filename;
340-
int fd;
341339
struct pretty_print_context ctx = {0};
342340

343341
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);
348342

349343
init_revisions(&rev, NULL);
350344
rev.ignore_merges = 1;
@@ -371,10 +365,7 @@ static void squash_message(struct commit *commit, struct commit_list *remotehead
371365
oid_to_hex(&commit->object.oid));
372366
pretty_print_commit(&ctx, commit, &out);
373367
}
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);
378369
strbuf_release(&out);
379370
}
380371

@@ -756,18 +747,6 @@ static void add_strategies(const char *string, unsigned attr)
756747

757748
}
758749

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-
771750
static void read_merge_msg(struct strbuf *msg)
772751
{
773752
const char *filename = git_path_merge_msg();
@@ -801,7 +780,7 @@ static void prepare_to_commit(struct commit_list *remoteheads)
801780
strbuf_addch(&msg, '\n');
802781
if (0 < option_edit)
803782
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);
805784
if (run_commit_hook(0 < option_edit, get_index_file(), "prepare-commit-msg",
806785
git_path_merge_msg(), "merge", NULL))
807786
abort_commit(remoteheads, NULL);
@@ -964,8 +943,6 @@ static int setup_with_upstream(const char ***argv)
964943

965944
static void write_merge_state(struct commit_list *remoteheads)
966945
{
967-
const char *filename;
968-
int fd;
969946
struct commit_list *j;
970947
struct strbuf buf = STRBUF_INIT;
971948

@@ -979,26 +956,14 @@ static void write_merge_state(struct commit_list *remoteheads)
979956
}
980957
strbuf_addf(&buf, "%s\n", oid_to_hex(oid));
981958
}
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);
989960
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);
991962

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);
996963
strbuf_reset(&buf);
997964
if (fast_forward == FF_NO)
998965
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);
1002967
}
1003968

1004969
static int default_edit_option(void)

cache.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,8 +1746,21 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
17461746
return write_in_full(fd, str, strlen(str));
17471747
}
17481748

1749-
extern int write_file(const char *path, const char *fmt, ...);
1750-
extern int write_file_gently(const char *path, const char *fmt, ...);
1749+
/**
1750+
* Open (and truncate) the file at path, write the contents of buf to it,
1751+
* and close it. Dies if any errors are encountered.
1752+
*/
1753+
extern void write_file_buf(const char *path, const char *buf, size_t len);
1754+
1755+
/**
1756+
* Like write_file_buf(), but format the contents into a buffer first.
1757+
* Additionally, write_file() will append a newline if one is not already
1758+
* present, making it convenient to write text files:
1759+
*
1760+
* write_file(path, "counter: %d", ctr);
1761+
*/
1762+
__attribute__((format (printf, 2, 3)))
1763+
extern void write_file(const char *path, const char *fmt, ...);
17511764

17521765
/* pager.c */
17531766
extern void setup_pager(void);

wrapper.c

Lines changed: 12 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -651,56 +651,28 @@ int xsnprintf(char *dst, size_t max, const char *fmt, ...)
651651
return len;
652652
}
653653

654-
static int write_file_v(const char *path, int fatal,
655-
const char *fmt, va_list params)
654+
void write_file_buf(const char *path, const char *buf, size_t len)
656655
{
657-
struct strbuf sb = STRBUF_INIT;
658-
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
659-
if (fd < 0) {
660-
if (fatal)
661-
die_errno(_("could not open %s for writing"), path);
662-
return -1;
663-
}
664-
strbuf_vaddf(&sb, fmt, params);
665-
strbuf_complete_line(&sb);
666-
if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
667-
int err = errno;
668-
close(fd);
669-
strbuf_release(&sb);
670-
errno = err;
671-
if (fatal)
672-
die_errno(_("could not write to %s"), path);
673-
return -1;
674-
}
675-
strbuf_release(&sb);
676-
if (close(fd)) {
677-
if (fatal)
678-
die_errno(_("could not close %s"), path);
679-
return -1;
680-
}
681-
return 0;
656+
int fd = xopen(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);
657+
if (write_in_full(fd, buf, len) != len)
658+
die_errno(_("could not write to %s"), path);
659+
if (close(fd))
660+
die_errno(_("could not close %s"), path);
682661
}
683662

684-
int write_file(const char *path, const char *fmt, ...)
663+
void write_file(const char *path, const char *fmt, ...)
685664
{
686-
int status;
687665
va_list params;
666+
struct strbuf sb = STRBUF_INIT;
688667

689668
va_start(params, fmt);
690-
status = write_file_v(path, 1, fmt, params);
669+
strbuf_vaddf(&sb, fmt, params);
691670
va_end(params);
692-
return status;
693-
}
694671

695-
int write_file_gently(const char *path, const char *fmt, ...)
696-
{
697-
int status;
698-
va_list params;
672+
strbuf_complete_line(&sb);
699673

700-
va_start(params, fmt);
701-
status = write_file_v(path, 0, fmt, params);
702-
va_end(params);
703-
return status;
674+
write_file_buf(path, sb.buf, sb.len);
675+
strbuf_release(&sb);
704676
}
705677

706678
void sleep_millisec(int millisec)

0 commit comments

Comments
 (0)