blob: ea4b2b1dfd64ce41f4752a93f4d5478c4e53deda [file] [log] [blame]
Junio C Hamanocf1b7862007-12-06 00:14:14 -08001/*
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;
Junio C Hamano727c3712010-04-03 00:37:00 +010013 unsigned loosens_error:1,
14 exclude_default:1;
Junio C Hamanocf1b7862007-12-06 00:14:14 -080015} whitespace_rule_names[] = {
Junio C Hamanoa4379002009-06-21 02:35:18 -070016 { "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 },
Junio C Hamanoafd9db42009-09-15 03:28:08 -070020 { "blank-at-eol", WS_BLANK_AT_EOL, 0 },
21 { "blank-at-eof", WS_BLANK_AT_EOF, 0 },
Chris Webb3e3ec2a2010-04-03 00:37:08 +010022 { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 },
Junio C Hamanocf1b7862007-12-06 00:14:14 -080023};
24
25unsigned parse_whitespace_rule(const char *string)
26{
27 unsigned rule = WS_DEFAULT_RULE;
28
29 while (string) {
30 int i;
31 size_t len;
32 const char *ep;
33 int negated = 0;
34
35 string = string + strspn(string, ", \t\n\r");
Rohit Mani2c5495f2014-03-07 22:48:31 -080036 ep = strchrnul(string, ',');
37 len = ep - string;
Junio C Hamanocf1b7862007-12-06 00:14:14 -080038
39 if (*string == '-') {
40 negated = 1;
41 string++;
42 len--;
43 }
44 if (!len)
45 break;
46 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
47 if (strncmp(whitespace_rule_names[i].rule_name,
48 string, len))
49 continue;
50 if (negated)
51 rule &= ~whitespace_rule_names[i].rule_bits;
52 else
53 rule |= whitespace_rule_names[i].rule_bits;
54 break;
55 }
Johannes Sixtf4b05a42010-11-30 09:29:11 +010056 if (strncmp(string, "tabwidth=", 9) == 0) {
57 unsigned tabwidth = atoi(string + 9);
58 if (0 < tabwidth && tabwidth < 0100) {
59 rule &= ~WS_TAB_WIDTH_MASK;
60 rule |= tabwidth;
61 }
62 else
63 warning("tabwidth %.*s out of range",
64 (int)(len - 9), string + 9);
65 }
Junio C Hamanocf1b7862007-12-06 00:14:14 -080066 string = ep;
67 }
Chris Webb3e3ec2a2010-04-03 00:37:08 +010068
69 if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
70 die("cannot enforce both tab-in-indent and indent-with-non-tab");
Junio C Hamanocf1b7862007-12-06 00:14:14 -080071 return rule;
72}
73
74static void setup_whitespace_attr_check(struct git_attr_check *check)
75{
76 static struct git_attr *attr_whitespace;
77
78 if (!attr_whitespace)
Junio C Hamano7fb0eaa2010-01-16 20:39:59 -080079 attr_whitespace = git_attr("whitespace");
Junio C Hamanocf1b7862007-12-06 00:14:14 -080080 check[0].attr = attr_whitespace;
81}
82
83unsigned whitespace_rule(const char *pathname)
84{
85 struct git_attr_check attr_whitespace_rule;
86
87 setup_whitespace_attr_check(&attr_whitespace_rule);
Michael Haggertyd932f4e2011-08-04 06:36:33 +020088 if (!git_check_attr(pathname, 1, &attr_whitespace_rule)) {
Junio C Hamanocf1b7862007-12-06 00:14:14 -080089 const char *value;
90
91 value = attr_whitespace_rule.value;
92 if (ATTR_TRUE(value)) {
93 /* true (whitespace) */
Johannes Sixtf4b05a42010-11-30 09:29:11 +010094 unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
Junio C Hamanocf1b7862007-12-06 00:14:14 -080095 int i;
96 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
Junio C Hamano727c3712010-04-03 00:37:00 +010097 if (!whitespace_rule_names[i].loosens_error &&
98 !whitespace_rule_names[i].exclude_default)
Junio C Hamanoa4379002009-06-21 02:35:18 -070099 all_rule |= whitespace_rule_names[i].rule_bits;
Junio C Hamanocf1b7862007-12-06 00:14:14 -0800100 return all_rule;
101 } else if (ATTR_FALSE(value)) {
102 /* false (-whitespace) */
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100103 return ws_tab_width(whitespace_rule_cfg);
Junio C Hamanocf1b7862007-12-06 00:14:14 -0800104 } else if (ATTR_UNSET(value)) {
105 /* reset to default (!whitespace) */
106 return whitespace_rule_cfg;
107 } else {
108 /* string */
109 return parse_whitespace_rule(value);
110 }
111 } else {
112 return whitespace_rule_cfg;
113 }
114}
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100115
116/* The returned string should be freed by the caller. */
117char *whitespace_error_string(unsigned ws)
118{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500119 struct strbuf err = STRBUF_INIT;
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700120 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
Wincent Colaiuta420f4f02007-12-14 12:23:43 +0100121 strbuf_addstr(&err, "trailing whitespace");
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700122 else {
123 if (ws & WS_BLANK_AT_EOL)
124 strbuf_addstr(&err, "trailing whitespace");
125 if (ws & WS_BLANK_AT_EOF) {
126 if (err.len)
127 strbuf_addstr(&err, ", ");
128 strbuf_addstr(&err, "new blank line at EOF");
129 }
130 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100131 if (ws & WS_SPACE_BEFORE_TAB) {
132 if (err.len)
133 strbuf_addstr(&err, ", ");
Wincent Colaiuta420f4f02007-12-14 12:23:43 +0100134 strbuf_addstr(&err, "space before tab in indent");
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100135 }
136 if (ws & WS_INDENT_WITH_NON_TAB) {
137 if (err.len)
138 strbuf_addstr(&err, ", ");
Wincent Colaiuta420f4f02007-12-14 12:23:43 +0100139 strbuf_addstr(&err, "indent with spaces");
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100140 }
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100141 if (ws & WS_TAB_IN_INDENT) {
142 if (err.len)
143 strbuf_addstr(&err, ", ");
144 strbuf_addstr(&err, "tab in indent");
145 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100146 return strbuf_detach(&err, NULL);
147}
148
149/* If stream is non-NULL, emits the line after checking. */
Junio C Hamano8f8841e2008-06-26 15:35:21 -0700150static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
151 FILE *stream, const char *set,
152 const char *reset, const char *ws)
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100153{
154 unsigned result = 0;
J. Bruce Fields954ecd42007-12-16 11:31:39 -0500155 int written = 0;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100156 int trailing_whitespace = -1;
157 int trailing_newline = 0;
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800158 int trailing_carriage_return = 0;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100159 int i;
160
161 /* Logic is simpler if we temporarily ignore the trailing newline. */
162 if (len > 0 && line[len - 1] == '\n') {
163 trailing_newline = 1;
164 len--;
165 }
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800166 if ((ws_rule & WS_CR_AT_EOL) &&
167 len > 0 && line[len - 1] == '\r') {
168 trailing_carriage_return = 1;
169 len--;
170 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100171
172 /* Check for trailing whitespace. */
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700173 if (ws_rule & WS_BLANK_AT_EOL) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100174 for (i = len - 1; i >= 0; i--) {
175 if (isspace(line[i])) {
176 trailing_whitespace = i;
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700177 result |= WS_BLANK_AT_EOL;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100178 }
179 else
180 break;
181 }
182 }
183
Kevin Ballardcfd1a982010-10-20 15:17:26 -0700184 if (trailing_whitespace == -1)
185 trailing_whitespace = len;
186
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100187 /* Check indentation */
Kevin Ballardcfd1a982010-10-20 15:17:26 -0700188 for (i = 0; i < trailing_whitespace; i++) {
J. Bruce Fields9afa2d42007-12-16 11:31:40 -0500189 if (line[i] == ' ')
J. Bruce Fields10209992007-12-16 11:31:38 -0500190 continue;
J. Bruce Fields10209992007-12-16 11:31:38 -0500191 if (line[i] != '\t')
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100192 break;
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500193 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
J. Bruce Fields10209992007-12-16 11:31:38 -0500194 result |= WS_SPACE_BEFORE_TAB;
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500195 if (stream) {
196 fputs(ws, stream);
197 fwrite(line + written, i - written, 1, stream);
198 fputs(reset, stream);
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100199 fwrite(line + i, 1, 1, stream);
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500200 }
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100201 } else if (ws_rule & WS_TAB_IN_INDENT) {
202 result |= WS_TAB_IN_INDENT;
203 if (stream) {
204 fwrite(line + written, i - written, 1, stream);
205 fputs(ws, stream);
206 fwrite(line + i, 1, 1, stream);
207 fputs(reset, stream);
208 }
209 } else if (stream) {
210 fwrite(line + written, i - written + 1, 1, stream);
211 }
J. Bruce Fields9afa2d42007-12-16 11:31:40 -0500212 written = i + 1;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100213 }
214
215 /* Check for indent using non-tab. */
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100216 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100217 result |= WS_INDENT_WITH_NON_TAB;
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500218 if (stream) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100219 fputs(ws, stream);
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500220 fwrite(line + written, i - written, 1, stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100221 fputs(reset, stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100222 }
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500223 written = i;
224 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100225
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500226 if (stream) {
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800227 /*
228 * Now the rest of the line starts at "written".
229 * The non-highlighted part ends at "trailing_whitespace".
230 */
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100231
232 /* Emit non-highlighted (middle) segment. */
J. Bruce Fields954ecd42007-12-16 11:31:39 -0500233 if (trailing_whitespace - written > 0) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100234 fputs(set, stream);
J. Bruce Fields954ecd42007-12-16 11:31:39 -0500235 fwrite(line + written,
236 trailing_whitespace - written, 1, stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100237 fputs(reset, stream);
238 }
239
240 /* Highlight errors in trailing whitespace. */
241 if (trailing_whitespace != len) {
242 fputs(ws, stream);
243 fwrite(line + trailing_whitespace,
244 len - trailing_whitespace, 1, stream);
245 fputs(reset, stream);
246 }
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800247 if (trailing_carriage_return)
248 fputc('\r', stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100249 if (trailing_newline)
250 fputc('\n', stream);
251 }
252 return result;
253}
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800254
Junio C Hamano8f8841e2008-06-26 15:35:21 -0700255void ws_check_emit(const char *line, int len, unsigned ws_rule,
256 FILE *stream, const char *set,
257 const char *reset, const char *ws)
258{
259 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
260}
261
262unsigned ws_check(const char *line, int len, unsigned ws_rule)
263{
264 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
265}
266
Junio C Hamano877f23c2008-06-26 15:36:59 -0700267int ws_blank_line(const char *line, int len, unsigned ws_rule)
268{
269 /*
270 * We _might_ want to treat CR differently from other
271 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
272 * for now we just use this stupid definition.
273 */
274 while (len-- > 0) {
275 if (!isspace(*line))
276 return 0;
277 line++;
278 }
279 return 1;
280}
281
Chris Webbd511bd32010-04-03 00:37:23 +0100282/* Copy the line onto the end of the strbuf while fixing whitespaces */
283void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count)
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800284{
285 /*
286 * len is number of bytes to be copied from src, starting
287 * at src. Typically src[len-1] is '\n', unless this is
288 * the incomplete last line.
289 */
290 int i;
291 int add_nl_to_tail = 0;
292 int add_cr_to_tail = 0;
293 int fixed = 0;
294 int last_tab_in_indent = -1;
295 int last_space_in_indent = -1;
296 int need_fix_leading_space = 0;
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800297
298 /*
299 * Strip trailing whitespace
300 */
Junio C Hamanoafd9db42009-09-15 03:28:08 -0700301 if (ws_rule & WS_BLANK_AT_EOL) {
Junio C Hamano422a82f2009-07-25 01:29:20 -0700302 if (0 < len && src[len - 1] == '\n') {
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800303 add_nl_to_tail = 1;
304 len--;
Junio C Hamano422a82f2009-07-25 01:29:20 -0700305 if (0 < len && src[len - 1] == '\r') {
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800306 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
307 len--;
308 }
309 }
310 if (0 < len && isspace(src[len - 1])) {
311 while (0 < len && isspace(src[len-1]))
312 len--;
313 fixed = 1;
314 }
315 }
316
317 /*
318 * Check leading whitespaces (indent)
319 */
320 for (i = 0; i < len; i++) {
321 char ch = src[i];
322 if (ch == '\t') {
323 last_tab_in_indent = i;
324 if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
325 0 <= last_space_in_indent)
326 need_fix_leading_space = 1;
327 } else if (ch == ' ') {
328 last_space_in_indent = i;
329 if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100330 ws_tab_width(ws_rule) <= i - last_tab_in_indent)
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800331 need_fix_leading_space = 1;
332 } else
333 break;
334 }
335
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800336 if (need_fix_leading_space) {
337 /* Process indent ourselves */
338 int consecutive_spaces = 0;
339 int last = last_tab_in_indent + 1;
340
341 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
342 /* have "last" point at one past the indent */
343 if (last_tab_in_indent < last_space_in_indent)
344 last = last_space_in_indent + 1;
345 else
346 last = last_tab_in_indent + 1;
347 }
348
349 /*
350 * between src[0..last-1], strip the funny spaces,
351 * updating them to tab as needed.
352 */
353 for (i = 0; i < last; i++) {
354 char ch = src[i];
355 if (ch != ' ') {
356 consecutive_spaces = 0;
Chris Webbd511bd32010-04-03 00:37:23 +0100357 strbuf_addch(dst, ch);
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800358 } else {
359 consecutive_spaces++;
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100360 if (consecutive_spaces == ws_tab_width(ws_rule)) {
Chris Webbd511bd32010-04-03 00:37:23 +0100361 strbuf_addch(dst, '\t');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800362 consecutive_spaces = 0;
363 }
364 }
365 }
366 while (0 < consecutive_spaces--)
Chris Webbd511bd32010-04-03 00:37:23 +0100367 strbuf_addch(dst, ' ');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800368 len -= last;
369 src += last;
370 fixed = 1;
Chris Webb4e35c512010-04-03 00:37:30 +0100371 } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
372 /* Expand tabs into spaces */
Johannes Sixtd35711a2010-11-30 09:22:04 +0100373 int start = dst->len;
Chris Webb4e35c512010-04-03 00:37:30 +0100374 int last = last_tab_in_indent + 1;
375 for (i = 0; i < last; i++) {
376 if (src[i] == '\t')
377 do {
378 strbuf_addch(dst, ' ');
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100379 } while ((dst->len - start) % ws_tab_width(ws_rule));
Chris Webb4e35c512010-04-03 00:37:30 +0100380 else
381 strbuf_addch(dst, src[i]);
382 }
383 len -= last;
384 src += last;
385 fixed = 1;
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800386 }
387
Chris Webbd511bd32010-04-03 00:37:23 +0100388 strbuf_add(dst, src, len);
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800389 if (add_cr_to_tail)
Chris Webbd511bd32010-04-03 00:37:23 +0100390 strbuf_addch(dst, '\r');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800391 if (add_nl_to_tail)
Chris Webbd511bd32010-04-03 00:37:23 +0100392 strbuf_addch(dst, '\n');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800393 if (fixed && error_count)
394 (*error_count)++;
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800395}