v15.go 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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/modules/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. repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".git"
  57. log.Trace("[%04d]: %s", idx, repoPath)
  58. hookDir := filepath.Join(repoPath, "hooks")
  59. customHookDir := filepath.Join(repoPath, "custom_hooks")
  60. for i, hookName := range hookNames {
  61. oldHookPath := filepath.Join(hookDir, hookName)
  62. newHookPath := filepath.Join(customHookDir, hookName)
  63. // Gogs didn't allow user to set custom update hook thus no migration for it.
  64. // In case user runs this migration multiple times, and custom hook exists,
  65. // we assume it's been migrated already.
  66. if hookName != "update" && com.IsFile(oldHookPath) && !com.IsExist(newHookPath) {
  67. os.MkdirAll(customHookDir, os.ModePerm)
  68. if err = os.Rename(oldHookPath, newHookPath); err != nil {
  69. return fmt.Errorf("move hook file to custom directory '%s' -> '%s': %v", oldHookPath, newHookPath, err)
  70. }
  71. }
  72. if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil {
  73. return fmt.Errorf("write hook file '%s': %v", oldHookPath, err)
  74. }
  75. }
  76. return nil
  77. })
  78. }