MCPcopy Create free account
hub / github.com/MiniCodeMonkey/chief / ListWorktrees

Function ListWorktrees

internal/git/worktree.go:110–147  ·  view source on GitHub ↗

ListWorktrees parses `git worktree list --porcelain` and returns all worktrees.

(repoDir string)

Source from the content-addressed store, hash-verified

108
109// ListWorktrees parses `git worktree list --porcelain` and returns all worktrees.
110func ListWorktrees(repoDir string) ([]Worktree, error) {
111 cmd := exec.Command("git", "worktree", "list", "--porcelain")
112 cmd.Dir = repoDir
113 output, err := cmd.Output()
114 if err != nil {
115 return nil, fmt.Errorf("failed to list worktrees: %w", err)
116 }
117
118 var worktrees []Worktree
119 var current Worktree
120
121 for _, line := range strings.Split(string(output), "\n") {
122 line = strings.TrimSpace(line)
123 switch {
124 case strings.HasPrefix(line, "worktree "):
125 current = Worktree{Path: strings.TrimPrefix(line, "worktree ")}
126 case strings.HasPrefix(line, "HEAD "):
127 current.HEAD = strings.TrimPrefix(line, "HEAD ")
128 case strings.HasPrefix(line, "branch "):
129 // refs/heads/branch-name -> branch-name
130 ref := strings.TrimPrefix(line, "branch ")
131 current.Branch = strings.TrimPrefix(ref, "refs/heads/")
132 case line == "prunable":
133 current.Prunable = true
134 case line == "":
135 if current.Path != "" {
136 worktrees = append(worktrees, current)
137 current = Worktree{}
138 }
139 }
140 }
141 // Append last entry if not empty-line terminated
142 if current.Path != "" {
143 worktrees = append(worktrees, current)
144 }
145
146 return worktrees, nil
147}
148
149// IsWorktree checks if a path is a valid git worktree.
150func IsWorktree(path string) bool {

Callers 1

TestListWorktreesFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestListWorktreesFunction · 0.68