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
)
103 strbuf_init(&err
, 0);
104 if (ws
& WS_TRAILING_SPACE
)
105 strbuf_addstr(&err
, "trailing whitespace");
106 if (ws
& WS_SPACE_BEFORE_TAB
) {
108 strbuf_addstr(&err
, ", ");
109 strbuf_addstr(&err
, "space before tab in indent");
111 if (ws
& WS_INDENT_WITH_NON_TAB
) {
113 strbuf_addstr(&err
, ", ");
114 strbuf_addstr(&err
, "indent with spaces");
116 return strbuf_detach(&err
, NULL
);
119 /* If stream is non-NULL, emits the line after checking. */
120 static unsigned ws_check_emit_1(const char *line
, int len
, unsigned ws_rule
,
121 FILE *stream
, const char *set
,
122 const char *reset
, const char *ws
)
126 int trailing_whitespace
= -1;
127 int trailing_newline
= 0;
128 int trailing_carriage_return
= 0;
131 /* Logic is simpler if we temporarily ignore the trailing newline. */
132 if (len
> 0 && line
[len
- 1] == '\n') {
133 trailing_newline
= 1;
136 if ((ws_rule
& WS_CR_AT_EOL
) &&
137 len
> 0 && line
[len
- 1] == '\r') {
138 trailing_carriage_return
= 1;
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
;
154 /* Check for space before tab in initial indent. */
155 for (i
= 0; i
< len
; i
++) {
160 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) && written
< i
) {
161 result
|= WS_SPACE_BEFORE_TAB
;
164 fwrite(line
+ written
, i
- written
, 1, stream
);
165 fputs(reset
, stream
);
168 fwrite(line
+ written
, i
- written
, 1, stream
);
170 fwrite(line
+ i
, 1, 1, stream
);
174 /* Check for indent using non-tab. */
175 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) && i
- written
>= 8) {
176 result
|= WS_INDENT_WITH_NON_TAB
;
179 fwrite(line
+ written
, i
- written
, 1, stream
);
180 fputs(reset
, stream
);
187 * Now the rest of the line starts at "written".
188 * The non-highlighted part ends at "trailing_whitespace".
190 if (trailing_whitespace
== -1)
191 trailing_whitespace
= len
;
193 /* Emit non-highlighted (middle) segment. */
194 if (trailing_whitespace
- written
> 0) {
196 fwrite(line
+ written
,
197 trailing_whitespace
- written
, 1, stream
);
198 fputs(reset
, stream
);
201 /* Highlight errors in trailing whitespace. */
202 if (trailing_whitespace
!= len
) {
204 fwrite(line
+ trailing_whitespace
,
205 len
- trailing_whitespace
, 1, stream
);
206 fputs(reset
, stream
);
208 if (trailing_carriage_return
)
210 if (trailing_newline
)
216 void ws_check_emit(const char *line
, int len
, unsigned ws_rule
,
217 FILE *stream
, const char *set
,
218 const char *reset
, const char *ws
)
220 (void)ws_check_emit_1(line
, len
, ws_rule
, stream
, set
, reset
, ws
);
223 unsigned ws_check(const char *line
, int len
, unsigned ws_rule
)
225 return ws_check_emit_1(line
, len
, ws_rule
, NULL
, NULL
, NULL
, NULL
);
228 int ws_blank_line(const char *line
, int len
, unsigned ws_rule
)
231 * We _might_ want to treat CR differently from other
232 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
233 * for now we just use this stupid definition.
243 /* Copy the line to the buffer while fixing whitespaces */
244 int ws_fix_copy(char *dst
, const char *src
, int len
, unsigned ws_rule
, int *error_count
)
247 * len is number of bytes to be copied from src, starting
248 * at src. Typically src[len-1] is '\n', unless this is
249 * the incomplete last line.
252 int add_nl_to_tail
= 0;
253 int add_cr_to_tail
= 0;
255 int last_tab_in_indent
= -1;
256 int last_space_in_indent
= -1;
257 int need_fix_leading_space
= 0;
261 * Strip trailing whitespace
263 if ((ws_rule
& WS_TRAILING_SPACE
) &&
264 (2 <= len
&& isspace(src
[len
-2]))) {
265 if (src
[len
- 1] == '\n') {
268 if (1 < len
&& src
[len
- 1] == '\r') {
269 add_cr_to_tail
= !!(ws_rule
& WS_CR_AT_EOL
);
273 if (0 < len
&& isspace(src
[len
- 1])) {
274 while (0 < len
&& isspace(src
[len
-1]))
281 * Check leading whitespaces (indent)
283 for (i
= 0; i
< len
; i
++) {
286 last_tab_in_indent
= i
;
287 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) &&
288 0 <= last_space_in_indent
)
289 need_fix_leading_space
= 1;
290 } else if (ch
== ' ') {
291 last_space_in_indent
= i
;
292 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) &&
293 8 <= i
- last_tab_in_indent
)
294 need_fix_leading_space
= 1;
300 if (need_fix_leading_space
) {
301 /* Process indent ourselves */
302 int consecutive_spaces
= 0;
303 int last
= last_tab_in_indent
+ 1;
305 if (ws_rule
& WS_INDENT_WITH_NON_TAB
) {
306 /* have "last" point at one past the indent */
307 if (last_tab_in_indent
< last_space_in_indent
)
308 last
= last_space_in_indent
+ 1;
310 last
= last_tab_in_indent
+ 1;
314 * between src[0..last-1], strip the funny spaces,
315 * updating them to tab as needed.
317 for (i
= 0; i
< last
; i
++) {
320 consecutive_spaces
= 0;
323 consecutive_spaces
++;
324 if (consecutive_spaces
== 8) {
326 consecutive_spaces
= 0;
330 while (0 < consecutive_spaces
--)
337 memcpy(dst
, src
, len
);
342 if (fixed
&& error_count
)
344 return dst
+ len
- buf
;