Skip to content

Commit ec699bb

Browse files
committed
config: allow user to know scope of config options
Add new option --show-scope which allows a user to know what the scope of listed config options are (local/global/system/etc.). Signed-off-by: Matthew Rogers <[email protected]>
1 parent b02fd2a commit ec699bb

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed

builtin/config.c

Lines changed: 52 additions & 8 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 (system, global, local, command line)")),
158160
OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
159161
OPT_END(),
160162
};
@@ -178,23 +180,63 @@ 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 const char *scope_to_string(enum config_scope scope) {
195+
/*
196+
* --local, --global, and --system work the same as --file so there's
197+
* no easy way for the parser to tell the difference when it is
198+
* setting the scope, so we use our information about which options
199+
* were passed
200+
*/
201+
if (use_local_config || scope == CONFIG_SCOPE_REPO) {
202+
return "local";
203+
} else if (use_global_config || scope == CONFIG_SCOPE_GLOBAL) {
204+
return "global";
205+
} else if (use_system_config || scope == CONFIG_SCOPE_SYSTEM) {
206+
return "system";
207+
} else if (given_config_source.use_stdin ||
208+
given_config_source.blob ||
209+
given_config_source.file ||
210+
scope == CONFIG_SCOPE_CMDLINE) {
211+
return "command line";
212+
} else {
213+
return "unknown";
214+
}
215+
}
216+
217+
static void show_config_scope(struct strbuf *buf)
218+
{
219+
const char term = end_nul ? '\0' : '\t';
220+
const char *scope = scope_to_string(current_config_scope());
221+
222+
strbuf_addch(buf, '(');
223+
if (end_nul)
224+
strbuf_addstr(buf, N_(scope));
225+
else
226+
quote_c_style(scope, buf, NULL, 0);
227+
strbuf_addch(buf, ')');
228+
strbuf_addch(buf, term);
229+
}
230+
192231
static int show_all_config(const char *key_, const char *value_, void *cb)
193232
{
194-
if (show_origin) {
233+
if (show_origin || show_scope) {
195234
struct strbuf buf = STRBUF_INIT;
196-
show_config_origin(&buf);
197-
/* Use fwrite as "buf" can contain \0's if "end_null" is set. */
235+
if (show_scope)
236+
show_config_scope(&buf);
237+
if (show_origin)
238+
show_config_origin(&buf);
239+
/* Use fwrite as "buf" can contain \0's if "end_nul" is set. */
198240
fwrite(buf.buf, 1, buf.len, stdout);
199241
strbuf_release(&buf);
200242
}
@@ -213,6 +255,8 @@ struct strbuf_list {
213255

214256
static int format_config(struct strbuf *buf, const char *key_, const char *value_)
215257
{
258+
if (show_scope)
259+
show_config_scope(buf);
216260
if (show_origin)
217261
show_config_origin(buf);
218262
if (show_keys)
@@ -678,7 +722,7 @@ int cmd_config(int argc, const char **argv, const char *prefix)
678722
config_options.git_dir = get_git_dir();
679723
}
680724

681-
if (end_null) {
725+
if (end_nul) {
682726
term = '\0';
683727
delim = '\n';
684728
key_delim = '\n';

t/t1300-config.sh

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,57 @@ test_expect_success !MINGW '--show-origin blob ref' '
17661766
test_cmp expect output
17671767
'
17681768

1769+
1770+
test_expect_success '--show-scope with --list' '
1771+
cat >expect <<-EOF &&
1772+
(global) user.global=true
1773+
(global) user.override=global
1774+
(global) include.path=$INCLUDE_DIR/absolute.include
1775+
(global) user.absolute=include
1776+
(local) user.local=true
1777+
(local) user.override=local
1778+
(local) include.path=../include/relative.include
1779+
(local) user.relative=include
1780+
(command line) user.cmdline=true
1781+
EOF
1782+
git -c user.cmdline=true config --list --show-scope >output &&
1783+
test_cmp expect output
1784+
'
1785+
1786+
test_expect_success !MINGW '--show-scope with --blob' '
1787+
blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
1788+
cat >expect <<-EOF &&
1789+
(command line) user.custom=true
1790+
EOF
1791+
git config --blob=$blob --show-scope --list >output &&
1792+
test_cmp expect output
1793+
'
1794+
test_expect_success '--show-scope with --local' '
1795+
cat >expect <<-\EOF &&
1796+
(local) user.local=true
1797+
(local) user.override=local
1798+
(local) include.path=../include/relative.include
1799+
EOF
1800+
git config --local --list --show-scope >output &&
1801+
test_cmp expect output
1802+
'
1803+
1804+
test_expect_success '--show-scope with --show-origin' '
1805+
cat >expect <<-EOF &&
1806+
(global) file:$HOME/.gitconfig user.global=true
1807+
(global) file:$HOME/.gitconfig user.override=global
1808+
(global) file:$HOME/.gitconfig include.path=$INCLUDE_DIR/absolute.include
1809+
(global) file:$INCLUDE_DIR/absolute.include user.absolute=include
1810+
(local) file:.git/config user.local=true
1811+
(local) file:.git/config user.override=local
1812+
(local) file:.git/config include.path=../include/relative.include
1813+
(local) file:.git/../include/relative.include user.relative=include
1814+
(command line) command line: user.cmdline=true
1815+
EOF
1816+
git -c user.cmdline=true config --list --show-origin --show-scope >output &&
1817+
test_cmp expect output
1818+
'
1819+
17691820
test_expect_success '--local requires a repo' '
17701821
# we expect 128 to ensure that we do not simply
17711822
# fail to find anything and return code "1"

0 commit comments

Comments
 (0)