blob: 9fb9b14760b100972a1adb5198bff9d6a9941808 [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");
36 ep = strchr(string, ',');
37 if (!ep)
38 len = strlen(string);
39 else
40 len = ep - string;
41
42 if (*string == '-') {
43 negated = 1;
44 string++;
45 len--;
46 }
47 if (!len)
48 break;
49 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
50 if (strncmp(whitespace_rule_names[i].rule_name,
51 string, len))
52 continue;
53 if (negated)
54 rule &= ~whitespace_rule_names[i].rule_bits;
55 else
56 rule |= whitespace_rule_names[i].rule_bits;
57 break;
58 }
Johannes Sixtf4b05a42010-11-30 09:29:11 +010059 if (strncmp(string, "tabwidth=", 9) == 0) {
60 unsigned tabwidth = atoi(string + 9);
61 if (0 < tabwidth && tabwidth < 0100) {
62 rule &= ~WS_TAB_WIDTH_MASK;
63 rule |= tabwidth;
64 }
65 else
66 warning("tabwidth %.*s out of range",
67 (int)(len - 9), string + 9);
68 }
Junio C Hamanocf1b7862007-12-06 00:14:14 -080069 string = ep;
70 }
Chris Webb3e3ec2a2010-04-03 00:37:08 +010071
72 if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
73 die("cannot enforce both tab-in-indent and indent-with-non-tab");
Junio C Hamanocf1b7862007-12-06 00:14:14 -080074 return rule;
75}
76
77static void setup_whitespace_attr_check(struct git_attr_check *check)
78{
79 static struct git_attr *attr_whitespace;
80
81 if (!attr_whitespace)
Junio C Hamano7fb0eaa2010-01-16 20:39:59 -080082 attr_whitespace = git_attr("whitespace");
Junio C Hamanocf1b7862007-12-06 00:14:14 -080083 check[0].attr = attr_whitespace;
84}
85
86unsigned whitespace_rule(const char *pathname)
87{
88 struct git_attr_check attr_whitespace_rule;
89
90 setup_whitespace_attr_check(&attr_whitespace_rule);
91 if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
92 const char *value;
93
94 value = attr_whitespace_rule.value;
95 if (ATTR_TRUE(value)) {
96 /* true (whitespace) */
Johannes Sixtf4b05a42010-11-30 09:29:11 +010097 unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
Junio C Hamanocf1b7862007-12-06 00:14:14 -080098 int i;
99 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
Junio C Hamano727c3712010-04-03 00:37:00 +0100100 if (!whitespace_rule_names[i].loosens_error &&
101 !whitespace_rule_names[i].exclude_default)
Junio C Hamanoa4379002009-06-21 02:35:18 -0700102 all_rule |= whitespace_rule_names[i].rule_bits;
Junio C Hamanocf1b7862007-12-06 00:14:14 -0800103 return all_rule;
104 } else if (ATTR_FALSE(value)) {
105 /* false (-whitespace) */
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100106 return ws_tab_width(whitespace_rule_cfg);
Junio C Hamanocf1b7862007-12-06 00:14:14 -0800107 } else if (ATTR_UNSET(value)) {
108 /* reset to default (!whitespace) */
109 return whitespace_rule_cfg;
110 } else {
111 /* string */
112 return parse_whitespace_rule(value);
113 }
114 } else {
115 return whitespace_rule_cfg;
116 }
117}
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100118
119/* The returned string should be freed by the caller. */
120char *whitespace_error_string(unsigned ws)
121{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500122 struct strbuf err = STRBUF_INIT;
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700123 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
Wincent Colaiuta420f4f02007-12-14 12:23:43 +0100124 strbuf_addstr(&err, "trailing whitespace");
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700125 else {
126 if (ws & WS_BLANK_AT_EOL)
127 strbuf_addstr(&err, "trailing whitespace");
128 if (ws & WS_BLANK_AT_EOF) {
129 if (err.len)
130 strbuf_addstr(&err, ", ");
131 strbuf_addstr(&err, "new blank line at EOF");
132 }
133 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100134 if (ws & WS_SPACE_BEFORE_TAB) {
135 if (err.len)
136 strbuf_addstr(&err, ", ");
Wincent Colaiuta420f4f02007-12-14 12:23:43 +0100137 strbuf_addstr(&err, "space before tab in indent");
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100138 }
139 if (ws & WS_INDENT_WITH_NON_TAB) {
140 if (err.len)
141 strbuf_addstr(&err, ", ");
Wincent Colaiuta420f4f02007-12-14 12:23:43 +0100142 strbuf_addstr(&err, "indent with spaces");
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100143 }
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100144 if (ws & WS_TAB_IN_INDENT) {
145 if (err.len)
146 strbuf_addstr(&err, ", ");
147 strbuf_addstr(&err, "tab in indent");
148 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100149 return strbuf_detach(&err, NULL);
150}
151
152/* If stream is non-NULL, emits the line after checking. */
Junio C Hamano8f8841e2008-06-26 15:35:21 -0700153static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
154 FILE *stream, const char *set,
155 const char *reset, const char *ws)
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100156{
157 unsigned result = 0;
J. Bruce Fields954ecd42007-12-16 11:31:39 -0500158 int written = 0;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100159 int trailing_whitespace = -1;
160 int trailing_newline = 0;
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800161 int trailing_carriage_return = 0;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100162 int i;
163
164 /* Logic is simpler if we temporarily ignore the trailing newline. */
165 if (len > 0 && line[len - 1] == '\n') {
166 trailing_newline = 1;
167 len--;
168 }
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800169 if ((ws_rule & WS_CR_AT_EOL) &&
170 len > 0 && line[len - 1] == '\r') {
171 trailing_carriage_return = 1;
172 len--;
173 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100174
175 /* Check for trailing whitespace. */
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700176 if (ws_rule & WS_BLANK_AT_EOL) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100177 for (i = len - 1; i >= 0; i--) {
178 if (isspace(line[i])) {
179 trailing_whitespace = i;
Junio C Hamanoaeb84b02009-09-05 22:21:17 -0700180 result |= WS_BLANK_AT_EOL;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100181 }
182 else
183 break;
184 }
185 }
186
Kevin Ballardcfd1a982010-10-20 15:17:26 -0700187 if (trailing_whitespace == -1)
188 trailing_whitespace = len;
189
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100190 /* Check indentation */
Kevin Ballardcfd1a982010-10-20 15:17:26 -0700191 for (i = 0; i < trailing_whitespace; i++) {
J. Bruce Fields9afa2d42007-12-16 11:31:40 -0500192 if (line[i] == ' ')
J. Bruce Fields10209992007-12-16 11:31:38 -0500193 continue;
J. Bruce Fields10209992007-12-16 11:31:38 -0500194 if (line[i] != '\t')
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100195 break;
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500196 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
J. Bruce Fields10209992007-12-16 11:31:38 -0500197 result |= WS_SPACE_BEFORE_TAB;
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500198 if (stream) {
199 fputs(ws, stream);
200 fwrite(line + written, i - written, 1, stream);
201 fputs(reset, stream);
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100202 fwrite(line + i, 1, 1, stream);
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500203 }
Chris Webb3e3ec2a2010-04-03 00:37:08 +0100204 } else if (ws_rule & WS_TAB_IN_INDENT) {
205 result |= WS_TAB_IN_INDENT;
206 if (stream) {
207 fwrite(line + written, i - written, 1, stream);
208 fputs(ws, stream);
209 fwrite(line + i, 1, 1, stream);
210 fputs(reset, stream);
211 }
212 } else if (stream) {
213 fwrite(line + written, i - written + 1, 1, stream);
214 }
J. Bruce Fields9afa2d42007-12-16 11:31:40 -0500215 written = i + 1;
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100216 }
217
218 /* Check for indent using non-tab. */
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100219 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100220 result |= WS_INDENT_WITH_NON_TAB;
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500221 if (stream) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100222 fputs(ws, stream);
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500223 fwrite(line + written, i - written, 1, stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100224 fputs(reset, stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100225 }
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500226 written = i;
227 }
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100228
J. Bruce Fieldsffe56882007-12-16 11:31:41 -0500229 if (stream) {
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800230 /*
231 * Now the rest of the line starts at "written".
232 * The non-highlighted part ends at "trailing_whitespace".
233 */
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100234
235 /* Emit non-highlighted (middle) segment. */
J. Bruce Fields954ecd42007-12-16 11:31:39 -0500236 if (trailing_whitespace - written > 0) {
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100237 fputs(set, stream);
J. Bruce Fields954ecd42007-12-16 11:31:39 -0500238 fwrite(line + written,
239 trailing_whitespace - written, 1, stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100240 fputs(reset, stream);
241 }
242
243 /* Highlight errors in trailing whitespace. */
244 if (trailing_whitespace != len) {
245 fputs(ws, stream);
246 fwrite(line + trailing_whitespace,
247 len - trailing_whitespace, 1, stream);
248 fputs(reset, stream);
249 }
Junio C Hamanob2979ff2008-01-15 00:59:05 -0800250 if (trailing_carriage_return)
251 fputc('\r', stream);
Wincent Colaiutac1795bb2007-12-13 14:32:29 +0100252 if (trailing_newline)
253 fputc('\n', stream);
254 }
255 return result;
256}
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800257
Junio C Hamano8f8841e2008-06-26 15:35:21 -0700258void ws_check_emit(const char *line, int len, unsigned ws_rule,
259 FILE *stream, const char *set,
260 const char *reset, const char *ws)
261{
262 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
263}
264
265unsigned ws_check(const char *line, int len, unsigned ws_rule)
266{
267 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
268}
269
Junio C Hamano877f23c2008-06-26 15:36:59 -0700270int ws_blank_line(const char *line, int len, unsigned ws_rule)
271{
272 /*
273 * We _might_ want to treat CR differently from other
274 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
275 * for now we just use this stupid definition.
276 */
277 while (len-- > 0) {
278 if (!isspace(*line))
279 return 0;
280 line++;
281 }
282 return 1;
283}
284
Chris Webbd511bd32010-04-03 00:37:23 +0100285/* Copy the line onto the end of the strbuf while fixing whitespaces */
286void 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 -0800287{
288 /*
289 * len is number of bytes to be copied from src, starting
290 * at src. Typically src[len-1] is '\n', unless this is
291 * the incomplete last line.
292 */
293 int i;
294 int add_nl_to_tail = 0;
295 int add_cr_to_tail = 0;
296 int fixed = 0;
297 int last_tab_in_indent = -1;
298 int last_space_in_indent = -1;
299 int need_fix_leading_space = 0;
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800300
301 /*
302 * Strip trailing whitespace
303 */
Junio C Hamanoafd9db42009-09-15 03:28:08 -0700304 if (ws_rule & WS_BLANK_AT_EOL) {
Junio C Hamano422a82f2009-07-25 01:29:20 -0700305 if (0 < len && src[len - 1] == '\n') {
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800306 add_nl_to_tail = 1;
307 len--;
Junio C Hamano422a82f2009-07-25 01:29:20 -0700308 if (0 < len && src[len - 1] == '\r') {
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800309 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
310 len--;
311 }
312 }
313 if (0 < len && isspace(src[len - 1])) {
314 while (0 < len && isspace(src[len-1]))
315 len--;
316 fixed = 1;
317 }
318 }
319
320 /*
321 * Check leading whitespaces (indent)
322 */
323 for (i = 0; i < len; i++) {
324 char ch = src[i];
325 if (ch == '\t') {
326 last_tab_in_indent = i;
327 if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
328 0 <= last_space_in_indent)
329 need_fix_leading_space = 1;
330 } else if (ch == ' ') {
331 last_space_in_indent = i;
332 if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100333 ws_tab_width(ws_rule) <= i - last_tab_in_indent)
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800334 need_fix_leading_space = 1;
335 } else
336 break;
337 }
338
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800339 if (need_fix_leading_space) {
340 /* Process indent ourselves */
341 int consecutive_spaces = 0;
342 int last = last_tab_in_indent + 1;
343
344 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
345 /* have "last" point at one past the indent */
346 if (last_tab_in_indent < last_space_in_indent)
347 last = last_space_in_indent + 1;
348 else
349 last = last_tab_in_indent + 1;
350 }
351
352 /*
353 * between src[0..last-1], strip the funny spaces,
354 * updating them to tab as needed.
355 */
356 for (i = 0; i < last; i++) {
357 char ch = src[i];
358 if (ch != ' ') {
359 consecutive_spaces = 0;
Chris Webbd511bd32010-04-03 00:37:23 +0100360 strbuf_addch(dst, ch);
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800361 } else {
362 consecutive_spaces++;
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100363 if (consecutive_spaces == ws_tab_width(ws_rule)) {
Chris Webbd511bd32010-04-03 00:37:23 +0100364 strbuf_addch(dst, '\t');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800365 consecutive_spaces = 0;
366 }
367 }
368 }
369 while (0 < consecutive_spaces--)
Chris Webbd511bd32010-04-03 00:37:23 +0100370 strbuf_addch(dst, ' ');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800371 len -= last;
372 src += last;
373 fixed = 1;
Chris Webb4e35c512010-04-03 00:37:30 +0100374 } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
375 /* Expand tabs into spaces */
Johannes Sixtd35711a2010-11-30 09:22:04 +0100376 int start = dst->len;
Chris Webb4e35c512010-04-03 00:37:30 +0100377 int last = last_tab_in_indent + 1;
378 for (i = 0; i < last; i++) {
379 if (src[i] == '\t')
380 do {
381 strbuf_addch(dst, ' ');
Johannes Sixtf4b05a42010-11-30 09:29:11 +0100382 } while ((dst->len - start) % ws_tab_width(ws_rule));
Chris Webb4e35c512010-04-03 00:37:30 +0100383 else
384 strbuf_addch(dst, src[i]);
385 }
386 len -= last;
387 src += last;
388 fixed = 1;
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800389 }
390
Chris Webbd511bd32010-04-03 00:37:23 +0100391 strbuf_add(dst, src, len);
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800392 if (add_cr_to_tail)
Chris Webbd511bd32010-04-03 00:37:23 +0100393 strbuf_addch(dst, '\r');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800394 if (add_nl_to_tail)
Chris Webbd511bd32010-04-03 00:37:23 +0100395 strbuf_addch(dst, '\n');
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800396 if (fixed && error_count)
397 (*error_count)++;
Junio C Hamanofe3403c2008-02-23 16:59:16 -0800398}