Skip to content

Commit cd70ab3

Browse files
wxiaoguangGiteaBot
andauthored
Fix incorrect object id hash function (#30708)
Great thanks to @oliverpool for figuring out the problem and proposing a fix. Regression of #28138 Incorrect hash causes the user's LFS files get all deleted when running `doctor fix all` (by the way, remove unused/non-standard comments) Co-authored-by: Giteabot <[email protected]>
1 parent 1e749b8 commit cd70ab3

File tree

3 files changed

+8
-15
lines changed

3 files changed

+8
-15
lines changed

modules/git/object_format.go

+4-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ type ObjectFormat interface {
3333
ComputeHash(t ObjectType, content []byte) ObjectID
3434
}
3535

36-
/* SHA1 Type */
3736
type Sha1ObjectFormatImpl struct{}
3837

3938
var (
@@ -70,14 +69,10 @@ func (h Sha1ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) ObjectID
7069
_, _ = hasher.Write([]byte(" "))
7170
_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
7271
_, _ = 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
72+
_, _ = hasher.Write(content)
73+
return h.MustID(hasher.Sum(nil))
7874
}
7975

80-
/* SHA256 Type */
8176
type Sha256ObjectFormatImpl struct{}
8277

8378
var (
@@ -116,11 +111,8 @@ func (h Sha256ObjectFormatImpl) ComputeHash(t ObjectType, content []byte) Object
116111
_, _ = hasher.Write([]byte(" "))
117112
_, _ = hasher.Write([]byte(strconv.FormatInt(int64(len(content)), 10)))
118113
_, _ = 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
114+
_, _ = hasher.Write(content)
115+
return h.MustID(hasher.Sum(nil))
124116
}
125117

126118
var (

modules/git/object_id.go

-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ type ObjectID interface {
1616
Type() ObjectFormat
1717
}
1818

19-
/* SHA1 */
2019
type Sha1Hash [20]byte
2120

2221
func (h *Sha1Hash) String() string {
@@ -40,7 +39,6 @@ func MustIDFromString(hexHash string) ObjectID {
4039
return id
4140
}
4241

43-
/* SHA256 */
4442
type Sha256Hash [32]byte
4543

4644
func (h *Sha256Hash) String() string {
@@ -54,7 +52,6 @@ func (h *Sha256Hash) IsZero() bool {
5452
func (h *Sha256Hash) RawValue() []byte { return h[:] }
5553
func (*Sha256Hash) Type() ObjectFormat { return Sha256ObjectFormat }
5654

57-
/* utility */
5855
func NewIDFromString(hexHash string) (ObjectID, error) {
5956
var theObjectFormat ObjectFormat
6057
for _, objectFormat := range SupportedObjectFormats {

modules/git/object_id_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ func TestIsValidSHAPattern(t *testing.T) {
1818
assert.False(t, h.IsValid("abc"))
1919
assert.False(t, h.IsValid("123g"))
2020
assert.False(t, h.IsValid("some random text"))
21+
assert.Equal(t, "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391", ComputeBlobHash(Sha1ObjectFormat, nil).String())
22+
assert.Equal(t, "2e65efe2a145dda7ee51d1741299f848e5bf752e", ComputeBlobHash(Sha1ObjectFormat, []byte("a")).String())
23+
assert.Equal(t, "473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813", ComputeBlobHash(Sha256ObjectFormat, nil).String())
24+
assert.Equal(t, "eb337bcee2061c5313c9a1392116b6c76039e9e30d71467ae359b36277e17dc7", ComputeBlobHash(Sha256ObjectFormat, []byte("a")).String())
2125
}

0 commit comments

Comments
 (0)