hooks.go 2.6 KB

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