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.

115 lines
3.3 KiB

  1. // Copyright 2020 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. "math"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/log"
  13. "code.gitea.io/gitea/modules/setting"
  14. "xorm.io/xorm"
  15. )
  16. func refixMergeBase(x *xorm.Engine) error {
  17. type Repository struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. OwnerID int64 `xorm:"UNIQUE(s) index"`
  20. OwnerName string
  21. LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
  22. Name string `xorm:"INDEX NOT NULL"`
  23. }
  24. type PullRequest struct {
  25. ID int64 `xorm:"pk autoincr"`
  26. Index int64
  27. HeadRepoID int64 `xorm:"INDEX"`
  28. BaseRepoID int64 `xorm:"INDEX"`
  29. HeadBranch string
  30. BaseBranch string
  31. MergeBase string `xorm:"VARCHAR(40)"`
  32. HasMerged bool `xorm:"INDEX"`
  33. MergedCommitID string `xorm:"VARCHAR(40)"`
  34. }
  35. var limit = setting.Database.IterateBufferSize
  36. if limit <= 0 {
  37. limit = 50
  38. }
  39. ticker := time.NewTicker(5 * time.Second)
  40. defer ticker.Stop()
  41. count, err := x.Where("has_merged = ?", true).Count(new(PullRequest))
  42. if err != nil {
  43. return err
  44. }
  45. log.Info("%d Merged Pull Request(s) to migrate ...", count)
  46. i := 0
  47. start := 0
  48. for {
  49. prs := make([]PullRequest, 0, 50)
  50. if err := x.Limit(limit, start).Asc("id").Where("has_merged = ?", true).Find(&prs); err != nil {
  51. return fmt.Errorf("Find: %v", err)
  52. }
  53. if len(prs) == 0 {
  54. break
  55. }
  56. start += 50
  57. for _, pr := range prs {
  58. baseRepo := &Repository{ID: pr.BaseRepoID}
  59. has, err := x.Table("repository").Get(baseRepo)
  60. if err != nil {
  61. return fmt.Errorf("Unable to get base repo %d %v", pr.BaseRepoID, err)
  62. }
  63. if !has {
  64. log.Error("Missing base repo with id %d for PR ID %d", pr.BaseRepoID, pr.ID)
  65. continue
  66. }
  67. userPath := filepath.Join(setting.RepoRootPath, strings.ToLower(baseRepo.OwnerName))
  68. repoPath := filepath.Join(userPath, strings.ToLower(baseRepo.Name)+".git")
  69. gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index)
  70. parentsString, err := git.NewCommand("rev-list", "--parents", "-n", "1", pr.MergedCommitID).RunInDir(repoPath)
  71. if err != nil {
  72. log.Error("Unable to get parents for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
  73. continue
  74. }
  75. parents := strings.Split(strings.TrimSpace(parentsString), " ")
  76. if len(parents) < 3 {
  77. continue
  78. }
  79. // we should recalculate
  80. args := append([]string{"merge-base", "--"}, parents[1:]...)
  81. args = append(args, gitRefName)
  82. pr.MergeBase, err = git.NewCommand(args...).RunInDir(repoPath)
  83. if err != nil {
  84. log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err)
  85. continue
  86. }
  87. pr.MergeBase = strings.TrimSpace(pr.MergeBase)
  88. x.ID(pr.ID).Cols("merge_base").Update(pr)
  89. i++
  90. select {
  91. case <-ticker.C:
  92. log.Info("%d/%d (%2.0f%%) Pull Request(s) migrated in %d batches. %d PRs Remaining ...", i, count, float64(i)/float64(count)*100, int(math.Ceil(float64(i)/float64(limit))), count-int64(i))
  93. default:
  94. }
  95. }
  96. }
  97. log.Info("Completed migrating %d Pull Request(s) in: %d batches", count, int(math.Ceil(float64(i)/float64(limit))))
  98. return nil
  99. }