git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] config: complain about --worktree outside of a git repo

From
Matheus Tavares <matheus.bernardino@usp.br>
Date
Sep 9, 2020, 13:16 UTC
Message-ID
<3dfd33eda3f9cbd0d87ba7bb31f4501aa7b8b8a8.1599657308.git.matheus.bernardino@usp.br>

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
Next: Junio C Hamano
Message 1 of 2 in “config: complain about --worktree outside of a git repo”
  1. config: complain about --worktree outside of a git repoMatheus Tavares, Sep 9, 2020
  2. Junio C HamanoSep 9, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.