MCPcopy Create free account
hub / github.com/diillson/chatcli / worktreeRemove

Method worktreeRemove

cli/worktree.go:145–208  ·  view source on GitHub ↗
(target string)

Source from the content-addressed store, hash-verified

143}
144
145func (cli *ChatCLI) worktreeRemove(target string) {
146 if !isGitRepo() {
147 fmt.Println(colorize(" "+i18n.T("wt.cmd.not_in_git_repo"), ColorYellow))
148 return
149 }
150
151 // Try as path first, then as branch name
152 removePath := target
153 if !filepath.IsAbs(removePath) {
154 // Try to find by branch name in worktree list
155 cmd := exec.Command("git", "worktree", "list", "--porcelain")
156 output, err := cmd.Output()
157 if err == nil {
158 lines := strings.Split(string(output), "\n")
159 for i, line := range lines {
160 if strings.HasPrefix(line, "branch refs/heads/"+target) {
161 // Found the branch, get the path from the previous "worktree" line
162 for j := i - 1; j >= 0; j-- {
163 if strings.HasPrefix(lines[j], "worktree ") {
164 removePath = strings.TrimPrefix(lines[j], "worktree ")
165 break
166 }
167 }
168 break
169 }
170 }
171 }
172 }
173
174 // Don't remove the main worktree
175 repoRoot := getGitRepoRoot()
176 if removePath == repoRoot {
177 fmt.Println(colorize(" "+i18n.T("wt.cmd.err_remove_main"), ColorRed))
178 return
179 }
180
181 cwd, _ := os.Getwd()
182 if cwd == removePath {
183 // We're in the worktree being removed, cd to repo root first
184 if err := os.Chdir(repoRoot); err != nil {
185 fmt.Println(colorize(" "+i18n.T("wt.cmd.err_chdir", err), ColorRed))
186 return
187 }
188 fmt.Println(colorize(" "+i18n.T("wt.cmd.back_to_main", repoRoot), ColorGray))
189 }
190
191 cmd := exec.Command("git", "worktree", "remove", removePath) //#nosec G204 -- agent/CLI tool execution; commands validated by command_validator + policy_manager upstream
192 _, err := cmd.CombinedOutput()
193 if err != nil {
194 // Try force remove
195 cmd = exec.Command("git", "worktree", "remove", "--force", removePath) //#nosec G204 -- agent/CLI tool execution; commands validated by command_validator + policy_manager upstream
196 output, err := cmd.CombinedOutput()
197 if err != nil {
198 fmt.Println(colorize(" "+i18n.T("wt.cmd.err_remove", strings.TrimSpace(string(output))), ColorRed))
199 return
200 }
201 }
202

Callers 1

handleWorktreeCommandMethod · 0.95

Calls 6

TFunction · 0.92
isGitRepoFunction · 0.85
colorizeFunction · 0.85
getGitRepoRootFunction · 0.85
OutputMethod · 0.65
InvalidateCacheMethod · 0.45

Tested by

no test coverage detected