4 * Copyright (c) 2007 Junio C Hamano
10 static struct whitespace_rule
{
11 const char *rule_name
;
13 unsigned loosens_error
;
14 } whitespace_rule_names
[] = {
15 { "trailing-space", WS_TRAILING_SPACE
, 0 },
16 { "space-before-tab", WS_SPACE_BEFORE_TAB
, 0 },
17 { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB
, 0 },
18 { "cr-at-eol", WS_CR_AT_EOL
, 1 },
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 if (!whitespace_rule_names
[i
].loosens_error
)
84 all_rule
|= whitespace_rule_names
[i
].rule_bits
;
86 } else if (ATTR_FALSE(value
)) {
87 /* false (-whitespace) */
89 } else if (ATTR_UNSET(value
)) {
90 /* reset to default (!whitespace) */
91 return whitespace_rule_cfg
;
94 return parse_whitespace_rule(value
);
97 return whitespace_rule_cfg
;
101 /* The returned string should be freed by the caller. */
102 char *whitespace_error_string(unsigned ws
)
104 struct strbuf err
= STRBUF_INIT
;
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 return strbuf_detach(&err
, NULL
);
120 /* If stream is non-NULL, emits the line after checking. */
121 static unsigned ws_check_emit_1(const char *line
, int len
, unsigned ws_rule
,
122 FILE *stream
, const char *set
,
123 const char *reset
, const char *ws
)
127 int trailing_whitespace
= -1;
128 int trailing_newline
= 0;
129 int trailing_carriage_return
= 0;
132 /* Logic is simpler if we temporarily ignore the trailing newline. */
133 if (len
> 0 && line
[len
- 1] == '\n') {
134 trailing_newline
= 1;
137 if ((ws_rule
& WS_CR_AT_EOL
) &&
138 len
> 0 && line
[len
- 1] == '\r') {
139 trailing_carriage_return
= 1;
143 /* Check for trailing whitespace. */
144 if (ws_rule
& WS_TRAILING_SPACE
) {
145 for (i
= len
- 1; i
>= 0; i
--) {
146 if (isspace(line
[i
])) {
147 trailing_whitespace
= i
;
148 result
|= WS_TRAILING_SPACE
;
155 /* Check for space before tab in initial indent. */
156 for (i
= 0; i
< len
; i
++) {
161 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) && written
< i
) {
162 result
|= WS_SPACE_BEFORE_TAB
;
165 fwrite(line
+ written
, i
- written
, 1, stream
);
166 fputs(reset
, stream
);
169 fwrite(line
+ written
, i
- written
, 1, stream
);
171 fwrite(line
+ i
, 1, 1, stream
);
175 /* Check for indent using non-tab. */
176 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) && i
- written
>= 8) {
177 result
|= WS_INDENT_WITH_NON_TAB
;
180 fwrite(line
+ written
, i
- written
, 1, stream
);
181 fputs(reset
, stream
);
188 * Now the rest of the line starts at "written".
189 * The non-highlighted part ends at "trailing_whitespace".
191 if (trailing_whitespace
== -1)
192 trailing_whitespace
= len
;
194 /* Emit non-highlighted (middle) segment. */
195 if (trailing_whitespace
- written
> 0) {
197 fwrite(line
+ written
,
198 trailing_whitespace
- written
, 1, stream
);
199 fputs(reset
, stream
);
202 /* Highlight errors in trailing whitespace. */
203 if (trailing_whitespace
!= len
) {
205 fwrite(line
+ trailing_whitespace
,
206 len
- trailing_whitespace
, 1, stream
);
207 fputs(reset
, stream
);
209 if (trailing_carriage_return
)
211 if (trailing_newline
)
217 void ws_check_emit(const char *line
, int len
, unsigned ws_rule
,
218 FILE *stream
, const char *set
,
219 const char *reset
, const char *ws
)
221 (void)ws_check_emit_1(line
, len
, ws_rule
, stream
, set
, reset
, ws
);
224 unsigned ws_check(const char *line
, int len
, unsigned ws_rule
)
226 return ws_check_emit_1(line
, len
, ws_rule
, NULL
, NULL
, NULL
, NULL
);
229 int ws_blank_line(const char *line
, int len
, unsigned ws_rule
)
232 * We _might_ want to treat CR differently from other
233 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
234 * for now we just use this stupid definition.
244 /* Copy the line to the buffer while fixing whitespaces */
245 int ws_fix_copy(char *dst
, const char *src
, int len
, unsigned ws_rule
, int *error_count
)
248 * len is number of bytes to be copied from src, starting
249 * at src. Typically src[len-1] is '\n', unless this is
250 * the incomplete last line.
253 int add_nl_to_tail
= 0;
254 int add_cr_to_tail
= 0;
256 int last_tab_in_indent
= -1;
257 int last_space_in_indent
= -1;
258 int need_fix_leading_space
= 0;
262 * Strip trailing whitespace
264 if (ws_rule
& WS_TRAILING_SPACE
) {
265 if (0 < len
&& src
[len
- 1] == '\n') {
268 if (0 < 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
;