Skip to content

Commit

Permalink
add refs (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Jun 4, 2023
1 parent bcd7c53 commit 92870eb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/store/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ type Client struct {
Conf *Config
Idx *Index
Head *Head
Refs *Refs
RootGoitPath string
}

func NewClient(config *Config, index *Index, head *Head, rootGoitPath string) *Client {
func NewClient(config *Config, index *Index, head *Head, refs *Refs, rootGoitPath string) *Client {
return &Client{
Conf: config,
Idx: index,
Head: head,
Refs: refs,
RootGoitPath: rootGoitPath,
}
}
57 changes: 57 additions & 0 deletions internal/store/refs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package store

import (
"os"
"path/filepath"

"github.com/JunNishimura/Goit/internal/sha"
)

type branch struct {
Name string
hash sha.SHA1
}

type Refs struct {
Heads []*branch
}

func NewRefs(rootGoitPath string) (*Refs, error) {
r := newRefs()
headsPath := filepath.Join(rootGoitPath, "refs", "heads")
files, err := os.ReadDir(headsPath)
if err != nil {
return nil, err
}
for _, file := range files {
b, err := newBranch(rootGoitPath, file.Name())
if err != nil {
return nil, err
}
r.Heads = append(r.Heads, b)
}
return r, nil
}

func newRefs() *Refs {
return &Refs{
Heads: make([]*branch, 0),
}
}

func newBranch(rootGoitPath, branchName string) (*branch, error) {
branchPath := filepath.Join(rootGoitPath, "refs", "heads", branchName)
hashByte, err := os.ReadFile(branchPath)
if err != nil {
return nil, err
}
hashString := string(hashByte)
hash, err := sha.ReadHash(hashString)
if err != nil {
return nil, err
}
return &branch{
Name: branchName,
hash: hash,
}, nil
}

0 comments on commit 92870eb

Please sign in to comment.