Skip to content

Commit 578625f

Browse files
szedergitster
authored andcommitted
config: add '--name-only' option to list only variable names
'git config' can only show values or name-value pairs, so if a shell script needs the names of set config variables it has to run 'git config --list' or '--get-regexp' and parse the output to separate config variable names from their values. However, such a parsing can't cope with multi-line values. Though 'git config' can produce null-terminated output for newline-safe parsing, that's of no use in such a case, becase shells can't cope with null characters. Even our own bash completion script suffers from these issues. Help the completion script, and shell scripts in general, by introducing the '--name-only' option to modify the output of '--list' and '--get-regexp' to list only the names of config variables, so they don't have to perform error-prone post processing to separate variable names from their values anymore. Signed-off-by: SZEDER Gábor <szeder@ira.uka.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 77bd3ea commit 578625f

4 files changed

Lines changed: 42 additions & 5 deletions

File tree

Documentation/git-config.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ SYNOPSIS
1414
'git config' [<file-option>] [type] --replace-all name value [value_regex]
1515
'git config' [<file-option>] [type] [-z|--null] --get name [value_regex]
1616
'git config' [<file-option>] [type] [-z|--null] --get-all name [value_regex]
17-
'git config' [<file-option>] [type] [-z|--null] --get-regexp name_regex [value_regex]
17+
'git config' [<file-option>] [type] [-z|--null] [--name-only] --get-regexp name_regex [value_regex]
1818
'git config' [<file-option>] [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>] [-z|--null] -l | --list
23+
'git config' [<file-option>] [-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
@@ -159,7 +159,7 @@ See also <<FILES>>.
159159

160160
-l::
161161
--list::
162-
List all variables set in config file.
162+
List all variables set in config file, along with their values.
163163

164164
--bool::
165165
'git config' will ensure that the output is "true" or "false"
@@ -190,6 +190,10 @@ See also <<FILES>>.
190190
output without getting confused e.g. by values that
191191
contain line breaks.
192192

193+
--name-only::
194+
Output only the names of config variables for `--list` or
195+
`--get-regexp`.
196+
193197
--get-colorbool name [stdout-is-tty]::
194198

195199
Find the color setting for `name` (e.g. `color.diff`) and output

builtin/config.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ static char *key;
1313
static regex_t *key_regexp;
1414
static regex_t *regexp;
1515
static int show_keys;
16+
static int omit_values;
1617
static int use_key_regexp;
1718
static int do_all;
1819
static int do_not_match;
@@ -78,6 +79,7 @@ static struct option builtin_config_options[] = {
7879
OPT_BIT(0, "path", &types, N_("value is a path (file or directory name)"), TYPE_PATH),
7980
OPT_GROUP(N_("Other")),
8081
OPT_BOOL('z', "null", &end_null, N_("terminate values with NUL byte")),
82+
OPT_BOOL(0, "name-only", &omit_values, N_("show variable names only")),
8183
OPT_BOOL(0, "includes", &respect_includes, N_("respect include directives on lookup")),
8284
OPT_END(),
8385
};
@@ -91,7 +93,7 @@ static void check_argc(int argc, int min, int max) {
9193

9294
static int show_all_config(const char *key_, const char *value_, void *cb)
9395
{
94-
if (value_)
96+
if (!omit_values && value_)
9597
printf("%s%c%s%c", key_, delim, value_, term);
9698
else
9799
printf("%s%c", key_, term);
@@ -117,6 +119,10 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
117119
strbuf_addstr(buf, key_);
118120
must_print_delim = 1;
119121
}
122+
if (omit_values) {
123+
strbuf_addch(buf, term);
124+
return 0;
125+
}
120126
if (types == TYPE_INT)
121127
sprintf(value, "%"PRId64,
122128
git_config_int64(key_, value_ ? value_ : ""));
@@ -549,7 +555,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
549555
default:
550556
usage_with_options(builtin_config_usage, builtin_config_options);
551557
}
552-
558+
if (omit_values &&
559+
!(actions == ACTION_LIST || actions == ACTION_GET_REGEXP)) {
560+
error("--name-only is only applicable to --list or --get-regexp");
561+
usage_with_options(builtin_config_usage, builtin_config_options);
562+
}
553563
if (actions == ACTION_LIST) {
554564
check_argc(argc, 0, 0);
555565
if (git_config_with_options(show_all_config, NULL,

contrib/completion/git-completion.bash

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,7 @@ _git_config ()
18871887
--get --get-all --get-regexp
18881888
--add --unset --unset-all
18891889
--remove-section --rename-section
1890+
--name-only
18901891
"
18911892
return
18921893
;;

t/t1300-repo-config.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,18 @@ test_expect_success '--list without repo produces empty output' '
352352
test_cmp expect output
353353
'
354354

355+
cat > expect << EOF
356+
beta.noindent
357+
nextsection.nonewline
358+
123456.a123
359+
version.1.2.3eX.alpha
360+
EOF
361+
362+
test_expect_success '--name-only --list' '
363+
git config --name-only --list >output &&
364+
test_cmp expect output
365+
'
366+
355367
cat > expect << EOF
356368
beta.noindent sillyValue
357369
nextsection.nonewline wow2 for me
@@ -362,6 +374,16 @@ test_expect_success '--get-regexp' '
362374
test_cmp expect output
363375
'
364376

377+
cat > expect << EOF
378+
beta.noindent
379+
nextsection.nonewline
380+
EOF
381+
382+
test_expect_success '--name-only --get-regexp' '
383+
git config --name-only --get-regexp in >output &&
384+
test_cmp expect output
385+
'
386+
365387
cat > expect << EOF
366388
wow2 for me
367389
wow4 for you

0 commit comments

Comments
 (0)