aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
Diffstat (limited to 'config')
-rw-r--r--config/config.go38
-rw-r--r--config/refspec.go11
-rw-r--r--config/refspec_test.go10
3 files changed, 56 insertions, 3 deletions
diff --git a/config/config.go b/config/config.go
index b70cebc..4cf5f7c 100644
--- a/config/config.go
+++ b/config/config.go
@@ -1,9 +1,20 @@
package config
-import "errors"
+import (
+ "errors"
+ "fmt"
+
+ "gopkg.in/src-d/go-git.v3/clients/common"
+)
+
+const (
+ DefaultRefSpec = "+refs/heads/*:refs/remotes/%s/*"
+)
var (
- ErrRemoteConfigNotFound = errors.New("remote config not found")
+ ErrRemoteConfigNotFound = errors.New("remote config not found")
+ ErrRemoteConfigEmptyURL = errors.New("remote config: empty URL")
+ ErrRemoteConfigEmptyName = errors.New("remote config: empty name")
)
type ConfigStorage interface {
@@ -16,5 +27,26 @@ type ConfigStorage interface {
type RemoteConfig struct {
Name string
URL string
- Fetch RefSpec
+ Fetch []RefSpec
+}
+
+// Validate validate the fields and set the default values
+func (c *RemoteConfig) Validate() error {
+ if c.Name == "" {
+ return ErrRemoteConfigEmptyName
+ }
+
+ if c.URL == "" {
+ return ErrRemoteConfigEmptyURL
+ }
+
+ if _, err := common.NewEndpoint(c.URL); err != nil {
+ return err
+ }
+
+ if len(c.Fetch) == 0 {
+ c.Fetch = []RefSpec{RefSpec(fmt.Sprintf(DefaultRefSpec, c.Name))}
+ }
+
+ return nil
}
diff --git a/config/refspec.go b/config/refspec.go
index e74bf78..dae13be 100644
--- a/config/refspec.go
+++ b/config/refspec.go
@@ -112,3 +112,14 @@ func (s RefSpec) Dst(n core.ReferenceName) core.ReferenceName {
func (s RefSpec) String() string {
return string(s)
}
+
+// MatchAny returns true if any of the RefSpec match with the given ReferenceName
+func MatchAny(l []RefSpec, n core.ReferenceName) bool {
+ for _, r := range l {
+ if r.Match(n) {
+ return true
+ }
+ }
+
+ return false
+}
diff --git a/config/refspec_test.go b/config/refspec_test.go
index b0bb8f5..632f5a4 100644
--- a/config/refspec_test.go
+++ b/config/refspec_test.go
@@ -67,3 +67,13 @@ func (s *RefSpecSuite) TestRefSpecDstBlob(c *C) {
"refs/remotes/origin/foo",
)
}
+func (s *RefSpecSuite) TestMatchAny(c *C) {
+ specs := []RefSpec{
+ "refs/heads/bar:refs/remotes/origin/foo",
+ "refs/heads/foo:refs/remotes/origin/bar",
+ }
+
+ c.Assert(MatchAny(specs, core.ReferenceName("refs/heads/foo")), Equals, true)
+ c.Assert(MatchAny(specs, core.ReferenceName("refs/heads/bar")), Equals, true)
+ c.Assert(MatchAny(specs, core.ReferenceName("refs/heads/master")), Equals, false)
+}