git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] init: fix bug regarding ~/ expansion in init.templateDir
@ 2021-05-25  3:41 Matheus Tavares
  2021-05-25  4:21 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Matheus Tavares @ 2021-05-25  3:41 UTC (permalink / raw)
  To: git; +Cc: ajrhunt, peff

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>
---
 builtin/init-db.c |  3 ++-
 t/t0001-init.sh   | 28 ++++++++++++++++++++--------
 2 files changed, 22 insertions(+), 9 deletions(-)

diff --git a/builtin/init-db.c b/builtin/init-db.c
index c19b35f1e6..2167796ff2 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -212,8 +212,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);
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 0803994874..acd662e403 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -186,21 +186,33 @@ test_expect_success 'init with --template (blank)' '
 	test_path_is_missing template-blank/.git/info/exclude
 '
 
+init_no_templatedir_env () {
+	(
+		sane_unset GIT_TEMPLATE_DIR &&
+		NO_SET_GIT_TEMPLATE_DIR=t &&
+		export NO_SET_GIT_TEMPLATE_DIR &&
+		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" &&
-	(
-		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
-	) &&
+
+	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 &&
-- 
2.31.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] init: fix bug regarding ~/ expansion in init.templateDir
  2021-05-25  3:41 [PATCH] init: fix bug regarding ~/ expansion in init.templateDir Matheus Tavares
@ 2021-05-25  4:21 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2021-05-25  4:21 UTC (permalink / raw)
  To: Matheus Tavares; +Cc: git, ajrhunt, peff

Matheus Tavares <matheus.bernardino@usp.br> writes:

> 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>
> ---

The patch looks like a clean regression fix.

> +init_no_templatedir_env () {
> +	(
> +		sane_unset GIT_TEMPLATE_DIR &&
> +		NO_SET_GIT_TEMPLATE_DIR=t &&
> +		export NO_SET_GIT_TEMPLATE_DIR &&
> +		git init "$1"

	(
		sane_unset GIT_TEMPLATE_DIR &&
		NO_SET_GIT_TEMPLATE_DIR=t git init "$1"
	)

would be a shorter way to write it, but this is inheriting from the
original that used longhand, so it is OK, I guess.  We cannot lose
the subprocess because we do not want to lose GIT_TEMPLATE_DIR in
tests that run after a test that uses this function.

OK.  We could lose the outermost {} but let's take the patch as-is.

Thanks.

> +	)
> +}
> +
>  test_expect_success 'init with init.templatedir set' '
>  	mkdir templatedir-source &&
>  	echo Content >templatedir-source/file &&
>  	test_config_global init.templatedir "${HOME}/templatedir-source" &&
> -	(
> -		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
> -	) &&
> +
> +	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 &&

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-05-25  4:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-25  3:41 [PATCH] init: fix bug regarding ~/ expansion in init.templateDir Matheus Tavares
2021-05-25  4:21 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).