* text=auto eol=lf
+/vendor/** -text -eol
+/public/vendor/** -text -eol
+
conf/* linguist-vendored
docker/* linguist-vendored
options/* linguist-vendored
-package commitgraph
-
-import (
- "time"
-
- "github.com/go-git/go-git/v5/plumbing"
-)
-
-// CommitData is a reduced representation of Commit as presented in the commit graph
-// file. It is merely useful as an optimization for walking the commit graphs.
-type CommitData struct {
- // TreeHash is the hash of the root tree of the commit.
- TreeHash plumbing.Hash
- // ParentIndexes are the indexes of the parent commits of the commit.
- ParentIndexes []int
- // ParentHashes are the hashes of the parent commits of the commit.
- ParentHashes []plumbing.Hash
- // Generation number is the pre-computed generation in the commit graph
- // or zero if not available
- Generation int
- // When is the timestamp of the commit.
- When time.Time
-}
-
-// Index represents a representation of commit graph that allows indexed
-// access to the nodes using commit object hash
-type Index interface {
- // GetIndexByHash gets the index in the commit graph from commit hash, if available
- GetIndexByHash(h plumbing.Hash) (int, error)
- // GetNodeByIndex gets the commit node from the commit graph using index
- // obtained from child node, if available
- GetCommitDataByIndex(i int) (*CommitData, error)
- // Hashes returns all the hashes that are available in the index
- Hashes() []plumbing.Hash
-}
+package commitgraph\r
+\r
+import (\r
+ "time"\r
+\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+)\r
+\r
+// CommitData is a reduced representation of Commit as presented in the commit graph\r
+// file. It is merely useful as an optimization for walking the commit graphs.\r
+type CommitData struct {\r
+ // TreeHash is the hash of the root tree of the commit.\r
+ TreeHash plumbing.Hash\r
+ // ParentIndexes are the indexes of the parent commits of the commit.\r
+ ParentIndexes []int\r
+ // ParentHashes are the hashes of the parent commits of the commit.\r
+ ParentHashes []plumbing.Hash\r
+ // Generation number is the pre-computed generation in the commit graph\r
+ // or zero if not available\r
+ Generation int\r
+ // When is the timestamp of the commit.\r
+ When time.Time\r
+}\r
+\r
+// Index represents a representation of commit graph that allows indexed\r
+// access to the nodes using commit object hash\r
+type Index interface {\r
+ // GetIndexByHash gets the index in the commit graph from commit hash, if available\r
+ GetIndexByHash(h plumbing.Hash) (int, error)\r
+ // GetNodeByIndex gets the commit node from the commit graph using index\r
+ // obtained from child node, if available\r
+ GetCommitDataByIndex(i int) (*CommitData, error)\r
+ // Hashes returns all the hashes that are available in the index\r
+ Hashes() []plumbing.Hash\r
+}\r
-package commitgraph
-
-import (
- "crypto/sha1"
- "hash"
- "io"
-
- "github.com/go-git/go-git/v5/plumbing"
- "github.com/go-git/go-git/v5/utils/binary"
-)
-
-// Encoder writes MemoryIndex structs to an output stream.
-type Encoder struct {
- io.Writer
- hash hash.Hash
-}
-
-// NewEncoder returns a new stream encoder that writes to w.
-func NewEncoder(w io.Writer) *Encoder {
- h := sha1.New()
- mw := io.MultiWriter(w, h)
- return &Encoder{mw, h}
-}
-
-// Encode writes an index into the commit-graph file
-func (e *Encoder) Encode(idx Index) error {
- // Get all the hashes in the input index
- hashes := idx.Hashes()
-
- // Sort the inout and prepare helper structures we'll need for encoding
- hashToIndex, fanout, extraEdgesCount := e.prepare(idx, hashes)
-
- chunkSignatures := [][]byte{oidFanoutSignature, oidLookupSignature, commitDataSignature}
- chunkSizes := []uint64{4 * 256, uint64(len(hashes)) * 20, uint64(len(hashes)) * 36}
- if extraEdgesCount > 0 {
- chunkSignatures = append(chunkSignatures, extraEdgeListSignature)
- chunkSizes = append(chunkSizes, uint64(extraEdgesCount)*4)
- }
-
- if err := e.encodeFileHeader(len(chunkSignatures)); err != nil {
- return err
- }
- if err := e.encodeChunkHeaders(chunkSignatures, chunkSizes); err != nil {
- return err
- }
- if err := e.encodeFanout(fanout); err != nil {
- return err
- }
- if err := e.encodeOidLookup(hashes); err != nil {
- return err
- }
- if extraEdges, err := e.encodeCommitData(hashes, hashToIndex, idx); err == nil {
- if err = e.encodeExtraEdges(extraEdges); err != nil {
- return err
- }
- } else {
- return err
- }
-
- return e.encodeChecksum()
-}
-
-func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, extraEdgesCount uint32) {
- // Sort the hashes and build our index
- plumbing.HashesSort(hashes)
- hashToIndex = make(map[plumbing.Hash]uint32)
- fanout = make([]uint32, 256)
- for i, hash := range hashes {
- hashToIndex[hash] = uint32(i)
- fanout[hash[0]]++
- }
-
- // Convert the fanout to cumulative values
- for i := 1; i <= 0xff; i++ {
- fanout[i] += fanout[i-1]
- }
-
- // Find out if we will need extra edge table
- for i := 0; i < len(hashes); i++ {
- v, _ := idx.GetCommitDataByIndex(i)
- if len(v.ParentHashes) > 2 {
- extraEdgesCount += uint32(len(v.ParentHashes) - 1)
- break
- }
- }
-
- return
-}
-
-func (e *Encoder) encodeFileHeader(chunkCount int) (err error) {
- if _, err = e.Write(commitFileSignature); err == nil {
- _, err = e.Write([]byte{1, 1, byte(chunkCount), 0})
- }
- return
-}
-
-func (e *Encoder) encodeChunkHeaders(chunkSignatures [][]byte, chunkSizes []uint64) (err error) {
- // 8 bytes of file header, 12 bytes for each chunk header and 12 byte for terminator
- offset := uint64(8 + len(chunkSignatures)*12 + 12)
- for i, signature := range chunkSignatures {
- if _, err = e.Write(signature); err == nil {
- err = binary.WriteUint64(e, offset)
- }
- if err != nil {
- return
- }
- offset += chunkSizes[i]
- }
- if _, err = e.Write(lastSignature); err == nil {
- err = binary.WriteUint64(e, offset)
- }
- return
-}
-
-func (e *Encoder) encodeFanout(fanout []uint32) (err error) {
- for i := 0; i <= 0xff; i++ {
- if err = binary.WriteUint32(e, fanout[i]); err != nil {
- return
- }
- }
- return
-}
-
-func (e *Encoder) encodeOidLookup(hashes []plumbing.Hash) (err error) {
- for _, hash := range hashes {
- if _, err = e.Write(hash[:]); err != nil {
- return err
- }
- }
- return
-}
-
-func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumbing.Hash]uint32, idx Index) (extraEdges []uint32, err error) {
- for _, hash := range hashes {
- origIndex, _ := idx.GetIndexByHash(hash)
- commitData, _ := idx.GetCommitDataByIndex(origIndex)
- if _, err = e.Write(commitData.TreeHash[:]); err != nil {
- return
- }
-
- var parent1, parent2 uint32
- if len(commitData.ParentHashes) == 0 {
- parent1 = parentNone
- parent2 = parentNone
- } else if len(commitData.ParentHashes) == 1 {
- parent1 = hashToIndex[commitData.ParentHashes[0]]
- parent2 = parentNone
- } else if len(commitData.ParentHashes) == 2 {
- parent1 = hashToIndex[commitData.ParentHashes[0]]
- parent2 = hashToIndex[commitData.ParentHashes[1]]
- } else if len(commitData.ParentHashes) > 2 {
- parent1 = hashToIndex[commitData.ParentHashes[0]]
- parent2 = uint32(len(extraEdges)) | parentOctopusUsed
- for _, parentHash := range commitData.ParentHashes[1:] {
- extraEdges = append(extraEdges, hashToIndex[parentHash])
- }
- extraEdges[len(extraEdges)-1] |= parentLast
- }
-
- if err = binary.WriteUint32(e, parent1); err == nil {
- err = binary.WriteUint32(e, parent2)
- }
- if err != nil {
- return
- }
-
- unixTime := uint64(commitData.When.Unix())
- unixTime |= uint64(commitData.Generation) << 34
- if err = binary.WriteUint64(e, unixTime); err != nil {
- return
- }
- }
- return
-}
-
-func (e *Encoder) encodeExtraEdges(extraEdges []uint32) (err error) {
- for _, parent := range extraEdges {
- if err = binary.WriteUint32(e, parent); err != nil {
- return
- }
- }
- return
-}
-
-func (e *Encoder) encodeChecksum() error {
- _, err := e.Write(e.hash.Sum(nil)[:20])
- return err
-}
+package commitgraph\r
+\r
+import (\r
+ "crypto/sha1"\r
+ "hash"\r
+ "io"\r
+\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+ "github.com/go-git/go-git/v5/utils/binary"\r
+)\r
+\r
+// Encoder writes MemoryIndex structs to an output stream.\r
+type Encoder struct {\r
+ io.Writer\r
+ hash hash.Hash\r
+}\r
+\r
+// NewEncoder returns a new stream encoder that writes to w.\r
+func NewEncoder(w io.Writer) *Encoder {\r
+ h := sha1.New()\r
+ mw := io.MultiWriter(w, h)\r
+ return &Encoder{mw, h}\r
+}\r
+\r
+// Encode writes an index into the commit-graph file\r
+func (e *Encoder) Encode(idx Index) error {\r
+ // Get all the hashes in the input index\r
+ hashes := idx.Hashes()\r
+\r
+ // Sort the inout and prepare helper structures we'll need for encoding\r
+ hashToIndex, fanout, extraEdgesCount := e.prepare(idx, hashes)\r
+\r
+ chunkSignatures := [][]byte{oidFanoutSignature, oidLookupSignature, commitDataSignature}\r
+ chunkSizes := []uint64{4 * 256, uint64(len(hashes)) * 20, uint64(len(hashes)) * 36}\r
+ if extraEdgesCount > 0 {\r
+ chunkSignatures = append(chunkSignatures, extraEdgeListSignature)\r
+ chunkSizes = append(chunkSizes, uint64(extraEdgesCount)*4)\r
+ }\r
+\r
+ if err := e.encodeFileHeader(len(chunkSignatures)); err != nil {\r
+ return err\r
+ }\r
+ if err := e.encodeChunkHeaders(chunkSignatures, chunkSizes); err != nil {\r
+ return err\r
+ }\r
+ if err := e.encodeFanout(fanout); err != nil {\r
+ return err\r
+ }\r
+ if err := e.encodeOidLookup(hashes); err != nil {\r
+ return err\r
+ }\r
+ if extraEdges, err := e.encodeCommitData(hashes, hashToIndex, idx); err == nil {\r
+ if err = e.encodeExtraEdges(extraEdges); err != nil {\r
+ return err\r
+ }\r
+ } else {\r
+ return err\r
+ }\r
+\r
+ return e.encodeChecksum()\r
+}\r
+\r
+func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, extraEdgesCount uint32) {\r
+ // Sort the hashes and build our index\r
+ plumbing.HashesSort(hashes)\r
+ hashToIndex = make(map[plumbing.Hash]uint32)\r
+ fanout = make([]uint32, 256)\r
+ for i, hash := range hashes {\r
+ hashToIndex[hash] = uint32(i)\r
+ fanout[hash[0]]++\r
+ }\r
+\r
+ // Convert the fanout to cumulative values\r
+ for i := 1; i <= 0xff; i++ {\r
+ fanout[i] += fanout[i-1]\r
+ }\r
+\r
+ // Find out if we will need extra edge table\r
+ for i := 0; i < len(hashes); i++ {\r
+ v, _ := idx.GetCommitDataByIndex(i)\r
+ if len(v.ParentHashes) > 2 {\r
+ extraEdgesCount += uint32(len(v.ParentHashes) - 1)\r
+ break\r
+ }\r
+ }\r
+\r
+ return\r
+}\r
+\r
+func (e *Encoder) encodeFileHeader(chunkCount int) (err error) {\r
+ if _, err = e.Write(commitFileSignature); err == nil {\r
+ _, err = e.Write([]byte{1, 1, byte(chunkCount), 0})\r
+ }\r
+ return\r
+}\r
+\r
+func (e *Encoder) encodeChunkHeaders(chunkSignatures [][]byte, chunkSizes []uint64) (err error) {\r
+ // 8 bytes of file header, 12 bytes for each chunk header and 12 byte for terminator\r
+ offset := uint64(8 + len(chunkSignatures)*12 + 12)\r
+ for i, signature := range chunkSignatures {\r
+ if _, err = e.Write(signature); err == nil {\r
+ err = binary.WriteUint64(e, offset)\r
+ }\r
+ if err != nil {\r
+ return\r
+ }\r
+ offset += chunkSizes[i]\r
+ }\r
+ if _, err = e.Write(lastSignature); err == nil {\r
+ err = binary.WriteUint64(e, offset)\r
+ }\r
+ return\r
+}\r
+\r
+func (e *Encoder) encodeFanout(fanout []uint32) (err error) {\r
+ for i := 0; i <= 0xff; i++ {\r
+ if err = binary.WriteUint32(e, fanout[i]); err != nil {\r
+ return\r
+ }\r
+ }\r
+ return\r
+}\r
+\r
+func (e *Encoder) encodeOidLookup(hashes []plumbing.Hash) (err error) {\r
+ for _, hash := range hashes {\r
+ if _, err = e.Write(hash[:]); err != nil {\r
+ return err\r
+ }\r
+ }\r
+ return\r
+}\r
+\r
+func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumbing.Hash]uint32, idx Index) (extraEdges []uint32, err error) {\r
+ for _, hash := range hashes {\r
+ origIndex, _ := idx.GetIndexByHash(hash)\r
+ commitData, _ := idx.GetCommitDataByIndex(origIndex)\r
+ if _, err = e.Write(commitData.TreeHash[:]); err != nil {\r
+ return\r
+ }\r
+\r
+ var parent1, parent2 uint32\r
+ if len(commitData.ParentHashes) == 0 {\r
+ parent1 = parentNone\r
+ parent2 = parentNone\r
+ } else if len(commitData.ParentHashes) == 1 {\r
+ parent1 = hashToIndex[commitData.ParentHashes[0]]\r
+ parent2 = parentNone\r
+ } else if len(commitData.ParentHashes) == 2 {\r
+ parent1 = hashToIndex[commitData.ParentHashes[0]]\r
+ parent2 = hashToIndex[commitData.ParentHashes[1]]\r
+ } else if len(commitData.ParentHashes) > 2 {\r
+ parent1 = hashToIndex[commitData.ParentHashes[0]]\r
+ parent2 = uint32(len(extraEdges)) | parentOctopusUsed\r
+ for _, parentHash := range commitData.ParentHashes[1:] {\r
+ extraEdges = append(extraEdges, hashToIndex[parentHash])\r
+ }\r
+ extraEdges[len(extraEdges)-1] |= parentLast\r
+ }\r
+\r
+ if err = binary.WriteUint32(e, parent1); err == nil {\r
+ err = binary.WriteUint32(e, parent2)\r
+ }\r
+ if err != nil {\r
+ return\r
+ }\r
+\r
+ unixTime := uint64(commitData.When.Unix())\r
+ unixTime |= uint64(commitData.Generation) << 34\r
+ if err = binary.WriteUint64(e, unixTime); err != nil {\r
+ return\r
+ }\r
+ }\r
+ return\r
+}\r
+\r
+func (e *Encoder) encodeExtraEdges(extraEdges []uint32) (err error) {\r
+ for _, parent := range extraEdges {\r
+ if err = binary.WriteUint32(e, parent); err != nil {\r
+ return\r
+ }\r
+ }\r
+ return\r
+}\r
+\r
+func (e *Encoder) encodeChecksum() error {\r
+ _, err := e.Write(e.hash.Sum(nil)[:20])\r
+ return err\r
+}\r
-package commitgraph
-
-import (
- "bytes"
- encbin "encoding/binary"
- "errors"
- "io"
- "time"
-
- "github.com/go-git/go-git/v5/plumbing"
- "github.com/go-git/go-git/v5/utils/binary"
-)
-
-var (
- // ErrUnsupportedVersion is returned by OpenFileIndex when the commit graph
- // file version is not supported.
- ErrUnsupportedVersion = errors.New("Unsupported version")
- // ErrUnsupportedHash is returned by OpenFileIndex when the commit graph
- // hash function is not supported. Currently only SHA-1 is defined and
- // supported
- ErrUnsupportedHash = errors.New("Unsupported hash algorithm")
- // ErrMalformedCommitGraphFile is returned by OpenFileIndex when the commit
- // graph file is corrupted.
- ErrMalformedCommitGraphFile = errors.New("Malformed commit graph file")
-
- commitFileSignature = []byte{'C', 'G', 'P', 'H'}
- oidFanoutSignature = []byte{'O', 'I', 'D', 'F'}
- oidLookupSignature = []byte{'O', 'I', 'D', 'L'}
- commitDataSignature = []byte{'C', 'D', 'A', 'T'}
- extraEdgeListSignature = []byte{'E', 'D', 'G', 'E'}
- lastSignature = []byte{0, 0, 0, 0}
-
- parentNone = uint32(0x70000000)
- parentOctopusUsed = uint32(0x80000000)
- parentOctopusMask = uint32(0x7fffffff)
- parentLast = uint32(0x80000000)
-)
-
-type fileIndex struct {
- reader io.ReaderAt
- fanout [256]int
- oidFanoutOffset int64
- oidLookupOffset int64
- commitDataOffset int64
- extraEdgeListOffset int64
-}
-
-// OpenFileIndex opens a serialized commit graph file in the format described at
-// https://github.com/git/git/blob/master/Documentation/technical/commit-graph-format.txt
-func OpenFileIndex(reader io.ReaderAt) (Index, error) {
- fi := &fileIndex{reader: reader}
-
- if err := fi.verifyFileHeader(); err != nil {
- return nil, err
- }
- if err := fi.readChunkHeaders(); err != nil {
- return nil, err
- }
- if err := fi.readFanout(); err != nil {
- return nil, err
- }
-
- return fi, nil
-}
-
-func (fi *fileIndex) verifyFileHeader() error {
- // Verify file signature
- var signature = make([]byte, 4)
- if _, err := fi.reader.ReadAt(signature, 0); err != nil {
- return err
- }
- if !bytes.Equal(signature, commitFileSignature) {
- return ErrMalformedCommitGraphFile
- }
-
- // Read and verify the file header
- var header = make([]byte, 4)
- if _, err := fi.reader.ReadAt(header, 4); err != nil {
- return err
- }
- if header[0] != 1 {
- return ErrUnsupportedVersion
- }
- if header[1] != 1 {
- return ErrUnsupportedHash
- }
-
- return nil
-}
-
-func (fi *fileIndex) readChunkHeaders() error {
- var chunkID = make([]byte, 4)
- for i := 0; ; i++ {
- chunkHeader := io.NewSectionReader(fi.reader, 8+(int64(i)*12), 12)
- if _, err := io.ReadAtLeast(chunkHeader, chunkID, 4); err != nil {
- return err
- }
- chunkOffset, err := binary.ReadUint64(chunkHeader)
- if err != nil {
- return err
- }
-
- if bytes.Equal(chunkID, oidFanoutSignature) {
- fi.oidFanoutOffset = int64(chunkOffset)
- } else if bytes.Equal(chunkID, oidLookupSignature) {
- fi.oidLookupOffset = int64(chunkOffset)
- } else if bytes.Equal(chunkID, commitDataSignature) {
- fi.commitDataOffset = int64(chunkOffset)
- } else if bytes.Equal(chunkID, extraEdgeListSignature) {
- fi.extraEdgeListOffset = int64(chunkOffset)
- } else if bytes.Equal(chunkID, lastSignature) {
- break
- }
- }
-
- if fi.oidFanoutOffset <= 0 || fi.oidLookupOffset <= 0 || fi.commitDataOffset <= 0 {
- return ErrMalformedCommitGraphFile
- }
-
- return nil
-}
-
-func (fi *fileIndex) readFanout() error {
- fanoutReader := io.NewSectionReader(fi.reader, fi.oidFanoutOffset, 256*4)
- for i := 0; i < 256; i++ {
- fanoutValue, err := binary.ReadUint32(fanoutReader)
- if err != nil {
- return err
- }
- if fanoutValue > 0x7fffffff {
- return ErrMalformedCommitGraphFile
- }
- fi.fanout[i] = int(fanoutValue)
- }
- return nil
-}
-
-func (fi *fileIndex) GetIndexByHash(h plumbing.Hash) (int, error) {
- var oid plumbing.Hash
-
- // Find the hash in the oid lookup table
- var low int
- if h[0] == 0 {
- low = 0
- } else {
- low = fi.fanout[h[0]-1]
- }
- high := fi.fanout[h[0]]
- for low < high {
- mid := (low + high) >> 1
- offset := fi.oidLookupOffset + int64(mid)*20
- if _, err := fi.reader.ReadAt(oid[:], offset); err != nil {
- return 0, err
- }
- cmp := bytes.Compare(h[:], oid[:])
- if cmp < 0 {
- high = mid
- } else if cmp == 0 {
- return mid, nil
- } else {
- low = mid + 1
- }
- }
-
- return 0, plumbing.ErrObjectNotFound
-}
-
-func (fi *fileIndex) GetCommitDataByIndex(idx int) (*CommitData, error) {
- if idx >= fi.fanout[0xff] {
- return nil, plumbing.ErrObjectNotFound
- }
-
- offset := fi.commitDataOffset + int64(idx)*36
- commitDataReader := io.NewSectionReader(fi.reader, offset, 36)
-
- treeHash, err := binary.ReadHash(commitDataReader)
- if err != nil {
- return nil, err
- }
- parent1, err := binary.ReadUint32(commitDataReader)
- if err != nil {
- return nil, err
- }
- parent2, err := binary.ReadUint32(commitDataReader)
- if err != nil {
- return nil, err
- }
- genAndTime, err := binary.ReadUint64(commitDataReader)
- if err != nil {
- return nil, err
- }
-
- var parentIndexes []int
- if parent2&parentOctopusUsed == parentOctopusUsed {
- // Octopus merge
- parentIndexes = []int{int(parent1 & parentOctopusMask)}
- offset := fi.extraEdgeListOffset + 4*int64(parent2&parentOctopusMask)
- buf := make([]byte, 4)
- for {
- _, err := fi.reader.ReadAt(buf, offset)
- if err != nil {
- return nil, err
- }
-
- parent := encbin.BigEndian.Uint32(buf)
- offset += 4
- parentIndexes = append(parentIndexes, int(parent&parentOctopusMask))
- if parent&parentLast == parentLast {
- break
- }
- }
- } else if parent2 != parentNone {
- parentIndexes = []int{int(parent1 & parentOctopusMask), int(parent2 & parentOctopusMask)}
- } else if parent1 != parentNone {
- parentIndexes = []int{int(parent1 & parentOctopusMask)}
- }
-
- parentHashes, err := fi.getHashesFromIndexes(parentIndexes)
- if err != nil {
- return nil, err
- }
-
- return &CommitData{
- TreeHash: treeHash,
- ParentIndexes: parentIndexes,
- ParentHashes: parentHashes,
- Generation: int(genAndTime >> 34),
- When: time.Unix(int64(genAndTime&0x3FFFFFFFF), 0),
- }, nil
-}
-
-func (fi *fileIndex) getHashesFromIndexes(indexes []int) ([]plumbing.Hash, error) {
- hashes := make([]plumbing.Hash, len(indexes))
-
- for i, idx := range indexes {
- if idx >= fi.fanout[0xff] {
- return nil, ErrMalformedCommitGraphFile
- }
-
- offset := fi.oidLookupOffset + int64(idx)*20
- if _, err := fi.reader.ReadAt(hashes[i][:], offset); err != nil {
- return nil, err
- }
- }
-
- return hashes, nil
-}
-
-// Hashes returns all the hashes that are available in the index
-func (fi *fileIndex) Hashes() []plumbing.Hash {
- hashes := make([]plumbing.Hash, fi.fanout[0xff])
- for i := 0; i < fi.fanout[0xff]; i++ {
- offset := fi.oidLookupOffset + int64(i)*20
- if n, err := fi.reader.ReadAt(hashes[i][:], offset); err != nil || n < 20 {
- return nil
- }
- }
- return hashes
-}
+package commitgraph\r
+\r
+import (\r
+ "bytes"\r
+ encbin "encoding/binary"\r
+ "errors"\r
+ "io"\r
+ "time"\r
+\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+ "github.com/go-git/go-git/v5/utils/binary"\r
+)\r
+\r
+var (\r
+ // ErrUnsupportedVersion is returned by OpenFileIndex when the commit graph\r
+ // file version is not supported.\r
+ ErrUnsupportedVersion = errors.New("Unsupported version")\r
+ // ErrUnsupportedHash is returned by OpenFileIndex when the commit graph\r
+ // hash function is not supported. Currently only SHA-1 is defined and\r
+ // supported\r
+ ErrUnsupportedHash = errors.New("Unsupported hash algorithm")\r
+ // ErrMalformedCommitGraphFile is returned by OpenFileIndex when the commit\r
+ // graph file is corrupted.\r
+ ErrMalformedCommitGraphFile = errors.New("Malformed commit graph file")\r
+\r
+ commitFileSignature = []byte{'C', 'G', 'P', 'H'}\r
+ oidFanoutSignature = []byte{'O', 'I', 'D', 'F'}\r
+ oidLookupSignature = []byte{'O', 'I', 'D', 'L'}\r
+ commitDataSignature = []byte{'C', 'D', 'A', 'T'}\r
+ extraEdgeListSignature = []byte{'E', 'D', 'G', 'E'}\r
+ lastSignature = []byte{0, 0, 0, 0}\r
+\r
+ parentNone = uint32(0x70000000)\r
+ parentOctopusUsed = uint32(0x80000000)\r
+ parentOctopusMask = uint32(0x7fffffff)\r
+ parentLast = uint32(0x80000000)\r
+)\r
+\r
+type fileIndex struct {\r
+ reader io.ReaderAt\r
+ fanout [256]int\r
+ oidFanoutOffset int64\r
+ oidLookupOffset int64\r
+ commitDataOffset int64\r
+ extraEdgeListOffset int64\r
+}\r
+\r
+// OpenFileIndex opens a serialized commit graph file in the format described at\r
+// https://github.com/git/git/blob/master/Documentation/technical/commit-graph-format.txt\r
+func OpenFileIndex(reader io.ReaderAt) (Index, error) {\r
+ fi := &fileIndex{reader: reader}\r
+\r
+ if err := fi.verifyFileHeader(); err != nil {\r
+ return nil, err\r
+ }\r
+ if err := fi.readChunkHeaders(); err != nil {\r
+ return nil, err\r
+ }\r
+ if err := fi.readFanout(); err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ return fi, nil\r
+}\r
+\r
+func (fi *fileIndex) verifyFileHeader() error {\r
+ // Verify file signature\r
+ var signature = make([]byte, 4)\r
+ if _, err := fi.reader.ReadAt(signature, 0); err != nil {\r
+ return err\r
+ }\r
+ if !bytes.Equal(signature, commitFileSignature) {\r
+ return ErrMalformedCommitGraphFile\r
+ }\r
+\r
+ // Read and verify the file header\r
+ var header = make([]byte, 4)\r
+ if _, err := fi.reader.ReadAt(header, 4); err != nil {\r
+ return err\r
+ }\r
+ if header[0] != 1 {\r
+ return ErrUnsupportedVersion\r
+ }\r
+ if header[1] != 1 {\r
+ return ErrUnsupportedHash\r
+ }\r
+\r
+ return nil\r
+}\r
+\r
+func (fi *fileIndex) readChunkHeaders() error {\r
+ var chunkID = make([]byte, 4)\r
+ for i := 0; ; i++ {\r
+ chunkHeader := io.NewSectionReader(fi.reader, 8+(int64(i)*12), 12)\r
+ if _, err := io.ReadAtLeast(chunkHeader, chunkID, 4); err != nil {\r
+ return err\r
+ }\r
+ chunkOffset, err := binary.ReadUint64(chunkHeader)\r
+ if err != nil {\r
+ return err\r
+ }\r
+\r
+ if bytes.Equal(chunkID, oidFanoutSignature) {\r
+ fi.oidFanoutOffset = int64(chunkOffset)\r
+ } else if bytes.Equal(chunkID, oidLookupSignature) {\r
+ fi.oidLookupOffset = int64(chunkOffset)\r
+ } else if bytes.Equal(chunkID, commitDataSignature) {\r
+ fi.commitDataOffset = int64(chunkOffset)\r
+ } else if bytes.Equal(chunkID, extraEdgeListSignature) {\r
+ fi.extraEdgeListOffset = int64(chunkOffset)\r
+ } else if bytes.Equal(chunkID, lastSignature) {\r
+ break\r
+ }\r
+ }\r
+\r
+ if fi.oidFanoutOffset <= 0 || fi.oidLookupOffset <= 0 || fi.commitDataOffset <= 0 {\r
+ return ErrMalformedCommitGraphFile\r
+ }\r
+\r
+ return nil\r
+}\r
+\r
+func (fi *fileIndex) readFanout() error {\r
+ fanoutReader := io.NewSectionReader(fi.reader, fi.oidFanoutOffset, 256*4)\r
+ for i := 0; i < 256; i++ {\r
+ fanoutValue, err := binary.ReadUint32(fanoutReader)\r
+ if err != nil {\r
+ return err\r
+ }\r
+ if fanoutValue > 0x7fffffff {\r
+ return ErrMalformedCommitGraphFile\r
+ }\r
+ fi.fanout[i] = int(fanoutValue)\r
+ }\r
+ return nil\r
+}\r
+\r
+func (fi *fileIndex) GetIndexByHash(h plumbing.Hash) (int, error) {\r
+ var oid plumbing.Hash\r
+\r
+ // Find the hash in the oid lookup table\r
+ var low int\r
+ if h[0] == 0 {\r
+ low = 0\r
+ } else {\r
+ low = fi.fanout[h[0]-1]\r
+ }\r
+ high := fi.fanout[h[0]]\r
+ for low < high {\r
+ mid := (low + high) >> 1\r
+ offset := fi.oidLookupOffset + int64(mid)*20\r
+ if _, err := fi.reader.ReadAt(oid[:], offset); err != nil {\r
+ return 0, err\r
+ }\r
+ cmp := bytes.Compare(h[:], oid[:])\r
+ if cmp < 0 {\r
+ high = mid\r
+ } else if cmp == 0 {\r
+ return mid, nil\r
+ } else {\r
+ low = mid + 1\r
+ }\r
+ }\r
+\r
+ return 0, plumbing.ErrObjectNotFound\r
+}\r
+\r
+func (fi *fileIndex) GetCommitDataByIndex(idx int) (*CommitData, error) {\r
+ if idx >= fi.fanout[0xff] {\r
+ return nil, plumbing.ErrObjectNotFound\r
+ }\r
+\r
+ offset := fi.commitDataOffset + int64(idx)*36\r
+ commitDataReader := io.NewSectionReader(fi.reader, offset, 36)\r
+\r
+ treeHash, err := binary.ReadHash(commitDataReader)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+ parent1, err := binary.ReadUint32(commitDataReader)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+ parent2, err := binary.ReadUint32(commitDataReader)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+ genAndTime, err := binary.ReadUint64(commitDataReader)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ var parentIndexes []int\r
+ if parent2&parentOctopusUsed == parentOctopusUsed {\r
+ // Octopus merge\r
+ parentIndexes = []int{int(parent1 & parentOctopusMask)}\r
+ offset := fi.extraEdgeListOffset + 4*int64(parent2&parentOctopusMask)\r
+ buf := make([]byte, 4)\r
+ for {\r
+ _, err := fi.reader.ReadAt(buf, offset)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ parent := encbin.BigEndian.Uint32(buf)\r
+ offset += 4\r
+ parentIndexes = append(parentIndexes, int(parent&parentOctopusMask))\r
+ if parent&parentLast == parentLast {\r
+ break\r
+ }\r
+ }\r
+ } else if parent2 != parentNone {\r
+ parentIndexes = []int{int(parent1 & parentOctopusMask), int(parent2 & parentOctopusMask)}\r
+ } else if parent1 != parentNone {\r
+ parentIndexes = []int{int(parent1 & parentOctopusMask)}\r
+ }\r
+\r
+ parentHashes, err := fi.getHashesFromIndexes(parentIndexes)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ return &CommitData{\r
+ TreeHash: treeHash,\r
+ ParentIndexes: parentIndexes,\r
+ ParentHashes: parentHashes,\r
+ Generation: int(genAndTime >> 34),\r
+ When: time.Unix(int64(genAndTime&0x3FFFFFFFF), 0),\r
+ }, nil\r
+}\r
+\r
+func (fi *fileIndex) getHashesFromIndexes(indexes []int) ([]plumbing.Hash, error) {\r
+ hashes := make([]plumbing.Hash, len(indexes))\r
+\r
+ for i, idx := range indexes {\r
+ if idx >= fi.fanout[0xff] {\r
+ return nil, ErrMalformedCommitGraphFile\r
+ }\r
+\r
+ offset := fi.oidLookupOffset + int64(idx)*20\r
+ if _, err := fi.reader.ReadAt(hashes[i][:], offset); err != nil {\r
+ return nil, err\r
+ }\r
+ }\r
+\r
+ return hashes, nil\r
+}\r
+\r
+// Hashes returns all the hashes that are available in the index\r
+func (fi *fileIndex) Hashes() []plumbing.Hash {\r
+ hashes := make([]plumbing.Hash, fi.fanout[0xff])\r
+ for i := 0; i < fi.fanout[0xff]; i++ {\r
+ offset := fi.oidLookupOffset + int64(i)*20\r
+ if n, err := fi.reader.ReadAt(hashes[i][:], offset); err != nil || n < 20 {\r
+ return nil\r
+ }\r
+ }\r
+ return hashes\r
+}\r
-package commitgraph
-
-import (
- "github.com/go-git/go-git/v5/plumbing"
-)
-
-// MemoryIndex provides a way to build the commit-graph in memory
-// for later encoding to file.
-type MemoryIndex struct {
- commitData []*CommitData
- indexMap map[plumbing.Hash]int
-}
-
-// NewMemoryIndex creates in-memory commit graph representation
-func NewMemoryIndex() *MemoryIndex {
- return &MemoryIndex{
- indexMap: make(map[plumbing.Hash]int),
- }
-}
-
-// GetIndexByHash gets the index in the commit graph from commit hash, if available
-func (mi *MemoryIndex) GetIndexByHash(h plumbing.Hash) (int, error) {
- i, ok := mi.indexMap[h]
- if ok {
- return i, nil
- }
-
- return 0, plumbing.ErrObjectNotFound
-}
-
-// GetCommitDataByIndex gets the commit node from the commit graph using index
-// obtained from child node, if available
-func (mi *MemoryIndex) GetCommitDataByIndex(i int) (*CommitData, error) {
- if i >= len(mi.commitData) {
- return nil, plumbing.ErrObjectNotFound
- }
-
- commitData := mi.commitData[i]
-
- // Map parent hashes to parent indexes
- if commitData.ParentIndexes == nil {
- parentIndexes := make([]int, len(commitData.ParentHashes))
- for i, parentHash := range commitData.ParentHashes {
- var err error
- if parentIndexes[i], err = mi.GetIndexByHash(parentHash); err != nil {
- return nil, err
- }
- }
- commitData.ParentIndexes = parentIndexes
- }
-
- return commitData, nil
-}
-
-// Hashes returns all the hashes that are available in the index
-func (mi *MemoryIndex) Hashes() []plumbing.Hash {
- hashes := make([]plumbing.Hash, 0, len(mi.indexMap))
- for k := range mi.indexMap {
- hashes = append(hashes, k)
- }
- return hashes
-}
-
-// Add adds new node to the memory index
-func (mi *MemoryIndex) Add(hash plumbing.Hash, commitData *CommitData) {
- // The parent indexes are calculated lazily in GetNodeByIndex
- // which allows adding nodes out of order as long as all parents
- // are eventually resolved
- commitData.ParentIndexes = nil
- mi.indexMap[hash] = len(mi.commitData)
- mi.commitData = append(mi.commitData, commitData)
-}
+package commitgraph\r
+\r
+import (\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+)\r
+\r
+// MemoryIndex provides a way to build the commit-graph in memory\r
+// for later encoding to file.\r
+type MemoryIndex struct {\r
+ commitData []*CommitData\r
+ indexMap map[plumbing.Hash]int\r
+}\r
+\r
+// NewMemoryIndex creates in-memory commit graph representation\r
+func NewMemoryIndex() *MemoryIndex {\r
+ return &MemoryIndex{\r
+ indexMap: make(map[plumbing.Hash]int),\r
+ }\r
+}\r
+\r
+// GetIndexByHash gets the index in the commit graph from commit hash, if available\r
+func (mi *MemoryIndex) GetIndexByHash(h plumbing.Hash) (int, error) {\r
+ i, ok := mi.indexMap[h]\r
+ if ok {\r
+ return i, nil\r
+ }\r
+\r
+ return 0, plumbing.ErrObjectNotFound\r
+}\r
+\r
+// GetCommitDataByIndex gets the commit node from the commit graph using index\r
+// obtained from child node, if available\r
+func (mi *MemoryIndex) GetCommitDataByIndex(i int) (*CommitData, error) {\r
+ if i >= len(mi.commitData) {\r
+ return nil, plumbing.ErrObjectNotFound\r
+ }\r
+\r
+ commitData := mi.commitData[i]\r
+\r
+ // Map parent hashes to parent indexes\r
+ if commitData.ParentIndexes == nil {\r
+ parentIndexes := make([]int, len(commitData.ParentHashes))\r
+ for i, parentHash := range commitData.ParentHashes {\r
+ var err error\r
+ if parentIndexes[i], err = mi.GetIndexByHash(parentHash); err != nil {\r
+ return nil, err\r
+ }\r
+ }\r
+ commitData.ParentIndexes = parentIndexes\r
+ }\r
+\r
+ return commitData, nil\r
+}\r
+\r
+// Hashes returns all the hashes that are available in the index\r
+func (mi *MemoryIndex) Hashes() []plumbing.Hash {\r
+ hashes := make([]plumbing.Hash, 0, len(mi.indexMap))\r
+ for k := range mi.indexMap {\r
+ hashes = append(hashes, k)\r
+ }\r
+ return hashes\r
+}\r
+\r
+// Add adds new node to the memory index\r
+func (mi *MemoryIndex) Add(hash plumbing.Hash, commitData *CommitData) {\r
+ // The parent indexes are calculated lazily in GetNodeByIndex\r
+ // which allows adding nodes out of order as long as all parents\r
+ // are eventually resolved\r
+ commitData.ParentIndexes = nil\r
+ mi.indexMap[hash] = len(mi.commitData)\r
+ mi.commitData = append(mi.commitData, commitData)\r
+}\r
-package commitgraph
-
-import (
- "io"
- "time"
-
- "github.com/go-git/go-git/v5/plumbing"
- "github.com/go-git/go-git/v5/plumbing/object"
- "github.com/go-git/go-git/v5/plumbing/storer"
-)
-
-// CommitNode is generic interface encapsulating a lightweight commit object retrieved
-// from CommitNodeIndex
-type CommitNode interface {
- // ID returns the Commit object id referenced by the commit graph node.
- ID() plumbing.Hash
- // Tree returns the Tree referenced by the commit graph node.
- Tree() (*object.Tree, error)
- // CommitTime returns the Commiter.When time of the Commit referenced by the commit graph node.
- CommitTime() time.Time
- // NumParents returns the number of parents in a commit.
- NumParents() int
- // ParentNodes return a CommitNodeIter for parents of specified node.
- ParentNodes() CommitNodeIter
- // ParentNode returns the ith parent of a commit.
- ParentNode(i int) (CommitNode, error)
- // ParentHashes returns hashes of the parent commits for a specified node
- ParentHashes() []plumbing.Hash
- // Generation returns the generation of the commit for reachability analysis.
- // Objects with newer generation are not reachable from objects of older generation.
- Generation() uint64
- // Commit returns the full commit object from the node
- Commit() (*object.Commit, error)
-}
-
-// CommitNodeIndex is generic interface encapsulating an index of CommitNode objects
-type CommitNodeIndex interface {
- // Get returns a commit node from a commit hash
- Get(hash plumbing.Hash) (CommitNode, error)
-}
-
-// CommitNodeIter is a generic closable interface for iterating over commit nodes.
-type CommitNodeIter interface {
- Next() (CommitNode, error)
- ForEach(func(CommitNode) error) error
- Close()
-}
-
-// parentCommitNodeIter provides an iterator for parent commits from associated CommitNodeIndex.
-type parentCommitNodeIter struct {
- node CommitNode
- i int
-}
-
-func newParentgraphCommitNodeIter(node CommitNode) CommitNodeIter {
- return &parentCommitNodeIter{node, 0}
-}
-
-// Next moves the iterator to the next commit and returns a pointer to it. If
-// there are no more commits, it returns io.EOF.
-func (iter *parentCommitNodeIter) Next() (CommitNode, error) {
- obj, err := iter.node.ParentNode(iter.i)
- if err == object.ErrParentNotFound {
- return nil, io.EOF
- }
- if err == nil {
- iter.i++
- }
-
- return obj, err
-}
-
-// ForEach call the cb function for each commit contained on this iter until
-// an error appends or the end of the iter is reached. If ErrStop is sent
-// the iteration is stopped but no error is returned. The iterator is closed.
-func (iter *parentCommitNodeIter) ForEach(cb func(CommitNode) error) error {
- for {
- obj, err := iter.Next()
- if err != nil {
- if err == io.EOF {
- return nil
- }
-
- return err
- }
-
- if err := cb(obj); err != nil {
- if err == storer.ErrStop {
- return nil
- }
-
- return err
- }
- }
-}
-
-func (iter *parentCommitNodeIter) Close() {
-}
+package commitgraph\r
+\r
+import (\r
+ "io"\r
+ "time"\r
+\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+ "github.com/go-git/go-git/v5/plumbing/object"\r
+ "github.com/go-git/go-git/v5/plumbing/storer"\r
+)\r
+\r
+// CommitNode is generic interface encapsulating a lightweight commit object retrieved\r
+// from CommitNodeIndex\r
+type CommitNode interface {\r
+ // ID returns the Commit object id referenced by the commit graph node.\r
+ ID() plumbing.Hash\r
+ // Tree returns the Tree referenced by the commit graph node.\r
+ Tree() (*object.Tree, error)\r
+ // CommitTime returns the Commiter.When time of the Commit referenced by the commit graph node.\r
+ CommitTime() time.Time\r
+ // NumParents returns the number of parents in a commit.\r
+ NumParents() int\r
+ // ParentNodes return a CommitNodeIter for parents of specified node.\r
+ ParentNodes() CommitNodeIter\r
+ // ParentNode returns the ith parent of a commit.\r
+ ParentNode(i int) (CommitNode, error)\r
+ // ParentHashes returns hashes of the parent commits for a specified node\r
+ ParentHashes() []plumbing.Hash\r
+ // Generation returns the generation of the commit for reachability analysis.\r
+ // Objects with newer generation are not reachable from objects of older generation.\r
+ Generation() uint64\r
+ // Commit returns the full commit object from the node\r
+ Commit() (*object.Commit, error)\r
+}\r
+\r
+// CommitNodeIndex is generic interface encapsulating an index of CommitNode objects\r
+type CommitNodeIndex interface {\r
+ // Get returns a commit node from a commit hash\r
+ Get(hash plumbing.Hash) (CommitNode, error)\r
+}\r
+\r
+// CommitNodeIter is a generic closable interface for iterating over commit nodes.\r
+type CommitNodeIter interface {\r
+ Next() (CommitNode, error)\r
+ ForEach(func(CommitNode) error) error\r
+ Close()\r
+}\r
+\r
+// parentCommitNodeIter provides an iterator for parent commits from associated CommitNodeIndex.\r
+type parentCommitNodeIter struct {\r
+ node CommitNode\r
+ i int\r
+}\r
+\r
+func newParentgraphCommitNodeIter(node CommitNode) CommitNodeIter {\r
+ return &parentCommitNodeIter{node, 0}\r
+}\r
+\r
+// Next moves the iterator to the next commit and returns a pointer to it. If\r
+// there are no more commits, it returns io.EOF.\r
+func (iter *parentCommitNodeIter) Next() (CommitNode, error) {\r
+ obj, err := iter.node.ParentNode(iter.i)\r
+ if err == object.ErrParentNotFound {\r
+ return nil, io.EOF\r
+ }\r
+ if err == nil {\r
+ iter.i++\r
+ }\r
+\r
+ return obj, err\r
+}\r
+\r
+// ForEach call the cb function for each commit contained on this iter until\r
+// an error appends or the end of the iter is reached. If ErrStop is sent\r
+// the iteration is stopped but no error is returned. The iterator is closed.\r
+func (iter *parentCommitNodeIter) ForEach(cb func(CommitNode) error) error {\r
+ for {\r
+ obj, err := iter.Next()\r
+ if err != nil {\r
+ if err == io.EOF {\r
+ return nil\r
+ }\r
+\r
+ return err\r
+ }\r
+\r
+ if err := cb(obj); err != nil {\r
+ if err == storer.ErrStop {\r
+ return nil\r
+ }\r
+\r
+ return err\r
+ }\r
+ }\r
+}\r
+\r
+func (iter *parentCommitNodeIter) Close() {\r
+}\r
-package commitgraph
-
-import (
- "fmt"
- "time"
-
- "github.com/go-git/go-git/v5/plumbing"
- "github.com/go-git/go-git/v5/plumbing/format/commitgraph"
- "github.com/go-git/go-git/v5/plumbing/object"
- "github.com/go-git/go-git/v5/plumbing/storer"
-)
-
-// graphCommitNode is a reduced representation of Commit as presented in the commit
-// graph file (commitgraph.Node). It is merely useful as an optimization for walking
-// the commit graphs.
-//
-// graphCommitNode implements the CommitNode interface.
-type graphCommitNode struct {
- // Hash for the Commit object
- hash plumbing.Hash
- // Index of the node in the commit graph file
- index int
-
- commitData *commitgraph.CommitData
- gci *graphCommitNodeIndex
-}
-
-// graphCommitNodeIndex is an index that can load CommitNode objects from both the commit
-// graph files and the object store.
-//
-// graphCommitNodeIndex implements the CommitNodeIndex interface
-type graphCommitNodeIndex struct {
- commitGraph commitgraph.Index
- s storer.EncodedObjectStorer
-}
-
-// NewGraphCommitNodeIndex returns CommitNodeIndex implementation that uses commit-graph
-// files as backing storage and falls back to object storage when necessary
-func NewGraphCommitNodeIndex(commitGraph commitgraph.Index, s storer.EncodedObjectStorer) CommitNodeIndex {
- return &graphCommitNodeIndex{commitGraph, s}
-}
-
-func (gci *graphCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) {
- // Check the commit graph first
- parentIndex, err := gci.commitGraph.GetIndexByHash(hash)
- if err == nil {
- parent, err := gci.commitGraph.GetCommitDataByIndex(parentIndex)
- if err != nil {
- return nil, err
- }
-
- return &graphCommitNode{
- hash: hash,
- index: parentIndex,
- commitData: parent,
- gci: gci,
- }, nil
- }
-
- // Fallback to loading full commit object
- commit, err := object.GetCommit(gci.s, hash)
- if err != nil {
- return nil, err
- }
-
- return &objectCommitNode{
- nodeIndex: gci,
- commit: commit,
- }, nil
-}
-
-func (c *graphCommitNode) ID() plumbing.Hash {
- return c.hash
-}
-
-func (c *graphCommitNode) Tree() (*object.Tree, error) {
- return object.GetTree(c.gci.s, c.commitData.TreeHash)
-}
-
-func (c *graphCommitNode) CommitTime() time.Time {
- return c.commitData.When
-}
-
-func (c *graphCommitNode) NumParents() int {
- return len(c.commitData.ParentIndexes)
-}
-
-func (c *graphCommitNode) ParentNodes() CommitNodeIter {
- return newParentgraphCommitNodeIter(c)
-}
-
-func (c *graphCommitNode) ParentNode(i int) (CommitNode, error) {
- if i < 0 || i >= len(c.commitData.ParentIndexes) {
- return nil, object.ErrParentNotFound
- }
-
- parent, err := c.gci.commitGraph.GetCommitDataByIndex(c.commitData.ParentIndexes[i])
- if err != nil {
- return nil, err
- }
-
- return &graphCommitNode{
- hash: c.commitData.ParentHashes[i],
- index: c.commitData.ParentIndexes[i],
- commitData: parent,
- gci: c.gci,
- }, nil
-}
-
-func (c *graphCommitNode) ParentHashes() []plumbing.Hash {
- return c.commitData.ParentHashes
-}
-
-func (c *graphCommitNode) Generation() uint64 {
- // If the commit-graph file was generated with older Git version that
- // set the generation to zero for every commit the generation assumption
- // is still valid. It is just less useful.
- return uint64(c.commitData.Generation)
-}
-
-func (c *graphCommitNode) Commit() (*object.Commit, error) {
- return object.GetCommit(c.gci.s, c.hash)
-}
-
-func (c *graphCommitNode) String() string {
- return fmt.Sprintf(
- "%s %s\nDate: %s",
- plumbing.CommitObject, c.ID(),
- c.CommitTime().Format(object.DateFormat),
- )
-}
+package commitgraph\r
+\r
+import (\r
+ "fmt"\r
+ "time"\r
+\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+ "github.com/go-git/go-git/v5/plumbing/format/commitgraph"\r
+ "github.com/go-git/go-git/v5/plumbing/object"\r
+ "github.com/go-git/go-git/v5/plumbing/storer"\r
+)\r
+\r
+// graphCommitNode is a reduced representation of Commit as presented in the commit\r
+// graph file (commitgraph.Node). It is merely useful as an optimization for walking\r
+// the commit graphs.\r
+//\r
+// graphCommitNode implements the CommitNode interface.\r
+type graphCommitNode struct {\r
+ // Hash for the Commit object\r
+ hash plumbing.Hash\r
+ // Index of the node in the commit graph file\r
+ index int\r
+\r
+ commitData *commitgraph.CommitData\r
+ gci *graphCommitNodeIndex\r
+}\r
+\r
+// graphCommitNodeIndex is an index that can load CommitNode objects from both the commit\r
+// graph files and the object store.\r
+//\r
+// graphCommitNodeIndex implements the CommitNodeIndex interface\r
+type graphCommitNodeIndex struct {\r
+ commitGraph commitgraph.Index\r
+ s storer.EncodedObjectStorer\r
+}\r
+\r
+// NewGraphCommitNodeIndex returns CommitNodeIndex implementation that uses commit-graph\r
+// files as backing storage and falls back to object storage when necessary\r
+func NewGraphCommitNodeIndex(commitGraph commitgraph.Index, s storer.EncodedObjectStorer) CommitNodeIndex {\r
+ return &graphCommitNodeIndex{commitGraph, s}\r
+}\r
+\r
+func (gci *graphCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) {\r
+ // Check the commit graph first\r
+ parentIndex, err := gci.commitGraph.GetIndexByHash(hash)\r
+ if err == nil {\r
+ parent, err := gci.commitGraph.GetCommitDataByIndex(parentIndex)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ return &graphCommitNode{\r
+ hash: hash,\r
+ index: parentIndex,\r
+ commitData: parent,\r
+ gci: gci,\r
+ }, nil\r
+ }\r
+\r
+ // Fallback to loading full commit object\r
+ commit, err := object.GetCommit(gci.s, hash)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ return &objectCommitNode{\r
+ nodeIndex: gci,\r
+ commit: commit,\r
+ }, nil\r
+}\r
+\r
+func (c *graphCommitNode) ID() plumbing.Hash {\r
+ return c.hash\r
+}\r
+\r
+func (c *graphCommitNode) Tree() (*object.Tree, error) {\r
+ return object.GetTree(c.gci.s, c.commitData.TreeHash)\r
+}\r
+\r
+func (c *graphCommitNode) CommitTime() time.Time {\r
+ return c.commitData.When\r
+}\r
+\r
+func (c *graphCommitNode) NumParents() int {\r
+ return len(c.commitData.ParentIndexes)\r
+}\r
+\r
+func (c *graphCommitNode) ParentNodes() CommitNodeIter {\r
+ return newParentgraphCommitNodeIter(c)\r
+}\r
+\r
+func (c *graphCommitNode) ParentNode(i int) (CommitNode, error) {\r
+ if i < 0 || i >= len(c.commitData.ParentIndexes) {\r
+ return nil, object.ErrParentNotFound\r
+ }\r
+\r
+ parent, err := c.gci.commitGraph.GetCommitDataByIndex(c.commitData.ParentIndexes[i])\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ return &graphCommitNode{\r
+ hash: c.commitData.ParentHashes[i],\r
+ index: c.commitData.ParentIndexes[i],\r
+ commitData: parent,\r
+ gci: c.gci,\r
+ }, nil\r
+}\r
+\r
+func (c *graphCommitNode) ParentHashes() []plumbing.Hash {\r
+ return c.commitData.ParentHashes\r
+}\r
+\r
+func (c *graphCommitNode) Generation() uint64 {\r
+ // If the commit-graph file was generated with older Git version that\r
+ // set the generation to zero for every commit the generation assumption\r
+ // is still valid. It is just less useful.\r
+ return uint64(c.commitData.Generation)\r
+}\r
+\r
+func (c *graphCommitNode) Commit() (*object.Commit, error) {\r
+ return object.GetCommit(c.gci.s, c.hash)\r
+}\r
+\r
+func (c *graphCommitNode) String() string {\r
+ return fmt.Sprintf(\r
+ "%s %s\nDate: %s",\r
+ plumbing.CommitObject, c.ID(),\r
+ c.CommitTime().Format(object.DateFormat),\r
+ )\r
+}\r
-package commitgraph
-
-import (
- "math"
- "time"
-
- "github.com/go-git/go-git/v5/plumbing"
- "github.com/go-git/go-git/v5/plumbing/object"
- "github.com/go-git/go-git/v5/plumbing/storer"
-)
-
-// objectCommitNode is a representation of Commit as presented in the GIT object format.
-//
-// objectCommitNode implements the CommitNode interface.
-type objectCommitNode struct {
- nodeIndex CommitNodeIndex
- commit *object.Commit
-}
-
-// NewObjectCommitNodeIndex returns CommitNodeIndex implementation that uses
-// only object storage to load the nodes
-func NewObjectCommitNodeIndex(s storer.EncodedObjectStorer) CommitNodeIndex {
- return &objectCommitNodeIndex{s}
-}
-
-func (oci *objectCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) {
- commit, err := object.GetCommit(oci.s, hash)
- if err != nil {
- return nil, err
- }
-
- return &objectCommitNode{
- nodeIndex: oci,
- commit: commit,
- }, nil
-}
-
-// objectCommitNodeIndex is an index that can load CommitNode objects only from the
-// object store.
-//
-// objectCommitNodeIndex implements the CommitNodeIndex interface
-type objectCommitNodeIndex struct {
- s storer.EncodedObjectStorer
-}
-
-func (c *objectCommitNode) CommitTime() time.Time {
- return c.commit.Committer.When
-}
-
-func (c *objectCommitNode) ID() plumbing.Hash {
- return c.commit.ID()
-}
-
-func (c *objectCommitNode) Tree() (*object.Tree, error) {
- return c.commit.Tree()
-}
-
-func (c *objectCommitNode) NumParents() int {
- return c.commit.NumParents()
-}
-
-func (c *objectCommitNode) ParentNodes() CommitNodeIter {
- return newParentgraphCommitNodeIter(c)
-}
-
-func (c *objectCommitNode) ParentNode(i int) (CommitNode, error) {
- if i < 0 || i >= len(c.commit.ParentHashes) {
- return nil, object.ErrParentNotFound
- }
-
- // Note: It's necessary to go through CommitNodeIndex here to ensure
- // that if the commit-graph file covers only part of the history we
- // start using it when that part is reached.
- return c.nodeIndex.Get(c.commit.ParentHashes[i])
-}
-
-func (c *objectCommitNode) ParentHashes() []plumbing.Hash {
- return c.commit.ParentHashes
-}
-
-func (c *objectCommitNode) Generation() uint64 {
- // Commit nodes representing objects outside of the commit graph can never
- // be reached by objects from the commit-graph thus we return the highest
- // possible value.
- return math.MaxUint64
-}
-
-func (c *objectCommitNode) Commit() (*object.Commit, error) {
- return c.commit, nil
-}
+package commitgraph\r
+\r
+import (\r
+ "math"\r
+ "time"\r
+\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+ "github.com/go-git/go-git/v5/plumbing/object"\r
+ "github.com/go-git/go-git/v5/plumbing/storer"\r
+)\r
+\r
+// objectCommitNode is a representation of Commit as presented in the GIT object format.\r
+//\r
+// objectCommitNode implements the CommitNode interface.\r
+type objectCommitNode struct {\r
+ nodeIndex CommitNodeIndex\r
+ commit *object.Commit\r
+}\r
+\r
+// NewObjectCommitNodeIndex returns CommitNodeIndex implementation that uses\r
+// only object storage to load the nodes\r
+func NewObjectCommitNodeIndex(s storer.EncodedObjectStorer) CommitNodeIndex {\r
+ return &objectCommitNodeIndex{s}\r
+}\r
+\r
+func (oci *objectCommitNodeIndex) Get(hash plumbing.Hash) (CommitNode, error) {\r
+ commit, err := object.GetCommit(oci.s, hash)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+\r
+ return &objectCommitNode{\r
+ nodeIndex: oci,\r
+ commit: commit,\r
+ }, nil\r
+}\r
+\r
+// objectCommitNodeIndex is an index that can load CommitNode objects only from the\r
+// object store.\r
+//\r
+// objectCommitNodeIndex implements the CommitNodeIndex interface\r
+type objectCommitNodeIndex struct {\r
+ s storer.EncodedObjectStorer\r
+}\r
+\r
+func (c *objectCommitNode) CommitTime() time.Time {\r
+ return c.commit.Committer.When\r
+}\r
+\r
+func (c *objectCommitNode) ID() plumbing.Hash {\r
+ return c.commit.ID()\r
+}\r
+\r
+func (c *objectCommitNode) Tree() (*object.Tree, error) {\r
+ return c.commit.Tree()\r
+}\r
+\r
+func (c *objectCommitNode) NumParents() int {\r
+ return c.commit.NumParents()\r
+}\r
+\r
+func (c *objectCommitNode) ParentNodes() CommitNodeIter {\r
+ return newParentgraphCommitNodeIter(c)\r
+}\r
+\r
+func (c *objectCommitNode) ParentNode(i int) (CommitNode, error) {\r
+ if i < 0 || i >= len(c.commit.ParentHashes) {\r
+ return nil, object.ErrParentNotFound\r
+ }\r
+\r
+ // Note: It's necessary to go through CommitNodeIndex here to ensure\r
+ // that if the commit-graph file covers only part of the history we\r
+ // start using it when that part is reached.\r
+ return c.nodeIndex.Get(c.commit.ParentHashes[i])\r
+}\r
+\r
+func (c *objectCommitNode) ParentHashes() []plumbing.Hash {\r
+ return c.commit.ParentHashes\r
+}\r
+\r
+func (c *objectCommitNode) Generation() uint64 {\r
+ // Commit nodes representing objects outside of the commit graph can never\r
+ // be reached by objects from the commit-graph thus we return the highest\r
+ // possible value.\r
+ return math.MaxUint64\r
+}\r
+\r
+func (c *objectCommitNode) Commit() (*object.Commit, error) {\r
+ return c.commit, nil\r
+}\r
-package commitgraph
-
-import (
- "io"
-
- "github.com/emirpasic/gods/trees/binaryheap"
-
- "github.com/go-git/go-git/v5/plumbing"
- "github.com/go-git/go-git/v5/plumbing/storer"
-)
-
-type commitNodeIteratorByCTime struct {
- heap *binaryheap.Heap
- seenExternal map[plumbing.Hash]bool
- seen map[plumbing.Hash]bool
-}
-
-// NewCommitNodeIterCTime returns a CommitNodeIter that walks the commit history,
-// starting at the given commit and visiting its parents while preserving Committer Time order.
-// this appears to be the closest order to `git log`
-// The given callback will be called for each visited commit. Each commit will
-// be visited only once. If the callback returns an error, walking will stop
-// and will return the error. Other errors might be returned if the history
-// cannot be traversed (e.g. missing objects). Ignore allows to skip some
-// commits from being iterated.
-func NewCommitNodeIterCTime(
- c CommitNode,
- seenExternal map[plumbing.Hash]bool,
- ignore []plumbing.Hash,
-) CommitNodeIter {
- seen := make(map[plumbing.Hash]bool)
- for _, h := range ignore {
- seen[h] = true
- }
-
- heap := binaryheap.NewWith(func(a, b interface{}) int {
- if a.(CommitNode).CommitTime().Before(b.(CommitNode).CommitTime()) {
- return 1
- }
- return -1
- })
-
- heap.Push(c)
-
- return &commitNodeIteratorByCTime{
- heap: heap,
- seenExternal: seenExternal,
- seen: seen,
- }
-}
-
-func (w *commitNodeIteratorByCTime) Next() (CommitNode, error) {
- var c CommitNode
- for {
- cIn, ok := w.heap.Pop()
- if !ok {
- return nil, io.EOF
- }
- c = cIn.(CommitNode)
- cID := c.ID()
-
- if w.seen[cID] || w.seenExternal[cID] {
- continue
- }
-
- w.seen[cID] = true
-
- for i, h := range c.ParentHashes() {
- if w.seen[h] || w.seenExternal[h] {
- continue
- }
- pc, err := c.ParentNode(i)
- if err != nil {
- return nil, err
- }
- w.heap.Push(pc)
- }
-
- return c, nil
- }
-}
-
-func (w *commitNodeIteratorByCTime) ForEach(cb func(CommitNode) error) error {
- for {
- c, err := w.Next()
- if err == io.EOF {
- break
- }
- if err != nil {
- return err
- }
-
- err = cb(c)
- if err == storer.ErrStop {
- break
- }
- if err != nil {
- return err
- }
- }
-
- return nil
-}
-
-func (w *commitNodeIteratorByCTime) Close() {}
+package commitgraph\r
+\r
+import (\r
+ "io"\r
+\r
+ "github.com/emirpasic/gods/trees/binaryheap"\r
+\r
+ "github.com/go-git/go-git/v5/plumbing"\r
+ "github.com/go-git/go-git/v5/plumbing/storer"\r
+)\r
+\r
+type commitNodeIteratorByCTime struct {\r
+ heap *binaryheap.Heap\r
+ seenExternal map[plumbing.Hash]bool\r
+ seen map[plumbing.Hash]bool\r
+}\r
+\r
+// NewCommitNodeIterCTime returns a CommitNodeIter that walks the commit history,\r
+// starting at the given commit and visiting its parents while preserving Committer Time order.\r
+// this appears to be the closest order to `git log`\r
+// The given callback will be called for each visited commit. Each commit will\r
+// be visited only once. If the callback returns an error, walking will stop\r
+// and will return the error. Other errors might be returned if the history\r
+// cannot be traversed (e.g. missing objects). Ignore allows to skip some\r
+// commits from being iterated.\r
+func NewCommitNodeIterCTime(\r
+ c CommitNode,\r
+ seenExternal map[plumbing.Hash]bool,\r
+ ignore []plumbing.Hash,\r
+) CommitNodeIter {\r
+ seen := make(map[plumbing.Hash]bool)\r
+ for _, h := range ignore {\r
+ seen[h] = true\r
+ }\r
+\r
+ heap := binaryheap.NewWith(func(a, b interface{}) int {\r
+ if a.(CommitNode).CommitTime().Before(b.(CommitNode).CommitTime()) {\r
+ return 1\r
+ }\r
+ return -1\r
+ })\r
+\r
+ heap.Push(c)\r
+\r
+ return &commitNodeIteratorByCTime{\r
+ heap: heap,\r
+ seenExternal: seenExternal,\r
+ seen: seen,\r
+ }\r
+}\r
+\r
+func (w *commitNodeIteratorByCTime) Next() (CommitNode, error) {\r
+ var c CommitNode\r
+ for {\r
+ cIn, ok := w.heap.Pop()\r
+ if !ok {\r
+ return nil, io.EOF\r
+ }\r
+ c = cIn.(CommitNode)\r
+ cID := c.ID()\r
+\r
+ if w.seen[cID] || w.seenExternal[cID] {\r
+ continue\r
+ }\r
+\r
+ w.seen[cID] = true\r
+\r
+ for i, h := range c.ParentHashes() {\r
+ if w.seen[h] || w.seenExternal[h] {\r
+ continue\r
+ }\r
+ pc, err := c.ParentNode(i)\r
+ if err != nil {\r
+ return nil, err\r
+ }\r
+ w.heap.Push(pc)\r
+ }\r
+\r
+ return c, nil\r
+ }\r
+}\r
+\r
+func (w *commitNodeIteratorByCTime) ForEach(cb func(CommitNode) error) error {\r
+ for {\r
+ c, err := w.Next()\r
+ if err == io.EOF {\r
+ break\r
+ }\r
+ if err != nil {\r
+ return err\r
+ }\r
+\r
+ err = cb(c)\r
+ if err == storer.ErrStop {\r
+ break\r
+ }\r
+ if err != nil {\r
+ return err\r
+ }\r
+ }\r
+\r
+ return nil\r
+}\r
+\r
+func (w *commitNodeIteratorByCTime) Close() {}\r
-Copyright (c) 2013 Caleb Spare
-
-MIT License
-
-Permission is hereby granted, free of charge, to any person obtaining
-a copy of this software and associated documentation files (the
-"Software"), to deal in the Software without restriction, including
-without limitation the rights to use, copy, modify, merge, publish,
-distribute, sublicense, and/or sell copies of the Software, and to
-permit persons to whom the Software is furnished to do so, subject to
-the following conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+Copyright (c) 2013 Caleb Spare\r
+\r
+MIT License\r
+\r
+Permission is hereby granted, free of charge, to any person obtaining\r
+a copy of this software and associated documentation files (the\r
+"Software"), to deal in the Software without restriction, including\r
+without limitation the rights to use, copy, modify, merge, publish,\r
+distribute, sublicense, and/or sell copies of the Software, and to\r
+permit persons to whom the Software is furnished to do so, subject to\r
+the following conditions:\r
+\r
+The above copyright notice and this permission notice shall be\r
+included in all copies or substantial portions of the Software.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r