Skip to content

Commit

Permalink
refactoring delete branch (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 6, 2023
1 parent 3103b81 commit 591a16a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 35 deletions.
30 changes: 3 additions & 27 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package cmd
import (
"fmt"

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

Expand All @@ -15,29 +14,6 @@ var (
deleteOption string = ""
)

func deleteBranch(client *store.Client, branchName string) error {
// branch validation
if branchName == client.Head.Reference {
return fmt.Errorf("error: cannot delete current branch '%s'", client.Head.Reference)
}
isBranchFound := false
for _, branch := range client.Refs.Heads {
if branch.Name == branchName {
isBranchFound = true
}
}
if !isBranchFound {
return fmt.Errorf("error: branch '%s' not found", branchName)
}

// delete branch
if err := client.Refs.DeleteBranch(client.RootGoitPath, branchName); err != nil {
return err
}

return nil
}

// branchCmd represents the branch command
var branchCmd = &cobra.Command{
Use: "branch",
Expand Down Expand Up @@ -82,13 +58,13 @@ var branchCmd = &cobra.Command{
// rename current branch
if renameOption != "" {
if err := client.Refs.RenameBranch(client.Head, client.RootGoitPath, renameOption); err != nil {
return err
return fmt.Errorf("fail to rename branch: %w", err)
}
}

if deleteOption != "" {
if err := deleteBranch(client, deleteOption); err != nil {
return err
if err := client.Refs.DeleteBranch(client.RootGoitPath, client.Head.Reference, deleteOption); err != nil {
return fmt.Errorf("fail to delete branch: %w", err)
}
}

Expand Down
21 changes: 13 additions & 8 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,18 +168,23 @@ func (r *Refs) getBranchPos(branchName string) int {
return NewBranchFlag
}

func (r *Refs) DeleteBranch(rootGoitPath, name string) error {
// delete branch from Refs
p := r.getBranchPos(name)
if p == NewBranchFlag {
return fmt.Errorf("branch '%s' not found", name)
func (r *Refs) DeleteBranch(rootGoitPath, headBranchName, deleteBranchName string) error {
// branch validation
if deleteBranchName == headBranchName {
return fmt.Errorf("error: cannot delete current branch '%s'", headBranchName)
}
n := r.getBranchPos(deleteBranchName)
if n == NewBranchFlag {
return fmt.Errorf("error: branch '%s' not found", deleteBranchName)
}
r.Heads = append(r.Heads[:p], r.Heads[p+1:]...)

// delete from refs
r.Heads = append(r.Heads[:n], r.Heads[n+1:]...)

// delete branch file
branchPath := filepath.Join(rootGoitPath, "refs", "heads", name)
branchPath := filepath.Join(rootGoitPath, "refs", "heads", deleteBranchName)
if err := os.Remove(branchPath); err != nil {
return err
return fmt.Errorf("fail to delete branch file: %w", err)
}

return nil
Expand Down

0 comments on commit 591a16a

Please sign in to comment.