diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2022-05-09 00:46:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-08 18:46:32 +0200 |
commit | 4ca1d7547a2c32cc65ca23d1a7698d1a8c921d65 (patch) | |
tree | fee132403adea441987ce25809c764e00fd962cd /services | |
parent | d4834071da9ce010fd4677ef339c598dd23dc130 (diff) | |
download | gitea-4ca1d7547a2c32cc65ca23d1a7698d1a8c921d65.tar.gz gitea-4ca1d7547a2c32cc65ca23d1a7698d1a8c921d65.zip |
Move some helper files out of models (#19355)
* Move some helper files out of models
* Some improvements
Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'services')
-rw-r--r-- | services/pull/merge.go | 5 | ||||
-rw-r--r-- | services/pull/patch.go | 3 | ||||
-rw-r--r-- | services/pull/pull.go | 3 | ||||
-rw-r--r-- | services/pull/temp_repo.go | 19 | ||||
-rw-r--r-- | services/pull/update.go | 3 | ||||
-rw-r--r-- | services/repository/branch.go | 4 | ||||
-rw-r--r-- | services/repository/files/temp_repo.go | 7 | ||||
-rw-r--r-- | services/repository/repository.go | 12 | ||||
-rw-r--r-- | services/wiki/wiki.go | 12 |
9 files changed, 39 insertions, 29 deletions
diff --git a/services/pull/merge.go b/services/pull/merge.go index e054df716b..0af3cc1613 100644 --- a/services/pull/merge.go +++ b/services/pull/merge.go @@ -28,6 +28,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/references" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" asymkey_service "code.gitea.io/gitea/services/asymkey" @@ -234,7 +235,7 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User return "", err } defer func() { - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("Merge: RemoveTemporaryPath: %s", err) } }() @@ -590,7 +591,7 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User headUser = pr.HeadRepo.Owner } - env = models.FullPushingEnvironment( + env = repo_module.FullPushingEnvironment( headUser, doer, pr.BaseRepo, diff --git a/services/pull/patch.go b/services/pull/patch.go index eeedcf2d38..6e2889b060 100644 --- a/services/pull/patch.go +++ b/services/pull/patch.go @@ -20,6 +20,7 @@ import ( "code.gitea.io/gitea/modules/graceful" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/process" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/util" "github.com/gobwas/glob" @@ -64,7 +65,7 @@ func TestPatch(pr *models.PullRequest) error { return err } defer func() { - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("Merge: RemoveTemporaryPath: %s", err) } }() diff --git a/services/pull/pull.go b/services/pull/pull.go index d226c60ec2..b94b6769a4 100644 --- a/services/pull/pull.go +++ b/services/pull/pull.go @@ -24,6 +24,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" "code.gitea.io/gitea/modules/process" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/sync" issue_service "code.gitea.io/gitea/services/issue" @@ -452,7 +453,7 @@ func pushToBaseRepoHelper(ctx context.Context, pr *models.PullRequest, prefixHea Branch: prefixHeadBranch + pr.HeadBranch + ":" + gitRefName, Force: true, // Use InternalPushingEnvironment here because we know that pre-receive and post-receive do not run on a refs/pulls/... - Env: models.InternalPushingEnvironment(pr.Issue.Poster, pr.BaseRepo), + Env: repo_module.InternalPushingEnvironment(pr.Issue.Poster, pr.BaseRepo), }); err != nil { if git.IsErrPushOutOfDate(err) { // This should not happen as we're using force! diff --git a/services/pull/temp_repo.go b/services/pull/temp_repo.go index f8f44ac018..6b01809d49 100644 --- a/services/pull/temp_repo.go +++ b/services/pull/temp_repo.go @@ -16,6 +16,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + repo_module "code.gitea.io/gitea/modules/repository" ) // createTemporaryRepo creates a temporary repo with "base" for pr.BaseBranch and "tracking" for pr.HeadBranch @@ -46,7 +47,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e } // Clone base repo. - tmpBasePath, err := models.CreateTemporaryPath("pull") + tmpBasePath, err := repo_module.CreateTemporaryPath("pull") if err != nil { log.Error("CreateTemporaryPath: %v", err) return "", err @@ -57,7 +58,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e if err := git.InitRepository(ctx, tmpBasePath, false); err != nil { log.Error("git init tmpBasePath: %v", err) - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } return "", err @@ -85,7 +86,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e if err := addCacheRepo(tmpBasePath, baseRepoPath); err != nil { log.Error("Unable to add base repository to temporary repo [%s -> %s]: %v", pr.BaseRepo.FullName(), tmpBasePath, err) - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } return "", fmt.Errorf("Unable to add base repository to temporary repo [%s -> tmpBasePath]: %v", pr.BaseRepo.FullName(), err) @@ -99,7 +100,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e Stderr: &errbuf, }); err != nil { log.Error("Unable to add base repository as origin [%s -> %s]: %v\n%s\n%s", pr.BaseRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String()) - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } return "", fmt.Errorf("Unable to add base repository as origin [%s -> tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), err, outbuf.String(), errbuf.String()) @@ -114,7 +115,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e Stderr: &errbuf, }); err != nil { log.Error("Unable to fetch origin base branch [%s:%s -> base, original_base in %s]: %v:\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, tmpBasePath, err, outbuf.String(), errbuf.String()) - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } return "", fmt.Errorf("Unable to fetch origin base branch [%s:%s -> base, original_base in tmpBasePath]: %v\n%s\n%s", pr.BaseRepo.FullName(), pr.BaseBranch, err, outbuf.String(), errbuf.String()) @@ -129,7 +130,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e Stderr: &errbuf, }); err != nil { log.Error("Unable to set HEAD as base branch [%s]: %v\n%s\n%s", tmpBasePath, err, outbuf.String(), errbuf.String()) - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } return "", fmt.Errorf("Unable to set HEAD as base branch [tmpBasePath]: %v\n%s\n%s", err, outbuf.String(), errbuf.String()) @@ -139,7 +140,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e if err := addCacheRepo(tmpBasePath, headRepoPath); err != nil { log.Error("Unable to add head repository to temporary repo [%s -> %s]: %v", pr.HeadRepo.FullName(), tmpBasePath, err) - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } return "", fmt.Errorf("Unable to head base repository to temporary repo [%s -> tmpBasePath]: %v", pr.HeadRepo.FullName(), err) @@ -152,7 +153,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e Stderr: &errbuf, }); err != nil { log.Error("Unable to add head repository as head_repo [%s -> %s]: %v\n%s\n%s", pr.HeadRepo.FullName(), tmpBasePath, err, outbuf.String(), errbuf.String()) - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } return "", fmt.Errorf("Unable to add head repository as head_repo [%s -> tmpBasePath]: %v\n%s\n%s", pr.HeadRepo.FullName(), err, outbuf.String(), errbuf.String()) @@ -176,7 +177,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e Stdout: &outbuf, Stderr: &errbuf, }); err != nil { - if err := models.RemoveTemporaryPath(tmpBasePath); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil { log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err) } if !git.IsBranchExist(ctx, pr.HeadRepo.RepoPath(), pr.HeadBranch) { diff --git a/services/pull/update.go b/services/pull/update.go index 08967b59b3..3c5c1c048c 100644 --- a/services/pull/update.go +++ b/services/pull/update.go @@ -14,6 +14,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + repo_module "code.gitea.io/gitea/modules/repository" ) // Update updates pull request with base branch. @@ -154,7 +155,7 @@ func GetDiverging(ctx context.Context, pr *models.PullRequest) (*git.DivergeObje return nil, err } defer func() { - if err := models.RemoveTemporaryPath(tmpRepo); err != nil { + if err := repo_module.RemoveTemporaryPath(tmpRepo); err != nil { log.Error("Merge: RemoveTemporaryPath: %s", err) } }() diff --git a/services/repository/branch.go b/services/repository/branch.go index d9fc47c63a..88fc67fa8c 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -36,7 +36,7 @@ func CreateNewBranch(ctx context.Context, doer *user_model.User, repo *repo_mode if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ Remote: repo.RepoPath(), Branch: fmt.Sprintf("%s%s:%s%s", git.BranchPrefix, oldBranchName, git.BranchPrefix, branchName), - Env: models.PushingEnvironment(doer, repo), + Env: repo_module.PushingEnvironment(doer, repo), }); err != nil { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { return err @@ -93,7 +93,7 @@ func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{ Remote: repo.RepoPath(), Branch: fmt.Sprintf("%s:%s%s", commit, git.BranchPrefix, branchName), - Env: models.PushingEnvironment(doer, repo), + Env: repo_module.PushingEnvironment(doer, repo), }); err != nil { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { return err diff --git a/services/repository/files/temp_repo.go b/services/repository/files/temp_repo.go index 8ebf991382..9c7d9aafec 100644 --- a/services/repository/files/temp_repo.go +++ b/services/repository/files/temp_repo.go @@ -19,6 +19,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/log" + repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" asymkey_service "code.gitea.io/gitea/services/asymkey" "code.gitea.io/gitea/services/gitdiff" @@ -34,7 +35,7 @@ type TemporaryUploadRepository struct { // NewTemporaryUploadRepository creates a new temporary upload repository func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Repository) (*TemporaryUploadRepository, error) { - basePath, err := models.CreateTemporaryPath("upload") + basePath, err := repo_module.CreateTemporaryPath("upload") if err != nil { return nil, err } @@ -45,7 +46,7 @@ func NewTemporaryUploadRepository(ctx context.Context, repo *repo_model.Reposito // Close the repository cleaning up all files func (t *TemporaryUploadRepository) Close() { defer t.gitRepo.Close() - if err := models.RemoveTemporaryPath(t.basePath); err != nil { + if err := repo_module.RemoveTemporaryPath(t.basePath); err != nil { log.Error("Failed to remove temporary path %s: %v", t.basePath, err) } } @@ -315,7 +316,7 @@ func (t *TemporaryUploadRepository) CommitTreeWithDate(parent string, author, co // Push the provided commitHash to the repository branch by the provided user func (t *TemporaryUploadRepository) Push(doer *user_model.User, commitHash, branch string) error { // Because calls hooks we need to pass in the environment - env := models.PushingEnvironment(doer, t.repo) + env := repo_module.PushingEnvironment(doer, t.repo) if err := git.Push(t.ctx, t.basePath, git.PushOptions{ Remote: t.repo.RepoPath(), Branch: strings.TrimSpace(commitHash) + ":" + git.BranchPrefix + strings.TrimSpace(branch), diff --git a/services/repository/repository.go b/services/repository/repository.go index 685a3c7601..6799ca586e 100644 --- a/services/repository/repository.go +++ b/services/repository/repository.go @@ -9,6 +9,8 @@ import ( "fmt" "code.gitea.io/gitea/models" + admin_model "code.gitea.io/gitea/models/admin" + "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/organization" packages_model "code.gitea.io/gitea/models/packages" repo_model "code.gitea.io/gitea/models/repo" @@ -16,7 +18,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/notification" repo_module "code.gitea.io/gitea/modules/repository" - cfg "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/setting" pull_service "code.gitea.io/gitea/services/pull" ) @@ -67,7 +69,7 @@ func PushCreateRepo(authUser, owner *user_model.User, repoName string) (*repo_mo repo, err := CreateRepository(authUser, owner, models.CreateRepoOptions{ Name: repoName, - IsPrivate: cfg.Repository.DefaultPushCreatePrivate, + IsPrivate: setting.Repository.DefaultPushCreatePrivate, }) if err != nil { return nil, err @@ -76,8 +78,10 @@ func PushCreateRepo(authUser, owner *user_model.User, repoName string) (*repo_mo return repo, nil } -// NewContext start repository service -func NewContext() error { +// Init start repository service +func Init() error { repo_module.LoadRepoConfig() + admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up temporary repository uploads", setting.Repository.Upload.TempPath) + admin_model.RemoveAllWithNotice(db.DefaultContext, "Clean up temporary repositories", repo_module.LocalCopyPath()) return initPushQueue() } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 796291fd38..43e35eed69 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -133,12 +133,12 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model hasMasterBranch := git.IsBranchExist(ctx, repo.WikiPath(), "master") - basePath, err := models.CreateTemporaryPath("update-wiki") + basePath, err := repo_module.CreateTemporaryPath("update-wiki") if err != nil { return err } defer func() { - if err := models.RemoveTemporaryPath(basePath); err != nil { + if err := repo_module.RemoveTemporaryPath(basePath); err != nil { log.Error("Merge: RemoveTemporaryPath: %s", err) } }() @@ -249,7 +249,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{ Remote: "origin", Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), - Env: models.FullPushingEnvironment( + Env: repo_module.FullPushingEnvironment( doer, doer, repo, @@ -287,12 +287,12 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model return fmt.Errorf("InitWiki: %v", err) } - basePath, err := models.CreateTemporaryPath("update-wiki") + basePath, err := repo_module.CreateTemporaryPath("update-wiki") if err != nil { return err } defer func() { - if err := models.RemoveTemporaryPath(basePath); err != nil { + if err := repo_module.RemoveTemporaryPath(basePath); err != nil { log.Error("Merge: RemoveTemporaryPath: %s", err) } }() @@ -363,7 +363,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model if err := git.Push(gitRepo.Ctx, basePath, git.PushOptions{ Remote: "origin", Branch: fmt.Sprintf("%s:%s%s", commitHash.String(), git.BranchPrefix, "master"), - Env: models.PushingEnvironment(doer, repo), + Env: repo_module.PushingEnvironment(doer, repo), }); err != nil { if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) { return err |