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