Skip to content

Commit aeb84b0

Browse files
committed
core.whitespace: split trailing-space into blank-at-{eol,eof}
People who configured trailing-space depended on it to catch both extra white space at the end of line, and extra blank lines at the end of file. Earlier attempt to introduce only blank-at-eof gave them an escape hatch to keep the old behaviour, but it is a regression until they explicitly specify the new error class. This introduces a blank-at-eol that only catches extra white space at the end of line, and makes the traditional trailing-space a convenient synonym to catch both blank-at-eol and blank-at-eof. This way, people who used trailing-space continue to catch both classes of errors. Signed-off-by: Junio C Hamano <[email protected]>
1 parent 690ed84 commit aeb84b0

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

Documentation/config.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ core.whitespace::
382382
consider them as errors. You can prefix `-` to disable
383383
any of them (e.g. `-trailing-space`):
384384
+
385-
* `trailing-space` treats trailing whitespaces at the end of the line
385+
* `blank-at-eol` treats trailing whitespaces at the end of the line
386386
as an error (enabled by default).
387387
* `space-before-tab` treats a space character that appears immediately
388388
before a tab character in the initial indent part of the line as an
@@ -391,6 +391,8 @@ core.whitespace::
391391
space characters as an error (not enabled by default).
392392
* `blank-at-eof` treats blank lines added at the end of file as an error
393393
(enabled by default).
394+
* `trailing-space` is a short-hand to cover both `blank-at-eol` and
395+
`blank-at-eof`.
394396
* `cr-at-eol` treats a carriage-return at the end of line as
395397
part of the line terminator, i.e. with it, `trailing-space`
396398
does not trigger if the character before such a carriage-return

cache.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,12 +841,13 @@ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, i
841841
* whitespace rules.
842842
* used by both diff and apply
843843
*/
844-
#define WS_TRAILING_SPACE 01
844+
#define WS_BLANK_AT_EOL 01
845845
#define WS_SPACE_BEFORE_TAB 02
846846
#define WS_INDENT_WITH_NON_TAB 04
847847
#define WS_CR_AT_EOL 010
848848
#define WS_BLANK_AT_EOF 020
849-
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|WS_BLANK_AT_EOF)
849+
#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
850+
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
850851
extern unsigned whitespace_rule_cfg;
851852
extern unsigned whitespace_rule(const char *);
852853
extern unsigned parse_whitespace_rule(const char *);

ws.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static struct whitespace_rule {
1515
{ "space-before-tab", WS_SPACE_BEFORE_TAB },
1616
{ "indent-with-non-tab", WS_INDENT_WITH_NON_TAB },
1717
{ "cr-at-eol", WS_CR_AT_EOL },
18+
{ "blank-at-eol", WS_BLANK_AT_EOL },
1819
{ "blank-at-eof", WS_BLANK_AT_EOF },
1920
};
2021

@@ -101,9 +102,19 @@ unsigned whitespace_rule(const char *pathname)
101102
char *whitespace_error_string(unsigned ws)
102103
{
103104
struct strbuf err;
105+
104106
strbuf_init(&err, 0);
105-
if (ws & WS_TRAILING_SPACE)
107+
if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
106108
strbuf_addstr(&err, "trailing whitespace");
109+
else {
110+
if (ws & WS_BLANK_AT_EOL)
111+
strbuf_addstr(&err, "trailing whitespace");
112+
if (ws & WS_BLANK_AT_EOF) {
113+
if (err.len)
114+
strbuf_addstr(&err, ", ");
115+
strbuf_addstr(&err, "new blank line at EOF");
116+
}
117+
}
107118
if (ws & WS_SPACE_BEFORE_TAB) {
108119
if (err.len)
109120
strbuf_addstr(&err, ", ");
@@ -114,11 +125,6 @@ char *whitespace_error_string(unsigned ws)
114125
strbuf_addstr(&err, ", ");
115126
strbuf_addstr(&err, "indent with spaces");
116127
}
117-
if (ws & WS_BLANK_AT_EOF) {
118-
if (err.len)
119-
strbuf_addstr(&err, ", ");
120-
strbuf_addstr(&err, "new blank line at EOF");
121-
}
122128
return strbuf_detach(&err, NULL);
123129
}
124130

@@ -146,11 +152,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
146152
}
147153

148154
/* Check for trailing whitespace. */
149-
if (ws_rule & WS_TRAILING_SPACE) {
155+
if (ws_rule & WS_BLANK_AT_EOL) {
150156
for (i = len - 1; i >= 0; i--) {
151157
if (isspace(line[i])) {
152158
trailing_whitespace = i;
153-
result |= WS_TRAILING_SPACE;
159+
result |= WS_BLANK_AT_EOL;
154160
}
155161
else
156162
break;
@@ -266,7 +272,7 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro
266272
/*
267273
* Strip trailing whitespace
268274
*/
269-
if ((ws_rule & WS_TRAILING_SPACE) &&
275+
if ((ws_rule & WS_BLANK_AT_EOL) &&
270276
(2 <= len && isspace(src[len-2]))) {
271277
if (src[len - 1] == '\n') {
272278
add_nl_to_tail = 1;

0 commit comments

Comments
 (0)