@@ -148,7 +148,7 @@ func CatFileBatch(ctx context.Context, repoPath string) (WriteCloserError, *bufi
148148// ReadBatchLine reads the header line from cat-file --batch
149149// We expect:
150150// <sha> SP <type> SP <size> LF
151- // sha is a 40byte not 20byte here
151+ // sha is a hex encoded here
152152func ReadBatchLine (rd * bufio.Reader ) (sha []byte , typ string , size int64 , err error ) {
153153 typ , err = rd .ReadString ('\n' )
154154 if err != nil {
@@ -251,20 +251,19 @@ headerLoop:
251251}
252252
253253// 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 >
255255//
256256// 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
258258
259- // constant hextable to help quickly convert between 20byte and 40byte hashes
259+ // constant hextable to help quickly convert between binary and hex representation
260260const hextable = "0123456789abcdef"
261261
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.
264264// 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 -- {
268267 v := sha [i ]
269268 vhi , vlo := v >> 4 , v & 0x0f
270269 shi , slo := hextable [vhi ], hextable [vlo ]
@@ -278,10 +277,10 @@ func To40ByteSHA(sha, out []byte) []byte {
278277// It is recommended therefore to pass in an fnameBuf large enough to avoid almost all allocations
279278//
280279// 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 >
282281//
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 ) {
285284 var readBytes []byte
286285
287286 // Read the Mode & fname
@@ -324,11 +323,12 @@ func ParseTreeLine(rd *bufio.Reader, modeBuf, fnameBuf, shaBuf []byte) (mode, fn
324323 fnameBuf = fnameBuf [:len (fnameBuf )- 1 ]
325324 fname = fnameBuf
326325
327- // Deal with the 20-byte SHA
326+ // Deal with the binary hash
328327 idx = 0
329- for idx < 20 {
328+ hashLen := hash .FullLength () / 2
329+ for idx < hashLen {
330330 var read int
331- read , err = rd .Read (shaBuf [idx :20 ])
331+ read , err = rd .Read (shaBuf [idx :hashLen ])
332332 n += read
333333 if err != nil {
334334 return mode , fname , sha , n , err
0 commit comments