]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | static 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 | ||
25 | unsigned 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 | } | |
59 | string = ep; | |
60 | } | |
3e3ec2ab CW |
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"); | |
cf1b7869 JH |
64 | return rule; |
65 | } | |
66 | ||
67 | static void setup_whitespace_attr_check(struct git_attr_check *check) | |
68 | { | |
69 | static struct git_attr *attr_whitespace; | |
70 | ||
71 | if (!attr_whitespace) | |
7fb0eaa2 | 72 | attr_whitespace = git_attr("whitespace"); |
cf1b7869 JH |
73 | check[0].attr = attr_whitespace; |
74 | } | |
75 | ||
76 | unsigned whitespace_rule(const char *pathname) | |
77 | { | |
78 | struct git_attr_check attr_whitespace_rule; | |
79 | ||
80 | setup_whitespace_attr_check(&attr_whitespace_rule); | |
81 | if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) { | |
82 | const char *value; | |
83 | ||
84 | value = attr_whitespace_rule.value; | |
85 | if (ATTR_TRUE(value)) { | |
86 | /* true (whitespace) */ | |
87 | unsigned all_rule = 0; | |
88 | int i; | |
89 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) | |
727c3718 JH |
90 | if (!whitespace_rule_names[i].loosens_error && |
91 | !whitespace_rule_names[i].exclude_default) | |
a437900f | 92 | all_rule |= whitespace_rule_names[i].rule_bits; |
cf1b7869 JH |
93 | return all_rule; |
94 | } else if (ATTR_FALSE(value)) { | |
95 | /* false (-whitespace) */ | |
96 | return 0; | |
97 | } else if (ATTR_UNSET(value)) { | |
98 | /* reset to default (!whitespace) */ | |
99 | return whitespace_rule_cfg; | |
100 | } else { | |
101 | /* string */ | |
102 | return parse_whitespace_rule(value); | |
103 | } | |
104 | } else { | |
105 | return whitespace_rule_cfg; | |
106 | } | |
107 | } | |
c1795bb0 WC |
108 | |
109 | /* The returned string should be freed by the caller. */ | |
110 | char *whitespace_error_string(unsigned ws) | |
111 | { | |
f285a2d7 | 112 | struct strbuf err = STRBUF_INIT; |
aeb84b05 | 113 | if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE) |
420f4f04 | 114 | strbuf_addstr(&err, "trailing whitespace"); |
aeb84b05 JH |
115 | else { |
116 | if (ws & WS_BLANK_AT_EOL) | |
117 | strbuf_addstr(&err, "trailing whitespace"); | |
118 | if (ws & WS_BLANK_AT_EOF) { | |
119 | if (err.len) | |
120 | strbuf_addstr(&err, ", "); | |
121 | strbuf_addstr(&err, "new blank line at EOF"); | |
122 | } | |
123 | } | |
c1795bb0 WC |
124 | if (ws & WS_SPACE_BEFORE_TAB) { |
125 | if (err.len) | |
126 | strbuf_addstr(&err, ", "); | |
420f4f04 | 127 | strbuf_addstr(&err, "space before tab in indent"); |
c1795bb0 WC |
128 | } |
129 | if (ws & WS_INDENT_WITH_NON_TAB) { | |
130 | if (err.len) | |
131 | strbuf_addstr(&err, ", "); | |
420f4f04 | 132 | strbuf_addstr(&err, "indent with spaces"); |
c1795bb0 | 133 | } |
3e3ec2ab CW |
134 | if (ws & WS_TAB_IN_INDENT) { |
135 | if (err.len) | |
136 | strbuf_addstr(&err, ", "); | |
137 | strbuf_addstr(&err, "tab in indent"); | |
138 | } | |
c1795bb0 WC |
139 | return strbuf_detach(&err, NULL); |
140 | } | |
141 | ||
142 | /* If stream is non-NULL, emits the line after checking. */ | |
8f8841e9 JH |
143 | static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule, |
144 | FILE *stream, const char *set, | |
145 | const char *reset, const char *ws) | |
c1795bb0 WC |
146 | { |
147 | unsigned result = 0; | |
954ecd43 | 148 | int written = 0; |
c1795bb0 WC |
149 | int trailing_whitespace = -1; |
150 | int trailing_newline = 0; | |
b2979ff5 | 151 | int trailing_carriage_return = 0; |
c1795bb0 WC |
152 | int i; |
153 | ||
154 | /* Logic is simpler if we temporarily ignore the trailing newline. */ | |
155 | if (len > 0 && line[len - 1] == '\n') { | |
156 | trailing_newline = 1; | |
157 | len--; | |
158 | } | |
b2979ff5 JH |
159 | if ((ws_rule & WS_CR_AT_EOL) && |
160 | len > 0 && line[len - 1] == '\r') { | |
161 | trailing_carriage_return = 1; | |
162 | len--; | |
163 | } | |
c1795bb0 WC |
164 | |
165 | /* Check for trailing whitespace. */ | |
aeb84b05 | 166 | if (ws_rule & WS_BLANK_AT_EOL) { |
c1795bb0 WC |
167 | for (i = len - 1; i >= 0; i--) { |
168 | if (isspace(line[i])) { | |
169 | trailing_whitespace = i; | |
aeb84b05 | 170 | result |= WS_BLANK_AT_EOL; |
c1795bb0 WC |
171 | } |
172 | else | |
173 | break; | |
174 | } | |
175 | } | |
176 | ||
3e3ec2ab | 177 | /* Check indentation */ |
c1795bb0 | 178 | for (i = 0; i < len; i++) { |
9afa2d4a | 179 | if (line[i] == ' ') |
1020999a | 180 | continue; |
1020999a | 181 | if (line[i] != '\t') |
c1795bb0 | 182 | break; |
ffe56885 | 183 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) { |
1020999a | 184 | result |= WS_SPACE_BEFORE_TAB; |
ffe56885 BF |
185 | if (stream) { |
186 | fputs(ws, stream); | |
187 | fwrite(line + written, i - written, 1, stream); | |
188 | fputs(reset, stream); | |
3e3ec2ab | 189 | fwrite(line + i, 1, 1, stream); |
ffe56885 | 190 | } |
3e3ec2ab CW |
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 | } | |
9afa2d4a | 202 | written = i + 1; |
c1795bb0 WC |
203 | } |
204 | ||
205 | /* Check for indent using non-tab. */ | |
ffe56885 | 206 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) { |
c1795bb0 | 207 | result |= WS_INDENT_WITH_NON_TAB; |
ffe56885 | 208 | if (stream) { |
c1795bb0 | 209 | fputs(ws, stream); |
ffe56885 | 210 | fwrite(line + written, i - written, 1, stream); |
c1795bb0 | 211 | fputs(reset, stream); |
c1795bb0 | 212 | } |
ffe56885 BF |
213 | written = i; |
214 | } | |
c1795bb0 | 215 | |
ffe56885 | 216 | if (stream) { |
b2979ff5 JH |
217 | /* |
218 | * Now the rest of the line starts at "written". | |
219 | * The non-highlighted part ends at "trailing_whitespace". | |
220 | */ | |
c1795bb0 WC |
221 | if (trailing_whitespace == -1) |
222 | trailing_whitespace = len; | |
223 | ||
224 | /* Emit non-highlighted (middle) segment. */ | |
954ecd43 | 225 | if (trailing_whitespace - written > 0) { |
c1795bb0 | 226 | fputs(set, stream); |
954ecd43 BF |
227 | fwrite(line + written, |
228 | trailing_whitespace - written, 1, stream); | |
c1795bb0 WC |
229 | fputs(reset, stream); |
230 | } | |
231 | ||
232 | /* Highlight errors in trailing whitespace. */ | |
233 | if (trailing_whitespace != len) { | |
234 | fputs(ws, stream); | |
235 | fwrite(line + trailing_whitespace, | |
236 | len - trailing_whitespace, 1, stream); | |
237 | fputs(reset, stream); | |
238 | } | |
b2979ff5 JH |
239 | if (trailing_carriage_return) |
240 | fputc('\r', stream); | |
c1795bb0 WC |
241 | if (trailing_newline) |
242 | fputc('\n', stream); | |
243 | } | |
244 | return result; | |
245 | } | |
fe3403c3 | 246 | |
8f8841e9 JH |
247 | void ws_check_emit(const char *line, int len, unsigned ws_rule, |
248 | FILE *stream, const char *set, | |
249 | const char *reset, const char *ws) | |
250 | { | |
251 | (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws); | |
252 | } | |
253 | ||
254 | unsigned ws_check(const char *line, int len, unsigned ws_rule) | |
255 | { | |
256 | return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL); | |
257 | } | |
258 | ||
877f23cc JH |
259 | int ws_blank_line(const char *line, int len, unsigned ws_rule) |
260 | { | |
261 | /* | |
262 | * We _might_ want to treat CR differently from other | |
263 | * whitespace characters when ws_rule has WS_CR_AT_EOL, but | |
264 | * for now we just use this stupid definition. | |
265 | */ | |
266 | while (len-- > 0) { | |
267 | if (!isspace(*line)) | |
268 | return 0; | |
269 | line++; | |
270 | } | |
271 | return 1; | |
272 | } | |
273 | ||
d511bd33 CW |
274 | /* Copy the line onto the end of the strbuf while fixing whitespaces */ |
275 | void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count) | |
fe3403c3 JH |
276 | { |
277 | /* | |
278 | * len is number of bytes to be copied from src, starting | |
279 | * at src. Typically src[len-1] is '\n', unless this is | |
280 | * the incomplete last line. | |
281 | */ | |
282 | int i; | |
283 | int add_nl_to_tail = 0; | |
284 | int add_cr_to_tail = 0; | |
285 | int fixed = 0; | |
286 | int last_tab_in_indent = -1; | |
287 | int last_space_in_indent = -1; | |
288 | int need_fix_leading_space = 0; | |
fe3403c3 JH |
289 | |
290 | /* | |
291 | * Strip trailing whitespace | |
292 | */ | |
afd9db41 | 293 | if (ws_rule & WS_BLANK_AT_EOL) { |
422a82f2 | 294 | if (0 < len && src[len - 1] == '\n') { |
fe3403c3 JH |
295 | add_nl_to_tail = 1; |
296 | len--; | |
422a82f2 | 297 | if (0 < len && src[len - 1] == '\r') { |
fe3403c3 JH |
298 | add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL); |
299 | len--; | |
300 | } | |
301 | } | |
302 | if (0 < len && isspace(src[len - 1])) { | |
303 | while (0 < len && isspace(src[len-1])) | |
304 | len--; | |
305 | fixed = 1; | |
306 | } | |
307 | } | |
308 | ||
309 | /* | |
310 | * Check leading whitespaces (indent) | |
311 | */ | |
312 | for (i = 0; i < len; i++) { | |
313 | char ch = src[i]; | |
314 | if (ch == '\t') { | |
315 | last_tab_in_indent = i; | |
316 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && | |
317 | 0 <= last_space_in_indent) | |
318 | need_fix_leading_space = 1; | |
319 | } else if (ch == ' ') { | |
320 | last_space_in_indent = i; | |
321 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && | |
322 | 8 <= i - last_tab_in_indent) | |
323 | need_fix_leading_space = 1; | |
324 | } else | |
325 | break; | |
326 | } | |
327 | ||
fe3403c3 JH |
328 | if (need_fix_leading_space) { |
329 | /* Process indent ourselves */ | |
330 | int consecutive_spaces = 0; | |
331 | int last = last_tab_in_indent + 1; | |
332 | ||
333 | if (ws_rule & WS_INDENT_WITH_NON_TAB) { | |
334 | /* have "last" point at one past the indent */ | |
335 | if (last_tab_in_indent < last_space_in_indent) | |
336 | last = last_space_in_indent + 1; | |
337 | else | |
338 | last = last_tab_in_indent + 1; | |
339 | } | |
340 | ||
341 | /* | |
342 | * between src[0..last-1], strip the funny spaces, | |
343 | * updating them to tab as needed. | |
344 | */ | |
345 | for (i = 0; i < last; i++) { | |
346 | char ch = src[i]; | |
347 | if (ch != ' ') { | |
348 | consecutive_spaces = 0; | |
d511bd33 | 349 | strbuf_addch(dst, ch); |
fe3403c3 JH |
350 | } else { |
351 | consecutive_spaces++; | |
352 | if (consecutive_spaces == 8) { | |
d511bd33 | 353 | strbuf_addch(dst, '\t'); |
fe3403c3 JH |
354 | consecutive_spaces = 0; |
355 | } | |
356 | } | |
357 | } | |
358 | while (0 < consecutive_spaces--) | |
d511bd33 | 359 | strbuf_addch(dst, ' '); |
fe3403c3 JH |
360 | len -= last; |
361 | src += last; | |
362 | fixed = 1; | |
4e35c51e CW |
363 | } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) { |
364 | /* Expand tabs into spaces */ | |
365 | int last = last_tab_in_indent + 1; | |
366 | for (i = 0; i < last; i++) { | |
367 | if (src[i] == '\t') | |
368 | do { | |
369 | strbuf_addch(dst, ' '); | |
370 | } while (dst->len % 8); | |
371 | else | |
372 | strbuf_addch(dst, src[i]); | |
373 | } | |
374 | len -= last; | |
375 | src += last; | |
376 | fixed = 1; | |
fe3403c3 JH |
377 | } |
378 | ||
d511bd33 | 379 | strbuf_add(dst, src, len); |
fe3403c3 | 380 | if (add_cr_to_tail) |
d511bd33 | 381 | strbuf_addch(dst, '\r'); |
fe3403c3 | 382 | if (add_nl_to_tail) |
d511bd33 | 383 | strbuf_addch(dst, '\n'); |
fe3403c3 JH |
384 | if (fixed && error_count) |
385 | (*error_count)++; | |
fe3403c3 | 386 | } |