4 * Copyright (c) 2007 Junio C Hamano
10 static struct whitespace_rule {
11 const char *rule_name;
13 unsigned loosens_error: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 },
25 unsigned parse_whitespace_rule(const char *string)
27 unsigned rule = WS_DEFAULT_RULE;
35 string = string + strspn(string, ", \t\n\r");
36 ep = strchrnul(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;
56 if (strncmp(string, "tabwidth=", 9) == 0) {
57 unsigned tabwidth = atoi(string + 9);
58 if (0 < tabwidth && tabwidth < 0100) {
59 rule &= ~WS_TAB_WIDTH_MASK;
63 warning("tabwidth %.*s out of range",
64 (int)(len - 9), string + 9);
69 if (rule & WS_TAB_IN_INDENT && rule & WS_INDENT_WITH_NON_TAB)
70 die("cannot enforce both tab-in-indent and indent-with-non-tab");
74 unsigned whitespace_rule(const char *pathname)
76 static struct attr_check *attr_whitespace_rule;
79 if (!attr_whitespace_rule)
80 attr_whitespace_rule = attr_check_initl("whitespace", NULL);
82 git_check_attr(&the_index, pathname, attr_whitespace_rule);
83 value = attr_whitespace_rule->items[0].value;
84 if (ATTR_TRUE(value)) {
85 /* true (whitespace) */
86 unsigned all_rule = ws_tab_width(whitespace_rule_cfg);
88 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
89 if (!whitespace_rule_names[i].loosens_error &&
90 !whitespace_rule_names[i].exclude_default)
91 all_rule |= whitespace_rule_names[i].rule_bits;
93 } else if (ATTR_FALSE(value)) {
94 /* false (-whitespace) */
95 return ws_tab_width(whitespace_rule_cfg);
96 } else if (ATTR_UNSET(value)) {
97 /* reset to default (!whitespace) */
98 return whitespace_rule_cfg;
101 return parse_whitespace_rule(value);
105 /* The returned string should be freed by the caller. */
106 char *whitespace_error_string(unsigned ws)
108 struct strbuf err = STRBUF_INIT;
109 if ((ws & WS_TRAILING_SPACE) == WS_TRAILING_SPACE)
110 strbuf_addstr(&err, "trailing whitespace");
112 if (ws & WS_BLANK_AT_EOL)
113 strbuf_addstr(&err, "trailing whitespace");
114 if (ws & WS_BLANK_AT_EOF) {
116 strbuf_addstr(&err, ", ");
117 strbuf_addstr(&err, "new blank line at EOF");
120 if (ws & WS_SPACE_BEFORE_TAB) {
122 strbuf_addstr(&err, ", ");
123 strbuf_addstr(&err, "space before tab in indent");
125 if (ws & WS_INDENT_WITH_NON_TAB) {
127 strbuf_addstr(&err, ", ");
128 strbuf_addstr(&err, "indent with spaces");
130 if (ws & WS_TAB_IN_INDENT) {
132 strbuf_addstr(&err, ", ");
133 strbuf_addstr(&err, "tab in indent");
135 return strbuf_detach(&err, NULL);
138 /* If stream is non-NULL, emits the line after checking. */
139 static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
140 FILE *stream, const char *set,
141 const char *reset, const char *ws)
145 int trailing_whitespace = -1;
146 int trailing_newline = 0;
147 int trailing_carriage_return = 0;
150 /* Logic is simpler if we temporarily ignore the trailing newline. */
151 if (len > 0 && line[len - 1] == '\n') {
152 trailing_newline = 1;
155 if ((ws_rule & WS_CR_AT_EOL) &&
156 len > 0 && line[len - 1] == '\r') {
157 trailing_carriage_return = 1;
161 /* Check for trailing whitespace. */
162 if (ws_rule & WS_BLANK_AT_EOL) {
163 for (i = len - 1; i >= 0; i--) {
164 if (isspace(line[i])) {
165 trailing_whitespace = i;
166 result |= WS_BLANK_AT_EOL;
173 if (trailing_whitespace == -1)
174 trailing_whitespace = len;
176 /* Check indentation */
177 for (i = 0; i < trailing_whitespace; i++) {
182 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
183 result |= WS_SPACE_BEFORE_TAB;
186 fwrite(line + written, i - written, 1, stream);
187 fputs(reset, stream);
188 fwrite(line + i, 1, 1, stream);
190 } else if (ws_rule & WS_TAB_IN_INDENT) {
191 result |= WS_TAB_IN_INDENT;
193 fwrite(line + written, i - written, 1, stream);
195 fwrite(line + i, 1, 1, stream);
196 fputs(reset, stream);
199 fwrite(line + written, i - written + 1, 1, stream);
204 /* Check for indent using non-tab. */
205 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= ws_tab_width(ws_rule)) {
206 result |= WS_INDENT_WITH_NON_TAB;
209 fwrite(line + written, i - written, 1, stream);
210 fputs(reset, stream);
217 * Now the rest of the line starts at "written".
218 * The non-highlighted part ends at "trailing_whitespace".
221 /* Emit non-highlighted (middle) segment. */
222 if (trailing_whitespace - written > 0) {
224 fwrite(line + written,
225 trailing_whitespace - written, 1, stream);
226 fputs(reset, stream);
229 /* Highlight errors in trailing whitespace. */
230 if (trailing_whitespace != len) {
232 fwrite(line + trailing_whitespace,
233 len - trailing_whitespace, 1, stream);
234 fputs(reset, stream);
236 if (trailing_carriage_return)
238 if (trailing_newline)
244 void ws_check_emit(const char *line, int len, unsigned ws_rule,
245 FILE *stream, const char *set,
246 const char *reset, const char *ws)
248 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
251 unsigned ws_check(const char *line, int len, unsigned ws_rule)
253 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
256 int ws_blank_line(const char *line, int len, unsigned ws_rule)
259 * We _might_ want to treat CR differently from other
260 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
261 * for now we just use this stupid definition.
271 /* Copy the line onto the end of the strbuf while fixing whitespaces */
272 void ws_fix_copy(struct strbuf *dst, const char *src, int len, unsigned ws_rule, int *error_count)
275 * len is number of bytes to be copied from src, starting
276 * at src. Typically src[len-1] is '\n', unless this is
277 * the incomplete last line.
280 int add_nl_to_tail = 0;
281 int add_cr_to_tail = 0;
283 int last_tab_in_indent = -1;
284 int last_space_in_indent = -1;
285 int need_fix_leading_space = 0;
288 * Strip trailing whitespace
290 if (ws_rule & WS_BLANK_AT_EOL) {
291 if (0 < len && src[len - 1] == '\n') {
294 if (0 < len && src[len - 1] == '\r') {
295 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
299 if (0 < len && isspace(src[len - 1])) {
300 while (0 < len && isspace(src[len-1]))
307 * Check leading whitespaces (indent)
309 for (i = 0; i < len; i++) {
312 last_tab_in_indent = i;
313 if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
314 0 <= last_space_in_indent)
315 need_fix_leading_space = 1;
316 } else if (ch == ' ') {
317 last_space_in_indent = i;
318 if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
319 ws_tab_width(ws_rule) <= i - last_tab_in_indent)
320 need_fix_leading_space = 1;
325 if (need_fix_leading_space) {
326 /* Process indent ourselves */
327 int consecutive_spaces = 0;
328 int last = last_tab_in_indent + 1;
330 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
331 /* have "last" point at one past the indent */
332 if (last_tab_in_indent < last_space_in_indent)
333 last = last_space_in_indent + 1;
335 last = last_tab_in_indent + 1;
339 * between src[0..last-1], strip the funny spaces,
340 * updating them to tab as needed.
342 for (i = 0; i < last; i++) {
345 consecutive_spaces = 0;
346 strbuf_addch(dst, ch);
348 consecutive_spaces++;
349 if (consecutive_spaces == ws_tab_width(ws_rule)) {
350 strbuf_addch(dst, '\t');
351 consecutive_spaces = 0;
355 while (0 < consecutive_spaces--)
356 strbuf_addch(dst, ' ');
360 } else if ((ws_rule & WS_TAB_IN_INDENT) && last_tab_in_indent >= 0) {
361 /* Expand tabs into spaces */
362 int start = dst->len;
363 int last = last_tab_in_indent + 1;
364 for (i = 0; i < last; i++) {
367 strbuf_addch(dst, ' ');
368 } while ((dst->len - start) % ws_tab_width(ws_rule));
370 strbuf_addch(dst, src[i]);
377 strbuf_add(dst, src, len);
379 strbuf_addch(dst, '\r');
381 strbuf_addch(dst, '\n');
382 if (fixed && error_count)