diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-20 01:30:05 +0200 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2016-08-20 01:30:05 +0200 |
commit | 4652c0e753c88e63ba111d49aa58edc655806c03 (patch) | |
tree | 28d8570676de3d0b5802c265406a82d5ba2bf842 | |
parent | 9f49aaed839ae608a0ffdaa0656b3975d3404002 (diff) | |
download | go-git-4652c0e753c88e63ba111d49aa58edc655806c03.tar.gz |
remote and repository base on ConfigStore
-rw-r--r-- | common.go | 10 | ||||
-rw-r--r-- | config/config.go | 14 | ||||
-rw-r--r-- | core/storage.go | 6 | ||||
-rw-r--r-- | remote.go | 61 | ||||
-rw-r--r-- | remote_test.go | 62 | ||||
-rw-r--r-- | repository.go | 29 |
6 files changed, 87 insertions, 95 deletions
@@ -3,8 +3,18 @@ package git import ( "io" "strings" + + "gopkg.in/src-d/go-git.v4/config" + "gopkg.in/src-d/go-git.v4/core" ) +// Storage storage of objects and references +type Storage interface { + ConfigStorage() config.ConfigStorage + ObjectStorage() core.ObjectStorage + ReferenceStorage() core.ReferenceStorage +} + // countLines returns the number of lines in a string à la git, this is // The newline character is assumed to be '\n'. The empty string // contains 0 lines. If the last line of the string doesn't end with a diff --git a/config/config.go b/config/config.go index 88172f4..6cc9036 100644 --- a/config/config.go +++ b/config/config.go @@ -1,9 +1,15 @@ package config -type Config interface { - Remote(name string) *RemoteConfig - Remotes() []*RemoteConfig - SetRemote(*RemoteConfig) +import "errors" + +var ( + ErrRemoteConfigNotFound = errors.New("remote config not found") +) + +type ConfigStorage interface { + Remote(name string) (*RemoteConfig, error) + Remotes() ([]*RemoteConfig, error) + SetRemote(*RemoteConfig) error } type RemoteConfig struct { diff --git a/core/storage.go b/core/storage.go index 6403f5f..f3225d8 100644 --- a/core/storage.go +++ b/core/storage.go @@ -5,12 +5,6 @@ import "errors" //ErrStop is used to stop a ForEach function in an Iter var ErrStop = errors.New("stop iter") -// Storage storage of objects and references -type Storage interface { - ObjectStorage() ObjectStorage - ReferenceStorage() ReferenceStorage -} - // ObjectStorage generic storage of objects type ObjectStorage interface { NewObject() Object @@ -12,56 +12,40 @@ import ( // Remote represents a connection to a remote repository type Remote struct { - config *config.RemoteConfig - - Name string - Endpoint common.Endpoint - Auth common.AuthMethod + Config *config.RemoteConfig + s Storage + // cache fields, there during the connection is open upSrv common.GitUploadPackService upInfo *common.GitUploadPackInfo } // NewRemote returns a new Remote, using as client http.DefaultClient -func NewRemote(name, url string) (*Remote, error) { - return NewAuthenticatedRemote(name, url, nil) +func NewRemote(s Storage, c *config.RemoteConfig) *Remote { + return &Remote{Config: c, s: s} } -// NewAuthenticatedRemote returns a new Remote using the given AuthMethod, using as -// client http.DefaultClient -func NewAuthenticatedRemote(name, url string, auth common.AuthMethod) (*Remote, error) { - endpoint, err := common.NewEndpoint(url) - if err != nil { - return nil, err - } - - upSrv, err := clients.NewGitUploadPackService(endpoint) - if err != nil { - return nil, err +// Connect with the endpoint +func (r *Remote) Connect() error { + if err := r.connectUploadPackService(); err != nil { + return err } - return &Remote{ - Endpoint: endpoint, - Name: name, - Auth: auth, - upSrv: upSrv, - }, nil + return r.retrieveUpInfo() } -// Connect with the endpoint -func (r *Remote) Connect() error { - var err error - if r.Auth == nil { - err = r.upSrv.Connect() - } else { - err = r.upSrv.ConnectWithAuth(r.Auth) +func (r *Remote) connectUploadPackService() error { + endpoint, err := common.NewEndpoint(r.Config.URL) + if err != nil { + return err } + r.upSrv, err = clients.NewGitUploadPackService(endpoint) if err != nil { return err } - return r.retrieveUpInfo() + return r.upSrv.Connect() } func (r *Remote) retrieveUpInfo() error { @@ -84,7 +68,7 @@ func (r *Remote) Capabilities() *common.Capabilities { } // Fetch returns a reader using the request -func (r *Remote) Fetch(s core.Storage, o *RemoteFetchOptions) (err error) { +func (r *Remote) Fetch(o *RemoteFetchOptions) (err error) { if err := o.Validate(); err != nil { return err } @@ -94,7 +78,7 @@ func (r *Remote) Fetch(s core.Storage, o *RemoteFetchOptions) (err error) { return err } - req, err := r.buildRequest(s.ReferenceStorage(), o, refs) + req, err := r.buildRequest(r.s.ReferenceStorage(), o, refs) if err != nil { return err } @@ -105,11 +89,11 @@ func (r *Remote) Fetch(s core.Storage, o *RemoteFetchOptions) (err error) { } defer checkClose(reader, &err) - if err := r.updateObjectStorage(s.ObjectStorage(), reader); err != nil { + if err := r.updateObjectStorage(r.s.ObjectStorage(), reader); err != nil { return err } - return r.updateLocalReferenceStorage(s.ReferenceStorage(), o.RefSpec, refs) + return r.updateLocalReferenceStorage(r.s.ReferenceStorage(), o.RefSpec, refs) } func (r *Remote) getWantedReferences(spec config.RefSpec) ([]*core.Reference, error) { @@ -204,7 +188,12 @@ func (r *Remote) Refs() core.ReferenceIter { return i } +// Disconnect from the remote and save the config func (r *Remote) Disconnect() error { + if err := r.s.ConfigStorage().SetRemote(r.Config); err != nil { + return err + } + r.upInfo = nil return r.upSrv.Disconnect() } diff --git a/remote_test.go b/remote_test.go index 451f801..1bac2fb 100644 --- a/remote_test.go +++ b/remote_test.go @@ -1,7 +1,7 @@ package git import ( - "gopkg.in/src-d/go-git.v4/clients/http" + "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/core" "gopkg.in/src-d/go-git.v4/storage/memory" @@ -14,25 +14,28 @@ type RemoteSuite struct { var _ = Suite(&RemoteSuite{}) -func (s *RemoteSuite) TestNewRemote(c *C) { - r, err := NewRemote("foo", RepositoryFixture) +func (s *RemoteSuite) TestConnect(c *C) { + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + + err := r.Connect() c.Assert(err, IsNil) - c.Assert(r.Name, Equals, "foo") - c.Assert(r.Endpoint.String(), Equals, RepositoryFixture) } func (s *RemoteSuite) TestNewRemoteInvalidEndpoint(c *C) { - r, err := NewRemote("foo", "qux") + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"}) + + err := r.Connect() c.Assert(err, NotNil) - c.Assert(r, IsNil) } func (s *RemoteSuite) TestNewRemoteInvalidSchemaEndpoint(c *C) { - r, err := NewRemote("foo", "qux://foo") + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"}) + + err := r.Connect() c.Assert(err, NotNil) - c.Assert(r, IsNil) } +/* func (s *RemoteSuite) TestNewAuthenticatedRemote(c *C) { a := &http.BasicAuth{} r, err := NewAuthenticatedRemote("foo", RepositoryFixture, a) @@ -40,17 +43,12 @@ func (s *RemoteSuite) TestNewAuthenticatedRemote(c *C) { c.Assert(r.Name, Equals, "foo") c.Assert(r.Endpoint.String(), Equals, RepositoryFixture) c.Assert(r.Auth, Equals, a) -} - -func (s *RemoteSuite) TestConnect(c *C) { - r, err := NewRemote("foo", RepositoryFixture) - c.Assert(err, IsNil) - c.Assert(r.Connect(), IsNil) -} +}*/ func (s *RemoteSuite) TestInfo(c *C) { - r, err := NewRemote("foo", RepositoryFixture) - c.Assert(err, IsNil) + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r.upSrv = &MockGitUploadPackService{} + c.Assert(r.Info(), IsNil) c.Assert(r.Connect(), IsNil) c.Assert(r.Info(), NotNil) @@ -58,32 +56,29 @@ func (s *RemoteSuite) TestInfo(c *C) { } func (s *RemoteSuite) TestDefaultBranch(c *C) { - r, err := NewRemote("foo", RepositoryFixture) + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} - c.Assert(err, IsNil) c.Assert(r.Connect(), IsNil) c.Assert(r.Head().Name(), Equals, core.ReferenceName("refs/heads/master")) } func (s *RemoteSuite) TestCapabilities(c *C) { - r, err := NewRemote("foo", RepositoryFixture) + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} - c.Assert(err, IsNil) c.Assert(r.Connect(), IsNil) c.Assert(r.Capabilities().Get("agent").Values, HasLen, 1) } func (s *RemoteSuite) TestFetch(c *C) { - r, err := NewRemote("foo", RepositoryFixture) + sto := memory.NewStorage() + r := NewRemote(sto, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} - c.Assert(err, IsNil) c.Assert(r.Connect(), IsNil) - sto := memory.NewStorage() - err = r.Fetch(sto, &RemoteFetchOptions{ + err := r.Fetch(&RemoteFetchOptions{ RefSpec: DefaultRefSpec, }) @@ -92,21 +87,19 @@ func (s *RemoteSuite) TestFetch(c *C) { } func (s *RemoteSuite) TestHead(c *C) { - r, err := NewRemote("foo", RepositoryFixture) + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} - c.Assert(err, IsNil) - err = r.Connect() + err := r.Connect() c.Assert(err, IsNil) c.Assert(r.Head().Hash(), Equals, core.NewHash("6ecf0ef2c2dffb796033e5a02219af86ec6584e5")) } func (s *RemoteSuite) TestRef(c *C) { - r, err := NewRemote("foo", RepositoryFixture) + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} - c.Assert(err, IsNil) - err = r.Connect() + err := r.Connect() c.Assert(err, IsNil) ref, err := r.Ref(core.HEAD, false) @@ -119,11 +112,10 @@ func (s *RemoteSuite) TestRef(c *C) { } func (s *RemoteSuite) TestRefs(c *C) { - r, err := NewRemote("foo", RepositoryFixture) + r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} - c.Assert(err, IsNil) - err = r.Connect() + err := r.Connect() c.Assert(err, IsNil) c.Assert(r.Refs(), NotNil) } diff --git a/repository.go b/repository.go index ada0bc4..d36ca05 100644 --- a/repository.go +++ b/repository.go @@ -3,11 +3,9 @@ package git import ( "errors" - "gopkg.in/src-d/go-git.v4/clients/common" + "gopkg.in/src-d/go-git.v4/config" "gopkg.in/src-d/go-git.v4/core" - "gopkg.in/src-d/go-git.v4/storage/filesystem" "gopkg.in/src-d/go-git.v4/storage/memory" - "gopkg.in/src-d/go-git.v4/utils/fs" ) var ( @@ -19,7 +17,7 @@ var ( // Repository giturl string, auth common.AuthMethod repository struct type Repository struct { Remotes map[string]*Remote - s core.Storage + s Storage } // NewMemoryRepository creates a new repository, backed by a memory.Storage @@ -27,6 +25,7 @@ func NewMemoryRepository() (*Repository, error) { return NewRepository(memory.NewStorage()) } +/* // NewFilesystemRepository creates a new repository, backed by a filesystem.Storage // based on a fs.OS, if you want to use a custom one you need to use the function // NewRepository and build you filesystem.Storage @@ -38,9 +37,10 @@ func NewFilesystemRepository(path string) (*Repository, error) { return NewRepository(s) } +*/ // NewRepository creates a new repository with the given Storage -func NewRepository(s core.Storage) (*Repository, error) { +func NewRepository(s Storage) (*Repository, error) { return &Repository{s: s}, nil } @@ -50,7 +50,11 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { return err } - remote, err := r.createRemote(o.RemoteName, o.URL, o.Auth) + remote, err := r.createRemote(&config.RemoteConfig{ + Name: o.RemoteName, + URL: o.URL, + }) + if err != nil { return err } @@ -66,7 +70,7 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { return err } - err = remote.Fetch(r.s, &RemoteFetchOptions{ + err = remote.Fetch(&RemoteFetchOptions{ RefSpec: spec, Depth: o.Depth, }) @@ -83,13 +87,10 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { return r.createReferences(head) } -func (r *Repository) createRemote(name, url string, auth common.AuthMethod) (*Remote, error) { - remote, err := NewAuthenticatedRemote(name, url, auth) - if err != nil { - return nil, err - } +func (r *Repository) createRemote(c *config.RemoteConfig) (*Remote, error) { + remote := NewRemote(r.s, c) - r.Remotes = map[string]*Remote{name: remote} + r.Remotes = map[string]*Remote{c.Name: remote} return remote, nil } @@ -130,7 +131,7 @@ func (r *Repository) Pull(o *RepositoryPullOptions) error { defer remote.Disconnect() - err = remote.Fetch(r.s, &RemoteFetchOptions{ + err = remote.Fetch(&RemoteFetchOptions{ Depth: o.Depth, }) |