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.

v134.go 2.7KB

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