@@ -80,6 +80,10 @@ func RefNameFromTag(shortName string) RefName {
8080 return RefName (TagPrefix + shortName )
8181}
8282
83+ func RefNameFromCommit (shortName string ) RefName {
84+ return RefName (shortName )
85+ }
86+
8387func (ref RefName ) String () string {
8488 return string (ref )
8589}
@@ -181,32 +185,38 @@ func (ref RefName) RefGroup() string {
181185 return ""
182186}
183187
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+
184197// RefType returns the simple ref type of the reference, e.g. branch, tag
185198// It's different from RefGroup, which is using the name of the directory under .git/refs
186- // Here we using branch but not heads, using tag but not tags
187- func (ref RefName ) RefType () string {
188- var refType string
189- if ref .IsBranch () {
190- refType = "branch"
191- } else if ref .IsTag () {
192- 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
193207 }
194- return refType
208+ return ""
195209}
196210
197- // RefURL returns the absolute URL for a ref in a repository
198- func RefURL (repoURL , ref string ) string {
199- refFullName := RefName (ref )
200- refName := util .PathEscapeSegments (refFullName .ShortName ())
201- switch {
202- case refFullName .IsBranch ():
203- return repoURL + "/src/branch/" + refName
204- case refFullName .IsTag ():
205- return repoURL + "/src/tag/" + refName
206- case ! Sha1ObjectFormat .IsValid (ref ):
207- // assume they mean a branch
208- return repoURL + "/src/branch/" + refName
209- default :
210- 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 ""
211220 }
221+ return string (refType ) + "/" + util .PathEscapeSegments (ref .ShortName ())
212222}
0 commit comments