v15.go 2.7 KB

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