Skip to content

Commit 1d1cd6a

Browse files
committed
fix(git): flip first flag immediately in ListWorktrees
The `first` flag (used to skip the main worktree entry) was not set to `false` until the blank-line separator was encountered. This meant that if the separator were ever missing, every linked worktree would be silently skipped. Move `first = false` into the `worktree` line handler where it logically belongs, and simplify the empty-line handler accordingly. Also replace a hand-rolled `contains` helper in tests with `strings.Contains`.
1 parent 26f48d3 commit 1d1cd6a

File tree

2 files changed

+4
-20
lines changed

2 files changed

+4
-20
lines changed

internal/git/git.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ func (g *Git) ListWorktrees() (map[string]string, error) {
316316

317317
if wtPath, ok := strings.CutPrefix(line, "worktree "); ok {
318318
if first {
319-
// Mark that we've seen the main worktree header; we'll skip
320-
// its branch line below.
319+
// Skip the main worktree entry entirely.
320+
first = false
321321
currentPath = ""
322322
} else {
323323
currentPath = wtPath
@@ -336,9 +336,6 @@ func (g *Git) ListWorktrees() (map[string]string, error) {
336336

337337
// Empty line separates worktree entries
338338
if line == "" {
339-
if first {
340-
first = false
341-
}
342339
currentPath = ""
343340
}
344341
}

internal/git/git_test.go

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"strings"
89
"testing"
910

1011
"github.com/boneskull/gh-stack/internal/git"
@@ -683,7 +684,7 @@ func TestGetResolvedGitDir(t *testing.T) {
683684
t.Errorf("worktree git dir should differ from main repo git dir")
684685
}
685686
// Should contain "worktrees" in the path
686-
if !contains(resolvedWtGitDir, "worktrees") {
687+
if !strings.Contains(resolvedWtGitDir, "worktrees") {
687688
t.Errorf("expected worktree git dir to contain 'worktrees', got %q", resolvedWtGitDir)
688689
}
689690
}
@@ -708,20 +709,6 @@ func TestIsRebaseInProgressWorktree(t *testing.T) {
708709
}
709710
}
710711

711-
// contains checks if substr is in s (simple helper to avoid importing strings in test).
712-
func contains(s, substr string) bool {
713-
return filepath.Base(s) == substr || len(s) >= len(substr) && searchString(s, substr)
714-
}
715-
716-
func searchString(s, substr string) bool {
717-
for i := 0; i <= len(s)-len(substr); i++ {
718-
if s[i:i+len(substr)] == substr {
719-
return true
720-
}
721-
}
722-
return false
723-
}
724-
725712
func TestRemoteBranchExists(t *testing.T) {
726713
// Create main repo
727714
dir := setupTestRepo(t)

0 commit comments

Comments
 (0)