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.

Introduce WRITE_FILE_BINARY flag bit, which no existing callers
pass, and unless that bit is set, make sure that write_file_v() adds
an extra LF at the end of an incomplete line as necessary.

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

Signed-off-by: Junio C Hamano <gits...@pobox.com>
---
 builtin/am.c | 10 ++--------
 wrapper.c    | 14 +++++++++++---
 2 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index 9c57677..486ff59 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -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,
diff --git a/wrapper.c b/wrapper.c
index 8c8925b..db39e1b 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -621,17 +621,25 @@ char *xgetcwd(void)
        return strbuf_detach(&sb, NULL);
 }
 
-static int write_file_v(const char *path, int fatal,
+
+#define WRITE_FILE_GENTLY (1 << 0)
+#define WRITE_FILE_BINARY (1 << 1)
+
+static int write_file_v(const char *path, unsigned flags,
                        const char *fmt, va_list params)
 {
+       int fatal = !(flags & WRITE_FILE_GENTLY);
        struct strbuf sb = STRBUF_INIT;
        int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0666);
+
        if (fd < 0) {
                if (fatal)
                        die_errno(_("could not open %s for writing"), path);
                return -1;
        }
        strbuf_vaddf(&sb, fmt, params);
+       if (!(flags & WRITE_FILE_BINARY))
+               strbuf_complete_line(&sb);
        if (write_in_full(fd, sb.buf, sb.len) != sb.len) {
                int err = errno;
                close(fd);
@@ -656,7 +664,7 @@ int write_file(const char *path, const char *fmt, ...)
        va_list params;
 
        va_start(params, fmt);
-       status = write_file_v(path, 1, fmt, params);
+       status = write_file_v(path, 0, fmt, params);
        va_end(params);
        return status;
 }
@@ -667,7 +675,7 @@ int write_file_gently(const char *path, const char *fmt, 
...)
        va_list params;
 
        va_start(params, fmt);
-       status = write_file_v(path, 0, fmt, params);
+       status = write_file_v(path, WRITE_FILE_GENTLY, fmt, params);
        va_end(params);
        return status;
 }
-- 
2.5.0-568-g53a3e28

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to