1
1
mirror of https://github.com/go-gitea/gitea synced 2024-11-17 23:54:25 +00:00
gitea/modules/git/hooks.go

125 lines
2.6 KiB
Go
Raw Normal View History

2014-10-06 21:50:00 +00:00
// Copyright 2014 The Gogs 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 git
import (
"errors"
"io/ioutil"
"os"
"path"
"strings"
2015-08-26 10:03:30 +00:00
"github.com/Unknwon/com"
2014-10-06 21:50:00 +00:00
)
// hookNames is a list of Git hooks' name that are supported.
var hookNames = []string{
"applypatch-msg",
2015-11-24 21:30:47 +00:00
"pre-applypatch",
"post-applypatch",
"pre-commit",
2014-10-06 21:50:00 +00:00
"prepare-commit-msg",
"commit-msg",
"post-commit",
2015-11-24 21:30:47 +00:00
"pre-rebase",
"post-checkout",
"post-merge",
"pre-push",
// "update",
2014-10-06 21:50:00 +00:00
"post-receive",
"post-update",
2015-11-24 21:30:47 +00:00
"push-to-checkout",
"pre-auto-gc",
"post-rewrite",
2014-10-06 21:50:00 +00:00
}
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
}
}
return false
}
// Hook represents a Git hook.
type Hook struct {
name string
IsActive bool // Indicates whether repository has this hook.
Content string // Content of hook if it's active.
Sample string // Sample content from Git.
path string // Hook file path.
}
// GetHook returns a Git hook by given name and repository.
func GetHook(repoPath, name string) (*Hook, error) {
if !IsValidHookName(name) {
return nil, ErrNotValidHook
}
h := &Hook{
name: name,
path: path.Join(repoPath, "hooks", name),
}
if isFile(h.path) {
data, err := ioutil.ReadFile(h.path)
if err != nil {
return nil, err
}
h.IsActive = true
h.Content = string(data)
} else if isFile(h.path + ".sample") {
data, err := ioutil.ReadFile(h.path + ".sample")
if err != nil {
return nil, err
}
h.Sample = string(data)
}
return h, nil
}
func (h *Hook) Name() string {
return h.name
}
// Update updates hook settings.
func (h *Hook) Update() error {
if len(strings.TrimSpace(h.Content)) == 0 {
2015-08-26 10:03:30 +00:00
if com.IsExist(h.path) {
return os.Remove(h.path)
}
return nil
2014-10-06 21:50:00 +00:00
}
2014-10-11 22:20:07 +00:00
return ioutil.WriteFile(h.path, []byte(strings.Replace(h.Content, "\r", "", -1)), os.ModePerm)
2014-10-06 21:50:00 +00:00
}
// ListHooks returns a list of Git hooks of given repository.
func ListHooks(repoPath string) (_ []*Hook, err error) {
if !isDir(path.Join(repoPath, "hooks")) {
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)
if err != nil {
return nil, err
}
}
return hooks, nil
}
func (repo *Repository) GetHook(name string) (*Hook, error) {
return GetHook(repo.Path, name)
}
func (repo *Repository) Hooks() ([]*Hook, error) {
return ListHooks(repo.Path)
}