@matheusbsbs / matheus-bagliano / commits / a9cc857ada
Fork 0

War on whitespace: first, a bit of retreat.

This introduces core.whitespace configuration variable that lets you specify the definition of "whitespace error". Currently there are two kinds of whitespace errors defined: * trailing-space: trailing whitespaces at the end of the line. * space-before-tab: a SP appears immediately before HT in the indent part of the line. You can specify the desired types of errors to be detected by listing their names (unique abbreviations are accepted) separated by comma. By default, these two errors are always detected, as that is the traditional behaviour. You can disable detection of a particular type of error by prefixing a '-' in front of the name of the error, like this: [core] whitespace = -trailing-space This patch teaches the code to output colored diff with DIFF_WHITESPACE color to highlight the detected whitespace errors to honor the new configuration. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Nov 2, 2007 at 00:24 UTC a9cc857ada7c57069ff00eed8d0addcf55849f39
4 files changed +70 -5
cache.h
+9
@@ -602,4 +602,13 @@ extern int diff_auto_refresh_index;
602 /* match-trees.c */
603 void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, int);
604
605 +/*
606 + * whitespace rules.
607 + * used by both diff and apply
608 + */
609 +#define WS_TRAILING_SPACE 01
610 +#define WS_SPACE_BEFORE_TAB 02
611 +#define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
612 +extern unsigned whitespace_rule;
613 +
614 #endif /* CACHE_H */
config.c
+52
@@ -246,6 +246,53 @@ static unsigned long get_unit_factor(const char *end)
246 die("unknown unit: '%s'", end);
247 }
248
249 +static struct whitespace_rule {
250 + const char *rule_name;
251 + unsigned rule_bits;
252 +} whitespace_rule_names[] = {
253 + { "trailing-space", WS_TRAILING_SPACE },
254 + { "space-before-tab", WS_SPACE_BEFORE_TAB },
255 +};
256 +
257 +static unsigned parse_whitespace_rule(const char *string)
258 +{
259 + unsigned rule = WS_DEFAULT_RULE;
260 +
261 + while (string) {
262 + int i;
263 + size_t len;
264 + const char *ep;
265 + int negated = 0;
266 +
267 + string = string + strspn(string, ", \t\n\r");
268 + ep = strchr(string, ',');
269 + if (!ep)
270 + len = strlen(string);
271 + else
272 + len = ep - string;
273 +
274 + if (*string == '-') {
275 + negated = 1;
276 + string++;
277 + len--;
278 + }
279 + if (!len)
280 + break;
281 + for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
282 + if (strncmp(whitespace_rule_names[i].rule_name,
283 + string, len))
284 + continue;
285 + if (negated)
286 + rule &= ~whitespace_rule_names[i].rule_bits;
287 + else
288 + rule |= whitespace_rule_names[i].rule_bits;
289 + break;
290 + }
291 + string = ep;
292 + }
293 + return rule;
294 +}
295 +
296 int git_parse_long(const char *value, long *ret)
297 {
298 if (value && *value) {
@@ -431,6 +478,11 @@ int git_default_config(const char *var, const char *value)
478 return 0;
479 }
480
481 + if (!strcmp(var, "core.whitespace")) {
482 + whitespace_rule = parse_whitespace_rule(value);
483 + return 0;
484 + }
485 +
486 /* Add other config variables here and to Documentation/config.txt. */
487 return 0;
488 }
diff.c
+8 -5
@@ -508,7 +508,8 @@ static void emit_line_with_ws(int nparents,
508 for (i = col0; i < len; i++) {
509 if (line[i] == '\t') {
510 last_tab_in_indent = i;
511 - if (0 <= last_space_in_indent)
511 + if ((whitespace_rule & WS_SPACE_BEFORE_TAB) &&
512 + 0 <= last_space_in_indent)
513 need_highlight_leading_space = 1;
514 }
515 else if (line[i] == ' ')
@@ -540,10 +541,12 @@ static void emit_line_with_ws(int nparents,
541 tail = len - 1;
542 if (line[tail] == '\n' && i < tail)
543 tail--;
543 - while (i < tail) {
544 - if (!isspace(line[tail]))
545 - break;
546 - tail--;
544 + if (whitespace_rule & WS_TRAILING_SPACE) {
545 + while (i < tail) {
546 + if (!isspace(line[tail]))
547 + break;
548 + tail--;
549 + }
550 }
551 if ((i < tail && line[tail + 1] != '\n')) {
552 /* This has whitespace between tail+1..len */
environment.c
+1
@@ -35,6 +35,7 @@ int pager_in_use;
35 int pager_use_color = 1;
36 char *editor_program;
37 int auto_crlf = 0; /* 1: both ways, -1: only when adding git objects */
38 +unsigned whitespace_rule = WS_DEFAULT_RULE;
39
40 /* This is set by setup_git_dir_gently() and/or git_default_config() */
41 char *git_work_tree_cfg;