color-words: Support diff.wordregex config option
When diff is invoked with --color-words (w/o =regex), use the regular expression the user has configured as diff.wordregex. diff drivers configured via attributes take precedence over the diff.wordregex-words setting. If the user wants to change them, they have their own configuration variables. Signed-off-by: Boyd Stephen Smith Jr <bss@iguanasuicide.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Boyd Stephen Smith Jr committed
Jan 20, 2009 at 21:46 UTC
98a4d87b87e9846eafd21ba232cc2b7ba3f718fc
4 files changed
+58
-5
Documentation/config.txt
+6
@@ -639,6 +639,12 @@ diff.suppress-blank-empty::
639
A boolean to inhibit the standard behavior of printing a space
640
before each empty output line. Defaults to false.
641
642
+diff.wordregex::
643
+ A POSIX Extended Regular Expression used to determine what is a "word"
644
+ when performing word-by-word difference calculations. Character
645
+ sequences that match the regular expression are "words", all other
646
+ characters are *ignorable* whitespace.
647
+
648
fetch.unpackLimit::
649
If the number of objects fetched over the git native
650
transfer is below this
Documentation/diff-options.txt
+4
-3
@@ -103,9 +103,10 @@ expression to make sure that it matches all non-whitespace characters.
103
A match that contains a newline is silently truncated(!) at the
104
newline.
105
+
106
-The regex can also be set via a diff driver, see
107
-linkgit:gitattributes[1]; giving it explicitly overrides any diff
108
-driver setting.
106
+The regex can also be set via a diff driver or configuration option, see
107
+linkgit:gitattributes[1] or linkgit:git-config[1]. Giving it explicitly
108
+overrides any diff driver or configuration setting. Diff drivers
109
+override configuration settings.
110
111
--no-renames::
112
Turn off rename detection, even when the configuration
diff.c
+5
@@ -23,6 +23,7 @@ static int diff_detect_rename_default;
23
static int diff_rename_limit_default = 200;
24
static int diff_suppress_blank_empty;
25
int diff_use_color_default = -1;
26
+static const char *diff_word_regex_cfg;
27
static const char *external_diff_cmd_cfg;
28
int diff_auto_refresh_index = 1;
29
static int diff_mnemonic_prefix;
@@ -92,6 +93,8 @@ int git_diff_ui_config(const char *var, const char *value, void *cb)
93
}
94
if (!strcmp(var, "diff.external"))
95
return git_config_string(&external_diff_cmd_cfg, var, value);
96
+ if (!strcmp(var, "diff.wordregex"))
97
+ return git_config_string(&diff_word_regex_cfg, var, value);
98
99
return git_diff_basic_config(var, value, cb);
100
}
@@ -1550,6 +1553,8 @@ static void builtin_diff(const char *name_a,
1553
o->word_regex = userdiff_word_regex(one);
1554
if (!o->word_regex)
1555
o->word_regex = userdiff_word_regex(two);
1556
+ if (!o->word_regex)
1557
+ o->word_regex = diff_word_regex_cfg;
1558
if (o->word_regex) {
1559
ecbdata.diff_words->word_regex = (regex_t *)
1560
xmalloc(sizeof(regex_t));
t/t4034-diff-words.sh
+43
-2
@@ -77,6 +77,7 @@ a = b + c<RESET>
77
78
<GREEN>aeff = aeff * ( aaa<RESET> )
79
EOF
80
+cp expect expect.letter-runs-are-words
81
82
test_expect_success 'word diff with a regular expression' '
83
@@ -92,7 +93,7 @@ post diff=testdriver
93
EOF
94
'
95
95
-test_expect_success 'option overrides default' '
96
+test_expect_success 'option overrides .gitattributes' '
97
98
word_diff --color-words="[a-z]+"
99
@@ -112,13 +113,53 @@ a = b + c<RESET>
113
114
<GREEN>aeff = aeff * ( aaa )<RESET>
115
EOF
116
+cp expect expect.non-whitespace-is-word
117
116
-test_expect_success 'use default supplied by driver' '
118
+test_expect_success 'use regex supplied by driver' '
119
120
word_diff --color-words
121
122
'
123
124
+test_expect_success 'set diff.wordregex option' '
125
+ git config diff.wordregex "[[:alnum:]]+"
126
+'
127
+
128
+cp expect.letter-runs-are-words expect
129
+
130
+test_expect_success 'command-line overrides config' '
131
+ word_diff --color-words="[a-z]+"
132
+'
133
+
134
+cp expect.non-whitespace-is-word expect
135
+
136
+test_expect_success '.gitattributes override config' '
137
+ word_diff --color-words
138
+'
139
+
140
+test_expect_success 'remove diff driver regex' '
141
+ git config --unset diff.testdriver.wordregex
142
+'
143
+
144
+cat > expect <<\EOF
145
+<WHITE>diff --git a/pre b/post<RESET>
146
+<WHITE>index 330b04f..5ed8eff 100644<RESET>
147
+<WHITE>--- a/pre<RESET>
148
+<WHITE>+++ b/post<RESET>
149
+<BROWN>@@ -1,3 +1,7 @@<RESET>
150
+h(4),<GREEN>hh[44<RESET>]
151
+<RESET>
152
+a = b + c<RESET>
153
+
154
+<GREEN>aa = a<RESET>
155
+
156
+<GREEN>aeff = aeff * ( aaa<RESET> )
157
+EOF
158
+
159
+test_expect_success 'use configured regex' '
160
+ word_diff --color-words
161
+'
162
+
163
echo 'aaa (aaa)' > pre
164
echo 'aaa (aaa) aaa' > post
165