v15.go 3.5 KB

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