config: add `git_die_config()` to the config-set API
authorTanay Abhra <tanayabh@gmail.com>
Thu, 7 Aug 2014 11:59:16 +0000 (04:59 -0700)
committerJunio C Hamano <gitster@pobox.com>
Thu, 7 Aug 2014 18:40:25 +0000 (11:40 -0700)
Add `git_die_config` that dies printing the line number and the file name
of the highest priority value for the configuration variable `key`. A custom
error message is also printed before dying, specified by the caller, which can
be skipped if `err` argument is set to NULL.

It has usage in non-callback based config value retrieval where we can
raise an error and die if there is a semantic error.
For example,

if (!git_config_get_value(key, &value)){
if (!strcmp(value, "foo"))
git_config_die(key, "value: `%s` is illegal", value);
else
/* do work */
}

Signed-off-by: Tanay Abhra <tanayabh@gmail.com>
Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Documentation/technical/api-config.txt
cache.h
config.c
index 815c1eecc3ddec98825d2d89e13899250c098238..7108888b8a2510e9c72b553855c5e1ef3b82e568 100644 (file)
@@ -155,6 +155,19 @@ as well as retrieval for the queried variable, including:
        Similar to `git_config_get_string`, but expands `~` or `~user` into
        the user's home directory when found at the beginning of the path.
 
+`git_die_config(const char *key, const char *err, ...)`::
+
+       First prints the error message specified by the caller in `err` and then
+       dies printing the line number and the file name of the highest priority
+       value for the configuration variable `key`.
+
+`void git_die_config_linenr(const char *key, const char *filename, int linenr)`::
+
+       Helper function which formats the die error message according to the
+       parameters entered. Used by `git_die_config()`. It can be used by callers
+       handling `git_config_get_value_multi()` to print the correct error message
+       for the desired value.
+
 See test-config.c for usage examples.
 
 Value Parsing Helpers
diff --git a/cache.h b/cache.h
index f11ce419566c2c11015ec04285c85f100b3564ca..89a0d515155c4a1408832fa925b064f877bbd856 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -1388,6 +1388,9 @@ struct key_value_info {
        int linenr;
 };
 
+extern NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3)));
+extern NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr);
+
 extern int committer_ident_sufficiently_given(void);
 extern int author_ident_sufficiently_given(void);
 
index cd59c4a0f2aa033288496757ec03e0f1a01f9338..8449a8ac450550372b0b17d3e0d5f63d3d159a70 100644 (file)
--- a/config.c
+++ b/config.c
@@ -1471,8 +1471,12 @@ const struct string_list *git_config_get_value_multi(const char *key)
 
 int git_config_get_string_const(const char *key, const char **dest)
 {
+       int ret;
        git_config_check_init();
-       return git_configset_get_string_const(&the_config_set, key, dest);
+       ret = git_configset_get_string_const(&the_config_set, key, dest);
+       if (ret < 0)
+               git_die_config(key, NULL);
+       return ret;
 }
 
 int git_config_get_string(const char *key, char **dest)
@@ -1513,8 +1517,39 @@ int git_config_get_maybe_bool(const char *key, int *dest)
 
 int git_config_get_pathname(const char *key, const char **dest)
 {
+       int ret;
        git_config_check_init();
-       return git_configset_get_pathname(&the_config_set, key, dest);
+       ret = git_configset_get_pathname(&the_config_set, key, dest);
+       if (ret < 0)
+               git_die_config(key, NULL);
+       return ret;
+}
+
+NORETURN
+void git_die_config_linenr(const char *key, const char *filename, int linenr)
+{
+       if (!filename)
+               die(_("unable to parse '%s' from command-line config"), key);
+       else
+               die(_("bad config variable '%s' in file '%s' at line %d"),
+                   key, filename, linenr);
+}
+
+NORETURN __attribute__((format(printf, 2, 3)))
+void git_die_config(const char *key, const char *err, ...)
+{
+       const struct string_list *values;
+       struct key_value_info *kv_info;
+
+       if (err) {
+               va_list params;
+               va_start(params, err);
+               vreportf("error: ", err, params);
+               va_end(params);
+       }
+       values = git_config_get_value_multi(key);
+       kv_info = values->items[values->nr - 1].util;
+       git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
 }
 
 /*