fix: git.ComputeHash did not write the content
(cherry picked from commit 5247fd50db
)
This commit is contained in:
parent
a7570ce5ac
commit
fd59a255e6
1 changed files with 16 additions and 20 deletions
|
@ -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 (
|
||||||
|
|
Loading…
Add table
Reference in a new issue