OSDN Git Service

Trailing whitespace and no newline fix
authorSZEDER Gábor <szeder@ira.uka.de>
Thu, 23 Jul 2009 00:24:38 +0000 (19:24 -0500)
committerJunio C Hamano <gitster@pobox.com>
Thu, 23 Jul 2009 01:54:55 +0000 (18:54 -0700)
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>
t/t4124-apply-ws-rule.sh
ws.c

index f83322e..5698a9a 100755 (executable)
@@ -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 819c797..8d855b7 100644 (file)
--- a/ws.c
+++ b/ws.c
@@ -261,9 +261,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') {