revision.c: remove implicit dependency on the_index
[git.git] / ws.c
blob55349b4c5d08597e38ab76d10cd0d11b944f4f37
1 /*
2 * Whitespace rules
4 * Copyright (c) 2007 Junio C Hamano
5 */
6 #include "cache.h"
7 #include "attr.h"
9 static struct whitespace_rule {
10 const char *rule_name;
11 unsigned rule_bits;
12 unsigned loosens_error:1,
13 exclude_default:1;
14 } whitespace_rule_names[] = {
15 { "trailing-space", WS_TRAILING_SPACE, 0 },
16 { "space-before-tab", WS_SPACE_BEFORE_TAB, 0 },
17 { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB, 0 },
18 { "cr-at-eol", WS_CR_AT_EOL, 1 },
19 { "blank-at-eol", WS_BLANK_AT_EOL, 0 },
20 { "blank-at-eof", WS_BLANK_AT_EOF, 0 },
21 { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 },
24 unsigned parse_whitespace_rule(const char *string)
26 unsigned rule = WS_DEFAULT_RULE;
28 while (string) {
29 int i;
30 size_t len;
31 const char *ep;
32 int negated = 0;
34 string = string + strspn(string, ", \t\n\r");
35 ep = strchrnul(string, ',');
36 len = ep - string;
38 if (*string == '-') {
39 negated = 1;
40 string++;
41 len--;
43 if (!len)
44 break;
45 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
46 if (strncmp(whitespace_rule_names[i].rule_name,
47 string, len))
48 continue;
49 if (negated)
50 rule &= ~whitespace_rule_names[i].rule_bits;
51 else
52 rule |= whitespace_rule_names[i].rule_bits;
53 break;
55 if (strncmp(string, "tabwidth=", 9) == 0) {
56 unsigned tabwidth = atoi(string + 9);
57 if (0 < tabwidth && tabwidth < 0100) {
58 rule &= ~WS_TAB_WIDTH_MASK;
59 rule |= tabwidth;
61 else
62 warning("tabwidth %.*s out of range",
63 (int)(len - 9), string + 9);
65 string = ep;
68 if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
69 die("cannot enforce both tab-in-indent and indent-with-non-tab");
70 return rule;
73 unsigned whitespace_rule(struct index_state *istate, const char *pathname)
75 static struct attr_check *attr_whitespace_rule;
77 if (!attr_whitespace_rule)
78 attr_whitespace_rule = attr_check_initl("whitespace", NULL);
80 if (!git_check_attr(istate, pathname, attr_whitespace_rule)) {
81 const char *value;
83 value = attr_whitespace_rule->items[0].value;
84 if (ATTR_TRUE(value)) {
85 /* true (whitespace) */
86 unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
87 int i;
88 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
89 if (!whitespace_rule_names[i].loosens_error &&
90 !whitespace_rule_names[i].exclude_default)
91 all_rule |= whitespace_rule_names[i].rule_bits;
92 return all_rule;
93 } else if (ATTR_FALSE(value)) {
94 /* false (-whitespace) */
95 return ws_tab_width(whitespace_rule_cfg);
96 } else if (ATTR_UNSET(value)) {
97 /* reset to default (!whitespace) */
98 return whitespace_rule_cfg;
99 } else {
100 /* string */
101 return parse_whitespace_rule(value);
103 } else {
104 return whitespace_rule_cfg;
108 /* The returned string should be freed by the caller. */
109 char *whitespace_error_string(unsigned ws)
111 struct strbuf err = STRBUF_INIT;
112 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
113 strbuf_addstr(&err, "trailing whitespace");
114 else {
115 if (ws & WS_BLANK_AT_EOL)
116 strbuf_addstr(&err, "trailing whitespace");
117 if (ws & WS_BLANK_AT_EOF) {
118 if (err.len)
119 strbuf_addstr(&err, ", ");
120 strbuf_addstr(&err, "new blank line at EOF");
123 if (ws & WS_SPACE_BEFORE_TAB) {
124 if (err.len)
125 strbuf_addstr(&err, ", ");
126 strbuf_addstr(&err, "space before tab in indent");
128 if (ws & WS_INDENT_WITH_NON_TAB) {
129 if (err.len)
130 strbuf_addstr(&err, ", ");
131 strbuf_addstr(&err, "indent with spaces");
133 if (ws & WS_TAB_IN_INDENT) {
134 if (err.len)
135 strbuf_addstr(&err, ", ");
136 strbuf_addstr(&err, "tab in indent");
138 return strbuf_detach(&err, NULL);
141 /* If stream is non-NULL, emits the line after checking. */
142 static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
143 FILE *stream, const char *set,
144 const char *reset, const char *ws)
146 unsigned result = 0;
147 int written = 0;
148 int trailing_whitespace = -1;
149 int trailing_newline = 0;
150 int trailing_carriage_return = 0;
151 int i;
153 /* Logic is simpler if we temporarily ignore the trailing newline. */
154 if (len > 0 && line[len - 1] == '\n') {
155 trailing_newline = 1;
156 len--;
158 if ((ws_rule & WS_CR_AT_EOL) &&
159 len > 0 && line[len - 1] == '\r') {
160 trailing_carriage_return = 1;
161 len--;
164 /* Check for trailing whitespace. */
165 if (ws_rule & WS_BLANK_AT_EOL) {
166 for (i = len - 1; i >= 0; i--) {
167 if (isspace(line[i])) {
168 trailing_whitespace = i;
169 result |= WS_BLANK_AT_EOL;
171 else
172 break;
176 if (trailing_whitespace == -1)
177 trailing_whitespace = len;
179 /* Check indentation */
180 for (i = 0; i < trailing_whitespace; i++) {
181 if (line[i] == ' ')
182 continue;
183 if (line[i] != '\t')
184 break;
185 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
186 result |= WS_SPACE_BEFORE_TAB;
187 if (stream) {
188 fputs(ws, stream);
189 fwrite(line + written, i - written, 1, stream);
190 fputs(reset, stream);
191 fwrite(line + i, 1, 1, stream);
193 } else if (ws_rule & WS_TAB_IN_INDENT) {
194 result |= WS_TAB_IN_INDENT;
195 if (stream) {
196 fwrite(line + written, i - written, 1, stream);
197 fputs(ws, stream);
198 fwrite(line + i, 1, 1, stream);
199 fputs(reset, stream);
201 } else if (stream) {
202 fwrite(line + written, i - written + 1, 1, stream);
204 written = i + 1;
207 /* Check for indent using non-tab. */
208 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
209 result |= WS_INDENT_WITH_NON_TAB;
210 if (stream) {
211 fputs(ws, stream);
212 fwrite(line + written, i - written, 1, stream);
213 fputs(reset, stream);
215 written = i;
218 if (stream) {
220 * Now the rest of the line starts at "written".
221 * The non-highlighted part ends at "trailing_whitespace".
224 /* Emit non-highlighted (middle) segment. */
225 if (trailing_whitespace - written > 0) {
226 fputs(set, stream);
227 fwrite(line + written,
228 trailing_whitespace - written, 1, stream);
229 fputs(reset, stream);
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);
239 if (trailing_carriage_return)
240 fputc('\r', stream);
241 if (trailing_newline)
242 fputc('\n', stream);
244 return result;
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)
251 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
254 unsigned ws_check(const char *line, int len, unsigned ws_rule)
256 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
259 int ws_blank_line(const char *line, int len, unsigned ws_rule)
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.
266 while (len-- > 0) {
267 if (!isspace(*line))
268 return 0;
269 line++;
271 return 1;
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)
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.
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;
291 * Strip trailing whitespace
293 if (ws_rule & WS_BLANK_AT_EOL) {
294 if (0 < len && src[len - 1] == '\n') {
295 add_nl_to_tail = 1;
296 len--;
297 if (0 < len && src[len - 1] == '\r') {
298 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
299 len--;
302 if (0 < len && isspace(src[len - 1])) {
303 while (0 < len && isspace(src[len-1]))
304 len--;
305 fixed = 1;
310 * Check leading whitespaces (indent)
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 ws_tab_width(ws_rule) <= i - last_tab_in_indent)
323 need_fix_leading_space = 1;
324 } else
325 break;
328 if (need_fix_leading_space) {
329 /* Process indent ourselves */
330 int consecutive_spaces = 0;
331 int last = last_tab_in_indent + 1;
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;
342 * between src[0..last-1], strip the funny spaces,
343 * updating them to tab as needed.
345 for (i = 0; i < last; i++) {
346 char ch = src[i];
347 if (ch != ' ') {
348 consecutive_spaces = 0;
349 strbuf_addch(dst, ch);
350 } else {
351 consecutive_spaces++;
352 if (consecutive_spaces == ws_tab_width(ws_rule)) {
353 strbuf_addch(dst, '\t');
354 consecutive_spaces = 0;
358 while (0 < consecutive_spaces--)
359 strbuf_addch(dst, ' ');
360 len -= last;
361 src += last;
362 fixed = 1;
363 } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
364 /* Expand tabs into spaces */
365 int start = dst->len;
366 int last = last_tab_in_indent + 1;
367 for (i = 0; i < last; i++) {
368 if (src[i] == '\t')
369 do {
370 strbuf_addch(dst, ' ');
371 } while ((dst->len - start) % ws_tab_width(ws_rule));
372 else
373 strbuf_addch(dst, src[i]);
375 len -= last;
376 src += last;
377 fixed = 1;
380 strbuf_add(dst, src, len);
381 if (add_cr_to_tail)
382 strbuf_addch(dst, '\r');
383 if (add_nl_to_tail)
384 strbuf_addch(dst, '\n');
385 if (fixed && error_count)
386 (*error_count)++;