Skip to content

Commit

Permalink
Some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed May 8, 2022
1 parent e4a41a8 commit 0880969
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 32 deletions.
11 changes: 10 additions & 1 deletion modules/repository/temp.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

Expand All @@ -12,6 +12,7 @@ import (

"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

// LocalCopyPath returns the local repository temporary copy path.
Expand All @@ -36,3 +37,11 @@ func CreateTemporaryPath(prefix string) (string, error) {
}
return basePath, nil
}

// RemoveTemporaryPath removes the temporary path
func RemoveTemporaryPath(basePath string) error {
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
return util.RemoveAll(basePath)
}
return nil
}
8 changes: 0 additions & 8 deletions modules/util/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ func RemoveAll(name string) error {
return err
}

// RemoveTemporaryPath removes the temporary path
func RemoveTemporaryPath(basePath string) error {
if _, err := os.Stat(basePath); !os.IsNotExist(err) {
return RemoveAll(basePath)
}
return nil
}

// Rename renames (moves) oldpath to newpath with at most 5 attempts.
func Rename(oldpath, newpath string) error {
var err error
Expand Down
4 changes: 2 additions & 2 deletions routers/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func mustInitCtx(ctx context.Context, fn func(ctx context.Context) error) {
func InitGitServices() {
setting.NewServices()
mustInit(storage.Init)
mustInit(repo_service.NewContext)
mustInit(repo_service.Init)
}

func syncAppPathForGit(ctx context.Context) error {
Expand Down Expand Up @@ -140,7 +140,7 @@ func GlobalInitInstalled(ctx context.Context) {
mustInit(oauth2.Init)

models.NewRepoContext()
mustInit(repo_service.NewContext)
mustInit(repo_service.Init)

// Booting long running goroutines.
cron.NewContext(ctx)
Expand Down
2 changes: 1 addition & 1 deletion routers/web/repo/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func LFSLocks(ctx *context.Context) {
return
}
defer func() {
if err := util.RemoveTemporaryPath(tmpBasePath); err != nil {
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("LFSLocks: RemoveTemporaryPath: %v", err)
}
}()
Expand Down
3 changes: 1 addition & 2 deletions services/pull/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
asymkey_service "code.gitea.io/gitea/services/asymkey"
issue_service "code.gitea.io/gitea/services/issue"
)
Expand Down Expand Up @@ -143,7 +142,7 @@ func rawMerge(ctx context.Context, pr *models.PullRequest, doer *user_model.User
return "", err
}
defer func() {
if err := util.RemoveTemporaryPath(tmpBasePath); err != nil {
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err)
}
}()
Expand Down
3 changes: 2 additions & 1 deletion services/pull/patch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestPatch(pr *models.PullRequest) error {
return err
}
defer func() {
if err := util.RemoveTemporaryPath(tmpBasePath); err != nil {
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err)
}
}()
Expand Down
17 changes: 8 additions & 9 deletions services/pull/temp_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"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/util"
)

// createTemporaryRepo creates a temporary repo with "base" for pr.BaseBranch and "tracking" for pr.HeadBranch
Expand Down Expand Up @@ -59,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 := util.RemoveTemporaryPath(tmpBasePath); err != nil {
if err := repo_module.RemoveTemporaryPath(tmpBasePath); err != nil {
log.Error("CreateTempRepo: RemoveTemporaryPath: %s", err)
}
return "", err
Expand Down Expand Up @@ -87,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 := util.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)
Expand All @@ -101,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 := util.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())
Expand All @@ -116,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 := util.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())
Expand All @@ -131,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 := util.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())
Expand All @@ -141,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 := util.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)
Expand All @@ -154,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 := util.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())
Expand All @@ -178,7 +177,7 @@ func createTemporaryRepo(ctx context.Context, pr *models.PullRequest) (string, e
Stdout: &outbuf,
Stderr: &errbuf,
}); err != nil {
if err := util.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) {
Expand Down
4 changes: 2 additions & 2 deletions services/pull/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
repo_module "code.gitea.io/gitea/modules/repository"
)

// Update updates pull request with base branch.
Expand Down Expand Up @@ -155,7 +155,7 @@ func GetDiverging(ctx context.Context, pr *models.PullRequest) (*git.DivergeObje
return nil, err
}
defer func() {
if err := util.RemoveTemporaryPath(tmpRepo); err != nil {
if err := repo_module.RemoveTemporaryPath(tmpRepo); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err)
}
}()
Expand Down
3 changes: 1 addition & 2 deletions services/repository/files/temp_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"code.gitea.io/gitea/modules/log"
repo_module "code.gitea.io/gitea/modules/repository"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
asymkey_service "code.gitea.io/gitea/services/asymkey"
"code.gitea.io/gitea/services/gitdiff"
)
Expand All @@ -47,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 := util.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)
}
}
Expand Down
4 changes: 2 additions & 2 deletions services/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ 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())
Expand Down
4 changes: 2 additions & 2 deletions services/wiki/wiki.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func updateWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return err
}
defer func() {
if err := util.RemoveTemporaryPath(basePath); err != nil {
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err)
}
}()
Expand Down Expand Up @@ -292,7 +292,7 @@ func DeleteWikiPage(ctx context.Context, doer *user_model.User, repo *repo_model
return err
}
defer func() {
if err := util.RemoveTemporaryPath(basePath); err != nil {
if err := repo_module.RemoveTemporaryPath(basePath); err != nil {
log.Error("Merge: RemoveTemporaryPath: %s", err)
}
}()
Expand Down

0 comments on commit 0880969

Please sign in to comment.