Compare commits
2 Commits
9866313b60
...
c90fd27d9b
| Author | SHA1 | Date | |
|---|---|---|---|
| c90fd27d9b | |||
| 5e43d76ef9 |
64
main.go
64
main.go
@@ -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,54 @@ 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 List(ctx context.Context, cmd *cli.Command) error {
|
||||
wtd, err := Getwtd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
project, err := os.Getwd()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dirs, err := os.ReadDir(filepath.Join(wtd, filepath.Base(project)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range dirs {
|
||||
fmt.Println(dirs[i].Name())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
cmd := cli.Command{
|
||||
Name: "wt",
|
||||
@@ -87,10 +145,12 @@ func main() {
|
||||
Name: "clone",
|
||||
},
|
||||
{
|
||||
Name: "list",
|
||||
Name: "list",
|
||||
Action: List,
|
||||
},
|
||||
{
|
||||
Name: "switch",
|
||||
Name: "switch",
|
||||
Action: Switch,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user