mirror of
https://github.com/go-gitea/gitea.git
synced 2025-04-22 01:18:45 +03:00
Move hooks function to gitrepo and reduce expose repopath (#33890)
Extract from #28966 Follow #33874
This commit is contained in:
@ -1,9 +1,10 @@
|
|||||||
// Copyright 2020 The Gitea Authors. All rights reserved.
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
package repository
|
package gitrepo
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -105,10 +106,18 @@ done
|
|||||||
return hookNames, hookTpls, giteaHookTpls
|
return hookNames, hookTpls, giteaHookTpls
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateDelegateHooks creates all the hooks scripts for the repo
|
// CreateDelegateHooksForRepo creates all the hooks scripts for the repo
|
||||||
func CreateDelegateHooks(repoPath string) (err error) {
|
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()
|
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
|
||||||
hookDir := filepath.Join(repoPath, "hooks")
|
|
||||||
|
|
||||||
for i, hookName := range hookNames {
|
for i, hookName := range hookNames {
|
||||||
oldHookPath := filepath.Join(hookDir, hookName)
|
oldHookPath := filepath.Join(hookDir, hookName)
|
||||||
@ -169,11 +178,19 @@ func ensureExecutable(filename string) error {
|
|||||||
return os.Chmod(filename, mode)
|
return os.Chmod(filename, mode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CheckDelegateHooks checks the hooks scripts for the repo
|
// CheckDelegateHooksForRepo checks the hooks scripts for the repo
|
||||||
func CheckDelegateHooks(repoPath string) ([]string, error) {
|
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()
|
hookNames, hookTpls, giteaHookTpls := getHookTemplates()
|
||||||
|
|
||||||
hookDir := filepath.Join(repoPath, "hooks")
|
|
||||||
results := make([]string, 0, 10)
|
results := make([]string, 0, 10)
|
||||||
|
|
||||||
for i, hookName := range hookNames {
|
for i, hookName := range hookNames {
|
@ -138,7 +138,7 @@ func CheckInitRepository(ctx context.Context, repo *repo_model.Repository) (err
|
|||||||
// Init git bare new repository.
|
// Init git bare new repository.
|
||||||
if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
|
if err = git.InitRepository(ctx, repo.RepoPath(), true, repo.ObjectFormatName); err != nil {
|
||||||
return fmt.Errorf("git.InitRepository: %w", err)
|
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 fmt.Errorf("createDelegateHooks: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -18,7 +18,6 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
"code.gitea.io/gitea/modules/gitrepo"
|
"code.gitea.io/gitea/modules/gitrepo"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/repository"
|
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/structs"
|
"code.gitea.io/gitea/modules/structs"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"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 {
|
func checkHooks(ctx context.Context, logger log.Logger, autofix bool) error {
|
||||||
if err := iterateRepositories(ctx, func(repo *repo_model.Repository) 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 {
|
if err != nil {
|
||||||
logger.Critical("Unable to check delegate hooks for repo %-v. ERROR: %v", repo, err)
|
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)
|
return fmt.Errorf("Unable to check delegate hooks for repo %-v. ERROR: %w", repo, err)
|
||||||
}
|
}
|
||||||
if len(results) > 0 && autofix {
|
if len(results) > 0 && autofix {
|
||||||
logger.Warn("Regenerated hooks for %s", repo.FullName())
|
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)
|
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)
|
return fmt.Errorf("Unable to recreate delegate hooks for %-v. ERROR: %w", repo, err)
|
||||||
}
|
}
|
||||||
|
@ -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())
|
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)
|
return fmt.Errorf("createDelegateHooks: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,8 +154,7 @@ func ForkRepository(ctx context.Context, doer, owner *user_model.User, opts Fork
|
|||||||
if opts.SingleBranch != "" {
|
if opts.SingleBranch != "" {
|
||||||
cloneCmd.AddArguments("--single-branch", "--branch").AddDynamicArguments(opts.SingleBranch)
|
cloneCmd.AddArguments("--single-branch", "--branch").AddDynamicArguments(opts.SingleBranch)
|
||||||
}
|
}
|
||||||
repoPath := repo_model.RepoPath(owner.Name, repo.Name)
|
if stdout, _, err := cloneCmd.AddDynamicArguments(oldRepoPath, repo.RepoPath()).
|
||||||
if stdout, _, err := cloneCmd.AddDynamicArguments(oldRepoPath, repoPath).
|
|
||||||
RunStdBytes(txCtx, &git.RunOpts{Timeout: 10 * time.Minute}); err != nil {
|
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)
|
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)
|
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").
|
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)
|
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)
|
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)
|
return fmt.Errorf("createDelegateHooks: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,7 +12,6 @@ import (
|
|||||||
"code.gitea.io/gitea/models/webhook"
|
"code.gitea.io/gitea/models/webhook"
|
||||||
"code.gitea.io/gitea/modules/gitrepo"
|
"code.gitea.io/gitea/modules/gitrepo"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
repo_module "code.gitea.io/gitea/modules/repository"
|
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
@ -32,11 +31,11 @@ func SyncRepositoryHooks(ctx context.Context) error {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := repo_module.CreateDelegateHooks(repo.RepoPath()); err != nil {
|
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
|
||||||
return fmt.Errorf("SyncRepositoryHook: %w", err)
|
return fmt.Errorf("SyncRepositoryHook: %w", err)
|
||||||
}
|
}
|
||||||
if repo.HasWiki() {
|
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)
|
return fmt.Errorf("SyncRepositoryHook: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
repo_model "code.gitea.io/gitea/models/repo"
|
repo_model "code.gitea.io/gitea/models/repo"
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/git"
|
"code.gitea.io/gitea/modules/git"
|
||||||
|
"code.gitea.io/gitea/modules/gitrepo"
|
||||||
"code.gitea.io/gitea/modules/lfs"
|
"code.gitea.io/gitea/modules/lfs"
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
"code.gitea.io/gitea/modules/migration"
|
"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.
|
// 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) {
|
func CleanUpMigrateInfo(ctx context.Context, repo *repo_model.Repository) (*repo_model.Repository, error) {
|
||||||
repoPath := repo.RepoPath()
|
if err := gitrepo.CreateDelegateHooksForRepo(ctx, repo); err != nil {
|
||||||
if err := repo_module.CreateDelegateHooks(repoPath); err != nil {
|
|
||||||
return repo, fmt.Errorf("createDelegateHooks: %w", err)
|
return repo, fmt.Errorf("createDelegateHooks: %w", err)
|
||||||
}
|
}
|
||||||
if repo.HasWiki() {
|
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)
|
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) {
|
if err != nil && !git.IsRemoteNotExistError(err) {
|
||||||
return repo, fmt.Errorf("CleanUpMigrateInfo: %w", err)
|
return repo, fmt.Errorf("CleanUpMigrateInfo: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
if err := git.InitRepository(ctx, repo.WikiPath(), true, repo.ObjectFormatName); err != nil {
|
||||||
return fmt.Errorf("InitRepository: %w", err)
|
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)
|
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 {
|
} 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)
|
return fmt.Errorf("unable to set default wiki branch to %q: %w", repo.DefaultWikiBranch, err)
|
||||||
|
Reference in New Issue
Block a user