Re: [PATCH 4/7] add line number and file name info to `config_set`

2014-07-23 Thread Junio C Hamano
Tanay Abhra  writes:

> @@ -1287,6 +1300,8 @@ static struct config_set_element 
> *configset_find_element(struct config_set *cs,
>  static int configset_add_value(struct config_set *cs, const char *key, const 
> char *value)
>  {
>   struct config_set_element *e;
> + struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
> + struct string_list_item *si;

I have this suspicion that we may want to eventually build a custom
"config_value_list" API that is very similar to what string_list
does, with the only difference from string_list being that the util
item of config_value_item (i.e. a parallel to string_list_item)
would not be a "void *" that points at anything, but is "struct
key_value_info" embedded within, so that we do not have to waste a
pointer and fragmented allocation.

I suspect such a config_value_list API must be built on top of a
kind of generics which C does not allow, which would mean we would
be doing some preprocessor macro tricks (similar to the way how
commit-slab.h allows different kinds of payload) that lets us build
a templated "string-list" API, discarding the existing
"string-list.[ch]" and replacing them with something like these two
liners:

declare_generic_string_list(string_list, void *); /* in string-list.h */
define_generic_string_list(string_list, void *); /* in string-list.c */

And at that point,

declare_generic_string_list(config_value_list, struct key_value);
define_generic_string_list(config_value_list, struct key_value);

would give us an API declaration and implementation that parallel
that of string-list, but with "struct key_value" in the util field
of each item.

But that kind of clean-up can come much later after this series
settles, and what this patch has is fine for now.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 4/7] add line number and file name info to `config_set`

2014-07-23 Thread Tanay Abhra
Store file name and line number for each key-value pair in the cache.
Use the information to print line number and file name in errors raised
by `git_config()` which now uses the configuration files caching layer
internally.

Signed-off-by: Tanay Abhra 
---
 config.c | 32 
 1 file changed, 28 insertions(+), 4 deletions(-)

diff --git a/config.c b/config.c
index a7fb9a4..43a951f 100644
--- a/config.c
+++ b/config.c
@@ -1237,9 +1237,15 @@ static int git_config_raw(config_fn_t fn, void *data)
return git_config_with_options(fn, data, NULL, 1);
 }
 
+struct key_value_info {
+   const char *filename;
+   int linenr;
+};
+
 static int configset_iter(struct config_set *cs, config_fn_t fn, void *data)
 {
int i;
+   struct key_value_info *kv_info;
struct string_list *strptr;
struct config_set_element *entry;
struct hashmap_iter iter;
@@ -1247,8 +1253,15 @@ static int configset_iter(struct config_set *cs, 
config_fn_t fn, void *data)
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 line in (TODO: file/line 
info)");
+   if (fn(entry->key, strptr->items[i].string, data) < 0) {
+   kv_info = strptr->items[i].util;
+   if (!kv_info->linenr)
+   die("unable to parse command-line 
config");
+   else
+   die("bad config file line %d in %s",
+   kv_info->linenr,
+   kv_info->filename);
+   }
}
}
return 0;
@@ -1287,6 +1300,8 @@ static struct config_set_element 
*configset_find_element(struct config_set *cs,
 static int configset_add_value(struct config_set *cs, const char *key, const 
char *value)
 {
struct config_set_element *e;
+   struct key_value_info *kv_info = xmalloc(sizeof(*kv_info));
+   struct string_list_item *si;
e = configset_find_element(cs, key);
/*
 * Since the keys are being fed by git_config*() callback mechanism, 
they
@@ -1299,7 +1314,16 @@ static int configset_add_value(struct config_set *cs, 
const char *key, const cha
string_list_init(&e->value_list, 1);
hashmap_add(&cs->config_hash, e);
}
-   string_list_append_nodup(&e->value_list, value ? xstrdup(value) : NULL);
+   si = string_list_append_nodup(&e->value_list, value ? xstrdup(value) : 
NULL);
+   if (cf) {
+   kv_info->filename = strintern(cf->name);
+   kv_info->linenr = cf->linenr;
+   } else {
+   /* for values read from `git_config_from_parameters()` */
+   kv_info->filename = strintern("parameter");
+   kv_info->linenr = 0;
+   }
+   si->util = kv_info;
 
return 0;
 }
@@ -1326,7 +1350,7 @@ void git_configset_clear(struct config_set *cs)
hashmap_iter_init(&cs->config_hash, &iter);
while ((entry = hashmap_iter_next(&iter))) {
free(entry->key);
-   string_list_clear(&entry->value_list, 0);
+   string_list_clear(&entry->value_list, 1);
}
hashmap_free(&cs->config_hash, 1);
cs->hash_initialized = 0;
-- 
1.9.0.GIT

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html