attr: convert git_all_attrs() to use "struct attr_check"
[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 },
3e3ec2ab 22 { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 },
cf1b7869
JH
23};
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");
2c5495f7
RM
36 ep = strchrnul(string, ',');
37 len = ep - string;
cf1b7869
JH
38
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 }
f4b05a49
JS
56 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 }
cf1b7869
JH
66 string = ep;
67 }
3e3ec2ab
CW
68
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");
cf1b7869
JH
71 return rule;
72}
73
7bd18054 74static void setup_whitespace_attr_check(struct attr_check_item *check)
cf1b7869
JH
75{
76 static struct git_attr *attr_whitespace;
77
78 if (!attr_whitespace)
7fb0eaa2 79 attr_whitespace = git_attr("whitespace");
cf1b7869
JH
80 check[0].attr = attr_whitespace;
81}
82
83unsigned whitespace_rule(const char *pathname)
84{
7bd18054 85 struct attr_check_item attr_whitespace_rule;
cf1b7869
JH
86
87 setup_whitespace_attr_check(&attr_whitespace_rule);
7bd18054 88 if (!git_check_attrs(pathname, 1, &attr_whitespace_rule)) {
cf1b7869
JH
89 const char *value;
90
91 value = attr_whitespace_rule.value;
92 if (ATTR_TRUE(value)) {
93 /* true (whitespace) */
f4b05a49 94 unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
cf1b7869
JH
95 int i;
96 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
727c3718
JH
97 if (!whitespace_rule_names[i].loosens_error &&
98 !whitespace_rule_names[i].exclude_default)
a437900f 99 all_rule |= whitespace_rule_names[i].rule_bits;
cf1b7869
JH
100 return all_rule;
101 } else if (ATTR_FALSE(value)) {
102 /* false (-whitespace) */
f4b05a49 103 return ws_tab_width(whitespace_rule_cfg);
cf1b7869
JH
104 } 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}
c1795bb0
WC
115
116/* The returned string should be freed by the caller. */
117char *whitespace_error_string(unsigned ws)
118{
f285a2d7 119 struct strbuf err = STRBUF_INIT;
aeb84b05 120 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
420f4f04 121 strbuf_addstr(&err, "trailing whitespace");
aeb84b05
JH
122 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 }
c1795bb0
WC
131 if (ws & WS_SPACE_BEFORE_TAB) {
132 if (err.len)
133 strbuf_addstr(&err, ", ");
420f4f04 134 strbuf_addstr(&err, "space before tab in indent");
c1795bb0
WC
135 }
136 if (ws & WS_INDENT_WITH_NON_TAB) {
137 if (err.len)
138 strbuf_addstr(&err, ", ");
420f4f04 139 strbuf_addstr(&err, "indent with spaces");
c1795bb0 140 }
3e3ec2ab
CW
141 if (ws & WS_TAB_IN_INDENT) {
142 if (err.len)
143 strbuf_addstr(&err, ", ");
144 strbuf_addstr(&err, "tab in indent");
145 }
c1795bb0
WC
146 return strbuf_detach(&err, NULL);
147}
148
149/* If stream is non-NULL, emits the line after checking. */
8f8841e9
JH
150static 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)
c1795bb0
WC
153{
154 unsigned result = 0;
954ecd43 155 int written = 0;
c1795bb0
WC
156 int trailing_whitespace = -1;
157 int trailing_newline = 0;
b2979ff5 158 int trailing_carriage_return = 0;
c1795bb0
WC
159 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 }
b2979ff5
JH
166 if ((ws_rule & WS_CR_AT_EOL) &&
167 len > 0 && line[len - 1] == '\r') {
168 trailing_carriage_return = 1;
169 len--;
170 }
c1795bb0
WC
171
172 /* Check for trailing whitespace. */
aeb84b05 173 if (ws_rule & WS_BLANK_AT_EOL) {
c1795bb0
WC
174 for (i = len - 1; i >= 0; i--) {
175 if (isspace(line[i])) {
176 trailing_whitespace = i;
aeb84b05 177 result |= WS_BLANK_AT_EOL;
c1795bb0
WC
178 }
179 else
180 break;
181 }
182 }
183
cfd1a984
KB
184 if (trailing_whitespace == -1)
185 trailing_whitespace = len;
186
3e3ec2ab 187 /* Check indentation */
cfd1a984 188 for (i = 0; i < trailing_whitespace; i++) {
9afa2d4a 189 if (line[i] == ' ')
1020999a 190 continue;
1020999a 191 if (line[i] != '\t')
c1795bb0 192 break;
ffe56885 193 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
1020999a 194 result |= WS_SPACE_BEFORE_TAB;
ffe56885
BF
195 if (stream) {
196 fputs(ws, stream);
197 fwrite(line + written, i - written, 1, stream);
198 fputs(reset, stream);
3e3ec2ab 199 fwrite(line + i, 1, 1, stream);
ffe56885 200 }
3e3ec2ab
CW
201 } 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 }
9afa2d4a 212 written = i + 1;
c1795bb0
WC
213 }
214
215 /* Check for indent using non-tab. */
f4b05a49 216 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
c1795bb0 217 result |= WS_INDENT_WITH_NON_TAB;
ffe56885 218 if (stream) {
c1795bb0 219 fputs(ws, stream);
ffe56885 220 fwrite(line + written, i - written, 1, stream);
c1795bb0 221 fputs(reset, stream);
c1795bb0 222 }
ffe56885
BF
223 written = i;
224 }
c1795bb0 225
ffe56885 226 if (stream) {
b2979ff5
JH
227 /*
228 * Now the rest of the line starts at "written".
229 * The non-highlighted part ends at "trailing_whitespace".
230 */
c1795bb0
WC
231
232 /* Emit non-highlighted (middle) segment. */
954ecd43 233 if (trailing_whitespace - written > 0) {
c1795bb0 234 fputs(set, stream);
954ecd43
BF
235 fwrite(line + written,
236 trailing_whitespace - written, 1, stream);
c1795bb0
WC
237 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 }
b2979ff5
JH
247 if (trailing_carriage_return)
248 fputc('\r', stream);
c1795bb0
WC
249 if (trailing_newline)
250 fputc('\n', stream);
251 }
252 return result;
253}
fe3403c3 254
8f8841e9
JH
255void 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
877f23cc
JH
267int 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
d511bd33
CW
282/* 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)
fe3403c3
JH
284{
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;
fe3403c3
JH
297
298 /*
299 * Strip trailing whitespace
300 */
afd9db41 301 if (ws_rule & WS_BLANK_AT_EOL) {
422a82f2 302 if (0 < len && src[len - 1] == '\n') {
fe3403c3
JH
303 add_nl_to_tail = 1;
304 len--;
422a82f2 305 if (0 < len && src[len - 1] == '\r') {
fe3403c3
JH
306 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) &&
f4b05a49 330 ws_tab_width(ws_rule) <= i - last_tab_in_indent)
fe3403c3
JH
331 need_fix_leading_space = 1;
332 } else
333 break;
334 }
335
fe3403c3
JH
336 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;
d511bd33 357 strbuf_addch(dst, ch);
fe3403c3
JH
358 } else {
359 consecutive_spaces++;
f4b05a49 360 if (consecutive_spaces == ws_tab_width(ws_rule)) {
d511bd33 361 strbuf_addch(dst, '\t');
fe3403c3
JH
362 consecutive_spaces = 0;
363 }
364 }
365 }
366 while (0 < consecutive_spaces--)
d511bd33 367 strbuf_addch(dst, ' ');
fe3403c3
JH
368 len -= last;
369 src += last;
370 fixed = 1;
4e35c51e
CW
371 } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
372 /* Expand tabs into spaces */
d35711ad 373 int start = dst->len;
4e35c51e
CW
374 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, ' ');
f4b05a49 379 } while ((dst->len - start) % ws_tab_width(ws_rule));
4e35c51e
CW
380 else
381 strbuf_addch(dst, src[i]);
382 }
383 len -= last;
384 src += last;
385 fixed = 1;
fe3403c3
JH
386 }
387
d511bd33 388 strbuf_add(dst, src, len);
fe3403c3 389 if (add_cr_to_tail)
d511bd33 390 strbuf_addch(dst, '\r');
fe3403c3 391 if (add_nl_to_tail)
d511bd33 392 strbuf_addch(dst, '\n');
fe3403c3
JH
393 if (fixed && error_count)
394 (*error_count)++;
fe3403c3 395}