| 1 |
package v2 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"crypto" |
| 6 |
encbin "encoding/binary" |
| 7 |
"errors" |
| 8 |
"io" |
| 9 |
"time" |
| 10 |
|
| 11 |
"github.com/go-git/go-git/v5/plumbing" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/hash" |
| 13 |
"github.com/go-git/go-git/v5/utils/binary" |
| 14 |
) |
| 15 |
|
| 16 |
var ( |
| 17 |
// ErrUnsupportedVersion is returned by OpenFileIndex when the commit graph |
| 18 |
// file version is not supported. |
| 19 |
ErrUnsupportedVersion = errors.New("unsupported version") |
| 20 |
// ErrUnsupportedHash is returned by OpenFileIndex when the commit graph |
| 21 |
// hash function is not supported. Currently only SHA-1 is defined and |
| 22 |
// supported. |
| 23 |
ErrUnsupportedHash = errors.New("unsupported hash algorithm") |
| 24 |
// ErrMalformedCommitGraphFile is returned by OpenFileIndex when the commit |
| 25 |
// graph file is corrupted. |
| 26 |
ErrMalformedCommitGraphFile = errors.New("malformed commit graph file") |
| 27 |
|
| 28 |
commitFileSignature = []byte{'C', 'G', 'P', 'H'} |
| 29 |
|
| 30 |
parentNone = uint32(0x70000000) |
| 31 |
parentOctopusUsed = uint32(0x80000000) |
| 32 |
parentOctopusMask = uint32(0x7fffffff) |
| 33 |
parentLast = uint32(0x80000000) |
| 34 |
) |
| 35 |
|
| 36 |
const ( |
| 37 |
szUint32 = 4 |
| 38 |
szUint64 = 8 |
| 39 |
|
| 40 |
szSignature = 4 |
| 41 |
szHeader = 4 |
| 42 |
szCommitData = 2*szUint32 + szUint64 |
| 43 |
|
| 44 |
lenFanout = 256 |
| 45 |
) |
| 46 |
|
| 47 |
type fileIndex struct { |
| 48 |
reader ReaderAtCloser |
| 49 |
fanout [lenFanout]uint32 |
| 50 |
offsets [lenChunks]int64 |
| 51 |
parent Index |
| 52 |
hasGenerationV2 bool |
| 53 |
minimumNumberOfHashes uint32 |
| 54 |
} |
| 55 |
|
| 56 |
// ReaderAtCloser is an interface that combines io.ReaderAt and io.Closer. |
| 57 |
type ReaderAtCloser interface { |
| 58 |
io.ReaderAt |
| 59 |
io.Closer |
| 60 |
} |
| 61 |
|
| 62 |
// OpenFileIndex opens a serialized commit graph file in the format described at |
| 63 |
// https://github.com/git/git/blob/master/Documentation/technical/commit-graph-format.txt |
| 64 |
func OpenFileIndex(reader ReaderAtCloser) (Index, error) { |
| 65 |
return OpenFileIndexWithParent(reader, nil) |
| 66 |
} |
| 67 |
|
| 68 |
// OpenFileIndexWithParent opens a serialized commit graph file in the format described at |
| 69 |
// https://github.com/git/git/blob/master/Documentation/technical/commit-graph-format.txt |
| 70 |
func OpenFileIndexWithParent(reader ReaderAtCloser, parent Index) (Index, error) { |
| 71 |
if reader == nil { |
| 72 |
return nil, io.ErrUnexpectedEOF |
| 73 |
} |
| 74 |
fi := &fileIndex{reader: reader, parent: parent} |
| 75 |
|
| 76 |
if err := fi.verifyFileHeader(); err != nil { |
| 77 |
return nil, err |
| 78 |
} |
| 79 |
if err := fi.readChunkHeaders(); err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
if err := fi.readFanout(); err != nil { |
| 83 |
return nil, err |
| 84 |
} |
| 85 |
|
| 86 |
fi.hasGenerationV2 = fi.offsets[GenerationDataChunk] > 0 |
| 87 |
if fi.parent != nil { |
| 88 |
fi.hasGenerationV2 = fi.hasGenerationV2 && fi.parent.HasGenerationV2() |
| 89 |
} |
| 90 |
|
| 91 |
if fi.parent != nil { |
| 92 |
fi.minimumNumberOfHashes = fi.parent.MaximumNumberOfHashes() |
| 93 |
} |
| 94 |
|
| 95 |
return fi, nil |
| 96 |
} |
| 97 |
|
| 98 |
// Close closes the underlying reader and the parent index if it exists. |
| 99 |
func (fi *fileIndex) Close() (err error) { |
| 100 |
if fi.parent != nil { |
| 101 |
defer func() { |
| 102 |
parentErr := fi.parent.Close() |
| 103 |
// only report the error from the parent if there is no error from the reader |
| 104 |
if err == nil { |
| 105 |
err = parentErr |
| 106 |
} |
| 107 |
}() |
| 108 |
} |
| 109 |
err = fi.reader.Close() |
| 110 |
return |
| 111 |
} |
| 112 |
|
| 113 |
func (fi *fileIndex) verifyFileHeader() error { |
| 114 |
// Verify file signature |
| 115 |
signature := make([]byte, szSignature) |
| 116 |
if _, err := fi.reader.ReadAt(signature, 0); err != nil { |
| 117 |
return err |
| 118 |
} |
| 119 |
if !bytes.Equal(signature, commitFileSignature) { |
| 120 |
return ErrMalformedCommitGraphFile |
| 121 |
} |
| 122 |
|
| 123 |
// Read and verify the file header |
| 124 |
header := make([]byte, szHeader) |
| 125 |
if _, err := fi.reader.ReadAt(header, szHeader); err != nil { |
| 126 |
return err |
| 127 |
} |
| 128 |
if header[0] != 1 { |
| 129 |
return ErrUnsupportedVersion |
| 130 |
} |
| 131 |
if !(hash.CryptoType == crypto.SHA1 && header[1] == 1) && |
| 132 |
!(hash.CryptoType == crypto.SHA256 && header[1] == 2) { |
| 133 |
// Unknown hash type / unsupported hash type |
| 134 |
return ErrUnsupportedHash |
| 135 |
} |
| 136 |
|
| 137 |
return nil |
| 138 |
} |
| 139 |
|
| 140 |
func (fi *fileIndex) readChunkHeaders() error { |
| 141 |
// The chunk table is a list of 4-byte chunk signatures and uint64 offsets into the file |
| 142 |
chunkID := make([]byte, szChunkSig) |
| 143 |
for i := 0; ; i++ { |
| 144 |
chunkHeader := io.NewSectionReader(fi.reader, szSignature+szHeader+(int64(i)*(szChunkSig+szUint64)), szChunkSig+szUint64) |
| 145 |
if _, err := io.ReadAtLeast(chunkHeader, chunkID, szChunkSig); err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
chunkOffset, err := binary.ReadUint64(chunkHeader) |
| 149 |
if err != nil { |
| 150 |
return err |
| 151 |
} |
| 152 |
|
| 153 |
chunkType, ok := ChunkTypeFromBytes(chunkID) |
| 154 |
if !ok { |
| 155 |
continue |
| 156 |
} |
| 157 |
if chunkType == ZeroChunk || int(chunkType) >= len(fi.offsets) { |
| 158 |
break |
| 159 |
} |
| 160 |
fi.offsets[chunkType] = int64(chunkOffset) |
| 161 |
} |
| 162 |
|
| 163 |
if fi.offsets[OIDFanoutChunk] <= 0 || fi.offsets[OIDLookupChunk] <= 0 || fi.offsets[CommitDataChunk] <= 0 { |
| 164 |
return ErrMalformedCommitGraphFile |
| 165 |
} |
| 166 |
|
| 167 |
return nil |
| 168 |
} |
| 169 |
|
| 170 |
func (fi *fileIndex) readFanout() error { |
| 171 |
// The Fanout table is a 256 entry table of the number (as uint32) of OIDs with first byte at most i. |
| 172 |
// Thus F[255] stores the total number of commits (N) |
| 173 |
fanoutReader := io.NewSectionReader(fi.reader, fi.offsets[OIDFanoutChunk], lenFanout*szUint32) |
| 174 |
for i := 0; i < 256; i++ { |
| 175 |
fanoutValue, err := binary.ReadUint32(fanoutReader) |
| 176 |
if err != nil { |
| 177 |
return err |
| 178 |
} |
| 179 |
if fanoutValue > 0x7fffffff { |
| 180 |
return ErrMalformedCommitGraphFile |
| 181 |
} |
| 182 |
fi.fanout[i] = fanoutValue |
| 183 |
} |
| 184 |
return nil |
| 185 |
} |
| 186 |
|
| 187 |
// GetIndexByHash looks up the provided hash in the commit-graph fanout and returns the index of the commit data for the given hash. |
| 188 |
func (fi *fileIndex) GetIndexByHash(h plumbing.Hash) (uint32, error) { |
| 189 |
var oid plumbing.Hash |
| 190 |
|
| 191 |
// Find the hash in the oid lookup table |
| 192 |
var low uint32 |
| 193 |
if h[0] == 0 { |
| 194 |
low = 0 |
| 195 |
} else { |
| 196 |
low = fi.fanout[h[0]-1] |
| 197 |
} |
| 198 |
high := fi.fanout[h[0]] |
| 199 |
for low < high { |
| 200 |
mid := (low + high) >> 1 |
| 201 |
offset := fi.offsets[OIDLookupChunk] + int64(mid)*hash.Size |
| 202 |
if _, err := fi.reader.ReadAt(oid[:], offset); err != nil { |
| 203 |
return 0, err |
| 204 |
} |
| 205 |
cmp := bytes.Compare(h[:], oid[:]) |
| 206 |
if cmp < 0 { |
| 207 |
high = mid |
| 208 |
} else if cmp == 0 { |
| 209 |
return mid + fi.minimumNumberOfHashes, nil |
| 210 |
} else { |
| 211 |
low = mid + 1 |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
if fi.parent != nil { |
| 216 |
idx, err := fi.parent.GetIndexByHash(h) |
| 217 |
if err != nil { |
| 218 |
return 0, err |
| 219 |
} |
| 220 |
return idx, nil |
| 221 |
} |
| 222 |
|
| 223 |
return 0, plumbing.ErrObjectNotFound |
| 224 |
} |
| 225 |
|
| 226 |
// GetCommitDataByIndex returns the commit data for the given index in the commit-graph. |
| 227 |
func (fi *fileIndex) GetCommitDataByIndex(idx uint32) (*CommitData, error) { |
| 228 |
if idx < fi.minimumNumberOfHashes { |
| 229 |
if fi.parent != nil { |
| 230 |
data, err := fi.parent.GetCommitDataByIndex(idx) |
| 231 |
if err != nil { |
| 232 |
return nil, err |
| 233 |
} |
| 234 |
return data, nil |
| 235 |
} |
| 236 |
|
| 237 |
return nil, plumbing.ErrObjectNotFound |
| 238 |
} |
| 239 |
idx -= fi.minimumNumberOfHashes |
| 240 |
if idx >= fi.fanout[0xff] { |
| 241 |
return nil, plumbing.ErrObjectNotFound |
| 242 |
} |
| 243 |
|
| 244 |
offset := fi.offsets[CommitDataChunk] + int64(idx)*(hash.Size+szCommitData) |
| 245 |
commitDataReader := io.NewSectionReader(fi.reader, offset, hash.Size+szCommitData) |
| 246 |
|
| 247 |
treeHash, err := binary.ReadHash(commitDataReader) |
| 248 |
if err != nil { |
| 249 |
return nil, err |
| 250 |
} |
| 251 |
parent1, err := binary.ReadUint32(commitDataReader) |
| 252 |
if err != nil { |
| 253 |
return nil, err |
| 254 |
} |
| 255 |
parent2, err := binary.ReadUint32(commitDataReader) |
| 256 |
if err != nil { |
| 257 |
return nil, err |
| 258 |
} |
| 259 |
genAndTime, err := binary.ReadUint64(commitDataReader) |
| 260 |
if err != nil { |
| 261 |
return nil, err |
| 262 |
} |
| 263 |
|
| 264 |
var parentIndexes []uint32 |
| 265 |
if parent2&parentOctopusUsed == parentOctopusUsed { |
| 266 |
// Octopus merge - Look-up the extra parents from the extra edge list |
| 267 |
// The extra edge list is a list of uint32s, each of which is an index into the Commit Data table, terminated by a index with the most significant bit on. |
| 268 |
parentIndexes = []uint32{parent1 & parentOctopusMask} |
| 269 |
offset := fi.offsets[ExtraEdgeListChunk] + szUint32*int64(parent2&parentOctopusMask) |
| 270 |
buf := make([]byte, szUint32) |
| 271 |
for { |
| 272 |
_, err := fi.reader.ReadAt(buf, offset) |
| 273 |
if err != nil { |
| 274 |
return nil, err |
| 275 |
} |
| 276 |
|
| 277 |
parent := encbin.BigEndian.Uint32(buf) |
| 278 |
offset += szUint32 |
| 279 |
parentIndexes = append(parentIndexes, parent&parentOctopusMask) |
| 280 |
if parent&parentLast == parentLast { |
| 281 |
break |
| 282 |
} |
| 283 |
} |
| 284 |
} else if parent2 != parentNone { |
| 285 |
parentIndexes = []uint32{parent1 & parentOctopusMask, parent2 & parentOctopusMask} |
| 286 |
} else if parent1 != parentNone { |
| 287 |
parentIndexes = []uint32{parent1 & parentOctopusMask} |
| 288 |
} |
| 289 |
|
| 290 |
parentHashes, err := fi.getHashesFromIndexes(parentIndexes) |
| 291 |
if err != nil { |
| 292 |
return nil, err |
| 293 |
} |
| 294 |
|
| 295 |
generationV2 := uint64(0) |
| 296 |
|
| 297 |
if fi.hasGenerationV2 { |
| 298 |
// set the GenerationV2 result to the commit time |
| 299 |
generationV2 = uint64(genAndTime & 0x3FFFFFFFF) |
| 300 |
|
| 301 |
// Next read the generation (offset) data from the generation data chunk |
| 302 |
offset := fi.offsets[GenerationDataChunk] + int64(idx)*szUint32 |
| 303 |
buf := make([]byte, szUint32) |
| 304 |
if _, err := fi.reader.ReadAt(buf, offset); err != nil { |
| 305 |
return nil, err |
| 306 |
} |
| 307 |
genV2Data := encbin.BigEndian.Uint32(buf) |
| 308 |
|
| 309 |
// check if the data is an overflow that needs to be looked up in the overflow chunk |
| 310 |
if genV2Data&0x80000000 > 0 { |
| 311 |
// Overflow |
| 312 |
offset := fi.offsets[GenerationDataOverflowChunk] + int64(genV2Data&0x7fffffff)*szUint64 |
| 313 |
buf := make([]byte, 8) |
| 314 |
if _, err := fi.reader.ReadAt(buf, offset); err != nil { |
| 315 |
return nil, err |
| 316 |
} |
| 317 |
|
| 318 |
generationV2 += encbin.BigEndian.Uint64(buf) |
| 319 |
} else { |
| 320 |
generationV2 += uint64(genV2Data) |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
return &CommitData{ |
| 325 |
TreeHash: treeHash, |
| 326 |
ParentIndexes: parentIndexes, |
| 327 |
ParentHashes: parentHashes, |
| 328 |
Generation: genAndTime >> 34, |
| 329 |
GenerationV2: generationV2, |
| 330 |
When: time.Unix(int64(genAndTime&0x3FFFFFFFF), 0), |
| 331 |
}, nil |
| 332 |
} |
| 333 |
|
| 334 |
// GetHashByIndex looks up the hash for the given index in the commit-graph. |
| 335 |
func (fi *fileIndex) GetHashByIndex(idx uint32) (found plumbing.Hash, err error) { |
| 336 |
if idx < fi.minimumNumberOfHashes { |
| 337 |
if fi.parent != nil { |
| 338 |
return fi.parent.GetHashByIndex(idx) |
| 339 |
} |
| 340 |
return found, ErrMalformedCommitGraphFile |
| 341 |
} |
| 342 |
idx -= fi.minimumNumberOfHashes |
| 343 |
if idx >= fi.fanout[0xff] { |
| 344 |
return found, ErrMalformedCommitGraphFile |
| 345 |
} |
| 346 |
|
| 347 |
offset := fi.offsets[OIDLookupChunk] + int64(idx)*hash.Size |
| 348 |
if _, err := fi.reader.ReadAt(found[:], offset); err != nil { |
| 349 |
return found, err |
| 350 |
} |
| 351 |
|
| 352 |
return found, nil |
| 353 |
} |
| 354 |
|
| 355 |
func (fi *fileIndex) getHashesFromIndexes(indexes []uint32) ([]plumbing.Hash, error) { |
| 356 |
hashes := make([]plumbing.Hash, len(indexes)) |
| 357 |
|
| 358 |
for i, idx := range indexes { |
| 359 |
if idx < fi.minimumNumberOfHashes { |
| 360 |
if fi.parent != nil { |
| 361 |
hash, err := fi.parent.GetHashByIndex(idx) |
| 362 |
if err != nil { |
| 363 |
return nil, err |
| 364 |
} |
| 365 |
hashes[i] = hash |
| 366 |
continue |
| 367 |
} |
| 368 |
|
| 369 |
return nil, ErrMalformedCommitGraphFile |
| 370 |
} |
| 371 |
|
| 372 |
idx -= fi.minimumNumberOfHashes |
| 373 |
if idx >= fi.fanout[0xff] { |
| 374 |
return nil, ErrMalformedCommitGraphFile |
| 375 |
} |
| 376 |
|
| 377 |
offset := fi.offsets[OIDLookupChunk] + int64(idx)*hash.Size |
| 378 |
if _, err := fi.reader.ReadAt(hashes[i][:], offset); err != nil { |
| 379 |
return nil, err |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
return hashes, nil |
| 384 |
} |
| 385 |
|
| 386 |
// Hashes returns all the hashes that are available in the index. |
| 387 |
func (fi *fileIndex) Hashes() []plumbing.Hash { |
| 388 |
hashes := make([]plumbing.Hash, fi.fanout[0xff]+fi.minimumNumberOfHashes) |
| 389 |
for i := uint32(0); i < fi.minimumNumberOfHashes; i++ { |
| 390 |
hash, err := fi.parent.GetHashByIndex(i) |
| 391 |
if err != nil { |
| 392 |
return nil |
| 393 |
} |
| 394 |
hashes[i] = hash |
| 395 |
} |
| 396 |
|
| 397 |
for i := uint32(0); i < fi.fanout[0xff]; i++ { |
| 398 |
offset := fi.offsets[OIDLookupChunk] + int64(i)*hash.Size |
| 399 |
if n, err := fi.reader.ReadAt(hashes[i+fi.minimumNumberOfHashes][:], offset); err != nil || n < hash.Size { |
| 400 |
return nil |
| 401 |
} |
| 402 |
} |
| 403 |
return hashes |
| 404 |
} |
| 405 |
|
| 406 |
func (fi *fileIndex) HasGenerationV2() bool { |
| 407 |
return fi.hasGenerationV2 |
| 408 |
} |
| 409 |
|
| 410 |
func (fi *fileIndex) MaximumNumberOfHashes() uint32 { |
| 411 |
return fi.minimumNumberOfHashes + fi.fanout[0xff] |
| 412 |
} |