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