You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
2.7 KiB

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