Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

plumbing: format/commitgraph, rename structs/fields to follow the terms used by git more closely #1134

Merged
merged 2 commits into from
Apr 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions plumbing/format/commitgraph/commitgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import (
"gopkg.in/src-d/go-git.v4/plumbing"
)

// Node is a reduced representation of Commit as presented in the commit graph
// 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 Node struct {
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.
Expand All @@ -29,7 +29,7 @@ type Index interface {
GetIndexByHash(h plumbing.Hash) (int, error)
// GetNodeByIndex gets the commit node from the commit graph using index
// obtained from child node, if available
GetNodeByIndex(i int) (*Node, error)
GetCommitDataByIndex(i int) (*CommitData, error)
// Hashes returns all the hashes that are available in the index
Hashes() []plumbing.Hash
}
41 changes: 20 additions & 21 deletions plumbing/format/commitgraph/commitgraph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,40 @@ func testDecodeHelper(c *C, path string) {
// Root commit
nodeIndex, err := index.GetIndexByHash(plumbing.NewHash("347c91919944a68e9413581a1bc15519550a3afe"))
c.Assert(err, IsNil)
node, err := index.GetNodeByIndex(nodeIndex)
commitData, err := index.GetCommitDataByIndex(nodeIndex)
c.Assert(err, IsNil)
c.Assert(len(node.ParentIndexes), Equals, 0)
c.Assert(len(node.ParentHashes), Equals, 0)
c.Assert(len(commitData.ParentIndexes), Equals, 0)
c.Assert(len(commitData.ParentHashes), Equals, 0)

// Regular commit
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("e713b52d7e13807e87a002e812041f248db3f643"))
c.Assert(err, IsNil)
node, err = index.GetNodeByIndex(nodeIndex)
commitData, err = index.GetCommitDataByIndex(nodeIndex)
c.Assert(err, IsNil)
c.Assert(len(node.ParentIndexes), Equals, 1)
c.Assert(len(node.ParentHashes), Equals, 1)
c.Assert(node.ParentHashes[0].String(), Equals, "347c91919944a68e9413581a1bc15519550a3afe")
c.Assert(len(commitData.ParentIndexes), Equals, 1)
c.Assert(len(commitData.ParentHashes), Equals, 1)
c.Assert(commitData.ParentHashes[0].String(), Equals, "347c91919944a68e9413581a1bc15519550a3afe")

// Merge commit
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("b29328491a0682c259bcce28741eac71f3499f7d"))
c.Assert(err, IsNil)
node, err = index.GetNodeByIndex(nodeIndex)
commitData, err = index.GetCommitDataByIndex(nodeIndex)
c.Assert(err, IsNil)
c.Assert(len(node.ParentIndexes), Equals, 2)
c.Assert(len(node.ParentHashes), Equals, 2)
c.Assert(node.ParentHashes[0].String(), Equals, "e713b52d7e13807e87a002e812041f248db3f643")
c.Assert(node.ParentHashes[1].String(), Equals, "03d2c021ff68954cf3ef0a36825e194a4b98f981")
c.Assert(len(commitData.ParentIndexes), Equals, 2)
c.Assert(len(commitData.ParentHashes), Equals, 2)
c.Assert(commitData.ParentHashes[0].String(), Equals, "e713b52d7e13807e87a002e812041f248db3f643")
c.Assert(commitData.ParentHashes[1].String(), Equals, "03d2c021ff68954cf3ef0a36825e194a4b98f981")

// Octopus merge commit
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("6f6c5d2be7852c782be1dd13e36496dd7ad39560"))
c.Assert(err, IsNil)
node, err = index.GetNodeByIndex(nodeIndex)
commitData, err = index.GetCommitDataByIndex(nodeIndex)
c.Assert(err, IsNil)
c.Assert(len(node.ParentIndexes), Equals, 3)
c.Assert(len(node.ParentHashes), Equals, 3)
c.Assert(node.ParentHashes[0].String(), Equals, "ce275064ad67d51e99f026084e20827901a8361c")
c.Assert(node.ParentHashes[1].String(), Equals, "bb13916df33ed23004c3ce9ed3b8487528e655c1")
c.Assert(node.ParentHashes[2].String(), Equals, "a45273fe2d63300e1962a9e26a6b15c276cd7082")
c.Assert(len(commitData.ParentIndexes), Equals, 3)
c.Assert(len(commitData.ParentHashes), Equals, 3)
c.Assert(commitData.ParentHashes[0].String(), Equals, "ce275064ad67d51e99f026084e20827901a8361c")
c.Assert(commitData.ParentHashes[1].String(), Equals, "bb13916df33ed23004c3ce9ed3b8487528e655c1")
c.Assert(commitData.ParentHashes[2].String(), Equals, "a45273fe2d63300e1962a9e26a6b15c276cd7082")

// Check all hashes
hashes := index.Hashes()
Expand Down Expand Up @@ -114,10 +114,9 @@ func (s *CommitgraphSuite) TestReencodeInMemory(c *C) {
c.Assert(err, IsNil)
memoryIndex := commitgraph.NewMemoryIndex()
for i, hash := range index.Hashes() {
node, err := index.GetNodeByIndex(i)
c.Assert(err, IsNil)
err = memoryIndex.Add(hash, node)
commitData, err := index.GetCommitDataByIndex(i)
c.Assert(err, IsNil)
memoryIndex.Add(hash, commitData)
}
reader.Close()

Expand Down
34 changes: 17 additions & 17 deletions plumbing/format/commitgraph/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ func (e *Encoder) Encode(idx Index) error {
hashes := idx.Hashes()

// Sort the inout and prepare helper structures we'll need for encoding
hashToIndex, fanout, largeEdgesCount := e.prepare(idx, hashes)
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 largeEdgesCount > 0 {
chunkSignatures = append(chunkSignatures, largeEdgeListSignature)
chunkSizes = append(chunkSizes, uint64(largeEdgesCount)*4)
if extraEdgesCount > 0 {
chunkSignatures = append(chunkSignatures, extraEdgeListSignature)
chunkSizes = append(chunkSizes, uint64(extraEdgesCount)*4)
}

if err = e.encodeFileHeader(len(chunkSignatures)); err != nil {
Expand All @@ -50,8 +50,8 @@ func (e *Encoder) Encode(idx Index) error {
if err = e.encodeOidLookup(hashes); err != nil {
return err
}
if largeEdges, err := e.encodeCommitData(hashes, hashToIndex, idx); err == nil {
if err = e.encodeLargeEdges(largeEdges); err != nil {
if extraEdges, err := e.encodeCommitData(hashes, hashToIndex, idx); err == nil {
if err = e.encodeExtraEdges(extraEdges); err != nil {
return err
}
}
Expand All @@ -61,7 +61,7 @@ func (e *Encoder) Encode(idx Index) error {
return e.encodeChecksum()
}

func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, largeEdgesCount uint32) {
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)
Expand All @@ -76,11 +76,11 @@ func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[pl
fanout[i] += fanout[i-1]
}

// Find out if we will need large edge table
// Find out if we will need extra edge table
for i := 0; i < len(hashes); i++ {
v, _ := idx.GetNodeByIndex(i)
v, _ := idx.GetCommitDataByIndex(i)
if len(v.ParentHashes) > 2 {
largeEdgesCount += uint32(len(v.ParentHashes) - 1)
extraEdgesCount += uint32(len(v.ParentHashes) - 1)
break
}
}
Expand Down Expand Up @@ -131,10 +131,10 @@ func (e *Encoder) encodeOidLookup(hashes []plumbing.Hash) (err error) {
return
}

func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumbing.Hash]uint32, idx Index) (largeEdges []uint32, err error) {
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.GetNodeByIndex(origIndex)
commitData, _ := idx.GetCommitDataByIndex(origIndex)
if _, err = e.Write(commitData.TreeHash[:]); err != nil {
return
}
Expand All @@ -151,11 +151,11 @@ func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumb
parent2 = hashToIndex[commitData.ParentHashes[1]]
} else if len(commitData.ParentHashes) > 2 {
parent1 = hashToIndex[commitData.ParentHashes[0]]
parent2 = uint32(len(largeEdges)) | parentOctopusUsed
parent2 = uint32(len(extraEdges)) | parentOctopusUsed
for _, parentHash := range commitData.ParentHashes[1:] {
largeEdges = append(largeEdges, hashToIndex[parentHash])
extraEdges = append(extraEdges, hashToIndex[parentHash])
}
largeEdges[len(largeEdges)-1] |= parentLast
extraEdges[len(extraEdges)-1] |= parentLast
}

if err = binary.WriteUint32(e, parent1); err == nil {
Expand All @@ -174,8 +174,8 @@ func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumb
return
}

func (e *Encoder) encodeLargeEdges(largeEdges []uint32) (err error) {
for _, parent := range largeEdges {
func (e *Encoder) encodeExtraEdges(extraEdges []uint32) (err error) {
for _, parent := range extraEdges {
if err = binary.WriteUint32(e, parent); err != nil {
return
}
Expand Down
18 changes: 9 additions & 9 deletions plumbing/format/commitgraph/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
var (
// ErrUnsupportedVersion is returned by OpenFileIndex when the commit graph
// file version is not supported.
ErrUnsupportedVersion = errors.New("Unsuported version")
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("Unsuported hash algorithm")
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")
Expand All @@ -27,7 +27,7 @@ var (
oidFanoutSignature = []byte{'O', 'I', 'D', 'F'}
oidLookupSignature = []byte{'O', 'I', 'D', 'L'}
commitDataSignature = []byte{'C', 'D', 'A', 'T'}
largeEdgeListSignature = []byte{'E', 'D', 'G', 'E'}
extraEdgeListSignature = []byte{'E', 'D', 'G', 'E'}
lastSignature = []byte{0, 0, 0, 0}

parentNone = uint32(0x70000000)
Expand All @@ -42,7 +42,7 @@ type fileIndex struct {
oidFanoutOffset int64
oidLookupOffset int64
commitDataOffset int64
largeEdgeListOffset int64
extraEdgeListOffset int64
}

// OpenFileIndex opens a serialized commit graph file in the format described at
Expand Down Expand Up @@ -106,8 +106,8 @@ func (fi *fileIndex) readChunkHeaders() error {
fi.oidLookupOffset = int64(chunkOffset)
} else if bytes.Equal(chunkID, commitDataSignature) {
fi.commitDataOffset = int64(chunkOffset)
} else if bytes.Equal(chunkID, largeEdgeListSignature) {
fi.largeEdgeListOffset = int64(chunkOffset)
} else if bytes.Equal(chunkID, extraEdgeListSignature) {
fi.extraEdgeListOffset = int64(chunkOffset)
} else if bytes.Equal(chunkID, lastSignature) {
break
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func (fi *fileIndex) GetIndexByHash(h plumbing.Hash) (int, error) {
return 0, plumbing.ErrObjectNotFound
}

func (fi *fileIndex) GetNodeByIndex(idx int) (*Node, error) {
func (fi *fileIndex) GetCommitDataByIndex(idx int) (*CommitData, error) {
if idx >= fi.fanout[0xff] {
return nil, plumbing.ErrObjectNotFound
}
Expand Down Expand Up @@ -194,7 +194,7 @@ func (fi *fileIndex) GetNodeByIndex(idx int) (*Node, error) {
if parent2&parentOctopusUsed == parentOctopusUsed {
// Octopus merge
parentIndexes = []int{int(parent1 & parentOctopusMask)}
offset := fi.largeEdgeListOffset + 4*int64(parent2&parentOctopusMask)
offset := fi.extraEdgeListOffset + 4*int64(parent2&parentOctopusMask)
buf := make([]byte, 4)
for {
_, err := fi.reader.ReadAt(buf, offset)
Expand All @@ -220,7 +220,7 @@ func (fi *fileIndex) GetNodeByIndex(idx int) (*Node, error) {
return nil, err
}

return &Node{
return &CommitData{
TreeHash: treeHash,
ParentIndexes: parentIndexes,
ParentHashes: parentHashes,
Expand Down
25 changes: 12 additions & 13 deletions plumbing/format/commitgraph/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type MemoryIndex struct {
commitData []*Node
commitData []*CommitData
indexMap map[plumbing.Hash]int
}

Expand All @@ -26,28 +26,28 @@ func (mi *MemoryIndex) GetIndexByHash(h plumbing.Hash) (int, error) {
return 0, plumbing.ErrObjectNotFound
}

// GetNodeByIndex gets the commit node from the commit graph using index
// GetCommitDataByIndex gets the commit node from the commit graph using index
// obtained from child node, if available
func (mi *MemoryIndex) GetNodeByIndex(i int) (*Node, error) {
func (mi *MemoryIndex) GetCommitDataByIndex(i int) (*CommitData, error) {
if int(i) >= len(mi.commitData) {
return nil, plumbing.ErrObjectNotFound
}

node := mi.commitData[i]
commitData := mi.commitData[i]

// Map parent hashes to parent indexes
if node.ParentIndexes == nil {
parentIndexes := make([]int, len(node.ParentHashes))
for i, parentHash := range node.ParentHashes {
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
}
}
node.ParentIndexes = parentIndexes
commitData.ParentIndexes = parentIndexes
}

return node, nil
return commitData, nil
}

// Hashes returns all the hashes that are available in the index
Expand All @@ -60,12 +60,11 @@ func (mi *MemoryIndex) Hashes() []plumbing.Hash {
}

// Add adds new node to the memory index
func (mi *MemoryIndex) Add(hash plumbing.Hash, node *Node) error {
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
node.ParentIndexes = nil
commitData.ParentIndexes = nil
mi.indexMap[hash] = len(mi.commitData)
mi.commitData = append(mi.commitData, node)
return nil
mi.commitData = append(mi.commitData, commitData)
}