Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions cmd/worktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func createNewBranchWorktree(gitClient git.GitClient, branchName, baseBranch, wo
if !dryRun {
fmt.Println(ui.Success(fmt.Sprintf("Created worktree at %s", worktreePath)))
fmt.Println(ui.Success(fmt.Sprintf("Branch %s with parent %s", ui.Branch(branchName), ui.Branch(baseBranch))))
symlinkEnvFile(gitClient, worktreePath)
fmt.Printf("\nTo switch to this worktree, run:\n %s\n", ui.Command(fmt.Sprintf("cd %s", worktreePath)))
}

Expand All @@ -296,6 +297,7 @@ func createWorktreeForExisting(gitClient git.GitClient, branchName, worktreePath
}
if !dryRun {
fmt.Println(ui.Success(fmt.Sprintf("Created worktree at %s", worktreePath)))
symlinkEnvFile(gitClient, worktreePath)
fmt.Printf("\nTo switch to this worktree, run:\n %s\n", ui.Command(fmt.Sprintf("cd %s", worktreePath)))
}
return nil
Expand All @@ -309,6 +311,7 @@ func createWorktreeForExisting(gitClient git.GitClient, branchName, worktreePath
}
if !dryRun {
fmt.Println(ui.Success(fmt.Sprintf("Created worktree at %s (tracking origin/%s)", worktreePath, branchName)))
symlinkEnvFile(gitClient, worktreePath)
fmt.Printf("\nTo switch to this worktree, run:\n %s\n", ui.Command(fmt.Sprintf("cd %s", worktreePath)))
}
return nil
Expand All @@ -334,6 +337,7 @@ func createWorktreeForExisting(gitClient git.GitClient, branchName, worktreePath
if !dryRun {
fmt.Println(ui.Success(fmt.Sprintf("Created worktree at %s", worktreePath)))
fmt.Println(ui.Success(fmt.Sprintf("Branch %s with parent %s", ui.Branch(branchName), ui.Branch(currentBranch))))
symlinkEnvFile(gitClient, worktreePath)
fmt.Printf("\nTo switch to this worktree, run:\n %s\n", ui.Command(fmt.Sprintf("cd %s", worktreePath)))
}
return nil
Expand Down Expand Up @@ -512,3 +516,29 @@ func pathWithinDir(path, dir string) bool {
}
return rel != ".." && !strings.HasPrefix(rel, ".."+string(os.PathSeparator))
}

func symlinkEnvFile(gitClient git.GitClient, worktreePath string) {
repoRoot, err := gitClient.GetRepoRoot()
if err != nil {
return
}

envPath := filepath.Join(repoRoot, ".env")
if _, err := os.Stat(envPath); os.IsNotExist(err) {
return // No .env file, nothing to do
}

targetPath := filepath.Join(worktreePath, ".env")

if dryRun {
fmt.Printf("Would symlink .env from %s\n", envPath)
return
}

if err := os.Symlink(envPath, targetPath); err != nil {
// Silently ignore errors (e.g., symlink already exists)
return
}

fmt.Println(ui.Success("Symlinked .env file"))
}