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)
commita185dd58ecc17f2ea16985d59c9bb7b09bec7775
treeacbffd1454d75700aeafa8c776ad4411710a6fbc
parent37be11994f95d0519f3a7274df5254e56f56a7fe
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>
builtin/init-db.c
t/t0001-init.sh