1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-25 08:59:31 -05:00

fix: git.ComputeHash did not write the content

This commit is contained in:
oliverpool 2024-04-26 10:02:47 +02:00
parent 3dfa5ba43a
commit 5247fd50db

View file

@ -6,6 +6,7 @@ package git
import ( import (
"crypto/sha1" "crypto/sha1"
"crypto/sha256" "crypto/sha256"
"hash"
"regexp" "regexp"
"strconv" "strconv"
) )
@ -33,6 +34,15 @@ type ObjectFormat interface {
ComputeHash(t ObjectType, content []byte) ObjectID ComputeHash(t ObjectType, content []byte) ObjectID
} }
func computeHash(dst []byte, hasher hash.Hash, t ObjectType, content []byte) []byte {
_, _ = hasher.Write(t.Bytes())
_, _ = hasher.Write([]byte(" "))
_, _ = hasher.Write([]byte(strconv.Itoa(len(content))))
_, _ = hasher.Write([]byte{0})
_, _ = hasher.Write(content)
return hasher.Sum(dst)
}
/* SHA1 Type */ /* SHA1 Type */
type Sha1ObjectFormatImpl struct{} type Sha1ObjectFormatImpl struct{}
@ -65,16 +75,9 @@ func (Sha1ObjectFormatImpl) MustID(b []byte) ObjectID {
// ComputeHash compute the hash for a given ObjectType and content // ComputeHash compute the hash for a given ObjectType and content
func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID { func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID {
hasher := sha1.New() var obj Sha1Hash
_, _ = hasher.Write(t.Bytes()) computeHash(obj[:0], sha1.New(), t, content)
_, _ = hasher.Write([]byte(" ")) return &obj
_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
_, _ = hasher.Write([]byte{0})
// HashSum generates a SHA1 for the provided hash
var sha1 Sha1Hash
copy(sha1[:], hasher.Sum(nil))
return &sha1
} }
/* SHA256 Type */ /* SHA256 Type */
@ -111,16 +114,9 @@ func (Sha256ObjectFormatImpl) MustID(b []byte) ObjectID {
// ComputeHash compute the hash for a given ObjectType and content // ComputeHash compute the hash for a given ObjectType and content
func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID { func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID {
hasher := sha256.New() var obj Sha256Hash
_, _ = hasher.Write(t.Bytes()) computeHash(obj[:0], sha256.New(), t, content)
_, _ = hasher.Write([]byte(" ")) return &obj
_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
_, _ = hasher.Write([]byte{0})
// HashSum generates a SHA256 for the provided hash
var sha256 Sha1Hash
copy(sha256[:], hasher.Sum(nil))
return &sha256
} }
var ( var (