summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2020-12-08 15:11:19 -0800
committerJunio C Hamano <gitster@pobox.com>2020-12-08 15:11:19 -0800
commita10e7842abf8ec62dcd513cf74207cf2401aa2f2 (patch)
tree8f9aa39f7428df4b99985db2b132f6b43b25f291 /config.c
parent6bac6a1ef9e9c2bcf6f927e2e58e66ef39659e93 (diff)
parentc902618795eee5dbc88ded238d0a359091ab0dee (diff)
downloadgit-a10e7842abf8ec62dcd513cf74207cf2401aa2f2.tar.gz
Merge branch 'ds/config-literal-value'
Various subcommands of "git config" that takes value_regex learn the "--literal-value" option to take the value_regex option as a literal string. * ds/config-literal-value: config doc: value-pattern is not necessarily a regexp config: implement --fixed-value with --get* config: plumb --fixed-value into config API config: add --fixed-value option, un-implemented t1300: add test for --replace-all with value-pattern t1300: test "set all" mode with value-pattern config: replace 'value_regex' with 'value_pattern' config: convert multi_replace to flags
Diffstat (limited to 'config.c')
-rw-r--r--config.c75
1 files changed, 40 insertions, 35 deletions
diff --git a/config.c b/config.c
index 8f324ed3a6..1137bd73af 100644
--- a/config.c
+++ b/config.c
@@ -2415,7 +2415,8 @@ struct config_store_data {
size_t baselen;
char *key;
int do_not_match;
- regex_t *value_regex;
+ const char *fixed_value;
+ regex_t *value_pattern;
int multi_replace;
struct {
size_t begin, end;
@@ -2429,10 +2430,10 @@ struct config_store_data {
static void config_store_data_clear(struct config_store_data *store)
{
free(store->key);
- if (store->value_regex != NULL &&
- store->value_regex != CONFIG_REGEX_NONE) {
- regfree(store->value_regex);
- free(store->value_regex);
+ if (store->value_pattern != NULL &&
+ store->value_pattern != CONFIG_REGEX_NONE) {
+ regfree(store->value_pattern);
+ free(store->value_pattern);
}
free(store->parsed);
free(store->seen);
@@ -2444,13 +2445,15 @@ static int matches(const char *key, const char *value,
{
if (strcmp(key, store->key))
return 0; /* not ours */
- if (!store->value_regex)
+ if (store->fixed_value)
+ return !strcmp(store->fixed_value, value);
+ if (!store->value_pattern)
return 1; /* always matches */
- if (store->value_regex == CONFIG_REGEX_NONE)
+ if (store->value_pattern == CONFIG_REGEX_NONE)
return 0; /* never matches */
return store->do_not_match ^
- (value && !regexec(store->value_regex, value, 0, NULL, 0));
+ (value && !regexec(store->value_pattern, value, 0, NULL, 0));
}
static int store_aux_event(enum config_event_t type,
@@ -2726,12 +2729,12 @@ void git_config_set(const char *key, const char *value)
/*
* If value==NULL, unset in (remove from) config,
- * if value_regex!=NULL, disregard key/value pairs where value does not match.
- * if value_regex==CONFIG_REGEX_NONE, do not match any existing values
+ * if value_pattern!=NULL, disregard key/value pairs where value does not match.
+ * if value_pattern==CONFIG_REGEX_NONE, do not match any existing values
* (only add a new one)
- * if multi_replace==0, nothing, or only one matching key/value is replaced,
- * else all matching key/values (regardless how many) are removed,
- * before the new pair is written.
+ * if flags contains the CONFIG_FLAGS_MULTI_REPLACE flag, all matching
+ * key/values are removed before a single new pair is written. If the
+ * flag is not present, then replace only the first match.
*
* Returns 0 on success.
*
@@ -2751,8 +2754,8 @@ void git_config_set(const char *key, const char *value)
*/
int git_config_set_multivar_in_file_gently(const char *config_filename,
const char *key, const char *value,
- const char *value_regex,
- int multi_replace)
+ const char *value_pattern,
+ unsigned flags)
{
int fd = -1, in_fd = -1;
int ret;
@@ -2769,7 +2772,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
if (ret)
goto out_free;
- store.multi_replace = multi_replace;
+ store.multi_replace = (flags & CONFIG_FLAGS_MULTI_REPLACE) != 0;
if (!config_filename)
config_filename = filename_buf = git_pathdup("config");
@@ -2812,22 +2815,24 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
int i, new_line = 0;
struct config_options opts;
- if (value_regex == NULL)
- store.value_regex = NULL;
- else if (value_regex == CONFIG_REGEX_NONE)
- store.value_regex = CONFIG_REGEX_NONE;
+ if (value_pattern == NULL)
+ store.value_pattern = NULL;
+ else if (value_pattern == CONFIG_REGEX_NONE)
+ store.value_pattern = CONFIG_REGEX_NONE;
+ else if (flags & CONFIG_FLAGS_FIXED_VALUE)
+ store.fixed_value = value_pattern;
else {
- if (value_regex[0] == '!') {
+ if (value_pattern[0] == '!') {
store.do_not_match = 1;
- value_regex++;
+ value_pattern++;
} else
store.do_not_match = 0;
- store.value_regex = (regex_t*)xmalloc(sizeof(regex_t));
- if (regcomp(store.value_regex, value_regex,
+ store.value_pattern = (regex_t*)xmalloc(sizeof(regex_t));
+ if (regcomp(store.value_pattern, value_pattern,
REG_EXTENDED)) {
- error(_("invalid pattern: %s"), value_regex);
- FREE_AND_NULL(store.value_regex);
+ error(_("invalid pattern: %s"), value_pattern);
+ FREE_AND_NULL(store.value_pattern);
ret = CONFIG_INVALID_PATTERN;
goto out_free;
}
@@ -2858,7 +2863,7 @@ int git_config_set_multivar_in_file_gently(const char *config_filename,
/* if nothing to unset, or too many matches, error out */
if ((store.seen_nr == 0 && value == NULL) ||
- (store.seen_nr > 1 && multi_replace == 0)) {
+ (store.seen_nr > 1 && !store.multi_replace)) {
ret = CONFIG_NOTHING_SET;
goto out_free;
}
@@ -2997,10 +3002,10 @@ write_err_out:
void git_config_set_multivar_in_file(const char *config_filename,
const char *key, const char *value,
- const char *value_regex, int multi_replace)
+ const char *value_pattern, unsigned flags)
{
if (!git_config_set_multivar_in_file_gently(config_filename, key, value,
- value_regex, multi_replace))
+ value_pattern, flags))
return;
if (value)
die(_("could not set '%s' to '%s'"), key, value);
@@ -3009,17 +3014,17 @@ void git_config_set_multivar_in_file(const char *config_filename,
}
int git_config_set_multivar_gently(const char *key, const char *value,
- const char *value_regex, int multi_replace)
+ const char *value_pattern, unsigned flags)
{
- return git_config_set_multivar_in_file_gently(NULL, key, value, value_regex,
- multi_replace);
+ return git_config_set_multivar_in_file_gently(NULL, key, value, value_pattern,
+ flags);
}
void git_config_set_multivar(const char *key, const char *value,
- const char *value_regex, int multi_replace)
+ const char *value_pattern, unsigned flags)
{
- git_config_set_multivar_in_file(NULL, key, value, value_regex,
- multi_replace);
+ git_config_set_multivar_in_file(NULL, key, value, value_pattern,
+ flags);
}
static int section_name_match (const char *buf, const char *name)