Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

v22.go 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. "github.com/go-xorm/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.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. }