From dfb0c574ddf7679a91b49217946e8aa2508960c2 Mon Sep 17 00:00:00 2001 From: Ayman Bagabas Date: Sun, 29 Dec 2024 17:04:35 +0300 Subject: [PATCH] plumbing: serverinfo, move to transport package It makes more sense to have this living in the transport package, as it is related to the transport layer. --- .../{serverinfo => transport}/serverinfo.go | 5 +- .../serverinfo_test.go | 78 +++++++++---------- 2 files changed, 38 insertions(+), 45 deletions(-) rename plumbing/{serverinfo => transport}/serverinfo.go (95%) rename plumbing/{serverinfo => transport}/serverinfo_test.go (75%) diff --git a/plumbing/serverinfo/serverinfo.go b/plumbing/transport/serverinfo.go similarity index 95% rename from plumbing/serverinfo/serverinfo.go rename to plumbing/transport/serverinfo.go index d7ea7ef06..3c29bf1e7 100644 --- a/plumbing/serverinfo/serverinfo.go +++ b/plumbing/transport/serverinfo.go @@ -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" @@ -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") diff --git a/plumbing/serverinfo/serverinfo_test.go b/plumbing/transport/serverinfo_test.go similarity index 75% rename from plumbing/serverinfo/serverinfo_test.go rename to plumbing/transport/serverinfo_test.go index 251746b6d..1af0ec314 100644 --- a/plumbing/serverinfo/serverinfo_test.go +++ b/plumbing/transport/serverinfo_test.go @@ -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" ) @@ -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) } @@ -120,15 +114,9 @@ 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) @@ -136,15 +124,9 @@ func (s *ServerInfoSuite) TestUpdateServerInfoTags(c *C) { } 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) @@ -152,30 +134,42 @@ func (s *ServerInfoSuite) TestUpdateServerInfoBasic(c *C) { } 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)