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

Use gitattributes to define per-path whitespace rule

The `core.whitespace` configuration variable allows you to define what `diff` and `apply` should consider whitespace errors for all paths in the project (See gitlink:git-config[1]). This attribute gives you finer control per path. For example, if you have these in the .gitattributes: frotz whitespace nitfol -whitespace xyzzy whitespace=-trailing all types of whitespace problems known to git are noticed in path 'frotz' (i.e. diff shows them in diff.whitespace color, and apply warns about them), no whitespace problem is noticed in path 'nitfol', and the default types of whitespace problems except "trailing whitespace" are noticed for path 'xyzzy'. A project with mixed Python and C might want to have: *.c whitespace *.py whitespace=-indent-with-non-tab in its toplevel .gitattributes file. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Dec 6, 2007 at 00:14 UTC cf1b7869f0c571bbd4f72a4355d9aca558baa0da
10 files changed +233 -74
Documentation/gitattributes.txt
+31
@@ -360,6 +360,37 @@ When left unspecified, the driver itself is used for both
360 internal merge and the final merge.
361
362
363 +Checking whitespace errors
364 +~~~~~~~~~~~~~~~~~~~~~~~~~~
365 +
366 +`whitespace`
367 +^^^^^^^^^^^^
368 +
369 +The `core.whitespace` configuration variable allows you to define what
370 +`diff` and `apply` should consider whitespace errors for all paths in
371 +the project (See gitlink:git-config[1]). This attribute gives you finer
372 +control per path.
373 +
374 +Set::
375 +
376 + Notice all types of potential whitespace errors known to git.
377 +
378 +Unset::
379 +
380 + Do not notice anything as error.
381 +
382 +Unspecified::
383 +
384 + Use the value of `core.whitespace` configuration variable to
385 + decide what to notice as error.
386 +
387 +String::
388 +
389 + Specify a comma separate list of common whitespace problems to
390 + notice in the same format as `core.whitespace` configuration
391 + variable.
392 +
393 +
394 EXAMPLE
395 -------
396
Makefile
+1 -1
@@ -312,7 +312,7 @@ LIB_OBJS = \
312 alloc.o merge-file.o path-list.o help.o unpack-trees.o $(DIFF_OBJS) \
313 color.o wt-status.o archive-zip.o archive-tar.o shallow.o utf8.o \
314 convert.o attr.o decorate.o progress.o mailmap.o symlinks.o remote.o \
315 - transport.o bundle.o walker.o parse-options.o
315 + transport.o bundle.o walker.o parse-options.o ws.o
316
317 BUILTIN_OBJS = \
318 builtin-add.o \
builtin-apply.c
+22 -14
@@ -144,6 +144,7 @@ struct patch {
144 unsigned int old_mode, new_mode;
145 int is_new, is_delete; /* -1 = unknown, 0 = false, 1 = true */
146 int rejected;
147 + unsigned ws_rule;
148 unsigned long deflate_origlen;
149 int lines_added, lines_deleted;
150 int score;
@@ -898,7 +899,7 @@ static int find_header(char *line, unsigned long size, int *hdrsize, struct patc
899 return -1;
900 }
901
901 -static void check_whitespace(const char *line, int len)
902 +static void check_whitespace(const char *line, int len, unsigned ws_rule)
903 {
904 const char *err = "Adds trailing whitespace";
905 int seen_space = 0;
@@ -910,14 +911,14 @@ static void check_whitespace(const char *line, int len)
911 * this function. That is, an addition of an empty line would
912 * check the '+' here. Sneaky...
913 */
913 - if ((whitespace_rule & WS_TRAILING_SPACE) && isspace(line[len-2]))
914 + if ((ws_rule & WS_TRAILING_SPACE) && isspace(line[len-2]))
915 goto error;
916
917 /*
918 * Make sure that there is no space followed by a tab in
919 * indentation.
920 */
920 - if (whitespace_rule & WS_SPACE_BEFORE_TAB) {
921 + if (ws_rule & WS_SPACE_BEFORE_TAB) {
922 err = "Space in indent is followed by a tab";
923 for (i = 1; i < len; i++) {
924 if (line[i] == '\t') {
@@ -935,7 +936,7 @@ static void check_whitespace(const char *line, int len)
936 * Make sure that the indentation does not contain more than
937 * 8 spaces.
938 */
938 - if ((whitespace_rule & WS_INDENT_WITH_NON_TAB) &&
939 + if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
940 (8 < len) && !strncmp("+ ", line, 9)) {
941 err = "Indent more than 8 places with spaces";
942 goto error;
@@ -1001,7 +1002,7 @@ static int parse_fragment(char *line, unsigned long size,
1002 case '-':
1003 if (apply_in_reverse &&
1004 ws_error_action != nowarn_ws_error)
1004 - check_whitespace(line, len);
1005 + check_whitespace(line, len, patch->ws_rule);
1006 deleted++;
1007 oldlines--;
1008 trailing = 0;
@@ -1009,7 +1010,7 @@ static int parse_fragment(char *line, unsigned long size,
1010 case '+':
1011 if (!apply_in_reverse &&
1012 ws_error_action != nowarn_ws_error)
1012 - check_whitespace(line, len);
1013 + check_whitespace(line, len, patch->ws_rule);
1014 added++;
1015 newlines--;
1016 trailing = 0;
@@ -1318,6 +1319,10 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
1319 if (offset < 0)
1320 return offset;
1321
1322 + patch->ws_rule = whitespace_rule(patch->new_name
1323 + ? patch->new_name
1324 + : patch->old_name);
1325 +
1326 patchsize = parse_single_patch(buffer + offset + hdrsize,
1327 size - offset - hdrsize, patch);
1328
@@ -1568,7 +1573,8 @@ static void remove_last_line(const char **rbuf, int *rsize)
1573 *rsize = offset + 1;
1574 }
1575
1571 -static int apply_line(char *output, const char *patch, int plen)
1576 +static int apply_line(char *output, const char *patch, int plen,
1577 + unsigned ws_rule)
1578 {
1579 /*
1580 * plen is number of bytes to be copied from patch,
@@ -1593,7 +1599,7 @@ static int apply_line(char *output, const char *patch, int plen)
1599 /*
1600 * Strip trailing whitespace
1601 */
1596 - if ((whitespace_rule & WS_TRAILING_SPACE) &&
1602 + if ((ws_rule & WS_TRAILING_SPACE) &&
1603 (1 < plen && isspace(patch[plen-1]))) {
1604 if (patch[plen] == '\n')
1605 add_nl_to_tail = 1;
@@ -1610,12 +1616,12 @@ static int apply_line(char *output, const char *patch, int plen)
1616 char ch = patch[i];
1617 if (ch == '\t') {
1618 last_tab_in_indent = i;
1613 - if ((whitespace_rule & WS_SPACE_BEFORE_TAB) &&
1619 + if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
1620 0 <= last_space_in_indent)
1621 need_fix_leading_space = 1;
1622 } else if (ch == ' ') {
1623 last_space_in_indent = i;
1618 - if ((whitespace_rule & WS_INDENT_WITH_NON_TAB) &&
1624 + if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
1625 last_tab_in_indent < 0 &&
1626 8 <= i)
1627 need_fix_leading_space = 1;
@@ -1629,7 +1635,7 @@ static int apply_line(char *output, const char *patch, int plen)
1635 int consecutive_spaces = 0;
1636 int last = last_tab_in_indent + 1;
1637
1632 - if (whitespace_rule & WS_INDENT_WITH_NON_TAB) {
1638 + if (ws_rule & WS_INDENT_WITH_NON_TAB) {
1639 /* have "last" point at one past the indent */
1640 if (last_tab_in_indent < last_space_in_indent)
1641 last = last_space_in_indent + 1;
@@ -1671,7 +1677,7 @@ static int apply_line(char *output, const char *patch, int plen)
1677 }
1678
1679 static int apply_one_fragment(struct strbuf *buf, struct fragment *frag,
1674 - int inaccurate_eof)
1680 + int inaccurate_eof, unsigned ws_rule)
1681 {
1682 int match_beginning, match_end;
1683 const char *patch = frag->patch;
@@ -1730,7 +1736,7 @@ static int apply_one_fragment(struct strbuf *buf, struct fragment *frag,
1736 case '+':
1737 if (first != '+' || !no_add) {
1738 int added = apply_line(new + newsize, patch,
1733 - plen);
1739 + plen, ws_rule);
1740 newsize += added;
1741 if (first == '+' &&
1742 added == 1 && new[newsize-1] == '\n')
@@ -1953,12 +1959,14 @@ static int apply_fragments(struct strbuf *buf, struct patch *patch)
1959 {
1960 struct fragment *frag = patch->fragments;
1961 const char *name = patch->old_name ? patch->old_name : patch->new_name;
1962 + unsigned ws_rule = patch->ws_rule;
1963 + unsigned inaccurate_eof = patch->inaccurate_eof;
1964
1965 if (patch->is_binary)
1966 return apply_binary(buf, patch);
1967
1968 while (frag) {
1961 - if (apply_one_fragment(buf, frag, patch->inaccurate_eof)) {
1969 + if (apply_one_fragment(buf, frag, inaccurate_eof, ws_rule)) {
1970 error("patch failed: %s:%ld", name, frag->oldpos);
1971 if (!apply_with_reject)
1972 return -1;
cache.h
+3 -1
@@ -610,6 +610,8 @@ void shift_tree(const unsigned char *, const unsigned char *, unsigned char *, i
610 #define WS_SPACE_BEFORE_TAB 02
611 #define WS_INDENT_WITH_NON_TAB 04
612 #define WS_DEFAULT_RULE (WS_TRAILING_SPACE|WS_SPACE_BEFORE_TAB)
613 -extern unsigned whitespace_rule;
613 +extern unsigned whitespace_rule_cfg;
614 +extern unsigned whitespace_rule(const char *);
615 +extern unsigned parse_whitespace_rule(const char *);
616
617 #endif /* CACHE_H */
config.c
+1 -49
@@ -246,54 +246,6 @@ 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 - { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB },
256 -};
257 -
258 -static unsigned parse_whitespace_rule(const char *string)
259 -{
260 - unsigned rule = WS_DEFAULT_RULE;
261 -
262 - while (string) {
263 - int i;
264 - size_t len;
265 - const char *ep;
266 - int negated = 0;
267 -
268 - string = string + strspn(string, ", \t\n\r");
269 - ep = strchr(string, ',');
270 - if (!ep)
271 - len = strlen(string);
272 - else
273 - len = ep - string;
274 -
275 - if (*string == '-') {
276 - negated = 1;
277 - string++;
278 - len--;
279 - }
280 - if (!len)
281 - break;
282 - for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
283 - if (strncmp(whitespace_rule_names[i].rule_name,
284 - string, len))
285 - continue;
286 - if (negated)
287 - rule &= ~whitespace_rule_names[i].rule_bits;
288 - else
289 - rule |= whitespace_rule_names[i].rule_bits;
290 - break;
291 - }
292 - string = ep;
293 - }
294 - return rule;
295 -}
296 -
249 int git_parse_long(const char *value, long *ret)
250 {
251 if (value && *value) {
@@ -480,7 +432,7 @@ int git_default_config(const char *var, const char *value)
432 }
433
434 if (!strcmp(var, "core.whitespace")) {
483 - whitespace_rule = parse_whitespace_rule(value);
435 + whitespace_rule_cfg = parse_whitespace_rule(value);
436 return 0;
437 }
438
diff.c
+12 -7
@@ -454,6 +454,7 @@ static void diff_words_show(struct diff_words_data *diff_words)
454 struct emit_callback {
455 struct xdiff_emit_state xm;
456 int nparents, color_diff;
457 + unsigned ws_rule;
458 const char **label_path;
459 struct diff_words_data *diff_words;
460 int *found_changesp;
@@ -493,8 +494,8 @@ static void emit_line(const char *set, const char *reset, const char *line, int
494 }
495
496 static void emit_line_with_ws(int nparents,
496 - const char *set, const char *reset, const char *ws,
497 - const char *line, int len)
497 + const char *set, const char *reset, const char *ws,
498 + const char *line, int len, unsigned ws_rule)
499 {
500 int col0 = nparents;
501 int last_tab_in_indent = -1;
@@ -511,7 +512,7 @@ static void emit_line_with_ws(int nparents,
512 for (i = col0; i < len; i++) {
513 if (line[i] == '\t') {
514 last_tab_in_indent = i;
514 - if ((whitespace_rule & WS_SPACE_BEFORE_TAB) &&
515 + if ((ws_rule & WS_SPACE_BEFORE_TAB) &&
516 0 <= last_space_in_indent)
517 need_highlight_leading_space = 1;
518 }
@@ -520,7 +521,7 @@ static void emit_line_with_ws(int nparents,
521 else
522 break;
523 }
523 - if ((whitespace_rule & WS_INDENT_WITH_NON_TAB) &&
524 + if ((ws_rule & WS_INDENT_WITH_NON_TAB) &&
525 0 <= last_space_in_indent &&
526 last_tab_in_indent < 0 &&
527 8 <= (i - col0)) {
@@ -551,7 +552,7 @@ static void emit_line_with_ws(int nparents,
552 tail = len - 1;
553 if (line[tail] == '\n' && i < tail)
554 tail--;
554 - if (whitespace_rule & WS_TRAILING_SPACE) {
555 + if (ws_rule & WS_TRAILING_SPACE) {
556 while (i < tail) {
557 if (!isspace(line[tail]))
558 break;
@@ -578,7 +579,7 @@ static void emit_add_line(const char *reset, struct emit_callback *ecbdata, cons
579 emit_line(set, reset, line, len);
580 else
581 emit_line_with_ws(ecbdata->nparents, set, reset, ws,
581 - line, len);
582 + line, len, ecbdata->ws_rule);
583 }
584
585 static void fn_out_consume(void *priv, char *line, unsigned long len)
@@ -994,6 +995,7 @@ struct checkdiff_t {
995 struct xdiff_emit_state xm;
996 const char *filename;
997 int lineno, color_diff;
998 + unsigned ws_rule;
999 };
1000
1001 static void checkdiff_consume(void *priv, char *line, unsigned long len)
@@ -1029,7 +1031,8 @@ static void checkdiff_consume(void *priv, char *line, unsigned long len)
1031 if (white_space_at_end)
1032 printf("white space at end");
1033 printf(":%s ", reset);
1032 - emit_line_with_ws(1, set, reset, ws, line, len);
1034 + emit_line_with_ws(1, set, reset, ws, line, len,
1035 + data->ws_rule);
1036 }
1037
1038 data->lineno++;
@@ -1330,6 +1333,7 @@ static void builtin_diff(const char *name_a,
1333 ecbdata.label_path = lbl;
1334 ecbdata.color_diff = o->color_diff;
1335 ecbdata.found_changesp = &o->found_changes;
1336 + ecbdata.ws_rule = whitespace_rule(name_b ? name_b : name_a);
1337 xpp.flags = XDF_NEED_MINIMAL | o->xdl_opts;
1338 xecfg.ctxlen = o->context;
1339 xecfg.flags = XDL_EMIT_FUNCNAMES;
@@ -1423,6 +1427,7 @@ static void builtin_checkdiff(const char *name_a, const char *name_b,
1427 data.filename = name_b ? name_b : name_a;
1428 data.lineno = 0;
1429 data.color_diff = o->color_diff;
1430 + data.ws_rule = whitespace_rule(data.filename);
1431
1432 if (fill_mmfile(&mf1, one) < 0 || fill_mmfile(&mf2, two) < 0)
1433 die("unable to read files to diff");
environment.c
+1 -1
@@ -35,7 +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;
38 +unsigned whitespace_rule_cfg = WS_DEFAULT_RULE;
39
40 /* This is set by setup_git_dir_gently() and/or git_default_config() */
41 char *git_work_tree_cfg;
t/t4019-diff-wserror.sh
+47
@@ -45,8 +45,24 @@ test_expect_success 'without -trail' '
45
46 '
47
48 +test_expect_success 'without -trail (attribute)' '
49 +
50 + git config --unset core.whitespace
51 + echo "F whitespace=-trail" >.gitattributes
52 + git diff --color >output
53 + grep "$blue_grep" output >error
54 + grep -v "$blue_grep" output >normal
55 +
56 + grep Eight normal >/dev/null &&
57 + grep HT error >/dev/null &&
58 + grep With normal >/dev/null &&
59 + grep No normal >/dev/null
60 +
61 +'
62 +
63 test_expect_success 'without -space' '
64
65 + rm -f .gitattributes
66 git config core.whitespace -space
67 git diff --color >output
68 grep "$blue_grep" output >error
@@ -59,8 +75,24 @@ test_expect_success 'without -space' '
75
76 '
77
78 +test_expect_success 'without -space (attribute)' '
79 +
80 + git config --unset core.whitespace
81 + echo "F whitespace=-space" >.gitattributes
82 + git diff --color >output
83 + grep "$blue_grep" output >error
84 + grep -v "$blue_grep" output >normal
85 +
86 + grep Eight normal >/dev/null &&
87 + grep HT normal >/dev/null &&
88 + grep With error >/dev/null &&
89 + grep No normal >/dev/null
90 +
91 +'
92 +
93 test_expect_success 'with indent-non-tab only' '
94
95 + rm -f .gitattributes
96 git config core.whitespace indent,-trailing,-space
97 git diff --color >output
98 grep "$blue_grep" output >error
@@ -73,4 +105,19 @@ test_expect_success 'with indent-non-tab only' '
105
106 '
107
108 +test_expect_success 'with indent-non-tab only (attribute)' '
109 +
110 + git config --unset core.whitespace
111 + echo "F whitespace=indent,-trailing,-space" >.gitattributes
112 + git diff --color >output
113 + grep "$blue_grep" output >error
114 + grep -v "$blue_grep" output >normal
115 +
116 + grep Eight error >/dev/null &&
117 + grep HT normal >/dev/null &&
118 + grep With normal >/dev/null &&
119 + grep No normal >/dev/null
120 +
121 +'
122 +
123 test_done
t/t4124-apply-ws-rule.sh
+19 -1
@@ -112,6 +112,15 @@ test_expect_success 'whitespace=error-all, no rule' '
112
113 '
114
115 +test_expect_success 'whitespace=error-all, no rule (attribute)' '
116 +
117 + git config --unset core.whitespace &&
118 + echo "target -whitespace" >.gitattributes &&
119 + apply_patch --whitespace=error-all &&
120 + diff file target
121 +
122 +'
123 +
124 for t in - ''
125 do
126 case "$t" in '') tt='!' ;; *) tt= ;; esac
@@ -121,11 +130,20 @@ do
130 for i in - ''
131 do
132 case "$i" in '') ti='#' ;; *) ti= ;; esac
124 - rule=${t}trailing,${s}space,${i}indent &&
133 + rule=${t}trailing,${s}space,${i}indent
134 +
135 + rm -f .gitattributes
136 test_expect_success "rule=$rule" '
137 git config core.whitespace "$rule" &&
138 test_fix "$tt$ts$ti"
139 '
140 +
141 + test_expect_success "rule=$rule (attributes)" '
142 + git config --unset core.whitespace &&
143 + echo "target whitespace=$rule" >.gitattributes &&
144 + test_fix "$tt$ts$ti"
145 + '
146 +
147 done
148 done
149 done
ws.c new
+96
@@ -0,0 +1,96 @@
1 +/*
2 + * Whitespace rules
3 + *
4 + * Copyright (c) 2007 Junio C Hamano
5 + */
6 +
7 +#include "cache.h"
8 +#include "attr.h"
9 +
10 +static struct whitespace_rule {
11 + const char *rule_name;
12 + unsigned rule_bits;
13 +} whitespace_rule_names[] = {
14 + { "trailing-space", WS_TRAILING_SPACE },
15 + { "space-before-tab", WS_SPACE_BEFORE_TAB },
16 + { "indent-with-non-tab", WS_INDENT_WITH_NON_TAB },
17 +};
18 +
19 +unsigned parse_whitespace_rule(const char *string)
20 +{
21 + unsigned rule = WS_DEFAULT_RULE;
22 +
23 + while (string) {
24 + int i;
25 + size_t len;
26 + const char *ep;
27 + int negated = 0;
28 +
29 + string = string + strspn(string, ", \t\n\r");
30 + ep = strchr(string, ',');
31 + if (!ep)
32 + len = strlen(string);
33 + else
34 + len = ep - string;
35 +
36 + if (*string == '-') {
37 + negated = 1;
38 + string++;
39 + len--;
40 + }
41 + if (!len)
42 + break;
43 + for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++) {
44 + if (strncmp(whitespace_rule_names[i].rule_name,
45 + string, len))
46 + continue;
47 + if (negated)
48 + rule &= ~whitespace_rule_names[i].rule_bits;
49 + else
50 + rule |= whitespace_rule_names[i].rule_bits;
51 + break;
52 + }
53 + string = ep;
54 + }
55 + return rule;
56 +}
57 +
58 +static void setup_whitespace_attr_check(struct git_attr_check *check)
59 +{
60 + static struct git_attr *attr_whitespace;
61 +
62 + if (!attr_whitespace)
63 + attr_whitespace = git_attr("whitespace", 10);
64 + check[0].attr = attr_whitespace;
65 +}
66 +
67 +unsigned whitespace_rule(const char *pathname)
68 +{
69 + struct git_attr_check attr_whitespace_rule;
70 +
71 + setup_whitespace_attr_check(&attr_whitespace_rule);
72 + if (!git_checkattr(pathname, 1, &attr_whitespace_rule)) {
73 + const char *value;
74 +
75 + value = attr_whitespace_rule.value;
76 + if (ATTR_TRUE(value)) {
77 + /* true (whitespace) */
78 + unsigned all_rule = 0;
79 + int i;
80 + for (i = 0; i < ARRAY_SIZE(whitespace_rule_names); i++)
81 + all_rule |= whitespace_rule_names[i].rule_bits;
82 + return all_rule;
83 + } else if (ATTR_FALSE(value)) {
84 + /* false (-whitespace) */
85 + return 0;
86 + } else if (ATTR_UNSET(value)) {
87 + /* reset to default (!whitespace) */
88 + return whitespace_rule_cfg;
89 + } else {
90 + /* string */
91 + return parse_whitespace_rule(value);
92 + }
93 + } else {
94 + return whitespace_rule_cfg;
95 + }
96 +}