Skip to content

Commit

Permalink
plumbing: serverinfo, move to transport package
Browse files Browse the repository at this point in the history
It makes more sense to have this living in the transport package, as it
is related to the transport layer.
  • Loading branch information
aymanbagabas committed Dec 29, 2024
1 parent 4b23312 commit dfb0c57
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package serverinfo
package transport

import (
"fmt"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/internal/reference"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
Expand All @@ -20,7 +19,7 @@ import (
func UpdateServerInfo(s storage.Storer, fs billy.Filesystem) error {
pos, ok := s.(storer.PackedObjectStorer)
if !ok {
return git.ErrPackedObjectsNotSupported
return ErrPackedObjectsNotSupported
}

infoRefs, err := fs.Create("info/refs")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
package serverinfo
package transport

import (
"io"
"strings"
"testing"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
fixtures "github.com/go-git/go-git-fixtures/v4"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/storage"
"github.com/go-git/go-git/v5/storage/filesystem"
"github.com/go-git/go-git/v5/storage/memory"
. "gopkg.in/check.v1"
)
Expand All @@ -21,16 +21,10 @@ type ServerInfoSuite struct{}

var _ = Suite(&ServerInfoSuite{})

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

func (s *ServerInfoSuite) TestUpdateServerInfoInit(c *C) {
fs := memfs.New()
st := memory.NewStorage()
r, err := git.Init(st, fs)
c.Assert(err, IsNil)
c.Assert(r, NotNil)

err = UpdateServerInfo(st, fs)
err := UpdateServerInfo(st, fs)
c.Assert(err, IsNil)
}

Expand Down Expand Up @@ -120,62 +114,62 @@ func assertObjectPacks(c *C, st storage.Storer, fs billy.Filesystem) {
}

func (s *ServerInfoSuite) TestUpdateServerInfoTags(c *C) {
fs := memfs.New()
st := memory.NewStorage()
r, err := git.Clone(st, fs, &git.CloneOptions{
URL: fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().URL,
})
c.Assert(err, IsNil)
c.Assert(r, NotNil)

err = UpdateServerInfo(st, fs)
fs := fixtures.ByURL("https://github.com/git-fixtures/tags.git").One().DotGit()
st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
err := UpdateServerInfo(st, fs)
c.Assert(err, IsNil)

assertInfoRefs(c, st, fs)
assertObjectPacks(c, st, fs)
}

func (s *ServerInfoSuite) TestUpdateServerInfoBasic(c *C) {
fs := memfs.New()
st := memory.NewStorage()
r, err := git.Clone(st, fs, &git.CloneOptions{
URL: fixtures.Basic().One().URL,
})
c.Assert(err, IsNil)
c.Assert(r, NotNil)

err = UpdateServerInfo(st, fs)
fs := fixtures.Basic().One().DotGit()
st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
err := UpdateServerInfo(st, fs)
c.Assert(err, IsNil)

assertInfoRefs(c, st, fs)
assertObjectPacks(c, st, fs)
}

func (s *ServerInfoSuite) TestUpdateServerInfoBasicChange(c *C) {
fs := memfs.New()
st := memory.NewStorage()
r, err := git.Clone(st, fs, &git.CloneOptions{
URL: fixtures.Basic().One().URL,
})
c.Assert(err, IsNil)
c.Assert(r, NotNil)

err = UpdateServerInfo(st, fs)
fs := fixtures.Basic().One().DotGit()
st := filesystem.NewStorage(fs, cache.NewObjectLRUDefault())
err := UpdateServerInfo(st, fs)
c.Assert(err, IsNil)

assertInfoRefs(c, st, fs)
assertObjectPacks(c, st, fs)

head, err := r.Head()
head, err := storer.ResolveReference(st, plumbing.HEAD)
c.Assert(err, IsNil)

ref := plumbing.NewHashReference("refs/heads/my-branch", head.Hash())
err = r.Storer.SetReference(ref)
err = st.SetReference(ref)
c.Assert(err, IsNil)

_, err = r.CreateTag("test-tag", head.Hash(), &git.CreateTagOptions{
Message: "test-tag",
})
name := "test-tag"
hash := head.Hash()
rawobj, err := object.GetObject(st, hash)
c.Assert(err, IsNil)
tag := &object.Tag{
Name: name,
Target: hash,
TargetType: rawobj.Type(),
Message: "test-tag",
}

obj := st.NewEncodedObject()
err = tag.Encode(obj)
c.Assert(err, IsNil)

target, err := st.SetEncodedObject(obj)
c.Assert(err, IsNil)

rname := plumbing.NewTagReferenceName(name)
tagref := plumbing.NewHashReference(rname, target)
err = st.SetReference(tagref)
c.Assert(err, IsNil)

err = UpdateServerInfo(st, fs)
Expand Down

0 comments on commit dfb0c57

Please sign in to comment.