Skip to content

Commit 9c787f3

Browse files
committed
Merge branch 'mm/config-path'
* mm/config-path: builtin-config: add --path option doing ~ and ~user expansion.
2 parents df24821 + 1349484 commit 9c787f3

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

Documentation/git-config.txt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ existing values that match the regexp are updated or unset. If
3737
you want to handle the lines that do *not* match the regex, just
3838
prepend a single exclamation mark in front (see also <<EXAMPLES>>).
3939

40-
The type specifier can be either '--int' or '--bool', which will make
40+
The type specifier can be either '--int' or '--bool', to make
4141
'git-config' ensure that the variable(s) are of the given type and
4242
convert the value to the canonical form (simple decimal number for int,
43-
a "true" or "false" string for bool). If no type specifier is passed,
44-
no checks or transformations are performed on the value.
43+
a "true" or "false" string for bool), or '--path', which does some
44+
path expansion (see '--path' below). If no type specifier is passed, no
45+
checks or transformations are performed on the value.
4546

4647
The file-option can be one of '--system', '--global' or '--file'
4748
which specify where the values will be read from or written to.
@@ -136,6 +137,13 @@ See also <<FILES>>.
136137
'git-config' will ensure that the output matches the format of
137138
either --bool or --int, as described above.
138139

140+
--path::
141+
'git-config' will expand leading '{tilde}' to the value of
142+
'$HOME', and '{tilde}user' to the home directory for the
143+
specified user. This option has no effect when setting the
144+
value (but you can use 'git config bla {tilde}/' from the
145+
command line to let your shell do the expansion).
146+
139147
-z::
140148
--null::
141149
For all options that output values and/or keys, always

builtin-config.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ static int end_null;
4545
#define TYPE_BOOL (1<<0)
4646
#define TYPE_INT (1<<1)
4747
#define TYPE_BOOL_OR_INT (1<<2)
48+
#define TYPE_PATH (1<<3)
4849

4950
static struct option builtin_config_options[] = {
5051
OPT_GROUP("Config file location"),
@@ -69,6 +70,7 @@ static struct option builtin_config_options[] = {
6970
OPT_BIT(0, "bool", &types, "value is \"true\" or \"false\"", TYPE_BOOL),
7071
OPT_BIT(0, "int", &types, "value is decimal number", TYPE_INT),
7172
OPT_BIT(0, "bool-or-int", &types, "value is --bool or --int", TYPE_BOOL_OR_INT),
73+
OPT_BIT(0, "path", &types, "value is a path (file or directory name)", TYPE_PATH),
7274
OPT_GROUP("Other"),
7375
OPT_BOOLEAN('z', "null", &end_null, "terminate values with NUL byte"),
7476
OPT_END(),
@@ -94,6 +96,7 @@ static int show_config(const char *key_, const char *value_, void *cb)
9496
{
9597
char value[256];
9698
const char *vptr = value;
99+
int must_free_vptr = 0;
97100
int dup_error = 0;
98101

99102
if (!use_key_regexp && strcmp(key_, key))
@@ -123,6 +126,9 @@ static int show_config(const char *key_, const char *value_, void *cb)
123126
vptr = v ? "true" : "false";
124127
else
125128
sprintf(value, "%d", v);
129+
} else if (types == TYPE_PATH) {
130+
git_config_pathname(&vptr, key_, value_);
131+
must_free_vptr = 1;
126132
}
127133
else
128134
vptr = value_?value_:"";
@@ -133,6 +139,12 @@ static int show_config(const char *key_, const char *value_, void *cb)
133139
}
134140
else
135141
printf("%s%c", vptr, term);
142+
if (must_free_vptr)
143+
/* If vptr must be freed, it's a pointer to a
144+
* dynamically allocated buffer, it's safe to cast to
145+
* const.
146+
*/
147+
free((char *)vptr);
136148

137149
return 0;
138150
}
@@ -215,7 +227,13 @@ static char *normalize_value(const char *key, const char *value)
215227
if (!value)
216228
return NULL;
217229

218-
if (types == 0)
230+
if (types == 0 || types == TYPE_PATH)
231+
/*
232+
* We don't do normalization for TYPE_PATH here: If
233+
* the path is like ~/foobar/, we prefer to store
234+
* "~/foobar/" in the config file, and to expand the ~
235+
* when retrieving the value.
236+
*/
219237
normalized = xstrdup(value);
220238
else {
221239
normalized = xmalloc(64);

t/t1300-repo-config.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,34 @@ test_expect_success 'set --bool-or-int' '
683683

684684
rm .git/config
685685

686+
cat >expect <<\EOF
687+
[path]
688+
home = ~/
689+
normal = /dev/null
690+
trailingtilde = foo~
691+
EOF
692+
693+
test_expect_success 'set --path' '
694+
git config --path path.home "~/" &&
695+
git config --path path.normal "/dev/null" &&
696+
git config --path path.trailingtilde "foo~" &&
697+
test_cmp expect .git/config'
698+
699+
cat >expect <<EOF
700+
$HOME/
701+
/dev/null
702+
foo~
703+
EOF
704+
705+
test_expect_success 'get --path' '
706+
git config --get --path path.home > result &&
707+
git config --get --path path.normal >> result &&
708+
git config --get --path path.trailingtilde >> result &&
709+
test_cmp expect result
710+
'
711+
712+
rm .git/config
713+
686714
git config quote.leading " test"
687715
git config quote.ending "test "
688716
git config quote.semicolon "test;test"

0 commit comments

Comments
 (0)