Merge branch 'nd/shallow-clone'
[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         { "tab-in-indent", WS_TAB_IN_INDENT, 0, 1 },
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                 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                 }
69                 string = ep;
70         }
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");
74         return rule;
75 }
76
77 static void setup_whitespace_attr_check(struct git_attr_check *check)
78 {
79         static struct git_attr *attr_whitespace;
80
81         if (!attr_whitespace)
82                 attr_whitespace = git_attr("whitespace");
83         check[0].attr = attr_whitespace;
84 }
85
86 unsigned whitespace_rule(const char *pathname)
87 {
88         struct git_attr_check attr_whitespace_rule;
89
90         setup_whitespace_attr_check(&attr_whitespace_rule);
91         if (!git_check_attr(pathname, 1, &attr_whitespace_rule)) {
92                 const char *value;
93
94                 value = attr_whitespace_rule.value;
95                 if (ATTR_TRUE(value)) {
96                         /* true (whitespace) */
97                         unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
98                         int i;
99                         for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
100                                 if (!whitespace_rule_names[i].loosens_error &&
101                                     !whitespace_rule_names[i].exclude_default)
102                                         all_rule |= whitespace_rule_names[i].rule_bits;
103                         return all_rule;
104                 } else if (ATTR_FALSE(value)) {
105                         /* false (-whitespace) */
106                         return ws_tab_width(whitespace_rule_cfg);
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 }
118
119 /* The returned string should be freed by the caller. */
120 char *whitespace_error_string(unsigned ws)
121 {
122         struct strbuf err = STRBUF_INIT;
123         if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
124                 strbuf_addstr(&err, "trailing whitespace");
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         }
134         if (ws & WS_SPACE_BEFORE_TAB) {
135                 if (err.len)
136                         strbuf_addstr(&err, ", ");
137                 strbuf_addstr(&err, "space before tab in indent");
138         }
139         if (ws & WS_INDENT_WITH_NON_TAB) {
140                 if (err.len)
141                         strbuf_addstr(&err, ", ");
142                 strbuf_addstr(&err, "indent with spaces");
143         }
144         if (ws & WS_TAB_IN_INDENT) {
145                 if (err.len)
146                         strbuf_addstr(&err, ", ");
147                 strbuf_addstr(&err, "tab in indent");
148         }
149         return strbuf_detach(&err, NULL);
150 }
151
152 /* If stream is non-NULL, emits the line after checking. */
153 static 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)
156 {
157         unsigned result = 0;
158         int written = 0;
159         int trailing_whitespace = -1;
160         int trailing_newline = 0;
161         int trailing_carriage_return = 0;
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         }
169         if ((ws_rule & WS_CR_AT_EOL) &&
170             len > 0 && line[len - 1] == '\r') {
171                 trailing_carriage_return = 1;
172                 len--;
173         }
174
175         /* Check for trailing whitespace. */
176         if (ws_rule & WS_BLANK_AT_EOL) {
177                 for (i = len - 1; i >= 0; i--) {
178                         if (isspace(line[i])) {
179                                 trailing_whitespace = i;
180                                 result |= WS_BLANK_AT_EOL;
181                         }
182                         else
183                                 break;
184                 }
185         }
186
187         if (trailing_whitespace == -1)
188                 trailing_whitespace = len;
189
190         /* Check indentation */
191         for (i = 0; i < trailing_whitespace; i++) {
192                 if (line[i] == ' ')
193                         continue;
194                 if (line[i] != '\t')
195                         break;
196                 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
197                         result |= WS_SPACE_BEFORE_TAB;
198                         if (stream) {
199                                 fputs(ws, stream);
200                                 fwrite(line + written, i - written, 1, stream);
201                                 fputs(reset, stream);
202                                 fwrite(line + i, 1, 1, stream);
203                         }
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                 }
215                 written = i + 1;
216         }
217
218         /* Check for indent using non-tab. */
219         if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
220                 result |= WS_INDENT_WITH_NON_TAB;
221                 if (stream) {
222                         fputs(ws, stream);
223                         fwrite(line + written, i - written, 1, stream);
224                         fputs(reset, stream);
225                 }
226                 written = i;
227         }
228
229         if (stream) {
230                 /*
231                  * Now the rest of the line starts at "written".
232                  * The non-highlighted part ends at "trailing_whitespace".
233                  */
234
235                 /* Emit non-highlighted (middle) segment. */
236                 if (trailing_whitespace - written > 0) {
237                         fputs(set, stream);
238                         fwrite(line + written,
239                             trailing_whitespace - written, 1, stream);
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                 }
250                 if (trailing_carriage_return)
251                         fputc('\r', stream);
252                 if (trailing_newline)
253                         fputc('\n', stream);
254         }
255         return result;
256 }
257
258 void 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
265 unsigned 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
270 int 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
285 /* Copy the line onto the end of the strbuf while fixing whitespaces */
286 void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count)
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;
300
301         /*
302          * Strip trailing whitespace
303          */
304         if (ws_rule & WS_BLANK_AT_EOL) {
305                 if (0 < len && src[len - 1] == '\n') {
306                         add_nl_to_tail = 1;
307                         len--;
308                         if (0 < len && src[len - 1] == '\r') {
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) &&
333                             ws_tab_width(ws_rule) <= i - last_tab_in_indent)
334                                 need_fix_leading_space = 1;
335                 } else
336                         break;
337         }
338
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;
360                                 strbuf_addch(dst, ch);
361                         } else {
362                                 consecutive_spaces++;
363                                 if (consecutive_spaces == ws_tab_width(ws_rule)) {
364                                         strbuf_addch(dst, '\t');
365                                         consecutive_spaces = 0;
366                                 }
367                         }
368                 }
369                 while (0 < consecutive_spaces--)
370                         strbuf_addch(dst, ' ');
371                 len -= last;
372                 src += last;
373                 fixed = 1;
374         } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
375                 /* Expand tabs into spaces */
376                 int start = dst->len;
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, ' ');
382                                 } while ((dst->len - start) % ws_tab_width(ws_rule));
383                         else
384                                 strbuf_addch(dst, src[i]);
385                 }
386                 len -= last;
387                 src += last;
388                 fixed = 1;
389         }
390
391         strbuf_add(dst, src, len);
392         if (add_cr_to_tail)
393                 strbuf_addch(dst, '\r');
394         if (add_nl_to_tail)
395                 strbuf_addch(dst, '\n');
396         if (fixed && error_count)
397                 (*error_count)++;
398 }