Skip to content

Commit 4e51389

Browse files
pks-tgitster
authored andcommitted
builtin/config: introduce "get" subcommand
Introduce a new "get" subcommand to git-config(1). Please refer to preceding commits regarding the motivation behind this change. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1497050 commit 4e51389

File tree

3 files changed

+194
-99
lines changed

3 files changed

+194
-99
lines changed

Documentation/git-config.txt

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,14 @@ SYNOPSIS
1010
--------
1111
[verse]
1212
'git config list' [<file-option>] [<display-option>] [--includes]
13+
'git config get' [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>
1314
'git config' [<file-option>] [--type=<type>] [--comment=<message>] [--fixed-value] [--show-origin] [--show-scope] [-z|--null] <name> [<value> [<value-pattern>]]
1415
'git config' [<file-option>] [--type=<type>] [--comment=<message>] --add <name> <value>
1516
'git config' [<file-option>] [--type=<type>] [--comment=<message>] [--fixed-value] --replace-all <name> <value> [<value-pattern>]
16-
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get <name> [<value-pattern>]
17-
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get-all <name> [<value-pattern>]
18-
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] [--name-only] --get-regexp <name-regex> [<value-pattern>]
19-
'git config' [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch <name> <URL>
2017
'git config' [<file-option>] [--fixed-value] --unset <name> [<value-pattern>]
2118
'git config' [<file-option>] [--fixed-value] --unset-all <name> [<value-pattern>]
2219
'git config' [<file-option>] --rename-section <old-name> <new-name>
2320
'git config' [<file-option>] --remove-section <name>
24-
'git config' [<file-option>] --get-color <name> [<default>]
2521
'git config' [<file-option>] --get-colorbool <name> [<stdout-is-tty>]
2622
'git config' [<file-option>] -e | --edit
2723

@@ -80,6 +76,12 @@ COMMANDS
8076
list::
8177
List all variables set in config file, along with their values.
8278

79+
get::
80+
Emits the value of the specified key. If key is present multiple times
81+
in the configuration, emits the last value. If `--all` is specified,
82+
emits all values associated with key. Returns error code 1 if key is
83+
not present.
84+
8385
[[OPTIONS]]
8486
OPTIONS
8587
-------
@@ -105,22 +107,16 @@ OPTIONS
105107
not contain linefeed characters (no multi-line comments are
106108
permitted).
107109

108-
--get::
109-
Get the value for a given key (optionally filtered by a regex
110-
matching the value). Returns error code 1 if the key was not
111-
found and the last value if multiple key values were found.
112-
113-
--get-all::
114-
Like get, but returns all values for a multi-valued key.
110+
--all::
111+
With `get`, return all values for a multi-valued key.
115112

116-
--get-regexp::
117-
Like --get-all, but interprets the name as a regular expression and
118-
writes out the key names. Regular expression matching is currently
119-
case-sensitive and done against a canonicalized version of the key
120-
in which section and variable names are lowercased, but subsection
121-
names are not.
113+
---regexp::
114+
With `get`, interpret the name as a regular expression. Regular
115+
expression matching is currently case-sensitive and done against a
116+
canonicalized version of the key in which section and variable names
117+
are lowercased, but subsection names are not.
122118

123-
--get-urlmatch <name> <URL>::
119+
--url=<URL>::
124120
When given a two-part <name> as <section>.<key>, the value for
125121
<section>.<URL>.<key> whose <URL> part matches the best to the
126122
given URL is returned (if no such key exists, the value for
@@ -251,7 +247,7 @@ Valid `<type>`'s include:
251247

252248
--name-only::
253249
Output only the names of config variables for `list` or
254-
`--get-regexp`.
250+
`get`.
255251

256252
--show-origin::
257253
Augment the output of all queried config options with the
@@ -275,17 +271,6 @@ Valid `<type>`'s include:
275271
When the color setting for `name` is undefined, the command uses
276272
`color.ui` as fallback.
277273

278-
--get-color <name> [<default>]::
279-
280-
Find the color configured for `name` (e.g. `color.diff.new`) and
281-
output it as the ANSI color escape sequence to the standard
282-
output. The optional `default` parameter is used instead, if
283-
there is no color configured for `name`.
284-
+
285-
`--type=color [--default=<default>]` is preferred over `--get-color`
286-
(but note that `--get-color` will omit the trailing newline printed by
287-
`--type=color`).
288-
289274
-e::
290275
--edit::
291276
Opens an editor to modify the specified config file; either
@@ -299,7 +284,7 @@ Valid `<type>`'s include:
299284
config files.
300285

301286
--default <value>::
302-
When using `--get`, and the requested variable is not found, behave as if
287+
When using `get`, and the requested variable is not found, behave as if
303288
<value> were the value assigned to that variable.
304289

305290
DEPRECATED MODES
@@ -308,15 +293,33 @@ DEPRECATED MODES
308293
The following modes have been deprecated in favor of subcommands. It is
309294
recommended to migrate to the new syntax.
310295

296+
'git config <name>'::
297+
Replaced by `git config get <name>`.
298+
311299
-l::
312300
--list::
313301
Replaced by `git config list`.
314302

303+
--get <name> [<value-pattern>]::
304+
Replaced by `git config get [--value=<pattern>] <name>`.
305+
306+
--get-all <name> [<value-pattern>]::
307+
Replaced by `git config get [--value=<pattern>] --all --show-names <name>`.
308+
309+
--get-regexp <name-regexp>::
310+
Replaced by `git config get --all --show-names --regexp <name-regexp>`.
311+
312+
--get-urlmatch <name> <URL>::
313+
Replaced by `git config get --all --show-names --url=<URL> <name>`.
314+
315+
--get-color <name> [<default>]::
316+
Replaced by `git config get --type=color [--default=<default>] <name>`.
317+
315318
CONFIGURATION
316319
-------------
317320
`pager.config` is only respected when listing configuration, i.e., when
318-
using `list` or any of the `--get-*` which may return multiple results.
319-
The default is to use a pager.
321+
using `list` or `get` which may return multiple results. The default is to use
322+
a pager.
320323

321324
[[FILES]]
322325
FILES
@@ -519,25 +522,19 @@ you have to provide a regex matching the value of exactly one line.
519522
To query the value for a given key, do
520523

521524
------------
522-
% git config --get core.filemode
523-
------------
524-
525-
or
526-
527-
------------
528-
% git config core.filemode
525+
% git config get core.filemode
529526
------------
530527

531528
or, to query a multivar:
532529

533530
------------
534-
% git config --get core.gitproxy "for kernel.org$"
531+
% git config get --value="for kernel.org$" core.gitproxy
535532
------------
536533

537534
If you want to know all the values for a multivar, do:
538535

539536
------------
540-
% git config --get-all core.gitproxy
537+
% git config get --all --show-names core.gitproxy
541538
------------
542539

543540
If you like to live dangerously, you can replace *all* core.gitproxy by a
@@ -571,20 +568,20 @@ script:
571568

572569
------------
573570
#!/bin/sh
574-
WS=$(git config --get-color color.diff.whitespace "blue reverse")
575-
RESET=$(git config --get-color "" "reset")
571+
WS=$(git config get --type=color --default="blue reverse" color.diff.whitespace)
572+
RESET=$(git config get --type=color --default="reset" "")
576573
echo "${WS}your whitespace color or blue reverse${RESET}"
577574
------------
578575

579576
For URLs in `https://weak.example.com`, `http.sslVerify` is set to
580577
false, while it is set to `true` for all others:
581578

582579
------------
583-
% git config --type=bool --get-urlmatch http.sslverify https://good.example.com
580+
% git config get --type=bool --url=https://good.example.com http.sslverify
584581
true
585-
% git config --type=bool --get-urlmatch http.sslverify https://weak.example.com
582+
% git config get --type=bool --url=https://weak.example.com http.sslverify
586583
false
587-
% git config --get-urlmatch http https://weak.example.com
584+
% git config get --url=https://weak.example.com http
588585
http.cookieFile /tmp/cookie.txt
589586
http.sslverify false
590587
------------

builtin/config.c

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
static const char *const builtin_config_usage[] = {
1919
N_("git config list [<file-option>] [<display-option>] [--includes]"),
20-
N_("git config [<options>]"),
20+
N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
2121
NULL
2222
};
2323

@@ -26,6 +26,11 @@ static const char *const builtin_config_list_usage[] = {
2626
NULL
2727
};
2828

29+
static const char *const builtin_config_get_usage[] = {
30+
N_("git config get [<file-option>] [<display-option>] [--includes] [--all] [--regexp=<regexp>] [--value=<value>] [--fixed-value] [--default=<default>] <name>"),
31+
NULL
32+
};
33+
2934
static char *key;
3035
static regex_t *key_regexp;
3136
static const char *value_pattern;
@@ -722,6 +727,16 @@ static void handle_nul(void) {
722727
OPT_STRING('f', "file", &given_config_source.file, N_("file"), N_("use given config file")), \
723728
OPT_STRING(0, "blob", &given_config_source.blob, N_("blob-id"), N_("read config from given blob object"))
724729

730+
#define CONFIG_TYPE_OPTIONS \
731+
OPT_GROUP(N_("Type")), \
732+
OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type), \
733+
OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL), \
734+
OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT), \
735+
OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT), \
736+
OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR), \
737+
OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH), \
738+
OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE)
739+
725740
#define CONFIG_DISPLAY_OPTIONS \
726741
OPT_GROUP(N_("Display options")), \
727742
OPT_BOOL('z', "null", &end_nul, N_("terminate values with NUL byte")), \
@@ -746,14 +761,7 @@ static struct option builtin_config_options[] = {
746761
OPT_CMDMODE('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
747762
OPT_CMDMODE(0, "get-color", &actions, N_("find the color configured: slot [<default>]"), ACTION_GET_COLOR),
748763
OPT_CMDMODE(0, "get-colorbool", &actions, N_("find the color setting: slot [<stdout-is-tty>]"), ACTION_GET_COLORBOOL),
749-
OPT_GROUP(N_("Type")),
750-
OPT_CALLBACK('t', "type", &type, N_("type"), N_("value is given this type"), option_parse_type),
751-
OPT_CALLBACK_VALUE(0, "bool", &type, N_("value is \"true\" or \"false\""), TYPE_BOOL),
752-
OPT_CALLBACK_VALUE(0, "int", &type, N_("value is decimal number"), TYPE_INT),
753-
OPT_CALLBACK_VALUE(0, "bool-or-int", &type, N_("value is --bool or --int"), TYPE_BOOL_OR_INT),
754-
OPT_CALLBACK_VALUE(0, "bool-or-str", &type, N_("value is --bool or string"), TYPE_BOOL_OR_STR),
755-
OPT_CALLBACK_VALUE(0, "path", &type, N_("value is a path (file or directory name)"), TYPE_PATH),
756-
OPT_CALLBACK_VALUE(0, "expiry-date", &type, N_("value is an expiry date"), TYPE_EXPIRY_DATE),
764+
CONFIG_TYPE_OPTIONS,
757765
CONFIG_DISPLAY_OPTIONS,
758766
OPT_GROUP(N_("Other")),
759767
OPT_STRING(0, "default", &default_value, N_("value"), N_("with --get, use default value when missing entry")),
@@ -799,8 +807,51 @@ static int cmd_config_list(int argc, const char **argv, const char *prefix)
799807
return 0;
800808
}
801809

810+
static int cmd_config_get(int argc, const char **argv, const char *prefix)
811+
{
812+
const char *value_pattern = NULL, *url = NULL;
813+
int flags = 0;
814+
struct option opts[] = {
815+
CONFIG_LOCATION_OPTIONS,
816+
CONFIG_TYPE_OPTIONS,
817+
OPT_GROUP(N_("Filter options")),
818+
OPT_BOOL(0, "all", &do_all, N_("return all values for multi-valued config options")),
819+
OPT_BOOL(0, "regexp", &use_key_regexp, N_("interpret the name as a regular expression")),
820+
OPT_STRING(0, "value", &value_pattern, N_("pattern"), N_("show config with values matching the pattern")),
821+
OPT_BIT(0, "fixed-value", &flags, N_("use string equality when comparing values to value pattern"), CONFIG_FLAGS_FIXED_VALUE),
822+
OPT_STRING(0, "url", &url, N_("URL"), N_("show config matching the given URL")),
823+
CONFIG_DISPLAY_OPTIONS,
824+
OPT_BOOL(0, "show-names", &show_keys, N_("show config keys in addition to their values")),
825+
OPT_GROUP(N_("Other")),
826+
OPT_BOOL(0, "includes", &respect_includes_opt, N_("respect include directives on lookup")),
827+
OPT_STRING(0, "default", &default_value, N_("value"), N_("use default value when missing entry")),
828+
OPT_END(),
829+
};
830+
831+
argc = parse_options(argc, argv, prefix, opts, builtin_config_get_usage,
832+
PARSE_OPT_STOP_AT_NON_OPTION);
833+
check_argc(argc, 1, 1);
834+
835+
if ((flags & CONFIG_FLAGS_FIXED_VALUE) && !value_pattern)
836+
die(_("--fixed-value only applies with 'value-pattern'"));
837+
if (default_value && (do_all || url))
838+
die(_("--default= cannot be used with --all or --url="));
839+
if (url && (do_all || use_key_regexp || value_pattern))
840+
die(_("--url= cannot be used with --all, --regexp or --value"));
841+
842+
handle_config_location(prefix);
843+
handle_nul();
844+
845+
setup_auto_pager("config", 1);
846+
847+
if (url)
848+
return get_urlmatch(argv[0], url);
849+
return get_value(argv[0], value_pattern, flags);
850+
}
851+
802852
static struct option builtin_subcommand_options[] = {
803853
OPT_SUBCOMMAND("list", &subcommand, cmd_config_list),
854+
OPT_SUBCOMMAND("get", &subcommand, cmd_config_get),
804855
OPT_END(),
805856
};
806857

0 commit comments

Comments
 (0)