hooks.go 2.6 KB

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