@matheusbsbs / matheus-bagliano / commits / 735c674416
Fork 0

Trailing whitespace and no newline fix

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Jul 22, 2009 at 19:24 UTC 735c674416b87505400fcf738fd3a38b52f0eccb
2 files changed +20 -3
t/t4124-apply-ws-rule.sh
+18
@@ -148,4 +148,22 @@ do
148 done
149 done
150
151 +create_patch () {
152 + sed -e "s/_/ /" <<-\EOF
153 + diff --git a/target b/target
154 + index e69de29..8bd6648 100644
155 + --- a/target
156 + +++ b/target
157 + @@ -0,0 +1 @@
158 + +A line with trailing whitespace and no newline_
159 + \ No newline at end of file
160 + EOF
161 +}
162 +
163 +test_expect_success 'trailing whitespace & no newline at the end of file' '
164 + >target &&
165 + create_patch | git apply --whitespace=fix - &&
166 + grep "newline$" target
167 +'
168 +
169 test_done
ws.c
+2 -3
@@ -261,9 +261,8 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro
261 /*
262 * Strip trailing whitespace
263 */
264 - if ((ws_rule & WS_TRAILING_SPACE) &&
265 - (2 <= len && isspace(src[len-2]))) {
266 - if (src[len - 1] == '\n') {
264 + if (ws_rule & WS_TRAILING_SPACE) {
265 + if (1 < len && src[len - 1] == '\n') {
266 add_nl_to_tail = 1;
267 len--;
268 if (1 < len && src[len - 1] == '\r') {