{"thread":{"id":"20206","subject":"[PATCH] Trailing whitespace and no newline fix","startedAt":"2009-07-23T00:24:38Z","lastAt":"2009-07-25T08:52:35Z","messageCount":4,"participants":["SZEDER Gábor","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"118525","messageId":"1248308678-23280-1-git-send-email-szeder@ira.uka.de","threadId":"20206","inReplyTo":null,"subject":"[PATCH] Trailing whitespace and no newline fix","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2009-07-23T00:24:38Z","receivedAt":"2009-07-23T00:24:38Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"If a patch adds a new line to the end of a file and this line ends with\none trailing whitespace character and has no newline, then\n'--whitespace=fix' currently does not remove that trailing whitespace.\n\nThis patch fixes this by removing the check for trailing whitespace at\nthe end of the line at a hardcoded offset which does not take the\neventual absence of newline into account.\n\nSigned-off-by: SZEDER Gábor <szeder@ira.uka.de>\n---\n t/t4124-apply-ws-rule.sh |   18 ++++++++++++++++++\n ws.c                     |    5 ++---\n 2 files changed, 20 insertions(+), 3 deletions(-)\n\ndiff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh\nindex f83322e..5698a9a 100755\n--- a/t/t4124-apply-ws-rule.sh\n+++ b/t/t4124-apply-ws-rule.sh\n@@ -148,4 +148,22 @@ do\n \tdone\n done\n \n+create_patch () {\n+\tsed -e \"s/_/ /\" <<-\\EOF\n+\t\tdiff --git a/target b/target\n+\t\tindex e69de29..8bd6648 100644\n+\t\t--- a/target\n+\t\t+++ b/target\n+\t\t@@ -0,0 +1 @@\n+\t\t+A line with trailing whitespace and no newline_\n+\t\t\\ No newline at end of file\n+\tEOF\n+}\n+\n+test_expect_success 'trailing whitespace & no newline at the end of file' '\n+\t>target &&\n+\tcreate_patch | git apply --whitespace=fix - &&\n+\tgrep \"newline$\" target\n+'\n+\n test_done\ndiff --git a/ws.c b/ws.c\nindex b1efcd9..9c20acd 100644\n--- a/ws.c\n+++ b/ws.c\n@@ -259,9 +259,8 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro\n \t/*\n \t * Strip trailing whitespace\n \t */\n-\tif ((ws_rule & WS_TRAILING_SPACE) &&\n-\t    (2 <= len && isspace(src[len-2]))) {\n-\t\tif (src[len - 1] == '\\n') {\n+\tif (ws_rule & WS_TRAILING_SPACE) {\n+\t\tif (1 < len && src[len - 1] == '\\n') {\n \t\t\tadd_nl_to_tail = 1;\n \t\t\tlen--;\n \t\t\tif (1 < len && src[len - 1] == '\\r') {\n-- \n1.6.4.rc1.73.ga0daf\n"},{"id":"118529","messageId":"7v8wig88jz.fsf@alter.siamese.dyndns.org","threadId":"20206","inReplyTo":"1248308678-23280-1-git-send-email-szeder@ira.uka.de","subject":"Re: [PATCH] Trailing whitespace and no newline fix","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-07-23T01:58:08Z","receivedAt":"2009-07-23T01:58:08Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder@ira.uka.de> writes:\n\n> If a patch adds a new line to the end of a file and this line ends with\n> one trailing whitespace character and has no newline, then\n> '--whitespace=fix' currently does not remove that trailing whitespace.\n\nLooks obvious and correct.  Thanks.\n"},{"id":"118705","messageId":"7vy6qdupio.fsf@alter.siamese.dyndns.org","threadId":"20206","inReplyTo":"1248308678-23280-1-git-send-email-szeder@ira.uka.de","subject":"Re: [PATCH] Trailing whitespace and no newline fix","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-07-25T08:37:35Z","receivedAt":"2009-07-25T08:37:35Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"SZEDER Gábor <szeder@ira.uka.de> writes:\n\n> If a patch adds a new line to the end of a file and this line ends with\n> one trailing whitespace character and has no newline, then\n> '--whitespace=fix' currently does not remove that trailing whitespace.\n>\n> This patch fixes this by removing the check for trailing whitespace at\n> the end of the line at a hardcoded offset which does not take the\n> eventual absence of newline into account.\n\nThis patch does a lot more than what it claims it does.\n\nTry to apply Jakub's 10 patch series with this, with --whitespace=fix, and\nnotice that the 9th patch gets all the blank lines removed, claiming that\n114 lines had whitespace errors, even though there is only one trailing\nwhitespace. \n\nHere is a fix.  I am pissed at myself not spotting this earlier.  The\nworst part is that it is part of 'maint' and all integration branches\nneeds fixing.\n\n-- >8 --\nFrom: Junio C Hamano <gitster@pobox.com>\nDate: Sat, 25 Jul 2009 01:29:20 -0700\nSubject: [PATCH] Fix severe breakage in \"git-apply --whitespace=fix\"\n\n735c674 (Trailing whitespace and no newline fix, 2009-07-22) completely\nbroke --whitespace=fix, causing it to lose all the empty lines in a patch.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n t/t4124-apply-ws-rule.sh |   10 +++++++---\n ws.c                     |    4 ++--\n 2 files changed, 9 insertions(+), 5 deletions(-)\n\ndiff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh\nindex 5698a9a..fac2093 100755\n--- a/t/t4124-apply-ws-rule.sh\n+++ b/t/t4124-apply-ws-rule.sh\n@@ -154,7 +154,9 @@ create_patch () {\n \t\tindex e69de29..8bd6648 100644\n \t\t--- a/target\n \t\t+++ b/target\n-\t\t@@ -0,0 +1 @@\n+\t\t@@ -0,0 +1,3 @@\n+\t\t+An empty line follows\n+\t\t+\n \t\t+A line with trailing whitespace and no newline_\n \t\t\\ No newline at end of file\n \tEOF\n@@ -162,8 +164,10 @@ create_patch () {\n \n test_expect_success 'trailing whitespace & no newline at the end of file' '\n \t>target &&\n-\tcreate_patch | git apply --whitespace=fix - &&\n-\tgrep \"newline$\" target\n+\tcreate_patch >patch-file &&\n+\tgit apply --whitespace=fix patch-file &&\n+\tgrep \"newline$\" target &&\n+\tgrep \"^$\" target\n '\n \n test_done\ndiff --git a/ws.c b/ws.c\nindex 8d855b7..59d0883 100644\n--- a/ws.c\n+++ b/ws.c\n@@ -262,10 +262,10 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro\n \t * Strip trailing whitespace\n \t */\n \tif (ws_rule & WS_TRAILING_SPACE) {\n-\t\tif (1 < len && src[len - 1] == '\\n') {\n+\t\tif (0 < len && src[len - 1] == '\\n') {\n \t\t\tadd_nl_to_tail = 1;\n \t\t\tlen--;\n-\t\t\tif (1 < len && src[len - 1] == '\\r') {\n+\t\t\tif (0 < len && src[len - 1] == '\\r') {\n \t\t\t\tadd_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);\n \t\t\t\tlen--;\n \t\t\t}\n-- \n1.6.4.rc2.19.gfa5c0\n"},{"id":"118706","messageId":"7vr5w5uoto.fsf_-_@alter.siamese.dyndns.org","threadId":"20206","inReplyTo":"7vy6qdupio.fsf@alter.siamese.dyndns.org","subject":"warning: \"git am --whitespace=fix\" broken in 1.6.4-rc2","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2009-07-25T08:52:35Z","receivedAt":"2009-07-25T08:52:35Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"I am very sorry that I did not spot this earlier, but 1.6.4-rc2 has a\nquite serious bug in \"git apply --whitespace=fix\" (hence \"git am\" with the\nsame option) in that it can lose all the added empty lines when it detects\nany trailing spaces.  I've sent a fix out already, and am planning to\nissue 1.6.4-rc3 this weekend, but in the meantime, please do not use it,\nnor any version that contains commit 735c674 (Trailing whitespace and no\nnewline fix, 2009-07-22).\n\nUnfortunately the commit is included in all recent integration branches\ndown to 'maint'.  I'll push out the fixes for them soonish.\n\nSorry for the trouble.\n"}]}