Add abstraction layer to delete repository from disk (#33879)

Extract from #28966 
Follow #33874
This commit is contained in:
Lunny Xiao
2025-03-14 11:38:55 -07:00
committed by GitHub
parent a0e0a30d23
commit 55cc649d3d
6 changed files with 37 additions and 11 deletions

View File

@ -5,6 +5,7 @@ package gitrepo
import (
"context"
"fmt"
"io"
"path/filepath"
"strings"
@ -20,8 +21,12 @@ type Repository interface {
GetOwnerName() string
}
func absPath(owner, name string) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(owner), strings.ToLower(name)+".git")
}
func repoPath(repo Repository) string {
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".git")
return absPath(repo.GetOwnerName(), repo.GetName())
}
func wikiPath(repo Repository) string {
@ -74,3 +79,17 @@ func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo Reposito
func IsRepositoryExist(ctx context.Context, repo Repository) (bool, error) {
return util.IsExist(repoPath(repo))
}
// DeleteRepository deletes the repository directory from the disk
func DeleteRepository(ctx context.Context, repo Repository) error {
return util.RemoveAll(repoPath(repo))
}
// RenameRepository renames a repository's name on disk
func RenameRepository(ctx context.Context, repo Repository, newName string) error {
newRepoPath := absPath(repo.GetOwnerName(), newName)
if err := util.Rename(repoPath(repo), newRepoPath); err != nil {
return fmt.Errorf("rename repository directory: %w", err)
}
return nil
}