Skip to content

Commit

Permalink
plumbing: transport, move server stuff into transport
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jun 8, 2024
1 parent 11c34f9 commit fce6fea
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 103 deletions.
23 changes: 0 additions & 23 deletions plumbing/server/common.go

This file was deleted.

3 changes: 3 additions & 0 deletions plumbing/transport/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ var (
ErrInvalidResponse = errors.New("invalid response")
// ErrTimeoutExceeded is returned when the timeout is exceeded.
ErrTimeoutExceeded = errors.New("timeout exceeded")
// ErrPackedObjectsNotSupported is returned when the server does not support
// packed objects.
ErrPackedObjectsNotSupported = errors.New("packed objects not supported")
// stdErrSkipPattern is used for skipping lines from a command's stderr output.
// Any line matching this pattern will be skipped from further
// processing and not be returned to calling code.
Expand Down
17 changes: 8 additions & 9 deletions plumbing/transport/file/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"strings"

"github.com/go-git/go-git/v5/plumbing/server"
"github.com/go-git/go-git/v5/plumbing/transport"
)

Expand All @@ -19,14 +18,14 @@ func init() {
var DefaultTransport = NewTransport(nil)

type runner struct {
loader server.Loader
loader transport.Loader
}

// NewTransport returns a new file transport that users go-git built-in server
// implementation to serve repositories.
func NewTransport(loader server.Loader) transport.Transport {
func NewTransport(loader transport.Loader) transport.Transport {
if loader == nil {
loader = server.DefaultLoader
loader = transport.DefaultLoader
}
return transport.NewPackTransport(&runner{loader})
}
Expand All @@ -47,7 +46,7 @@ func (r *runner) Command(ctx context.Context, cmd string, ep *transport.Endpoint
type command struct {
ctx context.Context
ep *transport.Endpoint
loader server.Loader
loader transport.Loader
service string
gitProtocol string

Expand All @@ -71,11 +70,11 @@ func (c *command) Start() error {

switch c.service {
case transport.UploadPackServiceName:
opts := &server.UploadPackOptions{
opts := &transport.UploadPackOptions{
GitProtocol: c.gitProtocol,
}
go func() {
if err := server.UploadPack(
if err := transport.UploadPack(
c.ctx,
st,
io.NopCloser(c.stdin),
Expand All @@ -89,11 +88,11 @@ func (c *command) Start() error {
}()
return nil
case transport.ReceivePackServiceName:
opts := &server.ReceivePackOptions{
opts := &transport.ReceivePackOptions{
GitProtocol: c.gitProtocol,
}
go func() {
if err := server.ReceivePack(
if err := transport.ReceivePack(
c.ctx,
st,
io.NopCloser(c.stdin),
Expand Down
15 changes: 7 additions & 8 deletions plumbing/server/loader.go → plumbing/transport/loader.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package server
package transport

import (
"path/filepath"

"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage"
"github.com/go-git/go-git/v5/storage/filesystem"

Expand All @@ -21,7 +20,7 @@ type Loader interface {
// Load loads a storer.Storer given a transport.Endpoint.
// Returns transport.ErrRepositoryNotFound if the repository does not
// exist.
Load(ep *transport.Endpoint) (storage.Storer, error)
Load(ep *Endpoint) (storage.Storer, error)
}

type fsLoader struct {
Expand All @@ -38,11 +37,11 @@ func NewFilesystemLoader(base billy.Filesystem, strict bool) Loader {
// Load looks up the endpoint's path in the base file system and returns a
// storer for it. Returns transport.ErrRepositoryNotFound if a repository does
// not exist in the given path.
func (l *fsLoader) Load(ep *transport.Endpoint) (storage.Storer, error) {
func (l *fsLoader) Load(ep *Endpoint) (storage.Storer, error) {
return l.load(ep, false)
}

func (l *fsLoader) load(ep *transport.Endpoint, tried bool) (storage.Storer, error) {
func (l *fsLoader) load(ep *Endpoint, tried bool) (storage.Storer, error) {
fs, err := l.base.Chroot(ep.Path)
if err != nil {
return nil, err
Expand All @@ -57,7 +56,7 @@ func (l *fsLoader) load(ep *transport.Endpoint, tried bool) (storage.Storer, err
}
return l.load(ep, true)
}
return nil, transport.ErrRepositoryNotFound
return nil, ErrRepositoryNotFound
}

return filesystem.NewStorage(fs, cache.NewObjectLRUDefault()), nil
Expand All @@ -70,10 +69,10 @@ type MapLoader map[string]storer.Storer
// Load returns a storer.Storer for given a transport.Endpoint by looking it up
// in the map. Returns transport.ErrRepositoryNotFound if the endpoint does not
// exist.
func (l MapLoader) Load(ep *transport.Endpoint) (storer.Storer, error) {
func (l MapLoader) Load(ep *Endpoint) (storer.Storer, error) {
s, ok := l[ep.String()]
if !ok {
return nil, transport.ErrRepositoryNotFound
return nil, ErrRepositoryNotFound
}

return s, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package server
package transport

import (
"os/exec"
"path/filepath"

"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage/memory"

. "gopkg.in/check.v1"
Expand All @@ -26,21 +25,21 @@ func (s *LoaderSuite) SetUpSuite(c *C) {
c.Assert(exec.Command("git", "init", "--bare", s.RepoPath).Run(), IsNil)
}

func (s *LoaderSuite) endpoint(c *C, url string) *transport.Endpoint {
ep, err := transport.NewEndpoint(url)
func (s *LoaderSuite) endpoint(c *C, url string) *Endpoint {
ep, err := NewEndpoint(url)
c.Assert(err, IsNil)
return ep
}

func (s *LoaderSuite) TestLoadNonExistent(c *C) {
sto, err := DefaultLoader.Load(s.endpoint(c, "does-not-exist"))
c.Assert(err, Equals, transport.ErrRepositoryNotFound)
c.Assert(err, Equals, ErrRepositoryNotFound)
c.Assert(sto, IsNil)
}

func (s *LoaderSuite) TestLoadNonExistentIgnoreHost(c *C) {
sto, err := DefaultLoader.Load(s.endpoint(c, "https://github.com/does-not-exist"))
c.Assert(err, Equals, transport.ErrRepositoryNotFound)
c.Assert(err, Equals, ErrRepositoryNotFound)
c.Assert(sto, IsNil)
}

Expand All @@ -57,13 +56,13 @@ func (s *LoaderSuite) TestLoadIgnoreHost(c *C) {
}

func (s *LoaderSuite) TestMapLoader(c *C) {
ep, err := transport.NewEndpoint("file://test")
ep, err := NewEndpoint("file://test")
sto := memory.NewStorage()
c.Assert(err, IsNil)

loader := MapLoader{ep.String(): sto}

ep, err = transport.NewEndpoint("file://test")
ep, err = NewEndpoint("file://test")
c.Assert(err, IsNil)

loaderSto, err := loader.Load(ep)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package transport

import (
"bufio"
Expand All @@ -13,7 +13,6 @@ import (
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ func ReceivePack(
}

if opts.AdvertiseRefs || !opts.StatelessRPC {
if err := AdvertiseReferences(ctx, st, w, transport.ReceivePackServiceName, opts.StatelessRPC); err != nil {
if err := AdvertiseReferences(ctx, st, w, ReceivePackServiceName, opts.StatelessRPC); err != nil {
return err
}
}
Expand Down
5 changes: 2 additions & 3 deletions plumbing/server/serve.go → plumbing/transport/serve.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package server
package transport

import (
"context"
Expand All @@ -14,7 +14,6 @@ import (
"github.com/go-git/go-git/v5/plumbing/protocol/packp"
"github.com/go-git/go-git/v5/plumbing/protocol/packp/capability"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/storage"
)

Expand All @@ -23,7 +22,7 @@ var ErrUpdateReference = errors.New("failed to update ref")
// AdvertiseReferences is a server command that implements the reference
// discovery phase of the Git transfer protocol.
func AdvertiseReferences(ctx context.Context, st storage.Storer, w io.Writer, service string, stateless bool) error {
forPush := service == transport.ReceivePackServiceName
forPush := service == ReceivePackServiceName
ar := packp.NewAdvRefs()

// Set server default capabilities
Expand Down
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
Loading

0 comments on commit fce6fea

Please sign in to comment.