diff options
author | Yusuke Hatanaka <arbalestimp@gmail.com> | 2017-03-28 12:47:24 +0900 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2017-03-28 05:47:24 +0200 |
commit | e190c37cf51a2a320cabd81b25057859ed689a3b (patch) | |
tree | 9620c6636b29c008194c75230d1b119e38db4dad /plumbing | |
parent | 62ad629b9a4213fdb8d33bcc7e0bea66d043fc41 (diff) | |
download | go-git-e190c37cf51a2a320cabd81b25057859ed689a3b.tar.gz |
plumbing: Reference, support slash separated branch (#302)
Diffstat (limited to 'plumbing')
-rw-r--r-- | plumbing/reference.go | 23 | ||||
-rw-r--r-- | plumbing/reference_test.go | 5 |
2 files changed, 26 insertions, 2 deletions
diff --git a/plumbing/reference.go b/plumbing/reference.go index 798c3dd..8fa103e 100644 --- a/plumbing/reference.go +++ b/plumbing/reference.go @@ -16,6 +16,16 @@ const ( ) var ( + refPrefixes = []string{ + refHeadPrefix, + refTagPrefix, + refRemotePrefix, + refNotePrefix, + refPrefix, + } +) + +var ( ErrReferenceNotFound = errors.New("reference not found") ) @@ -50,8 +60,17 @@ func (r ReferenceName) String() string { // Short returns the short name of a ReferenceName func (r ReferenceName) Short() string { - parts := strings.Split(string(r), "/") - return parts[len(parts)-1] + return r.removeRefPrefix() +} + +// Instead of hardcoding a number of components, we should remove the prefixes +// refHeadPrefix, refTagPrefix, refRemotePrefix, refNotePrefix and refPrefix +func (r ReferenceName) removeRefPrefix() string { + s := string(r) + for _, prefix := range refPrefixes { + s = strings.TrimPrefix(s, prefix) + } + return s } const ( diff --git a/plumbing/reference_test.go b/plumbing/reference_test.go index 849f159..6a695f4 100644 --- a/plumbing/reference_test.go +++ b/plumbing/reference_test.go @@ -18,6 +18,11 @@ func (s *ReferenceSuite) TestReferenceNameShort(c *C) { c.Assert(ExampleReferenceName.Short(), Equals, "v4") } +func (s *ReferenceSuite) TestReferenceNameWithSlash(c *C) { + r := ReferenceName("refs/remotes/origin/feature/AllowSlashes") + c.Assert(r.Short(), Equals, "origin/feature/AllowSlashes") +} + func (s *ReferenceSuite) TestNewReferenceFromStrings(c *C) { r := NewReferenceFromStrings("refs/heads/v4", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5") c.Assert(r.Type(), Equals, HashReference) |