Skip to content

Commit 5233051

Browse files
authored
Move some functions into services/repository (#17677)
1 parent 750a846 commit 5233051

File tree

15 files changed

+283
-285
lines changed

15 files changed

+283
-285
lines changed

cmd/admin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"code.gitea.io/gitea/modules/storage"
2626
auth_service "code.gitea.io/gitea/services/auth"
2727
"code.gitea.io/gitea/services/auth/source/oauth2"
28+
repo_service "code.gitea.io/gitea/services/repository"
2829

2930
"github.com/urfave/cli"
3031
)
@@ -612,7 +613,7 @@ func runRegenerateHooks(_ *cli.Context) error {
612613
if err := initDB(ctx); err != nil {
613614
return err
614615
}
615-
return repo_module.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
616+
return repo_service.SyncRepositoryHooks(graceful.GetManager().ShutdownContext())
616617
}
617618

618619
func runRegenerateKeys(_ *cli.Context) error {

integrations/api_repo_get_contents_list_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ import (
1313
"code.gitea.io/gitea/models"
1414
"code.gitea.io/gitea/models/unittest"
1515
"code.gitea.io/gitea/modules/git"
16-
repo_module "code.gitea.io/gitea/modules/repository"
1716
"code.gitea.io/gitea/modules/setting"
1817
api "code.gitea.io/gitea/modules/structs"
18+
repo_service "code.gitea.io/gitea/services/repository"
1919

2020
"github.com/stretchr/testify/assert"
2121
)
@@ -72,7 +72,7 @@ func testAPIGetContentsList(t *testing.T, u *url.URL) {
7272

7373
// Make a new branch in repo1
7474
newBranch := "test_branch"
75-
err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
75+
err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
7676
assert.NoError(t, err)
7777
// Get the commit ID of the default branch
7878
gitRepo, err := git.OpenRepository(repo1.RepoPath())

integrations/api_repo_get_contents_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
"code.gitea.io/gitea/models"
1313
"code.gitea.io/gitea/models/unittest"
1414
"code.gitea.io/gitea/modules/git"
15-
repo_module "code.gitea.io/gitea/modules/repository"
1615
"code.gitea.io/gitea/modules/setting"
1716
api "code.gitea.io/gitea/modules/structs"
17+
repo_service "code.gitea.io/gitea/services/repository"
1818

1919
"github.com/stretchr/testify/assert"
2020
)
@@ -73,7 +73,7 @@ func testAPIGetContents(t *testing.T, u *url.URL) {
7373

7474
// Make a new branch in repo1
7575
newBranch := "test_branch"
76-
err := repo_module.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
76+
err := repo_service.CreateNewBranch(user2, repo1, repo1.DefaultBranch, newBranch)
7777
assert.NoError(t, err)
7878
// Get the commit ID of the default branch
7979
gitRepo, err := git.OpenRepository(repo1.RepoPath())

modules/repository/branch.go

Lines changed: 0 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -24,92 +24,3 @@ func GetBranch(repo *models.Repository, branch string) (*git.Branch, error) {
2424

2525
return gitRepo.GetBranch(branch)
2626
}
27-
28-
// GetBranches returns branches from the repository, skipping skip initial branches and
29-
// returning at most limit branches, or all branches if limit is 0.
30-
func GetBranches(repo *models.Repository, skip, limit int) ([]*git.Branch, int, error) {
31-
return git.GetBranchesByPath(repo.RepoPath(), skip, limit)
32-
}
33-
34-
// checkBranchName validates branch name with existing repository branches
35-
func checkBranchName(repo *models.Repository, name string) error {
36-
gitRepo, err := git.OpenRepository(repo.RepoPath())
37-
if err != nil {
38-
return err
39-
}
40-
defer gitRepo.Close()
41-
42-
branches, _, err := GetBranches(repo, 0, 0)
43-
if err != nil {
44-
return err
45-
}
46-
47-
for _, branch := range branches {
48-
if branch.Name == name {
49-
return models.ErrBranchAlreadyExists{
50-
BranchName: branch.Name,
51-
}
52-
} else if (len(branch.Name) < len(name) && branch.Name+"/" == name[0:len(branch.Name)+1]) ||
53-
(len(branch.Name) > len(name) && name+"/" == branch.Name[0:len(name)+1]) {
54-
return models.ErrBranchNameConflict{
55-
BranchName: branch.Name,
56-
}
57-
}
58-
}
59-
60-
if _, err := gitRepo.GetTag(name); err == nil {
61-
return models.ErrTagAlreadyExists{
62-
TagName: name,
63-
}
64-
}
65-
66-
return nil
67-
}
68-
69-
// CreateNewBranch creates a new repository branch
70-
func CreateNewBranch(doer *models.User, repo *models.Repository, oldBranchName, branchName string) (err error) {
71-
// Check if branch name can be used
72-
if err := checkBranchName(repo, branchName); err != nil {
73-
return err
74-
}
75-
76-
if !git.IsBranchExist(repo.RepoPath(), oldBranchName) {
77-
return models.ErrBranchDoesNotExist{
78-
BranchName: oldBranchName,
79-
}
80-
}
81-
82-
if err := git.Push(repo.RepoPath(), git.PushOptions{
83-
Remote: repo.RepoPath(),
84-
Branch: fmt.Sprintf("%s:%s%s", oldBranchName, git.BranchPrefix, branchName),
85-
Env: models.PushingEnvironment(doer, repo),
86-
}); err != nil {
87-
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
88-
return err
89-
}
90-
return fmt.Errorf("Push: %v", err)
91-
}
92-
93-
return nil
94-
}
95-
96-
// CreateNewBranchFromCommit creates a new repository branch
97-
func CreateNewBranchFromCommit(doer *models.User, repo *models.Repository, commit, branchName string) (err error) {
98-
// Check if branch name can be used
99-
if err := checkBranchName(repo, branchName); err != nil {
100-
return err
101-
}
102-
103-
if err := git.Push(repo.RepoPath(), git.PushOptions{
104-
Remote: repo.RepoPath(),
105-
Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName),
106-
Env: models.PushingEnvironment(doer, repo),
107-
}); err != nil {
108-
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
109-
return err
110-
}
111-
return fmt.Errorf("Push: %v", err)
112-
}
113-
114-
return nil
115-
}

modules/repository/hooks.go

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,13 @@
55
package repository
66

77
import (
8-
"context"
98
"fmt"
109
"os"
1110
"path/filepath"
1211

13-
"code.gitea.io/gitea/models"
14-
"code.gitea.io/gitea/models/db"
1512
"code.gitea.io/gitea/modules/git"
16-
"code.gitea.io/gitea/modules/log"
1713
"code.gitea.io/gitea/modules/setting"
1814
"code.gitea.io/gitea/modules/util"
19-
20-
"xorm.io/builder"
2115
)
2216

2317
func getHookTemplates() (hookNames, hookTpls, giteaHookTpls []string) {
@@ -240,38 +234,3 @@ func CheckDelegateHooks(repoPath string) ([]string, error) {
240234
}
241235
return results, nil
242236
}
243-
244-
// SyncRepositoryHooks rewrites all repositories' pre-receive, update and post-receive hooks
245-
// to make sure the binary and custom conf path are up-to-date.
246-
func SyncRepositoryHooks(ctx context.Context) error {
247-
log.Trace("Doing: SyncRepositoryHooks")
248-
249-
if err := db.Iterate(
250-
db.DefaultContext,
251-
new(models.Repository),
252-
builder.Gt{"id": 0},
253-
func(idx int, bean interface{}) error {
254-
repo := bean.(*models.Repository)
255-
select {
256-
case <-ctx.Done():
257-
return db.ErrCancelledf("before sync repository hooks for %s", repo.FullName())
258-
default:
259-
}
260-
261-
if err := createDelegateHooks(repo.RepoPath()); err != nil {
262-
return fmt.Errorf("SyncRepositoryHook: %v", err)
263-
}
264-
if repo.HasWiki() {
265-
if err := createDelegateHooks(repo.WikiPath()); err != nil {
266-
return fmt.Errorf("SyncRepositoryHook: %v", err)
267-
}
268-
}
269-
return nil
270-
},
271-
); err != nil {
272-
return err
273-
}
274-
275-
log.Trace("Finished: SyncRepositoryHooks")
276-
return nil
277-
}

modules/repository/update.go

Lines changed: 0 additions & 136 deletions
This file was deleted.

routers/api/v1/repo/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func CreateBranch(ctx *context.APIContext) {
176176
opt.OldBranchName = ctx.Repo.Repository.DefaultBranch
177177
}
178178

179-
err := repo_module.CreateNewBranch(ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)
179+
err := repo_service.CreateNewBranch(ctx.User, ctx.Repo.Repository, opt.OldBranchName, opt.BranchName)
180180

181181
if err != nil {
182182
if models.IsErrBranchDoesNotExist(err) {
@@ -257,7 +257,7 @@ func ListBranches(ctx *context.APIContext) {
257257

258258
listOptions := utils.GetListOptions(ctx)
259259
skip, _ := listOptions.GetStartEnd()
260-
branches, totalNumOfBranches, err := repo_module.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize)
260+
branches, totalNumOfBranches, err := repo_service.GetBranches(ctx.Repo.Repository, skip, listOptions.PageSize)
261261
if err != nil {
262262
ctx.Error(http.StatusInternalServerError, "GetBranches", err)
263263
return

routers/init.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"code.gitea.io/gitea/modules/markup"
2626
"code.gitea.io/gitea/modules/markup/external"
2727
"code.gitea.io/gitea/modules/notification"
28-
repo_module "code.gitea.io/gitea/modules/repository"
2928
"code.gitea.io/gitea/modules/setting"
3029
"code.gitea.io/gitea/modules/ssh"
3130
"code.gitea.io/gitea/modules/storage"
@@ -45,7 +44,7 @@ import (
4544
repo_migrations "code.gitea.io/gitea/services/migrations"
4645
mirror_service "code.gitea.io/gitea/services/mirror"
4746
pull_service "code.gitea.io/gitea/services/pull"
48-
"code.gitea.io/gitea/services/repository"
47+
repo_service "code.gitea.io/gitea/services/repository"
4948
"code.gitea.io/gitea/services/webhook"
5049

5150
"gitea.com/go-chi/session"
@@ -73,7 +72,7 @@ func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
7372
func InitGitServices() {
7473
setting.NewServices()
7574
mustInit(storage.Init)
76-
mustInit(repository.NewContext)
75+
mustInit(repo_service.NewContext)
7776
}
7877

7978
func syncAppPathForGit(ctx context.Context) error {
@@ -85,7 +84,7 @@ func syncAppPathForGit(ctx context.Context) error {
8584
log.Info("AppPath changed from '%s' to '%s'", runtimeState.LastAppPath, setting.AppPath)
8685

8786
log.Info("re-sync repository hooks ...")
88-
mustInitCtx(ctx, repo_module.SyncRepositoryHooks)
87+
mustInitCtx(ctx, repo_service.SyncRepositoryHooks)
8988

9089
log.Info("re-write ssh public keys ...")
9190
mustInit(models.RewriteAllPublicKeys)

0 commit comments

Comments
 (0)