Skip to content

Commit

Permalink
fix bug (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 11, 2023
1 parent f5999e2 commit f46fef4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
14 changes: 11 additions & 3 deletions cmd/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,17 @@ func commit() error {
return fmt.Errorf("fail to write commit object: %w", err)
}

// update branch
if err := client.Refs.UpdateBranchHash(client.RootGoitPath, client.Head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to update branch %s: %w", client.Head.Reference, err)
// create/update branch
if client.Refs.IsBranchExist(client.Head.Reference) {
// update
if err := client.Refs.UpdateBranchHash(client.RootGoitPath, client.Head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to update branch %s: %w", client.Head.Reference, err)
}
} else {
// create
if err := client.Refs.AddBranch(client.RootGoitPath, client.Head.Reference, commit.Hash); err != nil {
return fmt.Errorf("fail to create branch %s: %w", client.Head.Reference, err)
}
}

return nil
Expand Down
9 changes: 9 additions & 0 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func (r *Refs) ListBranches(headBranchName string) {
}
}

func (r *Refs) IsBranchExist(branchName string) bool {
p := r.getBranchPos(branchName)
return p != NewBranchFlag
}

func (r *Refs) AddBranch(rootGoitPath, newBranchName string, newBranchHash sha.SHA1) error {
// check if branch already exists
n := r.getBranchPos(newBranchName)
Expand Down Expand Up @@ -153,6 +158,10 @@ func (r *Refs) RenameBranch(head *Head, rootGoitPath, newBranchName string) erro
// return the index of branch in the Refs Heads.
// if not found, return NewBranchFlag which is -1.
func (r *Refs) getBranchPos(branchName string) int {
if len(r.Heads) == 0 {
return NewBranchFlag
}

// binary search
left := 0
right := len(r.Heads)
Expand Down

0 comments on commit f46fef4

Please sign in to comment.