From 64c68220d203cb07be001184cde4b35d4b503344 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Mon, 6 Oct 2014 17:50:00 -0400 Subject: Fix #264 --- modules/git/hooks.go | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 modules/git/hooks.go (limited to 'modules/git/hooks.go') diff --git a/modules/git/hooks.go b/modules/git/hooks.go new file mode 100644 index 00000000..b8d15e5e --- /dev/null +++ b/modules/git/hooks.go @@ -0,0 +1,111 @@ +// 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" +) + +// hookNames is a list of Git hooks' name that are supported. +var hookNames = []string{ + "pre-applypatch", + "applypatch-msg", + "prepare-commit-msg", + "commit-msg", + "pre-commit", + "pre-rebase", + "post-commit", + "post-receive", + "post-update", +} + +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 { + return os.Remove(h.path) + } + return ioutil.WriteFile(h.path, []byte(h.Content), os.ModePerm) +} + +// 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) +} -- cgit v1.2.3 From 3005c4f6db725e0e637987d3ac154865526c1363 Mon Sep 17 00:00:00 2001 From: Unknwon Date: Sat, 11 Oct 2014 18:20:07 -0400 Subject: Fix diff css style, hooks \r char --- modules/git/hooks.go | 2 +- templates/repo/diff.tmpl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'modules/git/hooks.go') diff --git a/modules/git/hooks.go b/modules/git/hooks.go index b8d15e5e..5b3c88a9 100644 --- a/modules/git/hooks.go +++ b/modules/git/hooks.go @@ -83,7 +83,7 @@ func (h *Hook) Update() error { if len(strings.TrimSpace(h.Content)) == 0 { return os.Remove(h.path) } - return ioutil.WriteFile(h.path, []byte(h.Content), os.ModePerm) + return ioutil.WriteFile(h.path, []byte(strings.Replace(h.Content, "\r", "", -1)), os.ModePerm) } // ListHooks returns a list of Git hooks of given repository. diff --git a/templates/repo/diff.tmpl b/templates/repo/diff.tmpl index 30a983ac..9dac1cac 100644 --- a/templates/repo/diff.tmpl +++ b/templates/repo/diff.tmpl @@ -74,7 +74,7 @@ {{range .Diff.Files}} -
+
{{if not .IsBin}} -- cgit v1.2.3