Skip to content

Commit 975a06f

Browse files
committed
Some refactors
1 parent f242d7c commit 975a06f

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

modules/gitrepo/gitrepo.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package gitrepo
55

66
import (
77
"context"
8+
"fmt"
89
"io"
910
"path/filepath"
1011
"strings"
@@ -20,8 +21,12 @@ type Repository interface {
2021
GetOwnerName() string
2122
}
2223

24+
func absPath(owner, name string) string {
25+
return filepath.Join(setting.RepoRootPath, strings.ToLower(owner), strings.ToLower(name)+".git")
26+
}
27+
2328
func repoPath(repo Repository) string {
24-
return filepath.Join(setting.RepoRootPath, strings.ToLower(repo.GetOwnerName()), strings.ToLower(repo.GetName())+".git")
29+
return absPath(repo.GetOwnerName(), repo.GetName())
2530
}
2631

2732
func wikiPath(repo Repository) string {
@@ -79,3 +84,12 @@ func IsRepositoryExist(ctx context.Context, repo Repository) (bool, error) {
7984
func DeleteRepository(ctx context.Context, repo Repository) error {
8085
return util.RemoveAll(repoPath(repo))
8186
}
87+
88+
// RenameRepository renames a repository's name on disk
89+
func RenameRepository(ctx context.Context, repo Repository, newName string) error {
90+
newRepoPath := absPath(repo.GetOwnerName(), newName)
91+
if err := util.Rename(repoPath(repo), newRepoPath); err != nil {
92+
return fmt.Errorf("rename repository directory: %w", err)
93+
}
94+
return nil
95+
}

services/repository/transfer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
project_model "code.gitea.io/gitea/models/project"
1818
repo_model "code.gitea.io/gitea/models/repo"
1919
user_model "code.gitea.io/gitea/models/user"
20+
"code.gitea.io/gitea/modules/gitrepo"
2021
"code.gitea.io/gitea/modules/globallock"
2122
"code.gitea.io/gitea/modules/log"
2223
"code.gitea.io/gitea/modules/util"
@@ -335,8 +336,7 @@ func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newR
335336
}
336337
}
337338

338-
newRepoPath := repo_model.RepoPath(repo.Owner.Name, newRepoName)
339-
if err = util.Rename(repo.RepoPath(), newRepoPath); err != nil {
339+
if err = gitrepo.RenameRepository(ctx, repo, newRepoName); err != nil {
340340
return fmt.Errorf("rename repository directory: %w", err)
341341
}
342342

0 commit comments

Comments
 (0)