Skip to content

Commit f9cbf5a

Browse files
wolfogrelunny
andauthored
Util type to parse ref name (#21969)
Provide a new type to make it easier to parse a ref name. Actually, it's picked up from #21937, to make the origin PR lighter. Co-authored-by: Lunny Xiao <[email protected]>
1 parent 4e5d4d0 commit f9cbf5a

File tree

1 file changed

+37
-20
lines changed

1 file changed

+37
-20
lines changed

modules/git/ref.go

+37-20
Original file line numberDiff line numberDiff line change
@@ -55,40 +55,57 @@ func (ref *Reference) Commit() (*Commit, error) {
5555

5656
// ShortName returns the short name of the reference
5757
func (ref *Reference) ShortName() string {
58-
if ref == nil {
59-
return ""
60-
}
61-
if strings.HasPrefix(ref.Name, BranchPrefix) {
62-
return strings.TrimPrefix(ref.Name, BranchPrefix)
58+
return RefName(ref.Name).ShortName()
59+
}
60+
61+
// RefGroup returns the group type of the reference
62+
func (ref *Reference) RefGroup() string {
63+
return RefName(ref.Name).RefGroup()
64+
}
65+
66+
// RefName represents a git reference name
67+
type RefName string
68+
69+
func (ref RefName) IsBranch() bool {
70+
return strings.HasPrefix(string(ref), BranchPrefix)
71+
}
72+
73+
func (ref RefName) IsTag() bool {
74+
return strings.HasPrefix(string(ref), TagPrefix)
75+
}
76+
77+
// ShortName returns the short name of the reference name
78+
func (ref RefName) ShortName() string {
79+
refName := string(ref)
80+
if strings.HasPrefix(refName, BranchPrefix) {
81+
return strings.TrimPrefix(refName, BranchPrefix)
6382
}
64-
if strings.HasPrefix(ref.Name, TagPrefix) {
65-
return strings.TrimPrefix(ref.Name, TagPrefix)
83+
if strings.HasPrefix(refName, TagPrefix) {
84+
return strings.TrimPrefix(refName, TagPrefix)
6685
}
67-
if strings.HasPrefix(ref.Name, RemotePrefix) {
68-
return strings.TrimPrefix(ref.Name, RemotePrefix)
86+
if strings.HasPrefix(refName, RemotePrefix) {
87+
return strings.TrimPrefix(refName, RemotePrefix)
6988
}
70-
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
71-
return ref.Name[pullLen : strings.IndexByte(ref.Name[pullLen:], '/')+pullLen]
89+
if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
90+
return refName[pullLen : strings.IndexByte(refName[pullLen:], '/')+pullLen]
7291
}
7392

74-
return ref.Name
93+
return refName
7594
}
7695

7796
// RefGroup returns the group type of the reference
78-
func (ref *Reference) RefGroup() string {
79-
if ref == nil {
80-
return ""
81-
}
82-
if strings.HasPrefix(ref.Name, BranchPrefix) {
97+
func (ref RefName) RefGroup() string {
98+
refName := string(ref)
99+
if strings.HasPrefix(refName, BranchPrefix) {
83100
return "heads"
84101
}
85-
if strings.HasPrefix(ref.Name, TagPrefix) {
102+
if strings.HasPrefix(refName, TagPrefix) {
86103
return "tags"
87104
}
88-
if strings.HasPrefix(ref.Name, RemotePrefix) {
105+
if strings.HasPrefix(refName, RemotePrefix) {
89106
return "remotes"
90107
}
91-
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
108+
if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
92109
return "pull"
93110
}
94111
return ""

0 commit comments

Comments
 (0)