Git 1.9.0
[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");
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 }
f4b05a49
JS
59 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 }
cf1b7869
JH
69 string = ep;
70 }
3e3ec2ab
CW
71
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");
cf1b7869
JH
74 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)
7fb0eaa2 82 attr_whitespace = git_attr("whitespace");
cf1b7869
JH
83 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);
d932f4eb 91 if (!git_check_attr(pathname, 1, &attr_whitespace_rule)) {
cf1b7869
JH
92 const char *value;
93
94 value = attr_whitespace_rule.value;
95 if (ATTR_TRUE(value)) {
96 /* true (whitespace) */
f4b05a49 97 unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
cf1b7869
JH
98 int i;
99 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
727c3718
JH
100 if (!whitespace_rule_names[i].loosens_error &&
101 !whitespace_rule_names[i].exclude_default)
a437900f 102 all_rule |= whitespace_rule_names[i].rule_bits;
cf1b7869
JH
103 return all_rule;
104 } else if (ATTR_FALSE(value)) {
105 /* false (-whitespace) */
f4b05a49 106 return ws_tab_width(whitespace_rule_cfg);
cf1b7869
JH
107 } 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}
c1795bb0
WC
118
119/* The returned string should be freed by the caller. */
120char *whitespace_error_string(unsigned ws)
121{
f285a2d7 122 struct strbuf err = STRBUF_INIT;
aeb84b05 123 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
420f4f04 124 strbuf_addstr(&err, "trailing whitespace");
aeb84b05
JH
125 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 }
c1795bb0
WC
134 if (ws & WS_SPACE_BEFORE_TAB) {
135 if (err.len)
136 strbuf_addstr(&err, ", ");
420f4f04 137 strbuf_addstr(&err, "space before tab in indent");
c1795bb0
WC
138 }
139 if (ws & WS_INDENT_WITH_NON_TAB) {
140 if (err.len)
141 strbuf_addstr(&err, ", ");
420f4f04 142 strbuf_addstr(&err, "indent with spaces");
c1795bb0 143 }
3e3ec2ab
CW
144 if (ws & WS_TAB_IN_INDENT) {
145 if (err.len)
146 strbuf_addstr(&err, ", ");
147 strbuf_addstr(&err, "tab in indent");
148 }
c1795bb0
WC
149 return strbuf_detach(&err, NULL);
150}
151
152/* If stream is non-NULL, emits the line after checking. */
8f8841e9
JH
153static 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)
c1795bb0
WC
156{
157 unsigned result = 0;
954ecd43 158 int written = 0;
c1795bb0
WC
159 int trailing_whitespace = -1;
160 int trailing_newline = 0;
b2979ff5 161 int trailing_carriage_return = 0;
c1795bb0
WC
162 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 }
b2979ff5
JH
169 if ((ws_rule & WS_CR_AT_EOL) &&
170 len > 0 && line[len - 1] == '\r') {
171 trailing_carriage_return = 1;
172 len--;
173 }
c1795bb0
WC
174
175 /* Check for trailing whitespace. */
aeb84b05 176 if (ws_rule & WS_BLANK_AT_EOL) {
c1795bb0
WC
177 for (i = len - 1; i >= 0; i--) {
178 if (isspace(line[i])) {
179 trailing_whitespace = i;
aeb84b05 180 result |= WS_BLANK_AT_EOL;
c1795bb0
WC
181 }
182 else
183 break;
184 }
185 }
186
cfd1a984
KB
187 if (trailing_whitespace == -1)
188 trailing_whitespace = len;
189
3e3ec2ab 190 /* Check indentation */
cfd1a984 191 for (i = 0; i < trailing_whitespace; i++) {
9afa2d4a 192 if (line[i] == ' ')
1020999a 193 continue;
1020999a 194 if (line[i] != '\t')
c1795bb0 195 break;
ffe56885 196 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
1020999a 197 result |= WS_SPACE_BEFORE_TAB;
ffe56885
BF
198 if (stream) {
199 fputs(ws, stream);
200 fwrite(line + written, i - written, 1, stream);
201 fputs(reset, stream);
3e3ec2ab 202 fwrite(line + i, 1, 1, stream);
ffe56885 203 }
3e3ec2ab
CW
204 } 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 }
9afa2d4a 215 written = i + 1;
c1795bb0
WC
216 }
217
218 /* Check for indent using non-tab. */
f4b05a49 219 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
c1795bb0 220 result |= WS_INDENT_WITH_NON_TAB;
ffe56885 221 if (stream) {
c1795bb0 222 fputs(ws, stream);
ffe56885 223 fwrite(line + written, i - written, 1, stream);
c1795bb0 224 fputs(reset, stream);
c1795bb0 225 }
ffe56885
BF
226 written = i;
227 }
c1795bb0 228
ffe56885 229 if (stream) {
b2979ff5
JH
230 /*
231 * Now the rest of the line starts at "written".
232 * The non-highlighted part ends at "trailing_whitespace".
233 */
c1795bb0
WC
234
235 /* Emit non-highlighted (middle) segment. */
954ecd43 236 if (trailing_whitespace - written > 0) {
c1795bb0 237 fputs(set, stream);
954ecd43
BF
238 fwrite(line + written,
239 trailing_whitespace - written, 1, stream);
c1795bb0
WC
240 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 }
b2979ff5
JH
250 if (trailing_carriage_return)
251 fputc('\r', stream);
c1795bb0
WC
252 if (trailing_newline)
253 fputc('\n', stream);
254 }
255 return result;
256}
fe3403c3 257
8f8841e9
JH
258void 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
877f23cc
JH
270int 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
d511bd33
CW
285/* 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)
fe3403c3
JH
287{
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;
fe3403c3
JH
300
301 /*
302 * Strip trailing whitespace
303 */
afd9db41 304 if (ws_rule & WS_BLANK_AT_EOL) {
422a82f2 305 if (0 < len && src[len - 1] == '\n') {
fe3403c3
JH
306 add_nl_to_tail = 1;
307 len--;
422a82f2 308 if (0 < len && src[len - 1] == '\r') {
fe3403c3
JH
309 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) &&
f4b05a49 333 ws_tab_width(ws_rule) <= i - last_tab_in_indent)
fe3403c3
JH
334 need_fix_leading_space = 1;
335 } else
336 break;
337 }
338
fe3403c3
JH
339 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;
d511bd33 360 strbuf_addch(dst, ch);
fe3403c3
JH
361 } else {
362 consecutive_spaces++;
f4b05a49 363 if (consecutive_spaces == ws_tab_width(ws_rule)) {
d511bd33 364 strbuf_addch(dst, '\t');
fe3403c3
JH
365 consecutive_spaces = 0;
366 }
367 }
368 }
369 while (0 < consecutive_spaces--)
d511bd33 370 strbuf_addch(dst, ' ');
fe3403c3
JH
371 len -= last;
372 src += last;
373 fixed = 1;
4e35c51e
CW
374 } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
375 /* Expand tabs into spaces */
d35711ad 376 int start = dst->len;
4e35c51e
CW
377 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, ' ');
f4b05a49 382 } while ((dst->len - start) % ws_tab_width(ws_rule));
4e35c51e
CW
383 else
384 strbuf_addch(dst, src[i]);
385 }
386 len -= last;
387 src += last;
388 fixed = 1;
fe3403c3
JH
389 }
390
d511bd33 391 strbuf_add(dst, src, len);
fe3403c3 392 if (add_cr_to_tail)
d511bd33 393 strbuf_addch(dst, '\r');
fe3403c3 394 if (add_nl_to_tail)
d511bd33 395 strbuf_addch(dst, '\n');
fe3403c3
JH
396 if (fixed && error_count)
397 (*error_count)++;
fe3403c3 398}