aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing')
-rw-r--r--plumbing/reference.go23
-rw-r--r--plumbing/reference_test.go5
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)