Skip to content

Commit

Permalink
add switch branch (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 7, 2023
1 parent 96bc77b commit 3b8006d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cmd/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"errors"
"fmt"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -35,6 +36,13 @@ var switchCmd = &cobra.Command{
return errors.New("invalid create option format")
}

// switch branch == update HEAD
if len(args) == 1 {
if err := client.Head.Update(client.Refs, client.RootGoitPath, args[0]); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}
}

return nil
},
}
Expand Down
8 changes: 7 additions & 1 deletion internal/store/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ func newHead() *Head {
return &Head{}
}

func (h *Head) Update(rootGoitPath, newRef string) error {
func (h *Head) Update(refs *Refs, rootGoitPath, newRef string) error {
// check if branch exists
n := refs.getBranchPos(newRef)
if n == NewBranchFlag {
return fmt.Errorf("branch %s does not exist", newRef)
}

headPath := filepath.Join(rootGoitPath, "HEAD")
if _, err := os.Stat(headPath); os.IsNotExist(err) {
return errors.New("fail to find HEAD, cannot update")
Expand Down
2 changes: 1 addition & 1 deletion internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (r *Refs) RenameBranch(head *Head, rootGoitPath, newBranchName string) erro
}

// update HEAD
if err := head.Update(rootGoitPath, newBranchName); err != nil {
if err := head.Update(r, rootGoitPath, newBranchName); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}

Expand Down

0 comments on commit 3b8006d

Please sign in to comment.