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 <frank@lichtenheld.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Frank Lichtenheld committed
Jun 25, 2007 at 16:03 UTC
2275d502114c71045af991697048191fed88aac4
3 files changed
+58
-10
Documentation/git-config.txt
+13
-5
@@ -9,17 +9,17 @@ git-config - Get and set repository or global options
9
SYNOPSIS
10
--------
11
[verse]
12
-'git-config' [--system | --global] name [value [value_regex]]
12
+'git-config' [--system | --global] [-z|--null] name [value [value_regex]]
13
'git-config' [--system | --global] --add name value
14
'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]
18
'git-config' [--system | --global] --unset name [value_regex]
19
'git-config' [--system | --global] --unset-all name [value_regex]
20
'git-config' [--system | --global] --rename-section old_name new_name
21
'git-config' [--system | --global] --remove-section name
22
-'git-config' [--system | --global] -l | --list
22
+'git-config' [--system | --global] [-z|--null] -l | --list
23
24
DESCRIPTION
25
-----------
@@ -118,6 +118,14 @@ See also <<FILES>>.
118
in the config file will cause the value to be multiplied
119
by 1024, 1048576, or 1073741824 prior to output.
120
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
+
129
130
[[FILES]]
131
FILES
builtin-config.c
+13
-5
@@ -2,7 +2,7 @@
2
#include "cache.h"
3
4
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";
6
7
static char *key;
8
static regex_t *key_regexp;
@@ -12,14 +12,17 @@ static int use_key_regexp;
12
static int do_all;
13
static int do_not_match;
14
static int seen;
15
+static char delim = '=';
16
+static char key_delim = ' ';
17
+static char term = '\n';
18
static enum { T_RAW, T_INT, T_BOOL } type = T_RAW;
19
20
static int show_all_config(const char *key_, const char *value_)
21
{
22
if (value_)
20
- printf("%s=%s\n", key_, value_);
23
+ printf("%s%c%s%c", key_, delim, value_, term);
24
else
22
- printf("%s\n", key_);
25
+ printf("%s%c", key_, term);
26
return 0;
27
}
28
@@ -40,7 +43,7 @@ static int show_config(const char* key_, const char* value_)
43
44
if (show_keys) {
45
if (value_)
43
- printf("%s ", key_);
46
+ printf("%s%c", key_, key_delim);
47
else
48
printf("%s", key_);
49
}
@@ -58,7 +61,7 @@ static int show_config(const char* key_, const char* value_)
61
key_, vptr);
62
}
63
else
61
- printf("%s\n", vptr);
64
+ printf("%s%c", vptr, term);
65
66
return 0;
67
}
@@ -159,6 +162,11 @@ int cmd_config(int argc, const char **argv, const char *prefix)
162
}
163
else if (!strcmp(argv[1], "--system"))
164
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
+ }
170
else if (!strcmp(argv[1], "--rename-section")) {
171
int ret;
172
if (argc != 4)
t/t1300-repo-config.sh
+32
@@ -519,4 +519,36 @@ git config --list > result
519
520
test_expect_success 'value continued on next line' 'cmp result expect'
521
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
+
554
test_done