aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-05-24 11:07:16 +0000
committerGravatar Runxi Yu2026-05-24 11:09:59 +0000
commit36340918040627d93808c09dea8d9bd7b7457f82 (patch)
treedab5fa0edbf2f2bf315d1fda73c0f7ac5c8fc6d9
parentinternal/testgit: Add Algorithm method (diff)
signatureNo signature
object/id: Rename algorithm to object format
-rw-r--r--internal/testgit/object.go2
-rw-r--r--internal/testgit/repo.go18
-rw-r--r--object/commit/parse.go6
-rw-r--r--object/id/algorithm.go24
-rw-r--r--object/id/algorithm_details.go70
-rw-r--r--object/id/algorithm_ops.go41
-rw-r--r--object/id/algorithm_parse.go8
-rw-r--r--object/id/algorithm_test.go43
-rw-r--r--object/id/doc.go4
-rw-r--r--object/id/errors.go6
-rw-r--r--object/id/object_format.go25
-rw-r--r--object/id/object_format_details.go69
-rw-r--r--object/id/object_format_ops.go41
-rw-r--r--object/id/object_format_parse.go8
-rw-r--r--object/id/object_id.go4
-rw-r--r--object/id/object_id_ops.go12
-rw-r--r--object/id/object_id_parse.go28
-rw-r--r--object/id/object_id_test.go44
-rw-r--r--object/parse.go8
-rw-r--r--object/store/reader.go2
20 files changed, 210 insertions, 253 deletions
diff --git a/internal/testgit/object.go b/internal/testgit/object.go
index 566c8b82..fe4b329e 100644
--- a/internal/testgit/object.go
+++ b/internal/testgit/object.go
@@ -19,7 +19,7 @@ func (repo *Repo) HashObject(tb testing.TB, ty typ.Type, body io.Reader) id.Obje
tb.Fatalf("hash-object: %v", hex)
}
- id, err := id.FromHex(repo.algo, string(hex))
+ id, err := id.FromHex(repo.objectFormat, string(hex))
if err != nil {
tb.Fatalf("parse git hash-object output %q: %v", hex, err)
}
diff --git a/internal/testgit/repo.go b/internal/testgit/repo.go
index fcb6ae90..54330906 100644
--- a/internal/testgit/repo.go
+++ b/internal/testgit/repo.go
@@ -9,13 +9,13 @@ import (
)
type Repo struct {
- path string
- algo id.Algorithm
- env []string
+ path string
+ objectFormat id.ObjectFormat
+ env []string
}
-func (repo *Repo) Algorithm() id.Algorithm {
- return repo.algo
+func (repo *Repo) ObjectFormat() id.ObjectFormat {
+ return repo.objectFormat
}
func (repo *Repo) Command(
@@ -32,15 +32,15 @@ func (repo *Repo) Command(
}
type RepoOptions struct {
- ObjectFormat id.Algorithm
+ ObjectFormat id.ObjectFormat
}
func NewRepo(tb testing.TB, opts RepoOptions) (*Repo, error) {
tb.Helper()
repo := &Repo{
- path: tb.TempDir(),
- algo: opts.ObjectFormat,
+ path: tb.TempDir(),
+ objectFormat: opts.ObjectFormat,
env: append(os.Environ(),
"GIT_CONFIG_GLOBAL=/dev/null",
"GIT_CONFIG_SYSTEM=/dev/null",
@@ -53,5 +53,5 @@ func NewRepo(tb testing.TB, opts RepoOptions) (*Repo, error) {
),
}
- return repo, repo.Command(tb, "git", "init", "--object-format="+repo.algo.String(), "--", repo.path).Run()
+ return repo, repo.Command(tb, "git", "init", "--object-format="+repo.objectFormat.String(), "--", repo.path).Run()
}
diff --git a/object/commit/parse.go b/object/commit/parse.go
index 1198665b..e5b7a06e 100644
--- a/object/commit/parse.go
+++ b/object/commit/parse.go
@@ -13,7 +13,7 @@ import (
var ErrInvalidCommit = errors.New("object/commit: invalid commit")
// Parse decodes a commit object body.
-func Parse(body []byte, algo id.Algorithm) (*Commit, error) {
+func Parse(body []byte, objectFormat id.ObjectFormat) (*Commit, error) {
c := new(Commit)
i := 0
@@ -37,14 +37,14 @@ func Parse(body []byte, algo id.Algorithm) (*Commit, error) {
switch string(key) {
case "tree":
- id, err := id.FromHex(algo, string(value))
+ id, err := id.FromHex(objectFormat, string(value))
if err != nil {
return nil, fmt.Errorf("object: commit: tree: %w", err)
}
c.Tree = id
case "parent":
- id, err := id.FromHex(algo, string(value))
+ id, err := id.FromHex(objectFormat, string(value))
if err != nil {
return nil, fmt.Errorf("object: commit: parent: %w", err)
}
diff --git a/object/id/algorithm.go b/object/id/algorithm.go
deleted file mode 100644
index efe5f4c5..00000000
--- a/object/id/algorithm.go
+++ /dev/null
@@ -1,24 +0,0 @@
-package id
-
-// Algorithm identifies the hash algorithm used for Git object IDs.
-type Algorithm uint8
-
-const (
- // AlgorithmUnknown identifies an unknown object ID hash algorithm.
- AlgorithmUnknown Algorithm = iota
-
- // AlgorithmSHA1 identifies the SHA-1 object ID hash algorithm.
- // This is the default for all versions of Git until Git 3.0.
- AlgorithmSHA1
-
- // AlgorithmSHA256 identifies the SHA-256 object ID hash algorithm.
- // This is the default for Git 3.0 and beyond.
- AlgorithmSHA256
-)
-
-// SupportedAlgorithms returns all object ID algorithms supported by furgit.
-//
-// Labels: Mut-No.
-func SupportedAlgorithms() []Algorithm {
- return supportedAlgorithms
-}
diff --git a/object/id/algorithm_details.go b/object/id/algorithm_details.go
deleted file mode 100644
index 5f782be7..00000000
--- a/object/id/algorithm_details.go
+++ /dev/null
@@ -1,70 +0,0 @@
-package id
-
-import (
- "crypto/sha1" //#nosec:G505
- "crypto/sha256"
- "hash"
-)
-
-type algorithmDetails struct {
- name string
- size int
- sum func([]byte) ObjectID
- new func() hash.Hash
-}
-
-func (algo Algorithm) details() algorithmDetails {
- return algorithmTable[algo]
-}
-
-//nolint:gochecknoglobals
-var algorithmTable = [...]algorithmDetails{
- AlgorithmUnknown: {}, //nolint:exhaustruct
- AlgorithmSHA1: {
- name: "sha1",
- size: sha1.Size,
- sum: func(data []byte) ObjectID {
- sum := sha1.Sum(data) //#nosec G401
-
- var id ObjectID
- copy(id.data[:], sum[:])
- id.algo = AlgorithmSHA1
-
- return id
- },
- new: sha1.New,
- },
- AlgorithmSHA256: {
- name: "sha256",
- size: sha256.Size,
- sum: func(data []byte) ObjectID {
- sum := sha256.Sum256(data)
-
- var id ObjectID
- copy(id.data[:], sum[:])
- id.algo = AlgorithmSHA256
-
- return id
- },
- new: sha256.New,
- },
-}
-
-// maxObjectIDSize MUST be >= the largest supported algorithm size.
-const maxObjectIDSize = sha256.Size
-
-var (
- //nolint:gochecknoglobals
- algorithmByName = map[string]Algorithm{}
- //nolint:gochecknoglobals
- supportedAlgorithms []Algorithm
-)
-
-func init() { //nolint:gochecknoinits
- // Skip over AlgorithmUnknown.
- for algo := Algorithm(1); int(algo) < len(algorithmTable); algo++ {
- info := &algorithmTable[algo]
- algorithmByName[info.name] = algo
- supportedAlgorithms = append(supportedAlgorithms, algo)
- }
-}
diff --git a/object/id/algorithm_ops.go b/object/id/algorithm_ops.go
deleted file mode 100644
index 80f1f9cd..00000000
--- a/object/id/algorithm_ops.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package id
-
-import "hash"
-
-// HexLen returns the encoded hexadecimal length.
-func (algo Algorithm) HexLen() int {
- return algo.Size() * 2
-}
-
-// Size returns the hash size in bytes.
-func (algo Algorithm) Size() int {
- return algo.details().size
-}
-
-// New returns a new hash.Hash for this algorithm.
-func (algo Algorithm) New() (hash.Hash, error) {
- newFn := algo.details().new
- if newFn == nil {
- return nil, ErrInvalidAlgorithm
- }
-
- return newFn(), nil
-}
-
-// String returns the canonical algorithm name.
-func (algo Algorithm) String() string {
- return algo.details().name
-}
-
-// Sum computes an object ID from raw data using the selected algorithm.
-func (algo Algorithm) Sum(data []byte) ObjectID {
- return algo.details().sum(data)
-}
-
-// Zero returns the all-zero object ID for this algorithm.
-func (algo Algorithm) Zero() ObjectID {
- return ObjectID{
- algo: algo,
- data: [maxObjectIDSize]byte{},
- }
-}
diff --git a/object/id/algorithm_parse.go b/object/id/algorithm_parse.go
deleted file mode 100644
index 7349288f..00000000
--- a/object/id/algorithm_parse.go
+++ /dev/null
@@ -1,8 +0,0 @@
-package id
-
-// ParseAlgorithm parses a canonical algorithm name (e.g. "sha1", "sha256").
-func ParseAlgorithm(s string) (Algorithm, bool) {
- algo, ok := algorithmByName[s]
-
- return algo, ok
-}
diff --git a/object/id/algorithm_test.go b/object/id/algorithm_test.go
deleted file mode 100644
index a596236a..00000000
--- a/object/id/algorithm_test.go
+++ /dev/null
@@ -1,43 +0,0 @@
-package id_test
-
-import (
- "testing"
-
- "codeberg.org/lindenii/furgit/object/id"
-)
-
-func TestParseAlgorithm(t *testing.T) {
- t.Parallel()
-
- algo, ok := id.ParseAlgorithm("sha1")
- if !ok || algo != id.AlgorithmSHA1 {
- t.Fatalf("ParseAlgorithm(sha1) = (%v,%v)", algo, ok)
- }
-
- algo, ok = id.ParseAlgorithm("sha256")
- if !ok || algo != id.AlgorithmSHA256 {
- t.Fatalf("ParseAlgorithm(sha256) = (%v,%v)", algo, ok)
- }
-
- if _, ok := id.ParseAlgorithm("md5"); ok {
- t.Fatalf("ParseAlgorithm(md5) should fail")
- }
-}
-
-func TestAlgorithmSum(t *testing.T) {
- t.Parallel()
-
- id1 := id.AlgorithmSHA1.Sum([]byte("hello"))
- if id1.Algorithm() != id.AlgorithmSHA1 || id1.Algorithm().Size() != id.AlgorithmSHA1.Size() {
- t.Fatalf("sha1 sum produced invalid object id")
- }
-
- id2 := id.AlgorithmSHA256.Sum([]byte("hello"))
- if id2.Algorithm() != id.AlgorithmSHA256 || id2.Algorithm().Size() != id.AlgorithmSHA256.Size() {
- t.Fatalf("sha256 sum produced invalid object id")
- }
-
- if id1.String() == id2.String() {
- t.Fatalf("sha1 and sha256 should differ")
- }
-}
diff --git a/object/id/doc.go b/object/id/doc.go
index 66c48ac7..f8506f11 100644
--- a/object/id/doc.go
+++ b/object/id/doc.go
@@ -1,7 +1,7 @@
-// Package id provides Git object IDs and hash algorithms.
+// Package id provides Git object IDs and object formats.
//
// Not all properties about
-// each object ID hash algorithm
+// each object format (hash algorithm)
// are listed here.
// In particular,
// properties that only make sense for particular subsystems
diff --git a/object/id/errors.go b/object/id/errors.go
index 5f9a80af..ec8a6f67 100644
--- a/object/id/errors.go
+++ b/object/id/errors.go
@@ -3,9 +3,9 @@ package id
import "errors"
var (
- // ErrInvalidAlgorithm indicates an unsupported object ID algorithm.
- ErrInvalidAlgorithm = errors.New("objectid: invalid algorithm")
+ // ErrInvalidObjectFormat indicates an unsupported object format.
+ ErrInvalidObjectFormat = errors.New("object/id: invalid object format")
// ErrInvalidObjectID indicates malformed object ID data.
- ErrInvalidObjectID = errors.New("objectid: invalid object id")
+ ErrInvalidObjectID = errors.New("object/id: invalid object id")
)
diff --git a/object/id/object_format.go b/object/id/object_format.go
new file mode 100644
index 00000000..c21e8fa7
--- /dev/null
+++ b/object/id/object_format.go
@@ -0,0 +1,25 @@
+package id
+
+// ObjectFormat identifies the Git object format,
+// i.e., the hash algorithm used in git object IDs.
+type ObjectFormat uint8
+
+const (
+ // ObjectFormatUnknown identifies an unknown object format.
+ ObjectFormatUnknown ObjectFormat = iota
+
+ // ObjectFormatSHA1 identifies the SHA-1 object format.
+ // This is the default for all versions of Git until Git 3.0.
+ ObjectFormatSHA1
+
+ // ObjectFormatSHA256 identifies the SHA-256 object format.
+ // This is the default for Git 3.0 and beyond.
+ ObjectFormatSHA256
+)
+
+// SupportedObjectFormats returns all object formats supported by furgit.
+//
+// Labels: Mut-No.
+func SupportedObjectFormats() []ObjectFormat {
+ return supportedObjectFormats
+}
diff --git a/object/id/object_format_details.go b/object/id/object_format_details.go
new file mode 100644
index 00000000..d0278602
--- /dev/null
+++ b/object/id/object_format_details.go
@@ -0,0 +1,69 @@
+package id
+
+import (
+ "crypto/sha1" //#nosec:G505
+ "crypto/sha256"
+ "hash"
+)
+
+type objectFormatDetails struct {
+ name string
+ size int
+ sum func([]byte) ObjectID
+ new func() hash.Hash
+}
+
+func (objectFormat ObjectFormat) details() objectFormatDetails {
+ return objectFormatTable[objectFormat]
+}
+
+//nolint:gochecknoglobals
+var objectFormatTable = [...]objectFormatDetails{
+ ObjectFormatUnknown: {}, //nolint:exhaustruct
+ ObjectFormatSHA1: {
+ name: "sha1",
+ size: sha1.Size,
+ sum: func(data []byte) ObjectID {
+ sum := sha1.Sum(data) //#nosec G401
+
+ var id ObjectID
+ copy(id.data[:], sum[:])
+ id.objectFormat = ObjectFormatSHA1
+
+ return id
+ },
+ new: sha1.New,
+ },
+ ObjectFormatSHA256: {
+ name: "sha256",
+ size: sha256.Size,
+ sum: func(data []byte) ObjectID {
+ sum := sha256.Sum256(data)
+
+ var id ObjectID
+ copy(id.data[:], sum[:])
+ id.objectFormat = ObjectFormatSHA256
+
+ return id
+ },
+ new: sha256.New,
+ },
+}
+
+// maxObjectIDSize MUST be >= the largest supported object format size.
+const maxObjectIDSize = sha256.Size
+
+var (
+ //nolint:gochecknoglobals
+ objectFormatByName = map[string]ObjectFormat{}
+ //nolint:gochecknoglobals
+ supportedObjectFormats []ObjectFormat
+)
+
+func init() { //nolint:gochecknoinits
+ for objectFormat := ObjectFormatUnknown + 1; int(objectFormat) < len(objectFormatTable); objectFormat++ {
+ info := &objectFormatTable[objectFormat]
+ objectFormatByName[info.name] = objectFormat
+ supportedObjectFormats = append(supportedObjectFormats, objectFormat)
+ }
+}
diff --git a/object/id/object_format_ops.go b/object/id/object_format_ops.go
new file mode 100644
index 00000000..76077235
--- /dev/null
+++ b/object/id/object_format_ops.go
@@ -0,0 +1,41 @@
+package id
+
+import "hash"
+
+// HexLen returns the encoded hexadecimal length.
+func (objectFormat ObjectFormat) HexLen() int {
+ return objectFormat.Size() * 2
+}
+
+// Size returns the hash size in bytes.
+func (objectFormat ObjectFormat) Size() int {
+ return objectFormat.details().size
+}
+
+// New returns a new hash.Hash for this object format.
+func (objectFormat ObjectFormat) New() (hash.Hash, error) {
+ newFn := objectFormat.details().new
+ if newFn == nil {
+ return nil, ErrInvalidObjectFormat
+ }
+
+ return newFn(), nil
+}
+
+// String returns the canonical object format name.
+func (objectFormat ObjectFormat) String() string {
+ return objectFormat.details().name
+}
+
+// Sum computes an object ID from raw data using the selected object format.
+func (objectFormat ObjectFormat) Sum(data []byte) ObjectID {
+ return objectFormat.details().sum(data)
+}
+
+// Zero returns the all-zero object ID for this object format.
+func (objectFormat ObjectFormat) Zero() ObjectID {
+ return ObjectID{
+ objectFormat: objectFormat,
+ data: [maxObjectIDSize]byte{},
+ }
+}
diff --git a/object/id/object_format_parse.go b/object/id/object_format_parse.go
new file mode 100644
index 00000000..9e7401c7
--- /dev/null
+++ b/object/id/object_format_parse.go
@@ -0,0 +1,8 @@
+package id
+
+// ParseObjectFormat parses a canonical object format name (e.g. "sha1", "sha256").
+func ParseObjectFormat(s string) (ObjectFormat, bool) {
+ objectFormat, ok := objectFormatByName[s]
+
+ return objectFormat, ok
+}
diff --git a/object/id/object_id.go b/object/id/object_id.go
index 4cf562fa..926142ef 100644
--- a/object/id/object_id.go
+++ b/object/id/object_id.go
@@ -4,6 +4,6 @@ package id
//
//nolint:recvcheck
type ObjectID struct {
- algo Algorithm
- data [maxObjectIDSize]byte
+ objectFormat ObjectFormat
+ data [maxObjectIDSize]byte
}
diff --git a/object/id/object_id_ops.go b/object/id/object_id_ops.go
index 1ca0c6df..b97cb19a 100644
--- a/object/id/object_id_ops.go
+++ b/object/id/object_id_ops.go
@@ -5,14 +5,14 @@ import (
"encoding/hex"
)
-// Algorithm returns the object ID's hash algorithm.
-func (id ObjectID) Algorithm() Algorithm {
- return id.algo
+// ObjectFormat returns the object ID's object format (hash algorithm).
+func (id ObjectID) ObjectFormat() ObjectFormat {
+ return id.objectFormat
}
// Bytes returns a copy of the object ID bytes.
func (id ObjectID) Bytes() []byte {
- size := id.Algorithm().Size()
+ size := id.ObjectFormat().Size()
return append([]byte(nil), id.data[:size]...)
}
@@ -23,7 +23,7 @@ func (id ObjectID) Bytes() []byte {
//
// Labels: Mut-No.
func (id *ObjectID) RawBytes() []byte {
- size := id.Algorithm().Size()
+ size := id.ObjectFormat().Size()
return id.data[:size:size]
}
@@ -36,7 +36,7 @@ func (id ObjectID) Compare(other ObjectID) int {
// String returns the canonical hex representation.
func (id ObjectID) String() string {
- size := id.Algorithm().Size()
+ size := id.ObjectFormat().Size()
return hex.EncodeToString(id.data[:size])
}
diff --git a/object/id/object_id_parse.go b/object/id/object_id_parse.go
index b8e7da68..59b6669c 100644
--- a/object/id/object_id_parse.go
+++ b/object/id/object_id_parse.go
@@ -5,36 +5,36 @@ import (
"fmt"
)
-// FromBytes builds an object ID from raw bytes for the specified algorithm.
-func FromBytes(algo Algorithm, b []byte) (ObjectID, error) {
+// FromBytes builds an object ID from raw bytes for the specified object format.
+func FromBytes(objectFormat ObjectFormat, b []byte) (ObjectID, error) {
var id ObjectID
- if algo.Size() == 0 {
- return id, ErrInvalidAlgorithm
+ if objectFormat.Size() == 0 {
+ return id, ErrInvalidObjectFormat
}
- if len(b) != algo.Size() {
- return id, fmt.Errorf("%w: got %d bytes, expected %d", ErrInvalidObjectID, len(b), algo.Size())
+ if len(b) != objectFormat.Size() {
+ return id, fmt.Errorf("%w: got %d bytes, expected %d", ErrInvalidObjectID, len(b), objectFormat.Size())
}
copy(id.data[:], b)
- id.algo = algo
+ id.objectFormat = objectFormat
return id, nil
}
-// FromHex parses an object ID from hex for the specified algorithm.
-func FromHex(algo Algorithm, s string) (ObjectID, error) {
+// FromHex parses an object ID from hex for the specified object format.
+func FromHex(objectFormat ObjectFormat, s string) (ObjectID, error) {
var id ObjectID
- if algo.Size() == 0 {
- return id, ErrInvalidAlgorithm
+ if objectFormat.Size() == 0 {
+ return id, ErrInvalidObjectFormat
}
if len(s)%2 != 0 {
return id, fmt.Errorf("%w: odd hex length %d", ErrInvalidObjectID, len(s))
}
- if len(s) != algo.HexLen() {
- return id, fmt.Errorf("%w: got %d chars, expected %d", ErrInvalidObjectID, len(s), algo.HexLen())
+ if len(s) != objectFormat.HexLen() {
+ return id, fmt.Errorf("%w: got %d chars, expected %d", ErrInvalidObjectID, len(s), objectFormat.HexLen())
}
decoded, err := hex.DecodeString(s)
@@ -43,7 +43,7 @@ func FromHex(algo Algorithm, s string) (ObjectID, error) {
}
copy(id.data[:], decoded)
- id.algo = algo
+ id.objectFormat = objectFormat
return id, nil
}
diff --git a/object/id/object_id_test.go b/object/id/object_id_test.go
index ba9413ba..c3c22176 100644
--- a/object/id/object_id_test.go
+++ b/object/id/object_id_test.go
@@ -11,13 +11,13 @@ import (
func TestFromBytesErrors(t *testing.T) {
t.Parallel()
- _, err := id.FromBytes(id.AlgorithmUnknown, []byte{1, 2})
+ _, err := id.FromBytes(id.ObjectFormatUnknown, []byte{1, 2})
if err == nil {
- t.Fatalf("expected FromBytes unknown algo error")
+ t.Fatalf("expected FromBytes unknown object format error")
}
- for _, algo := range id.SupportedAlgorithms() {
- _, err = id.FromBytes(algo, []byte{1, 2})
+ for _, objectFormat := range id.SupportedObjectFormats() {
+ _, err = id.FromBytes(objectFormat, []byte{1, 2})
if err == nil {
t.Fatalf("expected FromBytes wrong size error")
}
@@ -27,8 +27,8 @@ func TestFromBytesErrors(t *testing.T) {
func TestBytesReturnsCopy(t *testing.T) {
t.Parallel()
- for _, algo := range id.SupportedAlgorithms() {
- id, err := id.FromHex(algo, strings.Repeat("01", algo.Size()))
+ for _, objectFormat := range id.SupportedObjectFormats() {
+ id, err := id.FromHex(objectFormat, strings.Repeat("01", objectFormat.Size()))
if err != nil {
t.Fatalf("ParseHex failed: %v", err)
}
@@ -50,31 +50,31 @@ func TestBytesReturnsCopy(t *testing.T) {
func TestFromHexErrors(t *testing.T) {
t.Parallel()
- t.Run("unknown algo", func(t *testing.T) {
+ t.Run("unknown object format", func(t *testing.T) {
t.Parallel()
- _, err := id.FromHex(id.AlgorithmUnknown, "00")
+ _, err := id.FromHex(id.ObjectFormatUnknown, "00")
if err == nil {
t.Fatalf("expected FromHex error")
}
})
// TODO: This may need to be revisited when hash-function-transition is implemented.
- for _, algo := range id.SupportedAlgorithms() {
- t.Run(algo.String(), func(t *testing.T) {
+ for _, objectFormat := range id.SupportedObjectFormats() {
+ t.Run(objectFormat.String(), func(t *testing.T) {
t.Parallel()
- _, err := id.FromHex(algo, strings.Repeat("0", algo.HexLen()-1))
+ _, err := id.FromHex(objectFormat, strings.Repeat("0", objectFormat.HexLen()-1))
if err == nil {
t.Fatalf("expected FromHex odd-len error")
}
- _, err = id.FromHex(algo, strings.Repeat("0", algo.HexLen()-2))
+ _, err = id.FromHex(objectFormat, strings.Repeat("0", objectFormat.HexLen()-2))
if err == nil {
t.Fatalf("expected FromHex wrong-len error")
}
- _, err = id.FromHex(algo, "z"+strings.Repeat("0", algo.HexLen()-1))
+ _, err = id.FromHex(objectFormat, "z"+strings.Repeat("0", objectFormat.HexLen()-1))
if err == nil {
t.Fatalf("expected FromHex invalid-hex error")
}
@@ -85,13 +85,13 @@ func TestFromHexErrors(t *testing.T) {
func TestFromHexRoundtrip(t *testing.T) {
t.Parallel()
- for _, algo := range id.SupportedAlgorithms() {
- t.Run(algo.String(), func(t *testing.T) {
+ for _, objectFormat := range id.SupportedObjectFormats() {
+ t.Run(objectFormat.String(), func(t *testing.T) {
t.Parallel()
- hex := strings.Repeat("01", algo.Size())
+ hex := strings.Repeat("01", objectFormat.Size())
- i, err := id.FromHex(algo, hex)
+ i, err := id.FromHex(objectFormat, hex)
if err != nil {
t.Fatalf("FromHex failed: %v", err)
}
@@ -100,16 +100,16 @@ func TestFromHexRoundtrip(t *testing.T) {
t.Fatalf("String() = %q, want %q", got, hex)
}
- if got := i.Algorithm().Size(); got != algo.Size() {
- t.Fatalf("Size() = %d, want %d", got, algo.Size())
+ if got := i.ObjectFormat().Size(); got != objectFormat.Size() {
+ t.Fatalf("Size() = %d, want %d", got, objectFormat.Size())
}
raw := i.Bytes()
- if len(raw) != algo.Size() {
- t.Fatalf("Bytes len = %d, want %d", len(raw), algo.Size())
+ if len(raw) != objectFormat.Size() {
+ t.Fatalf("Bytes len = %d, want %d", len(raw), objectFormat.Size())
}
- id2, err := id.FromBytes(algo, raw)
+ id2, err := id.FromBytes(objectFormat, raw)
if err != nil {
t.Fatalf("FromBytes failed: %v", err)
}
diff --git a/object/parse.go b/object/parse.go
index 6fbaa9b9..bdb4b39e 100644
--- a/object/parse.go
+++ b/object/parse.go
@@ -29,7 +29,7 @@ func (sizeMismatchError SizeMismatchError) Error() string {
// in "type size\x00body" format.
//
//nolint:ireturn
-func ParseWithHeader(raw []byte, algo id.Algorithm) (Object, error) {
+func ParseWithHeader(raw []byte, objectFormat id.ObjectFormat) (Object, error) {
ty, size, headerLen, err := header.Parse(raw)
if err != nil {
return nil, err //nolint:wrapcheck
@@ -40,20 +40,20 @@ func ParseWithHeader(raw []byte, algo id.Algorithm) (Object, error) {
return nil, SizeMismatchError{Expected: size, Got: len(body)}
}
- return ParseWithoutHeader(ty, body, algo)
+ return ParseWithoutHeader(ty, body, objectFormat)
}
// ParseWithoutHeader parses a typed object body.
//
//nolint:ireturn
-func ParseWithoutHeader(ty typ.Type, body []byte, algo id.Algorithm) (Object, error) {
+func ParseWithoutHeader(ty typ.Type, body []byte, objectFormat id.ObjectFormat) (Object, error) {
switch ty {
case typ.TypeBlob:
return blob.Parse(body) //nolint:wrapcheck
case typ.TypeTree:
panic("TODO")
case typ.TypeCommit:
- return commit.Parse(body, algo) //nolint:wrapcheck
+ return commit.Parse(body, objectFormat) //nolint:wrapcheck
case typ.TypeTag:
panic("TODO")
default:
diff --git a/object/store/reader.go b/object/store/reader.go
index f26354e3..b4df5081 100644
--- a/object/store/reader.go
+++ b/object/store/reader.go
@@ -18,7 +18,7 @@ type ObjectReader interface {
// ReadBytesFull reads a full serialized object as "type size\0content".
//
// In a valid repository,
- // hashing this payload with the same algorithm
+ // hashing this payload with the same object format
// yields the requested object ID.
// Users should treat this as an invariant;
// implementations should not re-verify it on every read.