aboutsummaryrefslogtreecommitdiffstats
path: root/config/refspec.go
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2017-01-31 12:00:40 +0100
committerMáximo Cuadros <mcuadros@gmail.com>2017-01-31 12:00:40 +0100
commit387683bbcf40ed48e0f1ad0884970712f9682b9e (patch)
treec13eee66eb19b4126d69543312efa12c7a7d389f /config/refspec.go
parentf7da595e8aef658cd3dfed897b84ada2b0eac921 (diff)
downloadgo-git-387683bbcf40ed48e0f1ad0884970712f9682b9e.tar.gz
config: RefSpec.Validate returning errors and doc (Fixes #232)
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