git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH v9 0/3] git config cache & special querying api utilizing the cache
@ 2014-07-15 14:29 Tanay Abhra
  2014-07-15 14:29 ` [PATCH v9 1/2] add `config_set` API for caching config-like files Tanay Abhra
  2014-07-15 14:29 ` [PATCH v9 2/2] test-config: add tests for the config_set API Tanay Abhra
  0 siblings, 2 replies; 26+ messages in thread
From: Tanay Abhra @ 2014-07-15 14:29 UTC (permalink / raw
  To: git; +Cc: Tanay Abhra, Ramkumar Ramachandra, Matthieu Moy, Junio C Hamano

Hi,

[PATCH v9]: Applied most of the review comments mentioned in [11]. Mostly asthetic changes.
	test-config now clears the config_set before exiting. Most of the tests now use the
	check-config function. check_config_init() now handles return values correctly.
	Diff between v8 and v9 is at the bottom. Thanks to Junio and Matthieu for the review.

[PATCH V8]: Moved the contents of config-set.c to config.c for future convenience. Reverted
	test 'find value with misspelled key' to the one in v5. See [10] for the discussion.

[PATCH V7]: Style nits and a broken && chain corrected in `t/t1308-config-set.sh`. See
	[9] for the nits.

[PATCH V6]: Style nits and mistakes corrected. Diff between v6 and v5[8] is at the bottom.
		Thanks to Matthieu, Ramsay and Ram for their suggestions.

[PATCH V5]: `config_set` now uses a single hashmap. Corrected style nits raised in
			the thread[7]. Thanks to Junio and Matthieu for their suggestions.

[PATCH v4]: Introduced `config_set` construct which points to a ordered set of
	config-files cached as hashmaps. Added relevant API functions. For more
	details see the documentation. Rewrote the git_config_get* family to use
	`config_set` internally. Added tests for both config_set API and git_config_get
	family. Added type specific API functions which parses the found value and
	converts it into a specific type.
	Most of the changes implemented are the result of discussion in [6].
	Thanks to Eric, Ramsay, Junio, Matthieu & Karsten for their suggestions
	and review.

[PATCH v3]: Added flag for NULL values that were causing segfaults in some cases.
	Added test-config for usage examples.
	Minor changes and corrections. Refer to discussion thread[5] for more details.
	Thanks to Matthieu, Jeff and Junio for their valuable suggestions.

[PATCH v2]:Changed the string_list to a struct instead of pointer to a struct.
	Added string-list initilization functions.
	Minor mistakes corrected acoording to review comments[4]. Thanks to
	Eric and Matthieu for their review.

[PATCH V1]:Most of the invaluable suggestions by Eric Sunshine, Torsten Bogershausen and
	Jeff King has been implemented[1]. Complete rewrite of config_cache*() family
	using git_config() as hook as suggested by Jeff. Thanks for the review.

[RFC V2]: Improved according to the suggestions by Eric Sunshine and Torsten Bogershausen.
	Added cache invalidation when config file is changed.[2]
	I am using git_config_set_multivar_in_file() as an update hook.

This is patch is for this year's GSoC. My project is
"Git Config API improvements". The link of my proposal is appended below [3].

The aim of this patch series is to generate a cache for querying values from
the config files in a non-callback manner as the current method reads and
parses the config files every time a value is queried for.

The cache is generated from hooking the update_cache function to the current
parsing and callback mechanism in config.c. It is implemented as an hashmap
using the hashmap-api with variables and its corresponding values list as
its members. The values in the list are sorted in order of increasing priority.
The cache is initialised the first time when any of the new query functions is
called. It is invalidated by using git_config_set_multivar_in_file() as an
update hook.

We add two new functions to the config-api, git_config_get_string() and
git_config_get_string_multi() for querying in a non callback manner from
the cache.

[1] http://marc.info/?t=140172066200006&r=1&w=2
[2] http://git.661346.n2.nabble.com/RFC-PATCH-0-2-Git-config-cache-amp-special-querying-api-utilizing-the-cache-td7611691.html
[3] https://drive.google.com/file/d/0B4suZ-aHqDcnSUZJRXVTTnZUN1E/edit?usp=sharing
[4] http://thread.gmane.org/gmane.comp.version-control.git/251073/focus=251369
[5] http://thread.gmane.org/gmane.comp.version-control.git/251704/
[6] http://thread.gmane.org/gmane.comp.version-control.git/252329/
[7] http://marc.info/?t=140428115200001&r=1&w=2
[8] http://article.gmane.org/gmane.comp.version-control.git/252942/
[9] http://thread.gmane.org/gmane.comp.version-control.git/252959/
[10] http://article.gmane.org/gmane.comp.version-control.git/253113
[11] http://thread.gmane.org/gmane.comp.version-control.git/253248


Tanay Abhra (2):
  config set
  test-config

 .gitignore                             |   1 +
 Documentation/technical/api-config.txt | 135 ++++++++++++++++
 Makefile                               |   1 +
 cache.h                                |  31 ++++
 config.c                               | 276 +++++++++++++++++++++++++++++++++
 t/t1308-config-set.sh                  | 212 +++++++++++++++++++++++++
 test-config.c                          | 140 +++++++++++++++++
 7 files changed, 796 insertions(+)
 create mode 100755 t/t1308-config-set.sh
 create mode 100644 test-config.c

-- 
1.9.0.GIT

-- 8< --
diff --git a/Documentation/technical/api-config.txt b/Documentation/technical/api-config.txt
index bb43830..48255a2 100644
--- a/Documentation/technical/api-config.txt
+++ b/Documentation/technical/api-config.txt
@@ -241,7 +241,8 @@ Configset API provides functions for the above mentioned work flow, including:
 `int git_configset_add_file(struct config_set *cs, const char *filename)`::
 
 	Parses the file and adds the variable-value pairs to the `config_set`,
-	dies if there is an error in parsing the file.
+	dies if there is an error in parsing the file. Returns 0 on success, or
+	-1 if the file doesnot exist or cannot be read.
 
 `int git_configset_get_value(struct config_set *cs, const char *key, const char **value)`::
 
diff --git a/config.c b/config.c
index aa58275..89e2d67 100644
--- a/config.c
+++ b/config.c
@@ -35,7 +35,7 @@ struct config_source {
 	long (*do_ftell)(struct config_source *c);
 };
 
-struct config_hash_entry {
+struct config_set_element {
 	struct hashmap_entry ent;
 	char *key;
 	struct string_list value_list;
@@ -1227,48 +1227,10 @@ int git_config(config_fn_t fn, void *data)
 	return git_config_with_options(fn, data, NULL, 1);
 }
 
-static int config_hash_add_value(const char *, const char *, struct hashmap *);
-
-static int config_hash_entry_cmp(const struct config_hash_entry *e1,
-				 const struct config_hash_entry *e2, const void *unused)
-{
-	return strcmp(e1->key, e2->key);
-}
-
-static void configset_init(struct config_set *cs)
-{
-	if (!cs->hash_initialized) {
-		hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_hash_entry_cmp, 0);
-		cs->hash_initialized = 1;
-	}
-}
-
-static int config_hash_callback(const char *key, const char *value, void *cb)
-{
-	struct config_set *cs = cb;
-	config_hash_add_value(key, value, &cs->config_hash);
-	return 0;
-}
-
-int git_configset_add_file(struct config_set *cs, const char *filename)
-{
-	int ret = 0;
-	configset_init(cs);
-	ret = git_config_from_file(config_hash_callback, filename, cs);
-	if (!ret)
-		return 0;
-	else {
-		hashmap_free(&cs->config_hash, 1);
-		cs->hash_initialized = 0;
-		return -1;
-	}
-}
-
-static struct config_hash_entry *config_hash_find_entry(const char *key,
-							struct hashmap *config_hash)
+static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
 {
-	struct config_hash_entry k;
-	struct config_hash_entry *found_entry;
+	struct config_set_element k;
+	struct config_set_element *found_entry;
 	char *normalized_key;
 	int ret;
 	/*
@@ -1282,21 +1244,21 @@ static struct config_hash_entry *config_hash_find_entry(const char *key,
 
 	hashmap_entry_init(&k, strhash(normalized_key));
 	k.key = normalized_key;
-	found_entry = hashmap_get(config_hash, &k, NULL);
+	found_entry = hashmap_get(&cs->config_hash, &k, NULL);
 	free(normalized_key);
 	return found_entry;
 }
 
-static struct string_list *configset_get_list(const char *key, struct config_set *cs)
+const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
 {
-	struct config_hash_entry *e = config_hash_find_entry(key, &cs->config_hash);
+	struct config_set_element *e = configset_find_element(cs, key);
 	return e ? &e->value_list : NULL;
 }
 
-static int config_hash_add_value(const char *key, const char *value, struct hashmap *config_hash)
+static int configset_add_value(struct config_set *cs, const char *key, const char *value)
 {
-	struct config_hash_entry *e;
-	e = config_hash_find_entry(key, config_hash);
+	struct config_set_element *e;
+	e = configset_find_element(cs, key);
 	/*
 	 * Since the keys are being fed by git_config*() callback mechanism, they
 	 * are already normalized. So simply add them without any further munging.
@@ -1307,43 +1269,28 @@ static int config_hash_add_value(const char *key, const char *value, struct hash
 		e->key = xstrdup(key);
 		memset(&e->value_list, 0, sizeof(e->value_list));
 		e->value_list.strdup_strings = 1;
-		hashmap_add(config_hash, e);
+		hashmap_add(&cs->config_hash, e);
 	}
 	string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
 
 	return 0;
 }
 
-void git_configset_init(struct config_set *cs)
-{
-	cs->hash_initialized = 0;
-}
-
-int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
+static int config_set_element_cmp(const struct config_set_element *e1,
+				 const struct config_set_element *e2, const void *unused)
 {
-	struct string_list *values = NULL;
-	/*
-	 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
-	 * queried key in the files of the configset, the value returned will be the last
-	 * value in the value list for that key.
-	 */
-	values = configset_get_list(key, cs);
-
-	if (!values)
-		return 1;
-	assert(values->nr > 0);
-	*value = values->items[values->nr - 1].string;
-	return 0;
+	return strcmp(e1->key, e2->key);
 }
 
-const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
+void git_configset_init(struct config_set *cs)
 {
-	return configset_get_list(key, cs);
+	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
+	cs->hash_initialized = 1;
 }
 
 void git_configset_clear(struct config_set *cs)
 {
-	struct config_hash_entry *entry;
+	struct config_set_element *entry;
 	struct hashmap_iter iter;
 	if (!cs->hash_initialized)
 		return;
@@ -1354,6 +1301,43 @@ void git_configset_clear(struct config_set *cs)
 		string_list_clear(&entry->value_list, 0);
 	}
 	hashmap_free(&cs->config_hash, 1);
+	cs->hash_initialized = 0;
+}
+
+static int config_hash_callback(const char *key, const char *value, void *cb)
+{
+	struct config_set *cs = cb;
+	configset_add_value(cs, key, value);
+	return 0;
+}
+
+int git_configset_add_file(struct config_set *cs, const char *filename)
+{
+	int ret = 0;
+	ret = git_config_from_file(config_hash_callback, filename, cs);
+	if (!ret)
+		return 0;
+	else {
+		git_configset_clear(cs);
+		return -1;
+	}
+}
+
+int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
+{
+	const struct string_list *values = NULL;
+	/*
+	 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
+	 * queried key in the files of the configset, the value returned will be the last
+	 * value in the value list for that key.
+	 */
+	values = git_configset_get_value_multi(cs, key);
+
+	if (!values)
+		return 1;
+	assert(values->nr > 0);
+	*value = values->items[values->nr - 1].string;
+	return 0;
 }
 
 int git_configset_get_string(struct config_set *cs, const char *key, const char **dest)
@@ -1427,20 +1411,24 @@ int git_configset_get_pathname(struct config_set *cs, const char *key, const cha
 		return 1;
 }
 
-static int git_config_check_init(void)
+static void git_config_check_init(void)
 {
 	int ret = 0;
 	if (the_config_set.hash_initialized)
-		return 0;
-	configset_init(&the_config_set);
+		return;
+	git_configset_init(&the_config_set);
 	ret = git_config(config_hash_callback, &the_config_set);
 	if (ret >= 0)
-		return 0;
-	else {
-		hashmap_free(&the_config_set.config_hash, 1);
-		the_config_set.hash_initialized = 0;
-		return -1;
-	}
+		return;
+	else
+		die("Unknown error when parsing one of the configuration files.");
+}
+
+void git_config_clear(void)
+{
+	if (!the_config_set.hash_initialized)
+		return;
+	git_configset_clear(&the_config_set);
 }
 
 int git_config_get_value(const char *key, const char **value)
@@ -1455,14 +1443,6 @@ const struct string_list *git_config_get_value_multi(const char *key)
 	return git_configset_get_value_multi(&the_config_set, key);
 }
 
-void git_config_clear(void)
-{
-	if (!the_config_set.hash_initialized)
-		return;
-	git_configset_clear(&the_config_set);
-	the_config_set.hash_initialized = 0;
-}
-
 int git_config_get_string(const char *key, const char **dest)
 {
 	git_config_check_init();
diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
index 87a29f1..94085eb 100755
--- a/t/t1308-config-set.sh
+++ b/t/t1308-config-set.sh
@@ -4,17 +4,33 @@ test_description='Test git config-set API in different settings'
 
 . ./test-lib.sh
 
-# 'check section.key value' verifies that the entry for section.key is
-# 'value'
-check() {
-	echo "$2" >expected &&
-	test-config get_value "$1" >actual 2>&1 &&
-	test_cmp expected actual
+# 'check_config get_* section.key value' verifies that the entry for
+# section.key is 'value'
+check_config () {
+	case "$1" in
+	expect_code)
+		if [ "$#" -lt 5 ];
+		then
+			>expect
+		else
+			printf "%s\n" "$5" >expect
+		fi &&
+		test_expect_code "$2" test-config "$3" "$4" >actual
+		;;
+	*)
+		op=$1 key=$2 &&
+		shift &&
+		shift &&
+		printf "%s\n" "$@" >expect &&
+		test-config "$op" "$key" >actual
+		;;
+	esac &&
+	test_cmp expect actual
 }
 
 test_expect_success 'setup default config' '
-	cat >.git/config << EOF
-	[core]
+	cat >.git/config <<\EOF
+	[case]
 		penguin = very blue
 		Movie = BadPhysics
 		UPPERCASE = true
@@ -35,7 +51,7 @@ test_expect_success 'setup default config' '
 		hi = upper-case
 	[my "foo bar"]
 		hi = lower-case
-	[core]
+	[case]
 		baz = bat
 		baz = hask
 	[lamb]
@@ -51,78 +67,74 @@ test_expect_success 'setup default config' '
 '
 
 test_expect_success 'get value for a simple key' '
-	check core.penguin "very blue"
+	check_config get_value case.penguin "very blue"
 '
 
 test_expect_success 'get value for a key with value as an empty string' '
-	check core.my ""
+	check_config get_value case.my ""
 '
 
 test_expect_success 'get value for a key with value as NULL' '
-	check core.foo "(NULL)"
+	check_config get_value case.foo "(NULL)"
 '
 
 test_expect_success 'upper case key' '
-	check core.UPPERCASE "true"
+	check_config get_value case.UPPERCASE "true" &&
+	check_config get_value case.uppercase "true"
 '
 
 test_expect_success 'mixed case key' '
-	check core.MixedCase "true"
+	check_config get_value case.MixedCase "true" &&
+	check_config get_value case.MIXEDCASE "true" &&
+	check_config get_value case.mixedcase "true"
 '
 
 test_expect_success 'key and value with mixed case' '
-	check core.Movie "BadPhysics"
+	check_config get_value case.Movie "BadPhysics"
 '
 
 test_expect_success 'key with case sensitive subsection' '
-	check "my.Foo bAr.hi" "mixed-case" &&
-	check "my.FOO BAR.hi" "upper-case" &&
-	check "my.foo bar.hi" "lower-case"
+	check_config get_value "my.Foo bAr.hi" "mixed-case" &&
+	check_config get_value "my.FOO BAR.hi" "upper-case" &&
+	check_config get_value "my.foo bar.hi" "lower-case"
 '
 
 test_expect_success 'key with case insensitive section header' '
-	check cores.baz "ball" &&
-	check Cores.baz "ball" &&
-	check CORES.baz "ball" &&
-	check coreS.baz "ball"
+	check_config get_value cores.baz "ball" &&
+	check_config get_value Cores.baz "ball" &&
+	check_config get_value CORES.baz "ball" &&
+	check_config get_value coreS.baz "ball"
+'
+
+test_expect_success 'key with case insensitive section header & variable' '
+	check_config get_value CORES.BAZ "ball" &&
+	check_config get_value cores.baz "ball" &&
+	check_config get_value cores.BaZ "ball" &&
+	check_config get_value cOreS.bAz "ball"
 '
 
 test_expect_success 'find value with misspelled key' '
-	echo "Value not found for \"my.fOo Bar.hi\"" >expect &&
-	test_must_fail test-config get_value "my.fOo Bar.hi" >actual &&
-	test_cmp expect actual
+	check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\""
 '
 
 test_expect_success 'find value with the highest priority' '
-	check core.baz "hask"
+	check_config get_value case.baz "hask"
 '
 
 test_expect_success 'find integer value for a key' '
-	echo 65 >expect &&
-	test-config get_int lamb.chop >actual &&
-	test_cmp expect actual
+	check_config get_int lamb.chop 65
 '
 
 test_expect_success 'find integer if value is non parse-able' '
-	echo 65 >expect &&
-	test_must_fail test-config get_int lamb.head >actual &&
-	test_must_fail test_cmp expect actual
+	check_config expect_code 128 get_int lamb.head
 '
 
 test_expect_success 'find bool value for the entered key' '
-	cat >expect <<-\EOF &&
-	1
-	0
-	1
-	1
-	1
-	EOF
-	test-config get_bool goat.head >actual &&
-	test-config get_bool goat.skin >>actual &&
-	test-config get_bool goat.nose >>actual &&
-	test-config get_bool goat.horns >>actual &&
-	test-config get_bool goat.legs >>actual &&
-	test_cmp expect actual
+	check_config get_bool goat.head 1 &&
+	check_config get_bool goat.skin 0 &&
+	check_config get_bool goat.nose 1 &&
+	check_config get_bool goat.horns 1 &&
+	check_config get_bool goat.legs 1
 '
 
 test_expect_success 'find multiple values' '
@@ -131,17 +143,17 @@ test_expect_success 'find multiple values' '
 	bat
 	hask
 	EOF
-	test-config get_value_multi "core.baz">actual &&
+	test-config get_value_multi "case.baz">actual &&
 	test_cmp expect actual
 '
 
 test_expect_success 'find value from a configset' '
 	cat >config2 <<-\EOF &&
-	[core]
+	[case]
 		baz = lama
 	[my]
 		new = silk
-	[core]
+	[case]
 		baz = ball
 	EOF
 	echo silk >expect &&
@@ -151,7 +163,7 @@ test_expect_success 'find value from a configset' '
 
 test_expect_success 'find value with highest priority from a configset' '
 	echo hask > expect &&
-	test-config configset_get_value core.baz config2 .git/config  >actual &&
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
 	test_cmp expect actual
 '
 
@@ -163,7 +175,37 @@ test_expect_success 'find value_list for a key from a configset' '
 	lama
 	ball
 	EOF
-	test-config configset_get_value core.baz config2 .git/config  >actual &&
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-existant files' '
+	echo "Error reading configuration file non-existant-file." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar non-existant-file 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-accessible  files' '
+	chmod -r .git/config &&
+	test_when_finished "chmod +r .git/config" &&
+	echo "Error reading configuration file .git/config." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in default config files' '
+	cp .git/config .git/config.old &&
+	test_when_finished "mv .git/config.old .git/config" &&
+	echo "[" >> .git/config &&
+	echo "fatal: bad config file line 35 in .git/config" >expect &&
+	test_expect_code 128 test-config get_value foo.bar 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in custom config files' '
+	echo "[" >> syntax-error &&
+	echo "fatal: bad config file line 1 in syntax-error" >expect &&
+	test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
 	test_cmp expect actual
 '
 
diff --git a/test-config.c b/test-config.c
index dc313c2..cad35f4 100644
--- a/test-config.c
+++ b/test-config.c
@@ -41,17 +41,17 @@ int main(int argc, char **argv)
 
 	if (argc < 2) {
 		fprintf(stderr, "Please, provide a command name on the command-line\n");
-		return 1;
+		goto exit1;
 	} else if (argc == 3 && !strcmp(argv[1], "get_value")) {
 		if (!git_config_get_value(argv[2], &v)) {
 			if (!v)
 				printf("(NULL)\n");
 			else
 				printf("%s\n", v);
-			return 0;
+			goto exit0;
 		} else {
 			printf("Value not found for \"%s\"\n", argv[2]);
-			return 1;
+			goto exit1;
 		}
 	} else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
 		strptr = git_config_get_value_multi(argv[2]);
@@ -63,46 +63,50 @@ int main(int argc, char **argv)
 				else
 					printf("%s\n", v);
 			}
-			return 0;
+			goto exit0;
 		} else {
 			printf("Value not found for \"%s\"\n", argv[2]);
-			return 1;
+			goto exit1;
 		}
 	} else if (argc == 3 && !strcmp(argv[1], "get_int")) {
 		if (!git_config_get_int(argv[2], &val)) {
 			printf("%d\n", val);
-			return 0;
+			goto exit0;
 		} else {
 			printf("Value not found for \"%s\"\n", argv[2]);
-			return 1;
+			goto exit1;
 		}
 	} else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
 		if (!git_config_get_bool(argv[2], &val)) {
 			printf("%d\n", val);
-			return 0;
+			goto exit0;
 		} else {
 			printf("Value not found for \"%s\"\n", argv[2]);
-			return 1;
+			goto exit1;
 		}
 	} else if (!strcmp(argv[1], "configset_get_value")) {
 		for (i = 3; i < argc; i++) {
-			if (git_configset_add_file(&cs, argv[i]))
-				return 2;
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
 		}
 		if (!git_configset_get_value(&cs, argv[2], &v)) {
 			if (!v)
 				printf("(NULL)\n");
 			else
 				printf("%s\n", v);
-			return 0;
+			goto exit0;
 		} else {
 			printf("Value not found for \"%s\"\n", argv[2]);
-			return 1;
+			goto exit1;
 		}
 	} else if (!strcmp(argv[1], "configset_get_value_multi")) {
 		for (i = 3; i < argc; i++) {
-			if (git_configset_add_file(&cs, argv[i]))
-				return 2;
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
 		}
 		strptr = git_configset_get_value_multi(&cs, argv[2]);
 		if (strptr) {
@@ -113,13 +117,24 @@ int main(int argc, char **argv)
 				else
 					printf("%s\n", v);
 			}
-			return 0;
+			goto exit0;
 		} else {
 			printf("Value not found for \"%s\"\n", argv[2]);
-			return 1;
+			goto exit1;
 		}
 	}
 
-	fprintf(stderr, "%s: Please check the syntax and the function name\n", argv[0]);
+	die("%s: Please check the syntax and the function name", argv[0]);
+
+exit0:
+	git_configset_clear(&cs);
+	return 0;
+
+exit1:
+	git_configset_clear(&cs);
 	return 1;
+
+exit2:
+	git_configset_clear(&cs);
+	return 2;
 }

-- 8< --

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v9 1/2] add `config_set` API for caching config-like files
  2014-07-15 14:29 [PATCH v9 0/3] git config cache & special querying api utilizing the cache Tanay Abhra
@ 2014-07-15 14:29 ` Tanay Abhra
  2014-07-15 15:46   ` Junio C Hamano
  2014-07-15 14:29 ` [PATCH v9 2/2] test-config: add tests for the config_set API Tanay Abhra
  1 sibling, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-15 14:29 UTC (permalink / raw
  To: git
  Cc: Tanay Abhra, Ramkumar Ramachandra, Matthieu Moy, Junio C Hamano,
	Matthieu Moy

Currently `git_config()` uses a callback mechanism and file rereads for
config values. Due to this approach, it is not uncommon for the config
files to be parsed several times during the run of a git program, with
different callbacks picking out different variables useful to themselves.

Add a `config_set`, that can be used to construct an in-memory cache for
config-like files that the caller specifies (i.e., files like `.gitmodules`,
`~/.gitconfig` etc.). Add two external functions `git_configset_get_value`
and `git_configset_get_value_multi` for querying from the config sets.
`git_configset_get_value` follows `last one wins` semantic (i.e. if there
are multiple matches for the queried key in the files of the configset the
value returned will be the last entry in `value_list`).
`git_configset_get_value_multi` returns a list of values sorted in order of
increasing priority (i.e. last match will be at the end of the list). Add
type specific query functions like `git_configset_get_bool` and similar.

Add a default `config_set`, `the_config_set` to cache all key-value pairs
read from usual config files (repo specific .git/config, user wide
~/.gitconfig, XDG config and the global /etc/gitconfig). `the_config_set`
is populated using `git_config()`.

Add two external functions `git_config_get_value` and 
`git_config_get_value_multi` for querying in a non-callback manner from
`the_config_set`. Also, add type specific query functions that are
implemented as a thin wrapper around the `config_set` API.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
 Documentation/technical/api-config.txt | 135 ++++++++++++++++
 cache.h                                |  31 ++++
 config.c                               | 276 +++++++++++++++++++++++++++++++++
 3 files changed, 442 insertions(+)

diff --git a/Documentation/technical/api-config.txt b/Documentation/technical/api-config.txt
index 230b3a0..48255a2 100644
--- a/Documentation/technical/api-config.txt
+++ b/Documentation/technical/api-config.txt
@@ -77,6 +77,81 @@ To read a specific file in git-config format, use
 `git_config_from_file`. This takes the same callback and data parameters
 as `git_config`.
 
+Querying For Specific Variables
+-------------------------------
+
+For programs wanting to query for specific variables in a non-callback
+manner, the config API provides two functions `git_config_get_value`
+and `git_config_get_value_multi`. They both read values from an internal
+cache generated previously from reading the config files.
+
+`int git_config_get_value(const char *key, const char **value)`::
+
+	Finds the highest-priority value for the configuration variable `key`,
+	stores the pointer to it in `value` and returns 0. When the
+	configuration variable `key` is not found, returns 1 without touching
+	`value`. The caller should not free or modify `value`, as it is owned
+	by the cache.
+
+`const struct string_list *git_config_get_value_multi(const char *key)`::
+
+	Finds and returns the value list, sorted in order of increasing priority
+	for the configuration variable `key`. When the configuration variable
+	`key` is not found, returns NULL. The caller should not free or modify
+	the returned pointer, as it is owned by the cache.
+
+`void git_config_clear(void)`::
+
+	Resets and invalidates the config cache.
+
+The config API also provides type specific API functions which do conversion
+as well as retrieval for the queried variable, including:
+
+`int git_config_get_int(const char *key, int *dest)`::
+
+	Finds and parses the value to an integer for the configuration variable
+	`key`. Dies on error; otherwise, stores the value of the parsed integer in
+	`dest` and returns 0. When the configuration variable `key` is not found,
+	returns 1 without touching `dest`.
+
+`int git_config_get_ulong(const char *key, unsigned long *dest)`::
+
+	Similar to `git_config_get_int` but for unsigned longs.
+
+`int git_config_get_bool(const char *key, int *dest)`::
+
+	Finds and parses the value into a boolean value, for the configuration
+	variable `key` respecting keywords like "true" and "false". Integer
+	values are converted into true/false values (when they are non-zero or
+	zero, respectively). Other values cause a die(). If parsing is successful,
+	stores the value of the parsed result in `dest` and returns 0. When the
+	configuration variable `key` is not found, returns 1 without touching
+	`dest`.
+
+`int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)`::
+
+	Similar to `git_config_get_bool`, except that integers are copied as-is,
+	and `is_bool` flag is unset.
+
+`int git_config_get_maybe_bool(const char *key, int *dest)`::
+
+	Similar to `git_config_get_bool`, except that it returns -1 on error
+	rather than dying.
+
+`int git_config_get_string(const char *key, const char **dest)`::
+
+	Allocates and copies the retrieved string into the `dest` parameter for
+	the configuration variable `key`; if NULL string is given, prints an
+	error message and returns -1. When the configuration variable `key` is
+	not found, returns 1 without touching `dest`.
+
+`int git_config_get_pathname(const char *key, const char **dest)`::
+
+	Similar to `git_config_get_string`, but expands `~` or `~user` into
+	the user's home directory when found at the beginning of the path.
+
+See test-config.c for usage examples.
+
 Value Parsing Helpers
 ---------------------
 
@@ -134,6 +209,66 @@ int read_file_with_include(const char *file, config_fn_t fn, void *data)
 `git_config` respects includes automatically. The lower-level
 `git_config_from_file` does not.
 
+Custom Configsets
+-----------------
+
+A `config_set` can be used to construct an in-memory cache for
+config-like files that the caller specifies (i.e., files like `.gitmodules`,
+`~/.gitconfig` etc.). For example,
+
+---------------------------------------
+struct config_set gm_config;
+git_configset_init(&gm_config);
+int b;
+/* we add config files to the config_set */
+git_configset_add_file(&gm_config, ".gitmodules");
+git_configset_add_file(&gm_config, ".gitmodules_alt");
+
+if (!git_configset_get_bool(gm_config, "submodule.frotz.ignore", &b)) {
+	/* hack hack hack */
+}
+
+/* when we are done with the configset */
+git_configset_clear(&gm_config);
+----------------------------------------
+
+Configset API provides functions for the above mentioned work flow, including:
+
+`void git_configset_init(struct config_set *cs)`::
+
+	Initializes the config_set `cs`.
+
+`int git_configset_add_file(struct config_set *cs, const char *filename)`::
+
+	Parses the file and adds the variable-value pairs to the `config_set`,
+	dies if there is an error in parsing the file. Returns 0 on success, or
+	-1 if the file doesnot exist or cannot be read.
+
+`int git_configset_get_value(struct config_set *cs, const char *key, const char **value)`::
+
+	Finds the highest-priority value for the configuration variable `key`
+	and config set `cs`, stores the pointer to it in `value` and returns 0.
+	When the configuration variable `key` is not found, returns 1 without
+	touching `value`. The caller should not free or modify `value`, as it
+	is owned by the cache.
+
+`const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)`::
+
+	Finds and returns the value list, sorted in order of increasing priority
+	for the configuration variable `key` and config set `cs`. When the
+	configuration variable `key` is not found, returns NULL. The caller
+	should not free or modify the returned pointer, as it is owned by the cache.
+
+`void git_configset_clear(struct config_set *cs)`::
+
+	Clears `config_set` structure, removes all saved variable-value pairs.
+
+In addition to above functions, the `config_set` API provides type specific
+functions in the vein of `git_config_get_int` and family but with an extra
+parameter, pointer to struct `config_set`.
+They all behave similarly to the `git_config_get*()` family described in
+"Querying For Specific Variables" above.
+
 Writing Config Files
 --------------------
 
diff --git a/cache.h b/cache.h
index 44aa439..ec5fd93 100644
--- a/cache.h
+++ b/cache.h
@@ -1329,6 +1329,37 @@ extern int parse_config_key(const char *var,
 			    const char **subsection, int *subsection_len,
 			    const char **key);
 
+struct config_set {
+	struct hashmap config_hash;
+	int hash_initialized;
+};
+
+extern void git_configset_init(struct config_set *cs);
+extern int git_configset_add_file(struct config_set *cs, const char *filename);
+extern int git_configset_get_value(struct config_set *cs, const char *key, const char **value);
+extern const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
+extern void git_configset_clear(struct config_set *cs);
+extern void git_configset_iter(struct config_set *cs, config_fn_t fn, void *data);
+extern int git_configset_get_string(struct config_set *cs, const char *key, const char **dest);
+extern int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
+extern int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
+extern int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
+
+extern int git_config_get_value(const char *key, const char **value);
+extern const struct string_list *git_config_get_value_multi(const char *key);
+extern void git_config_clear(void);
+extern void git_config_iter(config_fn_t fn, void *data);
+extern int git_config_get_string(const char *key, const char **dest);
+extern int git_config_get_int(const char *key, int *dest);
+extern int git_config_get_ulong(const char *key, unsigned long *dest);
+extern int git_config_get_bool(const char *key, int *dest);
+extern int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest);
+extern int git_config_get_maybe_bool(const char *key, int *dest);
+extern int git_config_get_pathname(const char *key, const char **dest);
+
 extern int committer_ident_sufficiently_given(void);
 extern int author_ident_sufficiently_given(void);
 
diff --git a/config.c b/config.c
index ba882a1..89e2d67 100644
--- a/config.c
+++ b/config.c
@@ -9,6 +9,8 @@
 #include "exec_cmd.h"
 #include "strbuf.h"
 #include "quote.h"
+#include "hashmap.h"
+#include "string-list.h"
 
 struct config_source {
 	struct config_source *prev;
@@ -33,10 +35,23 @@ struct config_source {
 	long (*do_ftell)(struct config_source *c);
 };
 
+struct config_set_element {
+	struct hashmap_entry ent;
+	char *key;
+	struct string_list value_list;
+};
+
 static struct config_source *cf;
 
 static int zlib_compression_seen;
 
+/*
+ * Default config_set that contains key-value pairs from the usual set of config
+ * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
+ * config file and the global /etc/gitconfig)
+ */
+static struct config_set the_config_set;
+
 static int config_file_fgetc(struct config_source *conf)
 {
 	return fgetc(conf->u.file);
@@ -1212,6 +1227,264 @@ int git_config(config_fn_t fn, void *data)
 	return git_config_with_options(fn, data, NULL, 1);
 }
 
+static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
+{
+	struct config_set_element k;
+	struct config_set_element *found_entry;
+	char *normalized_key;
+	int ret;
+	/*
+	 * `key` may come from the user, so normalize it before using it
+	 * for querying entries from the hashmap.
+	 */
+	ret = git_config_parse_key(key, &normalized_key, NULL);
+
+	if (ret)
+		return NULL;
+
+	hashmap_entry_init(&k, strhash(normalized_key));
+	k.key = normalized_key;
+	found_entry = hashmap_get(&cs->config_hash, &k, NULL);
+	free(normalized_key);
+	return found_entry;
+}
+
+const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
+{
+	struct config_set_element *e = configset_find_element(cs, key);
+	return e ? &e->value_list : NULL;
+}
+
+static int configset_add_value(struct config_set *cs, const char *key, const char *value)
+{
+	struct config_set_element *e;
+	e = configset_find_element(cs, key);
+	/*
+	 * Since the keys are being fed by git_config*() callback mechanism, they
+	 * are already normalized. So simply add them without any further munging.
+	 */
+	if (!e) {
+		e = xmalloc(sizeof(*e));
+		hashmap_entry_init(e, strhash(key));
+		e->key = xstrdup(key);
+		memset(&e->value_list, 0, sizeof(e->value_list));
+		e->value_list.strdup_strings = 1;
+		hashmap_add(&cs->config_hash, e);
+	}
+	string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
+
+	return 0;
+}
+
+static int config_set_element_cmp(const struct config_set_element *e1,
+				 const struct config_set_element *e2, const void *unused)
+{
+	return strcmp(e1->key, e2->key);
+}
+
+void git_configset_init(struct config_set *cs)
+{
+	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
+	cs->hash_initialized = 1;
+}
+
+void git_configset_clear(struct config_set *cs)
+{
+	struct config_set_element *entry;
+	struct hashmap_iter iter;
+	if (!cs->hash_initialized)
+		return;
+
+	hashmap_iter_init(&cs->config_hash, &iter);
+	while ((entry = hashmap_iter_next(&iter))) {
+		free(entry->key);
+		string_list_clear(&entry->value_list, 0);
+	}
+	hashmap_free(&cs->config_hash, 1);
+	cs->hash_initialized = 0;
+}
+
+static int config_hash_callback(const char *key, const char *value, void *cb)
+{
+	struct config_set *cs = cb;
+	configset_add_value(cs, key, value);
+	return 0;
+}
+
+int git_configset_add_file(struct config_set *cs, const char *filename)
+{
+	int ret = 0;
+	ret = git_config_from_file(config_hash_callback, filename, cs);
+	if (!ret)
+		return 0;
+	else {
+		git_configset_clear(cs);
+		return -1;
+	}
+}
+
+int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
+{
+	const struct string_list *values = NULL;
+	/*
+	 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
+	 * queried key in the files of the configset, the value returned will be the last
+	 * value in the value list for that key.
+	 */
+	values = git_configset_get_value_multi(cs, key);
+
+	if (!values)
+		return 1;
+	assert(values->nr > 0);
+	*value = values->items[values->nr - 1].string;
+	return 0;
+}
+
+int git_configset_get_string(struct config_set *cs, const char *key, const char **dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value))
+		return git_config_string(dest, key, value);
+	else
+		return 1;
+}
+
+int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_int(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_ulong(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_bool(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
+				int *is_bool, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_bool_or_int(key, value, is_bool);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_maybe_bool(key, value);
+		if (*dest == -1)
+			return -1;
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value))
+		return git_config_pathname(dest, key, value);
+	else
+		return 1;
+}
+
+static void git_config_check_init(void)
+{
+	int ret = 0;
+	if (the_config_set.hash_initialized)
+		return;
+	git_configset_init(&the_config_set);
+	ret = git_config(config_hash_callback, &the_config_set);
+	if (ret >= 0)
+		return;
+	else
+		die("Unknown error when parsing one of the configuration files.");
+}
+
+void git_config_clear(void)
+{
+	if (!the_config_set.hash_initialized)
+		return;
+	git_configset_clear(&the_config_set);
+}
+
+int git_config_get_value(const char *key, const char **value)
+{
+	git_config_check_init();
+	return git_configset_get_value(&the_config_set, key, value);
+}
+
+const struct string_list *git_config_get_value_multi(const char *key)
+{
+	git_config_check_init();
+	return git_configset_get_value_multi(&the_config_set, key);
+}
+
+int git_config_get_string(const char *key, const char **dest)
+{
+	git_config_check_init();
+	return git_configset_get_string(&the_config_set, key, dest);
+}
+
+int git_config_get_int(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_int(&the_config_set, key, dest);
+}
+
+int git_config_get_ulong(const char *key, unsigned long *dest)
+{
+	git_config_check_init();
+	return git_configset_get_ulong(&the_config_set, key, dest);
+}
+
+int git_config_get_bool(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_bool(&the_config_set, key, dest);
+}
+
+int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
+}
+
+int git_config_get_maybe_bool(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_maybe_bool(&the_config_set, key, dest);
+}
+
+int git_config_get_pathname(const char *key, const char **dest)
+{
+	git_config_check_init();
+	return git_configset_get_pathname(&the_config_set, key, dest);
+}
+
 /*
  * Find all the stuff for git_config_set() below.
  */
@@ -1707,6 +1980,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
 	lock = NULL;
 	ret = 0;
 
+	/* Invalidate the config cache */
+	git_config_clear();
+
 out_free:
 	if (lock)
 		rollback_lock_file(lock);
-- 
1.9.0.GIT

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v9 2/2] test-config: add tests for the config_set API
  2014-07-15 14:29 [PATCH v9 0/3] git config cache & special querying api utilizing the cache Tanay Abhra
  2014-07-15 14:29 ` [PATCH v9 1/2] add `config_set` API for caching config-like files Tanay Abhra
@ 2014-07-15 14:29 ` Tanay Abhra
  2014-07-15 15:57   ` Junio C Hamano
  1 sibling, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-15 14:29 UTC (permalink / raw
  To: git
  Cc: Tanay Abhra, Ramkumar Ramachandra, Matthieu Moy, Junio C Hamano,
	Matthieu Moy

Expose the `config_set` C API as a set of simple commands in order to
facilitate testing. Add tests for the `config_set` API as well as for
`git_config_get_*()` family for the usual config files.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
 .gitignore            |   1 +
 Makefile              |   1 +
 t/t1308-config-set.sh | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test-config.c         | 140 +++++++++++++++++++++++++++++++++
 4 files changed, 354 insertions(+)
 create mode 100755 t/t1308-config-set.sh
 create mode 100644 test-config.c

diff --git a/.gitignore b/.gitignore
index 42294e5..7677533 100644
--- a/.gitignore
+++ b/.gitignore
@@ -177,6 +177,7 @@
 /gitweb/static/gitweb.min.*
 /test-chmtime
 /test-ctype
+/test-config
 /test-date
 /test-delta
 /test-dump-cache-tree
diff --git a/Makefile b/Makefile
index b92418d..e070eb8 100644
--- a/Makefile
+++ b/Makefile
@@ -549,6 +549,7 @@ PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
 
 TEST_PROGRAMS_NEED_X += test-chmtime
 TEST_PROGRAMS_NEED_X += test-ctype
+TEST_PROGRAMS_NEED_X += test-config
 TEST_PROGRAMS_NEED_X += test-date
 TEST_PROGRAMS_NEED_X += test-delta
 TEST_PROGRAMS_NEED_X += test-dump-cache-tree
diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
new file mode 100755
index 0000000..94085eb
--- /dev/null
+++ b/t/t1308-config-set.sh
@@ -0,0 +1,212 @@
+#!/bin/sh
+
+test_description='Test git config-set API in different settings'
+
+. ./test-lib.sh
+
+# 'check_config get_* section.key value' verifies that the entry for
+# section.key is 'value'
+check_config () {
+	case "$1" in
+	expect_code)
+		if [ "$#" -lt 5 ];
+		then
+			>expect
+		else
+			printf "%s\n" "$5" >expect
+		fi &&
+		test_expect_code "$2" test-config "$3" "$4" >actual
+		;;
+	*)
+		op=$1 key=$2 &&
+		shift &&
+		shift &&
+		printf "%s\n" "$@" >expect &&
+		test-config "$op" "$key" >actual
+		;;
+	esac &&
+	test_cmp expect actual
+}
+
+test_expect_success 'setup default config' '
+	cat >.git/config <<\EOF
+	[case]
+		penguin = very blue
+		Movie = BadPhysics
+		UPPERCASE = true
+		MixedCase = true
+		my =
+		foo
+		baz = sam
+	[Cores]
+		WhatEver = Second
+		baz = bar
+	[cores]
+		baz = bat
+	[CORES]
+		baz = ball
+	[my "Foo bAr"]
+		hi = mixed-case
+	[my "FOO BAR"]
+		hi = upper-case
+	[my "foo bar"]
+		hi = lower-case
+	[case]
+		baz = bat
+		baz = hask
+	[lamb]
+		chop = 65
+		head = none
+	[goat]
+		legs = 4
+		head = true
+		skin = false
+		nose = 1
+		horns
+	EOF
+'
+
+test_expect_success 'get value for a simple key' '
+	check_config get_value case.penguin "very blue"
+'
+
+test_expect_success 'get value for a key with value as an empty string' '
+	check_config get_value case.my ""
+'
+
+test_expect_success 'get value for a key with value as NULL' '
+	check_config get_value case.foo "(NULL)"
+'
+
+test_expect_success 'upper case key' '
+	check_config get_value case.UPPERCASE "true" &&
+	check_config get_value case.uppercase "true"
+'
+
+test_expect_success 'mixed case key' '
+	check_config get_value case.MixedCase "true" &&
+	check_config get_value case.MIXEDCASE "true" &&
+	check_config get_value case.mixedcase "true"
+'
+
+test_expect_success 'key and value with mixed case' '
+	check_config get_value case.Movie "BadPhysics"
+'
+
+test_expect_success 'key with case sensitive subsection' '
+	check_config get_value "my.Foo bAr.hi" "mixed-case" &&
+	check_config get_value "my.FOO BAR.hi" "upper-case" &&
+	check_config get_value "my.foo bar.hi" "lower-case"
+'
+
+test_expect_success 'key with case insensitive section header' '
+	check_config get_value cores.baz "ball" &&
+	check_config get_value Cores.baz "ball" &&
+	check_config get_value CORES.baz "ball" &&
+	check_config get_value coreS.baz "ball"
+'
+
+test_expect_success 'key with case insensitive section header & variable' '
+	check_config get_value CORES.BAZ "ball" &&
+	check_config get_value cores.baz "ball" &&
+	check_config get_value cores.BaZ "ball" &&
+	check_config get_value cOreS.bAz "ball"
+'
+
+test_expect_success 'find value with misspelled key' '
+	check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\""
+'
+
+test_expect_success 'find value with the highest priority' '
+	check_config get_value case.baz "hask"
+'
+
+test_expect_success 'find integer value for a key' '
+	check_config get_int lamb.chop 65
+'
+
+test_expect_success 'find integer if value is non parse-able' '
+	check_config expect_code 128 get_int lamb.head
+'
+
+test_expect_success 'find bool value for the entered key' '
+	check_config get_bool goat.head 1 &&
+	check_config get_bool goat.skin 0 &&
+	check_config get_bool goat.nose 1 &&
+	check_config get_bool goat.horns 1 &&
+	check_config get_bool goat.legs 1
+'
+
+test_expect_success 'find multiple values' '
+	cat >expect <<-\EOF &&
+	sam
+	bat
+	hask
+	EOF
+	test-config get_value_multi "case.baz">actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'find value from a configset' '
+	cat >config2 <<-\EOF &&
+	[case]
+		baz = lama
+	[my]
+		new = silk
+	[case]
+		baz = ball
+	EOF
+	echo silk >expect &&
+	test-config configset_get_value my.new config2 .git/config >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'find value with highest priority from a configset' '
+	echo hask > expect &&
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'find value_list for a key from a configset' '
+	cat >except <<-\EOF &&
+	sam
+	bat
+	hask
+	lama
+	ball
+	EOF
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-existant files' '
+	echo "Error reading configuration file non-existant-file." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar non-existant-file 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-accessible files' '
+	chmod -r .git/config &&
+	test_when_finished "chmod +r .git/config" &&
+	echo "Error reading configuration file .git/config." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in default config files' '
+	cp .git/config .git/config.old &&
+	test_when_finished "mv .git/config.old .git/config" &&
+	echo "[" >> .git/config &&
+	echo "fatal: bad config file line 35 in .git/config" >expect &&
+	test_expect_code 128 test-config get_value foo.bar 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in custom config files' '
+	echo "[" >> syntax-error &&
+	echo "fatal: bad config file line 1 in syntax-error" >expect &&
+	test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
+	test_cmp expect actual
+'
+
+test_done
diff --git a/test-config.c b/test-config.c
new file mode 100644
index 0000000..cad35f4
--- /dev/null
+++ b/test-config.c
@@ -0,0 +1,140 @@
+#include "cache.h"
+#include "string-list.h"
+
+/*
+ * This program exposes the C API of the configuration mechanism
+ * as a set of simple commands in order to facilitate testing.
+ *
+ * Reads stdin and prints result of command to stdout:
+ *
+ * get_value -> prints the value with highest priority for the entered key
+ *
+ * get_value_multi -> prints all values for the entered key in increasing order
+ *		     of priority
+ *
+ * get_int -> print integer value for the entered key or die
+ *
+ * get_bool -> print bool value for the entered key or die
+ *
+ * configset_get_value -> returns value with the highest priority for the entered key
+ * 			from a config_set constructed from files entered as arguments.
+ *
+ * configset_get_value_multi -> returns value_list for the entered key sorted in
+ * 				ascending order of priority from a config_set
+ * 				constructed from files entered as arguments.
+ *
+ * Examples:
+ *
+ * To print the value with highest priority for key "foo.bAr Baz.rock":
+ * 	test-config get_value "foo.bAr Baz.rock"
+ *
+ */
+
+
+int main(int argc, char **argv)
+{
+	int i, val;
+	const char *v;
+	const struct string_list *strptr;
+	struct config_set cs;
+	git_configset_init(&cs);
+
+	if (argc < 2) {
+		fprintf(stderr, "Please, provide a command name on the command-line\n");
+		goto exit1;
+	} else if (argc == 3 && !strcmp(argv[1], "get_value")) {
+		if (!git_config_get_value(argv[2], &v)) {
+			if (!v)
+				printf("(NULL)\n");
+			else
+				printf("%s\n", v);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
+		strptr = git_config_get_value_multi(argv[2]);
+		if (strptr) {
+			for (i = 0; i < strptr->nr; i++) {
+				v = strptr->items[i].string;
+				if (!v)
+					printf("(NULL)\n");
+				else
+					printf("%s\n", v);
+			}
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_int")) {
+		if (!git_config_get_int(argv[2], &val)) {
+			printf("%d\n", val);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
+		if (!git_config_get_bool(argv[2], &val)) {
+			printf("%d\n", val);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (!strcmp(argv[1], "configset_get_value")) {
+		for (i = 3; i < argc; i++) {
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
+		}
+		if (!git_configset_get_value(&cs, argv[2], &v)) {
+			if (!v)
+				printf("(NULL)\n");
+			else
+				printf("%s\n", v);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (!strcmp(argv[1], "configset_get_value_multi")) {
+		for (i = 3; i < argc; i++) {
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
+		}
+		strptr = git_configset_get_value_multi(&cs, argv[2]);
+		if (strptr) {
+			for (i = 0; i < strptr->nr; i++) {
+				v = strptr->items[i].string;
+				if (!v)
+					printf("(NULL)\n");
+				else
+					printf("%s\n", v);
+			}
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	}
+
+	die("%s: Please check the syntax and the function name", argv[0]);
+
+exit0:
+	git_configset_clear(&cs);
+	return 0;
+
+exit1:
+	git_configset_clear(&cs);
+	return 1;
+
+exit2:
+	git_configset_clear(&cs);
+	return 2;
+}
-- 
1.9.0.GIT

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v9 1/2] add `config_set` API for caching config-like files
  2014-07-15 14:29 ` [PATCH v9 1/2] add `config_set` API for caching config-like files Tanay Abhra
@ 2014-07-15 15:46   ` Junio C Hamano
  2014-07-15 16:22     ` Tanay Abhra
  2014-07-16 11:41     ` [PATCH v9r1 " Tanay Abhra
  0 siblings, 2 replies; 26+ messages in thread
From: Junio C Hamano @ 2014-07-15 15:46 UTC (permalink / raw
  To: Tanay Abhra; +Cc: git, Ramkumar Ramachandra, Matthieu Moy, Matthieu Moy

Tanay Abhra <tanayabh@gmail.com> writes:

> diff --git a/config.c b/config.c
> index ba882a1..89e2d67 100644
> --- a/config.c
> +++ b/config.c
> ...
> +const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
> +{
> +	struct config_set_element *e = configset_find_element(cs, key);
> +	return e ? &e->value_list : NULL;
> +}

Somehow I find the placement of this function out of place.  Didn't
you get the same impression while proofreading the entire file after
you are done writing this patch?

The right place for it would probably be immediately before
git_configset_get_value(), I would think.

> +int git_configset_add_file(struct config_set *cs, const char *filename)
> +{
> +	int ret = 0;
> +	ret = git_config_from_file(config_hash_callback, filename, cs);
> +	if (!ret)
> +		return 0;
> +	else {
> +		git_configset_clear(cs);
> +		return -1;
> +	}
> +}

If I add contents from file "A" successfully and then attempt to
further add contents from file "B" which fails, I would lose
contents I read from "A"?

It would not be worth trying to make it transactional (i.e. making
sure cs contains what was there before the config-from-file was
called if it failed), so in such a case we may end up seeing a
mixture of full contents from A and partial contents from B, which
is just as useless as a cleared configset, so it is not wrong to
clear it per-se.  An alternative might be to return without
clearing, as we are leaving the configset in a useless state either
way and give the caller a choice between clearing it and continue,
and dying without even spending unnecessary cycles to clear.  That
might be more consistent with what git_config_check_init() does,
where you die without bothering to clear what was half-read into the
configset.

Whatever behaviour is chosen in the error codepath, it needs to be
documented.

Thanks.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v9 2/2] test-config: add tests for the config_set API
  2014-07-15 14:29 ` [PATCH v9 2/2] test-config: add tests for the config_set API Tanay Abhra
@ 2014-07-15 15:57   ` Junio C Hamano
  2014-07-15 17:07     ` Tanay Abhra
  2014-07-16 11:44     ` [PATCH v9r1 " Tanay Abhra
  0 siblings, 2 replies; 26+ messages in thread
From: Junio C Hamano @ 2014-07-15 15:57 UTC (permalink / raw
  To: Tanay Abhra; +Cc: git, Ramkumar Ramachandra, Matthieu Moy, Matthieu Moy

Tanay Abhra <tanayabh@gmail.com> writes:

> Expose the `config_set` C API as a set of simple commands in order to
> facilitate testing. Add tests for the `config_set` API as well as for
> `git_config_get_*()` family for the usual config files.
>
> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
> ---
>  .gitignore            |   1 +
>  Makefile              |   1 +
>  t/t1308-config-set.sh | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++
>  test-config.c         | 140 +++++++++++++++++++++++++++++++++
>  4 files changed, 354 insertions(+)
>  create mode 100755 t/t1308-config-set.sh
>  create mode 100644 test-config.c
>
> diff --git a/.gitignore b/.gitignore
> index 42294e5..7677533 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -177,6 +177,7 @@
>  /gitweb/static/gitweb.min.*
>  /test-chmtime
>  /test-ctype
> +/test-config
>  /test-date
>  /test-delta
>  /test-dump-cache-tree
> diff --git a/Makefile b/Makefile
> index b92418d..e070eb8 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -549,6 +549,7 @@ PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))
>  
>  TEST_PROGRAMS_NEED_X += test-chmtime
>  TEST_PROGRAMS_NEED_X += test-ctype
> +TEST_PROGRAMS_NEED_X += test-config
>  TEST_PROGRAMS_NEED_X += test-date
>  TEST_PROGRAMS_NEED_X += test-delta
>  TEST_PROGRAMS_NEED_X += test-dump-cache-tree
> diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
> new file mode 100755
> index 0000000..94085eb
> --- /dev/null
> +++ b/t/t1308-config-set.sh
> @@ -0,0 +1,212 @@
> +#!/bin/sh
> +
> +test_description='Test git config-set API in different settings'
> +
> +. ./test-lib.sh
> +
> +# 'check_config get_* section.key value' verifies that the entry for
> +# section.key is 'value'
> +check_config () {
> +	case "$1" in
> +	expect_code)
> +		if [ "$#" -lt 5 ];
> +		then

Spell out "test" and drop unnecessary semicolon, i.e.

	if test $# -lt 5
        then

> +			>expect
> +		else
> +			printf "%s\n" "$5" >expect

The other "expecting success" side of this function allows to expect
more than one line of output, but this one only allows you to expect
at most one line?  Why?

> +		fi &&
> +		test_expect_code "$2" test-config "$3" "$4" >actual
> +		;;
> +	*)
> +		op=$1 key=$2 &&
> +		shift &&
> +		shift &&
> +		printf "%s\n" "$@" >expect &&
> +		test-config "$op" "$key" >actual
> +		;;
> +	esac &&
> +	test_cmp expect actual

Perhaps you meant to say something like this instead?

        if test "$1" = expect_code
        then
        	expect_code="$2" && shift && shift
	else
        	expect_code=0
	fi &&
	op=$1 key=$2 && shift && shift
        if test $# != 0
        then
        	printf "%s\n" "$@"
	fi >expect &&
        test_expect_code $expet_code test-config "$op" "$key" >actual &&
	test_cmp expect actual

I dunno.

> +test_expect_success 'setup default config' '
> +	cat >.git/config <<\EOF

So the default .git/config that was prepared by "git init" is
discarded and replaced with this?  Shouldn't it be

	cat >>.git/config <<\EOF

instead?

> +test_expect_success 'find multiple values' '
> +	cat >expect <<-\EOF &&
> +	sam
> +	bat
> +	hask
> +	EOF
> +	test-config get_value_multi "case.baz">actual &&
> +	test_cmp expect actual
> +'

Hmmm, wasn't the whole point of the helper to allow us to make
things like the above into a one-liner, perhaps like this?

      check_config get_value_multi case.baz sam bat hask

I suspect the same applies to most if not all uses of test-config
in the remainder of this patch.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v9 1/2] add `config_set` API for caching config-like files
  2014-07-15 15:46   ` Junio C Hamano
@ 2014-07-15 16:22     ` Tanay Abhra
  2014-07-16 11:41     ` [PATCH v9r1 " Tanay Abhra
  1 sibling, 0 replies; 26+ messages in thread
From: Tanay Abhra @ 2014-07-15 16:22 UTC (permalink / raw
  To: Junio C Hamano; +Cc: git, Ramkumar Ramachandra, Matthieu Moy, Matthieu Moy



On 7/15/2014 9:16 PM, Junio C Hamano wrote:
> Tanay Abhra <tanayabh@gmail.com> writes:
> 
>> diff --git a/config.c b/config.c
>> index ba882a1..89e2d67 100644
>> --- a/config.c
>> +++ b/config.c
>> ...
>> +const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
>> +{
>> +	struct config_set_element *e = configset_find_element(cs, key);
>> +	return e ? &e->value_list : NULL;
>> +}
> 
> Somehow I find the placement of this function out of place.  Didn't
> you get the same impression while proofreading the entire file after
> you are done writing this patch?
> 
> The right place for it would probably be immediately before
> git_configset_get_value(), I would think.
>

Noted. My fault, will correct in the reroll.

>> +int git_configset_add_file(struct config_set *cs, const char *filename)
>> +{
>> +	int ret = 0;
>> +	ret = git_config_from_file(config_hash_callback, filename, cs);
>> +	if (!ret)
>> +		return 0;
>> +	else {
>> +		git_configset_clear(cs);
>> +		return -1;
>> +	}
>> +}
> 
> If I add contents from file "A" successfully and then attempt to
> further add contents from file "B" which fails, I would lose
> contents I read from "A"?
> 
> It would not be worth trying to make it transactional (i.e. making
> sure cs contains what was there before the config-from-file was
> called if it failed), so in such a case we may end up seeing a
> mixture of full contents from A and partial contents from B, which
> is just as useless as a cleared configset, so it is not wrong to
> clear it per-se.  An alternative might be to return without
> clearing, as we are leaving the configset in a useless state either
> way and give the caller a choice between clearing it and continue,
> and dying without even spending unnecessary cycles to clear.  That
> might be more consistent with what git_config_check_init() does,
> where you die without bothering to clear what was half-read into the
> configset.
> 
> Whatever behaviour is chosen in the error codepath, it needs to be
> documented.
>

Since syntactical errors in reading the file will cause a die when we
use either `git_config_from_file` or `git_config`, I don't think
incomplete parsing would trigger the error path, as it will die before
reaching there.

So, the best way would be just to return without clearing and let the
user take the decision if he wants to go on with the incomplete
config_set or clear it when he sees that configset_add_file() failed.

Die in git_config_check_init() is never triggered expect in rare race
conditions like between access_or_die() and git_config_from_file() in

	if (git_config_system() && !access_or_die(git_etc_gitconfig(), R_OK, 0)) {
		ret += git_config_from_file(fn, git_etc_gitconfig(),
					    data);
		found += 1;
	}
which I think would never happen in reality, dunno. I think I will take
out the die() in git_config_check_init(). Thus, the behavior of both
functions git_config_check_init() & git_configset_add_file will be consistent.

Thanks.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v9 2/2] test-config: add tests for the config_set API
  2014-07-15 15:57   ` Junio C Hamano
@ 2014-07-15 17:07     ` Tanay Abhra
  2014-07-15 18:26       ` Junio C Hamano
  2014-07-16 11:44     ` [PATCH v9r1 " Tanay Abhra
  1 sibling, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-15 17:07 UTC (permalink / raw
  To: Junio C Hamano; +Cc: git, Ramkumar Ramachandra, Matthieu Moy, Matthieu Moy



On 7/15/2014 9:27 PM, Junio C Hamano wrote:
>> +test_expect_success 'setup default config' '
>> +	cat >.git/config <<\EOF
> 
> So the default .git/config that was prepared by "git init" is
> discarded and replaced with this?  Shouldn't it be
> 
> 	cat >>.git/config <<\EOF
> 
> instead?
>

Most of tests like t1300-repo-config.sh or t1303-wacky-config.sh
clears the default config, will it be okay to clear it in
this test series also? I need it for
test_expect_success 'proper error on error in default config files' '
which requires me to compare the line no at which the error was found.

>> +test_expect_success 'find multiple values' '
>> +	cat >expect <<-\EOF &&
>> +	sam
>> +	bat
>> +	hask
>> +	EOF
>> +	test-config get_value_multi "case.baz">actual &&
>> +	test_cmp expect actual
>> +'
> 
> Hmmm, wasn't the whole point of the helper to allow us to make
> things like the above into a one-liner, perhaps like this?
> 
>       check_config get_value_multi case.baz sam bat hask

Noted and corrected.

> I suspect the same applies to most if not all uses of test-config
> in the remainder of this patch.
>

I cant use it for configset_get_value_* as it may have variable number
of files as arguments. :)

Thanks,
Tanay Abhra.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v9 2/2] test-config: add tests for the config_set API
  2014-07-15 17:07     ` Tanay Abhra
@ 2014-07-15 18:26       ` Junio C Hamano
  0 siblings, 0 replies; 26+ messages in thread
From: Junio C Hamano @ 2014-07-15 18:26 UTC (permalink / raw
  To: Tanay Abhra; +Cc: git, Ramkumar Ramachandra, Matthieu Moy, Matthieu Moy

Tanay Abhra <tanayabh@gmail.com> writes:

> ... I need it for
> test_expect_success 'proper error on error in default config files' '
> which requires me to compare the line no at which the error was found.

I see.  We may want to later rethink the way you validate the line
numbers, but in the meantime I think the version posted should
suffice, if you cannot do better than that.

Thanks.

^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH v9r1 1/2] add `config_set` API for caching config-like files
  2014-07-15 15:46   ` Junio C Hamano
  2014-07-15 16:22     ` Tanay Abhra
@ 2014-07-16 11:41     ` Tanay Abhra
  1 sibling, 0 replies; 26+ messages in thread
From: Tanay Abhra @ 2014-07-16 11:41 UTC (permalink / raw
  To: Junio C Hamano; +Cc: git, Ramkumar Ramachandra, Matthieu Moy, Matthieu Moy

Currently `git_config()` uses a callback mechanism and file rereads for
config values. Due to this approach, it is not uncommon for the config
files to be parsed several times during the run of a git program, with
different callbacks picking out different variables useful to themselves.

Add a `config_set`, that can be used to construct an in-memory cache for
config-like files that the caller specifies (i.e., files like `.gitmodules`,
`~/.gitconfig` etc.). Add two external functions `git_configset_get_value`
and `git_configset_get_value_multi` for querying from the config sets.
`git_configset_get_value` follows `last one wins` semantic (i.e. if there
are multiple matches for the queried key in the files of the configset the
value returned will be the last entry in `value_list`).
`git_configset_get_value_multi` returns a list of values sorted in order of
increasing priority (i.e. last match will be at the end of the list). Add
type specific query functions like `git_configset_get_bool` and similar.

Add a default `config_set`, `the_config_set` to cache all key-value pairs
read from usual config files (repo specific .git/config, user wide
~/.gitconfig, XDG config and the global /etc/gitconfig). `the_config_set`
is populated using `git_config()`.

Add two external functions `git_config_get_value` and
`git_config_get_value_multi` for querying in a non-callback manner from
`the_config_set`. Also, add type specific query functions that are
implemented as a thin wrapper around the `config_set` API.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
 Documentation/technical/api-config.txt | 137 +++++++++++++++++
 cache.h                                |  30 ++++
 config.c                               | 264 +++++++++++++++++++++++++++++++++
 3 files changed, 431 insertions(+)

diff --git a/Documentation/technical/api-config.txt b/Documentation/technical/api-config.txt
index 230b3a0..fc0e379 100644
--- a/Documentation/technical/api-config.txt
+++ b/Documentation/technical/api-config.txt
@@ -77,6 +77,81 @@ To read a specific file in git-config format, use
 `git_config_from_file`. This takes the same callback and data parameters
 as `git_config`.

+Querying For Specific Variables
+-------------------------------
+
+For programs wanting to query for specific variables in a non-callback
+manner, the config API provides two functions `git_config_get_value`
+and `git_config_get_value_multi`. They both read values from an internal
+cache generated previously from reading the config files.
+
+`int git_config_get_value(const char *key, const char **value)`::
+
+	Finds the highest-priority value for the configuration variable `key`,
+	stores the pointer to it in `value` and returns 0. When the
+	configuration variable `key` is not found, returns 1 without touching
+	`value`. The caller should not free or modify `value`, as it is owned
+	by the cache.
+
+`const struct string_list *git_config_get_value_multi(const char *key)`::
+
+	Finds and returns the value list, sorted in order of increasing priority
+	for the configuration variable `key`. When the configuration variable
+	`key` is not found, returns NULL. The caller should not free or modify
+	the returned pointer, as it is owned by the cache.
+
+`void git_config_clear(void)`::
+
+	Resets and invalidates the config cache.
+
+The config API also provides type specific API functions which do conversion
+as well as retrieval for the queried variable, including:
+
+`int git_config_get_int(const char *key, int *dest)`::
+
+	Finds and parses the value to an integer for the configuration variable
+	`key`. Dies on error; otherwise, stores the value of the parsed integer in
+	`dest` and returns 0. When the configuration variable `key` is not found,
+	returns 1 without touching `dest`.
+
+`int git_config_get_ulong(const char *key, unsigned long *dest)`::
+
+	Similar to `git_config_get_int` but for unsigned longs.
+
+`int git_config_get_bool(const char *key, int *dest)`::
+
+	Finds and parses the value into a boolean value, for the configuration
+	variable `key` respecting keywords like "true" and "false". Integer
+	values are converted into true/false values (when they are non-zero or
+	zero, respectively). Other values cause a die(). If parsing is successful,
+	stores the value of the parsed result in `dest` and returns 0. When the
+	configuration variable `key` is not found, returns 1 without touching
+	`dest`.
+
+`int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)`::
+
+	Similar to `git_config_get_bool`, except that integers are copied as-is,
+	and `is_bool` flag is unset.
+
+`int git_config_get_maybe_bool(const char *key, int *dest)`::
+
+	Similar to `git_config_get_bool`, except that it returns -1 on error
+	rather than dying.
+
+`int git_config_get_string(const char *key, const char **dest)`::
+
+	Allocates and copies the retrieved string into the `dest` parameter for
+	the configuration variable `key`; if NULL string is given, prints an
+	error message and returns -1. When the configuration variable `key` is
+	not found, returns 1 without touching `dest`.
+
+`int git_config_get_pathname(const char *key, const char **dest)`::
+
+	Similar to `git_config_get_string`, but expands `~` or `~user` into
+	the user's home directory when found at the beginning of the path.
+
+See test-config.c for usage examples.
+
 Value Parsing Helpers
 ---------------------

@@ -134,6 +209,68 @@ int read_file_with_include(const char *file, config_fn_t fn, void *data)
 `git_config` respects includes automatically. The lower-level
 `git_config_from_file` does not.

+Custom Configsets
+-----------------
+
+A `config_set` can be used to construct an in-memory cache for
+config-like files that the caller specifies (i.e., files like `.gitmodules`,
+`~/.gitconfig` etc.). For example,
+
+---------------------------------------
+struct config_set gm_config;
+git_configset_init(&gm_config);
+int b;
+/* we add config files to the config_set */
+git_configset_add_file(&gm_config, ".gitmodules");
+git_configset_add_file(&gm_config, ".gitmodules_alt");
+
+if (!git_configset_get_bool(gm_config, "submodule.frotz.ignore", &b)) {
+	/* hack hack hack */
+}
+
+/* when we are done with the configset */
+git_configset_clear(&gm_config);
+----------------------------------------
+
+Configset API provides functions for the above mentioned work flow, including:
+
+`void git_configset_init(struct config_set *cs)`::
+
+	Initializes the config_set `cs`.
+
+`int git_configset_add_file(struct config_set *cs, const char *filename)`::
+
+	Parses the file and adds the variable-value pairs to the `config_set`,
+	dies if there is an error in parsing the file. Returns 0 on success, or
+	-1 if the file doesnot exist or is inaccessible. The user has to decide
+	if he wants to free the incomplete configset or continue using it when
+	the function returns -1.
+
+`int git_configset_get_value(struct config_set *cs, const char *key, const char **value)`::
+
+	Finds the highest-priority value for the configuration variable `key`
+	and config set `cs`, stores the pointer to it in `value` and returns 0.
+	When the configuration variable `key` is not found, returns 1 without
+	touching `value`. The caller should not free or modify `value`, as it
+	is owned by the cache.
+
+`const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)`::
+
+	Finds and returns the value list, sorted in order of increasing priority
+	for the configuration variable `key` and config set `cs`. When the
+	configuration variable `key` is not found, returns NULL. The caller
+	should not free or modify the returned pointer, as it is owned by the cache.
+
+`void git_configset_clear(struct config_set *cs)`::
+
+	Clears `config_set` structure, removes all saved variable-value pairs.
+
+In addition to above functions, the `config_set` API provides type specific
+functions in the vein of `git_config_get_int` and family but with an extra
+parameter, pointer to struct `config_set`.
+They all behave similarly to the `git_config_get*()` family described in
+"Querying For Specific Variables" above.
+
 Writing Config Files
 --------------------

diff --git a/cache.h b/cache.h
index 44aa439..c67639d 100644
--- a/cache.h
+++ b/cache.h
@@ -1329,6 +1329,36 @@ extern int parse_config_key(const char *var,
 			    const char **subsection, int *subsection_len,
 			    const char **key);

+struct config_set {
+	struct hashmap config_hash;
+	int hash_initialized;
+};
+
+extern void git_configset_init(struct config_set *cs);
+extern int git_configset_add_file(struct config_set *cs, const char *filename);
+extern int git_configset_get_value(struct config_set *cs, const char *key, const char **value);
+extern const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
+extern void git_configset_clear(struct config_set *cs);
+extern int git_configset_get_string(struct config_set *cs, const char *key, const char **dest);
+extern int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
+extern int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
+extern int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
+
+extern int git_config_get_value(const char *key, const char **value);
+extern const struct string_list *git_config_get_value_multi(const char *key);
+extern void git_config_clear(void);
+extern void git_config_iter(config_fn_t fn, void *data);
+extern int git_config_get_string(const char *key, const char **dest);
+extern int git_config_get_int(const char *key, int *dest);
+extern int git_config_get_ulong(const char *key, unsigned long *dest);
+extern int git_config_get_bool(const char *key, int *dest);
+extern int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest);
+extern int git_config_get_maybe_bool(const char *key, int *dest);
+extern int git_config_get_pathname(const char *key, const char **dest);
+
 extern int committer_ident_sufficiently_given(void);
 extern int author_ident_sufficiently_given(void);

diff --git a/config.c b/config.c
index ba882a1..d14f761 100644
--- a/config.c
+++ b/config.c
@@ -9,6 +9,8 @@
 #include "exec_cmd.h"
 #include "strbuf.h"
 #include "quote.h"
+#include "hashmap.h"
+#include "string-list.h"

 struct config_source {
 	struct config_source *prev;
@@ -33,10 +35,23 @@ struct config_source {
 	long (*do_ftell)(struct config_source *c);
 };

+struct config_set_element {
+	struct hashmap_entry ent;
+	char *key;
+	struct string_list value_list;
+};
+
 static struct config_source *cf;

 static int zlib_compression_seen;

+/*
+ * Default config_set that contains key-value pairs from the usual set of config
+ * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
+ * config file and the global /etc/gitconfig)
+ */
+static struct config_set the_config_set;
+
 static int config_file_fgetc(struct config_source *conf)
 {
 	return fgetc(conf->u.file);
@@ -1212,6 +1227,252 @@ int git_config(config_fn_t fn, void *data)
 	return git_config_with_options(fn, data, NULL, 1);
 }

+static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
+{
+	struct config_set_element k;
+	struct config_set_element *found_entry;
+	char *normalized_key;
+	int ret;
+	/*
+	 * `key` may come from the user, so normalize it before using it
+	 * for querying entries from the hashmap.
+	 */
+	ret = git_config_parse_key(key, &normalized_key, NULL);
+
+	if (ret)
+		return NULL;
+
+	hashmap_entry_init(&k, strhash(normalized_key));
+	k.key = normalized_key;
+	found_entry = hashmap_get(&cs->config_hash, &k, NULL);
+	free(normalized_key);
+	return found_entry;
+}
+
+static int configset_add_value(struct config_set *cs, const char *key, const char *value)
+{
+	struct config_set_element *e;
+	e = configset_find_element(cs, key);
+	/*
+	 * Since the keys are being fed by git_config*() callback mechanism, they
+	 * are already normalized. So simply add them without any further munging.
+	 */
+	if (!e) {
+		e = xmalloc(sizeof(*e));
+		hashmap_entry_init(e, strhash(key));
+		e->key = xstrdup(key);
+		memset(&e->value_list, 0, sizeof(e->value_list));
+		e->value_list.strdup_strings = 1;
+		hashmap_add(&cs->config_hash, e);
+	}
+	string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
+
+	return 0;
+}
+
+static int config_set_element_cmp(const struct config_set_element *e1,
+				 const struct config_set_element *e2, const void *unused)
+{
+	return strcmp(e1->key, e2->key);
+}
+
+void git_configset_init(struct config_set *cs)
+{
+	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
+	cs->hash_initialized = 1;
+}
+
+void git_configset_clear(struct config_set *cs)
+{
+	struct config_set_element *entry;
+	struct hashmap_iter iter;
+	if (!cs->hash_initialized)
+		return;
+
+	hashmap_iter_init(&cs->config_hash, &iter);
+	while ((entry = hashmap_iter_next(&iter))) {
+		free(entry->key);
+		string_list_clear(&entry->value_list, 0);
+	}
+	hashmap_free(&cs->config_hash, 1);
+	cs->hash_initialized = 0;
+}
+
+static int config_hash_callback(const char *key, const char *value, void *cb)
+{
+	struct config_set *cs = cb;
+	configset_add_value(cs, key, value);
+	return 0;
+}
+
+int git_configset_add_file(struct config_set *cs, const char *filename)
+{
+	return git_config_from_file(config_hash_callback, filename, cs);
+}
+
+int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
+{
+	const struct string_list *values = NULL;
+	/*
+	 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
+	 * queried key in the files of the configset, the value returned will be the last
+	 * value in the value list for that key.
+	 */
+	values = git_configset_get_value_multi(cs, key);
+
+	if (!values)
+		return 1;
+	assert(values->nr > 0);
+	*value = values->items[values->nr - 1].string;
+	return 0;
+}
+
+const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
+{
+	struct config_set_element *e = configset_find_element(cs, key);
+	return e ? &e->value_list : NULL;
+}
+
+int git_configset_get_string(struct config_set *cs, const char *key, const char **dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value))
+		return git_config_string(dest, key, value);
+	else
+		return 1;
+}
+
+int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_int(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_ulong(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_bool(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
+				int *is_bool, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_bool_or_int(key, value, is_bool);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_maybe_bool(key, value);
+		if (*dest == -1)
+			return -1;
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value))
+		return git_config_pathname(dest, key, value);
+	else
+		return 1;
+}
+
+static void git_config_check_init(void)
+{
+	if (the_config_set.hash_initialized)
+		return;
+	git_configset_init(&the_config_set);
+	git_config(config_hash_callback, &the_config_set);
+}
+
+void git_config_clear(void)
+{
+	if (!the_config_set.hash_initialized)
+		return;
+	git_configset_clear(&the_config_set);
+}
+
+int git_config_get_value(const char *key, const char **value)
+{
+	git_config_check_init();
+	return git_configset_get_value(&the_config_set, key, value);
+}
+
+const struct string_list *git_config_get_value_multi(const char *key)
+{
+	git_config_check_init();
+	return git_configset_get_value_multi(&the_config_set, key);
+}
+
+int git_config_get_string(const char *key, const char **dest)
+{
+	git_config_check_init();
+	return git_configset_get_string(&the_config_set, key, dest);
+}
+
+int git_config_get_int(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_int(&the_config_set, key, dest);
+}
+
+int git_config_get_ulong(const char *key, unsigned long *dest)
+{
+	git_config_check_init();
+	return git_configset_get_ulong(&the_config_set, key, dest);
+}
+
+int git_config_get_bool(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_bool(&the_config_set, key, dest);
+}
+
+int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
+}
+
+int git_config_get_maybe_bool(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_maybe_bool(&the_config_set, key, dest);
+}
+
+int git_config_get_pathname(const char *key, const char **dest)
+{
+	git_config_check_init();
+	return git_configset_get_pathname(&the_config_set, key, dest);
+}
+
 /*
  * Find all the stuff for git_config_set() below.
  */
@@ -1707,6 +1968,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
 	lock = NULL;
 	ret = 0;

+	/* Invalidate the config cache */
+	git_config_clear();
+
 out_free:
 	if (lock)
 		rollback_lock_file(lock);
-- 
1.9.0.GIT

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v9r1 2/2] test-config: add tests for the config_set API
  2014-07-15 15:57   ` Junio C Hamano
  2014-07-15 17:07     ` Tanay Abhra
@ 2014-07-16 11:44     ` Tanay Abhra
  2014-07-16 11:49       ` Matthieu Moy
  1 sibling, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-16 11:44 UTC (permalink / raw
  To: Junio C Hamano; +Cc: git, Ramkumar Ramachandra, Matthieu Moy, Matthieu Moy

Expose the `config_set` C API as a set of simple commands in order to
facilitate testing. Add tests for the `config_set` API as well as for
`git_config_get_*()` family for the usual config files.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
 .gitignore            |   1 +
 Makefile              |   1 +
 t/t1308-config-set.sh | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test-config.c         | 140 +++++++++++++++++++++++++++++++++++
 4 files changed, 342 insertions(+)
 create mode 100755 t/t1308-config-set.sh
 create mode 100644 test-config.c

diff --git a/.gitignore b/.gitignore
index 42294e5..7677533 100644
--- a/.gitignore
+++ b/.gitignore
@@ -177,6 +177,7 @@
 /gitweb/static/gitweb.min.*
 /test-chmtime
 /test-ctype
+/test-config
 /test-date
 /test-delta
 /test-dump-cache-tree
diff --git a/Makefile b/Makefile
index b92418d..e070eb8 100644
--- a/Makefile
+++ b/Makefile
@@ -549,6 +549,7 @@ PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))

 TEST_PROGRAMS_NEED_X += test-chmtime
 TEST_PROGRAMS_NEED_X += test-ctype
+TEST_PROGRAMS_NEED_X += test-config
 TEST_PROGRAMS_NEED_X += test-date
 TEST_PROGRAMS_NEED_X += test-delta
 TEST_PROGRAMS_NEED_X += test-dump-cache-tree
diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
new file mode 100755
index 0000000..cf8f2d7
--- /dev/null
+++ b/t/t1308-config-set.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+test_description='Test git config-set API in different settings'
+
+. ./test-lib.sh
+
+# 'check_config get_* section.key value' verifies that the entry for
+# section.key is 'value'
+check_config () {
+	if test "$1" = expect_code
+	then
+		expect_code="$2" && shift && shift
+	else
+		expect_code=0
+	fi &&
+	op=$1 key=$2 && shift && shift
+	if test $# != 0
+	then
+		printf "%s\n" "$@"
+	fi >expect &&
+	test_expect_code $expect_code test-config "$op" "$key" >actual &&
+	test_cmp expect actual
+}
+
+test_expect_success 'setup default config' '
+	cat >.git/config <<\EOF
+	[case]
+		penguin = very blue
+		Movie = BadPhysics
+		UPPERCASE = true
+		MixedCase = true
+		my =
+		foo
+		baz = sam
+	[Cores]
+		WhatEver = Second
+		baz = bar
+	[cores]
+		baz = bat
+	[CORES]
+		baz = ball
+	[my "Foo bAr"]
+		hi = mixed-case
+	[my "FOO BAR"]
+		hi = upper-case
+	[my "foo bar"]
+		hi = lower-case
+	[case]
+		baz = bat
+		baz = hask
+	[lamb]
+		chop = 65
+		head = none
+	[goat]
+		legs = 4
+		head = true
+		skin = false
+		nose = 1
+		horns
+	EOF
+'
+
+test_expect_success 'get value for a simple key' '
+	check_config get_value case.penguin "very blue"
+'
+
+test_expect_success 'get value for a key with value as an empty string' '
+	check_config get_value case.my ""
+'
+
+test_expect_success 'get value for a key with value as NULL' '
+	check_config get_value case.foo "(NULL)"
+'
+
+test_expect_success 'upper case key' '
+	check_config get_value case.UPPERCASE "true" &&
+	check_config get_value case.uppercase "true"
+'
+
+test_expect_success 'mixed case key' '
+	check_config get_value case.MixedCase "true" &&
+	check_config get_value case.MIXEDCASE "true" &&
+	check_config get_value case.mixedcase "true"
+'
+
+test_expect_success 'key and value with mixed case' '
+	check_config get_value case.Movie "BadPhysics"
+'
+
+test_expect_success 'key with case sensitive subsection' '
+	check_config get_value "my.Foo bAr.hi" "mixed-case" &&
+	check_config get_value "my.FOO BAR.hi" "upper-case" &&
+	check_config get_value "my.foo bar.hi" "lower-case"
+'
+
+test_expect_success 'key with case insensitive section header' '
+	check_config get_value cores.baz "ball" &&
+	check_config get_value Cores.baz "ball" &&
+	check_config get_value CORES.baz "ball" &&
+	check_config get_value coreS.baz "ball"
+'
+
+test_expect_success 'key with case insensitive section header & variable' '
+	check_config get_value CORES.BAZ "ball" &&
+	check_config get_value cores.baz "ball" &&
+	check_config get_value cores.BaZ "ball" &&
+	check_config get_value cOreS.bAz "ball"
+'
+
+test_expect_success 'find value with misspelled key' '
+	check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\""
+'
+
+test_expect_success 'find value with the highest priority' '
+	check_config get_value case.baz "hask"
+'
+
+test_expect_success 'find integer value for a key' '
+	check_config get_int lamb.chop 65
+'
+
+test_expect_success 'find integer if value is non parse-able' '
+	check_config expect_code 128 get_int lamb.head
+'
+
+test_expect_success 'find bool value for the entered key' '
+	check_config get_bool goat.head 1 &&
+	check_config get_bool goat.skin 0 &&
+	check_config get_bool goat.nose 1 &&
+	check_config get_bool goat.horns 1 &&
+	check_config get_bool goat.legs 1
+'
+
+test_expect_success 'find multiple values' '
+	check_config get_value_multi case.baz sam bat hask
+'
+
+test_expect_success 'find value from a configset' '
+	cat >config2 <<-\EOF &&
+	[case]
+		baz = lama
+	[my]
+		new = silk
+	[case]
+		baz = ball
+	EOF
+	echo silk >expect &&
+	test-config configset_get_value my.new config2 .git/config >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'find value with highest priority from a configset' '
+	echo hask > expect &&
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'find value_list for a key from a configset' '
+	cat >except <<-\EOF &&
+	sam
+	bat
+	hask
+	lama
+	ball
+	EOF
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-existant files' '
+	echo "Error reading configuration file non-existant-file." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar non-existant-file 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-accessible  files' '
+	chmod -r .git/config &&
+	test_when_finished "chmod +r .git/config" &&
+	echo "Error reading configuration file .git/config." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in default config files' '
+	cp .git/config .git/config.old &&
+	test_when_finished "mv .git/config.old .git/config" &&
+	echo "[" >> .git/config &&
+	echo "fatal: bad config file line 35 in .git/config" >expect &&
+	test_expect_code 128 test-config get_value foo.bar 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in custom config files' '
+	echo "[" >> syntax-error &&
+	echo "fatal: bad config file line 1 in syntax-error" >expect &&
+	test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
+	test_cmp expect actual
+'
+
+test_done
diff --git a/test-config.c b/test-config.c
new file mode 100644
index 0000000..cad35f4
--- /dev/null
+++ b/test-config.c
@@ -0,0 +1,140 @@
+#include "cache.h"
+#include "string-list.h"
+
+/*
+ * This program exposes the C API of the configuration mechanism
+ * as a set of simple commands in order to facilitate testing.
+ *
+ * Reads stdin and prints result of command to stdout:
+ *
+ * get_value -> prints the value with highest priority for the entered key
+ *
+ * get_value_multi -> prints all values for the entered key in increasing order
+ *		     of priority
+ *
+ * get_int -> print integer value for the entered key or die
+ *
+ * get_bool -> print bool value for the entered key or die
+ *
+ * configset_get_value -> returns value with the highest priority for the entered key
+ * 			from a config_set constructed from files entered as arguments.
+ *
+ * configset_get_value_multi -> returns value_list for the entered key sorted in
+ * 				ascending order of priority from a config_set
+ * 				constructed from files entered as arguments.
+ *
+ * Examples:
+ *
+ * To print the value with highest priority for key "foo.bAr Baz.rock":
+ * 	test-config get_value "foo.bAr Baz.rock"
+ *
+ */
+
+
+int main(int argc, char **argv)
+{
+	int i, val;
+	const char *v;
+	const struct string_list *strptr;
+	struct config_set cs;
+	git_configset_init(&cs);
+
+	if (argc < 2) {
+		fprintf(stderr, "Please, provide a command name on the command-line\n");
+		goto exit1;
+	} else if (argc == 3 && !strcmp(argv[1], "get_value")) {
+		if (!git_config_get_value(argv[2], &v)) {
+			if (!v)
+				printf("(NULL)\n");
+			else
+				printf("%s\n", v);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
+		strptr = git_config_get_value_multi(argv[2]);
+		if (strptr) {
+			for (i = 0; i < strptr->nr; i++) {
+				v = strptr->items[i].string;
+				if (!v)
+					printf("(NULL)\n");
+				else
+					printf("%s\n", v);
+			}
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_int")) {
+		if (!git_config_get_int(argv[2], &val)) {
+			printf("%d\n", val);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
+		if (!git_config_get_bool(argv[2], &val)) {
+			printf("%d\n", val);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (!strcmp(argv[1], "configset_get_value")) {
+		for (i = 3; i < argc; i++) {
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
+		}
+		if (!git_configset_get_value(&cs, argv[2], &v)) {
+			if (!v)
+				printf("(NULL)\n");
+			else
+				printf("%s\n", v);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (!strcmp(argv[1], "configset_get_value_multi")) {
+		for (i = 3; i < argc; i++) {
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
+		}
+		strptr = git_configset_get_value_multi(&cs, argv[2]);
+		if (strptr) {
+			for (i = 0; i < strptr->nr; i++) {
+				v = strptr->items[i].string;
+				if (!v)
+					printf("(NULL)\n");
+				else
+					printf("%s\n", v);
+			}
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	}
+
+	die("%s: Please check the syntax and the function name", argv[0]);
+
+exit0:
+	git_configset_clear(&cs);
+	return 0;
+
+exit1:
+	git_configset_clear(&cs);
+	return 1;
+
+exit2:
+	git_configset_clear(&cs);
+	return 2;
+}
-- 
1.9.0.GIT

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r1 2/2] test-config: add tests for the config_set API
  2014-07-16 11:44     ` [PATCH v9r1 " Tanay Abhra
@ 2014-07-16 11:49       ` Matthieu Moy
  2014-07-16 12:22         ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
  2014-07-16 12:22         ` [PATCH v9r2 2/2] test-config: add tests for the config_set API Tanay Abhra
  0 siblings, 2 replies; 26+ messages in thread
From: Matthieu Moy @ 2014-07-16 11:49 UTC (permalink / raw
  To: Tanay Abhra; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> +# 'check_config get_* section.key value' verifies that the entry for
> +# section.key is 'value'
> +check_config () {
> +	if test "$1" = expect_code
> +	then
> +		expect_code="$2" && shift && shift
> +	else
> +		expect_code=0
> +	fi &&
> +	op=$1 key=$2 && shift && shift
> +	if test $# != 0

Broken &&-chain after shift.

(No time for more review right now, but I'll have time in ~3h from now)

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-16 11:49       ` Matthieu Moy
@ 2014-07-16 12:22         ` Tanay Abhra
  2014-07-16 16:06           ` Matthieu Moy
  2014-07-16 12:22         ` [PATCH v9r2 2/2] test-config: add tests for the config_set API Tanay Abhra
  1 sibling, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-16 12:22 UTC (permalink / raw
  To: Matthieu Moy; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Currently `git_config()` uses a callback mechanism and file rereads for
config values. Due to this approach, it is not uncommon for the config
files to be parsed several times during the run of a git program, with
different callbacks picking out different variables useful to themselves.

Add a `config_set`, that can be used to construct an in-memory cache for
config-like files that the caller specifies (i.e., files like `.gitmodules`,
`~/.gitconfig` etc.). Add two external functions `git_configset_get_value`
and `git_configset_get_value_multi` for querying from the config sets.
`git_configset_get_value` follows `last one wins` semantic (i.e. if there
are multiple matches for the queried key in the files of the configset the
value returned will be the last entry in `value_list`).
`git_configset_get_value_multi` returns a list of values sorted in order of
increasing priority (i.e. last match will be at the end of the list). Add
type specific query functions like `git_configset_get_bool` and similar.

Add a default `config_set`, `the_config_set` to cache all key-value pairs
read from usual config files (repo specific .git/config, user wide
~/.gitconfig, XDG config and the global /etc/gitconfig). `the_config_set`
is populated using `git_config()`.

Add two external functions `git_config_get_value` and
`git_config_get_value_multi` for querying in a non-callback manner from
`the_config_set`. Also, add type specific query functions that are
implemented as a thin wrapper around the `config_set` API.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
 Documentation/technical/api-config.txt | 137 +++++++++++++++++
 cache.h                                |  30 ++++
 config.c                               | 264 +++++++++++++++++++++++++++++++++
 3 files changed, 431 insertions(+)

diff --git a/Documentation/technical/api-config.txt b/Documentation/technical/api-config.txt
index 230b3a0..fc0e379 100644
--- a/Documentation/technical/api-config.txt
+++ b/Documentation/technical/api-config.txt
@@ -77,6 +77,81 @@ To read a specific file in git-config format, use
 `git_config_from_file`. This takes the same callback and data parameters
 as `git_config`.

+Querying For Specific Variables
+-------------------------------
+
+For programs wanting to query for specific variables in a non-callback
+manner, the config API provides two functions `git_config_get_value`
+and `git_config_get_value_multi`. They both read values from an internal
+cache generated previously from reading the config files.
+
+`int git_config_get_value(const char *key, const char **value)`::
+
+	Finds the highest-priority value for the configuration variable `key`,
+	stores the pointer to it in `value` and returns 0. When the
+	configuration variable `key` is not found, returns 1 without touching
+	`value`. The caller should not free or modify `value`, as it is owned
+	by the cache.
+
+`const struct string_list *git_config_get_value_multi(const char *key)`::
+
+	Finds and returns the value list, sorted in order of increasing priority
+	for the configuration variable `key`. When the configuration variable
+	`key` is not found, returns NULL. The caller should not free or modify
+	the returned pointer, as it is owned by the cache.
+
+`void git_config_clear(void)`::
+
+	Resets and invalidates the config cache.
+
+The config API also provides type specific API functions which do conversion
+as well as retrieval for the queried variable, including:
+
+`int git_config_get_int(const char *key, int *dest)`::
+
+	Finds and parses the value to an integer for the configuration variable
+	`key`. Dies on error; otherwise, stores the value of the parsed integer in
+	`dest` and returns 0. When the configuration variable `key` is not found,
+	returns 1 without touching `dest`.
+
+`int git_config_get_ulong(const char *key, unsigned long *dest)`::
+
+	Similar to `git_config_get_int` but for unsigned longs.
+
+`int git_config_get_bool(const char *key, int *dest)`::
+
+	Finds and parses the value into a boolean value, for the configuration
+	variable `key` respecting keywords like "true" and "false". Integer
+	values are converted into true/false values (when they are non-zero or
+	zero, respectively). Other values cause a die(). If parsing is successful,
+	stores the value of the parsed result in `dest` and returns 0. When the
+	configuration variable `key` is not found, returns 1 without touching
+	`dest`.
+
+`int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)`::
+
+	Similar to `git_config_get_bool`, except that integers are copied as-is,
+	and `is_bool` flag is unset.
+
+`int git_config_get_maybe_bool(const char *key, int *dest)`::
+
+	Similar to `git_config_get_bool`, except that it returns -1 on error
+	rather than dying.
+
+`int git_config_get_string(const char *key, const char **dest)`::
+
+	Allocates and copies the retrieved string into the `dest` parameter for
+	the configuration variable `key`; if NULL string is given, prints an
+	error message and returns -1. When the configuration variable `key` is
+	not found, returns 1 without touching `dest`.
+
+`int git_config_get_pathname(const char *key, const char **dest)`::
+
+	Similar to `git_config_get_string`, but expands `~` or `~user` into
+	the user's home directory when found at the beginning of the path.
+
+See test-config.c for usage examples.
+
 Value Parsing Helpers
 ---------------------

@@ -134,6 +209,68 @@ int read_file_with_include(const char *file, config_fn_t fn, void *data)
 `git_config` respects includes automatically. The lower-level
 `git_config_from_file` does not.

+Custom Configsets
+-----------------
+
+A `config_set` can be used to construct an in-memory cache for
+config-like files that the caller specifies (i.e., files like `.gitmodules`,
+`~/.gitconfig` etc.). For example,
+
+---------------------------------------
+struct config_set gm_config;
+git_configset_init(&gm_config);
+int b;
+/* we add config files to the config_set */
+git_configset_add_file(&gm_config, ".gitmodules");
+git_configset_add_file(&gm_config, ".gitmodules_alt");
+
+if (!git_configset_get_bool(gm_config, "submodule.frotz.ignore", &b)) {
+	/* hack hack hack */
+}
+
+/* when we are done with the configset */
+git_configset_clear(&gm_config);
+----------------------------------------
+
+Configset API provides functions for the above mentioned work flow, including:
+
+`void git_configset_init(struct config_set *cs)`::
+
+	Initializes the config_set `cs`.
+
+`int git_configset_add_file(struct config_set *cs, const char *filename)`::
+
+	Parses the file and adds the variable-value pairs to the `config_set`,
+	dies if there is an error in parsing the file. Returns 0 on success, or
+	-1 if the file doesnot exist or is inaccessible. The user has to decide
+	if he wants to free the incomplete configset or continue using it when
+	the function returns -1.
+
+`int git_configset_get_value(struct config_set *cs, const char *key, const char **value)`::
+
+	Finds the highest-priority value for the configuration variable `key`
+	and config set `cs`, stores the pointer to it in `value` and returns 0.
+	When the configuration variable `key` is not found, returns 1 without
+	touching `value`. The caller should not free or modify `value`, as it
+	is owned by the cache.
+
+`const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)`::
+
+	Finds and returns the value list, sorted in order of increasing priority
+	for the configuration variable `key` and config set `cs`. When the
+	configuration variable `key` is not found, returns NULL. The caller
+	should not free or modify the returned pointer, as it is owned by the cache.
+
+`void git_configset_clear(struct config_set *cs)`::
+
+	Clears `config_set` structure, removes all saved variable-value pairs.
+
+In addition to above functions, the `config_set` API provides type specific
+functions in the vein of `git_config_get_int` and family but with an extra
+parameter, pointer to struct `config_set`.
+They all behave similarly to the `git_config_get*()` family described in
+"Querying For Specific Variables" above.
+
 Writing Config Files
 --------------------

diff --git a/cache.h b/cache.h
index 44aa439..c67639d 100644
--- a/cache.h
+++ b/cache.h
@@ -1329,6 +1329,36 @@ extern int parse_config_key(const char *var,
 			    const char **subsection, int *subsection_len,
 			    const char **key);

+struct config_set {
+	struct hashmap config_hash;
+	int hash_initialized;
+};
+
+extern void git_configset_init(struct config_set *cs);
+extern int git_configset_add_file(struct config_set *cs, const char *filename);
+extern int git_configset_get_value(struct config_set *cs, const char *key, const char **value);
+extern const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key);
+extern void git_configset_clear(struct config_set *cs);
+extern int git_configset_get_string(struct config_set *cs, const char *key, const char **dest);
+extern int git_configset_get_int(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest);
+extern int git_configset_get_bool(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_bool_or_int(struct config_set *cs, const char *key, int *is_bool, int *dest);
+extern int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest);
+extern int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest);
+
+extern int git_config_get_value(const char *key, const char **value);
+extern const struct string_list *git_config_get_value_multi(const char *key);
+extern void git_config_clear(void);
+extern void git_config_iter(config_fn_t fn, void *data);
+extern int git_config_get_string(const char *key, const char **dest);
+extern int git_config_get_int(const char *key, int *dest);
+extern int git_config_get_ulong(const char *key, unsigned long *dest);
+extern int git_config_get_bool(const char *key, int *dest);
+extern int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest);
+extern int git_config_get_maybe_bool(const char *key, int *dest);
+extern int git_config_get_pathname(const char *key, const char **dest);
+
 extern int committer_ident_sufficiently_given(void);
 extern int author_ident_sufficiently_given(void);

diff --git a/config.c b/config.c
index ba882a1..d08c768 100644
--- a/config.c
+++ b/config.c
@@ -9,6 +9,8 @@
 #include "exec_cmd.h"
 #include "strbuf.h"
 #include "quote.h"
+#include "hashmap.h"
+#include "string-list.h"

 struct config_source {
 	struct config_source *prev;
@@ -33,10 +35,23 @@ struct config_source {
 	long (*do_ftell)(struct config_source *c);
 };

+struct config_set_element {
+	struct hashmap_entry ent;
+	char *key;
+	struct string_list value_list;
+};
+
 static struct config_source *cf;

 static int zlib_compression_seen;

+/*
+ * Default config_set that contains key-value pairs from the usual set of config
+ * config files (i.e repo specific .git/config, user wide ~/.gitconfig, XDG
+ * config file and the global /etc/gitconfig)
+ */
+static struct config_set the_config_set;
+
 static int config_file_fgetc(struct config_source *conf)
 {
 	return fgetc(conf->u.file);
@@ -1212,6 +1227,252 @@ int git_config(config_fn_t fn, void *data)
 	return git_config_with_options(fn, data, NULL, 1);
 }

+static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
+{
+	struct config_set_element k;
+	struct config_set_element *found_entry;
+	char *normalized_key;
+	int ret;
+	/*
+	 * `key` may come from the user, so normalize it before using it
+	 * for querying entries from the hashmap.
+	 */
+	ret = git_config_parse_key(key, &normalized_key, NULL);
+
+	if (ret)
+		return NULL;
+
+	hashmap_entry_init(&k, strhash(normalized_key));
+	k.key = normalized_key;
+	found_entry = hashmap_get(&cs->config_hash, &k, NULL);
+	free(normalized_key);
+	return found_entry;
+}
+
+static int configset_add_value(struct config_set *cs, const char *key, const char *value)
+{
+	struct config_set_element *e;
+	e = configset_find_element(cs, key);
+	/*
+	 * Since the keys are being fed by git_config*() callback mechanism, they
+	 * are already normalized. So simply add them without any further munging.
+	 */
+	if (!e) {
+		e = xmalloc(sizeof(*e));
+		hashmap_entry_init(e, strhash(key));
+		e->key = xstrdup(key);
+		memset(&e->value_list, 0, sizeof(e->value_list));
+		e->value_list.strdup_strings = 1;
+		hashmap_add(&cs->config_hash, e);
+	}
+	string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
+
+	return 0;
+}
+
+static int config_set_element_cmp(const struct config_set_element *e1,
+				 const struct config_set_element *e2, const void *unused)
+{
+	return strcmp(e1->key, e2->key);
+}
+
+void git_configset_init(struct config_set *cs)
+{
+	hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
+	cs->hash_initialized = 1;
+}
+
+void git_configset_clear(struct config_set *cs)
+{
+	struct config_set_element *entry;
+	struct hashmap_iter iter;
+	if (!cs->hash_initialized)
+		return;
+
+	hashmap_iter_init(&cs->config_hash, &iter);
+	while ((entry = hashmap_iter_next(&iter))) {
+		free(entry->key);
+		string_list_clear(&entry->value_list, 0);
+	}
+	hashmap_free(&cs->config_hash, 1);
+	cs->hash_initialized = 0;
+}
+
+static int config_set_callback(const char *key, const char *value, void *cb)
+{
+	struct config_set *cs = cb;
+	configset_add_value(cs, key, value);
+	return 0;
+}
+
+int git_configset_add_file(struct config_set *cs, const char *filename)
+{
+	return git_config_from_file(config_set_callback, filename, cs);
+}
+
+int git_configset_get_value(struct config_set *cs, const char *key, const char **value)
+{
+	const struct string_list *values = NULL;
+	/*
+	 * Follows "last one wins" semantic, i.e., if there are multiple matches for the
+	 * queried key in the files of the configset, the value returned will be the last
+	 * value in the value list for that key.
+	 */
+	values = git_configset_get_value_multi(cs, key);
+
+	if (!values)
+		return 1;
+	assert(values->nr > 0);
+	*value = values->items[values->nr - 1].string;
+	return 0;
+}
+
+const struct string_list *git_configset_get_value_multi(struct config_set *cs, const char *key)
+{
+	struct config_set_element *e = configset_find_element(cs, key);
+	return e ? &e->value_list : NULL;
+}
+
+int git_configset_get_string(struct config_set *cs, const char *key, const char **dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value))
+		return git_config_string(dest, key, value);
+	else
+		return 1;
+}
+
+int git_configset_get_int(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_int(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_ulong(struct config_set *cs, const char *key, unsigned long *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_ulong(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_bool(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_bool(key, value);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_bool_or_int(struct config_set *cs, const char *key,
+				int *is_bool, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_bool_or_int(key, value, is_bool);
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_maybe_bool(struct config_set *cs, const char *key, int *dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value)) {
+		*dest = git_config_maybe_bool(key, value);
+		if (*dest == -1)
+			return -1;
+		return 0;
+	} else
+		return 1;
+}
+
+int git_configset_get_pathname(struct config_set *cs, const char *key, const char **dest)
+{
+	const char *value;
+	if (!git_configset_get_value(cs, key, &value))
+		return git_config_pathname(dest, key, value);
+	else
+		return 1;
+}
+
+static void git_config_check_init(void)
+{
+	if (the_config_set.hash_initialized)
+		return;
+	git_configset_init(&the_config_set);
+	git_config(config_set_callback, &the_config_set);
+}
+
+void git_config_clear(void)
+{
+	if (!the_config_set.hash_initialized)
+		return;
+	git_configset_clear(&the_config_set);
+}
+
+int git_config_get_value(const char *key, const char **value)
+{
+	git_config_check_init();
+	return git_configset_get_value(&the_config_set, key, value);
+}
+
+const struct string_list *git_config_get_value_multi(const char *key)
+{
+	git_config_check_init();
+	return git_configset_get_value_multi(&the_config_set, key);
+}
+
+int git_config_get_string(const char *key, const char **dest)
+{
+	git_config_check_init();
+	return git_configset_get_string(&the_config_set, key, dest);
+}
+
+int git_config_get_int(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_int(&the_config_set, key, dest);
+}
+
+int git_config_get_ulong(const char *key, unsigned long *dest)
+{
+	git_config_check_init();
+	return git_configset_get_ulong(&the_config_set, key, dest);
+}
+
+int git_config_get_bool(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_bool(&the_config_set, key, dest);
+}
+
+int git_config_get_bool_or_int(const char *key, int *is_bool, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_bool_or_int(&the_config_set, key, is_bool, dest);
+}
+
+int git_config_get_maybe_bool(const char *key, int *dest)
+{
+	git_config_check_init();
+	return git_configset_get_maybe_bool(&the_config_set, key, dest);
+}
+
+int git_config_get_pathname(const char *key, const char **dest)
+{
+	git_config_check_init();
+	return git_configset_get_pathname(&the_config_set, key, dest);
+}
+
 /*
  * Find all the stuff for git_config_set() below.
  */
@@ -1707,6 +1968,9 @@ int git_config_set_multivar_in_file(const char *config_filename,
 	lock = NULL;
 	ret = 0;

+	/* Invalidate the config cache */
+	git_config_clear();
+
 out_free:
 	if (lock)
 		rollback_lock_file(lock);
-- 
1.9.0.GIT

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH v9r2 2/2] test-config: add tests for the config_set API
  2014-07-16 11:49       ` Matthieu Moy
  2014-07-16 12:22         ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
@ 2014-07-16 12:22         ` Tanay Abhra
  1 sibling, 0 replies; 26+ messages in thread
From: Tanay Abhra @ 2014-07-16 12:22 UTC (permalink / raw
  To: Matthieu Moy; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Expose the `config_set` C API as a set of simple commands in order to
facilitate testing. Add tests for the `config_set` API as well as for
`git_config_get_*()` family for the usual config files.

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
---
 .gitignore            |   1 +
 Makefile              |   1 +
 t/t1308-config-set.sh | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++
 test-config.c         | 140 +++++++++++++++++++++++++++++++++++
 4 files changed, 342 insertions(+)
 create mode 100755 t/t1308-config-set.sh
 create mode 100644 test-config.c

diff --git a/.gitignore b/.gitignore
index edb1dcf..fcdee42 100644
--- a/.gitignore
+++ b/.gitignore
@@ -178,6 +178,7 @@
 /gitweb/static/gitweb.min.*
 /test-chmtime
 /test-ctype
+/test-config
 /test-date
 /test-delta
 /test-dump-cache-tree
diff --git a/Makefile b/Makefile
index b92418d..e070eb8 100644
--- a/Makefile
+++ b/Makefile
@@ -549,6 +549,7 @@ PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))

 TEST_PROGRAMS_NEED_X += test-chmtime
 TEST_PROGRAMS_NEED_X += test-ctype
+TEST_PROGRAMS_NEED_X += test-config
 TEST_PROGRAMS_NEED_X += test-date
 TEST_PROGRAMS_NEED_X += test-delta
 TEST_PROGRAMS_NEED_X += test-dump-cache-tree
diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
new file mode 100755
index 0000000..4752fd9
--- /dev/null
+++ b/t/t1308-config-set.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+test_description='Test git config-set API in different settings'
+
+. ./test-lib.sh
+
+# 'check_config get_* section.key value' verifies that the entry for
+# section.key is 'value'
+check_config () {
+	if test "$1" = expect_code
+	then
+		expect_code="$2" && shift && shift
+	else
+		expect_code=0
+	fi &&
+	op=$1 key=$2 && shift && shift &&
+	if test $# != 0
+	then
+		printf "%s\n" "$@"
+	fi >expect &&
+	test_expect_code $expect_code test-config "$op" "$key" >actual &&
+	test_cmp expect actual
+}
+
+test_expect_success 'setup default config' '
+	cat >.git/config <<\EOF
+	[case]
+		penguin = very blue
+		Movie = BadPhysics
+		UPPERCASE = true
+		MixedCase = true
+		my =
+		foo
+		baz = sam
+	[Cores]
+		WhatEver = Second
+		baz = bar
+	[cores]
+		baz = bat
+	[CORES]
+		baz = ball
+	[my "Foo bAr"]
+		hi = mixed-case
+	[my "FOO BAR"]
+		hi = upper-case
+	[my "foo bar"]
+		hi = lower-case
+	[case]
+		baz = bat
+		baz = hask
+	[lamb]
+		chop = 65
+		head = none
+	[goat]
+		legs = 4
+		head = true
+		skin = false
+		nose = 1
+		horns
+	EOF
+'
+
+test_expect_success 'get value for a simple key' '
+	check_config get_value case.penguin "very blue"
+'
+
+test_expect_success 'get value for a key with value as an empty string' '
+	check_config get_value case.my ""
+'
+
+test_expect_success 'get value for a key with value as NULL' '
+	check_config get_value case.foo "(NULL)"
+'
+
+test_expect_success 'upper case key' '
+	check_config get_value case.UPPERCASE "true" &&
+	check_config get_value case.uppercase "true"
+'
+
+test_expect_success 'mixed case key' '
+	check_config get_value case.MixedCase "true" &&
+	check_config get_value case.MIXEDCASE "true" &&
+	check_config get_value case.mixedcase "true"
+'
+
+test_expect_success 'key and value with mixed case' '
+	check_config get_value case.Movie "BadPhysics"
+'
+
+test_expect_success 'key with case sensitive subsection' '
+	check_config get_value "my.Foo bAr.hi" "mixed-case" &&
+	check_config get_value "my.FOO BAR.hi" "upper-case" &&
+	check_config get_value "my.foo bar.hi" "lower-case"
+'
+
+test_expect_success 'key with case insensitive section header' '
+	check_config get_value cores.baz "ball" &&
+	check_config get_value Cores.baz "ball" &&
+	check_config get_value CORES.baz "ball" &&
+	check_config get_value coreS.baz "ball"
+'
+
+test_expect_success 'key with case insensitive section header & variable' '
+	check_config get_value CORES.BAZ "ball" &&
+	check_config get_value cores.baz "ball" &&
+	check_config get_value cores.BaZ "ball" &&
+	check_config get_value cOreS.bAz "ball"
+'
+
+test_expect_success 'find value with misspelled key' '
+	check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\""
+'
+
+test_expect_success 'find value with the highest priority' '
+	check_config get_value case.baz "hask"
+'
+
+test_expect_success 'find integer value for a key' '
+	check_config get_int lamb.chop 65
+'
+
+test_expect_success 'find integer if value is non parse-able' '
+	check_config expect_code 128 get_int lamb.head
+'
+
+test_expect_success 'find bool value for the entered key' '
+	check_config get_bool goat.head 1 &&
+	check_config get_bool goat.skin 0 &&
+	check_config get_bool goat.nose 1 &&
+	check_config get_bool goat.horns 1 &&
+	check_config get_bool goat.legs 1
+'
+
+test_expect_success 'find multiple values' '
+	check_config get_value_multi case.baz sam bat hask
+'
+
+test_expect_success 'find value from a configset' '
+	cat >config2 <<-\EOF &&
+	[case]
+		baz = lama
+	[my]
+		new = silk
+	[case]
+		baz = ball
+	EOF
+	echo silk >expect &&
+	test-config configset_get_value my.new config2 .git/config >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'find value with highest priority from a configset' '
+	echo hask > expect &&
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'find value_list for a key from a configset' '
+	cat >except <<-\EOF &&
+	sam
+	bat
+	hask
+	lama
+	ball
+	EOF
+	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-existant files' '
+	echo "Error reading configuration file non-existant-file." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar non-existant-file 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-accessible  files' '
+	chmod -r .git/config &&
+	test_when_finished "chmod +r .git/config" &&
+	echo "Error reading configuration file .git/config." >expect &&
+	test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in default config files' '
+	cp .git/config .git/config.old &&
+	test_when_finished "mv .git/config.old .git/config" &&
+	echo "[" >> .git/config &&
+	echo "fatal: bad config file line 35 in .git/config" >expect &&
+	test_expect_code 128 test-config get_value foo.bar 2>actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in custom config files' '
+	echo "[" >> syntax-error &&
+	echo "fatal: bad config file line 1 in syntax-error" >expect &&
+	test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
+	test_cmp expect actual
+'
+
+test_done
diff --git a/test-config.c b/test-config.c
new file mode 100644
index 0000000..cad35f4
--- /dev/null
+++ b/test-config.c
@@ -0,0 +1,140 @@
+#include "cache.h"
+#include "string-list.h"
+
+/*
+ * This program exposes the C API of the configuration mechanism
+ * as a set of simple commands in order to facilitate testing.
+ *
+ * Reads stdin and prints result of command to stdout:
+ *
+ * get_value -> prints the value with highest priority for the entered key
+ *
+ * get_value_multi -> prints all values for the entered key in increasing order
+ *		     of priority
+ *
+ * get_int -> print integer value for the entered key or die
+ *
+ * get_bool -> print bool value for the entered key or die
+ *
+ * configset_get_value -> returns value with the highest priority for the entered key
+ * 			from a config_set constructed from files entered as arguments.
+ *
+ * configset_get_value_multi -> returns value_list for the entered key sorted in
+ * 				ascending order of priority from a config_set
+ * 				constructed from files entered as arguments.
+ *
+ * Examples:
+ *
+ * To print the value with highest priority for key "foo.bAr Baz.rock":
+ * 	test-config get_value "foo.bAr Baz.rock"
+ *
+ */
+
+
+int main(int argc, char **argv)
+{
+	int i, val;
+	const char *v;
+	const struct string_list *strptr;
+	struct config_set cs;
+	git_configset_init(&cs);
+
+	if (argc < 2) {
+		fprintf(stderr, "Please, provide a command name on the command-line\n");
+		goto exit1;
+	} else if (argc == 3 && !strcmp(argv[1], "get_value")) {
+		if (!git_config_get_value(argv[2], &v)) {
+			if (!v)
+				printf("(NULL)\n");
+			else
+				printf("%s\n", v);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
+		strptr = git_config_get_value_multi(argv[2]);
+		if (strptr) {
+			for (i = 0; i < strptr->nr; i++) {
+				v = strptr->items[i].string;
+				if (!v)
+					printf("(NULL)\n");
+				else
+					printf("%s\n", v);
+			}
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_int")) {
+		if (!git_config_get_int(argv[2], &val)) {
+			printf("%d\n", val);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
+		if (!git_config_get_bool(argv[2], &val)) {
+			printf("%d\n", val);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (!strcmp(argv[1], "configset_get_value")) {
+		for (i = 3; i < argc; i++) {
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
+		}
+		if (!git_configset_get_value(&cs, argv[2], &v)) {
+			if (!v)
+				printf("(NULL)\n");
+			else
+				printf("%s\n", v);
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	} else if (!strcmp(argv[1], "configset_get_value_multi")) {
+		for (i = 3; i < argc; i++) {
+			if (git_configset_add_file(&cs, argv[i])) {
+				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+				goto exit2;
+			}
+		}
+		strptr = git_configset_get_value_multi(&cs, argv[2]);
+		if (strptr) {
+			for (i = 0; i < strptr->nr; i++) {
+				v = strptr->items[i].string;
+				if (!v)
+					printf("(NULL)\n");
+				else
+					printf("%s\n", v);
+			}
+			goto exit0;
+		} else {
+			printf("Value not found for \"%s\"\n", argv[2]);
+			goto exit1;
+		}
+	}
+
+	die("%s: Please check the syntax and the function name", argv[0]);
+
+exit0:
+	git_configset_clear(&cs);
+	return 0;
+
+exit1:
+	git_configset_clear(&cs);
+	return 1;
+
+exit2:
+	git_configset_clear(&cs);
+	return 2;
+}
-- 
1.9.0.GIT

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-16 12:22         ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
@ 2014-07-16 16:06           ` Matthieu Moy
  2014-07-16 16:09             ` [PATCH 1/3] fixup for patch 2: minor style fix Matthieu Moy
  2014-07-16 16:44             ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
  0 siblings, 2 replies; 26+ messages in thread
From: Matthieu Moy @ 2014-07-16 16:06 UTC (permalink / raw
  To: Tanay Abhra; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> implemented as a thin wrapper around the `config_set` API.
>
> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
>  Documentation/technical/api-config.txt | 137 +++++++++++++++++
>  cache.h                                |  30 ++++
>  config.c                               | 264 +++++++++++++++++++++++++++++++++
>  3 files changed, 431 insertions(+)

(you broke the patch by removing the ---)

> +static void git_config_check_init(void)
> +{
> +	if (the_config_set.hash_initialized)
> +		return;
> +	git_configset_init(&the_config_set);
> +	git_config(config_set_callback, &the_config_set);
> +}

So, you're now ignoring the return value of git_config. What is the
rationale for this? In particular, why did you reject the "die"
possibility (I understood that you were inclined to take this option, so
I'm curious why you changed your mind).

OTOH, you're transmitting the return value without dying here:

+int git_configset_add_file(struct config_set *cs, const char *filename)
+{
+	return git_config_from_file(config_set_callback, filename, cs);
+}

and I think this one is correct, as we cannot tell in advance how
serious an error would be for any callers. And we do test this (I think
we can improve a bit, I'll send a fixup patch).

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* [PATCH 1/3] fixup for patch 2: minor style fix
  2014-07-16 16:06           ` Matthieu Moy
@ 2014-07-16 16:09             ` Matthieu Moy
  2014-07-16 16:09               ` [PATCH 2/3] fixup for patch 2: actually check the return value Matthieu Moy
  2014-07-16 16:09               ` [PATCH 3/3] fixup for patch 1: typo Matthieu Moy
  2014-07-16 16:44             ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
  1 sibling, 2 replies; 26+ messages in thread
From: Matthieu Moy @ 2014-07-16 16:09 UTC (permalink / raw
  To: git, gitster; +Cc: artagnon, tanayabh, Matthieu Moy

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 t/t1308-config-set.sh | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
index 4752fd9..ea031bf 100755
--- a/t/t1308-config-set.sh
+++ b/t/t1308-config-set.sh
@@ -150,8 +150,8 @@ test_expect_success 'find value from a configset' '
 '
 
 test_expect_success 'find value with highest priority from a configset' '
-	echo hask > expect &&
-	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	echo hask >expect &&
+	test-config configset_get_value case.baz config2 .git/config >actual &&
 	test_cmp expect actual
 '
 
@@ -163,7 +163,7 @@ test_expect_success 'find value_list for a key from a configset' '
 	lama
 	ball
 	EOF
-	test-config configset_get_value case.baz config2 .git/config  >actual &&
+	test-config configset_get_value case.baz config2 .git/config >actual &&
 	test_cmp expect actual
 '
 
@@ -173,7 +173,7 @@ test_expect_success 'proper error on non-existant files' '
 	test_cmp expect actual
 '
 
-test_expect_success 'proper error on non-accessible  files' '
+test_expect_success 'proper error on non-accessible files' '
 	chmod -r .git/config &&
 	test_when_finished "chmod +r .git/config" &&
 	echo "Error reading configuration file .git/config." >expect &&
@@ -184,14 +184,14 @@ test_expect_success 'proper error on non-accessible  files' '
 test_expect_success 'proper error on error in default config files' '
 	cp .git/config .git/config.old &&
 	test_when_finished "mv .git/config.old .git/config" &&
-	echo "[" >> .git/config &&
+	echo "[" >>.git/config &&
 	echo "fatal: bad config file line 35 in .git/config" >expect &&
 	test_expect_code 128 test-config get_value foo.bar 2>actual &&
 	test_cmp expect actual
 '
 
 test_expect_success 'proper error on error in custom config files' '
-	echo "[" >> syntax-error &&
+	echo "[" >>syntax-error &&
 	echo "fatal: bad config file line 1 in syntax-error" >expect &&
 	test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
 	test_cmp expect actual
-- 
2.0.0.262.gdafc651

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 2/3] fixup for patch 2: actually check the return value
  2014-07-16 16:09             ` [PATCH 1/3] fixup for patch 2: minor style fix Matthieu Moy
@ 2014-07-16 16:09               ` Matthieu Moy
  2014-07-16 16:55                 ` Tanay Abhra
  2014-07-16 16:09               ` [PATCH 3/3] fixup for patch 1: typo Matthieu Moy
  1 sibling, 1 reply; 26+ messages in thread
From: Matthieu Moy @ 2014-07-16 16:09 UTC (permalink / raw
  To: git, gitster; +Cc: artagnon, tanayabh, Matthieu Moy

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
I won't fight for this, but I think it makes sense.

 t/t1308-config-set.sh |  4 ++--
 test-config.c         | 10 ++++++----
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
index ea031bf..f0307b7 100755
--- a/t/t1308-config-set.sh
+++ b/t/t1308-config-set.sh
@@ -168,7 +168,7 @@ test_expect_success 'find value_list for a key from a configset' '
 '
 
 test_expect_success 'proper error on non-existant files' '
-	echo "Error reading configuration file non-existant-file." >expect &&
+	echo "Error (-1) reading configuration file non-existant-file." >expect &&
 	test_expect_code 2 test-config configset_get_value foo.bar non-existant-file 2>actual &&
 	test_cmp expect actual
 '
@@ -176,7 +176,7 @@ test_expect_success 'proper error on non-existant files' '
 test_expect_success 'proper error on non-accessible files' '
 	chmod -r .git/config &&
 	test_when_finished "chmod +r .git/config" &&
-	echo "Error reading configuration file .git/config." >expect &&
+	echo "Error (-1) reading configuration file .git/config." >expect &&
 	test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
 	test_cmp expect actual
 '
diff --git a/test-config.c b/test-config.c
index cad35f4..9dd1b22 100644
--- a/test-config.c
+++ b/test-config.c
@@ -86,8 +86,9 @@ int main(int argc, char **argv)
 		}
 	} else if (!strcmp(argv[1], "configset_get_value")) {
 		for (i = 3; i < argc; i++) {
-			if (git_configset_add_file(&cs, argv[i])) {
-				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+			int err;
+			if ((err = git_configset_add_file(&cs, argv[i]))) {
+				fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
 				goto exit2;
 			}
 		}
@@ -103,8 +104,9 @@ int main(int argc, char **argv)
 		}
 	} else if (!strcmp(argv[1], "configset_get_value_multi")) {
 		for (i = 3; i < argc; i++) {
-			if (git_configset_add_file(&cs, argv[i])) {
-				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
+			int err;
+			if ((err = git_configset_add_file(&cs, argv[i]))) {
+				fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
 				goto exit2;
 			}
 		}
-- 
2.0.0.262.gdafc651

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* [PATCH 3/3] fixup for patch 1: typo
  2014-07-16 16:09             ` [PATCH 1/3] fixup for patch 2: minor style fix Matthieu Moy
  2014-07-16 16:09               ` [PATCH 2/3] fixup for patch 2: actually check the return value Matthieu Moy
@ 2014-07-16 16:09               ` Matthieu Moy
  1 sibling, 0 replies; 26+ messages in thread
From: Matthieu Moy @ 2014-07-16 16:09 UTC (permalink / raw
  To: git, gitster; +Cc: artagnon, tanayabh, Matthieu Moy

Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
---
 Documentation/technical/api-config.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Documentation/technical/api-config.txt b/Documentation/technical/api-config.txt
index fc0e379..8a86e45 100644
--- a/Documentation/technical/api-config.txt
+++ b/Documentation/technical/api-config.txt
@@ -242,7 +242,7 @@ Configset API provides functions for the above mentioned work flow, including:
 
 	Parses the file and adds the variable-value pairs to the `config_set`,
 	dies if there is an error in parsing the file. Returns 0 on success, or
-	-1 if the file doesnot exist or is inaccessible. The user has to decide
+	-1 if the file does not exist or is inaccessible. The user has to decide
 	if he wants to free the incomplete configset or continue using it when
 	the function returns -1.
 
-- 
2.0.0.262.gdafc651

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-16 16:06           ` Matthieu Moy
  2014-07-16 16:09             ` [PATCH 1/3] fixup for patch 2: minor style fix Matthieu Moy
@ 2014-07-16 16:44             ` Tanay Abhra
  2014-07-16 17:06               ` Matthieu Moy
  1 sibling, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-16 16:44 UTC (permalink / raw
  To: Matthieu Moy; +Cc: Junio C Hamano, git, Ramkumar Ramachandra



On 7/16/2014 9:36 PM, Matthieu Moy wrote:
> Tanay Abhra <tanayabh@gmail.com> writes:
> 
>> implemented as a thin wrapper around the `config_set` API.
>>
>> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
>> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
>>  Documentation/technical/api-config.txt | 137 +++++++++++++++++
>>  cache.h                                |  30 ++++
>>  config.c                               | 264 +++++++++++++++++++++++++++++++++
>>  3 files changed, 431 insertions(+)
> 
> (you broke the patch by removing the ---)
>

Yikes, sorry about that.

>> +static void git_config_check_init(void)
>> +{
>> +	if (the_config_set.hash_initialized)
>> +		return;
>> +	git_configset_init(&the_config_set);
>> +	git_config(config_set_callback, &the_config_set);
>> +}
> 
> So, you're now ignoring the return value of git_config. What is the
> rationale for this? In particular, why did you reject the "die"
> possibility (I understood that you were inclined to take this option, so
> I'm curious why you changed your mind).
>

The errors (non accessible, non existent files etc) were already being caught by
git_config_early(). Since git_config() only returns positive values except
the weird race case you mentioned, I thought the die confused the reader
of the patch more than it provided error checking. I also tried myself
simulating the race condition but failed. All the callers of git_config()
also ignore the return value, so I ended up ignoring the return value myself.

> OTOH, you're transmitting the return value without dying here:
> 
> +int git_configset_add_file(struct config_set *cs, const char *filename)
> +{
> +	return git_config_from_file(config_set_callback, filename, cs);
> +}
> 
> and I think this one is correct, as we cannot tell in advance how
> serious an error would be for any callers. And we do test this (I think
> we can improve a bit, I'll send a fixup patch).
>

After reading the commit log that you mentioned and some previous ones before
that I surmised that the official slant was to silently ignore nonexistent
files. Though an access_or_warn() check was placed on most of the files
like git attributes, since non accessible file errors may be a user configuration
error. So, I decided to ignore the return value.

But I do think that an access_or_warn() check should be put on git config --file
and git_configset_add_file since other parts of git follow it. What do
you think about it, still I will send followup patch correcting the git config
--file condition where it silently ignores the file access error and continues?

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/3] fixup for patch 2: actually check the return value
  2014-07-16 16:09               ` [PATCH 2/3] fixup for patch 2: actually check the return value Matthieu Moy
@ 2014-07-16 16:55                 ` Tanay Abhra
  2014-07-16 17:10                   ` Matthieu Moy
  0 siblings, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-16 16:55 UTC (permalink / raw
  To: Matthieu Moy, git, gitster; +Cc: artagnon

I think it would be unnecessary for the current iteration.
Currently git_configset_add_file has only two possible return values
-1 or 0. I could add specialized error values for ENOENT or ENOTDIR
or EACCES, but the logs show that we silently ignore the first two.
I can add an access warn for the third. What do you think?

Thanks,
Tanay.

On 7/16/2014 9:39 PM, Matthieu Moy wrote:
> Signed-off-by: Matthieu Moy <Matthieu.Moy@imag.fr>
> ---
> I won't fight for this, but I think it makes sense.
> 
>  t/t1308-config-set.sh |  4 ++--
>  test-config.c         | 10 ++++++----
>  2 files changed, 8 insertions(+), 6 deletions(-)
> 
> diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
> index ea031bf..f0307b7 100755
> --- a/t/t1308-config-set.sh
> +++ b/t/t1308-config-set.sh
> @@ -168,7 +168,7 @@ test_expect_success 'find value_list for a key from a configset' '
>  '
>  
>  test_expect_success 'proper error on non-existant files' '
> -	echo "Error reading configuration file non-existant-file." >expect &&
> +	echo "Error (-1) reading configuration file non-existant-file." >expect &&
>  	test_expect_code 2 test-config configset_get_value foo.bar non-existant-file 2>actual &&
>  	test_cmp expect actual
>  '
> @@ -176,7 +176,7 @@ test_expect_success 'proper error on non-existant files' '
>  test_expect_success 'proper error on non-accessible files' '
>  	chmod -r .git/config &&
>  	test_when_finished "chmod +r .git/config" &&
> -	echo "Error reading configuration file .git/config." >expect &&
> +	echo "Error (-1) reading configuration file .git/config." >expect &&
>  	test_expect_code 2 test-config configset_get_value foo.bar .git/config 2>actual &&
>  	test_cmp expect actual
>  '
> diff --git a/test-config.c b/test-config.c
> index cad35f4..9dd1b22 100644
> --- a/test-config.c
> +++ b/test-config.c
> @@ -86,8 +86,9 @@ int main(int argc, char **argv)
>  		}
>  	} else if (!strcmp(argv[1], "configset_get_value")) {
>  		for (i = 3; i < argc; i++) {
> -			if (git_configset_add_file(&cs, argv[i])) {
> -				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
> +			int err;
> +			if ((err = git_configset_add_file(&cs, argv[i]))) {
> +				fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
>  				goto exit2;
>  			}
>  		}
> @@ -103,8 +104,9 @@ int main(int argc, char **argv)
>  		}
>  	} else if (!strcmp(argv[1], "configset_get_value_multi")) {
>  		for (i = 3; i < argc; i++) {
> -			if (git_configset_add_file(&cs, argv[i])) {
> -				fprintf(stderr, "Error reading configuration file %s.\n", argv[i]);
> +			int err;
> +			if ((err = git_configset_add_file(&cs, argv[i]))) {
> +				fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
>  				goto exit2;
>  			}
>  		}
> 

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-16 16:44             ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
@ 2014-07-16 17:06               ` Matthieu Moy
       [not found]                 ` <53C6C2BD.3030703@gmail.com>
  0 siblings, 1 reply; 26+ messages in thread
From: Matthieu Moy @ 2014-07-16 17:06 UTC (permalink / raw
  To: Tanay Abhra; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> On 7/16/2014 9:36 PM, Matthieu Moy wrote:
>> Tanay Abhra <tanayabh@gmail.com> writes:
>>> +static void git_config_check_init(void)
>>> +{
>>> +	if (the_config_set.hash_initialized)
>>> +		return;
>>> +	git_configset_init(&the_config_set);
>>> +	git_config(config_set_callback, &the_config_set);
>>> +}
>> 
>> So, you're now ignoring the return value of git_config. What is the
>> rationale for this? In particular, why did you reject the "die"
>> possibility (I understood that you were inclined to take this option, so
>> I'm curious why you changed your mind).
>>
>
> The errors (non accessible, non existent files etc) were already being caught by
> git_config_early(). Since git_config() only returns positive values except
> the weird race case you mentioned, I thought the die confused the reader
> of the patch more than it provided error checking. I also tried myself
> simulating the race condition but failed. All the callers of git_config()
> also ignore the return value, so I ended up ignoring the return value myself.

OK. My preference would be to die, but your argument makes sense.

> But I do think that an access_or_warn() check should be put on git config --file
> and git_configset_add_file since other parts of git follow it. What do
> you think about it, still I will send followup patch correcting the git config
> --file condition where it silently ignores the file access error and continues?

I think it should be done, but should not be your priority (i.e. it's
good to do it, but only if the patch is trivial enough).

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH 2/3] fixup for patch 2: actually check the return value
  2014-07-16 16:55                 ` Tanay Abhra
@ 2014-07-16 17:10                   ` Matthieu Moy
  0 siblings, 0 replies; 26+ messages in thread
From: Matthieu Moy @ 2014-07-16 17:10 UTC (permalink / raw
  To: Tanay Abhra; +Cc: git, gitster, artagnon

Tanay Abhra <tanayabh@gmail.com> writes:

> I think it would be unnecessary for the current iteration.
> Currently git_configset_add_file has only two possible return values
> -1 or 0.

Yes. My point is just to check that statement in tests. (I'm usually
wary of statements like "is obviously true so I don't need to test
it" ;-) )

> I could add specialized error values for ENOENT or ENOTDIR or EACCES,
> but the logs show that we silently ignore the first two. I can add an
> access warn for the third. What do you think?

I think the code is fine as it is.

But anyway, it's not terribly important.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
       [not found]                 ` <53C6C2BD.3030703@gmail.com>
@ 2014-07-17 10:01                   ` Matthieu Moy
  2014-07-17 11:06                     ` Tanay Abhra
  2014-07-17 11:13                     ` Matthieu Moy
  0 siblings, 2 replies; 26+ messages in thread
From: Matthieu Moy @ 2014-07-17 10:01 UTC (permalink / raw
  To: Tanay Abhra; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> I have a question about renaming git_config() to git_config_raw().
> I was working on a patch and I am getting stuck here.
> It fails in t1001, t1020 & t4018. The reason for the last test failure
> is unknown right now. For the first two, it boils down to this,
>
> 		git init --bare bare-ancestor-aliased.git &&
> 		cd bare-ancestor-aliased.git &&
> 		echo "[alias] aliasedinit = init" >>config &&
> 		mkdir plain-nested &&
> 		cd plain-nested &&
> 		git aliasedinit

Git tries to read .git/config relative to the current directory, and
tries to resolve aliases from it. The problem is: if one tries to do
this from a subdirectory inside the repo, .git/config is not the right
path, and the alias lookup fails.

I'll investigate more later.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-17 10:01                   ` Matthieu Moy
@ 2014-07-17 11:06                     ` Tanay Abhra
  2014-07-17 11:34                       ` Matthieu Moy
  2014-07-17 11:13                     ` Matthieu Moy
  1 sibling, 1 reply; 26+ messages in thread
From: Tanay Abhra @ 2014-07-17 11:06 UTC (permalink / raw
  To: Matthieu Moy; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

On 7/17/2014 3:31 PM, Matthieu Moy wrote:
> Tanay Abhra <tanayabh@gmail.com> writes:
> 
>> I have a question about renaming git_config() to git_config_raw().
>> I was working on a patch and I am getting stuck here.
>> It fails in t1001, t1020 & t4018. The reason for the last test failure
>> is unknown right now. For the first two, it boils down to this,
>>
>> 		git init --bare bare-ancestor-aliased.git &&
>> 		cd bare-ancestor-aliased.git &&
>> 		echo "[alias] aliasedinit = init" >>config &&
>> 		mkdir plain-nested &&
>> 		cd plain-nested &&
>> 		git aliasedinit
> 
> Git tries to read .git/config relative to the current directory, and
> tries to resolve aliases from it. The problem is: if one tries to do
> this from a subdirectory inside the repo, .git/config is not the right
> path, and the alias lookup fails.
> 
> I'll investigate more later.
>

Hmn, this does the trick,
-- 8< --
diff --git a/cache.h b/cache.h
index c67639d..66b52f1 100644
--- a/cache.h
+++ b/cache.h
@@ -1272,6 +1272,7 @@ extern int git_config_from_buf(config_fn_t fn, const char *name,
 			       const char *buf, size_t len, void *data);
 extern void git_config_push_parameter(const char *text);
 extern int git_config_from_parameters(config_fn_t fn, void *data);
+extern int git_config_raw(config_fn_t fn, void *);
 extern int git_config(config_fn_t fn, void *);
 extern int git_config_with_options(config_fn_t fn, void *,
 				   struct git_config_source *config_source,
diff --git a/config.c b/config.c
index d14f761..9e3f99a 100644
--- a/config.c
+++ b/config.c
@@ -1222,11 +1222,36 @@ int git_config_with_options(config_fn_t fn, void *data,
 	return ret;
 }

-int git_config(config_fn_t fn, void *data)
+extern int git_config_raw(config_fn_t fn, void *data)
 {
 	return git_config_with_options(fn, data, NULL, 1);
 }

+int git_configset_iter(struct config_set *cs, config_fn_t fn, void *data)
+{
+	int i;
+	struct string_list *strptr;
+	struct config_set_element *entry;
+	struct hashmap_iter iter;
+	hashmap_iter_init(&cs->config_hash, &iter);
+	while ((entry = hashmap_iter_next(&iter))) {
+		strptr = &entry->value_list;
+		for (i = 0; i < strptr->nr; i++) {
+			if (fn(entry->key, strptr->items[i].string, data) < 0)
+				die("bad config file");
+		}
+	}
+	return 0;
+}
+
+static void git_config_check_init(void);
+
+int git_config(config_fn_t fn, void *data)
+{
+	git_config_check_init();
+	return git_configset_iter(&the_config_set, fn, data);
+}
+
 static struct config_set_element *configset_find_element(struct config_set *cs, const char *key)
 {
 	struct config_set_element k;
@@ -1409,7 +1434,7 @@ static void git_config_check_init(void)
 	if (the_config_set.hash_initialized)
 		return;
 	git_configset_init(&the_config_set);
-	git_config(config_hash_callback, &the_config_set);
+	git_config_raw(config_hash_callback, &the_config_set);
 }

 void git_config_clear(void)
diff --git a/pager.c b/pager.c
index 8b5cbc5..b4237e6 100644
--- a/pager.c
+++ b/pager.c
@@ -177,7 +177,7 @@ int check_pager_config(const char *cmd)
 	c.cmd = cmd;
 	c.want = -1;
 	c.value = NULL;
-	git_config(pager_command_config, &c);
+	git_config_raw(pager_command_config, &c);
 	if (c.value)
 		pager_program = c.value;
 	return c.want;
-- 8< --

The offending part is in git.c, line number 540:

static void execv_dashed_external(const char **argv)
{
	struct strbuf cmd = STRBUF_INIT;
	const char *tmp;
	int status;

	if (use_pager == -1)
		use_pager = check_pager_config(argv[0]);
----------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^
	commit_pager_choice();


which cause git_config() to be called before handle_alias
can call it in git.c line no 587,

static int run_argv(int *argcp, const char ***argv)
{
	int done_alias = 0;

	while (1) {
		/* See if it's a builtin */
		handle_builtin(*argcp, *argv);

		/* .. then try the external ones */
		execv_dashed_external(*argv);
/* calls git_config() first, skips the .git/config file*/
		/* It could be an alias -- this works around the insanity
		 * of overriding "git log" with "git show" by having
		 * alias.log = show
		 */
		if (done_alias)
			break;
		save_env();
		if (!handle_alias(argcp, argv))
/* does setup_git_directory_gently() before calling git_config() */
			break;
		done_alias = 1;
	}

	return done_alias;
}

I am searching for a more elegant solution to this problem.
Thanks,
Tanay Abhra.

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-17 10:01                   ` Matthieu Moy
  2014-07-17 11:06                     ` Tanay Abhra
@ 2014-07-17 11:13                     ` Matthieu Moy
  2014-07-17 17:12                       ` Junio C Hamano
  1 sibling, 1 reply; 26+ messages in thread
From: Matthieu Moy @ 2014-07-17 11:13 UTC (permalink / raw
  To: Tanay Abhra; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:

> Tanay Abhra <tanayabh@gmail.com> writes:
>
>> I have a question about renaming git_config() to git_config_raw().
>> I was working on a patch and I am getting stuck here.
>> It fails in t1001, t1020 & t4018. The reason for the last test failure
>> is unknown right now. For the first two, it boils down to this,
>>
>> 		git init --bare bare-ancestor-aliased.git &&
>> 		cd bare-ancestor-aliased.git &&
>> 		echo "[alias] aliasedinit = init" >>config &&
>> 		mkdir plain-nested &&
>> 		cd plain-nested &&
>> 		git aliasedinit
>
> Git tries to read .git/config relative to the current directory, and
> tries to resolve aliases from it. The problem is: if one tries to do
> this from a subdirectory inside the repo, .git/config is not the right
> path, and the alias lookup fails.
>
> I'll investigate more later.

This fixes the first two tests (it should be squashed into your PATCH 1
regardless of the rename git_config -> git_config_raw):

commit 42315d10e21a1273b73671a3f8c9f7640c4feb44 (HEAD, config-v9)
Author: Matthieu Moy <Matthieu.Moy@imag.fr>
Date:   Thu Jul 17 13:12:21 2014 +0200

    clear the config cache in setup_git_dir

diff --git a/setup.c b/setup.c
index 0a22f8b..c0d31f5 100644
--- a/setup.c
+++ b/setup.c
@@ -625,6 +625,15 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
        int one_filesystem = 1;
 
        /*
+        * We may have read an incomplete configuration before
+        * setting-up the git directory. If so, clear the cache so
+        * that the next queries to the configuration reload complete
+        * configuration (including the per-repo config file that we
+        * ignored previously).
+        */
+       git_config_clear();
+
+       /*
         * Let's assume that we are in a git repository.
         * If it turns out later that we are somewhere else, the value will be
         * updated accordingly.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-17 11:06                     ` Tanay Abhra
@ 2014-07-17 11:34                       ` Matthieu Moy
  0 siblings, 0 replies; 26+ messages in thread
From: Matthieu Moy @ 2014-07-17 11:34 UTC (permalink / raw
  To: Tanay Abhra; +Cc: Junio C Hamano, git, Ramkumar Ramachandra

Tanay Abhra <tanayabh@gmail.com> writes:

> I am searching for a more elegant solution to this problem.

The "efficient" (not sure about elegant) solution would be to keep one
configset per file, and re-parse only the files needed.

I find the solution I posted in the other thread relatively "elegant":
invalidate the config cache when things change.

The last test failure seems more tricky. This (dirty) patch fixes the
failure:

--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -255,6 +255,8 @@ static int builtin_diff_files(struct rev_info *revs, int argc, const char **argv
        return run_diff_files(revs, options);
 }
 
+int git_config_raw(config_fn_t fn, void *data);
+
 int cmd_diff(int argc, const char **argv, const char *prefix)
 {
        int i;
@@ -317,7 +319,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 
        if (!no_index)
                gitmodules_config();
-       git_config(git_diff_ui_config, NULL);
+       git_config_raw(git_diff_ui_config, NULL);
 
        init_revisions(&rev, prefix);
 

But this one does not:

diff --git a/builtin/diff.c b/builtin/diff.c
index 0f247d2..2012e81 100644
--- a/builtin/diff.c
+++ b/builtin/diff.c
@@ -317,7 +317,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
 
        if (!no_index)
                gitmodules_config();
+       git_config_clear();
        git_config(git_diff_ui_config, NULL);
+       git_config_clear();
 
        init_revisions(&rev, prefix);
 

So it's not just a matter of invalid cache not cleared, it's a real
difference between git_config() and git_config_raw(). The guilty part is
probably userdiff_config(const char *k, const char *v) in userdiff.c,
that parses the xfuncname config option.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply related	[flat|nested] 26+ messages in thread

* Re: [PATCH v9r2 1/2] add `config_set` API for caching config-like files
  2014-07-17 11:13                     ` Matthieu Moy
@ 2014-07-17 17:12                       ` Junio C Hamano
  0 siblings, 0 replies; 26+ messages in thread
From: Junio C Hamano @ 2014-07-17 17:12 UTC (permalink / raw
  To: Matthieu Moy; +Cc: Tanay Abhra, git, Ramkumar Ramachandra

Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:

> Matthieu Moy <Matthieu.Moy@grenoble-inp.fr> writes:
>
>> Tanay Abhra <tanayabh@gmail.com> writes:
>>
>>> I have a question about renaming git_config() to git_config_raw().
>>> I was working on a patch and I am getting stuck here.
>>> It fails in t1001, t1020 & t4018. The reason for the last test failure
>>> is unknown right now. For the first two, it boils down to this,
>>>
>>> 		git init --bare bare-ancestor-aliased.git &&
>>> 		cd bare-ancestor-aliased.git &&
>>> 		echo "[alias] aliasedinit = init" >>config &&
>>> 		mkdir plain-nested &&
>>> 		cd plain-nested &&
>>> 		git aliasedinit
>>
>> Git tries to read .git/config relative to the current directory, and
>> tries to resolve aliases from it. The problem is: if one tries to do
>> this from a subdirectory inside the repo, .git/config is not the right
>> path, and the alias lookup fails.
>>
>> I'll investigate more later.
>
> This fixes the first two tests (it should be squashed into your PATCH 1
> regardless of the rename git_config -> git_config_raw):
>
> commit 42315d10e21a1273b73671a3f8c9f7640c4feb44 (HEAD, config-v9)
> Author: Matthieu Moy <Matthieu.Moy@imag.fr>
> Date:   Thu Jul 17 13:12:21 2014 +0200
>
>     clear the config cache in setup_git_dir
>
> diff --git a/setup.c b/setup.c
> index 0a22f8b..c0d31f5 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -625,6 +625,15 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
>         int one_filesystem = 1;
>  
>         /*
> +        * We may have read an incomplete configuration before
> +        * setting-up the git directory. If so, clear the cache so
> +        * that the next queries to the configuration reload complete
> +        * configuration (including the per-repo config file that we
> +        * ignored previously).
> +        */
> +       git_config_clear();

Very sensible.


> +       /*
>          * Let's assume that we are in a git repository.
>          * If it turns out later that we are somewhere else, the value will be
>          * updated accordingly.

^ permalink raw reply	[flat|nested] 26+ messages in thread

end of thread, other threads:[~2014-07-17 17:12 UTC | newest]

Thread overview: 26+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-07-15 14:29 [PATCH v9 0/3] git config cache & special querying api utilizing the cache Tanay Abhra
2014-07-15 14:29 ` [PATCH v9 1/2] add `config_set` API for caching config-like files Tanay Abhra
2014-07-15 15:46   ` Junio C Hamano
2014-07-15 16:22     ` Tanay Abhra
2014-07-16 11:41     ` [PATCH v9r1 " Tanay Abhra
2014-07-15 14:29 ` [PATCH v9 2/2] test-config: add tests for the config_set API Tanay Abhra
2014-07-15 15:57   ` Junio C Hamano
2014-07-15 17:07     ` Tanay Abhra
2014-07-15 18:26       ` Junio C Hamano
2014-07-16 11:44     ` [PATCH v9r1 " Tanay Abhra
2014-07-16 11:49       ` Matthieu Moy
2014-07-16 12:22         ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
2014-07-16 16:06           ` Matthieu Moy
2014-07-16 16:09             ` [PATCH 1/3] fixup for patch 2: minor style fix Matthieu Moy
2014-07-16 16:09               ` [PATCH 2/3] fixup for patch 2: actually check the return value Matthieu Moy
2014-07-16 16:55                 ` Tanay Abhra
2014-07-16 17:10                   ` Matthieu Moy
2014-07-16 16:09               ` [PATCH 3/3] fixup for patch 1: typo Matthieu Moy
2014-07-16 16:44             ` [PATCH v9r2 1/2] add `config_set` API for caching config-like files Tanay Abhra
2014-07-16 17:06               ` Matthieu Moy
     [not found]                 ` <53C6C2BD.3030703@gmail.com>
2014-07-17 10:01                   ` Matthieu Moy
2014-07-17 11:06                     ` Tanay Abhra
2014-07-17 11:34                       ` Matthieu Moy
2014-07-17 11:13                     ` Matthieu Moy
2014-07-17 17:12                       ` Junio C Hamano
2014-07-16 12:22         ` [PATCH v9r2 2/2] test-config: add tests for the config_set API Tanay Abhra

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).