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 },
24 unsigned parse_whitespace_rule(const char *string
)
26 unsigned rule
= WS_DEFAULT_RULE
;
34 string
= string
+ strspn(string
, ", \t\n\r");
35 ep
= strchr(string
, ',');
48 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++) {
49 if (strncmp(whitespace_rule_names
[i
].rule_name
,
53 rule
&= ~whitespace_rule_names
[i
].rule_bits
;
55 rule
|= whitespace_rule_names
[i
].rule_bits
;
63 static void setup_whitespace_attr_check(struct git_attr_check
*check
)
65 static struct git_attr
*attr_whitespace
;
68 attr_whitespace
= git_attr("whitespace");
69 check
[0].attr
= attr_whitespace
;
72 unsigned whitespace_rule(const char *pathname
)
74 struct git_attr_check attr_whitespace_rule
;
76 setup_whitespace_attr_check(&attr_whitespace_rule
);
77 if (!git_checkattr(pathname
, 1, &attr_whitespace_rule
)) {
80 value
= attr_whitespace_rule
.value
;
81 if (ATTR_TRUE(value
)) {
82 /* true (whitespace) */
83 unsigned all_rule
= 0;
85 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++)
86 if (!whitespace_rule_names
[i
].loosens_error
&&
87 !whitespace_rule_names
[i
].exclude_default
)
88 all_rule
|= whitespace_rule_names
[i
].rule_bits
;
90 } else if (ATTR_FALSE(value
)) {
91 /* false (-whitespace) */
93 } else if (ATTR_UNSET(value
)) {
94 /* reset to default (!whitespace) */
95 return whitespace_rule_cfg
;
98 return parse_whitespace_rule(value
);
101 return whitespace_rule_cfg
;
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 return strbuf_detach(&err
, NULL
);
133 /* If stream is non-NULL, emits the line after checking. */
134 static unsigned ws_check_emit_1(const char *line
, int len
, unsigned ws_rule
,
135 FILE *stream
, const char *set
,
136 const char *reset
, const char *ws
)
140 int trailing_whitespace
= -1;
141 int trailing_newline
= 0;
142 int trailing_carriage_return
= 0;
145 /* Logic is simpler if we temporarily ignore the trailing newline. */
146 if (len
> 0 && line
[len
- 1] == '\n') {
147 trailing_newline
= 1;
150 if ((ws_rule
& WS_CR_AT_EOL
) &&
151 len
> 0 && line
[len
- 1] == '\r') {
152 trailing_carriage_return
= 1;
156 /* Check for trailing whitespace. */
157 if (ws_rule
& WS_BLANK_AT_EOL
) {
158 for (i
= len
- 1; i
>= 0; i
--) {
159 if (isspace(line
[i
])) {
160 trailing_whitespace
= i
;
161 result
|= WS_BLANK_AT_EOL
;
168 /* Check for space before tab in initial indent. */
169 for (i
= 0; i
< len
; i
++) {
174 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) && written
< i
) {
175 result
|= WS_SPACE_BEFORE_TAB
;
178 fwrite(line
+ written
, i
- written
, 1, stream
);
179 fputs(reset
, stream
);
182 fwrite(line
+ written
, i
- written
, 1, stream
);
184 fwrite(line
+ i
, 1, 1, stream
);
188 /* Check for indent using non-tab. */
189 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) && i
- written
>= 8) {
190 result
|= WS_INDENT_WITH_NON_TAB
;
193 fwrite(line
+ written
, i
- written
, 1, stream
);
194 fputs(reset
, stream
);
201 * Now the rest of the line starts at "written".
202 * The non-highlighted part ends at "trailing_whitespace".
204 if (trailing_whitespace
== -1)
205 trailing_whitespace
= len
;
207 /* Emit non-highlighted (middle) segment. */
208 if (trailing_whitespace
- written
> 0) {
210 fwrite(line
+ written
,
211 trailing_whitespace
- written
, 1, stream
);
212 fputs(reset
, stream
);
215 /* Highlight errors in trailing whitespace. */
216 if (trailing_whitespace
!= len
) {
218 fwrite(line
+ trailing_whitespace
,
219 len
- trailing_whitespace
, 1, stream
);
220 fputs(reset
, stream
);
222 if (trailing_carriage_return
)
224 if (trailing_newline
)
230 void ws_check_emit(const char *line
, int len
, unsigned ws_rule
,
231 FILE *stream
, const char *set
,
232 const char *reset
, const char *ws
)
234 (void)ws_check_emit_1(line
, len
, ws_rule
, stream
, set
, reset
, ws
);
237 unsigned ws_check(const char *line
, int len
, unsigned ws_rule
)
239 return ws_check_emit_1(line
, len
, ws_rule
, NULL
, NULL
, NULL
, NULL
);
242 int ws_blank_line(const char *line
, int len
, unsigned ws_rule
)
245 * We _might_ want to treat CR differently from other
246 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
247 * for now we just use this stupid definition.
257 /* Copy the line to the buffer while fixing whitespaces */
258 int ws_fix_copy(char *dst
, const char *src
, int len
, unsigned ws_rule
, int *error_count
)
261 * len is number of bytes to be copied from src, starting
262 * at src. Typically src[len-1] is '\n', unless this is
263 * the incomplete last line.
266 int add_nl_to_tail
= 0;
267 int add_cr_to_tail
= 0;
269 int last_tab_in_indent
= -1;
270 int last_space_in_indent
= -1;
271 int need_fix_leading_space
= 0;
275 * Strip trailing whitespace
277 if (ws_rule
& WS_BLANK_AT_EOL
) {
278 if (0 < len
&& src
[len
- 1] == '\n') {
281 if (0 < len
&& src
[len
- 1] == '\r') {
282 add_cr_to_tail
= !!(ws_rule
& WS_CR_AT_EOL
);
286 if (0 < len
&& isspace(src
[len
- 1])) {
287 while (0 < len
&& isspace(src
[len
-1]))
294 * Check leading whitespaces (indent)
296 for (i
= 0; i
< len
; i
++) {
299 last_tab_in_indent
= i
;
300 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) &&
301 0 <= last_space_in_indent
)
302 need_fix_leading_space
= 1;
303 } else if (ch
== ' ') {
304 last_space_in_indent
= i
;
305 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) &&
306 8 <= i
- last_tab_in_indent
)
307 need_fix_leading_space
= 1;
313 if (need_fix_leading_space
) {
314 /* Process indent ourselves */
315 int consecutive_spaces
= 0;
316 int last
= last_tab_in_indent
+ 1;
318 if (ws_rule
& WS_INDENT_WITH_NON_TAB
) {
319 /* have "last" point at one past the indent */
320 if (last_tab_in_indent
< last_space_in_indent
)
321 last
= last_space_in_indent
+ 1;
323 last
= last_tab_in_indent
+ 1;
327 * between src[0..last-1], strip the funny spaces,
328 * updating them to tab as needed.
330 for (i
= 0; i
< last
; i
++) {
333 consecutive_spaces
= 0;
336 consecutive_spaces
++;
337 if (consecutive_spaces
== 8) {
339 consecutive_spaces
= 0;
343 while (0 < consecutive_spaces
--)
350 memcpy(dst
, src
, len
);
355 if (fixed
&& error_count
)
357 return dst
+ len
- buf
;