aboutsummaryrefslogtreecommitdiff
path: root/objectid
diff options
context:
space:
mode:
authorGravatar Runxi Yu2026-02-21 13:38:02 +0800
committerGravatar Runxi Yu2026-02-21 14:28:15 +0800
commit94482cb2c97aa215f83940643c5d4c0933727dcb (patch)
treebee22fa113542abd1b863ee251fdcf0f9bd409b5 /objectid
parentdiff: Add package-level doc comment (diff)
signatureNo signature
*: Modernize and lint; add CI v0.1.17
Diffstat (limited to 'objectid')
-rw-r--r--objectid/objectid.go32
-rw-r--r--objectid/objectid_test.go2
2 files changed, 17 insertions, 17 deletions
diff --git a/objectid/objectid.go b/objectid/objectid.go
index da4b7a3a..2c085abf 100644
--- a/objectid/objectid.go
+++ b/objectid/objectid.go
@@ -2,7 +2,7 @@
package objectid
import (
- "crypto/sha1"
+ "crypto/sha1" //#nosec G505
"crypto/sha256"
"encoding/hex"
"errors"
@@ -42,15 +42,13 @@ var algorithmTable = [...]algorithmDetails{
name: "sha1",
size: sha1.Size,
sum: func(data []byte) ObjectID {
- sum := sha1.Sum(data)
+ sum := sha1.Sum(data) //#nosec G401
var id ObjectID
copy(id.data[:], sum[:])
id.algo = AlgorithmSHA1
return id
},
- new: func() hash.Hash {
- return sha1.New()
- },
+ new: sha1.New,
},
AlgorithmSHA256: {
name: "sha256",
@@ -62,9 +60,7 @@ var algorithmTable = [...]algorithmDetails{
id.algo = AlgorithmSHA256
return id
},
- new: func() hash.Hash {
- return sha256.New()
- },
+ new: sha256.New,
},
}
@@ -72,20 +68,16 @@ var algorithmByName = map[string]Algorithm{}
var supportedAlgorithms []Algorithm
func init() {
- for algo, info := range algorithmTable {
+ for algo := Algorithm(0); int(algo) < len(algorithmTable); algo++ {
+ info := algorithmTable[algo]
if info.name == "" {
continue
}
- parsed := Algorithm(algo)
- algorithmByName[info.name] = parsed
- supportedAlgorithms = append(supportedAlgorithms, parsed)
+ algorithmByName[info.name] = algo
+ supportedAlgorithms = append(supportedAlgorithms, algo)
}
}
-func (algo Algorithm) info() algorithmDetails {
- return algorithmTable[algo]
-}
-
// SupportedAlgorithms returns all object ID algorithms supported by furgit.
// Do not mutate.
func SupportedAlgorithms() []Algorithm {
@@ -131,7 +123,13 @@ func (algo Algorithm) New() (hash.Hash, error) {
return newFn(), nil
}
+func (algo Algorithm) info() algorithmDetails {
+ return algorithmTable[algo]
+}
+
// ObjectID represents a Git object ID.
+//
+//nolint:recvcheck
type ObjectID struct {
algo Algorithm
data [maxObjectIDSize]byte
@@ -184,7 +182,7 @@ func ParseHex(algo Algorithm, s string) (ObjectID, error) {
}
decoded, err := hex.DecodeString(s)
if err != nil {
- return id, fmt.Errorf("%w: decode: %v", ErrInvalidObjectID, err)
+ return id, fmt.Errorf("%w: decode: %w", ErrInvalidObjectID, err)
}
copy(id.data[:], decoded)
id.algo = algo
diff --git a/objectid/objectid_test.go b/objectid/objectid_test.go
index 7abeb963..ef191d39 100644
--- a/objectid/objectid_test.go
+++ b/objectid/objectid_test.go
@@ -47,6 +47,7 @@ func TestParseHexRoundtrip(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
id, err := objectid.ParseHex(tt.algo, tt.hex)
if err != nil {
t.Fatalf("ParseHex failed: %v", err)
@@ -90,6 +91,7 @@ func TestParseHexErrors(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
+ t.Parallel()
if _, err := objectid.ParseHex(tt.algo, tt.hex); err == nil {
t.Fatalf("expected ParseHex error")
}