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 <peff@peff.net> Acked-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 25, 2012 at 08:00 UTC 4c0a89fcde219df8db8fdb9635ef2ef40d002a6e
3 files changed +18 -1
Documentation/config.txt
+4 -1
@@ -95,7 +95,9 @@ included file is expanded immediately, as if its contents had been
95 found at the location of the include directive. If the value of the
96 `include.path` variable is a relative path, the path is considered to be
97 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.
101
102 Example
103 ~~~~~~~
@@ -122,6 +124,7 @@ Example
124 [include]
125 path = /path/to/foo.inc ; include by absolute path
126 path = foo ; expand "foo" relative to the current file
127 + path = ~/foo ; expand "foo" in your $HOME directory
128
129 Variables
130 ~~~~~~~~~
config.c
+6
@@ -37,6 +37,11 @@ static int handle_path_include(const char *path, struct config_include_data *inc
37 {
38 int ret = 0;
39 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;
45
46 /*
47 * 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
68 inc->depth--;
69 }
70 strbuf_release(&buf);
71 + free(expanded);
72 return ret;
73 }
74
t/t1305-config-include.sh
+8
@@ -29,6 +29,14 @@ test_expect_success 'chained relative paths' '
29 test_cmp expect actual
30 '
31
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 +
40 test_expect_success 'include options can still be examined' '
41 echo "[test]one = 1" >one &&
42 echo "[include]path = one" >.gitconfig &&