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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Aug 19, 2014 at 02:20 UTC
c1063be2a3e14ac042db4fe9b9bcea8f2768886a
3 files changed
+21
-7
builtin/config.c
+2
-1
@@ -599,7 +599,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
599
check_argc(argc, 2, 2);
600
value = normalize_value(argv[0], argv[1]);
601
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);
604
}
605
else if (actions == ACTION_REPLACE_ALL) {
606
check_write();
cache.h
+2
@@ -1233,6 +1233,8 @@ extern int update_server_info(int);
1233
#define CONFIG_INVALID_PATTERN 6
1234
#define CONFIG_GENERIC_ERROR 7
1235
1236
+#define CONFIG_REGEX_NONE ((void *)1)
1237
+
1238
struct git_config_source {
1239
unsigned int use_stdin:1;
1240
const char *file;
config.c
+17
-6
@@ -1230,10 +1230,15 @@ static struct {
1230
1231
static int matches(const char *key, const char *value)
1232
{
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));
1242
}
1243
1244
static int store_aux(const char *key, const char *value, void *cb)
@@ -1495,6 +1500,8 @@ out_free_ret_1:
1500
/*
1501
* If value==NULL, unset in (remove from) config,
1502
* 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)
1505
* if multi_replace==0, nothing, or only one matching key/value is replaced,
1506
* else all matching key/values (regardless how many) are removed,
1507
* before the new pair is written.
@@ -1578,6 +1585,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
1585
1586
if (value_regex == NULL)
1587
store.value_regex = NULL;
1588
+ else if (value_regex == CONFIG_REGEX_NONE)
1589
+ store.value_regex = CONFIG_REGEX_NONE;
1590
else {
1591
if (value_regex[0] == '!') {
1592
store.do_not_match = 1;
@@ -1609,7 +1618,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
1618
if (git_config_from_file(store_aux, config_filename, NULL)) {
1619
error("invalid config file %s", config_filename);
1620
free(store.key);
1612
- if (store.value_regex != NULL) {
1621
+ if (store.value_regex != NULL &&
1622
+ store.value_regex != CONFIG_REGEX_NONE) {
1623
regfree(store.value_regex);
1624
free(store.value_regex);
1625
}
@@ -1618,7 +1628,8 @@ int git_config_set_multivar_in_file(const char *config_filename,
1628
}
1629
1630
free(store.key);
1621
- if (store.value_regex != NULL) {
1631
+ if (store.value_regex != NULL &&
1632
+ store.value_regex != CONFIG_REGEX_NONE) {
1633
regfree(store.value_regex);
1634
free(store.value_regex);
1635
}