aboutsummaryrefslogtreecommitdiffstats
path: root/config/refspec.go
diff options
context:
space:
mode:
Diffstat (limited to 'config/refspec.go')
-rw-r--r--config/refspec.go22
1 files changed, 16 insertions, 6 deletions
diff --git a/config/refspec.go b/config/refspec.go
index dca0d82..dd68edc 100644
--- a/config/refspec.go
+++ b/config/refspec.go
@@ -1,6 +1,7 @@
package config
import (
+ "errors"
"strings"
"srcd.works/go-git.v4/plumbing"
@@ -12,6 +13,11 @@ const (
refSpecSeparator = ":"
)
+var (
+ ErrRefSpecMalformedSeparator = errors.New("malformed refspec, separators are wrong")
+ ErrRefSpecMalformedWildcard = errors.New("malformed refspec, missmatched number of wildcards")
+)
+
// RefSpec is a mapping from local branches to remote references
// The format of the refspec is an optional +, followed by <src>:<dst>, where
// <src> is the pattern for references on the remote side and <dst> is where
@@ -22,21 +28,25 @@ const (
// https://git-scm.com/book/es/v2/Git-Internals-The-Refspec
type RefSpec string
-// IsValid validates the RefSpec
-func (s RefSpec) IsValid() bool {
+// Validate validates the RefSpec
+func (s RefSpec) Validate() error {
spec := string(s)
if strings.Count(spec, refSpecSeparator) != 1 {
- return false
+ return ErrRefSpecMalformedSeparator
}
sep := strings.Index(spec, refSpecSeparator)
- if sep == len(spec) {
- return false
+ if sep == len(spec)-1 {
+ return ErrRefSpecMalformedSeparator
}
ws := strings.Count(spec[0:sep], refSpecWildcard)
wd := strings.Count(spec[sep+1:], refSpecWildcard)
- return ws == wd && ws < 2 && wd < 2
+ if ws == wd && ws < 2 && wd < 2 {
+ return nil
+ }
+
+ return ErrRefSpecMalformedWildcard
}
// IsForceUpdate returns if update is allowed in non fast-forward merges