diff options
-rw-r--r-- | config/config.go | 1 | ||||
-rw-r--r-- | config/refspec.go | 4 | ||||
-rw-r--r-- | formats/packfile/decoder_test.go | 22 | ||||
-rw-r--r-- | remote.go | 15 | ||||
-rw-r--r-- | remote_test.go | 24 | ||||
-rw-r--r-- | repository.go | 69 | ||||
-rw-r--r-- | repository_test.go | 25 | ||||
-rw-r--r-- | storage/memory/storage.go | 5 |
8 files changed, 103 insertions, 62 deletions
diff --git a/config/config.go b/config/config.go index 6cc9036..b70cebc 100644 --- a/config/config.go +++ b/config/config.go @@ -10,6 +10,7 @@ type ConfigStorage interface { Remote(name string) (*RemoteConfig, error) Remotes() ([]*RemoteConfig, error) SetRemote(*RemoteConfig) error + DeleteRemote(name string) error } type RemoteConfig struct { diff --git a/config/refspec.go b/config/refspec.go index 2fe8d22..e74bf78 100644 --- a/config/refspec.go +++ b/config/refspec.go @@ -108,3 +108,7 @@ func (s RefSpec) Dst(n core.ReferenceName) core.ReferenceName { return core.ReferenceName(dst[0:wd] + match + dst[wd+1:len(dst)]) } + +func (s RefSpec) String() string { + return string(s) +} diff --git a/formats/packfile/decoder_test.go b/formats/packfile/decoder_test.go index 6bafa4f..31d86ad 100644 --- a/formats/packfile/decoder_test.go +++ b/formats/packfile/decoder_test.go @@ -30,8 +30,8 @@ func (s *ReaderSuite) TestReadPackfile(c *C) { r := NewStream(f) d := NewDecoder(r) - sto := memory.NewObjectStorage() - err := d.Decode(sto) + sto := memory.NewStorage() + err := d.Decode(sto.ObjectStorage()) c.Assert(err, IsNil) AssertObjects(c, sto, []string{ @@ -63,8 +63,8 @@ func (s *ReaderSuite) testReadPackfileGitFixture(c *C, file string, format Forma r := NewSeekable(f) d := NewDecoder(r) - sto := memory.NewObjectStorage() - err = d.Decode(sto) + sto := memory.NewStorage() + err = d.Decode(sto.ObjectStorage()) c.Assert(err, IsNil) AssertObjects(c, sto, []string{ @@ -99,10 +99,12 @@ func (s *ReaderSuite) testReadPackfileGitFixture(c *C, file string, format Forma }) } -func AssertObjects(c *C, s *memory.ObjectStorage, expects []string) { - c.Assert(len(expects), Equals, len(s.Objects)) +func AssertObjects(c *C, s *memory.Storage, expects []string) { + o := s.ObjectStorage().(*memory.ObjectStorage) + + c.Assert(len(expects), Equals, len(o.Objects)) for _, exp := range expects { - obt, err := s.Get(core.NewHash(exp)) + obt, err := o.Get(core.NewHash(exp)) c.Assert(err, IsNil) c.Assert(obt.Hash().String(), Equals, exp) } @@ -170,9 +172,9 @@ func readFromFile(c *C, file string, format Format) *memory.ObjectStorage { r := NewSeekable(f) d := NewDecoder(r) - sto := memory.NewObjectStorage() - err = d.Decode(sto) + sto := memory.NewStorage() + err = d.Decode(sto.ObjectStorage()) c.Assert(err, IsNil) - return sto + return sto.ObjectStorage().(*memory.ObjectStorage) } @@ -12,17 +12,16 @@ import ( // Remote represents a connection to a remote repository type Remote struct { - Config *config.RemoteConfig - + c *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(s Storage, c *config.RemoteConfig) *Remote { - return &Remote{Config: c, s: s} +func newRemote(s Storage, c *config.RemoteConfig) *Remote { + return &Remote{s: s, c: c} } // Connect with the endpoint @@ -35,7 +34,7 @@ func (r *Remote) Connect() error { } func (r *Remote) connectUploadPackService() error { - endpoint, err := common.NewEndpoint(r.Config.URL) + endpoint, err := common.NewEndpoint(r.c.URL) if err != nil { return err } @@ -190,10 +189,6 @@ func (r *Remote) Refs() core.ReferenceIter { // 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 1bac2fb..bc81dad 100644 --- a/remote_test.go +++ b/remote_test.go @@ -15,21 +15,21 @@ type RemoteSuite struct { var _ = Suite(&RemoteSuite{}) func (s *RemoteSuite) TestConnect(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) err := r.Connect() c.Assert(err, IsNil) } -func (s *RemoteSuite) TestNewRemoteInvalidEndpoint(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"}) +func (s *RemoteSuite) TestnewRemoteInvalidEndpoint(c *C) { + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux"}) err := r.Connect() c.Assert(err, NotNil) } -func (s *RemoteSuite) TestNewRemoteInvalidSchemaEndpoint(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"}) +func (s *RemoteSuite) TestnewRemoteInvalidSchemaEndpoint(c *C) { + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: "qux://foo"}) err := r.Connect() c.Assert(err, NotNil) @@ -46,7 +46,7 @@ func (s *RemoteSuite) TestNewAuthenticatedRemote(c *C) { }*/ func (s *RemoteSuite) TestInfo(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} c.Assert(r.Info(), IsNil) @@ -56,7 +56,7 @@ func (s *RemoteSuite) TestInfo(c *C) { } func (s *RemoteSuite) TestDefaultBranch(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} c.Assert(r.Connect(), IsNil) @@ -64,7 +64,7 @@ func (s *RemoteSuite) TestDefaultBranch(c *C) { } func (s *RemoteSuite) TestCapabilities(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} c.Assert(r.Connect(), IsNil) @@ -73,7 +73,7 @@ func (s *RemoteSuite) TestCapabilities(c *C) { func (s *RemoteSuite) TestFetch(c *C) { sto := memory.NewStorage() - r := NewRemote(sto, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(sto, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} c.Assert(r.Connect(), IsNil) @@ -87,7 +87,7 @@ func (s *RemoteSuite) TestFetch(c *C) { } func (s *RemoteSuite) TestHead(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} err := r.Connect() @@ -96,7 +96,7 @@ func (s *RemoteSuite) TestHead(c *C) { } func (s *RemoteSuite) TestRef(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} err := r.Connect() @@ -112,7 +112,7 @@ func (s *RemoteSuite) TestRef(c *C) { } func (s *RemoteSuite) TestRefs(c *C) { - r := NewRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) + r := newRemote(nil, &config.RemoteConfig{Name: "foo", URL: RepositoryFixture}) r.upSrv = &MockGitUploadPackService{} err := r.Connect() diff --git a/repository.go b/repository.go index d36ca05..da4aada 100644 --- a/repository.go +++ b/repository.go @@ -5,19 +5,20 @@ import ( "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 ( ErrObjectNotFound = errors.New("object not found") - ErrUnknownRemote = errors.New("unknown remote") ErrInvalidReference = errors.New("invalid reference, should be a tag or a branch") ) // Repository giturl string, auth common.AuthMethod repository struct type Repository struct { - Remotes map[string]*Remote - s Storage + r map[string]*Remote + s Storage } // NewMemoryRepository creates a new repository, backed by a memory.Storage @@ -25,7 +26,6 @@ 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 @@ -37,11 +37,53 @@ func NewFilesystemRepository(path string) (*Repository, error) { return NewRepository(s) } -*/ // NewRepository creates a new repository with the given Storage func NewRepository(s Storage) (*Repository, error) { - return &Repository{s: s}, nil + return &Repository{ + s: s, + r: make(map[string]*Remote, 0), + }, nil +} + +// Remote return a remote if exists +func (r *Repository) Remote(name string) (*Remote, error) { + c, err := r.s.ConfigStorage().Remote(name) + if err != nil { + return nil, err + } + + return newRemote(r.s, c), nil +} + +// Remotes return all the remotes +func (r *Repository) Remotes() ([]*Remote, error) { + config, err := r.s.ConfigStorage().Remotes() + if err != nil { + return nil, err + } + + remotes := make([]*Remote, len(config)) + for i, c := range config { + remotes[i] = newRemote(r.s, c) + } + + return remotes, nil +} + +// CreateRemote creates a new remote +func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) { + remote := newRemote(r.s, c) + if err := r.s.ConfigStorage().SetRemote(c); err != nil { + return nil, err + } + + return remote, nil +} + +// DeleteRemote delete a remote from the repository and delete the config +func (r *Repository) DeleteRemote(name string) error { + return r.s.ConfigStorage().DeleteRemote(name) } // Clone clones a remote repository @@ -50,7 +92,7 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { return err } - remote, err := r.createRemote(&config.RemoteConfig{ + remote, err := r.CreateRemote(&config.RemoteConfig{ Name: o.RemoteName, URL: o.URL, }) @@ -87,13 +129,6 @@ func (r *Repository) Clone(o *RepositoryCloneOptions) error { return r.createReferences(head) } -func (r *Repository) createRemote(c *config.RemoteConfig) (*Remote, error) { - remote := NewRemote(r.s, c) - - r.Remotes = map[string]*Remote{c.Name: remote} - return remote, nil -} - func (r *Repository) createReferences(ref *core.Reference) error { if !ref.IsBranch() { // detached HEAD mode @@ -115,9 +150,9 @@ func (r *Repository) Pull(o *RepositoryPullOptions) error { return err } - remote, ok := r.Remotes[o.RemoteName] - if !ok { - return ErrUnknownRemote + remote, err := r.Remote(o.RemoteName) + if err != nil { + return err } head, err := remote.Ref(o.ReferenceName, true) diff --git a/repository_test.go b/repository_test.go index 2539a0b..462fbcf 100644 --- a/repository_test.go +++ b/repository_test.go @@ -70,8 +70,6 @@ func (s *RepositorySuite) TestClone(c *C) { r, err := NewMemoryRepository() c.Assert(err, IsNil) - c.Assert(r.Remotes, HasLen, 0) - head, err := r.Head() c.Assert(err, Equals, core.ErrReferenceNotFound) c.Assert(head, IsNil) @@ -81,8 +79,10 @@ func (s *RepositorySuite) TestClone(c *C) { }) c.Assert(err, IsNil) - c.Assert(r.Remotes, HasLen, 1) - c.Assert(r.Remotes[DefaultRemoteName], NotNil) + + remotes, err := r.Remotes() + c.Assert(err, IsNil) + c.Assert(remotes, HasLen, 1) head, err = r.Ref(core.HEAD, false) c.Assert(err, IsNil) @@ -112,8 +112,6 @@ func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) { r, err := NewMemoryRepository() c.Assert(err, IsNil) - c.Assert(r.Remotes, HasLen, 0) - head, err := r.Head() c.Assert(err, Equals, core.ErrReferenceNotFound) c.Assert(head, IsNil) @@ -125,8 +123,10 @@ func (s *RepositorySuite) TestCloneSingleBranchAndNonHEAD(c *C) { }) c.Assert(err, IsNil) - c.Assert(r.Remotes, HasLen, 1) - c.Assert(r.Remotes[DefaultRemoteName], NotNil) + + remotes, err := r.Remotes() + c.Assert(err, IsNil) + c.Assert(remotes, HasLen, 1) head, err = r.Ref(core.HEAD, false) c.Assert(err, IsNil) @@ -150,8 +150,6 @@ func (s *RepositorySuite) TestCloneSingleBranch(c *C) { r, err := NewMemoryRepository() c.Assert(err, IsNil) - c.Assert(r.Remotes, HasLen, 0) - head, err := r.Head() c.Assert(err, Equals, core.ErrReferenceNotFound) c.Assert(head, IsNil) @@ -162,8 +160,10 @@ func (s *RepositorySuite) TestCloneSingleBranch(c *C) { }) c.Assert(err, IsNil) - c.Assert(r.Remotes, HasLen, 1) - c.Assert(r.Remotes[DefaultRemoteName], NotNil) + + remotes, err := r.Remotes() + c.Assert(err, IsNil) + c.Assert(remotes, HasLen, 1) head, err = r.Ref(core.HEAD, false) c.Assert(err, IsNil) @@ -187,7 +187,6 @@ func (s *RepositorySuite) TestCloneDetachedHEAD(c *C) { r, err := NewMemoryRepository() c.Assert(err, IsNil) - c.Assert(r.Remotes, HasLen, 0) err = r.Clone(&RepositoryCloneOptions{ URL: RepositoryFixture, ReferenceName: core.ReferenceName("refs/tags/v1.0.0"), diff --git a/storage/memory/storage.go b/storage/memory/storage.go index e242008..d67368f 100644 --- a/storage/memory/storage.go +++ b/storage/memory/storage.go @@ -89,6 +89,11 @@ func (c *ConfigStorage) SetRemote(r *config.RemoteConfig) error { return nil } +func (c *ConfigStorage) DeleteRemote(name string) error { + delete(c.RemotesConfig, name) + return nil +} + // ObjectStorage is the implementation of core.ObjectStorage for memory.Object type ObjectStorage struct { Objects map[core.Hash]core.Object |