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.

hook.go 2.9KB

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