]>
Commit | Line | Data |
---|---|---|
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 | ||
10 | static struct whitespace_rule { | |
11 | const char *rule_name; | |
12 | unsigned rule_bits; | |
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 }, | |
b2979ff5 | 17 | { "cr-at-eol", WS_CR_AT_EOL }, |
cf1b7869 JH |
18 | }; |
19 | ||
20 | unsigned parse_whitespace_rule(const char *string) | |
21 | { | |
22 | unsigned rule = WS_DEFAULT_RULE; | |
23 | ||
24 | while (string) { | |
25 | int i; | |
26 | size_t len; | |
27 | const char *ep; | |
28 | int negated = 0; | |
29 | ||
30 | string = string + strspn(string, ", \t\n\r"); | |
31 | ep = strchr(string, ','); | |
32 | if (!ep) | |
33 | len = strlen(string); | |
34 | else | |
35 | len = ep - string; | |
36 | ||
37 | if (*string == '-') { | |
38 | negated = 1; | |
39 | string++; | |
40 | len--; | |
41 | } | |
42 | if (!len) | |
43 | break; | |
44 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) { | |
45 | if (strncmp(whitespace_rule_names[i].rule_name, | |
46 | string, len)) | |
47 | continue; | |
48 | if (negated) | |
49 | rule &= ~whitespace_rule_names[i].rule_bits; | |
50 | else | |
51 | rule |= whitespace_rule_names[i].rule_bits; | |
52 | break; | |
53 | } | |
54 | string = ep; | |
55 | } | |
56 | return rule; | |
57 | } | |
58 | ||
59 | static void setup_whitespace_attr_check(struct git_attr_check *check) | |
60 | { | |
61 | static struct git_attr *attr_whitespace; | |
62 | ||
63 | if (!attr_whitespace) | |
64 | attr_whitespace = git_attr("whitespace", 10); | |
65 | check[0].attr = attr_whitespace; | |
66 | } | |
67 | ||
68 | unsigned whitespace_rule(const char *pathname) | |
69 | { | |
70 | struct git_attr_check attr_whitespace_rule; | |
71 | ||
72 | setup_whitespace_attr_check(&attr_whitespace_rule); | |
73 | if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) { | |
74 | const char *value; | |
75 | ||
76 | value = attr_whitespace_rule.value; | |
77 | if (ATTR_TRUE(value)) { | |
78 | /* true (whitespace) */ | |
79 | unsigned all_rule = 0; | |
80 | int i; | |
81 | for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) | |
82 | all_rule |= whitespace_rule_names[i].rule_bits; | |
83 | return all_rule; | |
84 | } else if (ATTR_FALSE(value)) { | |
85 | /* false (-whitespace) */ | |
86 | return 0; | |
87 | } else if (ATTR_UNSET(value)) { | |
88 | /* reset to default (!whitespace) */ | |
89 | return whitespace_rule_cfg; | |
90 | } else { | |
91 | /* string */ | |
92 | return parse_whitespace_rule(value); | |
93 | } | |
94 | } else { | |
95 | return whitespace_rule_cfg; | |
96 | } | |
97 | } | |
c1795bb0 WC |
98 | |
99 | /* The returned string should be freed by the caller. */ | |
100 | char *whitespace_error_string(unsigned ws) | |
101 | { | |
102 | struct strbuf err; | |
103 | strbuf_init(&err, 0); | |
104 | if (ws & WS_TRAILING_SPACE) | |
420f4f04 | 105 | strbuf_addstr(&err, "trailing whitespace"); |
c1795bb0 WC |
106 | if (ws & WS_SPACE_BEFORE_TAB) { |
107 | if (err.len) | |
108 | strbuf_addstr(&err, ", "); | |
420f4f04 | 109 | strbuf_addstr(&err, "space before tab in indent"); |
c1795bb0 WC |
110 | } |
111 | if (ws & WS_INDENT_WITH_NON_TAB) { | |
112 | if (err.len) | |
113 | strbuf_addstr(&err, ", "); | |
420f4f04 | 114 | strbuf_addstr(&err, "indent with spaces"); |
c1795bb0 WC |
115 | } |
116 | return strbuf_detach(&err, NULL); | |
117 | } | |
118 | ||
119 | /* If stream is non-NULL, emits the line after checking. */ | |
120 | unsigned check_and_emit_line(const char *line, int len, unsigned ws_rule, | |
121 | FILE *stream, const char *set, | |
122 | const char *reset, const char *ws) | |
123 | { | |
124 | unsigned result = 0; | |
954ecd43 | 125 | int written = 0; |
c1795bb0 WC |
126 | int trailing_whitespace = -1; |
127 | int trailing_newline = 0; | |
b2979ff5 | 128 | int trailing_carriage_return = 0; |
c1795bb0 WC |
129 | int i; |
130 | ||
131 | /* Logic is simpler if we temporarily ignore the trailing newline. */ | |
132 | if (len > 0 && line[len - 1] == '\n') { | |
133 | trailing_newline = 1; | |
134 | len--; | |
135 | } | |
b2979ff5 JH |
136 | if ((ws_rule & WS_CR_AT_EOL) && |
137 | len > 0 && line[len - 1] == '\r') { | |
138 | trailing_carriage_return = 1; | |
139 | len--; | |
140 | } | |
c1795bb0 WC |
141 | |
142 | /* Check for trailing whitespace. */ | |
143 | if (ws_rule & WS_TRAILING_SPACE) { | |
144 | for (i = len - 1; i >= 0; i--) { | |
145 | if (isspace(line[i])) { | |
146 | trailing_whitespace = i; | |
147 | result |= WS_TRAILING_SPACE; | |
148 | } | |
149 | else | |
150 | break; | |
151 | } | |
152 | } | |
153 | ||
154 | /* Check for space before tab in initial indent. */ | |
155 | for (i = 0; i < len; i++) { | |
9afa2d4a | 156 | if (line[i] == ' ') |
1020999a | 157 | continue; |
1020999a | 158 | if (line[i] != '\t') |
c1795bb0 | 159 | break; |
ffe56885 | 160 | if ((ws_rule & WS_SPACE_BEFORE_TAB) && written < i) { |
1020999a | 161 | result |= WS_SPACE_BEFORE_TAB; |
ffe56885 BF |
162 | if (stream) { |
163 | fputs(ws, stream); | |
164 | fwrite(line + written, i - written, 1, stream); | |
165 | fputs(reset, stream); | |
166 | } | |
167 | } else if (stream) | |
168 | fwrite(line + written, i - written, 1, stream); | |
169 | if (stream) | |
170 | fwrite(line + i, 1, 1, stream); | |
9afa2d4a | 171 | written = i + 1; |
c1795bb0 WC |
172 | } |
173 | ||
174 | /* Check for indent using non-tab. */ | |
ffe56885 | 175 | if ((ws_rule & WS_INDENT_WITH_NON_TAB) && i - written >= 8) { |
c1795bb0 | 176 | result |= WS_INDENT_WITH_NON_TAB; |
ffe56885 | 177 | if (stream) { |
c1795bb0 | 178 | fputs(ws, stream); |
ffe56885 | 179 | fwrite(line + written, i - written, 1, stream); |
c1795bb0 | 180 | fputs(reset, stream); |
c1795bb0 | 181 | } |
ffe56885 BF |
182 | written = i; |
183 | } | |
c1795bb0 | 184 | |
ffe56885 | 185 | if (stream) { |
b2979ff5 JH |
186 | /* |
187 | * Now the rest of the line starts at "written". | |
188 | * The non-highlighted part ends at "trailing_whitespace". | |
189 | */ | |
c1795bb0 WC |
190 | if (trailing_whitespace == -1) |
191 | trailing_whitespace = len; | |
192 | ||
193 | /* Emit non-highlighted (middle) segment. */ | |
954ecd43 | 194 | if (trailing_whitespace - written > 0) { |
c1795bb0 | 195 | fputs(set, stream); |
954ecd43 BF |
196 | fwrite(line + written, |
197 | trailing_whitespace - written, 1, stream); | |
c1795bb0 WC |
198 | fputs(reset, stream); |
199 | } | |
200 | ||
201 | /* Highlight errors in trailing whitespace. */ | |
202 | if (trailing_whitespace != len) { | |
203 | fputs(ws, stream); | |
204 | fwrite(line + trailing_whitespace, | |
205 | len - trailing_whitespace, 1, stream); | |
206 | fputs(reset, stream); | |
207 | } | |
b2979ff5 JH |
208 | if (trailing_carriage_return) |
209 | fputc('\r', stream); | |
c1795bb0 WC |
210 | if (trailing_newline) |
211 | fputc('\n', stream); | |
212 | } | |
213 | return result; | |
214 | } |