refactor: git repo and relative path handling (#38522)

1. simplify "StorageRepo" code
2. simplify git.OpenRepository code
3. by the way, clean up some unused files in git test fixtures.
This commit is contained in:
wxiaoguang
2026-07-19 15:32:00 +08:00
committed by GitHub
parent ae176cd649
commit b06002f449
100 changed files with 315 additions and 290 deletions
+4 -4
View File
@@ -213,10 +213,10 @@ func DeleteUnadoptedRepository(ctx context.Context, doer, u *user_model.User, re
return err
}
relativePath := repo_model.RelativePath(u.Name, repoName)
exist, err := gitrepo.IsRepositoryExist(ctx, repo_model.StorageRepo(relativePath))
codeRepo := repo_model.CodeRepoByName(u.Name, repoName)
exist, err := gitrepo.IsRepositoryExist(ctx, codeRepo)
if err != nil {
log.Error("Unable to check if %s exists. Error: %v", relativePath, err)
log.Error("Unable to check if repo %s/%s exists. Error: %v", u.Name, repoName, err)
return err
}
if !exist {
@@ -235,7 +235,7 @@ func DeleteUnadoptedRepository(ctx context.Context, doer, u *user_model.User, re
}
}
return gitrepo.DeleteRepository(ctx, repo_model.StorageRepo(relativePath))
return gitrepo.DeleteRepository(ctx, codeRepo)
}
type unadoptedRepositories struct {
+2 -2
View File
@@ -79,7 +79,7 @@ func (t *TemporaryUploadRepository) Clone(ctx context.Context, branch string, ba
}
return fmt.Errorf("Clone: %w %s", err, stderr)
}
gitRepo, err := git.OpenRepository(t.basePath)
gitRepo, err := git.OpenRepositoryLocal(t.basePath)
if err != nil {
return err
}
@@ -92,7 +92,7 @@ func (t *TemporaryUploadRepository) Init(ctx context.Context, objectFormatName s
if err := git.InitRepository(ctx, t.basePath, false, objectFormatName); err != nil {
return err
}
gitRepo, err := git.OpenRepository(t.basePath)
gitRepo, err := git.OpenRepositoryLocal(t.basePath)
if err != nil {
return err
}
+1 -1
View File
@@ -16,7 +16,7 @@ import (
)
func BenchmarkGetCommitGraph(b *testing.B) {
currentRepo, err := git.OpenRepository(".")
currentRepo, err := git.OpenRepositoryLocal(".")
if err != nil || currentRepo == nil {
b.Error("Could not open repository")
}
+2 -2
View File
@@ -55,7 +55,7 @@ func cloneWiki(ctx context.Context, repo *repo_model.Repository, opts migration.
return "", err
}
if err := gitrepo.WriteCommitGraph(ctx, storageRepo); err != nil {
if err := git.WriteCommitGraph(ctx, storageRepo); err != nil {
cleanIncompleteWikiPath()
return "", err
}
@@ -102,7 +102,7 @@ func MigrateRepositoryGitData(ctx context.Context, u *user_model.User,
return repo, fmt.Errorf("clone error: %w", err)
}
if err := gitrepo.WriteCommitGraph(ctx, repo); err != nil {
if err := git.WriteCommitGraph(ctx, repo); err != nil {
return repo, err
}
+1 -1
View File
@@ -331,7 +331,7 @@ func CheckCreateRepository(ctx context.Context, doer, owner *user_model.User, na
} else if has {
return repo_model.ErrRepoAlreadyExist{Uname: owner.Name, Name: name}
}
repo := repo_model.StorageRepo(repo_model.RelativePath(owner.Name, name))
repo := repo_model.CodeRepoByName(owner.Name, name)
isExist, err := gitrepo.IsRepositoryExist(ctx, repo)
if err != nil {
log.Error("Unable to check if repo %s/%s exists, error: %v", owner.Name, name, err)
+21 -18
View File
@@ -94,7 +94,7 @@ func isRepositoryModelOrDirExist(ctx context.Context, u *user_model.User, repoNa
if err != nil {
return false, err
}
repo := repo_model.StorageRepo(repo_model.RelativePath(u.Name, repoName))
repo := repo_model.CodeRepoByName(u.Name, repoName)
isExist, err := gitrepo.IsRepositoryExist(ctx, repo)
return has || isExist, err
}
@@ -116,18 +116,19 @@ func transferOwnership(ctx context.Context, doer *user_model.User, newOwnerName
}
if repoRenamed {
oldRelativePath, newRelativePath := repo_model.RelativePath(newOwnerName, repo.Name), repo_model.RelativePath(oldOwnerName, repo.Name)
if err := gitrepo.RenameRepository(ctx, repo_model.StorageRepo(oldRelativePath), repo_model.StorageRepo(newRelativePath)); err != nil {
log.Error("Unable to move repository %s/%s directory from %s back to correct place %s: %v", oldOwnerName, repo.Name,
oldRelativePath, newRelativePath, err)
// revert the rename
from := repo_model.CodeRepoByName(newOwnerName, repo.Name)
to := repo_model.CodeRepoByName(oldOwnerName, repo.Name)
if err := gitrepo.RenameRepository(ctx, from, to); err != nil {
log.Error("Unable to revert repository %s/%s to %s/%s: %v", newOwnerName, repo.Name, oldOwnerName, repo.Name, err)
}
}
if wikiRenamed {
oldRelativePath, newRelativePath := repo_model.RelativeWikiPath(newOwnerName, repo.Name), repo_model.RelativeWikiPath(oldOwnerName, repo.Name)
if err := gitrepo.RenameRepository(ctx, repo_model.StorageRepo(oldRelativePath), repo_model.StorageRepo(newRelativePath)); err != nil {
log.Error("Unable to move wiki for repository %s/%s directory from %s back to correct place %s: %v", oldOwnerName, repo.Name,
oldRelativePath, newRelativePath, err)
from := repo_model.WikiRepoByName(newOwnerName, repo.Name)
to := repo_model.WikiRepoByName(oldOwnerName, repo.Name)
if err := gitrepo.RenameRepository(ctx, from, to); err != nil {
log.Error("Unable to revert wiki repository %s/%s to %s/%s: %v", newOwnerName, repo.Name, oldOwnerName, repo.Name, err)
}
}
@@ -302,19 +303,21 @@ func transferOwnership(ctx context.Context, doer *user_model.User, newOwnerName
}
// Rename remote repository to new path and delete local copy.
oldRelativePath, newRelativePath := repo_model.RelativePath(oldOwner.Name, repo.Name), repo_model.RelativePath(newOwner.Name, repo.Name)
if err := gitrepo.RenameRepository(ctx, repo_model.StorageRepo(oldRelativePath), repo_model.StorageRepo(newRelativePath)); err != nil {
oldCodeRepo := repo_model.CodeRepoByName(oldOwner.Name, repo.Name)
newCodeRepo := repo_model.CodeRepoByName(newOwner.Name, repo.Name)
if err := gitrepo.RenameRepository(ctx, oldCodeRepo, newCodeRepo); err != nil {
return fmt.Errorf("rename repository directory: %w", err)
}
repoRenamed = true
// Rename remote wiki repository to new path and delete local copy.
wikiStorageRepo := repo_model.StorageRepo(repo_model.RelativeWikiPath(oldOwner.Name, repo.Name))
if isExist, err := gitrepo.IsRepositoryExist(ctx, wikiStorageRepo); err != nil {
oldWikiRepo := repo_model.WikiRepoByName(oldOwner.Name, repo.Name)
if isExist, err := gitrepo.IsRepositoryExist(ctx, oldWikiRepo); err != nil {
log.Error("Unable to check if wiki of repo %s/%s exists. Error: %v", oldOwner.Name, repo.Name, err)
return err
} else if isExist {
if err := gitrepo.RenameRepository(ctx, wikiStorageRepo, repo_model.StorageRepo(repo_model.RelativeWikiPath(newOwner.Name, repo.Name))); err != nil {
newWikiRepo := repo_model.WikiRepoByName(newOwner.Name, repo.Name)
if err := gitrepo.RenameRepository(ctx, oldWikiRepo, newWikiRepo); err != nil {
return fmt.Errorf("rename repository wiki: %w", err)
}
wikiRenamed = true
@@ -373,14 +376,14 @@ func changeRepositoryName(ctx context.Context, repo *repo_model.Repository, newR
}
}
if err = gitrepo.RenameRepository(ctx, repo,
repo_model.StorageRepo(repo_model.RelativePath(repo.OwnerName, newRepoName))); err != nil {
newCodeRepo := repo_model.CodeRepoByName(repo.OwnerName, newRepoName)
if err = gitrepo.RenameRepository(ctx, repo, newCodeRepo); err != nil {
return fmt.Errorf("rename repository directory: %w", err)
}
if HasWiki(ctx, repo) {
if err = gitrepo.RenameRepository(ctx, repo.WikiStorageRepo(), repo_model.StorageRepo(
repo_model.RelativeWikiPath(repo.OwnerName, newRepoName))); err != nil {
newWikiRepo := repo_model.WikiRepoByName(repo.OwnerName, newRepoName)
if err = gitrepo.RenameRepository(ctx, repo.WikiStorageRepo(), newWikiRepo); err != nil {
return fmt.Errorf("rename repository wiki: %w", err)
}
}