Skip to content

Commit a30ddde

Browse files
committed
move branch functions to modules to avoid dependencies cycles
1 parent 2303bc9 commit a30ddde

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

modules/convert/pull.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"code.gitea.io/gitea/models"
99
"code.gitea.io/gitea/modules/git"
1010
"code.gitea.io/gitea/modules/log"
11+
repo_module "code.gitea.io/gitea/modules/repository"
1112
api "code.gitea.io/gitea/modules/structs"
1213
)
1314

@@ -69,7 +70,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
6970
Created: pr.Issue.CreatedUnix.AsTimePtr(),
7071
Updated: pr.Issue.UpdatedUnix.AsTimePtr(),
7172
}
72-
baseBranch, err = pr.BaseRepo.GetBranch(pr.BaseBranch)
73+
baseBranch, err = repo_module.GetBranch(pr.BaseRepo, pr.BaseBranch)
7374
if err != nil {
7475
if git.IsErrBranchNotExist(err) {
7576
apiPullRequest.Base = nil
@@ -98,7 +99,7 @@ func ToAPIPullRequest(pr *models.PullRequest) *api.PullRequest {
9899
apiPullRequest.Base = apiBaseBranchInfo
99100
}
100101

101-
headBranch, err = pr.HeadRepo.GetBranch(pr.HeadBranch)
102+
headBranch, err = repo_module.GetBranch(pr.HeadRepo, pr.HeadBranch)
102103
if err != nil {
103104
if git.IsErrBranchNotExist(err) {
104105
apiPullRequest.Head = nil

modules/repofiles/delete.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"code.gitea.io/gitea/models"
1212
"code.gitea.io/gitea/modules/git"
1313
api "code.gitea.io/gitea/modules/structs"
14+
repo_module "code.gitea.io/gitea/modules/repository"
1415
)
1516

1617
// DeleteRepoFileOptions holds the repository delete file options
@@ -37,15 +38,15 @@ func DeleteRepoFile(repo *models.Repository, doer *models.User, opts *DeleteRepo
3738
}
3839

3940
// oldBranch must exist for this operation
40-
if _, err := repo.GetBranch(opts.OldBranch); err != nil {
41+
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
4142
return nil, err
4243
}
4344

4445
// A NewBranch can be specified for the file to be created/updated in a new branch.
4546
// Check to make sure the branch does not already exist, otherwise we can't proceed.
4647
// If we aren't branching to a new branch, make sure user can commit to the given branch
4748
if opts.NewBranch != opts.OldBranch {
48-
newBranch, err := repo.GetBranch(opts.NewBranch)
49+
newBranch, err := repo_module.GetBranch(repo, opts.NewBranch)
4950
if git.IsErrNotExist(err) {
5051
return nil, err
5152
}

modules/repofiles/update.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"code.gitea.io/gitea/modules/git"
1919
"code.gitea.io/gitea/modules/lfs"
2020
"code.gitea.io/gitea/modules/log"
21-
"code.gitea.io/gitea/modules/repository"
21+
repo_module "code.gitea.io/gitea/modules/repository"
2222
"code.gitea.io/gitea/modules/setting"
2323
"code.gitea.io/gitea/modules/structs"
2424
pull_service "code.gitea.io/gitea/services/pull"
@@ -134,15 +134,15 @@ func CreateOrUpdateRepoFile(repo *models.Repository, doer *models.User, opts *Up
134134
}
135135

136136
// oldBranch must exist for this operation
137-
if _, err := repo.GetBranch(opts.OldBranch); err != nil {
137+
if _, err := repo_module.GetBranch(repo, opts.OldBranch); err != nil {
138138
return nil, err
139139
}
140140

141141
// A NewBranch can be specified for the file to be created/updated in a new branch.
142142
// Check to make sure the branch does not already exist, otherwise we can't proceed.
143143
// If we aren't branching to a new branch, make sure user can commit to the given branch
144144
if opts.NewBranch != opts.OldBranch {
145-
existingBranch, err := repo.GetBranch(opts.NewBranch)
145+
existingBranch, err := repo_module.GetBranch(repo, opts.NewBranch)
146146
if existingBranch != nil {
147147
return nil, models.ErrBranchAlreadyExists{
148148
BranchName: opts.NewBranch,
@@ -550,7 +550,7 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o
550550
if isNewRef && isDelRef {
551551
return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
552552
}
553-
var commits = &repository.PushCommits{}
553+
var commits = &repo_module.PushCommits{}
554554
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
555555
// If is tag reference
556556
tagName := opts.RefFullName[len(git.TagPrefix):]
@@ -585,7 +585,7 @@ func createCommitRepoActions(repo *models.Repository, gitRepo *git.Repository, o
585585
}
586586
}
587587

588-
commits = repository.ListToPushCommits(l)
588+
commits = repo_module.ListToPushCommits(l)
589589
}
590590
actions = append(actions, &CommitRepoActionOptions{
591591
PusherName: opts.PusherName,
@@ -610,7 +610,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito
610610
return nil, fmt.Errorf("Old and new revisions are both %s", git.EmptySHA)
611611
}
612612

613-
var commits = &repository.PushCommits{}
613+
var commits = &repo_module.PushCommits{}
614614
if strings.HasPrefix(opts.RefFullName, git.TagPrefix) {
615615
// If is tag reference
616616
tagName := opts.RefFullName[len(git.TagPrefix):]
@@ -621,7 +621,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito
621621
} else {
622622
// Clear cache for tag commit count
623623
cache.Remove(repo.GetCommitsCountCacheKey(tagName, true))
624-
if err := repository.PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
624+
if err := repo_module.PushUpdateAddTag(repo, gitRepo, tagName); err != nil {
625625
return nil, fmt.Errorf("PushUpdateAddTag: %v", err)
626626
}
627627
}
@@ -650,7 +650,7 @@ func createCommitRepoActionOption(repo *models.Repository, gitRepo *git.Reposito
650650
}
651651
}
652652

653-
commits = repository.ListToPushCommits(l)
653+
commits = repo_module.ListToPushCommits(l)
654654
}
655655

656656
return &CommitRepoActionOptions{
File renamed without changes.

routers/api/v1/repo/branch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"code.gitea.io/gitea/modules/convert"
1313
"code.gitea.io/gitea/modules/git"
1414
api "code.gitea.io/gitea/modules/structs"
15+
repo_module "code.gitea.io/gitea/modules/repository"
1516
)
1617

1718
// GetBranch get a branch of a repository
@@ -48,7 +49,7 @@ func GetBranch(ctx *context.APIContext) {
4849
ctx.NotFound()
4950
return
5051
}
51-
branch, err := ctx.Repo.Repository.GetBranch(ctx.Repo.BranchName)
52+
branch, err := repo_module.GetBranch(ctx.Repo.Repository, ctx.Repo.BranchName)
5253
if err != nil {
5354
if git.IsErrBranchNotExist(err) {
5455
ctx.NotFound(err)
@@ -95,7 +96,7 @@ func ListBranches(ctx *context.APIContext) {
9596
// "200":
9697
// "$ref": "#/responses/BranchList"
9798

98-
branches, err := repo_service.GetBranches(ctx.Repo.Repository)
99+
branches, err := repo_module.GetBranches(ctx.Repo.Repository)
99100
if err != nil {
100101
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
101102
return

routers/repo/branch.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import (
1515
"code.gitea.io/gitea/modules/git"
1616
"code.gitea.io/gitea/modules/log"
1717
"code.gitea.io/gitea/modules/repofiles"
18+
repo_module "code.gitea.io/gitea/modules/repository"
1819
"code.gitea.io/gitea/modules/util"
1920
"gopkg.in/src-d/go-git.v4/plumbing"
20-
repo_service "code.gitea.io/gitea/services/repository"
2121
)
2222

2323
const (
@@ -176,7 +176,7 @@ func deleteBranch(ctx *context.Context, branchName string) error {
176176
}
177177

178178
func loadBranches(ctx *context.Context) []*Branch {
179-
rawBranches, err := repo_service.GetBranches(ctx.Repo.Repository)
179+
rawBranches, err := repo_module.GetBranches(ctx.Repo.Repository)
180180
if err != nil {
181181
ctx.ServerError("GetBranches", err)
182182
return nil
@@ -325,9 +325,9 @@ func CreateBranch(ctx *context.Context, form auth.NewBranchForm) {
325325

326326
var err error
327327
if ctx.Repo.IsViewBranch {
328-
err = repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
328+
err = repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
329329
} else {
330-
err = repo_service.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
330+
err = repo_module.CreateNewBranchFromCommit(ctx.User, ctx.Repo.Repository, ctx.Repo.BranchName, form.NewBranchName)
331331
}
332332
if err != nil {
333333
if models.IsErrTagAlreadyExists(err) {

routers/repo/editor.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"code.gitea.io/gitea/modules/setting"
2323
"code.gitea.io/gitea/modules/upload"
2424
"code.gitea.io/gitea/modules/util"
25+
repo_module "code.gitea.io/gitea/modules/repository"
2526
)
2627

2728
const (
@@ -534,7 +535,7 @@ func UploadFilePost(ctx *context.Context, form auth.UploadRepoFileForm) {
534535
}
535536

536537
if oldBranchName != branchName {
537-
if _, err := ctx.Repo.Repository.GetBranch(branchName); err == nil {
538+
if _, err := repo_module.GetBranch(ctx.Repo.Repository, branchName); err == nil {
538539
ctx.Data["Err_NewBranchName"] = true
539540
ctx.RenderWithErr(ctx.Tr("repo.editor.branch_already_exists", branchName), tplUploadFile, &form)
540541
return
@@ -679,7 +680,7 @@ func GetUniquePatchBranchName(ctx *context.Context) string {
679680
prefix := ctx.User.LowerName + "-patch-"
680681
for i := 1; i <= 1000; i++ {
681682
branchName := fmt.Sprintf("%s%d", prefix, i)
682-
if _, err := ctx.Repo.Repository.GetBranch(branchName); err != nil {
683+
if _, err := repo_module.GetBranch(ctx.Repo.Repository, branchName); err != nil {
683684
if git.IsErrBranchNotExist(err) {
684685
return branchName
685686
}

services/mirror/mirror.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@ import (
1111
"strings"
1212
"time"
1313

14-
"code.gitea.io/gitea/modules/graceful"
15-
1614
"code.gitea.io/gitea/models"
1715
"code.gitea.io/gitea/modules/cache"
1816
"code.gitea.io/gitea/modules/git"
17+
"code.gitea.io/gitea/modules/graceful"
1918
"code.gitea.io/gitea/modules/log"
2019
"code.gitea.io/gitea/modules/notification"
21-
"code.gitea.io/gitea/modules/repository"
20+
repo_module "code.gitea.io/gitea/modules/repository"
2221
"code.gitea.io/gitea/modules/setting"
2322
"code.gitea.io/gitea/modules/sync"
2423
"code.gitea.io/gitea/modules/timeutil"
@@ -211,7 +210,7 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) {
211210
log.Error("OpenRepository: %v", err)
212211
return nil, false
213212
}
214-
if err = repository.SyncReleasesWithTags(m.Repo, gitRepo); err != nil {
213+
if err = repo_module.SyncReleasesWithTags(m.Repo, gitRepo); err != nil {
215214
gitRepo.Close()
216215
log.Error("Failed to synchronize tags to releases for repository: %v", err)
217216
}
@@ -253,7 +252,7 @@ func runSync(m *models.Mirror) ([]*mirrorSyncResult, bool) {
253252
}
254253
}
255254

256-
branches, err := repo_service.GetBranches(m.Repo)
255+
branches, err := repo_module.GetBranches(m.Repo)
257256
if err != nil {
258257
log.Error("GetBranches: %v", err)
259258
return nil, false
@@ -403,7 +402,7 @@ func syncMirror(repoID string) {
403402
continue
404403
}
405404

406-
theCommits := repository.ListToPushCommits(commits)
405+
theCommits := repo_module.ListToPushCommits(commits)
407406
if len(theCommits.Commits) > setting.UI.FeedMaxCommitNum {
408407
theCommits.Commits = theCommits.Commits[:setting.UI.FeedMaxCommitNum]
409408
}

0 commit comments

Comments
 (0)