Skip to content

Commit f773733

Browse files
KN4CK3Rzeripath
andauthored
Fix LFS commit finder not working (#15856) (#15874)
* Create a copy of the sha bytes. Co-authored-by: Andrew Thornton <[email protected]>
1 parent cbaf8e8 commit f773733

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

modules/git/batch_reader.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,18 @@ headerLoop:
149149
// constant hextable to help quickly convert between 20byte and 40byte hashes
150150
const hextable = "0123456789abcdef"
151151

152-
// To40ByteSHA converts a 20-byte SHA in a 40-byte slice into a 40-byte sha in place
153-
// without allocations. This is at least 100x quicker that hex.EncodeToString
154-
// NB This requires that sha is a 40-byte slice
155-
func To40ByteSHA(sha []byte) []byte {
152+
// To40ByteSHA converts a 20-byte SHA into a 40-byte sha. Input and output can be the
153+
// same 40 byte slice to support in place conversion without allocations.
154+
// This is at least 100x quicker that hex.EncodeToString
155+
// NB This requires that out is a 40-byte slice
156+
func To40ByteSHA(sha, out []byte) []byte {
156157
for i := 19; i >= 0; i-- {
157158
v := sha[i]
158159
vhi, vlo := v>>4, v&0x0f
159160
shi, slo := hextable[vhi], hextable[vlo]
160-
sha[i*2], sha[i*2+1] = shi, slo
161+
out[i*2], out[i*2+1] = shi, slo
161162
}
162-
return sha
163+
return out
163164
}
164165

165166
// ParseTreeLineSkipMode reads an entry from a tree in a cat-file --batch stream

modules/git/commit_info_nogogit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ revListLoop:
303303
commits[0] = string(commitID)
304304
}
305305
}
306-
treeID = To40ByteSHA(treeID)
306+
treeID = To40ByteSHA(treeID, treeID)
307307
_, err = batchStdinWriter.Write(treeID)
308308
if err != nil {
309309
return nil, err

modules/git/pipeline/lfs_nogogit.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
4343

4444
basePath := repo.Path
4545

46-
hashStr := hash.String()
47-
4846
// Use rev-list to provide us with all commits in order
4947
revListReader, revListWriter := io.Pipe()
5048
defer func() {
@@ -74,7 +72,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
7472

7573
fnameBuf := make([]byte, 4096)
7674
modeBuf := make([]byte, 40)
77-
workingShaBuf := make([]byte, 40)
75+
workingShaBuf := make([]byte, 20)
7876

7977
for scan.Scan() {
8078
// Get the next commit ID
@@ -132,8 +130,7 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
132130
return nil, err
133131
}
134132
n += int64(count)
135-
sha := git.To40ByteSHA(sha20byte)
136-
if bytes.Equal(sha, []byte(hashStr)) {
133+
if bytes.Equal(sha20byte, hash[:]) {
137134
result := LFSResult{
138135
Name: curPath + string(fname),
139136
SHA: curCommit.ID.String(),
@@ -143,7 +140,9 @@ func FindLFSFile(repo *git.Repository, hash git.SHA1) ([]*LFSResult, error) {
143140
}
144141
resultsMap[curCommit.ID.String()+":"+curPath+string(fname)] = &result
145142
} else if string(mode) == git.EntryModeTree.String() {
146-
trees = append(trees, sha)
143+
sha40Byte := make([]byte, 40)
144+
git.To40ByteSHA(sha20byte, sha40Byte)
145+
trees = append(trees, sha40Byte)
147146
paths = append(paths, curPath+string(fname)+"/")
148147
}
149148
}

0 commit comments

Comments
 (0)