Skip to content

Commit

Permalink
add reset method for head (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 17, 2023
1 parent 53a61fd commit 814716e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions internal/store/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,29 @@ func (h *Head) Update(refs *Refs, rootGoitPath, newRef string) error {

return nil
}

// reset Head to the specified state by hash
// This method does not change Head.Reference, just change Commit
func (h *Head) Reset(rootGoitPath string, refs *Refs, hash sha.SHA1) error {
// write branch hash
if err := refs.UpdateBranchHash(rootGoitPath, h.Reference, hash); err != nil {
return fmt.Errorf("fail to update branch hash: %w", err)
}

// get commit object
commitObject, err := object.GetObject(rootGoitPath, hash)
if err != nil {
return fmt.Errorf("fail to get commit object: %w", err)
}

// get commit
commit, err := object.NewCommit(commitObject)
if err != nil {
return fmt.Errorf("fail to get commit: %w", err)
}

// update commit
h.Commit = commit

return nil
}

0 comments on commit 814716e

Please sign in to comment.