@@ -6,25 +6,60 @@ package migrations
6
6
7
7
import (
8
8
"fmt"
9
+ "math"
10
+ "path/filepath"
11
+ "strings"
12
+ "time"
9
13
10
14
"code.gitea.io/gitea/models"
15
+ "code.gitea.io/gitea/modules/git"
11
16
"code.gitea.io/gitea/modules/log"
12
17
"code.gitea.io/gitea/modules/setting"
13
- pull_service "code.gitea.io/gitea/services/pull"
14
18
15
19
"xorm.io/xorm"
16
20
)
17
21
18
22
func addCommitDivergenceToPulls (x * xorm.Engine ) error {
23
+ type Repository struct {
24
+ ID int64 `xorm:"pk autoincr"`
25
+ OwnerID int64 `xorm:"UNIQUE(s) index"`
26
+ OwnerName string
27
+ LowerName string `xorm:"UNIQUE(s) INDEX NOT NULL"`
28
+ Name string `xorm:"INDEX NOT NULL"`
29
+ }
30
+
31
+ type PullRequest struct {
32
+ ID int64 `xorm:"pk autoincr"`
33
+
34
+ CommitsAhead int
35
+ CommitsBehind int
36
+
37
+ BaseRepoID int64 `xorm:"INDEX"`
38
+ BaseBranch string
39
+
40
+ HasMerged bool `xorm:"INDEX"`
41
+ MergedCommitID string `xorm:"VARCHAR(40)"`
42
+ }
19
43
20
44
if err := x .Sync2 (new (models.PullRequest )); err != nil {
21
45
return fmt .Errorf ("Sync2: %v" , err )
22
46
}
23
47
24
- var last int
48
+ last := 0
49
+ migrated := 0
50
+
25
51
batchSize := setting .Database .IterateBufferSize
26
52
sess := x .NewSession ()
27
53
defer sess .Close ()
54
+
55
+ ticker := time .NewTicker (5 * time .Second )
56
+ defer ticker .Stop ()
57
+ count , err := sess .Where ("has_merged = ?" , false ).Count (new (PullRequest ))
58
+ if err != nil {
59
+ return err
60
+ }
61
+ log .Info ("%d Unmerged Pull Request(s) to migrate ..." , count )
62
+
28
63
for {
29
64
if err := sess .Begin (); err != nil {
30
65
return err
@@ -37,27 +72,53 @@ func addCommitDivergenceToPulls(x *xorm.Engine) error {
37
72
if len (results ) == 0 {
38
73
break
39
74
}
40
- last += len ( results )
75
+ last += batchSize
41
76
42
77
for _ , pr := range results {
43
- divergence , err := pull_service .GetDiverging (pr )
78
+ baseRepo := & Repository {ID : pr .BaseRepoID }
79
+ has , err := x .Table ("repository" ).Get (baseRepo )
80
+ if err != nil {
81
+ return fmt .Errorf ("Unable to get base repo %d %v" , pr .BaseRepoID , err )
82
+ }
83
+ if ! has {
84
+ log .Error ("Missing base repo with id %d for PR ID %d" , pr .BaseRepoID , pr .ID )
85
+ continue
86
+ }
87
+ userPath := filepath .Join (setting .RepoRootPath , strings .ToLower (baseRepo .OwnerName ))
88
+ repoPath := filepath .Join (userPath , strings .ToLower (baseRepo .Name )+ ".git" )
89
+
90
+ gitRefName := fmt .Sprintf ("refs/pull/%d/head" , pr .Index )
91
+
92
+ divergence , err := git .GetDivergingCommits (repoPath , pr .BaseBranch , gitRefName )
44
93
if err != nil {
45
94
log .Warn ("Could not recalculate Divergence for pull: %d" , pr .ID )
46
95
pr .CommitsAhead = 0
47
96
pr .CommitsBehind = 0
48
97
}
49
- if divergence != nil {
50
- pr .CommitsAhead = divergence .Ahead
51
- pr .CommitsBehind = divergence .Behind
52
- }
98
+ pr .CommitsAhead = divergence .Ahead
99
+ pr .CommitsBehind = divergence .Behind
100
+
53
101
if _ , err = sess .ID (pr .ID ).Cols ("commits_ahead" , "commits_behind" ).Update (pr ); err != nil {
54
102
return fmt .Errorf ("Update Cols: %v" , err )
55
103
}
104
+ migrated ++
56
105
}
57
106
58
107
if err := sess .Commit (); err != nil {
59
108
return err
60
109
}
110
+ select {
111
+ case <- ticker .C :
112
+ log .Info (
113
+ "%d/%d (%2.0f%%) Pull Request(s) migrated in %d batches. %d PRs Remaining ..." ,
114
+ migrated ,
115
+ count ,
116
+ float64 (migrated )/ float64 (count )* 100 ,
117
+ int (math .Ceil (float64 (migrated )/ float64 (batchSize ))),
118
+ count - int64 (migrated ))
119
+ default :
120
+ }
61
121
}
122
+ log .Info ("Completed migrating %d Pull Request(s) in: %d batches" , count , int (math .Ceil (float64 (migrated )/ float64 (batchSize ))))
62
123
return nil
63
124
}
0 commit comments