Skip to content

Commit

Permalink
write_file_v(): do not leave incomplete line at the end
Browse files Browse the repository at this point in the history
All existing callers to this function use it to produce a text file
or an empty file, and a new callsite that mimick them must end their
payload with a LF.  If they forget to do so, the resulting file will
end with an incomplete line.

Teach write_file_v() to complete the incomplete line, if exists, so
that the callers do not have to.

With this, the caller-side fix in builtin/am.c becomes unnecessary.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
gitster committed Aug 25, 2015
1 parent 12d6ce1 commit e7ffa38
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 8 deletions.
10 changes: 2 additions & 8 deletions builtin/am.c
Expand Up @@ -199,19 +199,13 @@ static inline const char *am_path(const struct am_state *state, const char *path
static int write_state_text(const struct am_state *state,
const char *name, const char *string)
{
const char *fmt;

if (*string && string[strlen(string) - 1] != '\n')
fmt = "%s\n";
else
fmt = "%s";
return write_file(am_path(state, name), fmt, string);
return write_file(am_path(state, name), "%s", string);
}

static int write_state_count(const struct am_state *state,
const char *name, int value)
{
return write_file(am_path(state, name), "%d\n", value);
return write_file(am_path(state, name), "%d", value);
}

static int write_state_bool(const struct am_state *state,
Expand Down
1 change: 1 addition & 0 deletions wrapper.c
Expand Up @@ -632,6 +632,7 @@ static int write_file_v(const char *path, int fatal,
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);
Expand Down

0 comments on commit e7ffa38

Please sign in to comment.