hooks.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2014 The Gogs 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. package git
  5. import (
  6. "errors"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "strings"
  11. )
  12. // hookNames is a list of Git hooks' name that are supported.
  13. var hookNames = []string{
  14. "pre-applypatch",
  15. "applypatch-msg",
  16. "prepare-commit-msg",
  17. "commit-msg",
  18. "pre-commit",
  19. "pre-rebase",
  20. "post-commit",
  21. "post-receive",
  22. "post-update",
  23. }
  24. var (
  25. ErrNotValidHook = errors.New("not a valid Git hook")
  26. )
  27. // IsValidHookName returns true if given name is a valid Git hook.
  28. func IsValidHookName(name string) bool {
  29. for _, hn := range hookNames {
  30. if hn == name {
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. // Hook represents a Git hook.
  37. type Hook struct {
  38. name string
  39. IsActive bool // Indicates whether repository has this hook.
  40. Content string // Content of hook if it's active.
  41. Sample string // Sample content from Git.
  42. path string // Hook file path.
  43. }
  44. // GetHook returns a Git hook by given name and repository.
  45. func GetHook(repoPath, name string) (*Hook, error) {
  46. if !IsValidHookName(name) {
  47. return nil, ErrNotValidHook
  48. }
  49. h := &Hook{
  50. name: name,
  51. path: path.Join(repoPath, "hooks", name),
  52. }
  53. if isFile(h.path) {
  54. data, err := ioutil.ReadFile(h.path)
  55. if err != nil {
  56. return nil, err
  57. }
  58. h.IsActive = true
  59. h.Content = string(data)
  60. } else if isFile(h.path + ".sample") {
  61. data, err := ioutil.ReadFile(h.path + ".sample")
  62. if err != nil {
  63. return nil, err
  64. }
  65. h.Sample = string(data)
  66. }
  67. return h, nil
  68. }
  69. func (h *Hook) Name() string {
  70. return h.name
  71. }
  72. // Update updates hook settings.
  73. func (h *Hook) Update() error {
  74. if len(strings.TrimSpace(h.Content)) == 0 {
  75. return os.Remove(h.path)
  76. }
  77. return ioutil.WriteFile(h.path, []byte(strings.Replace(h.Content, "\r", "", -1)), os.ModePerm)
  78. }
  79. // ListHooks returns a list of Git hooks of given repository.
  80. func ListHooks(repoPath string) (_ []*Hook, err error) {
  81. if !isDir(path.Join(repoPath, "hooks")) {
  82. return nil, errors.New("hooks path does not exist")
  83. }
  84. hooks := make([]*Hook, len(hookNames))
  85. for i, name := range hookNames {
  86. hooks[i], err = GetHook(repoPath, name)
  87. if err != nil {
  88. return nil, err
  89. }
  90. }
  91. return hooks, nil
  92. }
  93. func (repo *Repository) GetHook(name string) (*Hook, error) {
  94. return GetHook(repo.Path, name)
  95. }
  96. func (repo *Repository) Hooks() ([]*Hook, error) {
  97. return ListHooks(repo.Path)
  98. }