@@ -185,32 +185,38 @@ func (ref RefName) RefGroup() string {
185185 return ""
186186}
187187
188+ // RefType is a simple ref type of the reference, it is used for UI and webhooks
189+ type RefType string
190+
191+ const (
192+ RefTypeBranch RefType = "branch"
193+ RefTypeTag RefType = "tag"
194+ RefTypeCommit RefType = "commit"
195+ )
196+
188197// RefType returns the simple ref type of the reference, e.g. branch, tag
189198// It's different from RefGroup, which is using the name of the directory under .git/refs
190- // Here we using branch but not heads, using tag but not tags
191- func (ref RefName ) RefType () string {
192- var refType string
193- if ref .IsBranch () {
194- refType = "branch"
195- } else if ref .IsTag () {
196- refType = "tag"
199+ func (ref RefName ) RefType () RefType {
200+ switch {
201+ case ref .IsBranch ():
202+ return RefTypeBranch
203+ case ref .IsTag ():
204+ return RefTypeTag
205+ case IsStringLikelyCommitID (nil , string (ref ), 6 ):
206+ return RefTypeCommit
197207 }
198- return refType
208+ return ""
199209}
200210
201- // RefURL returns the absolute URL for a ref in a repository
202- func RefURL (repoURL , ref string ) string {
203- refFullName := RefName (ref )
204- refName := util .PathEscapeSegments (refFullName .ShortName ())
205- switch {
206- case refFullName .IsBranch ():
207- return repoURL + "/src/branch/" + refName
208- case refFullName .IsTag ():
209- return repoURL + "/src/tag/" + refName
210- case ! Sha1ObjectFormat .IsValid (ref ):
211- // assume they mean a branch
212- return repoURL + "/src/branch/" + refName
213- default :
214- return repoURL + "/src/commit/" + refName
211+ // RefWebLinkPath returns a path for the reference that can be used in a web link:
212+ // * "branch/<branch_name>"
213+ // * "tag/<tag_name>"
214+ // * "commit/<commit_id>"
215+ // It returns an empty string if the reference is not a branch, tag or commit.
216+ func (ref RefName ) RefWebLinkPath () string {
217+ refType := ref .RefType ()
218+ if refType == "" {
219+ return ""
215220 }
221+ return string (refType ) + "/" + util .PathEscapeSegments (ref .ShortName ())
216222}
0 commit comments