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

[PATCH 1/3] ws.c: add a helper to format comma separated messages

From
Junio C Hamano <gitster@pobox.com>
Date
May 27, 2011, 22:48 UTC
Message-ID
<7vr57jrc8c.fsf_-_@alter.siamese.dyndns.org>
In-Reply-To
<7vy61rrcae.fsf@alter.siamese.dyndns.org>

We can find more than one class of whitespace errors on a single line, and we concatenate a message per class in a strbuf, separated with a colon. Use a small helper function to make the code easier to read.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 * Just a trivial clean-up before waging a "War on nbsp"
 ws.c |   43 +++++++++++++++++++------------------------
 1 files changed, 19 insertions(+), 24 deletions(-)
diff --git a/ws.c b/ws.c
index 9fb9b14..3058be4 100644
--- a/ws.c
+++ b/ws.c
@@ -116,36 +116,31 @@ unsigned whitespace_rule(const char *pathname)
 	}
 }
 
+static void add_err_item(struct strbuf *err, const char *message)
+{
+	if (err->len)
+		strbuf_addstr(err, ", ");
+	strbuf_addstr(err, message);
+}
+
 /* The returned string should be freed by the caller. */
 char *whitespace_error_string(unsigned ws)
 {
 	struct strbuf err = STRBUF_INIT;
-	if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
-		strbuf_addstr(&err, "trailing whitespace");
-	else {
+	if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE) {
+		add_err_item(&err, "trailing whitespace");
+	} else {
 		if (ws & WS_BLANK_AT_EOL)
-			strbuf_addstr(&err, "trailing whitespace");
-		if (ws & WS_BLANK_AT_EOF) {
-			if (err.len)
-				strbuf_addstr(&err, ", ");
-			strbuf_addstr(&err, "new blank line at EOF");
-		}
-	}
-	if (ws & WS_SPACE_BEFORE_TAB) {
-		if (err.len)
-			strbuf_addstr(&err, ", ");
-		strbuf_addstr(&err, "space before tab in indent");
-	}
-	if (ws & WS_INDENT_WITH_NON_TAB) {
-		if (err.len)
-			strbuf_addstr(&err, ", ");
-		strbuf_addstr(&err, "indent with spaces");
-	}
-	if (ws & WS_TAB_IN_INDENT) {
-		if (err.len)
-			strbuf_addstr(&err, ", ");
-		strbuf_addstr(&err, "tab in indent");
+			add_err_item(&err, "trailing whitespace");
+		if (ws & WS_BLANK_AT_EOF)
+			add_err_item(&err, "new blank line at EOF");
 	}
+	if (ws & WS_SPACE_BEFORE_TAB)
+		add_err_item(&err, "space before tab in indent");
+	if (ws & WS_INDENT_WITH_NON_TAB)
+		add_err_item(&err, "indent with spaces");
+	if (ws & WS_TAB_IN_INDENT)
+		add_err_item(&err, "tab in indent");
 	return strbuf_detach(&err, NULL);
 }
 
-- 
1.7.5.3.503.g893a4
Previous: Junio C HamanoNext: Junio C Hamano
Message 10 of 15 in “Whitespace and '&nbsp'”
  1. Linus TorvaldsMay 26, 2011
  2. Junio C HamanoMay 26, 2011
  3. Linus TorvaldsMay 26, 2011
  4. Andreas SchwabMay 27, 2011
  5. Linus TorvaldsMay 27, 2011
  6. Junio C HamanoMay 27, 2011
  7. Junio C HamanoMay 27, 2011
  8. Linus TorvaldsMay 27, 2011
  9. Junio C HamanoMay 27, 2011
  10. 1/3 ws.c: add a helper to format comma separated messagesJunio C Hamano, May 27, 2011
  11. 2/3 War on nbsp: a bit of retreatJunio C Hamano, May 27, 2011
  12. 3/3 War on nbsp: Add "nbsp" whitespace breakage classJunio C Hamano, May 27, 2011
  13. 4/3 War on nbsp: teach "git apply" to check and fix nbspJunio C Hamano, May 28, 2011
  14. Daniel NyströmMay 30, 2011
  15. Matthieu MoyMay 26, 2011

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.