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-eof", WS_BLANK_AT_EOF },
21 unsigned parse_whitespace_rule(const char *string)
23 unsigned rule = WS_DEFAULT_RULE;
31 string = string + strspn(string, ", \t\n\r");
32 ep = strchr(string, ',');
45 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
46 if (strncmp(whitespace_rule_names[i].rule_name,
50 rule &= ~whitespace_rule_names[i].rule_bits;
52 rule |= whitespace_rule_names[i].rule_bits;
60 static void setup_whitespace_attr_check(struct git_attr_check *check)
62 static struct git_attr *attr_whitespace;
65 attr_whitespace = git_attr("whitespace", 10);
66 check[0].attr = attr_whitespace;
69 unsigned whitespace_rule(const char *pathname)
71 struct git_attr_check attr_whitespace_rule;
73 setup_whitespace_attr_check(&attr_whitespace_rule);
74 if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
77 value = attr_whitespace_rule.value;
78 if (ATTR_TRUE(value)) {
79 /* true (whitespace) */
80 unsigned all_rule = 0;
82 for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
83 all_rule |= whitespace_rule_names[i].rule_bits;
85 } else if (ATTR_FALSE(value)) {
86 /* false (-whitespace) */
88 } else if (ATTR_UNSET(value)) {
89 /* reset to default (!whitespace) */
90 return whitespace_rule_cfg;
93 return parse_whitespace_rule(value);
96 return whitespace_rule_cfg;
100 /* The returned string should be freed by the caller. */
101 char *whitespace_error_string(unsigned ws)
104 strbuf_init(&err, 0);
105 if (ws & WS_TRAILING_SPACE)
106 strbuf_addstr(&err, "trailing whitespace");
107 if (ws & WS_SPACE_BEFORE_TAB) {
109 strbuf_addstr(&err, ", ");
110 strbuf_addstr(&err, "space before tab in indent");
112 if (ws & WS_INDENT_WITH_NON_TAB) {
114 strbuf_addstr(&err, ", ");
115 strbuf_addstr(&err, "indent with spaces");
117 if (ws & WS_BLANK_AT_EOF) {
119 strbuf_addstr(&err, ", ");
120 strbuf_addstr(&err, "new blank line at EOF");
122 return strbuf_detach(&err, NULL);
125 /* If stream is non-NULL, emits the line after checking. */
126 static unsigned ws_check_emit_1(const char *line, int len, unsigned ws_rule,
127 FILE *stream, const char *set,
128 const char *reset, const char *ws)
132 int trailing_whitespace = -1;
133 int trailing_newline = 0;
134 int trailing_carriage_return = 0;
137 /* Logic is simpler if we temporarily ignore the trailing newline. */
138 if (len > 0 && line[len - 1] == '\n') {
139 trailing_newline = 1;
142 if ((ws_rule & WS_CR_AT_EOL) &&
143 len > 0 && line[len - 1] == '\r') {
144 trailing_carriage_return = 1;
148 /* Check for trailing whitespace. */
149 if (ws_rule & WS_TRAILING_SPACE) {
150 for (i = len - 1; i >= 0; i--) {
151 if (isspace(line[i])) {
152 trailing_whitespace = i;
153 result |= WS_TRAILING_SPACE;
160 /* Check for space before tab in initial indent. */
161 for (i = 0; i < len; i++) {
166 if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) {
167 result |= WS_SPACE_BEFORE_TAB;
170 fwrite(line + written, i - written, 1, stream);
171 fputs(reset, stream);
174 fwrite(line + written, i - written, 1, stream);
176 fwrite(line + i, 1, 1, stream);
180 /* Check for indent using non-tab. */
181 if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) {
182 result |= WS_INDENT_WITH_NON_TAB;
185 fwrite(line + written, i - written, 1, stream);
186 fputs(reset, stream);
193 * Now the rest of the line starts at "written".
194 * The non-highlighted part ends at "trailing_whitespace".
196 if (trailing_whitespace == -1)
197 trailing_whitespace = len;
199 /* Emit non-highlighted (middle) segment. */
200 if (trailing_whitespace - written > 0) {
202 fwrite(line + written,
203 trailing_whitespace - written, 1, stream);
204 fputs(reset, stream);
207 /* Highlight errors in trailing whitespace. */
208 if (trailing_whitespace != len) {
210 fwrite(line + trailing_whitespace,
211 len - trailing_whitespace, 1, stream);
212 fputs(reset, stream);
214 if (trailing_carriage_return)
216 if (trailing_newline)
222 void ws_check_emit(const char *line, int len, unsigned ws_rule,
223 FILE *stream, const char *set,
224 const char *reset, const char *ws)
226 (void)ws_check_emit_1(line, len, ws_rule, stream, set, reset, ws);
229 unsigned ws_check(const char *line, int len, unsigned ws_rule)
231 return ws_check_emit_1(line, len, ws_rule, NULL, NULL, NULL, NULL);
234 int ws_blank_line(const char *line, int len, unsigned ws_rule)
237 * We _might_ want to treat CR differently from other
238 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
239 * for now we just use this stupid definition.
249 /* Copy the line to the buffer while fixing whitespaces */
250 int ws_fix_copy(char *dst, const char *src, int len, unsigned ws_rule, int *error_count)
253 * len is number of bytes to be copied from src, starting
254 * at src. Typically src[len-1] is '\n', unless this is
255 * the incomplete last line.
258 int add_nl_to_tail = 0;
259 int add_cr_to_tail = 0;
261 int last_tab_in_indent = -1;
262 int last_space_in_indent = -1;
263 int need_fix_leading_space = 0;
267 * Strip trailing whitespace
269 if ((ws_rule & WS_TRAILING_SPACE) &&
270 (2 <= len && isspace(src[len-2]))) {
271 if (src[len - 1] == '\n') {
274 if (1 < len && src[len - 1] == '\r') {
275 add_cr_to_tail = !!(ws_rule & WS_CR_AT_EOL);
279 if (0 < len && isspace(src[len - 1])) {
280 while (0 < len && isspace(src[len-1]))
287 * Check leading whitespaces (indent)
289 for (i = 0; i < len; i++) {
292 last_tab_in_indent = i;
293 if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
294 0 <= last_space_in_indent)
295 need_fix_leading_space = 1;
296 } else if (ch == ' ') {
297 last_space_in_indent = i;
298 if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
299 8 <= i - last_tab_in_indent)
300 need_fix_leading_space = 1;
306 if (need_fix_leading_space) {
307 /* Process indent ourselves */
308 int consecutive_spaces = 0;
309 int last = last_tab_in_indent + 1;
311 if (ws_rule & WS_INDENT_WITH_NON_TAB) {
312 /* have "last" point at one past the indent */
313 if (last_tab_in_indent < last_space_in_indent)
314 last = last_space_in_indent + 1;
316 last = last_tab_in_indent + 1;
320 * between src[0..last-1], strip the funny spaces,
321 * updating them to tab as needed.
323 for (i = 0; i < last; i++) {
326 consecutive_spaces = 0;
329 consecutive_spaces++;
330 if (consecutive_spaces == 8) {
332 consecutive_spaces = 0;
336 while (0 < consecutive_spaces--)
343 memcpy(dst, src, len);
348 if (fixed && error_count)
350 return dst + len - buf;