config: expand tildes in include.path variable
authorJeff King <peff@peff.net>
Wed, 25 Apr 2012 12:00:36 +0000 (08:00 -0400)
committerJunio C Hamano <gitster@pobox.com>
Mon, 30 Apr 2012 00:46:32 +0000 (17:46 -0700)
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>
Documentation/config.txt
config.c
t/t1305-config-include.sh
index c081657be774a70b453f493be11fbfb670452e86..e67c8ef36908c7cb572ebdbed1e21d920b4f547b 100644 (file)
@@ -95,7 +95,9 @@ included file is expanded immediately, as if its contents had been
 found at the location of the include directive. If the value of the
 `include.path` variable is a relative path, the path is considered to be
 relative to the configuration file in which the include directive was
 found at the location of the include directive. If the value of the
 `include.path` variable is a relative path, the path is considered to be
 relative to the configuration file in which the include directive was
-found. See below for examples.
+found. The value of `include.path` is subject to tilde expansion: `{tilde}/`
+is expanded to the value of `$HOME`, and `{tilde}user/` to the specified
+user's home directory. See below for examples.
 
 Example
 ~~~~~~~
 
 Example
 ~~~~~~~
@@ -122,6 +124,7 @@ Example
        [include]
                path = /path/to/foo.inc ; include by absolute path
                path = foo ; expand "foo" relative to the current file
        [include]
                path = /path/to/foo.inc ; include by absolute path
                path = foo ; expand "foo" relative to the current file
+               path = ~/foo ; expand "foo" in your $HOME directory
 
 Variables
 ~~~~~~~~~
 
 Variables
 ~~~~~~~~~
index 68d32940f3f8f1512fa36702beb98c2457ca5d46..2bbf02d1e8c2c05f9fba1149c307c1b3b06517fd 100644 (file)
--- a/config.c
+++ b/config.c
@@ -37,6 +37,11 @@ static int handle_path_include(const char *path, struct config_include_data *inc
 {
        int ret = 0;
        struct strbuf buf = STRBUF_INIT;
 {
        int ret = 0;
        struct strbuf buf = STRBUF_INIT;
+       char *expanded = expand_user_path(path);
+
+       if (!expanded)
+               return error("Could not expand include path '%s'", path);
+       path = expanded;
 
        /*
         * Use an absolute path as-is, but interpret relative paths
 
        /*
         * 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
                inc->depth--;
        }
        strbuf_release(&buf);
                inc->depth--;
        }
        strbuf_release(&buf);
+       free(expanded);
        return ret;
 }
 
        return ret;
 }
 
index 4b1cbaa0284f9b9cc3e29bf5f07b1a6a1f3a283c..a70707620f146d3fce69f77e08cae3a47253f157 100755 (executable)
@@ -29,6 +29,14 @@ test_expect_success 'chained relative paths' '
        test_cmp expect actual
 '
 
        test_cmp expect actual
 '
 
+test_expect_success 'include paths get tilde-expansion' '
+       echo "[test]one = 1" >one &&
+       echo "[include]path = ~/one" >.gitconfig &&
+       echo 1 >expect &&
+       git config test.one >actual &&
+       test_cmp expect actual
+'
+
 test_expect_success 'include options can still be examined' '
        echo "[test]one = 1" >one &&
        echo "[include]path = one" >.gitconfig &&
 test_expect_success 'include options can still be examined' '
        echo "[test]one = 1" >one &&
        echo "[include]path = one" >.gitconfig &&