init: fix bug regarding ~/ expansion in init.templateDir

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>

Matheus Tavares committed May 25, 2021 at 00:41 UTC a185dd58ecc17f2ea16985d59c9bb7b09bec7775
2 files changed +22 -9
builtin/init-db.c
+2 -1
@@ -211,8 +211,9 @@ static int create_default_files(const char *template_path,
211 * values (since we've just potentially changed what's available on
212 * disk).
213 */
214 - git_config_get_value("init.templatedir", &init_template_dir);
214 + git_config_get_pathname("init.templatedir", &init_template_dir);
215 copy_templates(template_path, init_template_dir);
216 + free((char *)init_template_dir);
217 git_config_clear();
218 reset_shared_repository();
219 git_config(git_default_config, NULL);
t/t0001-init.sh
+20 -8
@@ -186,21 +186,33 @@ test_expect_success 'init with --template (blank)' '
186 test_path_is_missing template-blank/.git/info/exclude
187 '
188
189 -test_expect_success 'init with init.templatedir set' '
190 - mkdir templatedir-source &&
191 - echo Content >templatedir-source/file &&
192 - test_config_global init.templatedir "${HOME}/templatedir-source" &&
189 +init_no_templatedir_env () {
190 (
194 - mkdir templatedir-set &&
195 - cd templatedir-set &&
191 sane_unset GIT_TEMPLATE_DIR &&
192 NO_SET_GIT_TEMPLATE_DIR=t &&
193 export NO_SET_GIT_TEMPLATE_DIR &&
199 - git init
200 - ) &&
194 + git init "$1"
195 + )
196 +}
197 +
198 +test_expect_success 'init with init.templatedir set' '
199 + mkdir templatedir-source &&
200 + echo Content >templatedir-source/file &&
201 + test_config_global init.templatedir "${HOME}/templatedir-source" &&
202 +
203 + init_no_templatedir_env templatedir-set &&
204 test_cmp templatedir-source/file templatedir-set/.git/file
205 '
206
207 +test_expect_success 'init with init.templatedir using ~ expansion' '
208 + mkdir -p templatedir-source &&
209 + echo Content >templatedir-source/file &&
210 + test_config_global init.templatedir "~/templatedir-source" &&
211 +
212 + init_no_templatedir_env templatedir-expansion &&
213 + test_cmp templatedir-source/file templatedir-expansion/.git/file
214 +'
215 +
216 test_expect_success 'init --bare/--shared overrides system/global config' '
217 test_config_global core.bare false &&
218 test_config_global core.sharedRepository 0640 &&