Skip to content

Commit

Permalink
refactoring list branch (#98)
Browse files Browse the repository at this point in the history
define ListBranches as Refs method.
  • Loading branch information
JunNishimura committed Jun 6, 2023
1 parent 03c3f2d commit d262ada
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
13 changes: 1 addition & 12 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path/filepath"

"github.com/JunNishimura/Goit/internal/store"
"github.com/fatih/color"
"github.com/spf13/cobra"
)

Expand All @@ -18,16 +17,6 @@ var (
deleteOption string = ""
)

func listBranch(client *store.Client) {
for _, branch := range client.Refs.Heads {
if branch.Name == client.Head.Reference {
color.Green("* %s", branch.Name)
} else {
fmt.Println(branch.Name)
}
}
}

func renameBranch(client *store.Client, newName string) error {
// check if new name is not used for other branches
for _, branch := range client.Refs.Heads {
Expand Down Expand Up @@ -119,7 +108,7 @@ var branchCmd = &cobra.Command{

// list branches
if isList {
listBranch(client)
client.Refs.ListBranches(client.Head.Reference)
}

// rename current branch
Expand Down
11 changes: 11 additions & 0 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"

"github.com/JunNishimura/Goit/internal/sha"
"github.com/fatih/color"
)

const (
Expand Down Expand Up @@ -80,6 +81,16 @@ func (r *Refs) GetBranch(name string) (*branch, error) {
return nil, fmt.Errorf("fail to find '%s' branch", name)
}

func (r *Refs) ListBranches(headBranchName string) {
for _, b := range r.Heads {
if b.Name == headBranchName {
color.Green("* %s", b.Name)
} else {
fmt.Println(b.Name)
}
}
}

func (r *Refs) AddBranch(rootGoitPath, newBranchName string, newBranchHash sha.SHA1) error {
// check if branch already exists
n := r.getBranchPos(newBranchName)
Expand Down

0 comments on commit d262ada

Please sign in to comment.