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 },
22 { "tab-in-indent", WS_TAB_IN_INDENT
, 0, 1 },
25 unsigned parse_whitespace_rule(const char *string
)
27 unsigned rule
= WS_DEFAULT_RULE
;
35 string
= string
+ strspn(string
, ", \t\n\r");
36 ep
= strchr(string
, ',');
49 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++) {
50 if (strncmp(whitespace_rule_names
[i
].rule_name
,
54 rule
&= ~whitespace_rule_names
[i
].rule_bits
;
56 rule
|= whitespace_rule_names
[i
].rule_bits
;
59 if (strncmp(string
, "tabwidth=", 9) == 0) {
60 unsigned tabwidth
= atoi(string
+ 9);
61 if (0 < tabwidth
&& tabwidth
< 0100) {
62 rule
&= ~WS_TAB_WIDTH_MASK
;
66 warning("tabwidth %.*s out of range",
67 (int)(len
- 9), string
+ 9);
72 if (rule
& WS_TAB_IN_INDENT
&& rule
& WS_INDENT_WITH_NON_TAB
)
73 die("cannot enforce both tab-in-indent and indent-with-non-tab");
77 static void setup_whitespace_attr_check(struct git_attr_check
*check
)
79 static struct git_attr
*attr_whitespace
;
82 attr_whitespace
= git_attr("whitespace");
83 check
[0].attr
= attr_whitespace
;
86 unsigned whitespace_rule(const char *pathname
)
88 struct git_attr_check attr_whitespace_rule
;
90 setup_whitespace_attr_check(&attr_whitespace_rule
);
91 if (!git_check_attr(pathname
, 1, &attr_whitespace_rule
)) {
94 value
= attr_whitespace_rule
.value
;
95 if (ATTR_TRUE(value
)) {
96 /* true (whitespace) */
97 unsigned all_rule
= ws_tab_width(whitespace_rule_cfg
);
99 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++)
100 if (!whitespace_rule_names
[i
].loosens_error
&&
101 !whitespace_rule_names
[i
].exclude_default
)
102 all_rule
|= whitespace_rule_names
[i
].rule_bits
;
104 } else if (ATTR_FALSE(value
)) {
105 /* false (-whitespace) */
106 return ws_tab_width(whitespace_rule_cfg
);
107 } else if (ATTR_UNSET(value
)) {
108 /* reset to default (!whitespace) */
109 return whitespace_rule_cfg
;
112 return parse_whitespace_rule(value
);
115 return whitespace_rule_cfg
;
119 /* The returned string should be freed by the caller. */
120 char *whitespace_error_string(unsigned ws
)
122 struct strbuf err
= STRBUF_INIT
;
123 if ((ws
& WS_TRAILING_SPACE
) == WS_TRAILING_SPACE
)
124 strbuf_addstr(&err
, "trailing whitespace");
126 if (ws
& WS_BLANK_AT_EOL
)
127 strbuf_addstr(&err
, "trailing whitespace");
128 if (ws
& WS_BLANK_AT_EOF
) {
130 strbuf_addstr(&err
, ", ");
131 strbuf_addstr(&err
, "new blank line at EOF");
134 if (ws
& WS_SPACE_BEFORE_TAB
) {
136 strbuf_addstr(&err
, ", ");
137 strbuf_addstr(&err
, "space before tab in indent");
139 if (ws
& WS_INDENT_WITH_NON_TAB
) {
141 strbuf_addstr(&err
, ", ");
142 strbuf_addstr(&err
, "indent with spaces");
144 if (ws
& WS_TAB_IN_INDENT
) {
146 strbuf_addstr(&err
, ", ");
147 strbuf_addstr(&err
, "tab in indent");
149 return strbuf_detach(&err
, NULL
);
152 /* If stream is non-NULL, emits the line after checking. */
153 static unsigned ws_check_emit_1(const char *line
, int len
, unsigned ws_rule
,
154 FILE *stream
, const char *set
,
155 const char *reset
, const char *ws
)
159 int trailing_whitespace
= -1;
160 int trailing_newline
= 0;
161 int trailing_carriage_return
= 0;
164 /* Logic is simpler if we temporarily ignore the trailing newline. */
165 if (len
> 0 && line
[len
- 1] == '\n') {
166 trailing_newline
= 1;
169 if ((ws_rule
& WS_CR_AT_EOL
) &&
170 len
> 0 && line
[len
- 1] == '\r') {
171 trailing_carriage_return
= 1;
175 /* Check for trailing whitespace. */
176 if (ws_rule
& WS_BLANK_AT_EOL
) {
177 for (i
= len
- 1; i
>= 0; i
--) {
178 if (isspace(line
[i
])) {
179 trailing_whitespace
= i
;
180 result
|= WS_BLANK_AT_EOL
;
187 if (trailing_whitespace
== -1)
188 trailing_whitespace
= len
;
190 /* Check indentation */
191 for (i
= 0; i
< trailing_whitespace
; i
++) {
196 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) && written
< i
) {
197 result
|= WS_SPACE_BEFORE_TAB
;
200 fwrite(line
+ written
, i
- written
, 1, stream
);
201 fputs(reset
, stream
);
202 fwrite(line
+ i
, 1, 1, stream
);
204 } else if (ws_rule
& WS_TAB_IN_INDENT
) {
205 result
|= WS_TAB_IN_INDENT
;
207 fwrite(line
+ written
, i
- written
, 1, stream
);
209 fwrite(line
+ i
, 1, 1, stream
);
210 fputs(reset
, stream
);
213 fwrite(line
+ written
, i
- written
+ 1, 1, stream
);
218 /* Check for indent using non-tab. */
219 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) && i
- written
>= ws_tab_width(ws_rule
)) {
220 result
|= WS_INDENT_WITH_NON_TAB
;
223 fwrite(line
+ written
, i
- written
, 1, stream
);
224 fputs(reset
, stream
);
231 * Now the rest of the line starts at "written".
232 * The non-highlighted part ends at "trailing_whitespace".
235 /* Emit non-highlighted (middle) segment. */
236 if (trailing_whitespace
- written
> 0) {
238 fwrite(line
+ written
,
239 trailing_whitespace
- written
, 1, stream
);
240 fputs(reset
, stream
);
243 /* Highlight errors in trailing whitespace. */
244 if (trailing_whitespace
!= len
) {
246 fwrite(line
+ trailing_whitespace
,
247 len
- trailing_whitespace
, 1, stream
);
248 fputs(reset
, stream
);
250 if (trailing_carriage_return
)
252 if (trailing_newline
)
258 void ws_check_emit(const char *line
, int len
, unsigned ws_rule
,
259 FILE *stream
, const char *set
,
260 const char *reset
, const char *ws
)
262 (void)ws_check_emit_1(line
, len
, ws_rule
, stream
, set
, reset
, ws
);
265 unsigned ws_check(const char *line
, int len
, unsigned ws_rule
)
267 return ws_check_emit_1(line
, len
, ws_rule
, NULL
, NULL
, NULL
, NULL
);
270 int ws_blank_line(const char *line
, int len
, unsigned ws_rule
)
273 * We _might_ want to treat CR differently from other
274 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
275 * for now we just use this stupid definition.
285 /* Copy the line onto the end of the strbuf while fixing whitespaces */
286 void ws_fix_copy(struct strbuf
*dst
, const char *src
, int len
, unsigned ws_rule
, int *error_count
)
289 * len is number of bytes to be copied from src, starting
290 * at src. Typically src[len-1] is '\n', unless this is
291 * the incomplete last line.
294 int add_nl_to_tail
= 0;
295 int add_cr_to_tail
= 0;
297 int last_tab_in_indent
= -1;
298 int last_space_in_indent
= -1;
299 int need_fix_leading_space
= 0;
302 * Strip trailing whitespace
304 if (ws_rule
& WS_BLANK_AT_EOL
) {
305 if (0 < len
&& src
[len
- 1] == '\n') {
308 if (0 < len
&& src
[len
- 1] == '\r') {
309 add_cr_to_tail
= !!(ws_rule
& WS_CR_AT_EOL
);
313 if (0 < len
&& isspace(src
[len
- 1])) {
314 while (0 < len
&& isspace(src
[len
-1]))
321 * Check leading whitespaces (indent)
323 for (i
= 0; i
< len
; i
++) {
326 last_tab_in_indent
= i
;
327 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) &&
328 0 <= last_space_in_indent
)
329 need_fix_leading_space
= 1;
330 } else if (ch
== ' ') {
331 last_space_in_indent
= i
;
332 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) &&
333 ws_tab_width(ws_rule
) <= i
- last_tab_in_indent
)
334 need_fix_leading_space
= 1;
339 if (need_fix_leading_space
) {
340 /* Process indent ourselves */
341 int consecutive_spaces
= 0;
342 int last
= last_tab_in_indent
+ 1;
344 if (ws_rule
& WS_INDENT_WITH_NON_TAB
) {
345 /* have "last" point at one past the indent */
346 if (last_tab_in_indent
< last_space_in_indent
)
347 last
= last_space_in_indent
+ 1;
349 last
= last_tab_in_indent
+ 1;
353 * between src[0..last-1], strip the funny spaces,
354 * updating them to tab as needed.
356 for (i
= 0; i
< last
; i
++) {
359 consecutive_spaces
= 0;
360 strbuf_addch(dst
, ch
);
362 consecutive_spaces
++;
363 if (consecutive_spaces
== ws_tab_width(ws_rule
)) {
364 strbuf_addch(dst
, '\t');
365 consecutive_spaces
= 0;
369 while (0 < consecutive_spaces
--)
370 strbuf_addch(dst
, ' ');
374 } else if ((ws_rule
& WS_TAB_IN_INDENT
) && last_tab_in_indent
>= 0) {
375 /* Expand tabs into spaces */
376 int start
= dst
->len
;
377 int last
= last_tab_in_indent
+ 1;
378 for (i
= 0; i
< last
; i
++) {
381 strbuf_addch(dst
, ' ');
382 } while ((dst
->len
- start
) % ws_tab_width(ws_rule
));
384 strbuf_addch(dst
, src
[i
]);
391 strbuf_add(dst
, src
, len
);
393 strbuf_addch(dst
, '\r');
395 strbuf_addch(dst
, '\n');
396 if (fixed
&& error_count
)