(branch string)
| 50 | } |
| 51 | |
| 52 | func (cli *ChatCLI) worktreeCreate(branch string) { |
| 53 | // Check if we're in a git repo |
| 54 | if !isGitRepo() { |
| 55 | fmt.Println(colorize(" "+i18n.T("wt.cmd.err_not_git_repo"), ColorRed)) |
| 56 | return |
| 57 | } |
| 58 | |
| 59 | // Get repo root |
| 60 | repoRoot := getGitRepoRoot() |
| 61 | if repoRoot == "" { |
| 62 | fmt.Println(colorize(" "+i18n.T("wt.cmd.err_repo_root"), ColorRed)) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | // Sanitize branch name |
| 67 | branch = strings.TrimSpace(branch) |
| 68 | safeName := strings.ReplaceAll(branch, "/", "-") |
| 69 | |
| 70 | // Create worktree path alongside the repo root |
| 71 | worktreePath := filepath.Join(filepath.Dir(repoRoot), fmt.Sprintf("%s-worktree-%s", filepath.Base(repoRoot), safeName)) |
| 72 | |
| 73 | // Check if branch already exists |
| 74 | branchExists := false |
| 75 | out, err := exec.Command("git", "-C", repoRoot, "branch", "--list", branch).Output() //#nosec G204 -- agent/CLI tool execution; commands validated by command_validator + policy_manager upstream |
| 76 | if err == nil && strings.TrimSpace(string(out)) != "" { |
| 77 | branchExists = true |
| 78 | } |
| 79 | |
| 80 | var cmd *exec.Cmd |
| 81 | if branchExists { |
| 82 | // Checkout existing branch in new worktree |
| 83 | cmd = exec.Command("git", "-C", repoRoot, "worktree", "add", worktreePath, branch) //#nosec G204 -- agent/CLI tool execution; commands validated by command_validator + policy_manager upstream |
| 84 | } else { |
| 85 | // Create new branch in new worktree |
| 86 | cmd = exec.Command("git", "-C", repoRoot, "worktree", "add", "-b", branch, worktreePath) //#nosec G204 -- agent/CLI tool execution; commands validated by command_validator + policy_manager upstream |
| 87 | } |
| 88 | |
| 89 | output, err := cmd.CombinedOutput() |
| 90 | if err != nil { |
| 91 | fmt.Println(colorize(" "+i18n.T("wt.cmd.err_create", strings.TrimSpace(string(output))), ColorRed)) |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | // Change to the worktree directory |
| 96 | if err := os.Chdir(worktreePath); err != nil { |
| 97 | fmt.Println(colorize(" "+i18n.T("wt.cmd.warn_chdir_after_create", worktreePath, err), ColorYellow)) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | fmt.Println(colorize(" "+i18n.T("wt.cmd.created_active"), ColorGreen)) |
| 102 | fmt.Println(colorize(" "+i18n.T("wt.cmd.kv_branch", branch), ColorCyan)) |
| 103 | fmt.Println(colorize(" "+i18n.T("wt.cmd.kv_path", worktreePath), ColorGray)) |
| 104 | fmt.Println() |
| 105 | |
| 106 | // Invalidate context builder cache since CWD changed |
| 107 | if cli.contextBuilder != nil { |
| 108 | cli.contextBuilder.InvalidateCache() |
| 109 | } |
no test coverage detected