Skip to content

Commit 473166b

Browse files
larsxschneidergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Lars Schneider <[email protected]> Acked-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7454ee3 commit 473166b

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

cache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,8 +1485,8 @@ struct git_config_source {
14851485
typedef int (*config_fn_t)(const char *, const char *, void *);
14861486
extern int git_default_config(const char *, const char *, void *);
14871487
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);
14901490
extern void git_config_push_parameter(const char *text);
14911491
extern int git_config_from_parameters(config_fn_t fn, void *data);
14921492
extern void git_config(config_fn_t fn, void *);
@@ -1525,6 +1525,8 @@ extern const char *get_log_output_encoding(void);
15251525
extern const char *get_commit_output_encoding(void);
15261526

15271527
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);
15281530

15291531
struct config_include_data {
15301532
int depth;

config.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct config_source {
2424
size_t pos;
2525
} buf;
2626
} u;
27+
const char *origin_type;
2728
const char *name;
2829
const char *path;
2930
int die_on_error;
@@ -471,9 +472,9 @@ static int git_parse_source(config_fn_t fn, void *data)
471472
break;
472473
}
473474
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);
475476
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);
477478
}
478479

479480
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)
588589
if (!value)
589590
value = "";
590591

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);
594595
die(_("bad numeric config value '%s' for '%s': %s"), value, name, reason);
595596
}
596597

@@ -1061,11 +1062,13 @@ static int do_config_from(struct config_source *top, config_fn_t fn, void *data)
10611062
}
10621063

10631064
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)
10651067
{
10661068
struct config_source top;
10671069

10681070
top.u.file = f;
1071+
top.origin_type = origin_type;
10691072
top.name = name;
10701073
top.path = path;
10711074
top.die_on_error = 1;
@@ -1078,7 +1081,7 @@ static int do_config_from_file(config_fn_t fn,
10781081

10791082
static int git_config_from_stdin(config_fn_t fn, void *data)
10801083
{
1081-
return do_config_from_file(fn, "<stdin>", NULL, stdin, data);
1084+
return do_config_from_file(fn, "standard input", "", NULL, stdin, data);
10821085
}
10831086

10841087
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)
10891092
f = fopen(filename, "r");
10901093
if (f) {
10911094
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);
10931096
funlockfile(f);
10941097
fclose(f);
10951098
}
10961099
return ret;
10971100
}
10981101

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)
11011104
{
11021105
struct config_source top;
11031106

11041107
top.u.buf.buf = buf;
11051108
top.u.buf.len = len;
11061109
top.u.buf.pos = 0;
1110+
top.origin_type = origin_type;
11071111
top.name = name;
11081112
top.path = NULL;
11091113
top.die_on_error = 0;
@@ -1132,7 +1136,7 @@ static int git_config_from_blob_sha1(config_fn_t fn,
11321136
return error("reference '%s' does not point to a blob", name);
11331137
}
11341138

1135-
ret = git_config_from_mem(fn, name, buf, size, data);
1139+
ret = git_config_from_mem(fn, "blob", name, buf, size, data);
11361140
free(buf);
11371141

11381142
return ret;
@@ -2385,3 +2389,13 @@ int parse_config_key(const char *var,
23852389

23862390
return 0;
23872391
}
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,8 @@ static const struct submodule *config_from(struct submodule_cache *cache,
427427
parameter.commit_sha1 = commit_sha1;
428428
parameter.gitmodules_sha1 = sha1;
429429
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);
432432
free(config);
433433

434434
switch (lookup_type) {

t/t1300-repo-config.sh

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,12 +700,18 @@ test_expect_success 'invalid unit' '
700700
git config aninvalid.unit >actual &&
701701
test_cmp expect actual &&
702702
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
704704
EOF
705705
test_must_fail git config --int --get aninvalid.unit 2>actual &&
706706
test_i18ncmp expect actual
707707
'
708708

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+
709715
cat > expect << EOF
710716
true
711717
false

t/t1308-config-set.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,14 @@ test_expect_success 'proper error on error in default config files' '
195195
cp .git/config .git/config.old &&
196196
test_when_finished "mv .git/config.old .git/config" &&
197197
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 &&
199199
test_expect_code 128 test-config get_value foo.bar 2>actual &&
200200
test_cmp expect actual
201201
'
202202

203203
test_expect_success 'proper error on error in custom config files' '
204204
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 &&
206206
test_expect_code 128 test-config configset_get_value foo.bar syntax-error 2>actual &&
207207
test_cmp expect actual
208208
'

0 commit comments

Comments
 (0)