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.

134 lines
2.9 KiB

  1. // Copyright 2019 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. "path/filepath"
  8. "strings"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/setting"
  11. "xorm.io/xorm"
  12. )
  13. func fixReleaseSha1OnReleaseTable(x *xorm.Engine) error {
  14. type Release struct {
  15. ID int64
  16. RepoID int64
  17. Sha1 string
  18. TagName string
  19. }
  20. type Repository struct {
  21. ID int64
  22. OwnerID int64
  23. Name string
  24. }
  25. type User struct {
  26. ID int64
  27. Name string
  28. }
  29. // UserPath returns the path absolute path of user repositories.
  30. UserPath := func(userName string) string {
  31. return filepath.Join(setting.RepoRootPath, strings.ToLower(userName))
  32. }
  33. // RepoPath returns repository path by given user and repository name.
  34. RepoPath := func(userName, repoName string) string {
  35. return filepath.Join(UserPath(userName), strings.ToLower(repoName)+".git")
  36. }
  37. // Update release sha1
  38. const batchSize = 100
  39. sess := x.NewSession()
  40. defer sess.Close()
  41. var (
  42. err error
  43. count int
  44. gitRepoCache = make(map[int64]*git.Repository)
  45. repoCache = make(map[int64]*Repository)
  46. userCache = make(map[int64]*User)
  47. )
  48. if err = sess.Begin(); err != nil {
  49. return err
  50. }
  51. for start := 0; ; start += batchSize {
  52. releases := make([]*Release, 0, batchSize)
  53. if err = sess.Limit(batchSize, start).Asc("id").Where("is_tag=?", false).Find(&releases); err != nil {
  54. return err
  55. }
  56. if len(releases) == 0 {
  57. break
  58. }
  59. for _, release := range releases {
  60. gitRepo, ok := gitRepoCache[release.RepoID]
  61. if !ok {
  62. repo, ok := repoCache[release.RepoID]
  63. if !ok {
  64. repo = new(Repository)
  65. has, err := sess.ID(release.RepoID).Get(repo)
  66. if err != nil {
  67. return err
  68. } else if !has {
  69. return fmt.Errorf("Repository %d is not exist", release.RepoID)
  70. }
  71. repoCache[release.RepoID] = repo
  72. }
  73. user, ok := userCache[repo.OwnerID]
  74. if !ok {
  75. user = new(User)
  76. has, err := sess.ID(repo.OwnerID).Get(user)
  77. if err != nil {
  78. return err
  79. } else if !has {
  80. return fmt.Errorf("User %d is not exist", repo.OwnerID)
  81. }
  82. userCache[repo.OwnerID] = user
  83. }
  84. gitRepo, err = git.OpenRepository(RepoPath(user.Name, repo.Name))
  85. if err != nil {
  86. return err
  87. }
  88. defer gitRepo.Close()
  89. gitRepoCache[release.RepoID] = gitRepo
  90. }
  91. release.Sha1, err = gitRepo.GetTagCommitID(release.TagName)
  92. if err != nil && !git.IsErrNotExist(err) {
  93. return err
  94. }
  95. if err == nil {
  96. if _, err = sess.ID(release.ID).Cols("sha1").Update(release); err != nil {
  97. return err
  98. }
  99. }
  100. count++
  101. if count >= 1000 {
  102. if err = sess.Commit(); err != nil {
  103. return err
  104. }
  105. if err = sess.Begin(); err != nil {
  106. return err
  107. }
  108. count = 0
  109. }
  110. }
  111. }
  112. return sess.Commit()
  113. }