Skip to content

Commit

Permalink
delete head.Update from RenameBranch method (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 11, 2023
1 parent 50c6319 commit 07f12d9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
6 changes: 5 additions & 1 deletion cmd/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ var branchCmd = &cobra.Command{

// rename current branch
if renameOption != "" {
if err := client.Refs.RenameBranch(client.Head, client.RootGoitPath, renameOption); err != nil {
if err := client.Refs.RenameBranch(client.RootGoitPath, client.Head.Reference, renameOption); err != nil {
return fmt.Errorf("fail to rename branch: %w", err)
}
// update HEAD
if err := client.Head.Update(client.Refs, client.RootGoitPath, renameOption); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}
}

if deleteOption != "" {
Expand Down
13 changes: 4 additions & 9 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,34 +124,29 @@ func (r *Refs) AddBranch(rootGoitPath, newBranchName string, newBranchHash sha.S
return nil
}

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

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

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

// rename file
oldPath := filepath.Join(rootGoitPath, "refs", "heads", head.Reference)
oldPath := filepath.Join(rootGoitPath, "refs", "heads", curBranchName)
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(r, rootGoitPath, newBranchName); err != nil {
return fmt.Errorf("fail to update HEAD: %w", err)
}

return nil
}

Expand Down

0 comments on commit 07f12d9

Please sign in to comment.