Skip to content

Commit a185dd5

Browse files
matheustavaresgitster
authored andcommitted
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 e4de450 ("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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 37be119 commit a185dd5

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

builtin/init-db.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ static int create_default_files(const char *template_path,
211211
* values (since we've just potentially changed what's available on
212212
* disk).
213213
*/
214-
git_config_get_value("init.templatedir", &init_template_dir);
214+
git_config_get_pathname("init.templatedir", &init_template_dir);
215215
copy_templates(template_path, init_template_dir);
216+
free((char *)init_template_dir);
216217
git_config_clear();
217218
reset_shared_repository();
218219
git_config(git_default_config, NULL);

t/t0001-init.sh

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,21 +186,33 @@ test_expect_success 'init with --template (blank)' '
186186
test_path_is_missing template-blank/.git/info/exclude
187187
'
188188

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 () {
193190
(
194-
mkdir templatedir-set &&
195-
cd templatedir-set &&
196191
sane_unset GIT_TEMPLATE_DIR &&
197192
NO_SET_GIT_TEMPLATE_DIR=t &&
198193
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 &&
201204
test_cmp templatedir-source/file templatedir-set/.git/file
202205
'
203206

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+
204216
test_expect_success 'init --bare/--shared overrides system/global config' '
205217
test_config_global core.bare false &&
206218
test_config_global core.sharedRepository 0640 &&

0 commit comments

Comments
 (0)