aboutsummaryrefslogblamecommitdiffstats
path: root/plumbing/format/commitgraph/commitgraph_test.go
blob: a8e455723e57a5f6fc60543de6d0e68385adf850 (plain) (tree)
1
2
3
4
5
6
7
8


                         
              


                    
                  


























































                                                                                                             

                                                                                                    



                                          

                                                                                          


                                              
                                                                           
                                     




                                                                                                 
                                                                           
                                     




                                                                                                      
                                     


                                                                              

                                         










                                                          
                                                                           
                                     



                                                                                                      
                                     










                                                                              
                                         








                                                          
package commitgraph_test

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"testing"

	"golang.org/x/exp/mmap"

	. "gopkg.in/check.v1"
	"gopkg.in/src-d/go-git-fixtures.v3"
	"gopkg.in/src-d/go-git.v4/plumbing"
	"gopkg.in/src-d/go-git.v4/plumbing/format/commitgraph"
)

func Test(t *testing.T) { TestingT(t) }

type CommitgraphSuite struct {
	fixtures.Suite
}

var _ = Suite(&CommitgraphSuite{})

func testDecodeHelper(c *C, path string) {
	reader, err := mmap.Open(path)
	c.Assert(err, IsNil)
	defer reader.Close()
	index, err := commitgraph.OpenFileIndex(reader)
	c.Assert(err, IsNil)

	// Root commit

	nodeIndex, err := index.GetIndexByHash(plumbing.NewHash("347c91919944a68e9413581a1bc15519550a3afe"))
	c.Assert(err, IsNil)
	node, err := index.GetNodeByIndex(nodeIndex)
	c.Assert(err, IsNil)
	c.Assert(len(node.ParentIndexes), Equals, 0)
	c.Assert(len(node.ParentHashes), Equals, 0)

	// Regular commit

	nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("e713b52d7e13807e87a002e812041f248db3f643"))
	c.Assert(err, IsNil)
	node, err = index.GetNodeByIndex(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")

	// Merge commit

	nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("b29328491a0682c259bcce28741eac71f3499f7d"))
	c.Assert(err, IsNil)
	node, err = index.GetNodeByIndex(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")

	// Octopus merge commit

	nodeIndex, err = index.GetIndexByHash(plumbing.NewHash("6f6c5d2be7852c782be1dd13e36496dd7ad39560"))
	c.Assert(err, IsNil)
	node, err = index.GetNodeByIndex(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")

	// Check all hashes

	hashes := index.Hashes()
	c.Assert(len(hashes), Equals, 11)
	c.Assert(hashes[0].String(), Equals, "03d2c021ff68954cf3ef0a36825e194a4b98f981")
	c.Assert(hashes[10].String(), Equals, "e713b52d7e13807e87a002e812041f248db3f643")
}

func (s *CommitgraphSuite) TestDecode(c *C) {
	fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) {
		dotgit := f.DotGit()
		testDecodeHelper(c, path.Join(dotgit.Root(), "objects", "info", "commit-graph"))
	})
}

func (s *CommitgraphSuite) TestReencode(c *C) {
	fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) {
		dotgit := f.DotGit()

		reader, err := mmap.Open(path.Join(dotgit.Root(), "objects", "info", "commit-graph"))
		c.Assert(err, IsNil)
		defer reader.Close()
		index, err := commitgraph.OpenFileIndex(reader)
		c.Assert(err, IsNil)

		writer, err := ioutil.TempFile(dotgit.Root(), "commit-graph")
		c.Assert(err, IsNil)
		tmpName := writer.Name()
		fmt.Printf(tmpName)
		defer os.Remove(tmpName)
		encoder := commitgraph.NewEncoder(writer)
		err = encoder.Encode(index)
		c.Assert(err, IsNil)
		writer.Close()

		testDecodeHelper(c, tmpName)
	})
}

func (s *CommitgraphSuite) TestReencodeInMemory(c *C) {
	fixtures.ByTag("commit-graph").Test(c, func(f *fixtures.Fixture) {
		dotgit := f.DotGit()

		reader, err := mmap.Open(path.Join(dotgit.Root(), "objects", "info", "commit-graph"))
		c.Assert(err, IsNil)
		index, err := commitgraph.OpenFileIndex(reader)
		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)
			c.Assert(err, IsNil)
		}
		reader.Close()

		writer, err := ioutil.TempFile(dotgit.Root(), "commit-graph")
		c.Assert(err, IsNil)
		tmpName := writer.Name()
		defer os.Remove(tmpName)
		encoder := commitgraph.NewEncoder(writer)
		err = encoder.Encode(memoryIndex)
		c.Assert(err, IsNil)
		writer.Close()

		testDecodeHelper(c, tmpName)
	})
}