Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

Commit e190c37

Browse files
hatajoemcuadros
authored andcommitted
plumbing: Reference, support slash separated branch (#302)
1 parent 62ad629 commit e190c37

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

plumbing/reference.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ const (
1515
symrefPrefix = "ref: "
1616
)
1717

18+
var (
19+
refPrefixes = []string{
20+
refHeadPrefix,
21+
refTagPrefix,
22+
refRemotePrefix,
23+
refNotePrefix,
24+
refPrefix,
25+
}
26+
)
27+
1828
var (
1929
ErrReferenceNotFound = errors.New("reference not found")
2030
)
@@ -50,8 +60,17 @@ func (r ReferenceName) String() string {
5060

5161
// Short returns the short name of a ReferenceName
5262
func (r ReferenceName) Short() string {
53-
parts := strings.Split(string(r), "/")
54-
return parts[len(parts)-1]
63+
return r.removeRefPrefix()
64+
}
65+
66+
// Instead of hardcoding a number of components, we should remove the prefixes
67+
// refHeadPrefix, refTagPrefix, refRemotePrefix, refNotePrefix and refPrefix
68+
func (r ReferenceName) removeRefPrefix() string {
69+
s := string(r)
70+
for _, prefix := range refPrefixes {
71+
s = strings.TrimPrefix(s, prefix)
72+
}
73+
return s
5574
}
5675

5776
const (

plumbing/reference_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ func (s *ReferenceSuite) TestReferenceNameShort(c *C) {
1818
c.Assert(ExampleReferenceName.Short(), Equals, "v4")
1919
}
2020

21+
func (s *ReferenceSuite) TestReferenceNameWithSlash(c *C) {
22+
r := ReferenceName("refs/remotes/origin/feature/AllowSlashes")
23+
c.Assert(r.Short(), Equals, "origin/feature/AllowSlashes")
24+
}
25+
2126
func (s *ReferenceSuite) TestNewReferenceFromStrings(c *C) {
2227
r := NewReferenceFromStrings("refs/heads/v4", "6ecf0ef2c2dffb796033e5a02219af86ec6584e5")
2328
c.Assert(r.Type(), Equals, HashReference)

0 commit comments

Comments
 (0)