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>

Junio C Hamano committed Aug 24, 2015 at 09:39 UTC e7ffa38c6e726e8014b76297b06f78e008deb2d0
2 files changed +3 -8
builtin/am.c
+2 -8
@@ -199,19 +199,13 @@ static inline const char *am_path(const struct am_state *state, const char *path
199 static int write_state_text(const struct am_state *state,
200 const char *name, const char *string)
201 {
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);
203 }
204
205 static int write_state_count(const struct am_state *state,
206 const char *name, int value)
207 {
214 - return write_file(am_path(state, name), "%d\n", value);
208 + return write_file(am_path(state, name), "%d", value);
209 }
210
211 static int write_state_bool(const struct am_state *state,
wrapper.c
+1
@@ -632,6 +632,7 @@ static int write_file_v(const char *path, int fatal,
632 return -1;
633 }
634 strbuf_vaddf(&sb, fmt, params);
635 + strbuf_complete_line(&sb);
636 if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
637 int err = errno;
638 close(fd);