Subject: [PATCH 4/5] write_file(): do not leave incomplete line at the end

From: Junio C Hamano <[email-masked]>
Date: Mon, 24 Aug 2015 10:09:45 -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.

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

Signed-off-by: Junio C Hamano <[email-masked]>
---
 cache.h   | 1 +
 wrapper.c | 3 +++
 2 files changed, 4 insertions(+)

diff --git a/cache.h b/cache.h
index f105235..dbfa4fa 100644
--- a/cache.h
+++ b/cache.h
@@ -1552,6 +1552,7 @@ static inline ssize_t write_str_in_full(int fd, const char *str)
  */
 #define WRITE_FILE_UNUSED_0 (1<<0)
 #define WRITE_FILE_GENTLY (1<<1)
+#define WRITE_FILE_BINARY (1<<2)
 __attribute__((format (printf, 3, 4)))
 extern int write_file(const char *path, unsigned flags, const char *fmt, ...);
 
diff --git a/wrapper.c b/wrapper.c
index 68d45b6..4cd2ca3 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -635,6 +635,9 @@ int write_file(const char *path, unsigned flags, const char *fmt, ...)
 	va_start(params, fmt);
 	strbuf_vaddf(&sb, fmt, params);
 	va_end(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);
-- 
2.5.0-568-g53a3e28