4 * Copyright (c) 2007 Junio C Hamano
10 static struct whitespace_rule {
11 const char *rule_name;
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 },
17 { "cr-at-eol", WS_CR_AT_EOL },
18 { "blank-at-eol", WS_BLANK_AT_EOL },
19 { "blank-at-eof", WS_BLANK_AT_EOF },
22 unsigned parse_whitespace_rule(const char *string)
24 unsigned rule = WS_DEFAULT_RULE;
32 string = string + strspn(string, ", \t\n\r");
33 ep = strchr(string, ',');
46 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
47 if (strncmp(whitespace_rule_names[i].rule_name,
51 rule &= ~whitespace_rule_names[i].rule_bits;
53 rule |= whitespace_rule_names[i].rule_bits;
61 static void setup_whitespace_attr_check(struct git_attr_check *check)
63 static struct git_attr *attr_whitespace;
66 attr_whitespace = git_attr("whitespace", 10);
67 check[0].attr = attr_whitespace;
70 unsigned whitespace_rule(const char *pathname)
72 struct git_attr_check attr_whitespace_rule;
74 setup_whitespace_attr_check(&attr_whitespace_rule);
75 if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
78 value = attr_whitespace_rule.value;
79 if (ATTR_TRUE(value)) {
80 /* true (whitespace) */
81 unsigned all_rule = 0;
83 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
84 all_rule |= whitespace_rule_names[i].rule_bits;
86 } else if (ATTR_FALSE(value)) {
87 /* false (-whitespace) */
89 } else if (ATTR_UNSET(value)) {
90 /* reset to default (!whitespace) */
91 return whitespace_rule_cfg;
94 return parse_whitespace_rule(value);
97 return whitespace_rule_cfg;
101 /* The returned string should be freed by the caller. */
102 char *whitespace_error_string(unsigned ws)
106 strbuf_init(&err, 0);
107 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
108 strbuf_addstr(&err, "trailing whitespace");
110 if (ws & WS_BLANK_AT_EOL)
111 strbuf_addstr(&err, "trailing whitespace");
112 if (ws & WS_BLANK_AT_EOF) {
114 strbuf_addstr(&err, ", ");
115 strbuf_addstr(&err, "new blank line at EOF");
118 if (ws & WS_SPACE_BEFORE_TAB) {
120 strbuf_addstr(&err, ", ");
121 strbuf_addstr(&err, "space before tab in indent");
123 if (ws & WS_INDENT_WITH_NON_TAB) {
125 strbuf_addstr(&err, ", ");
126 strbuf_addstr(&err, "indent with spaces");
128 return strbuf_detach(&err, NULL);
131 /* If stream is non-NULL, emits the line after checking. */
132 static 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)
138 int trailing_whitespace = -1;
139 int trailing_newline = 0;
140 int trailing_carriage_return = 0;
143 /* Logic is simpler if we temporarily ignore the trailing newline. */
144 if (len > 0 && line[len - 1] == '\n') {
145 trailing_newline = 1;
148 if ((ws_rule & WS_CR_AT_EOL) &&
149 len > 0 && line[len - 1] == '\r') {
150 trailing_carriage_return = 1;
154 /* Check for trailing whitespace. */
155 if (ws_rule & WS_BLANK_AT_EOL) {
156 for (i = len - 1; i >= 0; i--) {
157 if (isspace(line[i])) {
158 trailing_whitespace = i;
159 result |= WS_BLANK_AT_EOL;
166 /* Check for space before tab in initial indent. */
167 for (i = 0; i < len; i++) {
172 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
173 result |= WS_SPACE_BEFORE_TAB;
176 fwrite(line + written, i - written, 1, stream);
177 fputs(reset, stream);
180 fwrite(line + written, i - written, 1, stream);
182 fwrite(line + i, 1, 1, stream);
186 /* Check for indent using non-tab. */
187 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
188 result |= WS_INDENT_WITH_NON_TAB;
191 fwrite(line + written, i - written, 1, stream);
192 fputs(reset, stream);
199 * Now the rest of the line starts at "written".
200 * The non-highlighted part ends at "trailing_whitespace".
202 if (trailing_whitespace == -1)
203 trailing_whitespace = len;
205 /* Emit non-highlighted (middle) segment. */
206 if (trailing_whitespace - written > 0) {
208 fwrite(line + written,
209 trailing_whitespace - written, 1, stream);
210 fputs(reset, stream);
213 /* Highlight errors in trailing whitespace. */
214 if (trailing_whitespace != len) {
216 fwrite(line + trailing_whitespace,
217 len - trailing_whitespace, 1, stream);
218 fputs(reset, stream);
220 if (trailing_carriage_return)
222 if (trailing_newline)
228 void ws_check_emit(const char *line, int len, unsigned ws_rule,
229 FILE *stream, const char *set,
230 const char *reset, const char *ws)
232 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
235 unsigned ws_check(const char *line, int len, unsigned ws_rule)
237 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
240 int ws_blank_line(const char *line, int len, unsigned ws_rule)
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.
255 /* Copy the line to the buffer while fixing whitespaces */
256 int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
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.
264 int add_nl_to_tail = 0;
265 int add_cr_to_tail = 0;
267 int last_tab_in_indent = -1;
268 int last_space_in_indent = -1;
269 int need_fix_leading_space = 0;
273 * Strip trailing whitespace
275 if ((ws_rule & WS_BLANK_AT_EOL) &&
276 (2 <= len && isspace(src[len-2]))) {
277 if (src[len - 1] == '\n') {
280 if (1 < len && src[len - 1] == '\r') {
281 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
285 if (0 < len && isspace(src[len - 1])) {
286 while (0 < len && isspace(src[len-1]))
293 * Check leading whitespaces (indent)
295 for (i = 0; i < len; i++) {
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;
312 if (need_fix_leading_space) {
313 /* Process indent ourselves */
314 int consecutive_spaces = 0;
315 int last = last_tab_in_indent + 1;
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;
322 last = last_tab_in_indent + 1;
326 * between src[0..last-1], strip the funny spaces,
327 * updating them to tab as needed.
329 for (i = 0; i < last; i++) {
332 consecutive_spaces = 0;
335 consecutive_spaces++;
336 if (consecutive_spaces == 8) {
338 consecutive_spaces = 0;
342 while (0 < consecutive_spaces--)
349 memcpy(dst, src, len);
354 if (fixed && error_count)
356 return dst + len - buf;