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>
This commit is contained in:
Junio C Hamano
2009-09-05 22:21:17 -07:00
parent 690ed84363
commit aeb84b05ae
3 changed files with 21 additions and 12 deletions

View File

@ -382,7 +382,7 @@ core.whitespace::
consider them as errors. You can prefix `-` to disable consider them as errors. You can prefix `-` to disable
any of them (e.g. `-trailing-space`): any of them (e.g. `-trailing-space`):
+ +
* `trailing-space` treats trailing whitespaces at the end of the line * `blank-at-eol` treats trailing whitespaces at the end of the line
as an error (enabled by default). as an error (enabled by default).
* `space-before-tab` treats a space character that appears immediately * `space-before-tab` treats a space character that appears immediately
before a tab character in the initial indent part of the line as an before a tab character in the initial indent part of the line as an
@ -391,6 +391,8 @@ core.whitespace::
space characters as an error (not enabled by default). space characters as an error (not enabled by default).
* `blank-at-eof` treats blank lines added at the end of file as an error * `blank-at-eof` treats blank lines added at the end of file as an error
(enabled by default). (enabled by default).
* `trailing-space` is a short-hand to cover both `blank-at-eol` and
`blank-at-eof`.
* `cr-at-eol` treats a carriage-return at the end of line as * `cr-at-eol` treats a carriage-return at the end of line as
part of the line terminator, i.e. with it, `trailing-space` part of the line terminator, i.e. with it, `trailing-space`
does not trigger if the character before such a carriage-return does not trigger if the character before such a carriage-return

View File

@ -841,12 +841,13 @@ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, i
* whitespace rules. * whitespace rules.
* used by both diff and apply * used by both diff and apply
*/ */
#define WS_TRAILING_SPACE 01 #define WS_BLANK_AT_EOL 01
#define WS_SPACE_BEFORE_TAB 02 #define WS_SPACE_BEFORE_TAB 02
#define WS_INDENT_WITH_NON_TAB 04 #define WS_INDENT_WITH_NON_TAB 04
#define WS_CR_AT_EOL 010 #define WS_CR_AT_EOL 010
#define WS_BLANK_AT_EOF 020 #define WS_BLANK_AT_EOF 020
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB|WS_BLANK_AT_EOF) #define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
extern unsigned whitespace_rule_cfg; extern unsigned whitespace_rule_cfg;
extern unsigned whitespace_rule(const char *); extern unsigned whitespace_rule(const char *);
extern unsigned parse_whitespace_rule(const char *); extern unsigned parse_whitespace_rule(const char *);

24
ws.c
View File

@ -15,6 +15,7 @@ static struct whitespace_rule {
{ "space-before-tab", WS_SPACE_BEFORE_TAB }, { "space-before-tab", WS_SPACE_BEFORE_TAB },
{ "indent-with-non-tab", WS_INDENT_WITH_NON_TAB }, { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB },
{ "cr-at-eol", WS_CR_AT_EOL }, { "cr-at-eol", WS_CR_AT_EOL },
{ "blank-at-eol", WS_BLANK_AT_EOL },
{ "blank-at-eof", WS_BLANK_AT_EOF }, { "blank-at-eof", WS_BLANK_AT_EOF },
}; };
@ -101,9 +102,19 @@ unsigned whitespace_rule(const char *pathname)
char *whitespace_error_string(unsigned ws) char *whitespace_error_string(unsigned ws)
{ {
struct strbuf err; struct strbuf err;
strbuf_init(&err, 0); strbuf_init(&err, 0);
if (ws & WS_TRAILING_SPACE) if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
strbuf_addstr(&err, "trailing whitespace"); strbuf_addstr(&err, "trailing whitespace");
else {
if (ws & WS_BLANK_AT_EOL)
strbuf_addstr(&err, "trailing whitespace");
if (ws & WS_BLANK_AT_EOF) {
if (err.len)
strbuf_addstr(&err, ", ");
strbuf_addstr(&err, "new blank line at EOF");
}
}
if (ws & WS_SPACE_BEFORE_TAB) { if (ws & WS_SPACE_BEFORE_TAB) {
if (err.len) if (err.len)
strbuf_addstr(&err, ", "); strbuf_addstr(&err, ", ");
@ -114,11 +125,6 @@ char *whitespace_error_string(unsigned ws)
strbuf_addstr(&err, ", "); strbuf_addstr(&err, ", ");
strbuf_addstr(&err, "indent with spaces"); strbuf_addstr(&err, "indent with spaces");
} }
if (ws & WS_BLANK_AT_EOF) {
if (err.len)
strbuf_addstr(&err, ", ");
strbuf_addstr(&err, "new blank line at EOF");
}
return strbuf_detach(&err, NULL); return strbuf_detach(&err, NULL);
} }
@ -146,11 +152,11 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
} }
/* Check for trailing whitespace. */ /* Check for trailing whitespace. */
if (ws_rule & WS_TRAILING_SPACE) { if (ws_rule & WS_BLANK_AT_EOL) {
for (i = len - 1; i >= 0; i--) { for (i = len - 1; i >= 0; i--) {
if (isspace(line[i])) { if (isspace(line[i])) {
trailing_whitespace = i; trailing_whitespace = i;
result |= WS_TRAILING_SPACE; result |= WS_BLANK_AT_EOL;
} }
else else
break; break;
@ -266,7 +272,7 @@ int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *erro
/* /*
* Strip trailing whitespace * Strip trailing whitespace
*/ */
if ((ws_rule & WS_TRAILING_SPACE) && if ((ws_rule & WS_BLANK_AT_EOL) &&
(2 <= len && isspace(src[len-2]))) { (2 <= len && isspace(src[len-2]))) {
if (src[len - 1] == '\n') { if (src[len - 1] == '\n') {
add_nl_to_tail = 1; add_nl_to_tail = 1;