History

fix: git.ComputeHash did not write the content

oliverpool committed 2 years ago

Commit 5247fd51 parent 3dfa5ba1 file · +16 −20Browse files

Showing the changes to modules (1 of 1 file). Show all

Unified · Split

modules/git/object_format.go+16−20
@@ -6,6 +6,7 @@ package git · 5 unmodified lines
66 import (
77 "crypto/sha1"
88 "crypto/sha256"
9+ "hash"
910 "regexp"
1011 "strconv"
1112 )
@@ -33,6 +34,15 @@ type ObjectFormat interface { · 21 unmodified lines
3334 ComputeHash(t ObjectType, content []byte) ObjectID
3435 }
3536
37+func computeHash(dst []byte, hasher hash.Hash, t ObjectType, content []byte) []byte {
38+ _, _ = hasher.Write(t.Bytes())
39+ _, _ = hasher.Write([]byte(" "))
40+ _, _ = hasher.Write([]byte(strconv.Itoa(len(content))))
41+ _, _ = hasher.Write([]byte{0})
42+ _, _ = hasher.Write(content)
43+ return hasher.Sum(dst)
44+}
45+
3646 /* SHA1 Type */
3747 type Sha1ObjectFormatImpl struct{}
3848
@@ -65,16 +75,9 @@ func (Sha1ObjectFormatImpl) MustID(b []byte) ObjectID { · 26 unmodified lines
6575
6676 // ComputeHash compute the hash for a given ObjectType and content
6777 func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID {
68− hasher := sha1.New()
69− _, _ = hasher.Write(t.Bytes())
70− _, _ = hasher.Write([]byte(" "))
71− _, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
72− _, _ = hasher.Write([]byte{0})
73−
74− // HashSum generates a SHA1 for the provided hash
75− var sha1 Sha1Hash
76− copy(sha1[:], hasher.Sum(nil))
77− return &sha1
78+ var obj Sha1Hash
79+ computeHash(obj[:0], sha1.New(), t, content)
80+ return &obj
7881 }
7982
8083 /* SHA256 Type */
@@ -111,16 +114,9 @@ func (Sha256ObjectFormatImpl) MustID(b []byte) ObjectID { · 30 unmodified lines
111114
112115 // ComputeHash compute the hash for a given ObjectType and content
113116 func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID {
114− hasher := sha256.New()
115− _, _ = hasher.Write(t.Bytes())
116− _, _ = hasher.Write([]byte(" "))
117− _, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
118− _, _ = hasher.Write([]byte{0})
119−
120− // HashSum generates a SHA256 for the provided hash
121− var sha256 Sha1Hash
122− copy(sha256[:], hasher.Sum(nil))
123− return &sha256
117+ var obj Sha256Hash
118+ computeHash(obj[:0], sha256.New(), t, content)
119+ return &obj
124120 }
125121
126122 var (