summaryrefslogtreecommitdiff
path: root/config.c
diff options
context:
space:
mode:
authorLibravatar Johannes Schindelin <Johannes.Schindelin@gmx.de>2005-11-20 13:24:18 +0100
committerLibravatar Junio C Hamano <junkio@cox.net>2005-11-20 10:53:06 -0800
commitf98d863d2122e1b8781dfb9889df98876a26f315 (patch)
tree3b688c9ff010bd74cde0027fe63f1d225a53b0ab /config.c
parentmerge-recursive: Replace 'except:' (diff)
downloadtgif-f98d863d2122e1b8781dfb9889df98876a26f315.tar.xz
git-config-set: support selecting values by non-matching regex
Extend the regex syntax of value_regex so that prepending an exclamation mark means non-match: [core] quetzal = "Dodo" for Brainf*ck quetzal = "T. Rex" for Malbolge quetzal = "cat" You can match the third line with git-config-set --get quetzal '! for ' Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Diffstat (limited to 'config.c')
-rw-r--r--config.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/config.c b/config.c
index 697d79f536..5d237c862d 100644
--- a/config.c
+++ b/config.c
@@ -269,6 +269,7 @@ int git_config(config_fn_t fn)
static struct {
int baselen;
char* key;
+ int do_not_match;
regex_t* value_regex;
int multi_replace;
off_t offset[MAX_MATCHES];
@@ -276,13 +277,19 @@ static struct {
int seen;
} store;
+static int matches(const char* key, const char* value)
+{
+ return !strcmp(key, store.key) &&
+ (store.value_regex == NULL ||
+ (store.do_not_match ^
+ !regexec(store.value_regex, value, 0, NULL, 0)));
+}
+
static int store_aux(const char* key, const char* value)
{
switch (store.state) {
case KEY_SEEN:
- if (!strcmp(key, store.key) &&
- (store.value_regex == NULL ||
- !regexec(store.value_regex, value, 0, NULL, 0))) {
+ if (matches(key, value)) {
if (store.seen == 1 && store.multi_replace == 0) {
fprintf(stderr,
"Warning: %s has multiple values\n",
@@ -306,9 +313,7 @@ static int store_aux(const char* key, const char* value)
/* fallthru */
case SECTION_END_SEEN:
case START:
- if (!strcmp(key, store.key) &&
- (store.value_regex == NULL ||
- !regexec(store.value_regex, value, 0, NULL, 0))) {
+ if (matches(key, value)) {
store.offset[store.seen] = ftell(config_file);
store.state = KEY_SEEN;
store.seen++;
@@ -471,6 +476,12 @@ int git_config_set_multivar(const char* key, const char* value,
if (value_regex == NULL)
store.value_regex = NULL;
else {
+ if (value_regex[0] == '!') {
+ store.do_not_match = 1;
+ value_regex++;
+ } else
+ store.do_not_match = 0;
+
store.value_regex = (regex_t*)malloc(sizeof(regex_t));
if (regcomp(store.value_regex, value_regex,
REG_EXTENDED)) {