Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Util type to parse ref name #21969

Merged
merged 4 commits into from
Dec 1, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 37 additions & 20 deletions modules/git/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,40 +55,57 @@ func (ref *Reference) Commit() (*Commit, error) {

// ShortName returns the short name of the reference
func (ref *Reference) ShortName() string {
if ref == nil {
return ""
}
if strings.HasPrefix(ref.Name, BranchPrefix) {
return strings.TrimPrefix(ref.Name, BranchPrefix)
return RefName(ref.Name).ShortName()
}

// RefGroup returns the group type of the reference
func (ref *Reference) RefGroup() string {
return RefName(ref.Name).RefGroup()
}

// RefName represents a git reference name
type RefName string

func (ref RefName) IsBranch() bool {
return strings.HasPrefix(string(ref), BranchPrefix)
}

func (ref RefName) IsTag() bool {
Comment on lines +69 to +73
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No IsPull?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't know if it's pull or push from the ref name.

return strings.HasPrefix(string(ref), TagPrefix)
}

// ShortName returns the short name of the reference name
func (ref RefName) ShortName() string {
refName := string(ref)
if strings.HasPrefix(refName, BranchPrefix) {
return strings.TrimPrefix(refName, BranchPrefix)
}
if strings.HasPrefix(ref.Name, TagPrefix) {
return strings.TrimPrefix(ref.Name, TagPrefix)
if strings.HasPrefix(refName, TagPrefix) {
return strings.TrimPrefix(refName, TagPrefix)
}
if strings.HasPrefix(ref.Name, RemotePrefix) {
return strings.TrimPrefix(ref.Name, RemotePrefix)
if strings.HasPrefix(refName, RemotePrefix) {
return strings.TrimPrefix(refName, RemotePrefix)
}
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
return ref.Name[pullLen : strings.IndexByte(ref.Name[pullLen:], '/')+pullLen]
if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
return refName[pullLen : strings.IndexByte(refName[pullLen:], '/')+pullLen]
}

return ref.Name
return refName
}

// RefGroup returns the group type of the reference
func (ref *Reference) RefGroup() string {
if ref == nil {
return ""
}
if strings.HasPrefix(ref.Name, BranchPrefix) {
func (ref RefName) RefGroup() string {
refName := string(ref)
if strings.HasPrefix(refName, BranchPrefix) {
return "heads"
}
if strings.HasPrefix(ref.Name, TagPrefix) {
if strings.HasPrefix(refName, TagPrefix) {
return "tags"
}
if strings.HasPrefix(ref.Name, RemotePrefix) {
if strings.HasPrefix(refName, RemotePrefix) {
return "remotes"
}
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
if strings.HasPrefix(refName, PullPrefix) && strings.IndexByte(refName[pullLen:], '/') > -1 {
return "pull"
}
return ""
Expand Down