config: add `git_die_config()` to the config-set API
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>
Tanay Abhra committed
Aug 7, 2014 at 04:59 UTC
5a80e97c827e9d73884dbe4119bf97f6dd84b237
3 files changed
+53
-2
Documentation/technical/api-config.txt
+13
@@ -155,6 +155,19 @@ as well as retrieval for the queried variable, including:
155
Similar to `git_config_get_string`, but expands `~` or `~user` into
156
the user's home directory when found at the beginning of the path.
157
158
+`git_die_config(const char *key, const char *err, ...)`::
159
+
160
+ First prints the error message specified by the caller in `err` and then
161
+ dies printing the line number and the file name of the highest priority
162
+ value for the configuration variable `key`.
163
+
164
+`void git_die_config_linenr(const char *key, const char *filename, int linenr)`::
165
+
166
+ Helper function which formats the die error message according to the
167
+ parameters entered. Used by `git_die_config()`. It can be used by callers
168
+ handling `git_config_get_value_multi()` to print the correct error message
169
+ for the desired value.
170
+
171
See test-config.c for usage examples.
172
173
Value Parsing Helpers
cache.h
+3
@@ -1388,6 +1388,9 @@ struct key_value_info {
1388
int linenr;
1389
};
1390
1391
+extern NORETURN void git_die_config(const char *key, const char *err, ...) __attribute__((format(printf, 2, 3)));
1392
+extern NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr);
1393
+
1394
extern int committer_ident_sufficiently_given(void);
1395
extern int author_ident_sufficiently_given(void);
1396
config.c
+37
-2
@@ -1471,8 +1471,12 @@ const struct string_list *git_config_get_value_multi(const char *key)
1471
1472
int git_config_get_string_const(const char *key, const char **dest)
1473
{
1474
+ int ret;
1475
git_config_check_init();
1475
- return git_configset_get_string_const(&the_config_set, key, dest);
1476
+ ret = git_configset_get_string_const(&the_config_set, key, dest);
1477
+ if (ret < 0)
1478
+ git_die_config(key, NULL);
1479
+ return ret;
1480
}
1481
1482
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)
1517
1518
int git_config_get_pathname(const char *key, const char **dest)
1519
{
1520
+ int ret;
1521
git_config_check_init();
1517
- return git_configset_get_pathname(&the_config_set, key, dest);
1522
+ ret = git_configset_get_pathname(&the_config_set, key, dest);
1523
+ if (ret < 0)
1524
+ git_die_config(key, NULL);
1525
+ return ret;
1526
+}
1527
+
1528
+NORETURN
1529
+void git_die_config_linenr(const char *key, const char *filename, int linenr)
1530
+{
1531
+ if (!filename)
1532
+ die(_("unable to parse '%s' from command-line config"), key);
1533
+ else
1534
+ die(_("bad config variable '%s' in file '%s' at line %d"),
1535
+ key, filename, linenr);
1536
+}
1537
+
1538
+NORETURN __attribute__((format(printf, 2, 3)))
1539
+void git_die_config(const char *key, const char *err, ...)
1540
+{
1541
+ const struct string_list *values;
1542
+ struct key_value_info *kv_info;
1543
+
1544
+ if (err) {
1545
+ va_list params;
1546
+ va_start(params, err);
1547
+ vreportf("error: ", err, params);
1548
+ va_end(params);
1549
+ }
1550
+ values = git_config_get_value_multi(key);
1551
+ kv_info = values->items[values->nr - 1].util;
1552
+ git_die_config_linenr(key, kv_info->filename, kv_info->linenr);
1553
}
1554
1555
/*