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

Commit ca5d7bb

Browse files
authored
Merge pull request #1134 from filipnavara/commitgraph-fmt-3
plumbing: format/commitgraph, rename structs/fields to follow the terms used by git more closely
2 parents 830ee5b + 2123c53 commit ca5d7bb

File tree

5 files changed

+61
-63
lines changed

5 files changed

+61
-63
lines changed

plumbing/format/commitgraph/commitgraph.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66
"gopkg.in/src-d/go-git.v4/plumbing"
77
)
88

9-
// Node is a reduced representation of Commit as presented in the commit graph
9+
// CommitData is a reduced representation of Commit as presented in the commit graph
1010
// file. It is merely useful as an optimization for walking the commit graphs.
11-
type Node struct {
11+
type CommitData struct {
1212
// TreeHash is the hash of the root tree of the commit.
1313
TreeHash plumbing.Hash
1414
// ParentIndexes are the indexes of the parent commits of the commit.
@@ -29,7 +29,7 @@ type Index interface {
2929
GetIndexByHash(h plumbing.Hash) (int, error)
3030
// GetNodeByIndex gets the commit node from the commit graph using index
3131
// obtained from child node, if available
32-
GetNodeByIndex(i int) (*Node, error)
32+
GetCommitDataByIndex(i int) (*CommitData, error)
3333
// Hashes returns all the hashes that are available in the index
3434
Hashes() []plumbing.Hash
3535
}

plumbing/format/commitgraph/commitgraph_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,40 +32,40 @@ func testDecodeHelper(c *C, path string) {
3232
// Root commit
3333
nodeIndex, err := index.GetIndexByHash(plumbing.NewHash("347c91919944a68e9413581a1bc15519550a3afe"))
3434
c.Assert(err, IsNil)
35-
node, err := index.GetNodeByIndex(nodeIndex)
35+
commitData, err := index.GetCommitDataByIndex(nodeIndex)
3636
c.Assert(err, IsNil)
37-
c.Assert(len(node.ParentIndexes), Equals, 0)
38-
c.Assert(len(node.ParentHashes), Equals, 0)
37+
c.Assert(len(commitData.ParentIndexes), Equals, 0)
38+
c.Assert(len(commitData.ParentHashes), Equals, 0)
3939

4040
// Regular commit
4141
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("e713b52d7e13807e87a002e812041f248db3f643"))
4242
c.Assert(err, IsNil)
43-
node, err = index.GetNodeByIndex(nodeIndex)
43+
commitData, err = index.GetCommitDataByIndex(nodeIndex)
4444
c.Assert(err, IsNil)
45-
c.Assert(len(node.ParentIndexes), Equals, 1)
46-
c.Assert(len(node.ParentHashes), Equals, 1)
47-
c.Assert(node.ParentHashes[0].String(), Equals, "347c91919944a68e9413581a1bc15519550a3afe")
45+
c.Assert(len(commitData.ParentIndexes), Equals, 1)
46+
c.Assert(len(commitData.ParentHashes), Equals, 1)
47+
c.Assert(commitData.ParentHashes[0].String(), Equals, "347c91919944a68e9413581a1bc15519550a3afe")
4848

4949
// Merge commit
5050
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("b29328491a0682c259bcce28741eac71f3499f7d"))
5151
c.Assert(err, IsNil)
52-
node, err = index.GetNodeByIndex(nodeIndex)
52+
commitData, err = index.GetCommitDataByIndex(nodeIndex)
5353
c.Assert(err, IsNil)
54-
c.Assert(len(node.ParentIndexes), Equals, 2)
55-
c.Assert(len(node.ParentHashes), Equals, 2)
56-
c.Assert(node.ParentHashes[0].String(), Equals, "e713b52d7e13807e87a002e812041f248db3f643")
57-
c.Assert(node.ParentHashes[1].String(), Equals, "03d2c021ff68954cf3ef0a36825e194a4b98f981")
54+
c.Assert(len(commitData.ParentIndexes), Equals, 2)
55+
c.Assert(len(commitData.ParentHashes), Equals, 2)
56+
c.Assert(commitData.ParentHashes[0].String(), Equals, "e713b52d7e13807e87a002e812041f248db3f643")
57+
c.Assert(commitData.ParentHashes[1].String(), Equals, "03d2c021ff68954cf3ef0a36825e194a4b98f981")
5858

5959
// Octopus merge commit
6060
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("6f6c5d2be7852c782be1dd13e36496dd7ad39560"))
6161
c.Assert(err, IsNil)
62-
node, err = index.GetNodeByIndex(nodeIndex)
62+
commitData, err = index.GetCommitDataByIndex(nodeIndex)
6363
c.Assert(err, IsNil)
64-
c.Assert(len(node.ParentIndexes), Equals, 3)
65-
c.Assert(len(node.ParentHashes), Equals, 3)
66-
c.Assert(node.ParentHashes[0].String(), Equals, "ce275064ad67d51e99f026084e20827901a8361c")
67-
c.Assert(node.ParentHashes[1].String(), Equals, "bb13916df33ed23004c3ce9ed3b8487528e655c1")
68-
c.Assert(node.ParentHashes[2].String(), Equals, "a45273fe2d63300e1962a9e26a6b15c276cd7082")
64+
c.Assert(len(commitData.ParentIndexes), Equals, 3)
65+
c.Assert(len(commitData.ParentHashes), Equals, 3)
66+
c.Assert(commitData.ParentHashes[0].String(), Equals, "ce275064ad67d51e99f026084e20827901a8361c")
67+
c.Assert(commitData.ParentHashes[1].String(), Equals, "bb13916df33ed23004c3ce9ed3b8487528e655c1")
68+
c.Assert(commitData.ParentHashes[2].String(), Equals, "a45273fe2d63300e1962a9e26a6b15c276cd7082")
6969

7070
// Check all hashes
7171
hashes := index.Hashes()
@@ -114,10 +114,9 @@ func (s *CommitgraphSuite) TestReencodeInMemory(c *C) {
114114
c.Assert(err, IsNil)
115115
memoryIndex := commitgraph.NewMemoryIndex()
116116
for i, hash := range index.Hashes() {
117-
node, err := index.GetNodeByIndex(i)
118-
c.Assert(err, IsNil)
119-
err = memoryIndex.Add(hash, node)
117+
commitData, err := index.GetCommitDataByIndex(i)
120118
c.Assert(err, IsNil)
119+
memoryIndex.Add(hash, commitData)
121120
}
122121
reader.Close()
123122

plumbing/format/commitgraph/encoder.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ func (e *Encoder) Encode(idx Index) error {
2929
hashes := idx.Hashes()
3030

3131
// Sort the inout and prepare helper structures we'll need for encoding
32-
hashToIndex, fanout, largeEdgesCount := e.prepare(idx, hashes)
32+
hashToIndex, fanout, extraEdgesCount := e.prepare(idx, hashes)
3333

3434
chunkSignatures := [][]byte{oidFanoutSignature, oidLookupSignature, commitDataSignature}
3535
chunkSizes := []uint64{4 * 256, uint64(len(hashes)) * 20, uint64(len(hashes)) * 36}
36-
if largeEdgesCount > 0 {
37-
chunkSignatures = append(chunkSignatures, largeEdgeListSignature)
38-
chunkSizes = append(chunkSizes, uint64(largeEdgesCount)*4)
36+
if extraEdgesCount > 0 {
37+
chunkSignatures = append(chunkSignatures, extraEdgeListSignature)
38+
chunkSizes = append(chunkSizes, uint64(extraEdgesCount)*4)
3939
}
4040

4141
if err = e.encodeFileHeader(len(chunkSignatures)); err != nil {
@@ -50,8 +50,8 @@ func (e *Encoder) Encode(idx Index) error {
5050
if err = e.encodeOidLookup(hashes); err != nil {
5151
return err
5252
}
53-
if largeEdges, err := e.encodeCommitData(hashes, hashToIndex, idx); err == nil {
54-
if err = e.encodeLargeEdges(largeEdges); err != nil {
53+
if extraEdges, err := e.encodeCommitData(hashes, hashToIndex, idx); err == nil {
54+
if err = e.encodeExtraEdges(extraEdges); err != nil {
5555
return err
5656
}
5757
}
@@ -61,7 +61,7 @@ func (e *Encoder) Encode(idx Index) error {
6161
return e.encodeChecksum()
6262
}
6363

64-
func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, largeEdgesCount uint32) {
64+
func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[plumbing.Hash]uint32, fanout []uint32, extraEdgesCount uint32) {
6565
// Sort the hashes and build our index
6666
plumbing.HashesSort(hashes)
6767
hashToIndex = make(map[plumbing.Hash]uint32)
@@ -76,11 +76,11 @@ func (e *Encoder) prepare(idx Index, hashes []plumbing.Hash) (hashToIndex map[pl
7676
fanout[i] += fanout[i-1]
7777
}
7878

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

134-
func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumbing.Hash]uint32, idx Index) (largeEdges []uint32, err error) {
134+
func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumbing.Hash]uint32, idx Index) (extraEdges []uint32, err error) {
135135
for _, hash := range hashes {
136136
origIndex, _ := idx.GetIndexByHash(hash)
137-
commitData, _ := idx.GetNodeByIndex(origIndex)
137+
commitData, _ := idx.GetCommitDataByIndex(origIndex)
138138
if _, err = e.Write(commitData.TreeHash[:]); err != nil {
139139
return
140140
}
@@ -151,11 +151,11 @@ func (e *Encoder) encodeCommitData(hashes []plumbing.Hash, hashToIndex map[plumb
151151
parent2 = hashToIndex[commitData.ParentHashes[1]]
152152
} else if len(commitData.ParentHashes) > 2 {
153153
parent1 = hashToIndex[commitData.ParentHashes[0]]
154-
parent2 = uint32(len(largeEdges)) | parentOctopusUsed
154+
parent2 = uint32(len(extraEdges)) | parentOctopusUsed
155155
for _, parentHash := range commitData.ParentHashes[1:] {
156-
largeEdges = append(largeEdges, hashToIndex[parentHash])
156+
extraEdges = append(extraEdges, hashToIndex[parentHash])
157157
}
158-
largeEdges[len(largeEdges)-1] |= parentLast
158+
extraEdges[len(extraEdges)-1] |= parentLast
159159
}
160160

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

177-
func (e *Encoder) encodeLargeEdges(largeEdges []uint32) (err error) {
178-
for _, parent := range largeEdges {
177+
func (e *Encoder) encodeExtraEdges(extraEdges []uint32) (err error) {
178+
for _, parent := range extraEdges {
179179
if err = binary.WriteUint32(e, parent); err != nil {
180180
return
181181
}

plumbing/format/commitgraph/file.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
var (
1515
// ErrUnsupportedVersion is returned by OpenFileIndex when the commit graph
1616
// file version is not supported.
17-
ErrUnsupportedVersion = errors.New("Unsuported version")
17+
ErrUnsupportedVersion = errors.New("Unsupported version")
1818
// ErrUnsupportedHash is returned by OpenFileIndex when the commit graph
1919
// hash function is not supported. Currently only SHA-1 is defined and
2020
// supported
21-
ErrUnsupportedHash = errors.New("Unsuported hash algorithm")
21+
ErrUnsupportedHash = errors.New("Unsupported hash algorithm")
2222
// ErrMalformedCommitGraphFile is returned by OpenFileIndex when the commit
2323
// graph file is corrupted.
2424
ErrMalformedCommitGraphFile = errors.New("Malformed commit graph file")
@@ -27,7 +27,7 @@ var (
2727
oidFanoutSignature = []byte{'O', 'I', 'D', 'F'}
2828
oidLookupSignature = []byte{'O', 'I', 'D', 'L'}
2929
commitDataSignature = []byte{'C', 'D', 'A', 'T'}
30-
largeEdgeListSignature = []byte{'E', 'D', 'G', 'E'}
30+
extraEdgeListSignature = []byte{'E', 'D', 'G', 'E'}
3131
lastSignature = []byte{0, 0, 0, 0}
3232

3333
parentNone = uint32(0x70000000)
@@ -42,7 +42,7 @@ type fileIndex struct {
4242
oidFanoutOffset int64
4343
oidLookupOffset int64
4444
commitDataOffset int64
45-
largeEdgeListOffset int64
45+
extraEdgeListOffset int64
4646
}
4747

4848
// OpenFileIndex opens a serialized commit graph file in the format described at
@@ -106,8 +106,8 @@ func (fi *fileIndex) readChunkHeaders() error {
106106
fi.oidLookupOffset = int64(chunkOffset)
107107
} else if bytes.Equal(chunkID, commitDataSignature) {
108108
fi.commitDataOffset = int64(chunkOffset)
109-
} else if bytes.Equal(chunkID, largeEdgeListSignature) {
110-
fi.largeEdgeListOffset = int64(chunkOffset)
109+
} else if bytes.Equal(chunkID, extraEdgeListSignature) {
110+
fi.extraEdgeListOffset = int64(chunkOffset)
111111
} else if bytes.Equal(chunkID, lastSignature) {
112112
break
113113
}
@@ -165,7 +165,7 @@ func (fi *fileIndex) GetIndexByHash(h plumbing.Hash) (int, error) {
165165
return 0, plumbing.ErrObjectNotFound
166166
}
167167

168-
func (fi *fileIndex) GetNodeByIndex(idx int) (*Node, error) {
168+
func (fi *fileIndex) GetCommitDataByIndex(idx int) (*CommitData, error) {
169169
if idx >= fi.fanout[0xff] {
170170
return nil, plumbing.ErrObjectNotFound
171171
}
@@ -194,7 +194,7 @@ func (fi *fileIndex) GetNodeByIndex(idx int) (*Node, error) {
194194
if parent2&parentOctopusUsed == parentOctopusUsed {
195195
// Octopus merge
196196
parentIndexes = []int{int(parent1 & parentOctopusMask)}
197-
offset := fi.largeEdgeListOffset + 4*int64(parent2&parentOctopusMask)
197+
offset := fi.extraEdgeListOffset + 4*int64(parent2&parentOctopusMask)
198198
buf := make([]byte, 4)
199199
for {
200200
_, err := fi.reader.ReadAt(buf, offset)
@@ -220,7 +220,7 @@ func (fi *fileIndex) GetNodeByIndex(idx int) (*Node, error) {
220220
return nil, err
221221
}
222222

223-
return &Node{
223+
return &CommitData{
224224
TreeHash: treeHash,
225225
ParentIndexes: parentIndexes,
226226
ParentHashes: parentHashes,

plumbing/format/commitgraph/memory.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
type MemoryIndex struct {
8-
commitData []*Node
8+
commitData []*CommitData
99
indexMap map[plumbing.Hash]int
1010
}
1111

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

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

36-
node := mi.commitData[i]
36+
commitData := mi.commitData[i]
3737

3838
// Map parent hashes to parent indexes
39-
if node.ParentIndexes == nil {
40-
parentIndexes := make([]int, len(node.ParentHashes))
41-
for i, parentHash := range node.ParentHashes {
39+
if commitData.ParentIndexes == nil {
40+
parentIndexes := make([]int, len(commitData.ParentHashes))
41+
for i, parentHash := range commitData.ParentHashes {
4242
var err error
4343
if parentIndexes[i], err = mi.GetIndexByHash(parentHash); err != nil {
4444
return nil, err
4545
}
4646
}
47-
node.ParentIndexes = parentIndexes
47+
commitData.ParentIndexes = parentIndexes
4848
}
4949

50-
return node, nil
50+
return commitData, nil
5151
}
5252

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

6262
// Add adds new node to the memory index
63-
func (mi *MemoryIndex) Add(hash plumbing.Hash, node *Node) error {
63+
func (mi *MemoryIndex) Add(hash plumbing.Hash, commitData *CommitData) {
6464
// The parent indexes are calculated lazily in GetNodeByIndex
6565
// which allows adding nodes out of order as long as all parents
6666
// are eventually resolved
67-
node.ParentIndexes = nil
67+
commitData.ParentIndexes = nil
6868
mi.indexMap[hash] = len(mi.commitData)
69-
mi.commitData = append(mi.commitData, node)
70-
return nil
69+
mi.commitData = append(mi.commitData, commitData)
7170
}

0 commit comments

Comments
 (0)