From 9dad70dcb7f1b857480364eb02b54f4c9378d9fa Mon Sep 17 00:00:00 2001 From: Filippo Valsorda Date: Fri, 29 Oct 2021 17:05:32 -0400 Subject: git-codereview: handle branches checked out in a worktree Change-Id: I085f1d27e9d109a81bc40603b98ebf0d827dfd66 Reviewed-on: https://go-review.googlesource.com/c/review/+/359814 Trust: Filippo Valsorda Run-TryBot: Filippo Valsorda Auto-Submit: Filippo Valsorda TryBot-Result: Gopher Robot Reviewed-by: Russ Cox --- git-codereview/branch.go | 4 ++++ git-codereview/branch_test.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/git-codereview/branch.go b/git-codereview/branch.go index 37da5fa..c9b21a0 100644 --- a/git-codereview/branch.go +++ b/git-codereview/branch.go @@ -402,6 +402,10 @@ func LocalBranches() []*Branch { // (git rev-parse --abbrev-ref HEAD returns 'HEAD'). s = current.Name } + // + marks a branch checked out in a worktree. Worktrees in detached + // HEAD mode don't appear in the "git branch" output, so this is always + // a normal name. + s = strings.TrimPrefix(s, "+ ") branches = append(branches, &Branch{Name: s}) } return branches diff --git a/git-codereview/branch_test.go b/git-codereview/branch_test.go index d7db6ee..2ac3c03 100644 --- a/git-codereview/branch_test.go +++ b/git-codereview/branch_test.go @@ -5,6 +5,7 @@ package main import ( + "path/filepath" "reflect" "strings" "testing" @@ -83,6 +84,11 @@ func TestLocalBranches(t *testing.T) { t.Logf("detached head mode") trun(t, gt.client, "git", "checkout", "HEAD^0") checkLocalBranches(t, "HEAD", "main", "newbranch") + + t.Logf("worktree") + wt := filepath.Join(gt.tmpdir, "git-worktree") + trun(t, gt.client, "git", "worktree", "add", "-b", "wtbranch", wt) + checkLocalBranches(t, "HEAD", "main", "newbranch", "wtbranch") } func checkLocalBranches(t *testing.T, want ...string) { -- cgit v1.3