Skip to content

Commit 85b6131

Browse files
committed
Merge branch 'mr/show-config-scope' into jch
"git config" learned to show in which "scope", in addition to in which file, each config setting comes from. * mr/show-config-scope: config: add '--show-scope' to print the scope of a config value submodule-config: add subomdule config scope config: teach git_config_source to remember its scope config: preserve scope in do_git_config_sequence config: clarify meaning of command line scoping config: split repo scope to local and worktree config: make scope_name non-static and rename it t1300: create custom config file without special characters t1300: fix over-indented HERE-DOCs config: fix typo in variable name
2 parents c60db68 + 145d59f commit 85b6131

File tree

10 files changed

+248
-139
lines changed

10 files changed

+248
-139
lines changed

Documentation/git-config.txt

Lines changed: 10 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=<type>] [--show-origin] [-z|--null] name [value [value_regex]]
12+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] name [value [value_regex]]
1313
'git config' [<file-option>] [--type=<type>] --add name value
1414
'git config' [<file-option>] [--type=<type>] --replace-all name value [value_regex]
15-
'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get name [value_regex]
16-
'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] --get-all name [value_regex]
17-
'git config' [<file-option>] [--type=<type>] [--show-origin] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
15+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] --get name [value_regex]
16+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] --get-all name [value_regex]
17+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
1818
'git config' [<file-option>] [--type=<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>] [--show-origin] [-z|--null] [--name-only] -l | --list
23+
'git config' [<file-option>] [--show-origin] [--show-scope] [-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
@@ -222,6 +222,11 @@ Valid `<type>`'s include:
222222
the actual origin (config file path, ref, or blob id if
223223
applicable).
224224

225+
--show-scope::
226+
Similar to `--show-origin` in that it augments the output of
227+
all queried config options with the scope of that value
228+
(local, global, system, command).
229+
225230
--get-colorbool name [stdout-is-tty]::
226231

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

builtin/config.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ static int use_worktree_config;
2929
static struct git_config_source given_config_source;
3030
static int actions, type;
3131
static char *default_value;
32-
static int end_null;
32+
static int end_nul;
3333
static int respect_includes_opt = -1;
3434
static struct config_options config_options;
3535
static int show_origin;
36+
static int show_scope;
3637

3738
#define ACTION_GET (1<<0)
3839
#define ACTION_GET_ALL (1<<1)
@@ -151,10 +152,11 @@ static struct option builtin_config_options[] = {
151152
OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
152153
OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
153154
OPT_GROUP(N_("Other")),
154-
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
155+
OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")),
155156
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
156157
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
157158
OPT_BOOL(0, "show-origin", &show_origin, N_("show origin of config (file, standard input, blob, command line)")),
159+
OPT_BOOL(0, "show-scope", &show_scope, N_("show scope of config (worktree, local, global, system, command)")),
158160
OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
159161
OPT_END(),
160162
};
@@ -178,22 +180,34 @@ static void check_argc(int argc, int min, int max)
178180

179181
static void show_config_origin(struct strbuf *buf)
180182
{
181-
const char term = end_null ? '\0' : '\t';
183+
const char term = end_nul ? '\0' : '\t';
182184

183185
strbuf_addstr(buf, current_config_origin_type());
184186
strbuf_addch(buf, ':');
185-
if (end_null)
187+
if (end_nul)
186188
strbuf_addstr(buf, current_config_name());
187189
else
188190
quote_c_style(current_config_name(), buf, NULL, 0);
189191
strbuf_addch(buf, term);
190192
}
191193

194+
static void show_config_scope(struct strbuf *buf)
195+
{
196+
const char term = end_nul ? '\0' : '\t';
197+
const char *scope = config_scope_name(current_config_scope());
198+
199+
strbuf_addstr(buf, N_(scope));
200+
strbuf_addch(buf, term);
201+
}
202+
192203
static int show_all_config(const char *key_, const char *value_, void *cb)
193204
{
194-
if (show_origin) {
205+
if (show_origin || show_scope) {
195206
struct strbuf buf = STRBUF_INIT;
196-
show_config_origin(&buf);
207+
if (show_scope)
208+
show_config_scope(&buf);
209+
if (show_origin)
210+
show_config_origin(&buf);
197211
/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
198212
fwrite(buf.buf, 1, buf.len, stdout);
199213
strbuf_release(&buf);
@@ -213,6 +227,8 @@ struct strbuf_list {
213227

214228
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
215229
{
230+
if (show_scope)
231+
show_config_scope(buf);
216232
if (show_origin)
217233
show_config_origin(buf);
218234
if (show_keys)
@@ -622,6 +638,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
622638
!strcmp(given_config_source.file, "-")) {
623639
given_config_source.file = NULL;
624640
given_config_source.use_stdin = 1;
641+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
625642
}
626643

627644
if (use_global_config) {
@@ -637,6 +654,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
637654
*/
638655
die(_("$HOME not set"));
639656

657+
given_config_source.scope = CONFIG_SCOPE_GLOBAL;
658+
640659
if (access_or_warn(user_config, R_OK, 0) &&
641660
xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
642661
given_config_source.file = xdg_config;
@@ -646,11 +665,13 @@ int cmd_config(int argc, const char **argv, const char *prefix)
646665
free(xdg_config);
647666
}
648667
}
649-
else if (use_system_config)
668+
else if (use_system_config) {
650669
given_config_source.file = git_etc_gitconfig();
651-
else if (use_local_config)
670+
given_config_source.scope = CONFIG_SCOPE_SYSTEM;
671+
} else if (use_local_config) {
652672
given_config_source.file = git_pathdup("config");
653-
else if (use_worktree_config) {
673+
given_config_source.scope = CONFIG_SCOPE_LOCAL;
674+
} else if (use_worktree_config) {
654675
struct worktree **worktrees = get_worktrees(0);
655676
if (repository_format_worktree_config)
656677
given_config_source.file = git_pathdup("config.worktree");
@@ -662,13 +683,18 @@ int cmd_config(int argc, const char **argv, const char *prefix)
662683
"section in \"git help worktree\" for details"));
663684
else
664685
given_config_source.file = git_pathdup("config");
686+
given_config_source.scope = CONFIG_SCOPE_LOCAL;
665687
free_worktrees(worktrees);
666688
} else if (given_config_source.file) {
667689
if (!is_absolute_path(given_config_source.file) && prefix)
668690
given_config_source.file =
669691
prefix_filename(prefix, given_config_source.file);
692+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
693+
} else if (given_config_source.blob) {
694+
given_config_source.scope = CONFIG_SCOPE_COMMAND;
670695
}
671696

697+
672698
if (respect_includes_opt == -1)
673699
config_options.respect_includes = !given_config_source.file;
674700
else
@@ -678,7 +704,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
678704
config_options.git_dir = get_git_dir();
679705
}
680706

681-
if (end_null) {
707+
if (end_nul) {
682708
term = '\0';
683709
delim = '\n';
684710
key_delim = '\n';

config.c

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,7 @@ static int do_git_config_sequence(const struct config_options *opts,
17021702
char *xdg_config = xdg_config_home("config");
17031703
char *user_config = expand_user_path("~/.gitconfig", 0);
17041704
char *repo_config;
1705+
enum config_scope prev_parsing_scope = current_parsing_scope;
17051706

17061707
if (opts->commondir)
17071708
repo_config = mkpathdup("%s/config", opts->commondir);
@@ -1724,27 +1725,24 @@ static int do_git_config_sequence(const struct config_options *opts,
17241725
if (user_config && !access_or_die(user_config, R_OK, ACCESS_EACCES_OK))
17251726
ret += git_config_from_file(fn, user_config, data);
17261727

1727-
current_parsing_scope = CONFIG_SCOPE_REPO;
1728+
current_parsing_scope = CONFIG_SCOPE_LOCAL;
17281729
if (!opts->ignore_repo && repo_config &&
17291730
!access_or_die(repo_config, R_OK, 0))
17301731
ret += git_config_from_file(fn, repo_config, data);
17311732

1732-
/*
1733-
* Note: this should have a new scope, CONFIG_SCOPE_WORKTREE.
1734-
* But let's not complicate things before it's actually needed.
1735-
*/
1733+
current_parsing_scope = CONFIG_SCOPE_WORKTREE;
17361734
if (!opts->ignore_worktree && repository_format_worktree_config) {
17371735
char *path = git_pathdup("config.worktree");
17381736
if (!access_or_die(path, R_OK, 0))
17391737
ret += git_config_from_file(fn, path, data);
17401738
free(path);
17411739
}
17421740

1743-
current_parsing_scope = CONFIG_SCOPE_CMDLINE;
1741+
current_parsing_scope = CONFIG_SCOPE_COMMAND;
17441742
if (!opts->ignore_cmdline && git_config_from_parameters(fn, data) < 0)
17451743
die(_("unable to parse command-line config"));
17461744

1747-
current_parsing_scope = CONFIG_SCOPE_UNKNOWN;
1745+
current_parsing_scope = prev_parsing_scope;
17481746
free(xdg_config);
17491747
free(user_config);
17501748
free(repo_config);
@@ -1765,6 +1763,9 @@ int config_with_options(config_fn_t fn, void *data,
17651763
data = &inc;
17661764
}
17671765

1766+
if (config_source)
1767+
current_parsing_scope = config_source->scope;
1768+
17681769
/*
17691770
* If we have a specific filename, use it. Otherwise, follow the
17701771
* regular lookup sequence.
@@ -3297,6 +3298,26 @@ const char *current_config_origin_type(void)
32973298
}
32983299
}
32993300

3301+
const char *config_scope_name(enum config_scope scope)
3302+
{
3303+
switch (scope) {
3304+
case CONFIG_SCOPE_SYSTEM:
3305+
return "system";
3306+
case CONFIG_SCOPE_GLOBAL:
3307+
return "global";
3308+
case CONFIG_SCOPE_LOCAL:
3309+
return "local";
3310+
case CONFIG_SCOPE_WORKTREE:
3311+
return "worktree";
3312+
case CONFIG_SCOPE_COMMAND:
3313+
return "command";
3314+
case CONFIG_SCOPE_SUBMODULE:
3315+
return "submodule";
3316+
default:
3317+
return "unknown";
3318+
}
3319+
}
3320+
33003321
const char *current_config_name(void)
33013322
{
33023323
const char *name;

config.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,22 @@ struct object_id;
3535

3636
#define CONFIG_REGEX_NONE ((void *)1)
3737

38+
enum config_scope {
39+
CONFIG_SCOPE_UNKNOWN = 0,
40+
CONFIG_SCOPE_SYSTEM,
41+
CONFIG_SCOPE_GLOBAL,
42+
CONFIG_SCOPE_LOCAL,
43+
CONFIG_SCOPE_WORKTREE,
44+
CONFIG_SCOPE_COMMAND,
45+
CONFIG_SCOPE_SUBMODULE,
46+
};
47+
const char *config_scope_name(enum config_scope scope);
48+
3849
struct git_config_source {
3950
unsigned int use_stdin:1;
4051
const char *file;
4152
const char *blob;
53+
enum config_scope scope;
4254
};
4355

4456
enum config_origin_type {
@@ -294,14 +306,6 @@ int config_error_nonbool(const char *);
294306

295307
int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
296308

297-
enum config_scope {
298-
CONFIG_SCOPE_UNKNOWN = 0,
299-
CONFIG_SCOPE_SYSTEM,
300-
CONFIG_SCOPE_GLOBAL,
301-
CONFIG_SCOPE_REPO,
302-
CONFIG_SCOPE_CMDLINE,
303-
};
304-
305309
enum config_scope current_config_scope(void);
306310
const char *current_config_origin_type(void);
307311
const char *current_config_name(void);

remote.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ static int handle_config(const char *key, const char *value, void *cb)
369369
}
370370
remote = make_remote(name, namelen);
371371
remote->origin = REMOTE_CONFIG;
372-
if (current_config_scope() == CONFIG_SCOPE_REPO)
372+
if (current_config_scope() == CONFIG_SCOPE_LOCAL ||
373+
current_config_scope() == CONFIG_SCOPE_WORKTREE)
373374
remote->configured_in_repo = 1;
374375
if (!strcmp(subkey, "mirror"))
375376
remote->mirror = git_config_bool(key, value);

submodule-config.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,9 @@ static void submodule_cache_check_init(struct repository *repo)
635635
static void config_from_gitmodules(config_fn_t fn, struct repository *repo, void *data)
636636
{
637637
if (repo->worktree) {
638-
struct git_config_source config_source = { 0 };
638+
struct git_config_source config_source = {
639+
0, .scope = CONFIG_SCOPE_SUBMODULE
640+
};
639641
const struct config_options opts = { 0 };
640642
struct object_id oid;
641643
char *file;

t/helper/test-config.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,6 @@
3737
*
3838
*/
3939

40-
static const char *scope_name(enum config_scope scope)
41-
{
42-
switch (scope) {
43-
case CONFIG_SCOPE_SYSTEM:
44-
return "system";
45-
case CONFIG_SCOPE_GLOBAL:
46-
return "global";
47-
case CONFIG_SCOPE_REPO:
48-
return "repo";
49-
case CONFIG_SCOPE_CMDLINE:
50-
return "cmdline";
51-
default:
52-
return "unknown";
53-
}
54-
}
5540
static int iterate_cb(const char *var, const char *value, void *data)
5641
{
5742
static int nr;
@@ -63,7 +48,7 @@ static int iterate_cb(const char *var, const char *value, void *data)
6348
printf("value=%s\n", value ? value : "(null)");
6449
printf("origin=%s\n", current_config_origin_type());
6550
printf("name=%s\n", current_config_name());
66-
printf("scope=%s\n", scope_name(current_config_scope()));
51+
printf("scope=%s\n", config_scope_name(current_config_scope()));
6752

6853
return 0;
6954
}

0 commit comments

Comments
 (0)