feat: implement first version of switch

This commit is contained in:
2026-01-25 20:36:08 +01:00
parent 9866313b60
commit 5e43d76ef9

41
main.go
View File

@@ -32,6 +32,16 @@ func Getwtd() (string, error) {
return wtd, nil
}
func GetWorktree(cwd string, name string) (string, error) {
wtd, err := Getwtd()
if err != nil {
return "", err
}
project := filepath.Base(cwd)
return filepath.Join(wtd, project, name), nil
}
func Init(ctx context.Context, cmd *cli.Command) error {
fmt.Println("init")
cwd, err := os.Getwd()
@@ -75,6 +85,34 @@ func Init(ctx context.Context, cmd *cli.Command) error {
return nil
}
func Switch(ctx context.Context, cmd *cli.Command) error {
branch := strings.TrimSpace(cmd.Args().Get(0))
if len(branch) == 0 {
return fmt.Errorf("no worktree name given")
}
cwd, err := os.Getwd()
if err != nil {
return err
}
worktree, err := GetWorktree(cwd, branch)
if err != nil {
return err
}
if !exists(worktree) {
if err := exec.Command("git", "worktree", "add", worktree, branch).Run(); err != nil {
return err
}
}
if _, err := os.Lstat(cwd); err == nil {
if err := os.Remove(cwd); err != nil {
return err
}
}
return os.Symlink(worktree, cwd)
}
func main() {
cmd := cli.Command{
Name: "wt",
@@ -90,7 +128,8 @@ func main() {
Name: "list",
},
{
Name: "switch",
Name: "switch",
Action: Switch,
},
},
}