aboutsummaryrefslogtreecommitdiffstats
path: root/modules/gitrepo/gitrepo.go
diff options
context:
space:
mode:
Diffstat (limited to 'modules/gitrepo/gitrepo.go')
-rw-r--r--modules/gitrepo/gitrepo.go36
1 files changed, 24 insertions, 12 deletions
diff --git a/modules/gitrepo/gitrepo.go b/modules/gitrepo/gitrepo.go
index 540b724489..5da65e2452 100644
--- a/modules/gitrepo/gitrepo.go
+++ b/modules/gitrepo/gitrepo.go
@@ -5,9 +5,9 @@ package gitrepo
import (
"context"
+ "fmt"
"io"
"path/filepath"
- "strings"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/reqctx"
@@ -15,17 +15,15 @@ import (
"code.gitea.io/gitea/modules/util"
)
+// Repository represents a git repository which stored in a disk
type Repository interface {
- GetName() string
- GetOwnerName() string
+ RelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path
}
+// RelativePath should be an unix style path like username/reponame.git
+// This method should change it according to the current OS.
func repoPath(repo Repository) string {
- return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".git")
-}
-
-func wikiPath(repo Repository) string {
- return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".wiki.git")
+ return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.RelativePath()))
}
// OpenRepository opens the repository at the given relative path with the provided context.
@@ -33,10 +31,6 @@ func OpenRepository(ctx context.Context, repo Repository) (*git.Repository, erro
return git.OpenRepository(ctx, repoPath(repo))
}
-func OpenWikiRepository(ctx context.Context, repo Repository) (*git.Repository, error) {
- return git.OpenRepository(ctx, wikiPath(repo))
-}
-
// contextKey is a value for use with context.WithValue.
type contextKey struct {
repoPath string
@@ -69,3 +63,21 @@ func RepositoryFromRequestContextOrOpen(ctx reqctx.RequestContext, repo Reposito
ctx.SetContextValue(ck, gitRepo)
return gitRepo, nil
}
+
+// IsRepositoryExist returns true if the repository directory exists in the disk
+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, newRepo Repository) error {
+ if err := util.Rename(repoPath(repo), repoPath(newRepo)); err != nil {
+ return fmt.Errorf("rename repository directory: %w", err)
+ }
+ return nil
+}