aboutsummaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
authorMáximo Cuadros <mcuadros@gmail.com>2020-05-11 00:28:21 +0200
committerGitHub <noreply@github.com>2020-05-11 00:28:21 +0200
commit568154cab87659b31c751678c7f7dc93fedf60af (patch)
tree233f993126f9346b06979202042bd5d95ba70bf2 /config
parente4166c560bfa0bcdadd3197ad00365b4f1c027fb (diff)
parent8ecd388ae101a0dd88b78dc47ebfef9fe51699df (diff)
downloadgo-git-568154cab87659b31c751678c7f7dc93fedf60af.tar.gz
Merge pull request #58 from go-git/exact-sha1
Remote.Fetch: support exact SHA1 refspecs
Diffstat (limited to 'config')
-rw-r--r--config/refspec.go9
-rw-r--r--config/refspec_test.go16
2 files changed, 22 insertions, 3 deletions
diff --git a/config/refspec.go b/config/refspec.go
index 87cf2a6..4bfaa37 100644
--- a/config/refspec.go
+++ b/config/refspec.go
@@ -25,7 +25,7 @@ var (
// reference even if it isn’t a fast-forward.
// eg.: "+refs/heads/*:refs/remotes/origin/*"
//
-// https://git-scm.com/book/es/v2/Git-Internals-The-Refspec
+// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
type RefSpec string
// Validate validates the RefSpec
@@ -59,6 +59,11 @@ func (s RefSpec) IsDelete() bool {
return s[0] == refSpecSeparator[0]
}
+// IsExactSHA1 returns true if the source is a SHA1 hash.
+func (s RefSpec) IsExactSHA1() bool {
+ return plumbing.IsHash(s.Src())
+}
+
// Src return the src side.
func (s RefSpec) Src() string {
spec := string(s)
@@ -69,8 +74,8 @@ func (s RefSpec) Src() string {
} else {
start = 0
}
- end := strings.Index(spec, refSpecSeparator)
+ end := strings.Index(spec, refSpecSeparator)
return spec[start:end]
}
diff --git a/config/refspec_test.go b/config/refspec_test.go
index b9c43b2..3be7573 100644
--- a/config/refspec_test.go
+++ b/config/refspec_test.go
@@ -3,8 +3,8 @@ package config
import (
"testing"
- . "gopkg.in/check.v1"
"github.com/go-git/go-git/v5/plumbing"
+ . "gopkg.in/check.v1"
)
type RefSpecSuite struct{}
@@ -37,6 +37,12 @@ func (s *RefSpecSuite) TestRefSpecIsValid(c *C) {
spec = RefSpec("refs/heads:")
c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedSeparator)
+
+ spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/foo")
+ c.Assert(spec.Validate(), Equals, nil)
+
+ spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/*")
+ c.Assert(spec.Validate(), Equals, ErrRefSpecMalformedWildcard)
}
func (s *RefSpecSuite) TestRefSpecIsForceUpdate(c *C) {
@@ -58,6 +64,14 @@ func (s *RefSpecSuite) TestRefSpecIsDelete(c *C) {
c.Assert(spec.IsDelete(), Equals, false)
}
+func (s *RefSpecSuite) TestRefSpecIsExactSHA1(c *C) {
+ spec := RefSpec("foo:refs/heads/master")
+ c.Assert(spec.IsExactSHA1(), Equals, false)
+
+ spec = RefSpec("12039e008f9a4e3394f3f94f8ea897785cb09448:refs/heads/foo")
+ c.Assert(spec.IsExactSHA1(), Equals, true)
+}
+
func (s *RefSpecSuite) TestRefSpecSrc(c *C) {
spec := RefSpec("refs/heads/*:refs/remotes/origin/*")
c.Assert(spec.Src(), Equals, "refs/heads/*")