whitespace: we cannot "catch all errors known to git" anymore
[git/git.git] / ws.c
CommitLineData
cf1b7869
JH
1/*
2 * Whitespace rules
3 *
4 * Copyright (c) 2007 Junio C Hamano
5 */
6
7#include "cache.h"
8#include "attr.h"
9
10static struct whitespace_rule {
11 const char *rule_name;
12 unsigned rule_bits;
727c3718
JH
13 unsigned loosens_error:1,
14 exclude_default:1;
cf1b7869 15} whitespace_rule_names[] = {
a437900f
JH
16 { "trailing-space", WS_TRAILING_SPACE, 0 },
17 { "space-before-tab", WS_SPACE_BEFORE_TAB, 0 },
18 { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB, 0 },
19 { "cr-at-eol", WS_CR_AT_EOL, 1 },
afd9db41
JH
20 { "blank-at-eol", WS_BLANK_AT_EOL, 0 },
21 { "blank-at-eof", WS_BLANK_AT_EOF, 0 },
cf1b7869
JH
22};
23
24unsigned parse_whitespace_rule(const char *string)
25{
26 unsigned rule = WS_DEFAULT_RULE;
27
28 while (string) {
29 int i;
30 size_t len;
31 const char *ep;
32 int negated = 0;
33
34 string = string + strspn(string, ", \t\n\r");
35 ep = strchr(string, ',');
36 if (!ep)
37 len = strlen(string);
38 else
39 len = ep - string;
40
41 if (*string == '-') {
42 negated = 1;
43 string++;
44 len--;
45 }
46 if (!len)
47 break;
48 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
49 if (strncmp(whitespace_rule_names[i].rule_name,
50 string, len))
51 continue;
52 if (negated)
53 rule &= ~whitespace_rule_names[i].rule_bits;
54 else
55 rule |= whitespace_rule_names[i].rule_bits;
56 break;
57 }
58 string = ep;
59 }
60 return rule;
61}
62
63static void setup_whitespace_attr_check(struct git_attr_check *check)
64{
65 static struct git_attr *attr_whitespace;
66
67 if (!attr_whitespace)
7fb0eaa2 68 attr_whitespace = git_attr("whitespace");
cf1b7869
JH
69 check[0].attr = attr_whitespace;
70}
71
72unsigned whitespace_rule(const char *pathname)
73{
74 struct git_attr_check attr_whitespace_rule;
75
76 setup_whitespace_attr_check(&attr_whitespace_rule);
77 if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
78 const char *value;
79
80 value = attr_whitespace_rule.value;
81 if (ATTR_TRUE(value)) {
82 /* true (whitespace) */
83 unsigned all_rule = 0;
84 int i;
85 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
727c3718
JH
86 if (!whitespace_rule_names[i].loosens_error &&
87 !whitespace_rule_names[i].exclude_default)
a437900f 88 all_rule |= whitespace_rule_names[i].rule_bits;
cf1b7869
JH
89 return all_rule;
90 } else if (ATTR_FALSE(value)) {
91 /* false (-whitespace) */
92 return 0;
93 } else if (ATTR_UNSET(value)) {
94 /* reset to default (!whitespace) */
95 return whitespace_rule_cfg;
96 } else {
97 /* string */
98 return parse_whitespace_rule(value);
99 }
100 } else {
101 return whitespace_rule_cfg;
102 }
103}
c1795bb0
WC
104
105/* The returned string should be freed by the caller. */
106char *whitespace_error_string(unsigned ws)
107{
f285a2d7 108 struct strbuf err = STRBUF_INIT;
aeb84b05 109 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
420f4f04 110 strbuf_addstr(&err, "trailing whitespace");
aeb84b05
JH
111 else {
112 if (ws & WS_BLANK_AT_EOL)
113 strbuf_addstr(&err, "trailing whitespace");
114 if (ws & WS_BLANK_AT_EOF) {
115 if (err.len)
116 strbuf_addstr(&err, ", ");
117 strbuf_addstr(&err, "new blank line at EOF");
118 }
119 }
c1795bb0
WC
120 if (ws & WS_SPACE_BEFORE_TAB) {
121 if (err.len)
122 strbuf_addstr(&err, ", ");
420f4f04 123 strbuf_addstr(&err, "space before tab in indent");
c1795bb0
WC
124 }
125 if (ws & WS_INDENT_WITH_NON_TAB) {
126 if (err.len)
127 strbuf_addstr(&err, ", ");
420f4f04 128 strbuf_addstr(&err, "indent with spaces");
c1795bb0
WC
129 }
130 return strbuf_detach(&err, NULL);
131}
132
133/* If stream is non-NULL, emits the line after checking. */
8f8841e9
JH
134static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
135 FILE *stream, const char *set,
136 const char *reset, const char *ws)
c1795bb0
WC
137{
138 unsigned result = 0;
954ecd43 139 int written = 0;
c1795bb0
WC
140 int trailing_whitespace = -1;
141 int trailing_newline = 0;
b2979ff5 142 int trailing_carriage_return = 0;
c1795bb0
WC
143 int i;
144
145 /* Logic is simpler if we temporarily ignore the trailing newline. */
146 if (len > 0 && line[len - 1] == '\n') {
147 trailing_newline = 1;
148 len--;
149 }
b2979ff5
JH
150 if ((ws_rule & WS_CR_AT_EOL) &&
151 len > 0 && line[len - 1] == '\r') {
152 trailing_carriage_return = 1;
153 len--;
154 }
c1795bb0
WC
155
156 /* Check for trailing whitespace. */
aeb84b05 157 if (ws_rule & WS_BLANK_AT_EOL) {
c1795bb0
WC
158 for (i = len - 1; i >= 0; i--) {
159 if (isspace(line[i])) {
160 trailing_whitespace = i;
aeb84b05 161 result |= WS_BLANK_AT_EOL;
c1795bb0
WC
162 }
163 else
164 break;
165 }
166 }
167
168 /* Check for space before tab in initial indent. */
169 for (i = 0; i < len; i++) {
9afa2d4a 170 if (line[i] == ' ')
1020999a 171 continue;
1020999a 172 if (line[i] != '\t')
c1795bb0 173 break;
ffe56885 174 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
1020999a 175 result |= WS_SPACE_BEFORE_TAB;
ffe56885
BF
176 if (stream) {
177 fputs(ws, stream);
178 fwrite(line + written, i - written, 1, stream);
179 fputs(reset, stream);
180 }
181 } else if (stream)
182 fwrite(line + written, i - written, 1, stream);
183 if (stream)
184 fwrite(line + i, 1, 1, stream);
9afa2d4a 185 written = i + 1;
c1795bb0
WC
186 }
187
188 /* Check for indent using non-tab. */
ffe56885 189 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
c1795bb0 190 result |= WS_INDENT_WITH_NON_TAB;
ffe56885 191 if (stream) {
c1795bb0 192 fputs(ws, stream);
ffe56885 193 fwrite(line + written, i - written, 1, stream);
c1795bb0 194 fputs(reset, stream);
c1795bb0 195 }
ffe56885
BF
196 written = i;
197 }
c1795bb0 198
ffe56885 199 if (stream) {
b2979ff5
JH
200 /*
201 * Now the rest of the line starts at "written".
202 * The non-highlighted part ends at "trailing_whitespace".
203 */
c1795bb0
WC
204 if (trailing_whitespace == -1)
205 trailing_whitespace = len;
206
207 /* Emit non-highlighted (middle) segment. */
954ecd43 208 if (trailing_whitespace - written > 0) {
c1795bb0 209 fputs(set, stream);
954ecd43
BF
210 fwrite(line + written,
211 trailing_whitespace - written, 1, stream);
c1795bb0
WC
212 fputs(reset, stream);
213 }
214
215 /* Highlight errors in trailing whitespace. */
216 if (trailing_whitespace != len) {
217 fputs(ws, stream);
218 fwrite(line + trailing_whitespace,
219 len - trailing_whitespace, 1, stream);
220 fputs(reset, stream);
221 }
b2979ff5
JH
222 if (trailing_carriage_return)
223 fputc('\r', stream);
c1795bb0
WC
224 if (trailing_newline)
225 fputc('\n', stream);
226 }
227 return result;
228}
fe3403c3 229
8f8841e9
JH
230void ws_check_emit(const char *line, int len, unsigned ws_rule,
231 FILE *stream, const char *set,
232 const char *reset, const char *ws)
233{
234 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
235}
236
237unsigned ws_check(const char *line, int len, unsigned ws_rule)
238{
239 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
240}
241
877f23cc
JH
242int ws_blank_line(const char *line, int len, unsigned ws_rule)
243{
244 /*
245 * We _might_ want to treat CR differently from other
246 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
247 * for now we just use this stupid definition.
248 */
249 while (len-- > 0) {
250 if (!isspace(*line))
251 return 0;
252 line++;
253 }
254 return 1;
255}
256
fe3403c3
JH
257/* Copy the line to the buffer while fixing whitespaces */
258int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
259{
260 /*
261 * len is number of bytes to be copied from src, starting
262 * at src. Typically src[len-1] is '\n', unless this is
263 * the incomplete last line.
264 */
265 int i;
266 int add_nl_to_tail = 0;
267 int add_cr_to_tail = 0;
268 int fixed = 0;
269 int last_tab_in_indent = -1;
270 int last_space_in_indent = -1;
271 int need_fix_leading_space = 0;
272 char *buf;
273
274 /*
275 * Strip trailing whitespace
276 */
afd9db41 277 if (ws_rule & WS_BLANK_AT_EOL) {
422a82f2 278 if (0 < len && src[len - 1] == '\n') {
fe3403c3
JH
279 add_nl_to_tail = 1;
280 len--;
422a82f2 281 if (0 < len && src[len - 1] == '\r') {
fe3403c3
JH
282 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
283 len--;
284 }
285 }
286 if (0 < len && isspace(src[len - 1])) {
287 while (0 < len && isspace(src[len-1]))
288 len--;
289 fixed = 1;
290 }
291 }
292
293 /*
294 * Check leading whitespaces (indent)
295 */
296 for (i = 0; i < len; i++) {
297 char ch = src[i];
298 if (ch == '\t') {
299 last_tab_in_indent = i;
300 if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
301 0 <= last_space_in_indent)
302 need_fix_leading_space = 1;
303 } else if (ch == ' ') {
304 last_space_in_indent = i;
305 if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
306 8 <= i - last_tab_in_indent)
307 need_fix_leading_space = 1;
308 } else
309 break;
310 }
311
312 buf = dst;
313 if (need_fix_leading_space) {
314 /* Process indent ourselves */
315 int consecutive_spaces = 0;
316 int last = last_tab_in_indent + 1;
317
318 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
319 /* have "last" point at one past the indent */
320 if (last_tab_in_indent < last_space_in_indent)
321 last = last_space_in_indent + 1;
322 else
323 last = last_tab_in_indent + 1;
324 }
325
326 /*
327 * between src[0..last-1], strip the funny spaces,
328 * updating them to tab as needed.
329 */
330 for (i = 0; i < last; i++) {
331 char ch = src[i];
332 if (ch != ' ') {
333 consecutive_spaces = 0;
334 *dst++ = ch;
335 } else {
336 consecutive_spaces++;
337 if (consecutive_spaces == 8) {
338 *dst++ = '\t';
339 consecutive_spaces = 0;
340 }
341 }
342 }
343 while (0 < consecutive_spaces--)
344 *dst++ = ' ';
345 len -= last;
346 src += last;
347 fixed = 1;
348 }
349
350 memcpy(dst, src, len);
351 if (add_cr_to_tail)
352 dst[len++] = '\r';
353 if (add_nl_to_tail)
354 dst[len++] = '\n';
355 if (fixed && error_count)
356 (*error_count)++;
357 return dst + len - buf;
358}