Skip to content

Commit

Permalink
refactoring rename branch (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 6, 2023
1 parent 10801d1 commit 3103b81
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 45 deletions.
34 changes: 1 addition & 33 deletions cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/JunNishimura/Goit/internal/store"
"github.com/spf13/cobra"
Expand All @@ -17,36 +15,6 @@ var (
deleteOption string = ""
)

func renameBranch(client *store.Client, newName string) error {
// check if new name is not used for other branches
for _, branch := range client.Refs.Heads {
if branch.Name == newName {
return fmt.Errorf("fatal: branch named '%s' already exists", newName)
}
}

// rename current branch
branch, err := client.Refs.GetBranch(client.Head.Reference)
if err != nil {
return err
}
branch.Name = newName

// rename file
oldPath := filepath.Join(client.RootGoitPath, "refs", "heads", client.Head.Reference)
newPath := filepath.Join(client.RootGoitPath, "refs", "heads", newName)
if err := os.Rename(oldPath, newPath); err != nil {
return fmt.Errorf("fail to rename: %w", err)
}

// rename HEAD
if err := client.Head.Update(client.RootGoitPath, newName); err != nil {
return err
}

return nil
}

func deleteBranch(client *store.Client, branchName string) error {
// branch validation
if branchName == client.Head.Reference {
Expand Down Expand Up @@ -113,7 +81,7 @@ var branchCmd = &cobra.Command{

// rename current branch
if renameOption != "" {
if err := renameBranch(client, renameOption); err != nil {
if err := client.Refs.RenameBranch(client.Head, client.RootGoitPath, renameOption); err != nil {
return err
}
}
Expand Down
62 changes: 50 additions & 12 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,6 @@ func newRefs() *Refs {
}
}

func (r *Refs) GetBranch(name string) (*branch, error) {
for _, b := range r.Heads {
if b.Name == name {
return b, nil
}
}
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 {
Expand Down Expand Up @@ -119,14 +110,61 @@ func (r *Refs) AddBranch(rootGoitPath, newBranchName string, newBranchHash sha.S
return nil
}

func (r *Refs) RenameBranch(head *Head, rootGoitPath, newBranchName string) error {
// check if new branch name is not used for other branches
n := r.getBranchPos(newBranchName)
if n != NewBranchFlag {
return fmt.Errorf("fatal: branch named '%s' already exists", newBranchName)
}

// get current branch
curNum := r.getBranchPos(head.Reference)
if n == NewBranchFlag {
return fmt.Errorf("head branch '%s' does not exist", head.Reference)
}

// rename branch
r.Heads[curNum].Name = newBranchName

// rename file
oldPath := filepath.Join(rootGoitPath, "refs", "heads", head.Reference)
newPath := filepath.Join(rootGoitPath, "refs", "heads", newBranchName)
if err := os.Rename(oldPath, newPath); err != nil {
return fmt.Errorf("fail to rename file: %w", err)
}

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

return nil
}

// return the index of branch in the Refs Heads.
// if not found, return NewBranchFlag which is -1.
func (r *Refs) getBranchPos(branchName string) int {
for n, branch := range r.Heads {
if branch.Name == branchName {
return n
// binary search
left := 0
right := len(r.Heads)
for {
middle := (left + right) / 2
b := r.Heads[middle]
if b.Name == branchName {
return middle
}
if b.Name < branchName {
left = middle + 1
}
if b.Name > branchName {
right = middle
}

if right-left < 1 {
break
}
}

return NewBranchFlag
}

Expand Down

0 comments on commit 3103b81

Please sign in to comment.