From f4ce6b4ee7c629b100c071e179af1bf22db73d70 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Thu, 5 Mar 2026 21:05:56 +0800 Subject: objectid, format/pack/ingest: Pack hash ID in algo --- format/pack/ingest/rev_write.go | 16 +--------------- objectid/algorithms.go | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/format/pack/ingest/rev_write.go b/format/pack/ingest/rev_write.go index d7eab31f..afa9b4a9 100644 --- a/format/pack/ingest/rev_write.go +++ b/format/pack/ingest/rev_write.go @@ -3,8 +3,6 @@ package ingest import ( "encoding/binary" "slices" - - "codeberg.org/lindenii/furgit/objectid" ) const ( @@ -47,7 +45,7 @@ func writeRev(state *ingestState) error { return err } - binary.BigEndian.PutUint32(scratch[:4], hashID(state.algo)) + binary.BigEndian.PutUint32(scratch[:4], state.algo.PackHashID()) err = writeAndHash(state.revFile, hashImpl, scratch[:4]) if err != nil { @@ -101,15 +99,3 @@ func buildPackOrder(state *ingestState) []int { return out } - -// hashID converts object algorithm to pack hash-id encoding. -func hashID(algo objectid.Algorithm) uint32 { - switch algo { - case objectid.AlgorithmSHA1: - return 1 - case objectid.AlgorithmSHA256: - return 2 - default: - return 0 - } -} diff --git a/objectid/algorithms.go b/objectid/algorithms.go index 0a8d4517..a07edf7b 100644 --- a/objectid/algorithms.go +++ b/objectid/algorithms.go @@ -19,17 +19,19 @@ const ( ) type algorithmDetails struct { - name string - size int - sum func([]byte) ObjectID - new func() hash.Hash + name string + size int + packHashID uint32 + sum func([]byte) ObjectID + new func() hash.Hash } var algorithmTable = [...]algorithmDetails{ AlgorithmUnknown: {}, AlgorithmSHA1: { - name: "sha1", - size: sha1.Size, + name: "sha1", + size: sha1.Size, + packHashID: 1, sum: func(data []byte) ObjectID { sum := sha1.Sum(data) //#nosec G401 @@ -42,8 +44,9 @@ var algorithmTable = [...]algorithmDetails{ new: sha1.New, }, AlgorithmSHA256: { - name: "sha256", - size: sha256.Size, + name: "sha256", + size: sha256.Size, + packHashID: 2, sum: func(data []byte) ObjectID { sum := sha256.Sum256(data) @@ -107,6 +110,13 @@ func (algo Algorithm) HexLen() int { return algo.Size() * 2 } +// PackHashID returns the Git pack/rev hash-id encoding for this algorithm. +// +// Unknown algorithms return 0. +func (algo Algorithm) PackHashID() uint32 { + return algo.info().packHashID +} + // Sum computes an object ID from raw data using the selected algorithm. func (algo Algorithm) Sum(data []byte) ObjectID { return algo.info().sum(data) -- cgit v1.3.1-10-gc9f91