aboutsummaryrefslogtreecommitdiff
path: root/obj_commit.go
diff options
context:
space:
mode:
Diffstat (limited to 'obj_commit.go')
-rw-r--r--obj_commit.go28
1 files changed, 14 insertions, 14 deletions
diff --git a/obj_commit.go b/obj_commit.go
index 84de2c41..f733a56f 100644
--- a/obj_commit.go
+++ b/obj_commit.go
@@ -7,10 +7,10 @@ import (
)
// Commit mirrors the structure of a Git commit object.
-type Commit struct {
- Hash Hash
- Tree Hash
- Parents []Hash
+type Commit[T HashType] struct {
+ Hash Hash[T]
+ Tree Hash[T]
+ Parents []Hash[T]
Author Ident
Committer Ident
Message []byte
@@ -18,12 +18,12 @@ type Commit struct {
}
// ObjType allows Commit to satisfy the Object interface.
-func (*Commit) ObjType() ObjType {
+func (*Commit[T]) ObjType() ObjType {
return ObjCommit
}
-func parseCommit(id Hash, body []byte, hashSize int) (*Commit, error) {
- c := new(Commit)
+func parseCommit[T HashType](id Hash[T], body []byte) (*Commit[T], error) {
+ c := new(Commit[T])
c.Hash = id
i := 0
for i < len(body) {
@@ -39,13 +39,13 @@ func parseCommit(id Hash, body []byte, hashSize int) (*Commit, error) {
switch {
case bytes.HasPrefix(line, []byte("tree ")):
- treeID, err := ParseHashWithSize(string(line[5:]), hashSize)
+ treeID, err := ParseHash[T](string(line[5:]))
if err != nil {
return nil, fmt.Errorf("furgit: commit: tree: %w", err)
}
c.Tree = treeID
case bytes.HasPrefix(line, []byte("parent ")):
- parent, err := ParseHashWithSize(string(line[7:]), hashSize)
+ parent, err := ParseHash[T](string(line[7:]))
if err != nil {
return nil, fmt.Errorf("furgit: commit: parent: %w", err)
}
@@ -91,11 +91,11 @@ func parseCommit(id Hash, body []byte, hashSize int) (*Commit, error) {
return c, nil
}
-func commitBody(c *Commit, hashSize int) []byte {
+func commitBody[T HashType](c *Commit[T]) []byte {
var buf bytes.Buffer
- fmt.Fprintf(&buf, "tree %s\n", c.Tree.StringWithSize(hashSize))
+ fmt.Fprintf(&buf, "tree %s\n", c.Tree.String())
for _, p := range c.Parents {
- fmt.Fprintf(&buf, "parent %s\n", p.StringWithSize(hashSize))
+ fmt.Fprintf(&buf, "parent %s\n", p.String())
}
buf.WriteString("author ")
buf.Write(c.Author.Serialize())
@@ -110,8 +110,8 @@ func commitBody(c *Commit, hashSize int) []byte {
}
// Serialize renders a Commit into canonical Git format.
-func (c *Commit) Serialize(hashSize int) ([]byte, error) {
- body := commitBody(c, hashSize)
+func (c *Commit[T]) Serialize() ([]byte, error) {
+ body := commitBody(c)
header, err := headerForType(ObjCommit, body)
if err != nil {
return nil, err