@@ -148,7 +148,7 @@ func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
148
148
// ReadBatchLine reads the header line from cat-file --batch
149
149
// We expect:
150
150
// <sha> SP <type> SP <size> LF
151
- // sha is a 40byte not 20byte here
151
+ // sha is a hex encoded here
152
152
func ReadBatchLine (rd * bufio.Reader ) (sha []byte , typ string , size int64 , err error ) {
153
153
typ , err = rd .ReadString ('\n' )
154
154
if err != nil {
@@ -251,20 +251,19 @@ headerLoop:
251
251
}
252
252
253
253
// git tree files are a list:
254
- // <mode-in-ascii> SP <fname> NUL <20-byte SHA >
254
+ // <mode-in-ascii> SP <fname> NUL <binary Hash >
255
255
//
256
256
// Unfortunately this 20-byte notation is somewhat in conflict to all other git tools
257
- // Therefore we need some method to convert these 20-byte SHAs to a 40-byte SHA
257
+ // Therefore we need some method to convert these binary hashes to hex hashes
258
258
259
- // constant hextable to help quickly convert between 20byte and 40byte hashes
259
+ // constant hextable to help quickly convert between binary and hex representation
260
260
const hextable = "0123456789abcdef"
261
261
262
- // To40ByteSHA converts a 20-byte SHA into a 40-byte sha . Input and output can be the
263
- // same 40 byte slice to support in place conversion without allocations.
262
+ // BinToHexHeash converts a binary Hash into a hex encoded one . Input and output can be the
263
+ // same byte slice to support in place conversion without allocations.
264
264
// This is at least 100x quicker that hex.EncodeToString
265
- // NB This requires that out is a 40-byte slice
266
- func To40ByteSHA (sha , out []byte ) []byte {
267
- for i := 19 ; i >= 0 ; i -- {
265
+ func BinToHexHash (hash HashType , sha , out []byte ) []byte {
266
+ for i := hash .FullLength ()/ 2 - 1 ; i >= 0 ; i -- {
268
267
v := sha [i ]
269
268
vhi , vlo := v >> 4 , v & 0x0f
270
269
shi , slo := hextable [vhi ], hextable [vlo ]
@@ -278,10 +277,10 @@ func To40ByteSHA(sha, out []byte) []byte {
278
277
// It is recommended therefore to pass in an fnameBuf large enough to avoid almost all allocations
279
278
//
280
279
// Each line is composed of:
281
- // <mode-in-ascii-dropping-initial-zeros> SP <fname> NUL <20-byte SHA >
280
+ // <mode-in-ascii-dropping-initial-zeros> SP <fname> NUL <binary HASH >
282
281
//
283
- // We don't attempt to convert the 20-byte SHA to 40-byte SHA to save a lot of time
284
- func ParseTreeLine (rd * bufio.Reader , modeBuf , fnameBuf , shaBuf []byte ) (mode , fname , sha []byte , n int , err error ) {
282
+ // We don't attempt to convert the raw HASH to save a lot of time
283
+ func ParseTreeLine (hash HashType , rd * bufio.Reader , modeBuf , fnameBuf , shaBuf []byte ) (mode , fname , sha []byte , n int , err error ) {
285
284
var readBytes []byte
286
285
287
286
// Read the Mode & fname
@@ -324,11 +323,12 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
324
323
fnameBuf = fnameBuf [:len (fnameBuf )- 1 ]
325
324
fname = fnameBuf
326
325
327
- // Deal with the 20-byte SHA
326
+ // Deal with the binary hash
328
327
idx = 0
329
- for idx < 20 {
328
+ hashLen := hash .FullLength () / 2
329
+ for idx < hashLen {
330
330
var read int
331
- read , err = rd .Read (shaBuf [idx :20 ])
331
+ read , err = rd .Read (shaBuf [idx :hashLen ])
332
332
n += read
333
333
if err != nil {
334
334
return mode , fname , sha , n , err
0 commit comments