Skip to content

Commit c1063be

Browse files
peffgitster
authored andcommitted
config: avoid a funny sentinel value "a^"
Introduce CONFIG_REGEX_NONE as a more explicit sentinel value to say "we do not want to replace any existing entry" and use it in the implementation of "git config --add". Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c846664 commit c1063be

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

builtin/config.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
599599
check_argc(argc, 2, 2);
600600
value = normalize_value(argv[0], argv[1]);
601601
return git_config_set_multivar_in_file(given_config_source.file,
602-
argv[0], value, "a^", 0);
602+
argv[0], value,
603+
CONFIG_REGEX_NONE, 0);
603604
}
604605
else if (actions == ACTION_REPLACE_ALL) {
605606
check_write();

cache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,8 @@ extern int update_server_info(int);
12331233
#define CONFIG_INVALID_PATTERN 6
12341234
#define CONFIG_GENERIC_ERROR 7
12351235

1236+
#define CONFIG_REGEX_NONE ((void *)1)
1237+
12361238
struct git_config_source {
12371239
unsigned int use_stdin:1;
12381240
const char *file;

config.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,10 +1230,15 @@ static struct {
12301230

12311231
static int matches(const char *key, const char *value)
12321232
{
1233-
return !strcmp(key, store.key) &&
1234-
(store.value_regex == NULL ||
1235-
(store.do_not_match ^
1236-
(value && !regexec(store.value_regex, value, 0, NULL, 0))));
1233+
if (strcmp(key, store.key))
1234+
return 0; /* not ours */
1235+
if (!store.value_regex)
1236+
return 1; /* always matches */
1237+
if (store.value_regex == CONFIG_REGEX_NONE)
1238+
return 0; /* never matches */
1239+
1240+
return store.do_not_match ^
1241+
(value && !regexec(store.value_regex, value, 0, NULL, 0));
12371242
}
12381243

12391244
static int store_aux(const char *key, const char *value, void *cb)
@@ -1495,6 +1500,8 @@ int git_config_parse_key(const char *key, char **store_key, int *baselen_)
14951500
/*
14961501
* If value==NULL, unset in (remove from) config,
14971502
* if value_regex!=NULL, disregard key/value pairs where value does not match.
1503+
* if value_regex==CONFIG_REGEX_NONE, do not match any existing values
1504+
* (only add a new one)
14981505
* if multi_replace==0, nothing, or only one matching key/value is replaced,
14991506
* else all matching key/values (regardless how many) are removed,
15001507
* before the new pair is written.
@@ -1578,6 +1585,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
15781585

15791586
if (value_regex == NULL)
15801587
store.value_regex = NULL;
1588+
else if (value_regex == CONFIG_REGEX_NONE)
1589+
store.value_regex = CONFIG_REGEX_NONE;
15811590
else {
15821591
if (value_regex[0] == '!') {
15831592
store.do_not_match = 1;
@@ -1609,7 +1618,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
16091618
if (git_config_from_file(store_aux, config_filename, NULL)) {
16101619
error("invalid config file %s", config_filename);
16111620
free(store.key);
1612-
if (store.value_regex != NULL) {
1621+
if (store.value_regex != NULL &&
1622+
store.value_regex != CONFIG_REGEX_NONE) {
16131623
regfree(store.value_regex);
16141624
free(store.value_regex);
16151625
}
@@ -1618,7 +1628,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
16181628
}
16191629

16201630
free(store.key);
1621-
if (store.value_regex != NULL) {
1631+
if (store.value_regex != NULL &&
1632+
store.value_regex != CONFIG_REGEX_NONE) {
16221633
regfree(store.value_regex);
16231634
free(store.value_regex);
16241635
}

0 commit comments

Comments
 (0)