From 7fd44a85ca8e2614df1fcacc5409322a6b83fe62 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Sun, 16 Mar 2025 09:12:55 -0700 Subject: [PATCH] Move hooks function to gitrepo and reduce expose repopath (#33890) Extract from #28966 Follow #33874 --- modules/{repository => gitrepo}/hooks.go | 31 ++++++++++++++++++------ modules/repository/init.go | 2 +- services/doctor/misc.go | 5 ++-- services/repository/adopt.go | 2 +- services/repository/fork.go | 7 +++--- services/repository/hooks.go | 5 ++-- services/repository/migrate.go | 8 +++--- services/wiki/wiki.go | 2 +- 8 files changed, 38 insertions(+), 24 deletions(-) rename modules/{repository => gitrepo}/hooks.go (86%) diff --git a/modules/repository/hooks.go b/modules/gitrepo/hooks.go similarity index 86% rename from modules/repository/hooks.go rename to modules/gitrepo/hooks.go index 95849789ab..cf44f792c6 100644 --- a/modules/repository/hooks.go +++ b/modules/gitrepo/hooks.go @@ -1,9 +1,10 @@ // Copyright 2020 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package repository +package gitrepo import ( + "context" "fmt" "os" "path/filepath" @@ -105,10 +106,18 @@ done return hookNames, hookTpls, giteaHookTpls } -// CreateDelegateHooks creates all the hooks scripts for the repo -func CreateDelegateHooks(repoPath string) (err error) { +// CreateDelegateHooksForRepo creates all the hooks scripts for the repo +func CreateDelegateHooksForRepo(_ context.Context, repo Repository) (err error) { + return createDelegateHooks(filepath.Join(repoPath(repo), "hooks")) +} + +// CreateDelegateHooksForWiki creates all the hooks scripts for the wiki repo +func CreateDelegateHooksForWiki(_ context.Context, repo Repository) (err error) { + return createDelegateHooks(filepath.Join(wikiPath(repo), "hooks")) +} + +func createDelegateHooks(hookDir string) (err error) { hookNames, hookTpls, giteaHookTpls := getHookTemplates() - hookDir := filepath.Join(repoPath, "hooks") for i, hookName := range hookNames { oldHookPath := filepath.Join(hookDir, hookName) @@ -169,11 +178,19 @@ func ensureExecutable(filename string) error { return os.Chmod(filename, mode) } -// CheckDelegateHooks checks the hooks scripts for the repo -func CheckDelegateHooks(repoPath string) ([]string, error) { +// CheckDelegateHooksForRepo checks the hooks scripts for the repo +func CheckDelegateHooksForRepo(_ context.Context, repo Repository) ([]string, error) { + return checkDelegateHooks(filepath.Join(repoPath(repo), "hooks")) +} + +// CheckDelegateHooksForWiki checks the hooks scripts for the repo +func CheckDelegateHooksForWiki(_ context.Context, repo Repository) ([]string, error) { + return checkDelegateHooks(filepath.Join(wikiPath(repo), "hooks")) +} + +func checkDelegateHooks(hookDir string) ([]string, error) { hookNames, hookTpls, giteaHookTpls := getHookTemplates() - hookDir := filepath.Join(repoPath, "hooks") results := make([]string, 0, 10) for i, hookName := range hookNames { diff --git a/modules/repository/init.go b/modules/repository/init.go index 772ae3efe3..3fc1261baa 100644 --- a/modules/repository/init.go +++ b/modules/repository/init.go @@ -138,7 +138,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 { return fmt.Errorf("git.InitRepository: %w", err) - } else if err = CreateDelegateHooks(repo.RepoPath()); err != nil { + } else if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil { return fmt.Errorf("createDelegateHooks: %w", err) } return nil diff --git a/services/doctor/misc.go b/services/doctor/misc.go index f36a9ab478..260a28ec4c 100644 --- a/services/doctor/misc.go +++ b/services/doctor/misc.go @@ -18,7 +18,6 @@ import ( "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" - "code.gitea.io/gitea/modules/repository" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/util" @@ -50,14 +49,14 @@ func checkScriptType(ctx context.Context, logger log.Logger, autofix bool) error func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error { if err := iterateRepositories(ctx, func(repo *repo_model.Repository) error { - results, err := repository.CheckDelegateHooks(repo.RepoPath()) + results, err := gitrepo.CheckDelegateHooksForRepo(ctx, repo) if err != nil { logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err) return fmt.Errorf("Unable to check delegate hooks for repo %-v. ERROR: %w", repo, err) } if len(results) > 0 && autofix { logger.Warn("Regenerated hooks for %s", repo.FullName()) - if err := repository.CreateDelegateHooks(repo.RepoPath()); err != nil { + if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil { logger.Critical("Unable to recreate delegate hooks for %-v. ERROR: %v", repo, err) return fmt.Errorf("Unable to recreate delegate hooks for %-v. ERROR: %w", repo, err) } diff --git a/services/repository/adopt.go b/services/repository/adopt.go index 729441c9f1..ea4f9a1920 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -115,7 +115,7 @@ func adoptRepository(ctx context.Context, repo *repo_model.Repository, defaultBr return fmt.Errorf("adoptRepository: path does not already exist: %s", repo.FullName()) } - if err := repo_module.CreateDelegateHooks(repo.RepoPath()); err != nil { + if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil { return fmt.Errorf("createDelegateHooks: %w", err) } diff --git a/services/repository/fork.go b/services/repository/fork.go index ec6ba56ddf..7f7364acfc 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -154,8 +154,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork if opts.SingleBranch != "" { cloneCmd.AddArguments("--single-branch", "--branch").AddDynamicArguments(opts.SingleBranch) } - repoPath := repo_model.RepoPath(owner.Name, repo.Name) - if stdout, _, err := cloneCmd.AddDynamicArguments(oldRepoPath, repoPath). + if stdout, _, err := cloneCmd.AddDynamicArguments(oldRepoPath, repo.RepoPath()). RunStdBytes(txCtx, &git.RunOpts{Timeout: 10 * time.Minute}); err != nil { log.Error("Fork Repository (git clone) Failed for %v (from %v):\nStdout: %s\nError: %v", repo, opts.BaseRepo, stdout, err) return fmt.Errorf("git clone: %w", err) @@ -166,12 +165,12 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork } if stdout, _, err := git.NewCommand("update-server-info"). - RunStdString(txCtx, &git.RunOpts{Dir: repoPath}); err != nil { + RunStdString(txCtx, &git.RunOpts{Dir: repo.RepoPath()}); err != nil { log.Error("Fork Repository (git update-server-info) failed for %v:\nStdout: %s\nError: %v", repo, stdout, err) return fmt.Errorf("git update-server-info: %w", err) } - if err = repo_module.CreateDelegateHooks(repoPath); err != nil { + if err = gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil { return fmt.Errorf("createDelegateHooks: %w", err) } diff --git a/services/repository/hooks.go b/services/repository/hooks.go index 97e9e290a3..2b3eb79153 100644 --- a/services/repository/hooks.go +++ b/services/repository/hooks.go @@ -12,7 +12,6 @@ import ( "code.gitea.io/gitea/models/webhook" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" - repo_module "code.gitea.io/gitea/modules/repository" "xorm.io/builder" ) @@ -32,11 +31,11 @@ func SyncRepositoryHooks(ctx context.Context) error { default: } - if err := repo_module.CreateDelegateHooks(repo.RepoPath()); err != nil { + if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil { return fmt.Errorf("SyncRepositoryHook: %w", err) } if repo.HasWiki() { - if err := repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil { + if err := gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil { return fmt.Errorf("SyncRepositoryHook: %w", err) } } diff --git a/services/repository/migrate.go b/services/repository/migrate.go index 50b434f003..1969b16a2d 100644 --- a/services/repository/migrate.go +++ b/services/repository/migrate.go @@ -15,6 +15,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" 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/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/migration" @@ -264,17 +265,16 @@ func cleanUpMigrateGitConfig(ctx context.Context, repoPath string) error { // CleanUpMigrateInfo finishes migrating repository and/or wiki with things that don't need to be done for mirrors. func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) { - repoPath := repo.RepoPath() - if err := repo_module.CreateDelegateHooks(repoPath); err != nil { + if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil { return repo, fmt.Errorf("createDelegateHooks: %w", err) } if repo.HasWiki() { - if err := repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil { + if err := gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil { return repo, fmt.Errorf("createDelegateHooks.(wiki): %w", err) } } - _, _, err := git.NewCommand("remote", "rm", "origin").RunStdString(ctx, &git.RunOpts{Dir: repoPath}) + _, _, err := git.NewCommand("remote", "rm", "origin").RunStdString(ctx, &git.RunOpts{Dir: repo.RepoPath()}) if err != nil && !git.IsRemoteNotExistError(err) { return repo, fmt.Errorf("CleanUpMigrateInfo: %w", err) } diff --git a/services/wiki/wiki.go b/services/wiki/wiki.go index 413d416b8d..a3fe07927d 100644 --- a/services/wiki/wiki.go +++ b/services/wiki/wiki.go @@ -41,7 +41,7 @@ func InitWiki(ctx context.Context, repo *repo_model.Repository) error { if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil { return fmt.Errorf("InitRepository: %w", err) - } else if err = repo_module.CreateDelegateHooks(repo.WikiPath()); err != nil { + } else if err = gitrepo.CreateDelegateHooksForWiki(ctx, repo); err != nil { return fmt.Errorf("createDelegateHooks: %w", err) } else if _, _, err = git.NewCommand("symbolic-ref", "HEAD").AddDynamicArguments(git.BranchPrefix+repo.DefaultWikiBranch).RunStdString(ctx, &git.RunOpts{Dir: repo.WikiPath()}); err != nil { return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err)