diff options
author | zeripath <art27@cantab.net> | 2021-09-16 14:34:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-16 15:34:54 +0200 |
commit | 8de44d1995e73ec139ede5626466af1fce8c571a (patch) | |
tree | 1d47dbb69c70e2716785e804c39fcf59ba701559 /routers/private/internal_repo.go | |
parent | a959ed99c2966dfc10ac41710eb74b7baf490bcf (diff) | |
download | gitea-8de44d1995e73ec139ede5626466af1fce8c571a.tar.gz gitea-8de44d1995e73ec139ede5626466af1fce8c571a.zip |
Clean-up HookPreReceive and restore functionality for pushing non-standard refs (#16705)
* Clean-up HookPreReceive and restore functionality for pushing non-standard refs
There was an inadvertent breaking change in #15629 meaning that notes refs and other
git extension refs will be automatically rejected.
Further following #14295 and #15629 the pre-recieve hook code is untenably long and
too complex.
This PR refactors the hook code and removes the incorrect forced rejection of
non-standard refs.
Fix #16688
Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'routers/private/internal_repo.go')
-rw-r--r-- | routers/private/internal_repo.go | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/routers/private/internal_repo.go b/routers/private/internal_repo.go new file mode 100644 index 0000000000..60daa1dbea --- /dev/null +++ b/routers/private/internal_repo.go @@ -0,0 +1,84 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +// Package private includes all internal routes. The package name internal is ideal but Golang is not allowed, so we use private as package name instead. +package private + +import ( + "context" + "fmt" + "net/http" + + "code.gitea.io/gitea/models" + gitea_context "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" +) + +// __________ +// \______ \ ____ ______ ____ +// | _// __ \\____ \ / _ \ +// | | \ ___/| |_> > <_> ) +// |____|_ /\___ > __/ \____/ +// \/ \/|__| +// _____ .__ __ +// / _ \ ______ _____|__| ____ ____ _____ ____ _____/ |_ +// / /_\ \ / ___// ___/ |/ ___\ / \ / \_/ __ \ / \ __\ +// / | \\___ \ \___ \| / /_/ > | \ Y Y \ ___/| | \ | +// \____|__ /____ >____ >__\___ /|___| /__|_| /\___ >___| /__| +// \/ \/ \/ /_____/ \/ \/ \/ \/ + +// This file contains common functions relating to setting the Repository for the +// internal routes + +// RepoAssignment assigns the repository and gitrepository to the private context +func RepoAssignment(ctx *gitea_context.PrivateContext) context.CancelFunc { + ownerName := ctx.Params(":owner") + repoName := ctx.Params(":repo") + + repo := loadRepository(ctx, ownerName, repoName) + if ctx.Written() { + // Error handled in loadRepository + return nil + } + + gitRepo, err := git.OpenRepository(repo.RepoPath()) + if err != nil { + log.Error("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err) + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "Err": fmt.Sprintf("Failed to open repository: %s/%s Error: %v", ownerName, repoName, err), + }) + return nil + } + + ctx.Repo = &gitea_context.Repository{ + Repository: repo, + GitRepo: gitRepo, + } + + // We opened it, we should close it + cancel := func() { + // If it's been set to nil then assume someone else has closed it. + if ctx.Repo.GitRepo != nil { + ctx.Repo.GitRepo.Close() + } + } + + return cancel +} + +func loadRepository(ctx *gitea_context.PrivateContext, ownerName, repoName string) *models.Repository { + repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName) + if err != nil { + log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err) + ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ + "Err": fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err), + }) + return nil + } + if repo.OwnerName == "" { + repo.OwnerName = ownerName + } + return repo +} |