1
0
mirror of https://github.com/go-gitea/gitea.git synced 2024-06-29 01:45:30 +00:00
This commit is contained in:
József Fényes 2024-06-17 19:50:52 +03:00 committed by GitHub
commit 60a1111b2f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 106 additions and 23 deletions

View File

@ -421,6 +421,19 @@ USER = root
;;
;SLOW_QUERY_THRESHOLD = 5s
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[git.hooks]
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Git prereceive hook name
;GIT_HOOK_PRERECEIVE_NAME = pre-receive
;; Git update hook name
;GIT_HOOK_UPDATE_NAME = update
;; Git post receive hook name
;GIT_HOOK_POSTRECEIVE_NAME = post-receive
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
[security]

View File

@ -1125,6 +1125,12 @@ This section only does "set" config, a removed config key from this section won'
- `core.logAllRefUpdates`: **true**
- `gc.reflogExpire`: **90**
### Git - Hook options (`git.hooks`)
- `GIT_HOOK_PRERECEIVE_NAME`: **pre-receive**: Specifies the name of the pre-receive hook. Wont accept paths as value, will only accept filenames.
- `GIT_HOOK_UPDATE_NAME`: **update**: Specifies the name of the update hook. Wont accept paths as value, will only accept filenames.
- `GIT_HOOK_POSTRECEIVE_NAME`: **post-receive**: Specifies the name of the post-receive hook. Wont accept paths as value, will only accept filenames.
## Metrics (`metrics`)
- `ENABLED`: **false**: Enables /metrics endpoint for prometheus.

View File

@ -25,8 +25,7 @@ import (
const RequiredVersion = "2.0.0" // the minimum Git version required
type Features struct {
gitVersion *version.Version
gitVersion *version.Version
UsingGogit bool
SupportProcReceive bool // >= 2.29
SupportHashSha256 bool // >= 2.42, SHA-256 repositories no longer an experimental curiosity

View File

@ -12,27 +12,19 @@ import (
"strings"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)
// hookNames is a list of Git server hooks' name that are supported.
var hookNames = []string{
"pre-receive",
"update",
"post-receive",
}
var hookNames = []string{"pre-receive", "update", "post-receive"}
// ErrNotValidHook error when a git hook is not valid
var ErrNotValidHook = errors.New("not a valid Git hook")
// IsValidHookName returns true if given name is a valid Git hook.
func IsValidHookName(name string) bool {
for _, hn := range hookNames {
if hn == name {
return true
}
// hookNames contains the hook name (key) linked with the filename of the resulting hook.
func GetHookNames() map[string]string {
return map[string]string{
"pre-receive": setting.GitHookPrereceiveName,
"update": setting.GitHookUpdateName,
"post-receive": setting.GitHookPostreceiveName,
}
return false
}
// Hook represents a Git hook.
@ -44,6 +36,19 @@ type Hook struct {
path string // Hook file path.
}
// ErrNotValidHook error when a git hook is not valid
var ErrNotValidHook = errors.New("not a valid Git hook")
// IsValidHookName returns true if given name is a valid Git hook.
func IsValidHookName(name string) bool {
for hn := range GetHookNames() {
if hn == name {
return true
}
}
return false
}
// GetHook returns a Git hook by given name and repository.
func GetHook(repoPath, name string) (*Hook, error) {
if !IsValidHookName(name) {
@ -51,7 +56,7 @@ func GetHook(repoPath, name string) (*Hook, error) {
}
h := &Hook{
name: name,
path: path.Join(repoPath, "hooks", name+".d", name),
path: filepath.Join(repoPath, "hooks", name+".d", GetHookNames()[name]),
}
samplePath := filepath.Join(repoPath, "hooks", name+".sample")
if isFile(h.path) {
@ -107,9 +112,9 @@ func ListHooks(repoPath string) (_ []*Hook, err error) {
return nil, errors.New("hooks path does not exist")
}
hooks := make([]*Hook, len(hookNames))
for i, name := range hookNames {
hooks[i], err = GetHook(repoPath, name)
hooks := make([]*Hook, len(GetHookNames()))
for i := range hookNames {
hooks[i], err = GetHook(repoPath, hookNames[i])
if err != nil {
return nil, err
}

42
modules/setting/hooks.go Normal file
View File

@ -0,0 +1,42 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package setting
import (
"fmt"
"path/filepath"
"code.gitea.io/gitea/modules/log"
)
var (
// Git hook settings
GitHookPrereceiveName string
GitHookPostreceiveName string
GitHookUpdateName string
)
func isValidFileName(filename string) error {
if filepath.Base(filename) != filename || filepath.IsAbs(filename) || filename == "." || filename == ".." {
return fmt.Errorf("can only contain filenames, not other directories")
}
return nil
}
func loadHooksFrom(rootCfg ConfigProvider) {
githooks := rootCfg.Section("git.hooks")
GitHookPrereceiveName = githooks.Key("GIT_HOOK_PRERECEIVE_NAME").MustString("pre-receive")
GitHookUpdateName = githooks.Key("GIT_HOOK_UPDATE_NAME").MustString("update")
GitHookPostreceiveName = githooks.Key("GIT_HOOK_POSTRECEIVE_NAME").MustString("post-receive")
if err := isValidFileName(GitHookPrereceiveName); err != nil {
log.Fatal("'%s' is an invalid [git.hooks].GIT_HOOK_PRERECEIVE_NAME: %v", GitHookPrereceiveName, err)
}
if err := isValidFileName(GitHookUpdateName); err != nil {
log.Fatal("'%s' is an invalid [git.hooks].GIT_HOOK_UPDATE_NAME: %v", GitHookUpdateName, err)
}
if err := isValidFileName(GitHookPostreceiveName); err != nil {
log.Fatal("'%s' is an invalid [git.hooks].GIT_HOOK_POSTRECEIVE_NAME: %v", GitHookPostreceiveName, err)
}
}

View File

@ -118,6 +118,7 @@ func loadCommonSettingsFrom(cfg ConfigProvider) error {
loadOAuth2From(cfg)
loadSecurityFrom(cfg)
loadHooksFrom(cfg)
if err := loadAttachmentFrom(cfg); err != nil {
return err
}

View File

@ -3138,6 +3138,9 @@ config.disable_router_log = Disable Router Log
config.run_user = Run As Username
config.run_mode = Run Mode
config.git_version = Git Version
config.git_hookprereceivename = Git Prereceive Hook Name
config.git_hookupdatename = Git Update Hook Name
config.git_postreceivename = Git Postreceive Hook Name
config.app_data_path = App Data Path
config.repo_root_path = Repository Root Path
config.lfs_root_path = LFS Root Path

View File

@ -113,7 +113,9 @@ func Config(ctx *context.Context) {
ctx.Data["RunUser"] = setting.RunUser
ctx.Data["RunMode"] = util.ToTitleCase(setting.RunMode)
ctx.Data["GitVersion"] = git.DefaultFeatures().VersionInfo()
ctx.Data["GitHookPrereceiveName"] = setting.GitHookPrereceiveName
ctx.Data["GitHookUpdateName"] = setting.GitHookUpdateName
ctx.Data["GitHookPostreceiveName"] = setting.GitHookPostreceiveName
ctx.Data["AppDataPath"] = setting.AppDataPath
ctx.Data["RepoRootPath"] = setting.RepoRootPath
ctx.Data["CustomRootPath"] = setting.CustomPath

View File

@ -311,6 +311,18 @@
<dd>{{.Git.Timeout.Pull}} {{ctx.Locale.Tr "tool.raw_seconds"}}</dd>
<dt>{{ctx.Locale.Tr "admin.config.git_gc_timeout"}}</dt>
<dd>{{.Git.Timeout.GC}} {{ctx.Locale.Tr "tool.raw_seconds"}}</dd>
<div class="divider"></div>
<dt>{{ctx.Locale.Tr "admin.config.git_hookprereceivename"}}</dt>
<dd>{{.GitHookPrereceiveName}}</dd>
<dt>{{ctx.Locale.Tr "admin.config.git_hookupdatename"}}</dt>
<dd>{{.GitHookUpdateName}}</dd>
<dt>{{ctx.Locale.Tr "admin.config.git_postreceivename"}}</dt>
<dd>{{.GitHookPostreceiveName}}</dd>
</dl>
</div>