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 <gitster@pobox.com>
Junio C Hamano committed
Sep 5, 2009 at 22:21 UTC
aeb84b05ae448596c336807631d9633492b3049a
3 files changed
+21
-12
Documentation/config.txt
+3
-1
@@ -382,7 +382,7 @@ core.whitespace::
382
consider them as errors. You can prefix `-` to disable
383
any of them (e.g. `-trailing-space`):
384
+
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
386
as an error (enabled by default).
387
* `space-before-tab` treats a space character that appears immediately
388
before a tab character in the initial indent part of the line as an
@@ -391,6 +391,8 @@ core.whitespace::
391
space characters as an error (not enabled by default).
392
* `blank-at-eof` treats blank lines added at the end of file as an error
393
(enabled by default).
394
+* `trailing-space` is a short-hand to cover both `blank-at-eol` and
395
+ `blank-at-eof`.
396
* `cr-at-eol` treats a carriage-return at the end of line as
397
part of the line terminator, i.e. with it, `trailing-space`
398
does not trigger if the character before such a carriage-return
cache.h
+3
-2
@@ -841,12 +841,13 @@ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, i
841
* whitespace rules.
842
* used by both diff and apply
843
*/
844
-#define WS_TRAILING_SPACE 01
844
+#define WS_BLANK_AT_EOL 01
845
#define WS_SPACE_BEFORE_TAB 02
846
#define WS_INDENT_WITH_NON_TAB 04
847
#define WS_CR_AT_EOL 010
848
#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)
851
extern unsigned whitespace_rule_cfg;
852
extern unsigned whitespace_rule(const char *);
853
extern unsigned parse_whitespace_rule(const char *);
ws.c
+15
-9
@@ -15,6 +15,7 @@ static struct whitespace_rule {
15
{ "space-before-tab", WS_SPACE_BEFORE_TAB },
16
{ "indent-with-non-tab", WS_INDENT_WITH_NON_TAB },
17
{ "cr-at-eol", WS_CR_AT_EOL },
18
+ { "blank-at-eol", WS_BLANK_AT_EOL },
19
{ "blank-at-eof", WS_BLANK_AT_EOF },
20
};
21
@@ -101,9 +102,19 @@ unsigned whitespace_rule(const char *pathname)
102
char *whitespace_error_string(unsigned ws)
103
{
104
struct strbuf err;
105
+
106
strbuf_init(&err, 0);
105
- if (ws & WS_TRAILING_SPACE)
107
+ if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
108
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
+ }
118
if (ws & WS_SPACE_BEFORE_TAB) {
119
if (err.len)
120
strbuf_addstr(&err, ", ");
@@ -114,11 +125,6 @@ char *whitespace_error_string(unsigned ws)
125
strbuf_addstr(&err, ", ");
126
strbuf_addstr(&err, "indent with spaces");
127
}
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
- }
128
return strbuf_detach(&err, NULL);
129
}
130
@@ -146,11 +152,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
152
}
153
154
/* Check for trailing whitespace. */
149
- if (ws_rule & WS_TRAILING_SPACE) {
155
+ if (ws_rule & WS_BLANK_AT_EOL) {
156
for (i = len - 1; i >= 0; i--) {
157
if (isspace(line[i])) {
158
trailing_whitespace = i;
153
- result |= WS_TRAILING_SPACE;
159
+ result |= WS_BLANK_AT_EOL;
160
}
161
else
162
break;
@@ -266,7 +272,7 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro
272
/*
273
* Strip trailing whitespace
274
*/
269
- if ((ws_rule & WS_TRAILING_SPACE) &&
275
+ if ((ws_rule & WS_BLANK_AT_EOL) &&
276
(2 <= len && isspace(src[len-2]))) {
277
if (src[len - 1] == '\n') {
278
add_nl_to_tail = 1;