Skip to content

Commit 8de44d1

Browse files
authored
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>
1 parent a959ed9 commit 8de44d1

9 files changed

+1000
-780
lines changed

modules/web/route.go

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func Wrap(handlers ...interface{}) http.HandlerFunc {
3131
func(ctx *context.Context) goctx.CancelFunc,
3232
func(*context.APIContext),
3333
func(*context.PrivateContext),
34+
func(*context.PrivateContext) goctx.CancelFunc,
3435
func(http.Handler) http.Handler:
3536
default:
3637
panic(fmt.Sprintf("Unsupported handler type: %#v", t))
@@ -59,6 +60,15 @@ func Wrap(handlers ...interface{}) http.HandlerFunc {
5960
if ctx.Written() {
6061
return
6162
}
63+
case func(*context.PrivateContext) goctx.CancelFunc:
64+
ctx := context.GetPrivateContext(req)
65+
cancel := t(ctx)
66+
if cancel != nil {
67+
defer cancel()
68+
}
69+
if ctx.Written() {
70+
return
71+
}
6272
case func(ctx *context.Context):
6373
ctx := context.GetContext(req)
6474
t(ctx)

routers/private/default_branch.go

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2021 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
// 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.
6+
package private
7+
8+
import (
9+
"fmt"
10+
"net/http"
11+
12+
"code.gitea.io/gitea/models"
13+
gitea_context "code.gitea.io/gitea/modules/context"
14+
"code.gitea.io/gitea/modules/git"
15+
"code.gitea.io/gitea/modules/log"
16+
"code.gitea.io/gitea/modules/private"
17+
)
18+
19+
// ________ _____ .__ __
20+
// \______ \ _____/ ____\____ __ __| |_/ |_
21+
// | | \_/ __ \ __\\__ \ | | \ |\ __\
22+
// | ` \ ___/| | / __ \| | / |_| |
23+
// /_______ /\___ >__| (____ /____/|____/__|
24+
// \/ \/ \/
25+
// __________ .__
26+
// \______ \____________ ____ ____ | |__
27+
// | | _/\_ __ \__ \ / \_/ ___\| | \
28+
// | | \ | | \// __ \| | \ \___| Y \
29+
// |______ / |__| (____ /___| /\___ >___| /
30+
// \/ \/ \/ \/ \/
31+
32+
// SetDefaultBranch updates the default branch
33+
func SetDefaultBranch(ctx *gitea_context.PrivateContext) {
34+
ownerName := ctx.Params(":owner")
35+
repoName := ctx.Params(":repo")
36+
branch := ctx.Params(":branch")
37+
repo, err := models.GetRepositoryByOwnerAndName(ownerName, repoName)
38+
if err != nil {
39+
log.Error("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err)
40+
ctx.JSON(http.StatusInternalServerError, private.Response{
41+
Err: fmt.Sprintf("Failed to get repository: %s/%s Error: %v", ownerName, repoName, err),
42+
})
43+
return
44+
}
45+
if repo.OwnerName == "" {
46+
repo.OwnerName = ownerName
47+
}
48+
49+
repo.DefaultBranch = branch
50+
gitRepo, err := git.OpenRepository(repo.RepoPath())
51+
if err != nil {
52+
ctx.JSON(http.StatusInternalServerError, private.Response{
53+
Err: fmt.Sprintf("Failed to get git repository: %s/%s Error: %v", ownerName, repoName, err),
54+
})
55+
return
56+
}
57+
if err := gitRepo.SetDefaultBranch(repo.DefaultBranch); err != nil {
58+
if !git.IsErrUnsupportedVersion(err) {
59+
gitRepo.Close()
60+
ctx.JSON(http.StatusInternalServerError, private.Response{
61+
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
62+
})
63+
return
64+
}
65+
}
66+
gitRepo.Close()
67+
68+
if err := repo.UpdateDefaultBranch(); err != nil {
69+
ctx.JSON(http.StatusInternalServerError, private.Response{
70+
Err: fmt.Sprintf("Unable to set default branch on repository: %s/%s Error: %v", ownerName, repoName, err),
71+
})
72+
return
73+
}
74+
ctx.PlainText(http.StatusOK, []byte("success"))
75+
}

0 commit comments

Comments
 (0)