@matheusbsbs / matheus-bagliano / commits / 145d59f482
Fork 0

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 <mattr94@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Matthew Rogers committed Feb 10, 2020 at 00:30 UTC 145d59f48233c64cb8a9262c9f1451cc7d66b530
3 files changed +87 -7
Documentation/git-config.txt
+10 -5
@@ -9,18 +9,18 @@ git-config - Get and set repository or global options
9 SYNOPSIS
10 --------
11 [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]]
13 'git config' [<file-option>] [--type=<type>] --add name value
14 '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]
18 'git config' [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch name URL
19 'git config' [<file-option>] --unset name [value_regex]
20 'git config' [<file-option>] --unset-all name [value_regex]
21 'git config' [<file-option>] --rename-section old_name new_name
22 '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
24 'git config' [<file-option>] --get-color name [default]
25 'git config' [<file-option>] --get-colorbool name [stdout-is-tty]
26 'git config' [<file-option>] -e | --edit
@@ -222,6 +222,11 @@ Valid `<type>`'s include:
222 the actual origin (config file path, ref, or blob id if
223 applicable).
224
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 +
230 --get-colorbool name [stdout-is-tty]::
231
232 Find the color setting for `name` (e.g. `color.diff`) and output
builtin/config.c
+18 -2
@@ -33,6 +33,7 @@ static int end_nul;
33 static int respect_includes_opt = -1;
34 static struct config_options config_options;
35 static int show_origin;
36 +static int show_scope;
37
38 #define ACTION_GET (1<<0)
39 #define ACTION_GET_ALL (1<<1)
@@ -155,6 +156,7 @@ static struct option builtin_config_options[] = {
156 OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
157 OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
158 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)")),
160 OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
161 OPT_END(),
162 };
@@ -189,11 +191,23 @@ static void show_config_origin(struct strbuf *buf)
191 strbuf_addch(buf, term);
192 }
193
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 +
203 static int show_all_config(const char *key_, const char *value_, void *cb)
204 {
194 - if (show_origin) {
205 + if (show_origin || show_scope) {
206 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);
211 /* Use fwrite as "buf" can contain \0's if "end_null" is set. */
212 fwrite(buf.buf, 1, buf.len, stdout);
213 strbuf_release(&buf);
@@ -213,6 +227,8 @@ struct strbuf_list {
227
228 static int format_config(struct strbuf *buf, const char *key_, const char *value_)
229 {
230 + if (show_scope)
231 + show_config_scope(buf);
232 if (show_origin)
233 show_config_origin(buf);
234 if (show_keys)
t/t1300-config.sh
+59
@@ -1771,6 +1771,65 @@ test_expect_success '--show-origin blob ref' '
1771 test_cmp expect output
1772 '
1773
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 +
1833 test_expect_success '--local requires a repo' '
1834 # we expect 128 to ensure that we do not simply
1835 # fail to find anything and return code "1"