Skip to content

Commit 3f1bae1

Browse files
derrickstoleegitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c90702a commit 3f1bae1

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

builtin/config.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ static const char *const builtin_config_usage[] = {
1414

1515
static char *key;
1616
static regex_t *key_regexp;
17+
static const char *value_pattern;
1718
static regex_t *regexp;
1819
static int show_keys;
1920
static int omit_values;
@@ -298,6 +299,8 @@ static int collect_config(const char *key_, const char *value_, void *cb)
298299
return 0;
299300
if (use_key_regexp && regexec(key_regexp, key_, 0, NULL, 0))
300301
return 0;
302+
if (fixed_value && strcmp(value_pattern, (value_?value_:"")))
303+
return 0;
301304
if (regexp != NULL &&
302305
(do_not_match ^ !!regexec(regexp, (value_?value_:""), 0, NULL, 0)))
303306
return 0;
@@ -308,7 +311,7 @@ static int collect_config(const char *key_, const char *value_, void *cb)
308311
return format_config(&values->items[values->nr++], key_, value_);
309312
}
310313

311-
static int get_value(const char *key_, const char *regex_)
314+
static int get_value(const char *key_, const char *regex_, unsigned flags)
312315
{
313316
int ret = CONFIG_GENERIC_ERROR;
314317
struct strbuf_list values = {NULL};
@@ -345,7 +348,9 @@ static int get_value(const char *key_, const char *regex_)
345348
}
346349
}
347350

348-
if (regex_) {
351+
if (regex_ && (flags & CONFIG_FLAGS_FIXED_VALUE))
352+
value_pattern = regex_;
353+
else if (regex_) {
349354
if (regex_[0] == '!') {
350355
do_not_match = 1;
351356
regex_++;
@@ -890,19 +895,19 @@ int cmd_config(int argc, const char **argv, const char *prefix)
890895
}
891896
else if (actions == ACTION_GET) {
892897
check_argc(argc, 1, 2);
893-
return get_value(argv[0], argv[1]);
898+
return get_value(argv[0], argv[1], flags);
894899
}
895900
else if (actions == ACTION_GET_ALL) {
896901
do_all = 1;
897902
check_argc(argc, 1, 2);
898-
return get_value(argv[0], argv[1]);
903+
return get_value(argv[0], argv[1], flags);
899904
}
900905
else if (actions == ACTION_GET_REGEXP) {
901906
show_keys = 1;
902907
use_key_regexp = 1;
903908
do_all = 1;
904909
check_argc(argc, 1, 2);
905-
return get_value(argv[0], argv[1]);
910+
return get_value(argv[0], argv[1], flags);
906911
}
907912
else if (actions == ACTION_GET_URLMATCH) {
908913
check_argc(argc, 2, 2);

t/t1300-config.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2044,4 +2044,26 @@ test_expect_success '--fixed-value uses exact string matching' '
20442044
test_cmp expect actual
20452045
'
20462046

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+
20472069
test_done

0 commit comments

Comments
 (0)