Skip to content

Commit

Permalink
add Update Method for Head (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 4, 2023
1 parent e015550 commit e35cc26
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/store/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,34 @@ func NewHead(rootGoitPath string) (*Head, error) {
func newHead() *Head {
return &Head{}
}

func (h *Head) Update(rootGoitPath, newRef string) error {
headPath := filepath.Join(rootGoitPath, "HEAD")
if _, err := os.Stat(headPath); os.IsNotExist(err) {
return errors.New("fail to find HEAD, cannot update")
}
f, err := os.Create(headPath)
if err != nil {
return fmt.Errorf("fail to create HEAD: %w", err)
}
defer f.Close()

if _, err := f.WriteString(fmt.Sprintf("ref: refs/heads/%s", newRef)); err != nil {
return fmt.Errorf("fail to write HEAD: %w", err)
}

h.Reference = newRef

// get commit from branch
branchPath := filepath.Join(rootGoitPath, "refs", "heads", newRef)
if _, err := os.Stat(branchPath); os.IsNotExist(err) {
return fmt.Errorf("fail to find branch %s: %w", newRef, err)
}
commit, err := getHeadCommit(newRef, rootGoitPath)
if err != nil {
return ErrInvalidHead
}
h.Commit = commit

return nil
}

0 comments on commit e35cc26

Please sign in to comment.