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
},
20 unsigned parse_whitespace_rule(const char *string
)
22 unsigned rule
= WS_DEFAULT_RULE
;
30 string
= string
+ strspn(string
, ", \t\n\r");
31 ep
= strchr(string
, ',');
44 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++) {
45 if (strncmp(whitespace_rule_names
[i
].rule_name
,
49 rule
&= ~whitespace_rule_names
[i
].rule_bits
;
51 rule
|= whitespace_rule_names
[i
].rule_bits
;
59 static void setup_whitespace_attr_check(struct git_attr_check
*check
)
61 static struct git_attr
*attr_whitespace
;
64 attr_whitespace
= git_attr("whitespace", 10);
65 check
[0].attr
= attr_whitespace
;
68 unsigned whitespace_rule(const char *pathname
)
70 struct git_attr_check attr_whitespace_rule
;
72 setup_whitespace_attr_check(&attr_whitespace_rule
);
73 if (!git_checkattr(pathname
, 1, &attr_whitespace_rule
)) {
76 value
= attr_whitespace_rule
.value
;
77 if (ATTR_TRUE(value
)) {
78 /* true (whitespace) */
79 unsigned all_rule
= 0;
81 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++)
82 all_rule
|= whitespace_rule_names
[i
].rule_bits
;
84 } else if (ATTR_FALSE(value
)) {
85 /* false (-whitespace) */
87 } else if (ATTR_UNSET(value
)) {
88 /* reset to default (!whitespace) */
89 return whitespace_rule_cfg
;
92 return parse_whitespace_rule(value
);
95 return whitespace_rule_cfg
;
99 /* The returned string should be freed by the caller. */
100 char *whitespace_error_string(unsigned ws
)
102 struct strbuf err
= STRBUF_INIT
;
103 if (ws
& WS_TRAILING_SPACE
)
104 strbuf_addstr(&err
, "trailing whitespace");
105 if (ws
& WS_SPACE_BEFORE_TAB
) {
107 strbuf_addstr(&err
, ", ");
108 strbuf_addstr(&err
, "space before tab in indent");
110 if (ws
& WS_INDENT_WITH_NON_TAB
) {
112 strbuf_addstr(&err
, ", ");
113 strbuf_addstr(&err
, "indent with spaces");
115 return strbuf_detach(&err
, NULL
);
118 /* If stream is non-NULL, emits the line after checking. */
119 static unsigned ws_check_emit_1(const char *line
, int len
, unsigned ws_rule
,
120 FILE *stream
, const char *set
,
121 const char *reset
, const char *ws
)
125 int trailing_whitespace
= -1;
126 int trailing_newline
= 0;
127 int trailing_carriage_return
= 0;
130 /* Logic is simpler if we temporarily ignore the trailing newline. */
131 if (len
> 0 && line
[len
- 1] == '\n') {
132 trailing_newline
= 1;
135 if ((ws_rule
& WS_CR_AT_EOL
) &&
136 len
> 0 && line
[len
- 1] == '\r') {
137 trailing_carriage_return
= 1;
141 /* Check for trailing whitespace. */
142 if (ws_rule
& WS_TRAILING_SPACE
) {
143 for (i
= len
- 1; i
>= 0; i
--) {
144 if (isspace(line
[i
])) {
145 trailing_whitespace
= i
;
146 result
|= WS_TRAILING_SPACE
;
153 /* Check for space before tab in initial indent. */
154 for (i
= 0; i
< len
; i
++) {
159 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) && written
< i
) {
160 result
|= WS_SPACE_BEFORE_TAB
;
163 fwrite(line
+ written
, i
- written
, 1, stream
);
164 fputs(reset
, stream
);
167 fwrite(line
+ written
, i
- written
, 1, stream
);
169 fwrite(line
+ i
, 1, 1, stream
);
173 /* Check for indent using non-tab. */
174 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) && i
- written
>= 8) {
175 result
|= WS_INDENT_WITH_NON_TAB
;
178 fwrite(line
+ written
, i
- written
, 1, stream
);
179 fputs(reset
, stream
);
186 * Now the rest of the line starts at "written".
187 * The non-highlighted part ends at "trailing_whitespace".
189 if (trailing_whitespace
== -1)
190 trailing_whitespace
= len
;
192 /* Emit non-highlighted (middle) segment. */
193 if (trailing_whitespace
- written
> 0) {
195 fwrite(line
+ written
,
196 trailing_whitespace
- written
, 1, stream
);
197 fputs(reset
, stream
);
200 /* Highlight errors in trailing whitespace. */
201 if (trailing_whitespace
!= len
) {
203 fwrite(line
+ trailing_whitespace
,
204 len
- trailing_whitespace
, 1, stream
);
205 fputs(reset
, stream
);
207 if (trailing_carriage_return
)
209 if (trailing_newline
)
215 void ws_check_emit(const char *line
, int len
, unsigned ws_rule
,
216 FILE *stream
, const char *set
,
217 const char *reset
, const char *ws
)
219 (void)ws_check_emit_1(line
, len
, ws_rule
, stream
, set
, reset
, ws
);
222 unsigned ws_check(const char *line
, int len
, unsigned ws_rule
)
224 return ws_check_emit_1(line
, len
, ws_rule
, NULL
, NULL
, NULL
, NULL
);
227 int ws_blank_line(const char *line
, int len
, unsigned ws_rule
)
230 * We _might_ want to treat CR differently from other
231 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
232 * for now we just use this stupid definition.
242 /* Copy the line to the buffer while fixing whitespaces */
243 int ws_fix_copy(char *dst
, const char *src
, int len
, unsigned ws_rule
, int *error_count
)
246 * len is number of bytes to be copied from src, starting
247 * at src. Typically src[len-1] is '\n', unless this is
248 * the incomplete last line.
251 int add_nl_to_tail
= 0;
252 int add_cr_to_tail
= 0;
254 int last_tab_in_indent
= -1;
255 int last_space_in_indent
= -1;
256 int need_fix_leading_space
= 0;
260 * Strip trailing whitespace
262 if ((ws_rule
& WS_TRAILING_SPACE
) &&
263 (2 <= len
&& isspace(src
[len
-2]))) {
264 if (src
[len
- 1] == '\n') {
267 if (1 < len
&& src
[len
- 1] == '\r') {
268 add_cr_to_tail
= !!(ws_rule
& WS_CR_AT_EOL
);
272 if (0 < len
&& isspace(src
[len
- 1])) {
273 while (0 < len
&& isspace(src
[len
-1]))
280 * Check leading whitespaces (indent)
282 for (i
= 0; i
< len
; i
++) {
285 last_tab_in_indent
= i
;
286 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) &&
287 0 <= last_space_in_indent
)
288 need_fix_leading_space
= 1;
289 } else if (ch
== ' ') {
290 last_space_in_indent
= i
;
291 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) &&
292 8 <= i
- last_tab_in_indent
)
293 need_fix_leading_space
= 1;
299 if (need_fix_leading_space
) {
300 /* Process indent ourselves */
301 int consecutive_spaces
= 0;
302 int last
= last_tab_in_indent
+ 1;
304 if (ws_rule
& WS_INDENT_WITH_NON_TAB
) {
305 /* have "last" point at one past the indent */
306 if (last_tab_in_indent
< last_space_in_indent
)
307 last
= last_space_in_indent
+ 1;
309 last
= last_tab_in_indent
+ 1;
313 * between src[0..last-1], strip the funny spaces,
314 * updating them to tab as needed.
316 for (i
= 0; i
< last
; i
++) {
319 consecutive_spaces
= 0;
322 consecutive_spaces
++;
323 if (consecutive_spaces
== 8) {
325 consecutive_spaces
= 0;
329 while (0 < consecutive_spaces
--)
336 memcpy(dst
, src
, len
);
341 if (fixed
&& error_count
)
343 return dst
+ len
- buf
;