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

[PATCH] worktree: allow the main brach of a bare repository to be checked out

From
Dennis Kaarsemaker <dennis@kaarsemaker.net>
Date
Oct 12, 2016, 16:41 UTC
Message-ID
<20161012164104.zcxpcwqrp5h65qmc@hurricane>
In-Reply-To
<CACsJy8CmgmGLEi0xQUY9Eo-4FkA4eDNk9WJ2LtEDVFQBjbFdCA@mail.gmail.com>

In bare repositories, get_worktrees() still returns the main repository, so git worktree list can show it. ignore it in find_shared_symref so we can still check out the main branch.

Signed-off-by: Dennis Kaarsemaker <dennis@kaarsemaker.net>
---
 t/t2025-worktree-add.sh | 8 ++++++++
 worktree.c              | 2 ++
 2 files changed, 10 insertions(+)
On Sun, 2016-10-09 at 17:52 +0700, Duy Nguyen wrote:
Show 18 quoted lines
> On Sun, Oct 9, 2016 at 2:51 PM, Dennis Kaarsemaker > <dennis@kaarsemaker.net> wrote:
> > On Sat, 2016-10-08 at 19:30 -0500, Michael Tutty wrote:
> > > 
> > > The only exception seems to be merging to master. When I do git
> > > worktree add /tmp/path/to/worktree master I get an error:
> > > 
> > > [fatal: 'master' is already checked out at '/path/to/bare/repo']
> > > 
> > 
> > The worktree code treats the base repo as a worktree, even if it's
> > bare. For the purpose of being able to do a checkout of the main branch
> > of a bare repo, this patch should do:
> > 
> --snip--
> 
> You're fast :) I'm still studying  8d9fdd7 (worktree.c: check whether
> branch is rebased in another worktree - 2016-04-22). But yeah that
> should fix it.
OK, so here it is as a proper patch.
D.
diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
index 4bcc335..2996c38 100755
--- a/t/t2025-worktree-add.sh
+++ b/t/t2025-worktree-add.sh
@@ -138,6 +138,14 @@ test_expect_success 'checkout from a bare repo without "add"' '
 	)
 '
 
++test_expect_success '"add" default branch of a bare repo' '
+	(
+		git clone --bare . bare2 &&
+		cd bare2 &&
+		git worktree add ../there3 master
+	)
+'
+
 test_expect_success 'checkout with grafts' '
 	test_when_finished rm .git/info/grafts &&
 	test_commit abc &&
diff --git a/worktree.c b/worktree.c
index 5acfe4c..35e95b7 100644
--- a/worktree.c
+++ b/worktree.c
@@ -345,6 +345,8 @@ const struct worktree *find_shared_symref(const char *symref,
 
 	for (i = 0; worktrees[i]; i++) {
 		struct worktree *wt = worktrees[i];
+		if(wt->is_bare)
+			continue;
 
 		if (wt->is_detached && !strcmp(symref, "HEAD")) {
 			if (is_worktree_being_rebased(wt, target)) {
-- 
2.10.1-356-g947a599


-- 
Dennis Kaarsemaker <dennis@kaarsemaker.net>
http://twitter.com/seveas
Previous: Kevin DaudtNext: Michael Tutty
Message 9 of 14 in “Bug? git worktree fails with master on bare repo”
  1. Michael TuttyOct 9, 2016
  2. Kevin DaudtOct 9, 2016
  3. Dennis KaarsemakerOct 9, 2016
  4. Duy NguyenOct 9, 2016
  5. Michael TuttyOct 9, 2016
  6. Duy NguyenOct 10, 2016
  7. Michael TuttyOct 10, 2016
  8. Kevin DaudtOct 11, 2016
  9. worktree: allow the main brach of a bare repository to be checked outDennis Kaarsemaker, Oct 12, 2016
  10. Michael TuttyOct 12, 2016
  11. Junio C HamanoOct 12, 2016
  12. Junio C HamanoOct 12, 2016
  13. Duy NguyenOct 13, 2016
  14. Dennis KaarsemakerOct 12, 2016

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.