diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2025-03-14 11:38:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-03-14 18:38:55 +0000 |
commit | 55cc649d3d293a13eee7a56ca1744577c08b4e6c (patch) | |
tree | 73d51a1cab48cfd63faaf7f3546cb146c6ef4442 /services | |
parent | a0e0a30d23e0890538bbe8e9edc3c07ae5242bb1 (diff) | |
download | gitea-55cc649d3d293a13eee7a56ca1744577c08b4e6c.tar.gz gitea-55cc649d3d293a13eee7a56ca1744577c08b4e6c.zip |
Add abstraction layer to delete repository from disk (#33879)
Extract from #28966
Follow #33874
Diffstat (limited to 'services')
-rw-r--r-- | services/repository/create.go | 2 | ||||
-rw-r--r-- | services/repository/delete.go | 10 | ||||
-rw-r--r-- | services/repository/fork.go | 2 | ||||
-rw-r--r-- | services/repository/transfer.go | 4 |
4 files changed, 12 insertions, 6 deletions
diff --git a/services/repository/create.go b/services/repository/create.go index 62b5931fd9..1a6a68b35a 100644 --- a/services/repository/create.go +++ b/services/repository/create.go @@ -277,7 +277,7 @@ func CreateRepositoryDirectly(ctx context.Context, doer, u *user_model.User, opt } if err = initRepository(ctx, doer, repo, opts); err != nil { - if err2 := util.RemoveAll(repo.RepoPath()); err2 != nil { + if err2 := gitrepo.DeleteRepository(ctx, repo); err2 != nil { log.Error("initRepository: %v", err) return fmt.Errorf( "delete repo directory %s/%s failed(2): %v", u.Name, repo.Name, err2) diff --git a/services/repository/delete.go b/services/repository/delete.go index 3b953d3ec7..ff74a20817 100644 --- a/services/repository/delete.go +++ b/services/repository/delete.go @@ -23,6 +23,7 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/models/webhook" actions_module "code.gitea.io/gitea/modules/actions" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/storage" @@ -289,8 +290,13 @@ func DeleteRepositoryDirectly(ctx context.Context, doer *user_model.User, repoID // we delete the file but the database rollback, the repository will be broken. // Remove repository files. - repoPath := repo.RepoPath() - system_model.RemoveAllWithNotice(ctx, "Delete repository files", repoPath) + if err := gitrepo.DeleteRepository(ctx, repo); err != nil { + desc := fmt.Sprintf("Delete repository files [%s]: %v", repo.FullName(), err) + // Note we use the db.DefaultContext here rather than passing in a context as the context may be cancelled + if err = system_model.CreateNotice(db.DefaultContext, system_model.NoticeRepository, desc); err != nil { + log.Error("CreateRepositoryNotice: %v", err) + } + } // Remove wiki files if repo.HasWiki() { diff --git a/services/repository/fork.go b/services/repository/fork.go index 1bd4d4a13b..ec6ba56ddf 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -116,7 +116,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork // As the transaction will be failed and hence database changes will be destroyed we only need // to delete the related repository on the filesystem - if errDelete := util.RemoveAll(repo.RepoPath()); errDelete != nil { + if errDelete := gitrepo.DeleteRepository(ctx, repo); errDelete != nil { log.Error("Failed to remove fork repo") } } diff --git a/services/repository/transfer.go b/services/repository/transfer.go index bd3bf326b4..3940b2a142 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -17,6 +17,7 @@ import ( project_model "code.gitea.io/gitea/models/project" repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/globallock" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/util" @@ -335,8 +336,7 @@ func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newR } } - newRepoPath := repo_model.RepoPath(repo.Owner.Name, newRepoName) - if err = util.Rename(repo.RepoPath(), newRepoPath); err != nil { + if err = gitrepo.RenameRepository(ctx, repo, newRepoName); err != nil { return fmt.Errorf("rename repository directory: %w", err) } |