Skip to content

Commit c90702a

Browse files
derrickstoleegitster
authored andcommitted
config: plumb --fixed-value into config API
The git_config_set_multivar_in_file_gently() and related methods now take a 'flags' bitfield, so add a new bit representing the --fixed-value option from 'git config'. This alters the purpose of the value_pattern parameter to be an exact string match. This requires some initialization changes in git_config_set_multivar_in_file_gently() and a new strcmp() call in the matches() method. The new CONFIG_FLAGS_FIXED_VALUE flag is initialized in builtin/config.c based on the --fixed-value option, and that needs to be updated in several callers. This patch only affects some of the modes of 'git config', and the rest will be completed in the next change. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent fda4394 commit c90702a

File tree

4 files changed

+73
-5
lines changed

4 files changed

+73
-5
lines changed

builtin/config.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
633633
{
634634
int nongit = !startup_info->have_repository;
635635
char *value;
636+
int flags = 0;
636637

637638
given_config_source.file = xstrdup_or_null(getenv(CONFIG_ENVIRONMENT));
638639

@@ -800,6 +801,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
800801
error(_("--fixed-value only applies with 'value-pattern'"));
801802
usage_builtin_config();
802803
}
804+
805+
flags |= CONFIG_FLAGS_FIXED_VALUE;
803806
}
804807

805808
if (actions & PAGING_ACTIONS)
@@ -863,7 +866,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
863866
value = normalize_value(argv[0], argv[1]);
864867
UNLEAK(value);
865868
return git_config_set_multivar_in_file_gently(given_config_source.file,
866-
argv[0], value, argv[2], 0);
869+
argv[0], value, argv[2],
870+
flags);
867871
}
868872
else if (actions == ACTION_ADD) {
869873
check_write();
@@ -872,7 +876,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
872876
UNLEAK(value);
873877
return git_config_set_multivar_in_file_gently(given_config_source.file,
874878
argv[0], value,
875-
CONFIG_REGEX_NONE, 0);
879+
CONFIG_REGEX_NONE,
880+
flags);
876881
}
877882
else if (actions == ACTION_REPLACE_ALL) {
878883
check_write();
@@ -881,7 +886,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
881886
UNLEAK(value);
882887
return git_config_set_multivar_in_file_gently(given_config_source.file,
883888
argv[0], value, argv[2],
884-
CONFIG_FLAGS_MULTI_REPLACE);
889+
flags | CONFIG_FLAGS_MULTI_REPLACE);
885890
}
886891
else if (actions == ACTION_GET) {
887892
check_argc(argc, 1, 2);
@@ -908,7 +913,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
908913
check_argc(argc, 1, 2);
909914
if (argc == 2)
910915
return git_config_set_multivar_in_file_gently(given_config_source.file,
911-
argv[0], NULL, argv[1], 0);
916+
argv[0], NULL, argv[1],
917+
flags);
912918
else
913919
return git_config_set_in_file_gently(given_config_source.file,
914920
argv[0], NULL);
@@ -918,7 +924,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
918924
check_argc(argc, 1, 2);
919925
return git_config_set_multivar_in_file_gently(given_config_source.file,
920926
argv[0], NULL, argv[1],
921-
CONFIG_FLAGS_MULTI_REPLACE);
927+
flags | CONFIG_FLAGS_MULTI_REPLACE);
922928
}
923929
else if (actions == ACTION_RENAME_SECTION) {
924930
int ret;

config.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,6 +2415,7 @@ struct config_store_data {
24152415
size_t baselen;
24162416
char *key;
24172417
int do_not_match;
2418+
const char *fixed_value;
24182419
regex_t *value_pattern;
24192420
int multi_replace;
24202421
struct {
@@ -2444,6 +2445,8 @@ static int matches(const char *key, const char *value,
24442445
{
24452446
if (strcmp(key, store->key))
24462447
return 0; /* not ours */
2448+
if (store->fixed_value)
2449+
return !strcmp(store->fixed_value, value);
24472450
if (!store->value_pattern)
24482451
return 1; /* always matches */
24492452
if (store->value_pattern == CONFIG_REGEX_NONE)
@@ -2816,6 +2819,8 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
28162819
store.value_pattern = NULL;
28172820
else if (value_pattern == CONFIG_REGEX_NONE)
28182821
store.value_pattern = CONFIG_REGEX_NONE;
2822+
else if (flags & CONFIG_FLAGS_FIXED_VALUE)
2823+
store.fixed_value = value_pattern;
28192824
else {
28202825
if (value_pattern[0] == '!') {
28212826
store.do_not_match = 1;

config.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ int git_config_key_is_valid(const char *key);
269269
*/
270270
#define CONFIG_FLAGS_MULTI_REPLACE (1 << 0)
271271

272+
/*
273+
* When CONFIG_FLAGS_FIXED_VALUE is specified, match key/value pairs
274+
* by string comparison (not regex match) to the provided value_pattern
275+
* parameter.
276+
*/
277+
#define CONFIG_FLAGS_FIXED_VALUE (1 << 1)
278+
272279
int git_config_set_multivar_gently(const char *, const char *, const char *, unsigned);
273280
void git_config_set_multivar(const char *, const char *, const char *, unsigned);
274281
int git_config_set_multivar_in_file_gently(const char *, const char *, const char *, const char *, unsigned);

t/t1300-config.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,4 +1994,54 @@ test_expect_success 'refuse --fixed-value for incompatible actions' '
19941994
test_must_fail git config --file=config --fixed-value --unset-all dev.null
19951995
'
19961996

1997+
test_expect_success '--fixed-value uses exact string matching' '
1998+
test_when_finished rm -f config initial &&
1999+
META="a+b*c?d[e]f.g" &&
2000+
git config --file=initial fixed.test "$META" &&
2001+
2002+
cp initial config &&
2003+
git config --file=config fixed.test bogus "$META" &&
2004+
git config --file=config --list >actual &&
2005+
cat >expect <<-EOF &&
2006+
fixed.test=$META
2007+
fixed.test=bogus
2008+
EOF
2009+
test_cmp expect actual &&
2010+
2011+
cp initial config &&
2012+
git config --file=config --fixed-value fixed.test bogus "$META" &&
2013+
git config --file=config --list >actual &&
2014+
cat >expect <<-\EOF &&
2015+
fixed.test=bogus
2016+
EOF
2017+
test_cmp expect actual &&
2018+
2019+
cp initial config &&
2020+
test_must_fail git config --file=config --unset fixed.test "$META" &&
2021+
git config --file=config --fixed-value --unset fixed.test "$META" &&
2022+
test_must_fail git config --file=config fixed.test &&
2023+
2024+
cp initial config &&
2025+
test_must_fail git config --file=config --unset-all fixed.test "$META" &&
2026+
git config --file=config --fixed-value --unset-all fixed.test "$META" &&
2027+
test_must_fail git config --file=config fixed.test &&
2028+
2029+
cp initial config &&
2030+
git config --file=config --replace-all fixed.test bogus "$META" &&
2031+
git config --file=config --list >actual &&
2032+
cat >expect <<-EOF &&
2033+
fixed.test=$META
2034+
fixed.test=bogus
2035+
EOF
2036+
test_cmp expect actual &&
2037+
2038+
git config --file=config --fixed-value --replace-all fixed.test bogus "$META" &&
2039+
git config --file=config --list >actual &&
2040+
cat >expect <<-EOF &&
2041+
fixed.test=bogus
2042+
fixed.test=bogus
2043+
EOF
2044+
test_cmp expect actual
2045+
'
2046+
19972047
test_done

0 commit comments

Comments
 (0)