From 387683bbcf40ed48e0f1ad0884970712f9682b9e Mon Sep 17 00:00:00 2001 From: Máximo Cuadros Date: Tue, 31 Jan 2017 12:00:40 +0100 Subject: config: RefSpec.Validate returning errors and doc (Fixes #232) --- config/refspec.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'config/refspec.go') 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 :, where // is the pattern for references on the remote side and 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 -- cgit