Skip to content

Commit fda4394

Browse files
derrickstoleegitster
authored andcommitted
config: add --fixed-value option, un-implemented
The 'git config' builtin takes a 'value-pattern' parameter for several actions. This can cause confusion when expecting exact value matches instead of regex matches, especially when the input string contains metacharacters. While callers can escape the patterns themselves, it would be more friendly to allow an argument to disable the pattern matching in favor of an exact string match. Add a new '--fixed-value' option that does not currently change the behavior. The implementation will be filled in by later changes for each appropriate action. For now, check and test that --fixed-value will abort the command when included with an incompatible action or without a 'value-pattern' argument. The name '--fixed-value' was chosen over something simpler like '--fixed' because some commands allow regular expressions on the key in addition to the value. Signed-off-by: Derrick Stolee <dstolee@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent d156719 commit fda4394

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

Documentation/git-config.txt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ git-config - Get and set repository or global options
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] name [value [value-pattern]]
12+
'git config' [<file-option>] [--type=<type>] [--fixed-value] [--show-origin] [--show-scope] [-z|--null] name [value [value-pattern]]
1313
'git config' [<file-option>] [--type=<type>] --add name value
14-
'git config' [<file-option>] [--type=<type>] --replace-all name value [value-pattern]
15-
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] --get name [value-pattern]
16-
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] --get-all name [value-pattern]
17-
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--name-only] --get-regexp name_regex [value-pattern]
14+
'git config' [<file-option>] [--type=<type>] [--fixed-value] --replace-all name value [value-pattern]
15+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get name [value-pattern]
16+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] --get-all name [value-pattern]
17+
'git config' [<file-option>] [--type=<type>] [--show-origin] [--show-scope] [-z|--null] [--fixed-value] [--name-only] --get-regexp name_regex [value-pattern]
1818
'git config' [<file-option>] [--type=<type>] [-z|--null] --get-urlmatch name URL
19-
'git config' [<file-option>] --unset name [value-pattern]
20-
'git config' [<file-option>] --unset-all name [value-pattern]
19+
'git config' [<file-option>] [--fixed-value] --unset name [value-pattern]
20+
'git config' [<file-option>] [--fixed-value] --unset-all name [value-pattern]
2121
'git config' [<file-option>] --rename-section old_name new_name
2222
'git config' [<file-option>] --remove-section name
2323
'git config' [<file-option>] [--show-origin] [--show-scope] [-z|--null] [--name-only] -l | --list
@@ -165,6 +165,12 @@ See also <<FILES>>.
165165
--list::
166166
List all variables set in config file, along with their values.
167167

168+
--fixed-value::
169+
When used with the `value-pattern` argument, treat `value-pattern` as
170+
an exact string instead of a regular expression. This will restrict
171+
the name/value pairs that are matched to only those where the value
172+
is exactly equal to the `value-pattern`.
173+
168174
--type <type>::
169175
'git config' will ensure that any input or output is valid under the given
170176
type constraint(s), and will canonicalize outgoing values in `<type>`'s

builtin/config.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ static int respect_includes_opt = -1;
3434
static struct config_options config_options;
3535
static int show_origin;
3636
static int show_scope;
37+
static int fixed_value;
3738

3839
#define ACTION_GET (1<<0)
3940
#define ACTION_GET_ALL (1<<1)
@@ -144,6 +145,7 @@ static struct option builtin_config_options[] = {
144145
OPT_BIT(0, "rename-section", &actions, N_("rename section: old-name new-name"), ACTION_RENAME_SECTION),
145146
OPT_BIT(0, "remove-section", &actions, N_("remove a section: name"), ACTION_REMOVE_SECTION),
146147
OPT_BIT('l', "list", &actions, N_("list all"), ACTION_LIST),
148+
OPT_BOOL(0, "fixed-value", &fixed_value, N_("use string equality when comparing values to 'value-pattern'")),
147149
OPT_BIT('e', "edit", &actions, N_("open an editor"), ACTION_EDIT),
148150
OPT_BIT(0, "get-color", &actions, N_("find the color configured: slot [default]"), ACTION_GET_COLOR),
149151
OPT_BIT(0, "get-colorbool", &actions, N_("find the color setting: slot [stdout-is-tty]"), ACTION_GET_COLORBOOL),
@@ -766,6 +768,40 @@ int cmd_config(int argc, const char **argv, const char *prefix)
766768
usage_builtin_config();
767769
}
768770

771+
/* check usage of --fixed-value */
772+
if (fixed_value) {
773+
int allowed_usage = 0;
774+
775+
switch (actions) {
776+
/* git config --get <name> <value-pattern> */
777+
case ACTION_GET:
778+
/* git config --get-all <name> <value-pattern> */
779+
case ACTION_GET_ALL:
780+
/* git config --get-regexp <name-pattern> <value-pattern> */
781+
case ACTION_GET_REGEXP:
782+
/* git config --unset <name> <value-pattern> */
783+
case ACTION_UNSET:
784+
/* git config --unset-all <name> <value-pattern> */
785+
case ACTION_UNSET_ALL:
786+
allowed_usage = argc > 1 && !!argv[1];
787+
break;
788+
789+
/* git config <name> <value> <value-pattern> */
790+
case ACTION_SET_ALL:
791+
/* git config --replace-all <name> <value> <value-pattern> */
792+
case ACTION_REPLACE_ALL:
793+
allowed_usage = argc > 2 && !!argv[2];
794+
break;
795+
796+
/* other options don't allow --fixed-value */
797+
}
798+
799+
if (!allowed_usage) {
800+
error(_("--fixed-value only applies with 'value-pattern'"));
801+
usage_builtin_config();
802+
}
803+
}
804+
769805
if (actions & PAGING_ACTIONS)
770806
setup_auto_pager("config", 1);
771807

t/t1300-config.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,4 +1970,28 @@ test_expect_success '--replace-all and value-pattern' '
19701970
test_cmp expect actual
19711971
'
19721972

1973+
test_expect_success 'refuse --fixed-value for incompatible actions' '
1974+
test_when_finished rm -f config &&
1975+
git config --file=config dev.null bogus &&
1976+
1977+
# These modes do not allow --fixed-value at all
1978+
test_must_fail git config --file=config --fixed-value --add dev.null bogus &&
1979+
test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
1980+
test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
1981+
test_must_fail git config --file=config --fixed-value --rename-section dev null &&
1982+
test_must_fail git config --file=config --fixed-value --remove-section dev &&
1983+
test_must_fail git config --file=config --fixed-value --list &&
1984+
test_must_fail git config --file=config --fixed-value --get-color dev.null &&
1985+
test_must_fail git config --file=config --fixed-value --get-colorbool dev.null &&
1986+
1987+
# These modes complain when --fixed-value has no value-pattern
1988+
test_must_fail git config --file=config --fixed-value dev.null bogus &&
1989+
test_must_fail git config --file=config --fixed-value --replace-all dev.null bogus &&
1990+
test_must_fail git config --file=config --fixed-value --get dev.null &&
1991+
test_must_fail git config --file=config --fixed-value --get-all dev.null &&
1992+
test_must_fail git config --file=config --fixed-value --get-regexp "dev.*" &&
1993+
test_must_fail git config --file=config --fixed-value --unset dev.null &&
1994+
test_must_fail git config --file=config --fixed-value --unset-all dev.null
1995+
'
1996+
19731997
test_done

0 commit comments

Comments
 (0)