v15.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright 2017 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 migrations
  5. import (
  6. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. "github.com/Unknwon/com"
  12. "github.com/go-xorm/xorm"
  13. log "gopkg.in/clog.v1"
  14. "github.com/gogits/gogs/pkg/setting"
  15. )
  16. func generateAndMigrateGitHooks(x *xorm.Engine) (err error) {
  17. type Repository struct {
  18. ID int64
  19. OwnerID int64
  20. Name string
  21. }
  22. type User struct {
  23. ID int64
  24. Name string
  25. }
  26. var (
  27. hookNames = []string{"pre-receive", "update", "post-receive"}
  28. hookTpls = []string{
  29. fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' pre-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
  30. fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' update $1 $2 $3\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
  31. fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' post-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
  32. }
  33. )
  34. // Cleanup old update.log and http.log files.
  35. filepath.Walk(setting.LogRootPath, func(path string, info os.FileInfo, err error) error {
  36. if !info.IsDir() &&
  37. (strings.HasPrefix(filepath.Base(path), "update.log") ||
  38. strings.HasPrefix(filepath.Base(path), "http.log")) {
  39. os.Remove(path)
  40. }
  41. return nil
  42. })
  43. return x.Where("id > 0").Iterate(new(Repository),
  44. func(idx int, bean interface{}) error {
  45. repo := bean.(*Repository)
  46. if repo.Name == "." || repo.Name == ".." {
  47. return nil
  48. }
  49. user := new(User)
  50. has, err := x.Where("id = ?", repo.OwnerID).Get(user)
  51. if err != nil {
  52. return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
  53. } else if !has {
  54. return nil
  55. }
  56. repoBase := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name))
  57. repoPath := repoBase + ".git"
  58. wikiPath := repoBase + ".wiki.git"
  59. log.Trace("[%04d]: %s", idx, repoPath)
  60. hookDir := filepath.Join(repoPath, "hooks")
  61. customHookDir := filepath.Join(repoPath, "custom_hooks")
  62. wikiHookDir := filepath.Join(wikiPath, "hooks")
  63. for i, hookName := range hookNames {
  64. oldHookPath := filepath.Join(hookDir, hookName)
  65. newHookPath := filepath.Join(customHookDir, hookName)
  66. // Gogs didn't allow user to set custom update hook thus no migration for it.
  67. // In case user runs this migration multiple times, and custom hook exists,
  68. // we assume it's been migrated already.
  69. if hookName != "update" && com.IsFile(oldHookPath) && !com.IsExist(customHookDir) {
  70. os.MkdirAll(customHookDir, os.ModePerm)
  71. if err = os.Rename(oldHookPath, newHookPath); err != nil {
  72. return fmt.Errorf("move hook file to custom directory '%s' -> '%s': %v", oldHookPath, newHookPath, err)
  73. }
  74. }
  75. if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), os.ModePerm); err != nil {
  76. return fmt.Errorf("write hook file '%s': %v", oldHookPath, err)
  77. }
  78. if com.IsDir(wikiPath) {
  79. os.MkdirAll(wikiHookDir, os.ModePerm)
  80. wikiHookPath := filepath.Join(wikiHookDir, hookName)
  81. if err = ioutil.WriteFile(wikiHookPath, []byte(hookTpls[i]), os.ModePerm); err != nil {
  82. return fmt.Errorf("write wiki hook file '%s': %v", wikiHookPath, err)
  83. }
  84. }
  85. }
  86. return nil
  87. })
  88. }