diff --git a/models/repo/repo.go b/models/repo/repo.go index a8732f60bfff8..6880dedfe5873 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -219,20 +219,38 @@ func RelativePath(ownerName, repoName string) string { return strings.ToLower(ownerName) + "/" + strings.ToLower(repoName) + ".git" } -// RelativePath should be an unix style path like username/reponame.git -func (repo *Repository) RelativePath() string { +// GetRelativePath should be an unix style path like username/reponame.git +func (repo *Repository) GetRelativePath() string { return RelativePath(repo.OwnerName, repo.Name) } -type StorageRepo string +// GetObjectFormatName returns the object format name of the repository +func (repo *Repository) GetObjectFormatName() string { + return repo.ObjectFormatName +} + +type StorageRepo struct { + RelativePath string + ObjectFormatName string +} -// RelativePath should be an unix style path like username/reponame.git -func (sr StorageRepo) RelativePath() string { - return string(sr) +// GetRelativePath should be an unix style path like username/reponame.git +func (sr StorageRepo) GetRelativePath() string { + return sr.RelativePath } +// GetObjectFormatName returns the object format name of the repository +func (sr StorageRepo) GetObjectFormatName() string { + return sr.ObjectFormatName +} + +// WikiStorageRepo returns the storage repo for the wiki +// The wiki repository should have the same object format as the code repository func (repo *Repository) WikiStorageRepo() StorageRepo { - return StorageRepo(strings.ToLower(repo.OwnerName) + "/" + strings.ToLower(repo.Name) + ".wiki.git") + return StorageRepo{ + RelativePath: strings.ToLower(repo.OwnerName) + "/" + strings.ToLower(repo.Name) + ".wiki.git", + ObjectFormatName: repo.ObjectFormatName, + } } // SanitizedOriginalURL returns a sanitized OriginalURL diff --git a/modules/git/fsck.go b/modules/git/fsck.go deleted file mode 100644 index a52684c84fffe..0000000000000 --- a/modules/git/fsck.go +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright 2024 The Gitea Authors. All rights reserved. -// SPDX-License-Identifier: MIT - -package git - -import ( - "context" - "time" -) - -// Fsck verifies the connectivity and validity of the objects in the database -func Fsck(ctx context.Context, repoPath string, timeout time.Duration, args TrustedCmdArgs) error { - return NewCommand("fsck").AddArguments(args...).Run(ctx, &RunOpts{Timeout: timeout, Dir: repoPath}) -} diff --git a/modules/gitrepo/fsck.go b/modules/gitrepo/fsck.go new file mode 100644 index 0000000000000..bcec6aa03f1ec --- /dev/null +++ b/modules/gitrepo/fsck.go @@ -0,0 +1,16 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package gitrepo + +import ( + "context" + "time" + + "code.gitea.io/gitea/modules/git" +) + +// Fsck verifies the connectivity and validity of the objects in the database +func Fsck(ctx context.Context, repo Repository, timeout time.Duration, args git.TrustedCmdArgs) error { + return git.NewCommand("fsck").AddArguments(args...).Run(ctx, &git.RunOpts{Timeout: timeout, Dir: repoPath(repo)}) +} diff --git a/modules/gitrepo/gitrepo.go b/modules/gitrepo/gitrepo.go index 5da65e2452704..55fd667610a94 100644 --- a/modules/gitrepo/gitrepo.go +++ b/modules/gitrepo/gitrepo.go @@ -17,13 +17,14 @@ import ( // Repository represents a git repository which stored in a disk type Repository interface { - RelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path + GetRelativePath() string // We don't assume how the directory structure of the repository is, so we only need the relative path + GetObjectFormatName() string } // 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, filepath.FromSlash(repo.RelativePath())) + return filepath.Join(setting.RepoRootPath, filepath.FromSlash(repo.GetRelativePath())) } // OpenRepository opens the repository at the given relative path with the provided context. @@ -81,3 +82,7 @@ func RenameRepository(ctx context.Context, repo, newRepo Repository) error { } return nil } + +func InitRepository(ctx context.Context, repo Repository) error { + return git.InitRepository(ctx, repoPath(repo), true, repo.GetObjectFormatName()) +} diff --git a/modules/repository/init.go b/modules/repository/init.go index ace21254ba0c4..1070a9250f373 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -12,7 +12,6 @@ import ( issues_model "code.gitea.io/gitea/models/issues" repo_model "code.gitea.io/gitea/models/repo" - "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/label" "code.gitea.io/gitea/modules/log" @@ -136,7 +135,7 @@ func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err } // Init git bare new repository. - if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil { + if err = gitrepo.InitRepository(ctx, repo); err != nil { return fmt.Errorf("git.InitRepository: %w", err) } else if err = gitrepo.CreateDelegateHooks(ctx, repo); err != nil { return fmt.Errorf("createDelegateHooks: %w", err) diff --git a/services/migrations/gitea_uploader_test.go b/services/migrations/gitea_uploader_test.go index f52d4157c8136..d308e622530f1 100644 --- a/services/migrations/gitea_uploader_test.go +++ b/services/migrations/gitea_uploader_test.go @@ -236,6 +236,7 @@ func TestGiteaUploadUpdateGitForPullRequest(t *testing.T) { // fromRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) baseRef := "master" + // this is very different from the real situation. It should be a bare repository for all the Gitea managed repositories assert.NoError(t, git.InitRepository(git.DefaultContext, fromRepo.RepoPath(), false, fromRepo.ObjectFormatName)) err := git.NewCommand("symbolic-ref").AddDynamicArguments("HEAD", git.BranchPrefix+baseRef).Run(git.DefaultContext, &git.RunOpts{Dir: fromRepo.RepoPath()}) assert.NoError(t, err) diff --git a/services/repository/check.go b/services/repository/check.go index b475fbc487d29..46fb8ba616df6 100644 --- a/services/repository/check.go +++ b/services/repository/check.go @@ -14,6 +14,7 @@ import ( system_model "code.gitea.io/gitea/models/system" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" repo_module "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/util" @@ -47,10 +48,9 @@ func GitFsckRepos(ctx context.Context, timeout time.Duration, args git.TrustedCm // GitFsckRepo calls 'git fsck' to check an individual repository's health. func GitFsckRepo(ctx context.Context, repo *repo_model.Repository, timeout time.Duration, args git.TrustedCmdArgs) error { - log.Trace("Running health check on repository %-v", repo) - repoPath := repo.RepoPath() - if err := git.Fsck(ctx, repoPath, timeout, args); err != nil { - log.Warn("Failed to health check repository (%-v): %v", repo, err) + log.Trace("Running health check on repository %-v", repo.FullName()) + if err := gitrepo.Fsck(ctx, repo, timeout, args); err != nil { + log.Warn("Failed to health check repository (%-v): %v", repo.FullName(), err) if err = system_model.CreateRepositoryNotice("Failed to health check repository (%s): %v", repo.FullName(), err); err != nil { log.Error("CreateRepositoryNotice: %v", err) } @@ -190,7 +190,7 @@ func ReinitMissingRepositories(ctx context.Context) error { default: } log.Trace("Initializing %d/%d...", repo.OwnerID, repo.ID) - if err := git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil { + if err := gitrepo.InitRepository(ctx, repo); err != nil { log.Error("Unable (re)initialize repository %d at %s. Error: %v", repo.ID, repo.RepoPath(), err) if err2 := system_model.CreateRepositoryNotice("InitRepository [%d]: %v", repo.ID, err); err2 != nil { log.Error("CreateRepositoryNotice: %v", err2) diff --git a/services/repository/transfer.go b/services/repository/transfer.go index a589bc469d87f..6dd8f1d1f8807 100644 --- a/services/repository/transfer.go +++ b/services/repository/transfer.go @@ -337,7 +337,10 @@ 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 { + repo_model.StorageRepo{ + RelativePath: repo_model.RelativePath(repo.OwnerName, newRepoName), + ObjectFormatName: repo.ObjectFormatName, + }); err != nil { return fmt.Errorf("rename repository directory: %w", err) } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index b21f46639dc99..556e3b72588c6 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -39,7 +39,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { return nil } - if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil { + if err := gitrepo.InitRepository(ctx, repo.WikiStorageRepo()); err != nil { return fmt.Errorf("InitRepository: %w", err) } else if err = gitrepo.CreateDelegateHooks(ctx, repo.WikiStorageRepo()); err != nil { return fmt.Errorf("createDelegateHooks: %w", err)