github.com/stffabi/git-lfs@v2.3.5-0.20180214015214-8eeaa8d88902+incompatible/git/odb/pack/index_v1.go (about) 1 package pack 2 3 import ( 4 "encoding/binary" 5 ) 6 7 // V1 implements IndexVersion for v1 packfiles. 8 type V1 struct{} 9 10 // Name implements IndexVersion.Name by returning the 20 byte SHA-1 object name 11 // for the given entry at offset "at" in the v1 index file "idx". 12 func (v *V1) Name(idx *Index, at int64) ([]byte, error) { 13 var sha [20]byte 14 if _, err := idx.readAt(sha[:], v1ShaOffset(at)); err != nil { 15 return nil, err 16 } 17 18 return sha[:], nil 19 } 20 21 // Entry implements IndexVersion.Entry for v1 packfiles by parsing and returning 22 // the IndexEntry specified at the offset "at" in the given index file. 23 func (v *V1) Entry(idx *Index, at int64) (*IndexEntry, error) { 24 var offs [4]byte 25 if _, err := idx.readAt(offs[:], v1EntryOffset(at)); err != nil { 26 return nil, err 27 } 28 29 return &IndexEntry{ 30 PackOffset: uint64(binary.BigEndian.Uint32(offs[:])), 31 }, nil 32 } 33 34 // Width implements IndexVersion.Width() by returning the number of bytes that 35 // v1 packfile index header occupy. 36 func (v *V1) Width() int64 { 37 return indexV1Width 38 } 39 40 // v1ShaOffset returns the location of the SHA1 of an object given at "at". 41 func v1ShaOffset(at int64) int64 { 42 // Skip forward until the desired entry. 43 return v1EntryOffset(at) + 44 // Skip past the 4-byte object offset in the desired entry to 45 // the SHA1. 46 indexObjectSmallOffsetWidth 47 } 48 49 // v1EntryOffset returns the location of the packfile offset for the object 50 // given at "at". 51 func v1EntryOffset(at int64) int64 { 52 // Skip the L1 fanout table 53 return indexOffsetV1Start + 54 // Skip the object entries before the one located at "at" 55 (indexObjectEntryV1Width * at) 56 }