write_file_v(): do not leave incomplete line at the end
authorJunio C Hamano <gitster@pobox.com>
Mon, 24 Aug 2015 16:39:48 +0000 (09:39 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 25 Aug 2015 19:48:39 +0000 (12:48 -0700)
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>
builtin/am.c
wrapper.c
index 9c576779c36caa41edf20d7dd94a51893b00206c..486ff594d7e87de4119e48d9771ec5b356ad14c6 100644 (file)
@@ -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,
index 8c8925b72a804395bfdec30108beb5dc9150ee9d..0e22d4381438b2c262e1cd5ee0cc4effc2fb3c3d 100644 (file)
--- a/wrapper.c
+++ b/wrapper.c
@@ -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);