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
+1 -1
View File
@@ -80,7 +80,7 @@ func (archiver *RepoArchiver) RelativePath() string {
return fmt.Sprintf("%d/%s/%s.%s", archiver.RepoID, archiver.CommitID[:2], archiver.CommitID, archiver.Type.String())
}
// repoArchiverForRelativePath takes a relativePath created from (archiver *RepoArchiver) RelativePath() and creates a shell repoArchiver struct representing it
// repoArchiverForRelativePath takes a relativePath created from RepoArchiver.RelativePath() and creates a shell repoArchiver struct representing it
func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
parts := strings.SplitN(relativePath, "/", 3)
if len(parts) != 3 {
-24
View File
@@ -226,30 +226,6 @@ func init() {
db.RegisterModel(new(Repository))
}
func RelativePath(ownerName, repoName string) string {
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".git"
}
func (repo *Repository) GitRepoLocation() string {
return RelativePath(repo.OwnerName, repo.Name)
}
func (repo *Repository) GitRepoUniqueID() string {
return strconv.FormatInt(repo.ID, 10)
}
type StorageRepo string
func (sr StorageRepo) GitRepoUniqueID() string {
// TODO: need to correctly refactor this method in the future.
// "unique id" should be a cache-key-friendly string, but not a full repo path
return string(sr)
}
func (sr StorageRepo) GitRepoLocation() string {
return string(sr)
}
// SanitizedOriginalURL returns a sanitized OriginalURL
func (repo *Repository) SanitizedOriginalURL() string {
if repo.OriginalURL == "" {
+56
View File
@@ -0,0 +1,56 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"strconv"
"strings"
"gitea.dev/modules/git/gitcmd"
)
func repoCodeGitRepoRelativePath(ownerName, repoName string) string {
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".git"
}
func repoWikiGitRepoRelativePath(ownerName, repoName string) string {
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".wiki.git"
}
// CodeRepoByName returns an unmanaged repository facade for the code repository of the given owner and repository name.
// Usually it is used for migration fixes or repository adoption/creation/rename/transfer.
func CodeRepoByName(ownerName, repoName string) gitcmd.RepositoryFacade {
return gitcmd.RepositoryUnmanaged(repoCodeGitRepoRelativePath(ownerName, repoName))
}
func WikiRepoByName(ownerName, repoName string) gitcmd.RepositoryFacade {
return gitcmd.RepositoryUnmanaged(repoWikiGitRepoRelativePath(ownerName, repoName))
}
func repoCodeGitRepoManagedID(repoID int64) string {
return "repo-" + strconv.FormatInt(repoID, 10)
}
func (repo *Repository) CodeStorageRepo() gitcmd.RepositoryFacade {
id := repoCodeGitRepoManagedID(repo.ID)
repoPath := repoCodeGitRepoRelativePath(repo.OwnerName, repo.Name)
return gitcmd.RepositoryManaged(id, repoPath)
}
func (repo *Repository) GitRepoLocation() string {
// TODO: use CodeGitRepo instead of this one
return repoCodeGitRepoRelativePath(repo.OwnerName, repo.Name)
}
func (repo *Repository) GitRepoManagedID() string {
// TODO: use CodeGitRepo instead of this one
return repoCodeGitRepoManagedID(repo.ID)
}
func (repo *Repository) WikiStorageRepo() gitcmd.RepositoryFacade {
// The wiki repository should have the same object format as the code repository. TODO: old comment, REALLY? Why?
id := "repo-wiki-" + strconv.FormatInt(repo.ID, 10)
repoPath := repoWikiGitRepoRelativePath(repo.OwnerName, repo.Name)
return gitcmd.RepositoryManaged(id, repoPath)
}
+27
View File
@@ -0,0 +1,27 @@
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo_test
import (
"testing"
repo_model "gitea.dev/models/repo"
"gitea.dev/models/unittest"
"github.com/stretchr/testify/assert"
)
func TestRepository_GitRepo(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, "user2/repo1.git", repo_model.CodeRepoByName(repo.OwnerName, repo.Name).GitRepoLocation())
assert.Equal(t, "user2/repo1.git", repo.CodeStorageRepo().GitRepoLocation())
assert.Equal(t, "repo-1", repo.CodeStorageRepo().GitRepoManagedID())
assert.Equal(t, "user2/repo1.wiki.git", repo_model.WikiRepoByName(repo.OwnerName, repo.Name).GitRepoLocation())
assert.Equal(t, "user2/repo1.wiki.git", repo.WikiStorageRepo().GitRepoLocation())
assert.Equal(t, "repo-wiki-1", repo.WikiStorageRepo().GitRepoManagedID())
}
-11
View File
@@ -7,7 +7,6 @@ package repo
import (
"context"
"fmt"
"strings"
user_model "gitea.dev/models/user"
"gitea.dev/modules/util"
@@ -74,13 +73,3 @@ func (err ErrWikiInvalidFileName) Unwrap() error {
func (repo *Repository) WikiCloneLink(ctx context.Context, doer *user_model.User) *CloneLink {
return repo.cloneLink(ctx, doer, repo.Name+".wiki")
}
func RelativeWikiPath(ownerName, repoName string) string {
return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".wiki.git"
}
// WikiStorageRepo returns the storage repo for the wiki like "owner-name/repo-name.wiki.git"
// The wiki repository should have the same object format as the code repository. TODO: REALLY? Why?
func (repo *Repository) WikiStorageRepo() StorageRepo {
return StorageRepo(RelativeWikiPath(repo.OwnerName, repo.Name))
}
-8
View File
@@ -20,11 +20,3 @@ func TestRepository_WikiCloneLink(t *testing.T) {
assert.Equal(t, "ssh://sshuser@try.gitea.io:3000/user2/repo1.wiki.git", cloneLink.SSH)
assert.Equal(t, "https://try.gitea.io/user2/repo1.wiki.git", cloneLink.HTTPS)
}
func TestRepository_RelativeWikiPath(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
assert.Equal(t, "user2/repo1.wiki.git", repo_model.RelativeWikiPath(repo.OwnerName, repo.Name))
assert.Equal(t, "user2/repo1.wiki.git", repo.WikiStorageRepo().GitRepoLocation())
}