Skip to content

Commit 735c674

Browse files
szedergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b810cbb commit 735c674

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

t/t4124-apply-ws-rule.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,22 @@ do
148148
done
149149
done
150150

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+
151169
test_done

ws.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro
261261
/*
262262
* Strip trailing whitespace
263263
*/
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') {
267266
add_nl_to_tail = 1;
268267
len--;
269268
if (1 < len && src[len - 1] == '\r') {

0 commit comments

Comments
 (0)