Skip to content

Commit e7ffa38

Browse files
committed
write_file_v(): do not leave incomplete line at the end
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>
1 parent 12d6ce1 commit e7ffa38

File tree

2 files changed

+3
-8
lines changed

2 files changed

+3
-8
lines changed

builtin/am.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,13 @@ static inline const char *am_path(const struct am_state *state, const char *path
199199
static int write_state_text(const struct am_state *state,
200200
const char *name, const char *string)
201201
{
202-
const char *fmt;
203-
204-
if (*string && string[strlen(string) - 1] != '\n')
205-
fmt = "%s\n";
206-
else
207-
fmt = "%s";
208-
return write_file(am_path(state, name), fmt, string);
202+
return write_file(am_path(state, name), "%s", string);
209203
}
210204

211205
static int write_state_count(const struct am_state *state,
212206
const char *name, int value)
213207
{
214-
return write_file(am_path(state, name), "%d\n", value);
208+
return write_file(am_path(state, name), "%d", value);
215209
}
216210

217211
static int write_state_bool(const struct am_state *state,

wrapper.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ static int write_file_v(const char *path, int fatal,
632632
return -1;
633633
}
634634
strbuf_vaddf(&sb, fmt, params);
635+
strbuf_complete_line(&sb);
635636
if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
636637
int err = errno;
637638
close(fd);

0 commit comments

Comments
 (0)