git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
* [PATCH] config: complain about --worktree outside of a git repo
@ 2020-09-09 13:16 Matheus Tavares
  2020-09-09 19:47 ` Junio C Hamano
  0 siblings, 1 reply; 2+ messages in thread
From: Matheus Tavares @ 2020-09-09 13:16 UTC (permalink / raw)
  To: git; +Cc: peff

Running `git config --worktree` outside of a git repository hits a BUG()
when trying to enumerate the worktrees. Let's catch this error earlier
and die() with a friendlier message.

Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
---
 builtin/config.c  | 12 ++++++++----
 t/t1300-config.sh | 13 ++++++++-----
 2 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/builtin/config.c b/builtin/config.c
index 5e39f61885..53e411d68a 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -628,11 +628,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 		usage_builtin_config();
 	}
 
-	if (use_local_config && nongit)
-		die(_("--local can only be used inside a git repository"));
+	if (nongit) {
+		if (use_local_config)
+			die(_("--local can only be used inside a git repository"));
+		if (given_config_source.blob)
+			die(_("--blob can only be used inside a git repository"));
+		if (use_worktree_config)
+			die(_("--worktree can only be used inside a git repository"));
 
-	if (given_config_source.blob && nongit)
-		die(_("--blob can only be used inside a git repository"));
+	}
 
 	if (given_config_source.file &&
 			!strcmp(given_config_source.file, "-")) {
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
index 97ebfe1f9d..825d9a184f 100755
--- a/t/t1300-config.sh
+++ b/t/t1300-config.sh
@@ -1836,11 +1836,14 @@ test_expect_success '--show-scope with --show-origin' '
 	test_cmp expect output
 '
 
-test_expect_success '--local requires a repo' '
-	# we expect 128 to ensure that we do not simply
-	# fail to find anything and return code "1"
-	test_expect_code 128 nongit git config --local foo.bar
-'
+for opt in --local --worktree
+do
+	test_expect_success "$opt requires a repo" '
+		# we expect 128 to ensure that we do not simply
+		# fail to find anything and return code "1"
+		test_expect_code 128 nongit git config $opt foo.bar
+	'
+done
 
 cat >.git/config <<-\EOF &&
 [core]
-- 
2.28.0


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

* Re: [PATCH] config: complain about --worktree outside of a git repo
  2020-09-09 13:16 [PATCH] config: complain about --worktree outside of a git repo Matheus Tavares
@ 2020-09-09 19:47 ` Junio C Hamano
  0 siblings, 0 replies; 2+ messages in thread
From: Junio C Hamano @ 2020-09-09 19:47 UTC (permalink / raw)
  To: Matheus Tavares; +Cc: git, peff

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

> Running `git config --worktree` outside of a git repository hits a BUG()
> when trying to enumerate the worktrees. Let's catch this error earlier
> and die() with a friendlier message.
>
> Signed-off-by: Matheus Tavares <matheus.bernardino@usp.br>
> ---
>  builtin/config.c  | 12 ++++++++----
>  t/t1300-config.sh | 13 ++++++++-----
>  2 files changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/builtin/config.c b/builtin/config.c
> index 5e39f61885..53e411d68a 100644
> --- a/builtin/config.c
> +++ b/builtin/config.c
> @@ -628,11 +628,15 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>  		usage_builtin_config();
>  	}
>  
> -	if (use_local_config && nongit)
> -		die(_("--local can only be used inside a git repository"));
> +	if (nongit) {
> +		if (use_local_config)
> +			die(_("--local can only be used inside a git repository"));
> +		if (given_config_source.blob)
> +			die(_("--blob can only be used inside a git repository"));
> +		if (use_worktree_config)
> +			die(_("--worktree can only be used inside a git repository"));
>  
> -	if (given_config_source.blob && nongit)
> -		die(_("--blob can only be used inside a git repository"));
> +	}

OK.  The updated structure makes it more obvious that we handle
three special cases.  Good jobs.

Thanks.

>  
>  	if (given_config_source.file &&
>  			!strcmp(given_config_source.file, "-")) {
> diff --git a/t/t1300-config.sh b/t/t1300-config.sh
> index 97ebfe1f9d..825d9a184f 100755
> --- a/t/t1300-config.sh
> +++ b/t/t1300-config.sh
> @@ -1836,11 +1836,14 @@ test_expect_success '--show-scope with --show-origin' '
>  	test_cmp expect output
>  '
>  
> -test_expect_success '--local requires a repo' '
> -	# we expect 128 to ensure that we do not simply
> -	# fail to find anything and return code "1"
> -	test_expect_code 128 nongit git config --local foo.bar
> -'
> +for opt in --local --worktree
> +do
> +	test_expect_success "$opt requires a repo" '
> +		# we expect 128 to ensure that we do not simply
> +		# fail to find anything and return code "1"
> +		test_expect_code 128 nongit git config $opt foo.bar
> +	'
> +done
>  
>  cat >.git/config <<-\EOF &&
>  [core]

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

end of thread, other threads:[~2020-09-09 19:47 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-09 13:16 [PATCH] config: complain about --worktree outside of a git repo Matheus Tavares
2020-09-09 19:47 ` Junio C Hamano

git@vger.kernel.org list mirror (unofficial, one of many)

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://public-inbox.org/git
	git clone --mirror http://ou63pmih66umazou.onion/git
	git clone --mirror http://czquwvybam4bgbro.onion/git
	git clone --mirror http://hjrcffqmbrq6wope.onion/git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 git git/ https://public-inbox.org/git \
		git@vger.kernel.org
	public-inbox-index git

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.version-control.git
	nntp://ou63pmih66umazou.onion/inbox.comp.version-control.git
	nntp://czquwvybam4bgbro.onion/inbox.comp.version-control.git
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.version-control.git
	nntp://news.gmane.io/gmane.comp.version-control.git
 note: .onion URLs require Tor: https://www.torproject.org/

code repositories for project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git