Skip to content

Commit 3e3ec2a

Browse files
arachsysgitster
authored andcommitted
whitespace: add tab-in-indent error class
Some projects and languages use coding style where no tab character is used to indent the lines. This only adds support and documentation for "apply --whitespace=warn" and "diff --check"; later patches add "apply --whitespace=fix" and tests. Signed-off-by: Chris Webb <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 727c371 commit 3e3ec2a

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

Documentation/config.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,8 @@ core.whitespace::
481481
error (enabled by default).
482482
* `indent-with-non-tab` treats a line that is indented with 8 or more
483483
space characters as an error (not enabled by default).
484+
* `tab-in-indent` treats a tab character in the initial indent part of
485+
the line as an error (not enabled by default).
484486
* `blank-at-eof` treats blank lines added at the end of file as an error
485487
(enabled by default).
486488
* `trailing-space` is a short-hand to cover both `blank-at-eol` and

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ void shift_tree_by(const unsigned char *, const unsigned char *, unsigned char *
10401040
#define WS_INDENT_WITH_NON_TAB 04
10411041
#define WS_CR_AT_EOL 010
10421042
#define WS_BLANK_AT_EOF 020
1043+
#define WS_TAB_IN_INDENT 040
10431044
#define WS_TRAILING_SPACE (WS_BLANK_AT_EOL|WS_BLANK_AT_EOF)
10441045
#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
10451046
extern unsigned whitespace_rule_cfg;

ws.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ static struct whitespace_rule {
1919
{ "cr-at-eol", WS_CR_AT_EOL, 1 },
2020
{ "blank-at-eol", WS_BLANK_AT_EOL, 0 },
2121
{ "blank-at-eof", WS_BLANK_AT_EOF, 0 },
22+
{ "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 },
2223
};
2324

2425
unsigned parse_whitespace_rule(const char *string)
@@ -57,6 +58,9 @@ unsigned parse_whitespace_rule(const char *string)
5758
}
5859
string = ep;
5960
}
61+
62+
if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
63+
die("cannot enforce both tab-in-indent and indent-with-non-tab");
6064
return rule;
6165
}
6266

@@ -127,6 +131,11 @@ char *whitespace_error_string(unsigned ws)
127131
strbuf_addstr(&err, ", ");
128132
strbuf_addstr(&err, "indent with spaces");
129133
}
134+
if (ws & WS_TAB_IN_INDENT) {
135+
if (err.len)
136+
strbuf_addstr(&err, ", ");
137+
strbuf_addstr(&err, "tab in indent");
138+
}
130139
return strbuf_detach(&err, NULL);
131140
}
132141

@@ -165,7 +174,7 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
165174
}
166175
}
167176

168-
/* Check for space before tab in initial indent. */
177+
/* Check indentation */
169178
for (i = 0; i < len; i++) {
170179
if (line[i] == ' ')
171180
continue;
@@ -177,11 +186,19 @@ static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
177186
fputs(ws, stream);
178187
fwrite(line + written, i - written, 1, stream);
179188
fputs(reset, stream);
189+
fwrite(line + i, 1, 1, stream);
180190
}
181-
} else if (stream)
182-
fwrite(line + written, i - written, 1, stream);
183-
if (stream)
184-
fwrite(line + i, 1, 1, stream);
191+
} else if (ws_rule & WS_TAB_IN_INDENT) {
192+
result |= WS_TAB_IN_INDENT;
193+
if (stream) {
194+
fwrite(line + written, i - written, 1, stream);
195+
fputs(ws, stream);
196+
fwrite(line + i, 1, 1, stream);
197+
fputs(reset, stream);
198+
}
199+
} else if (stream) {
200+
fwrite(line + written, i - written + 1, 1, stream);
201+
}
185202
written = i + 1;
186203
}
187204

0 commit comments

Comments
 (0)