Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit e709938

Browse files
authored
Merge pull request #1094 from niukuo/remote_anonymous
add Repository.CreateRemoteAnonymous
2 parents 7908116 + 8b789ad commit e709938

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

repository.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var (
4949
ErrRepositoryAlreadyExists = errors.New("repository already exists")
5050
ErrRemoteNotFound = errors.New("remote not found")
5151
ErrRemoteExists = errors.New("remote already exists")
52+
ErrAnonymousRemoteName = errors.New("anonymous remote name must be 'anonymous'")
5253
ErrWorktreeNotProvided = errors.New("worktree should be provided")
5354
ErrIsBareRepository = errors.New("worktree not available in a bare repository")
5455
ErrUnableToResolveCommit = errors.New("unable to resolve commit")
@@ -492,6 +493,22 @@ func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) {
492493
return remote, r.Storer.SetConfig(cfg)
493494
}
494495

496+
// CreateRemoteAnonymous creates a new anonymous remote. c.Name must be "anonymous".
497+
// It's used like 'git fetch [email protected]:src-d/go-git.git master:master'.
498+
func (r *Repository) CreateRemoteAnonymous(c *config.RemoteConfig) (*Remote, error) {
499+
if err := c.Validate(); err != nil {
500+
return nil, err
501+
}
502+
503+
if c.Name != "anonymous" {
504+
return nil, ErrAnonymousRemoteName
505+
}
506+
507+
remote := newRemote(r.Storer, c)
508+
509+
return remote, nil
510+
}
511+
495512
// DeleteRemote delete a remote from the repository and delete the config
496513
func (r *Repository) DeleteRemote(name string) error {
497514
cfg, err := r.Storer.Config()

repository_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,36 @@ func (s *RepositorySuite) TestCreateRemoteInvalid(c *C) {
231231
c.Assert(remote, IsNil)
232232
}
233233

234+
func (s *RepositorySuite) TestCreateRemoteAnonymous(c *C) {
235+
r, _ := Init(memory.NewStorage(), nil)
236+
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
237+
Name: "anonymous",
238+
URLs: []string{"http://foo/foo.git"},
239+
})
240+
241+
c.Assert(err, IsNil)
242+
c.Assert(remote.Config().Name, Equals, "anonymous")
243+
}
244+
245+
func (s *RepositorySuite) TestCreateRemoteAnonymousInvalidName(c *C) {
246+
r, _ := Init(memory.NewStorage(), nil)
247+
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
248+
Name: "not_anonymous",
249+
URLs: []string{"http://foo/foo.git"},
250+
})
251+
252+
c.Assert(err, Equals, ErrAnonymousRemoteName)
253+
c.Assert(remote, IsNil)
254+
}
255+
256+
func (s *RepositorySuite) TestCreateRemoteAnonymousInvalid(c *C) {
257+
r, _ := Init(memory.NewStorage(), nil)
258+
remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{})
259+
260+
c.Assert(err, Equals, config.ErrRemoteConfigEmptyName)
261+
c.Assert(remote, IsNil)
262+
}
263+
234264
func (s *RepositorySuite) TestDeleteRemote(c *C) {
235265
r, _ := Init(memory.NewStorage(), nil)
236266
_, err := r.CreateRemote(&config.RemoteConfig{

0 commit comments

Comments
 (0)