You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

v22.go 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2017 The Gitea 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. "code.gitea.io/gitea/modules/setting"
  12. "github.com/unknwon/com"
  13. "xorm.io/xorm"
  14. )
  15. func generateAndMigrateWikiGitHooks(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\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/pre-receive.d\"`; do\n sh \"$SHELL_FOLDER/pre-receive.d/$i\"\ndone", setting.ScriptType),
  29. fmt.Sprintf("#!/usr/bin/env %s\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/update.d\"`; do\n sh \"$SHELL_FOLDER/update.d/$i\" $1 $2 $3\ndone", setting.ScriptType),
  30. fmt.Sprintf("#!/usr/bin/env %s\nORI_DIR=`pwd`\nSHELL_FOLDER=$(cd \"$(dirname \"$0\")\";pwd)\ncd \"$ORI_DIR\"\nfor i in `ls \"$SHELL_FOLDER/post-receive.d\"`; do\n sh \"$SHELL_FOLDER/post-receive.d/$i\"\ndone", setting.ScriptType),
  31. }
  32. giteaHookTpls = []string{
  33. fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' pre-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
  34. fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' update $1 $2 $3\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
  35. fmt.Sprintf("#!/usr/bin/env %s\n\"%s\" hook --config='%s' post-receive\n", setting.ScriptType, setting.AppPath, setting.CustomConf),
  36. }
  37. )
  38. return x.Where("id > 0").BufferSize(setting.Database.IterateBufferSize).Iterate(new(Repository),
  39. func(idx int, bean interface{}) error {
  40. repo := bean.(*Repository)
  41. user := new(User)
  42. has, err := x.Where("id = ?", repo.OwnerID).Get(user)
  43. if err != nil {
  44. return fmt.Errorf("query owner of repository [repo_id: %d, owner_id: %d]: %v", repo.ID, repo.OwnerID, err)
  45. } else if !has {
  46. return nil
  47. }
  48. repoPath := filepath.Join(setting.RepoRootPath, strings.ToLower(user.Name), strings.ToLower(repo.Name)) + ".wiki.git"
  49. if !com.IsExist(repoPath) {
  50. return nil
  51. }
  52. hookDir := filepath.Join(repoPath, "hooks")
  53. for i, hookName := range hookNames {
  54. oldHookPath := filepath.Join(hookDir, hookName)
  55. newHookPath := filepath.Join(hookDir, hookName+".d", "gitea")
  56. customHooksDir := filepath.Join(hookDir, hookName+".d")
  57. // if it's exist, that means you have upgraded ever
  58. if com.IsExist(customHooksDir) {
  59. continue
  60. }
  61. if err = os.MkdirAll(customHooksDir, os.ModePerm); err != nil {
  62. return fmt.Errorf("create hooks dir '%s': %v", customHooksDir, err)
  63. }
  64. // WARNING: Old server-side hooks will be moved to sub directory with the same name
  65. if hookName != "update" && com.IsExist(oldHookPath) {
  66. newPlace := filepath.Join(hookDir, hookName+".d", hookName)
  67. if err = os.Rename(oldHookPath, newPlace); err != nil {
  68. return fmt.Errorf("Remove old hook file '%s' to '%s': %v", oldHookPath, newPlace, err)
  69. }
  70. }
  71. if err = ioutil.WriteFile(oldHookPath, []byte(hookTpls[i]), 0777); err != nil {
  72. return fmt.Errorf("write old hook file '%s': %v", oldHookPath, err)
  73. }
  74. if err = ioutil.WriteFile(newHookPath, []byte(giteaHookTpls[i]), 0777); err != nil {
  75. return fmt.Errorf("write new hook file '%s': %v", oldHookPath, err)
  76. }
  77. }
  78. return nil
  79. })
  80. }