| 1 |
package v2_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"os" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/go-git/go-billy/v5" |
| 8 |
"github.com/go-git/go-billy/v5/util" |
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/cache" |
| 11 |
commitgraph "github.com/go-git/go-git/v5/plumbing/format/commitgraph/v2" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/object" |
| 14 |
"github.com/go-git/go-git/v5/storage/filesystem" |
| 15 |
|
| 16 |
fixtures "github.com/go-git/go-git-fixtures/v4" |
| 17 |
. "gopkg.in/check.v1" |
| 18 |
) |
| 19 |
|
| 20 |
func Test(t *testing.T) { TestingT(t) } |
| 21 |
|
| 22 |
type CommitgraphSuite struct { |
| 23 |
fixtures.Suite |
| 24 |
} |
| 25 |
|
| 26 |
var _ = Suite(&CommitgraphSuite{}) |
| 27 |
|
| 28 |
func testReadIndex(c *C, fs billy.Filesystem, path string) commitgraph.Index { |
| 29 |
reader, err := fs.Open(path) |
| 30 |
c.Assert(err, IsNil) |
| 31 |
index, err := commitgraph.OpenFileIndex(reader) |
| 32 |
c.Assert(err, IsNil) |
| 33 |
c.Assert(index, NotNil) |
| 34 |
return index |
| 35 |
} |
| 36 |
|
| 37 |
func testDecodeHelper(c *C, index commitgraph.Index) { |
| 38 |
// Root commit |
| 39 |
nodeIndex, err := index.GetIndexByHash(plumbing.NewHash("347c91919944a68e9413581a1bc15519550a3afe")) |
| 40 |
c.Assert(err, IsNil) |
| 41 |
commitData, err := index.GetCommitDataByIndex(nodeIndex) |
| 42 |
c.Assert(err, IsNil) |
| 43 |
c.Assert(len(commitData.ParentIndexes), Equals, 0) |
| 44 |
c.Assert(len(commitData.ParentHashes), Equals, 0) |
| 45 |
|
| 46 |
// Regular commit |
| 47 |
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("e713b52d7e13807e87a002e812041f248db3f643")) |
| 48 |
c.Assert(err, IsNil) |
| 49 |
commitData, err = index.GetCommitDataByIndex(nodeIndex) |
| 50 |
c.Assert(err, IsNil) |
| 51 |
c.Assert(len(commitData.ParentIndexes), Equals, 1) |
| 52 |
c.Assert(len(commitData.ParentHashes), Equals, 1) |
| 53 |
c.Assert(commitData.ParentHashes[0].String(), Equals, "347c91919944a68e9413581a1bc15519550a3afe") |
| 54 |
|
| 55 |
// Merge commit |
| 56 |
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("b29328491a0682c259bcce28741eac71f3499f7d")) |
| 57 |
c.Assert(err, IsNil) |
| 58 |
commitData, err = index.GetCommitDataByIndex(nodeIndex) |
| 59 |
c.Assert(err, IsNil) |
| 60 |
c.Assert(len(commitData.ParentIndexes), Equals, 2) |
| 61 |
c.Assert(len(commitData.ParentHashes), Equals, 2) |
| 62 |
c.Assert(commitData.ParentHashes[0].String(), Equals, "e713b52d7e13807e87a002e812041f248db3f643") |
| 63 |
c.Assert(commitData.ParentHashes[1].String(), Equals, "03d2c021ff68954cf3ef0a36825e194a4b98f981") |
| 64 |
|
| 65 |
// Octopus merge commit |
| 66 |
nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("6f6c5d2be7852c782be1dd13e36496dd7ad39560")) |
| 67 |
c.Assert(err, IsNil) |
| 68 |
commitData, err = index.GetCommitDataByIndex(nodeIndex) |
| 69 |
c.Assert(err, IsNil) |
| 70 |
c.Assert(len(commitData.ParentIndexes), Equals, 3) |
| 71 |
c.Assert(len(commitData.ParentHashes), Equals, 3) |
| 72 |
c.Assert(commitData.ParentHashes[0].String(), Equals, "ce275064ad67d51e99f026084e20827901a8361c") |
| 73 |
c.Assert(commitData.ParentHashes[1].String(), Equals, "bb13916df33ed23004c3ce9ed3b8487528e655c1") |
| 74 |
c.Assert(commitData.ParentHashes[2].String(), Equals, "a45273fe2d63300e1962a9e26a6b15c276cd7082") |
| 75 |
|
| 76 |
// Check all hashes |
| 77 |
hashes := index.Hashes() |
| 78 |
c.Assert(len(hashes), Equals, 11) |
| 79 |
c.Assert(hashes[0].String(), Equals, "03d2c021ff68954cf3ef0a36825e194a4b98f981") |
| 80 |
c.Assert(hashes[10].String(), Equals, "e713b52d7e13807e87a002e812041f248db3f643") |
| 81 |
} |
| 82 |
|
| 83 |
func (s *CommitgraphSuite) TestDecodeMultiChain(c *C) { |
| 84 |
fixtures.ByTag("commit-graph-chain-2").Test(c, func(f *fixtures.Fixture) { |
| 85 |
dotgit := f.DotGit() |
| 86 |
index, err := commitgraph.OpenChainOrFileIndex(dotgit) |
| 87 |
c.Assert(err, IsNil) |
| 88 |
defer index.Close() |
| 89 |
storer := filesystem.NewStorage(f.DotGit(), cache.NewObjectLRUDefault()) |
| 90 |
p := f.Packfile() |
| 91 |
defer p.Close() |
| 92 |
packfile.UpdateObjectStorage(storer, p) |
| 93 |
|
| 94 |
for idx, hash := range index.Hashes() { |
| 95 |
idx2, err := index.GetIndexByHash(hash) |
| 96 |
c.Assert(err, IsNil) |
| 97 |
c.Assert(idx2, Equals, uint32(idx)) |
| 98 |
hash2, err := index.GetHashByIndex(idx2) |
| 99 |
c.Assert(err, IsNil) |
| 100 |
c.Assert(hash2.String(), Equals, hash.String()) |
| 101 |
|
| 102 |
commitData, err := index.GetCommitDataByIndex(uint32(idx)) |
| 103 |
c.Assert(err, IsNil) |
| 104 |
commit, err := object.GetCommit(storer, hash) |
| 105 |
c.Assert(err, IsNil) |
| 106 |
|
| 107 |
for i, parent := range commit.ParentHashes { |
| 108 |
c.Assert(hash.String()+":"+parent.String(), Equals, hash.String()+":"+commitData.ParentHashes[i].String()) |
| 109 |
} |
| 110 |
} |
| 111 |
}) |
| 112 |
} |
| 113 |
|
| 114 |
func (s *CommitgraphSuite) TestDecode(c *C) { |
| 115 |
fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) { |
| 116 |
dotgit := f.DotGit() |
| 117 |
index := testReadIndex(c, dotgit, dotgit.Join("objects", "info", "commit-graph")) |
| 118 |
defer index.Close() |
| 119 |
testDecodeHelper(c, index) |
| 120 |
}) |
| 121 |
} |
| 122 |
|
| 123 |
func (s *CommitgraphSuite) TestDecodeChain(c *C) { |
| 124 |
fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) { |
| 125 |
dotgit := f.DotGit() |
| 126 |
index, err := commitgraph.OpenChainOrFileIndex(dotgit) |
| 127 |
c.Assert(err, IsNil) |
| 128 |
defer index.Close() |
| 129 |
testDecodeHelper(c, index) |
| 130 |
}) |
| 131 |
|
| 132 |
fixtures.ByTag("commit-graph-chain").Test(c, func(f *fixtures.Fixture) { |
| 133 |
dotgit := f.DotGit() |
| 134 |
index, err := commitgraph.OpenChainOrFileIndex(dotgit) |
| 135 |
c.Assert(err, IsNil) |
| 136 |
defer index.Close() |
| 137 |
testDecodeHelper(c, index) |
| 138 |
}) |
| 139 |
} |
| 140 |
|
| 141 |
func (s *CommitgraphSuite) TestReencode(c *C) { |
| 142 |
fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) { |
| 143 |
dotgit := f.DotGit() |
| 144 |
|
| 145 |
reader, err := dotgit.Open(dotgit.Join("objects", "info", "commit-graph")) |
| 146 |
c.Assert(err, IsNil) |
| 147 |
defer reader.Close() |
| 148 |
index, err := commitgraph.OpenFileIndex(reader) |
| 149 |
c.Assert(err, IsNil) |
| 150 |
defer index.Close() |
| 151 |
|
| 152 |
writer, err := util.TempFile(dotgit, "", "commit-graph") |
| 153 |
c.Assert(err, IsNil) |
| 154 |
tmpName := writer.Name() |
| 155 |
defer os.Remove(tmpName) |
| 156 |
|
| 157 |
encoder := commitgraph.NewEncoder(writer) |
| 158 |
err = encoder.Encode(index) |
| 159 |
c.Assert(err, IsNil) |
| 160 |
writer.Close() |
| 161 |
|
| 162 |
tmpIndex := testReadIndex(c, dotgit, tmpName) |
| 163 |
defer tmpIndex.Close() |
| 164 |
testDecodeHelper(c, tmpIndex) |
| 165 |
}) |
| 166 |
} |
| 167 |
|
| 168 |
func (s *CommitgraphSuite) TestReencodeInMemory(c *C) { |
| 169 |
fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) { |
| 170 |
dotgit := f.DotGit() |
| 171 |
|
| 172 |
reader, err := dotgit.Open(dotgit.Join("objects", "info", "commit-graph")) |
| 173 |
c.Assert(err, IsNil) |
| 174 |
index, err := commitgraph.OpenFileIndex(reader) |
| 175 |
c.Assert(err, IsNil) |
| 176 |
|
| 177 |
memoryIndex := commitgraph.NewMemoryIndex() |
| 178 |
defer memoryIndex.Close() |
| 179 |
for i, hash := range index.Hashes() { |
| 180 |
commitData, err := index.GetCommitDataByIndex(uint32(i)) |
| 181 |
c.Assert(err, IsNil) |
| 182 |
memoryIndex.Add(hash, commitData) |
| 183 |
} |
| 184 |
index.Close() |
| 185 |
|
| 186 |
writer, err := util.TempFile(dotgit, "", "commit-graph") |
| 187 |
c.Assert(err, IsNil) |
| 188 |
tmpName := writer.Name() |
| 189 |
defer os.Remove(tmpName) |
| 190 |
|
| 191 |
encoder := commitgraph.NewEncoder(writer) |
| 192 |
err = encoder.Encode(memoryIndex) |
| 193 |
c.Assert(err, IsNil) |
| 194 |
writer.Close() |
| 195 |
|
| 196 |
tmpIndex := testReadIndex(c, dotgit, tmpName) |
| 197 |
defer tmpIndex.Close() |
| 198 |
testDecodeHelper(c, tmpIndex) |
| 199 |
}) |
| 200 |
} |