config: implement --fixed-value with --get*
The config builtin does its own regex matching of values for the --get, --get-all, and --get-regexp modes. Plumb the existing 'flags' parameter to the get_value() method so we can initialize the value-pattern argument as a fixed string instead of a regex pattern. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Derrick Stolee committed
Nov 25, 2020 at 22:12 UTC
3f1bae1dc3432acf7c586cd938e524f8d30eed31
2 files changed
+32
-5
builtin/config.c
+10
-5
@@ -14,6 +14,7 @@ static const char *const builtin_config_usage[] = {
14
15
static char *key;
16
static regex_t *key_regexp;
17
+static const char *value_pattern;
18
static regex_t *regexp;
19
static int show_keys;
20
static int omit_values;
@@ -298,6 +299,8 @@ static int collect_config(const char *key_, const char *value_, void *cb)
299
return 0;
300
if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
301
return 0;
302
+ if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
303
+ return 0;
304
if (regexp != NULL &&
305
(do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
306
return 0;
@@ -308,7 +311,7 @@ static int collect_config(const char *key_, const char *value_, void *cb)
311
return format_config(&values->items[values->nr++], key_, value_);
312
}
313
311
-static int get_value(const char *key_, const char *regex_)
314
+static int get_value(const char *key_, const char *regex_, unsigned flags)
315
{
316
int ret = CONFIG_GENERIC_ERROR;
317
struct strbuf_list values = {NULL};
@@ -345,7 +348,9 @@ static int get_value(const char *key_, const char *regex_)
348
}
349
}
350
348
- if (regex_) {
351
+ if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
352
+ value_pattern = regex_;
353
+ else if (regex_) {
354
if (regex_[0] == '!') {
355
do_not_match = 1;
356
regex_++;
@@ -890,19 +895,19 @@ int cmd_config(int argc, const char **argv, const char *prefix)
895
}
896
else if (actions == ACTION_GET) {
897
check_argc(argc, 1, 2);
893
- return get_value(argv[0], argv[1]);
898
+ return get_value(argv[0], argv[1], flags);
899
}
900
else if (actions == ACTION_GET_ALL) {
901
do_all = 1;
902
check_argc(argc, 1, 2);
898
- return get_value(argv[0], argv[1]);
903
+ return get_value(argv[0], argv[1], flags);
904
}
905
else if (actions == ACTION_GET_REGEXP) {
906
show_keys = 1;
907
use_key_regexp = 1;
908
do_all = 1;
909
check_argc(argc, 1, 2);
905
- return get_value(argv[0], argv[1]);
910
+ return get_value(argv[0], argv[1], flags);
911
}
912
else if (actions == ACTION_GET_URLMATCH) {
913
check_argc(argc, 2, 2);
t/t1300-config.sh
+22
@@ -2044,4 +2044,26 @@ test_expect_success '--fixed-value uses exact string matching' '
2044
test_cmp expect actual
2045
'
2046
2047
+test_expect_success '--get and --get-all with --fixed-value' '
2048
+ test_when_finished rm -f config &&
2049
+ META="a+b*c?d[e]f.g" &&
2050
+ git config --file=config fixed.test bogus &&
2051
+ git config --file=config --add fixed.test "$META" &&
2052
+
2053
+ git config --file=config --get fixed.test bogus &&
2054
+ test_must_fail git config --file=config --get fixed.test "$META" &&
2055
+ git config --file=config --get --fixed-value fixed.test "$META" &&
2056
+ test_must_fail git config --file=config --get --fixed-value fixed.test non-existent &&
2057
+
2058
+ git config --file=config --get-all fixed.test bogus &&
2059
+ test_must_fail git config --file=config --get-all fixed.test "$META" &&
2060
+ git config --file=config --get-all --fixed-value fixed.test "$META" &&
2061
+ test_must_fail git config --file=config --get-all --fixed-value fixed.test non-existent &&
2062
+
2063
+ git config --file=config --get-regexp fixed+ bogus &&
2064
+ test_must_fail git config --file=config --get-regexp fixed+ "$META" &&
2065
+ git config --file=config --get-regexp --fixed-value fixed+ "$META" &&
2066
+ test_must_fail git config --file=config --get-regexp --fixed-value fixed+ non-existent
2067
+'
2068
+
2069
test_done