4 * Copyright (c) 2007 Junio C Hamano
7 #define DISABLE_SIGN_COMPARE_WARNINGS
9 #include "git-compat-util.h"
14 unsigned whitespace_rule_cfg
= WS_DEFAULT_RULE
;
16 static struct whitespace_rule
{
17 const char *rule_name
;
19 unsigned loosens_error
:1,
21 } whitespace_rule_names
[] = {
22 { "trailing-space", WS_TRAILING_SPACE
, 0 },
23 { "space-before-tab", WS_SPACE_BEFORE_TAB
, 0 },
24 { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB
, 0 },
25 { "cr-at-eol", WS_CR_AT_EOL
, 1 },
26 { "blank-at-eol", WS_BLANK_AT_EOL
, 0 },
27 { "blank-at-eof", WS_BLANK_AT_EOF
, 0 },
28 { "tab-in-indent", WS_TAB_IN_INDENT
, 0, 1 },
31 unsigned parse_whitespace_rule(const char *string
)
33 unsigned rule
= WS_DEFAULT_RULE
;
42 string
= string
+ strspn(string
, ", \t\n\r");
43 ep
= strchrnul(string
, ',');
53 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++) {
54 if (strncmp(whitespace_rule_names
[i
].rule_name
,
58 rule
&= ~whitespace_rule_names
[i
].rule_bits
;
60 rule
|= whitespace_rule_names
[i
].rule_bits
;
63 if (skip_prefix(string
, "tabwidth=", &arg
)) {
64 unsigned tabwidth
= atoi(arg
);
65 if (0 < tabwidth
&& tabwidth
< 0100) {
66 rule
&= ~WS_TAB_WIDTH_MASK
;
70 warning("tabwidth %.*s out of range",
71 (int)(ep
- arg
), arg
);
76 if (rule
& WS_TAB_IN_INDENT
&& rule
& WS_INDENT_WITH_NON_TAB
)
77 die("cannot enforce both tab-in-indent and indent-with-non-tab");
81 unsigned whitespace_rule(struct index_state
*istate
, const char *pathname
)
83 static struct attr_check
*attr_whitespace_rule
;
86 if (!attr_whitespace_rule
)
87 attr_whitespace_rule
= attr_check_initl("whitespace", NULL
);
89 git_check_attr(istate
, pathname
, attr_whitespace_rule
);
90 value
= attr_whitespace_rule
->items
[0].value
;
91 if (ATTR_TRUE(value
)) {
92 /* true (whitespace) */
93 unsigned all_rule
= ws_tab_width(whitespace_rule_cfg
);
95 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++)
96 if (!whitespace_rule_names
[i
].loosens_error
&&
97 !whitespace_rule_names
[i
].exclude_default
)
98 all_rule
|= whitespace_rule_names
[i
].rule_bits
;
100 } else if (ATTR_FALSE(value
)) {
101 /* false (-whitespace) */
102 return ws_tab_width(whitespace_rule_cfg
);
103 } else if (ATTR_UNSET(value
)) {
104 /* reset to default (!whitespace) */
105 return whitespace_rule_cfg
;
108 return parse_whitespace_rule(value
);
112 /* The returned string should be freed by the caller. */
113 char *whitespace_error_string(unsigned ws
)
115 struct strbuf err
= STRBUF_INIT
;
116 if ((ws
& WS_TRAILING_SPACE
) == WS_TRAILING_SPACE
)
117 strbuf_addstr(&err
, "trailing whitespace");
119 if (ws
& WS_BLANK_AT_EOL
)
120 strbuf_addstr(&err
, "trailing whitespace");
121 if (ws
& WS_BLANK_AT_EOF
) {
123 strbuf_addstr(&err
, ", ");
124 strbuf_addstr(&err
, "new blank line at EOF");
127 if (ws
& WS_SPACE_BEFORE_TAB
) {
129 strbuf_addstr(&err
, ", ");
130 strbuf_addstr(&err
, "space before tab in indent");
132 if (ws
& WS_INDENT_WITH_NON_TAB
) {
134 strbuf_addstr(&err
, ", ");
135 strbuf_addstr(&err
, "indent with spaces");
137 if (ws
& WS_TAB_IN_INDENT
) {
139 strbuf_addstr(&err
, ", ");
140 strbuf_addstr(&err
, "tab in indent");
142 return strbuf_detach(&err
, NULL
);
145 /* If stream is non-NULL, emits the line after checking. */
146 static unsigned ws_check_emit_1(const char *line
, int len
, unsigned ws_rule
,
147 FILE *stream
, const char *set
,
148 const char *reset
, const char *ws
)
152 int trailing_whitespace
= -1;
153 int trailing_newline
= 0;
154 int trailing_carriage_return
= 0;
157 /* Logic is simpler if we temporarily ignore the trailing newline. */
158 if (len
> 0 && line
[len
- 1] == '\n') {
159 trailing_newline
= 1;
162 if ((ws_rule
& WS_CR_AT_EOL
) &&
163 len
> 0 && line
[len
- 1] == '\r') {
164 trailing_carriage_return
= 1;
168 /* Check for trailing whitespace. */
169 if (ws_rule
& WS_BLANK_AT_EOL
) {
170 for (i
= len
- 1; i
>= 0; i
--) {
171 if (isspace(line
[i
])) {
172 trailing_whitespace
= i
;
173 result
|= WS_BLANK_AT_EOL
;
180 if (trailing_whitespace
== -1)
181 trailing_whitespace
= len
;
183 /* Check indentation */
184 for (i
= 0; i
< trailing_whitespace
; i
++) {
189 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) && written
< i
) {
190 result
|= WS_SPACE_BEFORE_TAB
;
193 fwrite(line
+ written
, i
- written
, 1, stream
);
194 fputs(reset
, stream
);
195 fwrite(line
+ i
, 1, 1, stream
);
197 } else if (ws_rule
& WS_TAB_IN_INDENT
) {
198 result
|= WS_TAB_IN_INDENT
;
200 fwrite(line
+ written
, i
- written
, 1, stream
);
202 fwrite(line
+ i
, 1, 1, stream
);
203 fputs(reset
, stream
);
206 fwrite(line
+ written
, i
- written
+ 1, 1, stream
);
211 /* Check for indent using non-tab. */
212 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) && i
- written
>= ws_tab_width(ws_rule
)) {
213 result
|= WS_INDENT_WITH_NON_TAB
;
216 fwrite(line
+ written
, i
- written
, 1, stream
);
217 fputs(reset
, stream
);
224 * Now the rest of the line starts at "written".
225 * The non-highlighted part ends at "trailing_whitespace".
228 /* Emit non-highlighted (middle) segment. */
229 if (trailing_whitespace
- written
> 0) {
231 fwrite(line
+ written
,
232 trailing_whitespace
- written
, 1, stream
);
233 fputs(reset
, stream
);
236 /* Highlight errors in trailing whitespace. */
237 if (trailing_whitespace
!= len
) {
239 fwrite(line
+ trailing_whitespace
,
240 len
- trailing_whitespace
, 1, stream
);
241 fputs(reset
, stream
);
243 if (trailing_carriage_return
)
245 if (trailing_newline
)
251 void ws_check_emit(const char *line
, int len
, unsigned ws_rule
,
252 FILE *stream
, const char *set
,
253 const char *reset
, const char *ws
)
255 (void)ws_check_emit_1(line
, len
, ws_rule
, stream
, set
, reset
, ws
);
258 unsigned ws_check(const char *line
, int len
, unsigned ws_rule
)
260 return ws_check_emit_1(line
, len
, ws_rule
, NULL
, NULL
, NULL
, NULL
);
263 int ws_blank_line(const char *line
, int len
)
266 * We _might_ want to treat CR differently from other
267 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
268 * for now we just use this stupid definition.
278 /* Copy the line onto the end of the strbuf while fixing whitespaces */
279 void ws_fix_copy(struct strbuf
*dst
, const char *src
, int len
, unsigned ws_rule
, int *error_count
)
282 * len is number of bytes to be copied from src, starting
283 * at src. Typically src[len-1] is '\n', unless this is
284 * the incomplete last line.
287 int add_nl_to_tail
= 0;
288 int add_cr_to_tail
= 0;
290 int last_tab_in_indent
= -1;
291 int last_space_in_indent
= -1;
292 int need_fix_leading_space
= 0;
295 * Strip trailing whitespace
297 if (ws_rule
& WS_BLANK_AT_EOL
) {
298 if (0 < len
&& src
[len
- 1] == '\n') {
301 if (0 < len
&& src
[len
- 1] == '\r') {
302 add_cr_to_tail
= !!(ws_rule
& WS_CR_AT_EOL
);
306 if (0 < len
&& isspace(src
[len
- 1])) {
307 while (0 < len
&& isspace(src
[len
-1]))
314 * Check leading whitespaces (indent)
316 for (i
= 0; i
< len
; i
++) {
319 last_tab_in_indent
= i
;
320 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) &&
321 0 <= last_space_in_indent
)
322 need_fix_leading_space
= 1;
323 } else if (ch
== ' ') {
324 last_space_in_indent
= i
;
325 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) &&
326 ws_tab_width(ws_rule
) <= i
- last_tab_in_indent
)
327 need_fix_leading_space
= 1;
332 if (need_fix_leading_space
) {
333 /* Process indent ourselves */
334 int consecutive_spaces
= 0;
335 int last
= last_tab_in_indent
+ 1;
337 if (ws_rule
& WS_INDENT_WITH_NON_TAB
) {
338 /* have "last" point at one past the indent */
339 if (last_tab_in_indent
< last_space_in_indent
)
340 last
= last_space_in_indent
+ 1;
342 last
= last_tab_in_indent
+ 1;
346 * between src[0..last-1], strip the funny spaces,
347 * updating them to tab as needed.
349 for (i
= 0; i
< last
; i
++) {
352 consecutive_spaces
= 0;
353 strbuf_addch(dst
, ch
);
355 consecutive_spaces
++;
356 if (consecutive_spaces
== ws_tab_width(ws_rule
)) {
357 strbuf_addch(dst
, '\t');
358 consecutive_spaces
= 0;
362 while (0 < consecutive_spaces
--)
363 strbuf_addch(dst
, ' ');
367 } else if ((ws_rule
& WS_TAB_IN_INDENT
) && last_tab_in_indent
>= 0) {
368 /* Expand tabs into spaces */
369 int start
= dst
->len
;
370 int last
= last_tab_in_indent
+ 1;
371 for (i
= 0; i
< last
; i
++) {
374 strbuf_addch(dst
, ' ');
375 } while ((dst
->len
- start
) % ws_tab_width(ws_rule
));
377 strbuf_addch(dst
, src
[i
]);
384 strbuf_add(dst
, src
, len
);
386 strbuf_addch(dst
, '\r');
388 strbuf_addch(dst
, '\n');
389 if (fixed
&& error_count
)