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
;
62 if (rule
& WS_TAB_IN_INDENT
&& rule
& WS_INDENT_WITH_NON_TAB
)
63 die("cannot enforce both tab-in-indent and indent-with-non-tab");
67 static void setup_whitespace_attr_check(struct git_attr_check
*check
)
69 static struct git_attr
*attr_whitespace
;
72 attr_whitespace
= git_attr("whitespace");
73 check
[0].attr
= attr_whitespace
;
76 unsigned whitespace_rule(const char *pathname
)
78 struct git_attr_check attr_whitespace_rule
;
80 setup_whitespace_attr_check(&attr_whitespace_rule
);
81 if (!git_checkattr(pathname
, 1, &attr_whitespace_rule
)) {
84 value
= attr_whitespace_rule
.value
;
85 if (ATTR_TRUE(value
)) {
86 /* true (whitespace) */
87 unsigned all_rule
= 0;
89 for (i
= 0; i
< ARRAY_SIZE(whitespace_rule_names
); i
++)
90 if (!whitespace_rule_names
[i
].loosens_error
&&
91 !whitespace_rule_names
[i
].exclude_default
)
92 all_rule
|= whitespace_rule_names
[i
].rule_bits
;
94 } else if (ATTR_FALSE(value
)) {
95 /* false (-whitespace) */
97 } else if (ATTR_UNSET(value
)) {
98 /* reset to default (!whitespace) */
99 return whitespace_rule_cfg
;
102 return parse_whitespace_rule(value
);
105 return whitespace_rule_cfg
;
109 /* The returned string should be freed by the caller. */
110 char *whitespace_error_string(unsigned ws
)
112 struct strbuf err
= STRBUF_INIT
;
113 if ((ws
& WS_TRAILING_SPACE
) == WS_TRAILING_SPACE
)
114 strbuf_addstr(&err
, "trailing whitespace");
116 if (ws
& WS_BLANK_AT_EOL
)
117 strbuf_addstr(&err
, "trailing whitespace");
118 if (ws
& WS_BLANK_AT_EOF
) {
120 strbuf_addstr(&err
, ", ");
121 strbuf_addstr(&err
, "new blank line at EOF");
124 if (ws
& WS_SPACE_BEFORE_TAB
) {
126 strbuf_addstr(&err
, ", ");
127 strbuf_addstr(&err
, "space before tab in indent");
129 if (ws
& WS_INDENT_WITH_NON_TAB
) {
131 strbuf_addstr(&err
, ", ");
132 strbuf_addstr(&err
, "indent with spaces");
134 if (ws
& WS_TAB_IN_INDENT
) {
136 strbuf_addstr(&err
, ", ");
137 strbuf_addstr(&err
, "tab in indent");
139 return strbuf_detach(&err
, NULL
);
142 /* If stream is non-NULL, emits the line after checking. */
143 static unsigned ws_check_emit_1(const char *line
, int len
, unsigned ws_rule
,
144 FILE *stream
, const char *set
,
145 const char *reset
, const char *ws
)
149 int trailing_whitespace
= -1;
150 int trailing_newline
= 0;
151 int trailing_carriage_return
= 0;
154 /* Logic is simpler if we temporarily ignore the trailing newline. */
155 if (len
> 0 && line
[len
- 1] == '\n') {
156 trailing_newline
= 1;
159 if ((ws_rule
& WS_CR_AT_EOL
) &&
160 len
> 0 && line
[len
- 1] == '\r') {
161 trailing_carriage_return
= 1;
165 /* Check for trailing whitespace. */
166 if (ws_rule
& WS_BLANK_AT_EOL
) {
167 for (i
= len
- 1; i
>= 0; i
--) {
168 if (isspace(line
[i
])) {
169 trailing_whitespace
= i
;
170 result
|= WS_BLANK_AT_EOL
;
177 if (trailing_whitespace
== -1)
178 trailing_whitespace
= len
;
180 /* Check indentation */
181 for (i
= 0; i
< trailing_whitespace
; i
++) {
186 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) && written
< i
) {
187 result
|= WS_SPACE_BEFORE_TAB
;
190 fwrite(line
+ written
, i
- written
, 1, stream
);
191 fputs(reset
, stream
);
192 fwrite(line
+ i
, 1, 1, stream
);
194 } else if (ws_rule
& WS_TAB_IN_INDENT
) {
195 result
|= WS_TAB_IN_INDENT
;
197 fwrite(line
+ written
, i
- written
, 1, stream
);
199 fwrite(line
+ i
, 1, 1, stream
);
200 fputs(reset
, stream
);
203 fwrite(line
+ written
, i
- written
+ 1, 1, stream
);
208 /* Check for indent using non-tab. */
209 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) && i
- written
>= 8) {
210 result
|= WS_INDENT_WITH_NON_TAB
;
213 fwrite(line
+ written
, i
- written
, 1, stream
);
214 fputs(reset
, stream
);
221 * Now the rest of the line starts at "written".
222 * The non-highlighted part ends at "trailing_whitespace".
225 /* Emit non-highlighted (middle) segment. */
226 if (trailing_whitespace
- written
> 0) {
228 fwrite(line
+ written
,
229 trailing_whitespace
- written
, 1, stream
);
230 fputs(reset
, stream
);
233 /* Highlight errors in trailing whitespace. */
234 if (trailing_whitespace
!= len
) {
236 fwrite(line
+ trailing_whitespace
,
237 len
- trailing_whitespace
, 1, stream
);
238 fputs(reset
, stream
);
240 if (trailing_carriage_return
)
242 if (trailing_newline
)
248 void ws_check_emit(const char *line
, int len
, unsigned ws_rule
,
249 FILE *stream
, const char *set
,
250 const char *reset
, const char *ws
)
252 (void)ws_check_emit_1(line
, len
, ws_rule
, stream
, set
, reset
, ws
);
255 unsigned ws_check(const char *line
, int len
, unsigned ws_rule
)
257 return ws_check_emit_1(line
, len
, ws_rule
, NULL
, NULL
, NULL
, NULL
);
260 int ws_blank_line(const char *line
, int len
, unsigned ws_rule
)
263 * We _might_ want to treat CR differently from other
264 * whitespace characters when ws_rule has WS_CR_AT_EOL, but
265 * for now we just use this stupid definition.
275 /* Copy the line onto the end of the strbuf while fixing whitespaces */
276 void ws_fix_copy(struct strbuf
*dst
, const char *src
, int len
, unsigned ws_rule
, int *error_count
)
279 * len is number of bytes to be copied from src, starting
280 * at src. Typically src[len-1] is '\n', unless this is
281 * the incomplete last line.
284 int add_nl_to_tail
= 0;
285 int add_cr_to_tail
= 0;
287 int last_tab_in_indent
= -1;
288 int last_space_in_indent
= -1;
289 int need_fix_leading_space
= 0;
292 * Strip trailing whitespace
294 if (ws_rule
& WS_BLANK_AT_EOL
) {
295 if (0 < len
&& src
[len
- 1] == '\n') {
298 if (0 < len
&& src
[len
- 1] == '\r') {
299 add_cr_to_tail
= !!(ws_rule
& WS_CR_AT_EOL
);
303 if (0 < len
&& isspace(src
[len
- 1])) {
304 while (0 < len
&& isspace(src
[len
-1]))
311 * Check leading whitespaces (indent)
313 for (i
= 0; i
< len
; i
++) {
316 last_tab_in_indent
= i
;
317 if ((ws_rule
& WS_SPACE_BEFORE_TAB
) &&
318 0 <= last_space_in_indent
)
319 need_fix_leading_space
= 1;
320 } else if (ch
== ' ') {
321 last_space_in_indent
= i
;
322 if ((ws_rule
& WS_INDENT_WITH_NON_TAB
) &&
323 8 <= i
- last_tab_in_indent
)
324 need_fix_leading_space
= 1;
329 if (need_fix_leading_space
) {
330 /* Process indent ourselves */
331 int consecutive_spaces
= 0;
332 int last
= last_tab_in_indent
+ 1;
334 if (ws_rule
& WS_INDENT_WITH_NON_TAB
) {
335 /* have "last" point at one past the indent */
336 if (last_tab_in_indent
< last_space_in_indent
)
337 last
= last_space_in_indent
+ 1;
339 last
= last_tab_in_indent
+ 1;
343 * between src[0..last-1], strip the funny spaces,
344 * updating them to tab as needed.
346 for (i
= 0; i
< last
; i
++) {
349 consecutive_spaces
= 0;
350 strbuf_addch(dst
, ch
);
352 consecutive_spaces
++;
353 if (consecutive_spaces
== 8) {
354 strbuf_addch(dst
, '\t');
355 consecutive_spaces
= 0;
359 while (0 < consecutive_spaces
--)
360 strbuf_addch(dst
, ' ');
364 } else if ((ws_rule
& WS_TAB_IN_INDENT
) && last_tab_in_indent
>= 0) {
365 /* Expand tabs into spaces */
366 int start
= dst
->len
;
367 int last
= last_tab_in_indent
+ 1;
368 for (i
= 0; i
< last
; i
++) {
371 strbuf_addch(dst
, ' ');
372 } while ((dst
->len
- start
) % 8);
374 strbuf_addch(dst
, src
[i
]);
381 strbuf_add(dst
, src
, len
);
383 strbuf_addch(dst
, '\r');
385 strbuf_addch(dst
, '\n');
386 if (fixed
&& error_count
)