Skip to content

Commit fc2256f

Browse files
mschererGusted
and
Gusted
committed
Refactor various strings
Fixes #16478 Co-authored-by: Gusted <williamzijl7@hotmail.com>
1 parent 7d0629a commit fc2256f

File tree

17 files changed

+46
-33
lines changed

17 files changed

+46
-33
lines changed

contrib/pr/checkout.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func main() {
215215
//Use git cli command for windows
216216
runCmd("git", "fetch", remoteUpstream, fmt.Sprintf("pull/%s/head:%s", pr, branch))
217217
} else {
218-
ref := fmt.Sprintf("refs/pull/%s/head:%s", pr, branchRef)
218+
ref := fmt.Sprintf(gitea_git.PullPrefix+"%s/head:%s", pr, branchRef)
219219
err = repo.Fetch(&git.FetchOptions{
220220
RemoteName: remoteUpstream,
221221
RefSpecs: []config.RefSpec{

models/issue.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"code.gitea.io/gitea/models/unit"
2121
user_model "code.gitea.io/gitea/models/user"
2222
"code.gitea.io/gitea/modules/base"
23+
"code.gitea.io/gitea/modules/git"
2324
"code.gitea.io/gitea/modules/log"
2425
"code.gitea.io/gitea/modules/references"
2526
api "code.gitea.io/gitea/modules/structs"
@@ -761,8 +762,8 @@ func (issue *Issue) ChangeRef(doer *user_model.User, oldRef string) (err error)
761762
if err = issue.loadRepo(db.GetEngine(ctx)); err != nil {
762763
return fmt.Errorf("loadRepo: %v", err)
763764
}
764-
oldRefFriendly := strings.TrimPrefix(oldRef, "refs/heads/")
765-
newRefFriendly := strings.TrimPrefix(issue.Ref, "refs/heads/")
765+
oldRefFriendly := strings.TrimPrefix(oldRef, git.BranchPrefix)
766+
newRefFriendly := strings.TrimPrefix(issue.Ref, git.BranchPrefix)
766767

767768
opts := &CreateCommentOptions{
768769
Type: CommentTypeChangeIssueRef,

models/pull.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/models/db"
1414
"code.gitea.io/gitea/models/unit"
1515
user_model "code.gitea.io/gitea/models/user"
16+
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
1819
"code.gitea.io/gitea/modules/timeutil"
@@ -349,7 +350,7 @@ func (pr *PullRequest) GetDefaultSquashMessage() string {
349350

350351
// GetGitRefName returns git ref for hidden pull request branch
351352
func (pr *PullRequest) GetGitRefName() string {
352-
return fmt.Sprintf("refs/pull/%d/head", pr.Index)
353+
return fmt.Sprintf(git.PullPrefix+"%d/head", pr.Index)
353354
}
354355

355356
// IsChecking returns true if this pull request is still checking conflict.

modules/convert/pull.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func ToAPIPullRequest(pr *models.PullRequest, doer *user_model.User) *api.PullRe
7979
},
8080
Head: &api.PRBranchInfo{
8181
Name: pr.HeadBranch,
82-
Ref: fmt.Sprintf("refs/pull/%d/head", pr.Index),
82+
Ref: fmt.Sprintf(git.PullPrefix+"%d/head", pr.Index),
8383
RepoID: -1,
8484
},
8585
}

modules/git/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func CommitChangesWithArgs(repoPath string, args []string, opts CommitChangesOpt
141141
func AllCommitsCount(repoPath string, hidePRRefs bool, files ...string) (int64, error) {
142142
args := []string{"--all", "--count"}
143143
if hidePRRefs {
144-
args = append([]string{"--exclude=refs/pull/*"}, args...)
144+
args = append([]string{"--exclude=" + PullPrefix + "*"}, args...)
145145
}
146146
cmd := NewCommand("rev-list")
147147
cmd.AddArguments(args...)

modules/git/ref.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ package git
66

77
import "strings"
88

9+
const (
10+
// RemotePrefix is the base directory of the remotes information of git.
11+
RemotePrefix = "refs/remotes/"
12+
// PullPrefix is the base directory of the pull information of git.
13+
PullPrefix = "refs/pull/"
14+
15+
pullLen = len(PullPrefix)
16+
)
17+
918
// Reference represents a Git ref.
1019
type Reference struct {
1120
Name string
@@ -24,17 +33,17 @@ func (ref *Reference) ShortName() string {
2433
if ref == nil {
2534
return ""
2635
}
27-
if strings.HasPrefix(ref.Name, "refs/heads/") {
28-
return ref.Name[11:]
36+
if strings.HasPrefix(ref.Name, BranchPrefix) {
37+
return strings.TrimPrefix(ref.Name, BranchPrefix)
2938
}
30-
if strings.HasPrefix(ref.Name, "refs/tags/") {
31-
return ref.Name[10:]
39+
if strings.HasPrefix(ref.Name, TagPrefix) {
40+
return strings.TrimPrefix(ref.Name, TagPrefix)
3241
}
33-
if strings.HasPrefix(ref.Name, "refs/remotes/") {
34-
return ref.Name[13:]
42+
if strings.HasPrefix(ref.Name, RemotePrefix) {
43+
return strings.TrimPrefix(ref.Name, RemotePrefix)
3544
}
36-
if strings.HasPrefix(ref.Name, "refs/pull/") && strings.IndexByte(ref.Name[10:], '/') > -1 {
37-
return ref.Name[10 : strings.IndexByte(ref.Name[10:], '/')+10]
45+
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
46+
return ref.Name[pullLen : strings.IndexByte(ref.Name[pullLen:], '/')+pullLen]
3847
}
3948

4049
return ref.Name
@@ -45,16 +54,16 @@ func (ref *Reference) RefGroup() string {
4554
if ref == nil {
4655
return ""
4756
}
48-
if strings.HasPrefix(ref.Name, "refs/heads/") {
57+
if strings.HasPrefix(ref.Name, BranchPrefix) {
4958
return "heads"
5059
}
51-
if strings.HasPrefix(ref.Name, "refs/tags/") {
60+
if strings.HasPrefix(ref.Name, TagPrefix) {
5261
return "tags"
5362
}
54-
if strings.HasPrefix(ref.Name, "refs/remotes/") {
63+
if strings.HasPrefix(ref.Name, RemotePrefix) {
5564
return "remotes"
5665
}
57-
if strings.HasPrefix(ref.Name, "refs/pull/") && strings.IndexByte(ref.Name[10:], '/') > -1 {
66+
if strings.HasPrefix(ref.Name, PullPrefix) && strings.IndexByte(ref.Name[pullLen:], '/') > -1 {
5867
return "pull"
5968
}
6069
return ""

modules/git/repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ func parseSize(objects string) *CountObject {
371371

372372
// GetLatestCommitTime returns time for latest commit in repository (across all branches)
373373
func GetLatestCommitTime(repoPath string) (time.Time, error) {
374-
cmd := NewCommand("for-each-ref", "--sort=-committerdate", "refs/heads/", "--count", "1", "--format=%(committerdate)")
374+
cmd := NewCommand("for-each-ref", "--sort=-committerdate", BranchPrefix, "--count", "1", "--format=%(committerdate)")
375375
stdout, err := cmd.RunInDir(repoPath)
376376
if err != nil {
377377
return time.Time{}, err

modules/git/repo_compare.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (repo *Repository) GetMergeBase(tmpRemote string, base, head string) (strin
3333
}
3434

3535
if tmpRemote != "origin" {
36-
tmpBaseName := "refs/remotes/" + tmpRemote + "/tmp_" + base
36+
tmpBaseName := RemotePrefix + tmpRemote + "/tmp_" + base
3737
// Fetch commit into a temporary branch in order to be able to handle commits and tags
3838
_, err := NewCommandContext(repo.Ctx, "fetch", tmpRemote, base+":"+tmpBaseName).RunInDir(repo.Path)
3939
if err == nil {

modules/git/repo_ref_nogogit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (repo *Repository) GetRefsFiltered(pattern string) ([]*Reference, error) {
6666
refName = refName[:len(refName)-1]
6767

6868
// refName cannot be HEAD but can be remotes or stash
69-
if strings.HasPrefix(refName, "/refs/remotes/") || refName == "/refs/stash" {
69+
if strings.HasPrefix(refName, RemotePrefix) || refName == "/refs/stash" {
7070
continue
7171
}
7272

modules/gitgraph/graph.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func GetCommitGraph(r *git.Repository, page int, maxAllowedColors int, hidePRRef
2929
args = append(args, "--graph", "--date-order", "--decorate=full")
3030

3131
if hidePRRefs {
32-
args = append(args, "--exclude=refs/pull/*")
32+
args = append(args, "--exclude="+git.PullPrefix+"*")
3333
}
3434

3535
if len(branches) == 0 {

modules/migration/pullrequest.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ package migration
88
import (
99
"fmt"
1010
"time"
11+
12+
"code.gitea.io/gitea/modules/git"
1113
)
1214

1315
// PullRequest defines a standard pull request information
@@ -43,7 +45,7 @@ func (p *PullRequest) IsForkPullRequest() bool {
4345

4446
// GetGitRefName returns pull request relative path to head
4547
func (p PullRequest) GetGitRefName() string {
46-
return fmt.Sprintf("refs/pull/%d/head", p.Number)
48+
return fmt.Sprintf(git.PullPrefix+"%d/head", p.Number)
4749
}
4850

4951
// PullRequestBranch represents a pull request branch

routers/web/repo/commit.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func Graph(ctx *context.Context) {
104104
copy(realBranches, branches)
105105
for i, branch := range realBranches {
106106
if strings.HasPrefix(branch, "--") {
107-
realBranches[i] = "refs/heads/" + branch
107+
realBranches[i] = git.BranchPrefix + branch
108108
}
109109
}
110110
ctx.Data["SelectedBranches"] = realBranches

services/mirror/mirror_pull.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ func SyncPullMirror(ctx context.Context, repoID int64) bool {
403403

404404
for _, result := range results {
405405
// Discard GitHub pull requests, i.e. refs/pull/*
406-
if strings.HasPrefix(result.refName, "refs/pull/") {
406+
if strings.HasPrefix(result.refName, git.PullPrefix) {
407407
continue
408408
}
409409

@@ -499,7 +499,7 @@ func checkAndUpdateEmptyRepository(m *models.Mirror, gitRepo *git.Repository, re
499499
}
500500
firstName := ""
501501
for _, result := range results {
502-
if strings.HasPrefix(result.refName, "refs/pull/") {
502+
if strings.HasPrefix(result.refName, git.PullPrefix) {
503503
continue
504504
}
505505
tp, name := git.SplitRefName(result.refName)

services/pull/merge.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,9 @@ func rawMerge(pr *models.PullRequest, doer *user_model.User, mergeStyle models.M
421421
var pushCmd *git.Command
422422
if mergeStyle == models.MergeStyleRebaseUpdate {
423423
// force push the rebase result to head brach
424-
pushCmd = git.NewCommand("push", "-f", "head_repo", stagingBranch+":refs/heads/"+pr.HeadBranch)
424+
pushCmd = git.NewCommand("push", "-f", "head_repo", stagingBranch+":"+git.BranchPrefix+pr.HeadBranch)
425425
} else {
426-
pushCmd = git.NewCommand("push", "origin", baseBranch+":refs/heads/"+pr.BaseBranch)
426+
pushCmd = git.NewCommand("push", "origin", baseBranch+":"+git.BranchPrefix+pr.BaseBranch)
427427
}
428428

429429
// Push back to upstream.

services/pull/pull.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,8 @@ func pushToBaseRepoHelper(pr *models.PullRequest, prefixHeadBranch string) (err
451451
log.Info("Can't push with %s%s", prefixHeadBranch, pr.HeadBranch)
452452
return err
453453
}
454-
log.Info("Retrying to push with refs/heads/%s", pr.HeadBranch)
455-
err = pushToBaseRepoHelper(pr, "refs/heads/")
454+
log.Info("Retrying to push with "+git.BranchPrefix+"%s", pr.HeadBranch)
455+
err = pushToBaseRepoHelper(pr, git.BranchPrefix)
456456
return err
457457
}
458458
log.Error("Unable to push PR head for %s#%d (%-v:%s) due to Error: %v", pr.BaseRepo.FullName(), pr.Index, pr.BaseRepo, gitRefName, err)

services/repository/branch.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ func RenameBranch(repo *models.Repository, doer *user_model.User, gitRepo *git.R
152152
return "", err
153153
}
154154

155-
notification.NotifyDeleteRef(doer, repo, "branch", "refs/heads/"+from)
156-
notification.NotifyCreateRef(doer, repo, "branch", "refs/heads/"+to)
155+
notification.NotifyDeleteRef(doer, repo, "branch", git.BranchPrefix+from)
156+
notification.NotifyCreateRef(doer, repo, "branch", git.BranchPrefix+to)
157157

158158
return "", nil
159159
}

services/repository/files/temp_repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash strin
266266
env := models.PushingEnvironment(doer, t.repo)
267267
if err := git.Push(t.gitRepo.Ctx, t.basePath, git.PushOptions{
268268
Remote: t.repo.RepoPath(),
269-
Branch: strings.TrimSpace(commitHash) + ":refs/heads/" + strings.TrimSpace(branch),
269+
Branch: strings.TrimSpace(commitHash) + ":" + git.BranchPrefix + strings.TrimSpace(branch),
270270
Env: env,
271271
}); err != nil {
272272
if git.IsErrPushOutOfDate(err) {

0 commit comments

Comments
 (0)