diff options
author | mscherer <mscherer@users.noreply.github.com> | 2021-12-02 08:28:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-02 15:28:08 +0800 |
commit | 34b5436ae1af2735546bb519a950eabf4990212d (patch) | |
tree | 12953d3ad1915baba2ba0f59ddb1a5a4357668fc /modules/git/ref.go | |
parent | ba57e30f13906ad7104da7892dc3dc79721ed2fe (diff) | |
download | gitea-34b5436ae1af2735546bb519a950eabf4990212d.tar.gz gitea-34b5436ae1af2735546bb519a950eabf4990212d.zip |
Refactor various strings (#17784)
Fixes #16478
Co-authored-by: Gusted <williamzijl7@hotmail.com>
Co-authored-by: Gusted <williamzijl7@hotmail.com>
Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'modules/git/ref.go')
-rw-r--r-- | modules/git/ref.go | 33 |
1 files changed, 21 insertions, 12 deletions
diff --git a/modules/git/ref.go b/modules/git/ref.go index 2a2798b18f..9fd071ce58 100644 --- a/modules/git/ref.go +++ b/modules/git/ref.go @@ -6,6 +6,15 @@ package git import "strings" +const ( + // RemotePrefix is the base directory of the remotes information of git. + RemotePrefix = "refs/remotes/" + // PullPrefix is the base directory of the pull information of git. + PullPrefix = "refs/pull/" + + pullLen = len(PullPrefix) +) + // Reference represents a Git ref. type Reference struct { Name string @@ -24,17 +33,17 @@ func (ref *Reference) ShortName() string { if ref == nil { return "" } - if strings.HasPrefix(ref.Name, "refs/heads/") { - return ref.Name[11:] + if strings.HasPrefix(ref.Name, BranchPrefix) { + return strings.TrimPrefix(ref.Name, BranchPrefix) } - if strings.HasPrefix(ref.Name, "refs/tags/") { - return ref.Name[10:] + if strings.HasPrefix(ref.Name, TagPrefix) { + return strings.TrimPrefix(ref.Name, TagPrefix) } - if strings.HasPrefix(ref.Name, "refs/remotes/") { - return ref.Name[13:] + if strings.HasPrefix(ref.Name, RemotePrefix) { + return strings.TrimPrefix(ref.Name, RemotePrefix) } - if strings.HasPrefix(ref.Name, "refs/pull/") && strings.IndexByte(ref.Name[10:], '/') > -1 { - return ref.Name[10 : strings.IndexByte(ref.Name[10:], '/')+10] + if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 { + return ref.Name[pullLen : strings.IndexByte(ref.Name[pullLen:], '/')+pullLen] } return ref.Name @@ -45,16 +54,16 @@ func (ref *Reference) RefGroup() string { if ref == nil { return "" } - if strings.HasPrefix(ref.Name, "refs/heads/") { + if strings.HasPrefix(ref.Name, BranchPrefix) { return "heads" } - if strings.HasPrefix(ref.Name, "refs/tags/") { + if strings.HasPrefix(ref.Name, TagPrefix) { return "tags" } - if strings.HasPrefix(ref.Name, "refs/remotes/") { + if strings.HasPrefix(ref.Name, RemotePrefix) { return "remotes" } - if strings.HasPrefix(ref.Name, "refs/pull/") && strings.IndexByte(ref.Name[10:], '/') > -1 { + if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 { return "pull" } return "" |