Skip to content

Commit dd0f567

Browse files
committed
Merge branch 'ls/config-origin'
The configuration system has been taught to phrase where it found a bad configuration variable in a better way in its error messages. "git config" learnt a new "--show-origin" option to indicate where the values come from. * ls/config-origin: config: add '--show-origin' option to print the origin of a config value config: add 'origin_type' to config_source struct rename git_config_from_buf to git_config_from_mem t: do not hide Git's exit code in tests using 'nul_to_q'
2 parents 11529ec + 70bd879 commit dd0f567

File tree

8 files changed

+237
-26
lines changed

8 files changed

+237
-26
lines changed

Documentation/git-config.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ git-config - Get and set repository or global options
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git config' [<file-option>] [type] [-z|--null] name [value [value_regex]]
12+
'git config' [<file-option>] [type] [--show-origin] [-z|--null] name [value [value_regex]]
1313
'git config' [<file-option>] [type] --add name value
1414
'git config' [<file-option>] [type] --replace-all name value [value_regex]
15-
'git config' [<file-option>] [type] [-z|--null] --get name [value_regex]
16-
'git config' [<file-option>] [type] [-z|--null] --get-all name [value_regex]
17-
'git config' [<file-option>] [type] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
15+
'git config' [<file-option>] [type] [--show-origin] [-z|--null] --get name [value_regex]
16+
'git config' [<file-option>] [type] [--show-origin] [-z|--null] --get-all name [value_regex]
17+
'git config' [<file-option>] [type] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
1818
'git config' [<file-option>] [type] [-z|--null] --get-urlmatch name URL
1919
'git config' [<file-option>] --unset name [value_regex]
2020
'git config' [<file-option>] --unset-all name [value_regex]
2121
'git config' [<file-option>] --rename-section old_name new_name
2222
'git config' [<file-option>] --remove-section name
23-
'git config' [<file-option>] [-z|--null] [--name-only] -l | --list
23+
'git config' [<file-option>] [--show-origin] [-z|--null] [--name-only] -l | --list
2424
'git config' [<file-option>] --get-color name [default]
2525
'git config' [<file-option>] --get-colorbool name [stdout-is-tty]
2626
'git config' [<file-option>] -e | --edit
@@ -194,6 +194,12 @@ See also <<FILES>>.
194194
Output only the names of config variables for `--list` or
195195
`--get-regexp`.
196196

197+
--show-origin::
198+
Augment the output of all queried config options with the
199+
origin type (file, standard input, blob, command line) and
200+
the actual origin (config file path, ref, or blob id if
201+
applicable).
202+
197203
--get-colorbool name [stdout-is-tty]::
198204

199205
Find the color setting for `name` (e.g. `color.diff`) and output

builtin/config.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "color.h"
44
#include "parse-options.h"
55
#include "urlmatch.h"
6+
#include "quote.h"
67

78
static const char *const builtin_config_usage[] = {
89
N_("git config [<options>]"),
@@ -27,6 +28,7 @@ static int actions, types;
2728
static const char *get_color_slot, *get_colorbool_slot;
2829
static int end_null;
2930
static int respect_includes = -1;
31+
static int show_origin;
3032

3133
#define ACTION_GET (1<<0)
3234
#define ACTION_GET_ALL (1<<1)
@@ -81,6 +83,7 @@ static struct option builtin_config_options[] = {
8183
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
8284
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
8385
OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
86+
OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
8487
OPT_END(),
8588
};
8689

@@ -91,8 +94,28 @@ static void check_argc(int argc, int min, int max) {
9194
usage_with_options(builtin_config_usage, builtin_config_options);
9295
}
9396

97+
static void show_config_origin(struct strbuf *buf)
98+
{
99+
const char term = end_null ? '\0' : '\t';
100+
101+
strbuf_addstr(buf, current_config_origin_type());
102+
strbuf_addch(buf, ':');
103+
if (end_null)
104+
strbuf_addstr(buf, current_config_name());
105+
else
106+
quote_c_style(current_config_name(), buf, NULL, 0);
107+
strbuf_addch(buf, term);
108+
}
109+
94110
static int show_all_config(const char *key_, const char *value_, void *cb)
95111
{
112+
if (show_origin) {
113+
struct strbuf buf = STRBUF_INIT;
114+
show_config_origin(&buf);
115+
/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
116+
fwrite(buf.buf, 1, buf.len, stdout);
117+
strbuf_release(&buf);
118+
}
96119
if (!omit_values && value_)
97120
printf("%s%c%s%c", key_, delim, value_, term);
98121
else
@@ -108,6 +131,8 @@ struct strbuf_list {
108131

109132
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
110133
{
134+
if (show_origin)
135+
show_config_origin(buf);
111136
if (show_keys)
112137
strbuf_addstr(buf, key_);
113138
if (!omit_values) {
@@ -538,6 +563,14 @@ int cmd_config(int argc, const char **argv, const char *prefix)
538563
error("--name-only is only applicable to --list or --get-regexp");
539564
usage_with_options(builtin_config_usage, builtin_config_options);
540565
}
566+
567+
if (show_origin && !(actions &
568+
(ACTION_GET|ACTION_GET_ALL|ACTION_GET_REGEXP|ACTION_LIST))) {
569+
error("--show-origin is only applicable to --get, --get-all, "
570+
"--get-regexp, and --list.");
571+
usage_with_options(builtin_config_usage, builtin_config_options);
572+
}
573+
541574
if (actions == ACTION_LIST) {
542575
check_argc(argc, 0, 0);
543576
if (git_config_with_options(show_all_config, NULL,

cache.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,8 +1508,8 @@ struct git_config_source {
15081508
typedef int (*config_fn_t)(const char *, const char *, void *);
15091509
extern int git_default_config(const char *, const char *, void *);
15101510
extern int git_config_from_file(config_fn_t fn, const char *, void *);
1511-
extern int git_config_from_buf(config_fn_t fn, const char *name,
1512-
const char *buf, size_t len, void *data);
1511+
extern int git_config_from_mem(config_fn_t fn, const char *origin_type,
1512+
const char *name, const char *buf, size_t len, void *data);
15131513
extern void git_config_push_parameter(const char *text);
15141514
extern int git_config_from_parameters(config_fn_t fn, void *data);
15151515
extern void git_config(config_fn_t fn, void *);
@@ -1548,6 +1548,8 @@ extern const char *get_log_output_encoding(void);
15481548
extern const char *get_commit_output_encoding(void);
15491549

15501550
extern int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
1551+
extern const char *current_config_origin_type(void);
1552+
extern const char *current_config_name(void);
15511553

15521554
struct config_include_data {
15531555
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_buf(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_buf(fn, name, buf, size, data);
1139+
ret = git_config_from_mem(fn, "blob", name, buf, size, data);
11361140
free(buf);
11371141

11381142
return ret;
@@ -2407,3 +2411,13 @@ int parse_config_key(const char *var,
24072411

24082412
return 0;
24092413
}
2414+
2415+
const char *current_config_origin_type(void)
2416+
{
2417+
return cf && cf->origin_type ? cf->origin_type : "command line";
2418+
}
2419+
2420+
const char *current_config_name(void)
2421+
{
2422+
return cf && cf->name ? cf->name : "";
2423+
}

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_buf(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) {

0 commit comments

Comments
 (0)