config: add 'origin_type' to config_source struct

Use the config origin_type to print more detailed error messages that inform the user about the origin of a config error (file, stdin, blob). Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com> Signed-off-by: Lars Schneider <larsxschneider@gmail.com> Acked-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lars Schneider committed Feb 19, 2016 at 10:16 UTC 473166b99078a2724b4fcda11a6a5327b9af60da
5 files changed +40 -18
cache.h
+4 -2
@@ -1485,8 +1485,8 @@ struct git_config_source {
1485 typedef int (*config_fn_t)(const char *, const char *, void *);
1486 extern int git_default_config(const char *, const char *, void *);
1487 extern int git_config_from_file(config_fn_t fn, const char *, void *);
1488 -extern int git_config_from_mem(config_fn_t fn, const char *name,
1489 - const char *buf, size_t len, void *data);
1488 +extern int git_config_from_mem(config_fn_t fn, const char *origin_type,
1489 + const char *name, const char *buf, size_t len, void *data);
1490 extern void git_config_push_parameter(const char *text);
1491 extern int git_config_from_parameters(config_fn_t fn, void *data);
1492 extern void git_config(config_fn_t fn, void *);
@@ -1525,6 +1525,8 @@ extern const char *get_log_output_encoding(void);
1525 extern const char *get_commit_output_encoding(void);
1526
1527 extern int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
1528 +extern const char *current_config_origin_type(void);
1529 +extern const char *current_config_name(void);
1530
1531 struct config_include_data {
1532 int depth;
config.c
+25 -11
@@ -24,6 +24,7 @@ struct config_source {
24 size_t pos;
25 } buf;
26 } u;
27 + const char *origin_type;
28 const char *name;
29 const char *path;
30 int die_on_error;
@@ -471,9 +472,9 @@ static int git_parse_source(config_fn_t fn, void *data)
472 break;
473 }
474 if (cf->die_on_error)
474 - die(_("bad config file line %d in %s"), cf->linenr, cf->name);
475 + die(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
476 else
476 - return error(_("bad config file line %d in %s"), cf->linenr, cf->name);
477 + return error(_("bad config line %d in %s %s"), cf->linenr, cf->origin_type, cf->name);
478 }
479
480 static int parse_unit_factor(const char *end, uintmax_t *val)
@@ -588,9 +589,9 @@ static void die_bad_number(const char *name, const char *value)
589 if (!value)
590 value = "";
591
591 - if (cf && cf->name)
592 - die(_("bad numeric config value '%s' for '%s' in %s: %s"),
593 - value, name, cf->name, reason);
592 + if (cf && cf->origin_type && cf->name)
593 + die(_("bad numeric config value '%s' for '%s' in %s %s: %s"),
594 + value, name, cf->origin_type, cf->name, reason);
595 die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
596 }
597
@@ -1061,11 +1062,13 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
1062 }
1063
1064 static int do_config_from_file(config_fn_t fn,
1064 - const char *name, const char *path, FILE *f, void *data)
1065 + const char *origin_type, const char *name, const char *path, FILE *f,
1066 + void *data)
1067 {
1068 struct config_source top;
1069
1070 top.u.file = f;
1071 + top.origin_type = origin_type;
1072 top.name = name;
1073 top.path = path;
1074 top.die_on_error = 1;
@@ -1078,7 +1081,7 @@ static int do_config_from_file(config_fn_t fn,
1081
1082 static int git_config_from_stdin(config_fn_t fn, void *data)
1083 {
1081 - return do_config_from_file(fn, "<stdin>", NULL, stdin, data);
1084 + return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
1085 }
1086
1087 int git_config_from_file(config_fn_t fn, const char *filename, void *data)
@@ -1089,21 +1092,22 @@ int git_config_from_file(config_fn_t fn, const char *filename, void *data)
1092 f = fopen(filename, "r");
1093 if (f) {
1094 flockfile(f);
1092 - ret = do_config_from_file(fn, filename, filename, f, data);
1095 + ret = do_config_from_file(fn, "file", filename, filename, f, data);
1096 funlockfile(f);
1097 fclose(f);
1098 }
1099 return ret;
1100 }
1101
1099 -int git_config_from_mem(config_fn_t fn, const char *name, const char *buf,
1100 - size_t len, void *data)
1102 +int git_config_from_mem(config_fn_t fn, const char *origin_type,
1103 + const char *name, const char *buf, size_t len, void *data)
1104 {
1105 struct config_source top;
1106
1107 top.u.buf.buf = buf;
1108 top.u.buf.len = len;
1109 top.u.buf.pos = 0;
1110 + top.origin_type = origin_type;
1111 top.name = name;
1112 top.path = NULL;
1113 top.die_on_error = 0;
@@ -1132,7 +1136,7 @@ static int git_config_from_blob_sha1(config_fn_t fn,
1136 return error("reference '%s' does not point to a blob", name);
1137 }
1138
1135 - ret = git_config_from_mem(fn, name, buf, size, data);
1139 + ret = git_config_from_mem(fn, "blob", name, buf, size, data);
1140 free(buf);
1141
1142 return ret;
@@ -2385,3 +2389,13 @@ int parse_config_key(const char *var,
2389
2390 return 0;
2391 }
2392 +
2393 +const char *current_config_origin_type(void)
2394 +{
2395 + return cf && cf->origin_type ? cf->origin_type : "command line";
2396 +}
2397 +
2398 +const char *current_config_name(void)
2399 +{
2400 + return cf && cf->name ? cf->name : "";
2401 +}
submodule-config.c
+2 -2
@@ -427,8 +427,8 @@ static const struct submodule *config_from(struct submodule_cache *cache,
427 parameter.commit_sha1 = commit_sha1;
428 parameter.gitmodules_sha1 = sha1;
429 parameter.overwrite = 0;
430 - git_config_from_mem(parse_config, rev.buf, config, config_size,
431 - &parameter);
430 + git_config_from_mem(parse_config, "submodule-blob", rev.buf,
431 + config, config_size, &parameter);
432 free(config);
433
434 switch (lookup_type) {
t/t1300-repo-config.sh
+7 -1
@@ -700,12 +700,18 @@ test_expect_success 'invalid unit' '
700 git config aninvalid.unit >actual &&
701 test_cmp expect actual &&
702 cat >expect <<-\EOF &&
703 - fatal: bad numeric config value '\''1auto'\'' for '\''aninvalid.unit'\'' in .git/config: invalid unit
703 + fatal: bad numeric config value '\''1auto'\'' for '\''aninvalid.unit'\'' in file .git/config: invalid unit
704 EOF
705 test_must_fail git config --int --get aninvalid.unit 2>actual &&
706 test_i18ncmp expect actual
707 '
708
709 +test_expect_success 'invalid stdin config' '
710 + echo "fatal: bad config line 1 in standard input " >expect &&
711 + echo "[broken" | test_must_fail git config --list --file - >output 2>&1 &&
712 + test_cmp expect output
713 +'
714 +
715 cat > expect << EOF
716 true
717 false
t/t1308-config-set.sh
+2 -2
@@ -195,14 +195,14 @@ test_expect_success 'proper error on error in default config files' '
195 cp .git/config .git/config.old &&
196 test_when_finished "mv .git/config.old .git/config" &&
197 echo "[" >>.git/config &&
198 - echo "fatal: bad config file line 34 in .git/config" >expect &&
198 + echo "fatal: bad config line 34 in file .git/config" >expect &&
199 test_expect_code 128 test-config get_value foo.bar 2>actual &&
200 test_cmp expect actual
201 '
202
203 test_expect_success 'proper error on error in custom config files' '
204 echo "[" >>syntax-error &&
205 - echo "fatal: bad config file line 1 in syntax-error" >expect &&
205 + echo "fatal: bad config line 1 in file syntax-error" >expect &&
206 test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
207 test_cmp expect actual
208 '