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. "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. // Note: we should not create hookDir here because update hook file should already exists inside this direcotry,
  61. // if this directory does not exist, the current setup is not correct anyway.
  62. hookDir := filepath.Join(repoPath, "hooks")
  63. customHookDir := filepath.Join(repoPath, "custom_hooks")
  64. wikiHookDir := filepath.Join(wikiPath, "hooks")
  65. for i, hookName := range hookNames {
  66. oldHookPath := filepath.Join(hookDir, hookName)
  67. newHookPath := filepath.Join(customHookDir, hookName)
  68. // Gogs didn't allow user to set custom update hook thus no migration for it.
  69. // In case user runs this migration multiple times, and custom hook exists,
  70. // we assume it's been migrated already.
  71. if hookName != "update" && com.IsFile(oldHookPath) && !com.IsExist(customHookDir) {
  72. os.MkdirAll(customHookDir, os.ModePerm)
  73. if err = os.Rename(oldHookPath, newHookPath); err != nil {
  74. return fmt.Errorf("move hook file to custom directory '%s' -> '%s': %v", oldHookPath, newHookPath, err)
  75. }
  76. }
  77. if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), os.ModePerm); err != nil {
  78. return fmt.Errorf("write hook file '%s': %v", oldHookPath, err)
  79. }
  80. if com.IsDir(wikiPath) {
  81. os.MkdirAll(wikiHookDir, os.ModePerm)
  82. wikiHookPath := filepath.Join(wikiHookDir, hookName)
  83. if err = ioutil.WriteFile(wikiHookPath, []byte(hookTpls[i]), os.ModePerm); err != nil {
  84. return fmt.Errorf("write wiki hook file '%s': %v", wikiHookPath, err)
  85. }
  86. }
  87. }
  88. return nil
  89. })
  90. }