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