git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/6] write_file_v(): do not leave incomplete line at the end

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 24, 2015, 20:58 UTC
Message-ID
<1440449890-29490-5-git-send-email-gitster@pobox.com>
In-Reply-To
<1440449890-29490-1-git-send-email-gitster@pobox.com>

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 <gitster@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
Previous: Junio C HamanoNext: Junio C Hamano
Message 32 of 36 in “Minor builtin 'git am' side-effect”
  1. SZEDER GáborAug 20, 2015
  2. Junio C HamanoAug 20, 2015
  3. am: terminate state files with a newlinePaul Tan, Aug 23, 2015
  4. SZEDER GáborAug 23, 2015
  5. Junio C HamanoAug 23, 2015
  6. Jeff KingAug 24, 2015
  7. Junio C HamanoAug 24, 2015
  8. Jeff KingAug 24, 2015
  9. 0/5 "am" state file fix with write_file() clean-upJunio C Hamano, Aug 24, 2015
  10. 1/5 builtin/am: introduce write_state_*() helper functionsJunio C Hamano, Aug 24, 2015
  11. 2/5 builtin/am: make sure state files are textJunio C Hamano, Aug 24, 2015
  12. 3/5 write_file(): introduce an explicit WRITE_FILE_GENTLY requestJunio C Hamano, Aug 24, 2015
  13. Junio C HamanoAug 24, 2015
  14. Duy NguyenAug 25, 2015
  15. setup: update the right file in multiple checkoutsNguyễn Thái Ngọc Duy, Aug 25, 2015
  16. Junio C HamanoAug 25, 2015
  17. Duy NguyenAug 31, 2015
  18. 4/5 write_file(): do not leave incomplete line at the endJunio C Hamano, Aug 24, 2015
  19. 5/5 write_file(): clean up transitional mess of flag words and terminating LFJunio C Hamano, Aug 24, 2015
  20. Jeff KingAug 24, 2015
  21. Junio C HamanoAug 24, 2015
  22. Jeff KingAug 24, 2015
  23. Junio C HamanoAug 24, 2015
  24. 0/6 "am" state file fix with write_file() clean-upJunio C Hamano, Aug 24, 2015
  25. 1/6 builtin/am: introduce write_state_*() helper functionsJunio C Hamano, Aug 24, 2015
  26. 2/6 builtin/am: make sure state files are textJunio C Hamano, Aug 24, 2015
  27. Jeff KingAug 24, 2015
  28. Junio C HamanoAug 25, 2015
  29. Jeff KingAug 25, 2015
  30. Junio C HamanoAug 25, 2015
  31. 3/6 write_file(): drop "fatal" parameterJunio C Hamano, Aug 24, 2015
  32. 4/6 write_file_v(): do not leave incomplete line at the endJunio C Hamano, Aug 24, 2015
  33. 5/6 write_file(): drop caller-supplied LF from calls to create a one-liner fileJunio C Hamano, Aug 24, 2015
  34. 6/6 write_file(): drop caller-supplied LF from multi-line fileJunio C Hamano, Aug 24, 2015
  35. Jeff KingAug 25, 2015
  36. brian m. carlsonAug 24, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.