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

[PATCH] Trailing whitespace and no newline fix

From
SZEDER Gábor <szeder@ira.uka.de>
Date
Jul 23, 2009, 00:24 UTC
Message-ID
<1248308678-23280-1-git-send-email-szeder@ira.uka.de>

If a patch adds a new line to the end of a file and this line ends with one trailing whitespace character and has no newline, then '--whitespace=fix' currently does not remove that trailing whitespace.

This patch fixes this by removing the check for trailing whitespace at the end of the line at a hardcoded offset which does not take the eventual absence of newline into account.

Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
 t/t4124-apply-ws-rule.sh |   18 ++++++++++++++++++
 ws.c                     |    5 ++---
 2 files changed, 20 insertions(+), 3 deletions(-)
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
index f83322e..5698a9a 100755
--- a/t/t4124-apply-ws-rule.sh
+++ b/t/t4124-apply-ws-rule.sh
@@ -148,4 +148,22 @@ do
 	done
 done
 
+create_patch () {
+	sed -e "s/_/ /" <<-\EOF
+		diff --git a/target b/target
+		index e69de29..8bd6648 100644
+		--- a/target
+		+++ b/target
+		@@ -0,0 +1 @@
+		+A line with trailing whitespace and no newline_
+		\ No newline at end of file
+	EOF
+}
+
+test_expect_success 'trailing whitespace & no newline at the end of file' '
+	>target &&
+	create_patch | git apply --whitespace=fix - &&
+	grep "newline$" target
+'
+
 test_done
diff --git a/ws.c b/ws.c
index b1efcd9..9c20acd 100644
--- a/ws.c
+++ b/ws.c
@@ -259,9 +259,8 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro
 	/*
 	 * Strip trailing whitespace
 	 */
-	if ((ws_rule & WS_TRAILING_SPACE) &&
-	    (2 <= len && isspace(src[len-2]))) {
-		if (src[len - 1] == '\n') {
+	if (ws_rule & WS_TRAILING_SPACE) {
+		if (1 < len && src[len - 1] == '\n') {
 			add_nl_to_tail = 1;
 			len--;
 			if (1 < len && src[len - 1] == '\r') {
-- 
1.6.4.rc1.73.ga0daf
Next: Junio C Hamano
Message 1 of 4 in “Trailing whitespace and no newline fix”
  1. Trailing whitespace and no newline fixSZEDER Gábor, Jul 23, 2009
  2. Junio C HamanoJul 23, 2009
  3. Junio C HamanoJul 25, 2009
  4. warning: "git am --whitespace=fix" broken in 1.6.4-rc2Junio C Hamano, Jul 25, 2009

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.