]> git.ipfire.org Git - thirdparty/git.git/commitdiff
init: fix bug regarding ~/ expansion in init.templateDir
authorMatheus Tavares <matheus.bernardino@usp.br>
Tue, 25 May 2021 03:41:01 +0000 (00:41 -0300)
committerJunio C Hamano <gitster@pobox.com>
Tue, 25 May 2021 04:22:08 +0000 (13:22 +0900)
We used to read the init.templateDir setting at builtin/init-db.c using
a git_config() callback that, in turn, called git_config_pathname(). To
simplify the config reading logic at this file and plug a memory leak,
this was replaced by a direct call to git_config_get_value() at
e4de4502e6 ("init: remove git_init_db_config() while fixing leaks",
2021-03-14). However, this function doesn't provide path expanding
semantics, like git_config_pathname() does, so paths with '~/' and
'~user/' are treated literally. This makes 'git init' fail to handle
init.templateDir paths using these constructs:

$ git config init.templateDir '~/templates_dir'
$ git init
'warning: templates not found in ~/templates_dir'

Replace the git_config_get_value() call by git_config_get_pathname(),
which does the '~/' and '~user/' expansions. Also add a regression test.
Note that unlike git_config_get_value(), the config cache does not own
the memory for the path returned by git_config_get_pathname(), so we
must free() it.

Reported on IRC by rkta.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/init-db.c
t/t0001-init.sh

index efc66523e22c481cf31595f9ecddcd8ddac8b4ce..7d606de7baf951ef2a17253f5bf0a5f8979e69e7 100644 (file)
@@ -211,8 +211,9 @@ static int create_default_files(const char *template_path,
         * values (since we've just potentially changed what's available on
         * disk).
         */
-       git_config_get_value("init.templatedir", &init_template_dir);
+       git_config_get_pathname("init.templatedir", &init_template_dir);
        copy_templates(template_path, init_template_dir);
+       free((char *)init_template_dir);
        git_config_clear();
        reset_shared_repository();
        git_config(git_default_config, NULL);
index 0803994874f91659551ac5e9f1a8c0b484430cfe..acd662e403b9d3a89e58a0dc4d682b2b9e21b533 100755 (executable)
@@ -186,21 +186,33 @@ test_expect_success 'init with --template (blank)' '
        test_path_is_missing template-blank/.git/info/exclude
 '
 
-test_expect_success 'init with init.templatedir set' '
-       mkdir templatedir-source &&
-       echo Content >templatedir-source/file &&
-       test_config_global init.templatedir "${HOME}/templatedir-source" &&
+init_no_templatedir_env () {
        (
-               mkdir templatedir-set &&
-               cd templatedir-set &&
                sane_unset GIT_TEMPLATE_DIR &&
                NO_SET_GIT_TEMPLATE_DIR=t &&
                export NO_SET_GIT_TEMPLATE_DIR &&
-               git init
-       ) &&
+               git init "$1"
+       )
+}
+
+test_expect_success 'init with init.templatedir set' '
+       mkdir templatedir-source &&
+       echo Content >templatedir-source/file &&
+       test_config_global init.templatedir "${HOME}/templatedir-source" &&
+
+       init_no_templatedir_env templatedir-set &&
        test_cmp templatedir-source/file templatedir-set/.git/file
 '
 
+test_expect_success 'init with init.templatedir using ~ expansion' '
+       mkdir -p templatedir-source &&
+       echo Content >templatedir-source/file &&
+       test_config_global init.templatedir "~/templatedir-source" &&
+
+       init_no_templatedir_env templatedir-expansion &&
+       test_cmp templatedir-source/file templatedir-expansion/.git/file
+'
+
 test_expect_success 'init --bare/--shared overrides system/global config' '
        test_config_global core.bare false &&
        test_config_global core.sharedRepository 0640 &&