|
| 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 | + |
| 5 | +package migrations |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "code.gitea.io/gitea/modules/git" |
| 13 | + "code.gitea.io/gitea/modules/log" |
| 14 | + "code.gitea.io/gitea/modules/setting" |
| 15 | + |
| 16 | + "xorm.io/xorm" |
| 17 | +) |
| 18 | + |
| 19 | +func fixMergeBase(x *xorm.Engine) error { |
| 20 | + type Repository struct { |
| 21 | + ID int64 `xorm:"pk autoincr"` |
| 22 | + OwnerID int64 `xorm:"UNIQUE(s) index"` |
| 23 | + OwnerName string |
| 24 | + LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"` |
| 25 | + Name string `xorm:"INDEX NOT NULL"` |
| 26 | + } |
| 27 | + |
| 28 | + type PullRequest struct { |
| 29 | + ID int64 `xorm:"pk autoincr"` |
| 30 | + Index int64 |
| 31 | + HeadRepoID int64 `xorm:"INDEX"` |
| 32 | + BaseRepoID int64 `xorm:"INDEX"` |
| 33 | + HeadBranch string |
| 34 | + BaseBranch string |
| 35 | + MergeBase string `xorm:"VARCHAR(40)"` |
| 36 | + |
| 37 | + HasMerged bool `xorm:"INDEX"` |
| 38 | + MergedCommitID string `xorm:"VARCHAR(40)"` |
| 39 | + } |
| 40 | + |
| 41 | + var limit = setting.Database.IterateBufferSize |
| 42 | + if limit <= 0 { |
| 43 | + limit = 50 |
| 44 | + } |
| 45 | + |
| 46 | + i := 0 |
| 47 | + for { |
| 48 | + prs := make([]PullRequest, 0, 50) |
| 49 | + if err := x.Limit(limit, i).Asc("id").Find(&prs); err != nil { |
| 50 | + return fmt.Errorf("Find: %v", err) |
| 51 | + } |
| 52 | + if len(prs) == 0 { |
| 53 | + break |
| 54 | + } |
| 55 | + |
| 56 | + i += 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 | + |
| 70 | + gitRefName := fmt.Sprintf("refs/pull/%d/head", pr.Index) |
| 71 | + |
| 72 | + if !pr.HasMerged { |
| 73 | + var err error |
| 74 | + pr.MergeBase, err = git.NewCommand("merge-base", "--", pr.BaseBranch, gitRefName).RunInDir(repoPath) |
| 75 | + if err != nil { |
| 76 | + var err2 error |
| 77 | + pr.MergeBase, err2 = git.NewCommand("rev-parse", git.BranchPrefix+pr.BaseBranch).RunInDir(repoPath) |
| 78 | + if err2 != nil { |
| 79 | + log.Error("Unable to get merge base for PR ID %d, Index %d in %s/%s. Error: %v & %v", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err, err2) |
| 80 | + continue |
| 81 | + } |
| 82 | + } |
| 83 | + } else { |
| 84 | + var err error |
| 85 | + pr.MergeBase, err = git.NewCommand("merge-base", "--", pr.MergedCommitID+"^", gitRefName).RunInDir(repoPath) |
| 86 | + if err != nil { |
| 87 | + log.Error("Unable to get merge base for merged PR ID %d, Index %d in %s/%s. Error: %", pr.ID, pr.Index, baseRepo.OwnerName, baseRepo.Name, err) |
| 88 | + continue |
| 89 | + } |
| 90 | + } |
| 91 | + pr.MergeBase = strings.TrimSpace(pr.MergeBase) |
| 92 | + x.ID(pr.ID).Cols("merge_base").Update(pr) |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return nil |
| 97 | +} |
0 commit comments