Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 4c0a89f

Browse files
peffgitster
authored andcommitted
config: expand tildes in include.path variable
You can already use relative paths in include.path, which means that including "foo" from your global "~/.gitconfig" will look in your home directory. However, you might want to do something clever like putting "~/.gitconfig-foo" in a specific repository's config file. Signed-off-by: Jeff King <[email protected]> Acked-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e8dde3e commit 4c0a89f

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Documentation/config.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ included file is expanded immediately, as if its contents had been
9595
found at the location of the include directive. If the value of the
9696
`include.path` variable is a relative path, the path is considered to be
9797
relative to the configuration file in which the include directive was
98-
found. See below for examples.
98+
found. The value of `include.path` is subject to tilde expansion: `{tilde}/`
99+
is expanded to the value of `$HOME`, and `{tilde}user/` to the specified
100+
user's home directory. See below for examples.
99101

100102
Example
101103
~~~~~~~
@@ -122,6 +124,7 @@ Example
122124
[include]
123125
path = /path/to/foo.inc ; include by absolute path
124126
path = foo ; expand "foo" relative to the current file
127+
path = ~/foo ; expand "foo" in your $HOME directory
125128

126129
Variables
127130
~~~~~~~~~

config.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ static int handle_path_include(const char *path, struct config_include_data *inc
3737
{
3838
int ret = 0;
3939
struct strbuf buf = STRBUF_INIT;
40+
char *expanded = expand_user_path(path);
41+
42+
if (!expanded)
43+
return error("Could not expand include path '%s'", path);
44+
path = expanded;
4045

4146
/*
4247
* Use an absolute path as-is, but interpret relative paths
@@ -63,6 +68,7 @@ static int handle_path_include(const char *path, struct config_include_data *inc
6368
inc->depth--;
6469
}
6570
strbuf_release(&buf);
71+
free(expanded);
6672
return ret;
6773
}
6874

t/t1305-config-include.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ test_expect_success 'chained relative paths' '
2929
test_cmp expect actual
3030
'
3131

32+
test_expect_success 'include paths get tilde-expansion' '
33+
echo "[test]one = 1" >one &&
34+
echo "[include]path = ~/one" >.gitconfig &&
35+
echo 1 >expect &&
36+
git config test.one >actual &&
37+
test_cmp expect actual
38+
'
39+
3240
test_expect_success 'include options can still be examined' '
3341
echo "[test]one = 1" >one &&
3442
echo "[include]path = one" >.gitconfig &&

0 commit comments

Comments
 (0)