aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2019-03-25 01:02:31 +0100
committerGitHub <noreply@github.com>2019-03-25 01:02:31 +0100
commite7099381e2fbc7bc0d46184f392a26383dc3ffbe (patch)
treee5f58c46b246c999360f75a2b594fcc0e1cd2c0b
parent79081164bccd82fb12c7bfd058554812113eb7a4 (diff)
parent8b789adc353780b4a6688e8d2b3e8f47e60bf487 (diff)
downloadgo-git-e7099381e2fbc7bc0d46184f392a26383dc3ffbe.tar.gz
Merge pull request #1094 from niukuo/remote_anonymous
add Repository.CreateRemoteAnonymous
-rw-r--r--repository.go17
-rw-r--r--repository_test.go30
2 files changed, 47 insertions, 0 deletions
diff --git a/repository.go b/repository.go
index de92d64..e5b12b0 100644
--- a/repository.go
+++ b/repository.go
@@ -49,6 +49,7 @@ var (
ErrRepositoryAlreadyExists = errors.New("repository already exists")
ErrRemoteNotFound = errors.New("remote not found")
ErrRemoteExists = errors.New("remote already exists")
+ ErrAnonymousRemoteName = errors.New("anonymous remote name must be 'anonymous'")
ErrWorktreeNotProvided = errors.New("worktree should be provided")
ErrIsBareRepository = errors.New("worktree not available in a bare repository")
ErrUnableToResolveCommit = errors.New("unable to resolve commit")
@@ -492,6 +493,22 @@ func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) {
return remote, r.Storer.SetConfig(cfg)
}
+// CreateRemoteAnonymous creates a new anonymous remote. c.Name must be "anonymous".
+// It's used like 'git fetch git@github.com:src-d/go-git.git master:master'.
+func (r *Repository) CreateRemoteAnonymous(c *config.RemoteConfig) (*Remote, error) {
+ if err := c.Validate(); err != nil {
+ return nil, err
+ }
+
+ if c.Name != "anonymous" {
+ return nil, ErrAnonymousRemoteName
+ }
+
+ remote := newRemote(r.Storer, c)
+
+ return remote, nil
+}
+
// DeleteRemote delete a remote from the repository and delete the config
func (r *Repository) DeleteRemote(name string) error {
cfg, err := r.Storer.Config()
diff --git a/repository_test.go b/repository_test.go
index 1549737..ccbe29b 100644
--- a/repository_test.go
+++ b/repository_test.go
@@ -231,6 +231,36 @@ func (s *RepositorySuite) TestCreateRemoteInvalid(c *C) {
c.Assert(remote, IsNil)
}
+func (s *RepositorySuite) TestCreateRemoteAnonymous(c *C) {
+ r, _ := Init(memory.NewStorage(), nil)
+ remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
+ Name: "anonymous",
+ URLs: []string{"http://foo/foo.git"},
+ })
+
+ c.Assert(err, IsNil)
+ c.Assert(remote.Config().Name, Equals, "anonymous")
+}
+
+func (s *RepositorySuite) TestCreateRemoteAnonymousInvalidName(c *C) {
+ r, _ := Init(memory.NewStorage(), nil)
+ remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{
+ Name: "not_anonymous",
+ URLs: []string{"http://foo/foo.git"},
+ })
+
+ c.Assert(err, Equals, ErrAnonymousRemoteName)
+ c.Assert(remote, IsNil)
+}
+
+func (s *RepositorySuite) TestCreateRemoteAnonymousInvalid(c *C) {
+ r, _ := Init(memory.NewStorage(), nil)
+ remote, err := r.CreateRemoteAnonymous(&config.RemoteConfig{})
+
+ c.Assert(err, Equals, config.ErrRemoteConfigEmptyName)
+ c.Assert(remote, IsNil)
+}
+
func (s *RepositorySuite) TestDeleteRemote(c *C) {
r, _ := Init(memory.NewStorage(), nil)
_, err := r.CreateRemote(&config.RemoteConfig{