summaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authormscherer <mscherer@users.noreply.github.com>2021-12-02 08:28:08 +0100
committerGitHub <noreply@github.com>2021-12-02 15:28:08 +0800
commit34b5436ae1af2735546bb519a950eabf4990212d (patch)
tree12953d3ad1915baba2ba0f59ddb1a5a4357668fc /modules
parentba57e30f13906ad7104da7892dc3dc79721ed2fe (diff)
downloadgitea-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')
-rw-r--r--modules/convert/pull.go2
-rw-r--r--modules/git/commit.go2
-rw-r--r--modules/git/ref.go33
-rw-r--r--modules/git/repo.go2
-rw-r--r--modules/git/repo_compare.go2
-rw-r--r--modules/git/repo_ref_nogogit.go2
-rw-r--r--modules/gitgraph/graph.go2
-rw-r--r--modules/migration/pullrequest.go4
8 files changed, 30 insertions, 19 deletions
diff --git a/modules/convert/pull.go b/modules/convert/pull.go
index ea1e4bb3c2..a5e7d9608a 100644
--- a/modules/convert/pull.go
+++ b/modules/convert/pull.go
@@ -79,7 +79,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
},
Head: &api.PRBranchInfo{
Name: pr.HeadBranch,
- Ref: fmt.Sprintf("refs/pull/%d/head", pr.Index),
+ Ref: fmt.Sprintf(git.PullPrefix+"%d/head", pr.Index),
RepoID: -1,
},
}
diff --git a/modules/git/commit.go b/modules/git/commit.go
index fe2c2b9774..26c5445e2a 100644
--- a/modules/git/commit.go
+++ b/modules/git/commit.go
@@ -141,7 +141,7 @@ func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOpt
func AllCommitsCount(repoPath string, hidePRRefs bool, files ...string) (int64, error) {
args := []string{"--all", "--count"}
if hidePRRefs {
- args = append([]string{"--exclude=refs/pull/*"}, args...)
+ args = append([]string{"--exclude=" + PullPrefix + "*"}, args...)
}
cmd := NewCommand("rev-list")
cmd.AddArguments(args...)
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 ""
diff --git a/modules/git/repo.go b/modules/git/repo.go
index 3ff2b6fe2d..3950bb4a92 100644
--- a/modules/git/repo.go
+++ b/modules/git/repo.go
@@ -371,7 +371,7 @@ func parseSize(objects string) *CountObject {
// GetLatestCommitTime returns time for latest commit in repository (across all branches)
func GetLatestCommitTime(repoPath string) (time.Time, error) {
- cmd := NewCommand("for-each-ref", "--sort=-committerdate", "refs/heads/", "--count", "1", "--format=%(committerdate)")
+ cmd := NewCommand("for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)")
stdout, err := cmd.RunInDir(repoPath)
if err != nil {
return time.Time{}, err
diff --git a/modules/git/repo_compare.go b/modules/git/repo_compare.go
index 303bb5bc03..4342eb4b2f 100644
--- a/modules/git/repo_compare.go
+++ b/modules/git/repo_compare.go
@@ -33,7 +33,7 @@ func (repo *Repository) GetMergeBase(tmpRemote string, base, head string) (strin
}
if tmpRemote != "origin" {
- tmpBaseName := "refs/remotes/" + tmpRemote + "/tmp_" + base
+ tmpBaseName := RemotePrefix + tmpRemote + "/tmp_" + base
// Fetch commit into a temporary branch in order to be able to handle commits and tags
_, err := NewCommandContext(repo.Ctx, "fetch", tmpRemote, base+":"+tmpBaseName).RunInDir(repo.Path)
if err == nil {
diff --git a/modules/git/repo_ref_nogogit.go b/modules/git/repo_ref_nogogit.go
index 790b717d38..5c9ed57ea1 100644
--- a/modules/git/repo_ref_nogogit.go
+++ b/modules/git/repo_ref_nogogit.go
@@ -66,7 +66,7 @@ func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
refName = refName[:len(refName)-1]
// refName cannot be HEAD but can be remotes or stash
- if strings.HasPrefix(refName, "/refs/remotes/") || refName == "/refs/stash" {
+ if strings.HasPrefix(refName, RemotePrefix) || refName == "/refs/stash" {
continue
}
diff --git a/modules/gitgraph/graph.go b/modules/gitgraph/graph.go
index 8505678639..cf994bfd4c 100644
--- a/modules/gitgraph/graph.go
+++ b/modules/gitgraph/graph.go
@@ -29,7 +29,7 @@ func GetCommitGraph(r *git.Repository, page int, maxAllowedColors int, hidePRRef
args = append(args, "--graph", "--date-order", "--decorate=full")
if hidePRRefs {
- args = append(args, "--exclude=refs/pull/*")
+ args = append(args, "--exclude="+git.PullPrefix+"*")
}
if len(branches) == 0 {
diff --git a/modules/migration/pullrequest.go b/modules/migration/pullrequest.go
index 9ca9a70b7d..498768ea48 100644
--- a/modules/migration/pullrequest.go
+++ b/modules/migration/pullrequest.go
@@ -8,6 +8,8 @@ package migration
import (
"fmt"
"time"
+
+ "code.gitea.io/gitea/modules/git"
)
// PullRequest defines a standard pull request information
@@ -43,7 +45,7 @@ func (p *PullRequest) IsForkPullRequest() bool {
// GetGitRefName returns pull request relative path to head
func (p PullRequest) GetGitRefName() string {
- return fmt.Sprintf("refs/pull/%d/head", p.Number)
+ return fmt.Sprintf(git.PullPrefix+"%d/head", p.Number)
}
// PullRequestBranch represents a pull request branch