shithub: furgit

Download patch

ref: 4905605124ab0ea390cdd65963d5a6a1a5258c45
parent: 2da7bba85f89289dd545cf967fd27efa4235c36e
author: Runxi Yu <me@runxiyu.org>
date: Wed Mar 4 04:23:23 EST 2026

objectid: File splitting

--- /dev/null
+++ b/objectid/algorithms.go
@@ -1,0 +1,127 @@
+package objectid
+
+import (
+	"crypto/sha1" //#nosec gosec
+	"crypto/sha256"
+	"hash"
+)
+
+// maxObjectIDSize MUST be >= the largest supported algorithm size.
+const maxObjectIDSize = sha256.Size
+
+// Algorithm identifies the hash algorithm used for Git object IDs.
+type Algorithm uint8
+
+const (
+	AlgorithmUnknown Algorithm = iota
+	AlgorithmSHA1
+	AlgorithmSHA256
+)
+
+type algorithmDetails struct {
+	name string
+	size int
+	sum  func([]byte) ObjectID
+	new  func() hash.Hash
+}
+
+var algorithmTable = [...]algorithmDetails{
+	AlgorithmUnknown: {},
+	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,
+	},
+}
+
+var (
+	algorithmByName     = map[string]Algorithm{}
+	supportedAlgorithms []Algorithm
+)
+
+func init() { //nolint:gochecknoinits
+	for algo := Algorithm(0); int(algo) < len(algorithmTable); algo++ {
+		info := algorithmTable[algo]
+		if info.name == "" {
+			continue
+		}
+
+		algorithmByName[info.name] = algo
+		supportedAlgorithms = append(supportedAlgorithms, algo)
+	}
+}
+
+// SupportedAlgorithms returns all object ID algorithms supported by furgit.
+// Do not mutate.
+func SupportedAlgorithms() []Algorithm {
+	return supportedAlgorithms
+}
+
+// ParseAlgorithm parses a canonical algorithm name (e.g. "sha1", "sha256").
+func ParseAlgorithm(s string) (Algorithm, bool) {
+	algo, ok := algorithmByName[s]
+
+	return algo, ok
+}
+
+// Size returns the hash size in bytes.
+func (algo Algorithm) Size() int {
+	return algo.info().size
+}
+
+// String returns the canonical algorithm name.
+func (algo Algorithm) String() string {
+	inf := algo.info()
+	if inf.name == "" {
+		return "unknown"
+	}
+
+	return inf.name
+}
+
+// HexLen returns the encoded hexadecimal length.
+func (algo Algorithm) HexLen() int {
+	return algo.Size() * 2
+}
+
+// Sum computes an object ID from raw data using the selected algorithm.
+func (algo Algorithm) Sum(data []byte) ObjectID {
+	return algo.info().sum(data)
+}
+
+// New returns a new hash.Hash for this algorithm.
+func (algo Algorithm) New() (hash.Hash, error) {
+	newFn := algo.info().new
+	if newFn == nil {
+		return nil, ErrInvalidAlgorithm
+	}
+
+	return newFn(), nil
+}
+
+func (algo Algorithm) info() algorithmDetails {
+	return algorithmTable[algo]
+}
--- /dev/null
+++ b/objectid/errors.go
@@ -1,0 +1,10 @@
+package objectid
+
+import "errors"
+
+var (
+	// ErrInvalidAlgorithm indicates an unsupported object ID algorithm.
+	ErrInvalidAlgorithm = errors.New("objectid: invalid algorithm")
+	// ErrInvalidObjectID indicates malformed object ID data.
+	ErrInvalidObjectID = errors.New("objectid: invalid object id")
+)
--- a/objectid/objectid.go
+++ b/objectid/objectid.go
@@ -2,140 +2,11 @@
 package objectid
 
 import (
-	"crypto/sha1" //#nosec G505
-	"crypto/sha256"
+	//#nosec G505
+
 	"encoding/hex"
-	"errors"
 	"fmt"
-	"hash"
 )
-
-var (
-	// ErrInvalidAlgorithm indicates an unsupported object ID algorithm.
-	ErrInvalidAlgorithm = errors.New("objectid: invalid algorithm")
-	// ErrInvalidObjectID indicates malformed object ID data.
-	ErrInvalidObjectID = errors.New("objectid: invalid object id")
-)
-
-// maxObjectIDSize MUST be >= the largest supported algorithm size.
-const maxObjectIDSize = sha256.Size
-
-// Algorithm identifies the hash algorithm used for Git object IDs.
-type Algorithm uint8
-
-const (
-	AlgorithmUnknown Algorithm = iota
-	AlgorithmSHA1
-	AlgorithmSHA256
-)
-
-type algorithmDetails struct {
-	name string
-	size int
-	sum  func([]byte) ObjectID
-	new  func() hash.Hash
-}
-
-var algorithmTable = [...]algorithmDetails{
-	AlgorithmUnknown: {},
-	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,
-	},
-}
-
-var (
-	algorithmByName     = map[string]Algorithm{}
-	supportedAlgorithms []Algorithm
-)
-
-func init() { //nolint:gochecknoinits
-	for algo := Algorithm(0); int(algo) < len(algorithmTable); algo++ {
-		info := algorithmTable[algo]
-		if info.name == "" {
-			continue
-		}
-
-		algorithmByName[info.name] = algo
-		supportedAlgorithms = append(supportedAlgorithms, algo)
-	}
-}
-
-// SupportedAlgorithms returns all object ID algorithms supported by furgit.
-// Do not mutate.
-func SupportedAlgorithms() []Algorithm {
-	return supportedAlgorithms
-}
-
-// ParseAlgorithm parses a canonical algorithm name (e.g. "sha1", "sha256").
-func ParseAlgorithm(s string) (Algorithm, bool) {
-	algo, ok := algorithmByName[s]
-
-	return algo, ok
-}
-
-// Size returns the hash size in bytes.
-func (algo Algorithm) Size() int {
-	return algo.info().size
-}
-
-// String returns the canonical algorithm name.
-func (algo Algorithm) String() string {
-	inf := algo.info()
-	if inf.name == "" {
-		return "unknown"
-	}
-
-	return inf.name
-}
-
-// HexLen returns the encoded hexadecimal length.
-func (algo Algorithm) HexLen() int {
-	return algo.Size() * 2
-}
-
-// Sum computes an object ID from raw data using the selected algorithm.
-func (algo Algorithm) Sum(data []byte) ObjectID {
-	return algo.info().sum(data)
-}
-
-// New returns a new hash.Hash for this algorithm.
-func (algo Algorithm) New() (hash.Hash, error) {
-	newFn := algo.info().new
-	if newFn == nil {
-		return nil, ErrInvalidAlgorithm
-	}
-
-	return newFn(), nil
-}
-
-func (algo Algorithm) info() algorithmDetails {
-	return algorithmTable[algo]
-}
 
 // ObjectID represents a Git object ID.
 //
--