Skip to content

Commit 2275d50

Browse files
flichtenheldgitster
authored andcommitted
config: Add --null/-z option for null-delimted output
Use \n as delimiter between key and value and \0 as delimiter after each key/value pair. This should be easily parsable output. Signed-off-by: Frank Lichtenheld <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68fb465 commit 2275d50

File tree

3 files changed

+58
-10
lines changed

3 files changed

+58
-10
lines changed

Documentation/git-config.txt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ git-config - Get and set repository or global options
99
SYNOPSIS
1010
--------
1111
[verse]
12-
'git-config' [--system | --global] name [value [value_regex]]
12+
'git-config' [--system | --global] [-z|--null] name [value [value_regex]]
1313
'git-config' [--system | --global] --add name value
1414
'git-config' [--system | --global] --replace-all name [value [value_regex]]
15-
'git-config' [--system | --global] [type] --get name [value_regex]
16-
'git-config' [--system | --global] [type] --get-all name [value_regex]
17-
'git-config' [--system | --global] [type] --get-regexp name_regex [value_regex]
15+
'git-config' [--system | --global] [type] [-z|--null] --get name [value_regex]
16+
'git-config' [--system | --global] [type] [-z|--null] --get-all name [value_regex]
17+
'git-config' [--system | --global] [type] [-z|--null] --get-regexp name_regex [value_regex]
1818
'git-config' [--system | --global] --unset name [value_regex]
1919
'git-config' [--system | --global] --unset-all name [value_regex]
2020
'git-config' [--system | --global] --rename-section old_name new_name
2121
'git-config' [--system | --global] --remove-section name
22-
'git-config' [--system | --global] -l | --list
22+
'git-config' [--system | --global] [-z|--null] -l | --list
2323

2424
DESCRIPTION
2525
-----------
@@ -118,6 +118,14 @@ See also <<FILES>>.
118118
in the config file will cause the value to be multiplied
119119
by 1024, 1048576, or 1073741824 prior to output.
120120

121+
-z, --null::
122+
For all options that output values and/or keys, always
123+
end values with with the null character (instead of a
124+
newline). Use newline instead as a delimiter between
125+
key and value. This allows for secure parsing of the
126+
output without getting confused e.g. by values that
127+
contain line breaks.
128+
121129

122130
[[FILES]]
123131
FILES

builtin-config.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#include "cache.h"
33

44
static const char git_config_set_usage[] =
5-
"git-config [ --global | --system ] [ --bool | --int ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list";
5+
"git-config [ --global | --system ] [ --bool | --int ] [ -z | --null ] [--get | --get-all | --get-regexp | --replace-all | --add | --unset | --unset-all] name [value [value_regex]] | --rename-section old_name new_name | --remove-section name | --list";
66

77
static char *key;
88
static regex_t *key_regexp;
@@ -12,14 +12,17 @@ static int use_key_regexp;
1212
static int do_all;
1313
static int do_not_match;
1414
static int seen;
15+
static char delim = '=';
16+
static char key_delim = ' ';
17+
static char term = '\n';
1518
static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
1619

1720
static int show_all_config(const char *key_, const char *value_)
1821
{
1922
if (value_)
20-
printf("%s=%s\n", key_, value_);
23+
printf("%s%c%s%c", key_, delim, value_, term);
2124
else
22-
printf("%s\n", key_);
25+
printf("%s%c", key_, term);
2326
return 0;
2427
}
2528

@@ -40,7 +43,7 @@ static int show_config(const char* key_, const char* value_)
4043

4144
if (show_keys) {
4245
if (value_)
43-
printf("%s ", key_);
46+
printf("%s%c", key_, key_delim);
4447
else
4548
printf("%s", key_);
4649
}
@@ -58,7 +61,7 @@ static int show_config(const char* key_, const char* value_)
5861
key_, vptr);
5962
}
6063
else
61-
printf("%s\n", vptr);
64+
printf("%s%c", vptr, term);
6265

6366
return 0;
6467
}
@@ -159,6 +162,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
159162
}
160163
else if (!strcmp(argv[1], "--system"))
161164
setenv("GIT_CONFIG", ETC_GITCONFIG, 1);
165+
else if (!strcmp(argv[1], "--null") || !strcmp(argv[1], "-z")) {
166+
term = '\0';
167+
delim = '\n';
168+
key_delim = '\n';
169+
}
162170
else if (!strcmp(argv[1], "--rename-section")) {
163171
int ret;
164172
if (argc != 4)

t/t1300-repo-config.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,4 +519,36 @@ git config --list > result
519519

520520
test_expect_success 'value continued on next line' 'cmp result expect'
521521

522+
cat > .git/config <<\EOF
523+
[section "sub=section"]
524+
val1 = foo=bar
525+
val2 = foo\nbar
526+
val3 = \n\n
527+
val4 =
528+
val5
529+
EOF
530+
531+
cat > expect <<\EOF
532+
Key: section.sub=section.val1
533+
Value: foo=bar
534+
Key: section.sub=section.val2
535+
Value: foo
536+
bar
537+
Key: section.sub=section.val3
538+
Value:
539+
540+
541+
Key: section.sub=section.val4
542+
Value:
543+
Key: section.sub=section.val5
544+
EOF
545+
546+
git config --null --list | perl -0ne 'chop;($key,$value)=split(/\n/,$_,2);print "Key: $key\n";print "Value: $value\n" if defined($value)' > result
547+
548+
test_expect_success '--null --list' 'cmp result expect'
549+
550+
git config --null --get-regexp 'val[0-9]' | perl -0ne 'chop;($key,$value)=split(/\n/,$_,2);print "Key: $key\n";print "Value: $value\n" if defined($value)' > result
551+
552+
test_expect_success '--null --get-regexp' 'cmp result expect'
553+
522554
test_done

0 commit comments

Comments
 (0)