Skip to content

Commit f76463e

Browse files
committed
config: add '--show-scope' to print the scope of a config value
When a user queries config values with --show-origin, often it's difficult to determine what the actual "scope" (local, global, etc.) of a given value is based on just the origin file. Teach 'git config' the '--show-scope' option to print the scope of all displayed config values. Note that we should never see anything of "submodule" scope as that is only ever used by submodule-config.c when parsing the '.gitmodules' file. Signed-off-by: Matthew Rogers <[email protected]>
1 parent dd37624 commit f76463e

File tree

3 files changed

+87
-7
lines changed

3 files changed

+87
-7
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: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ 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)
@@ -155,6 +156,7 @@ static struct option builtin_config_options[] = {
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
};
@@ -189,11 +191,23 @@ static void show_config_origin(struct strbuf *buf)
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)

t/t1300-config.sh

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,65 @@ test_expect_success '--show-origin blob ref' '
17711771
test_cmp expect output
17721772
'
17731773

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

0 commit comments

Comments
 (0)