Skip to content

Commit cbce199

Browse files
committed
some improvements
1 parent ba25ea6 commit cbce199

File tree

3 files changed

+13
-26
lines changed

3 files changed

+13
-26
lines changed

services/pull/update.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,6 @@ func Update(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.
3434
}
3535
defer releaser()
3636

37-
if err := pr.LoadBaseRepo(ctx); err != nil {
38-
log.Error("unable to load BaseRepo for PR %-v during update-by-merge: %v", pr, err)
39-
return fmt.Errorf("unable to load BaseRepo for PR[%d] during update-by-merge: %w", pr.ID, err)
40-
}
41-
42-
diffCount, err := GetDiverging(ctx, pr)
43-
if err != nil {
44-
return err
45-
} else if diffCount.Behind == 0 {
46-
return fmt.Errorf("HeadBranch of PR %d is up to date", pr.Index)
47-
}
48-
4937
if err := pr.LoadBaseRepo(ctx); err != nil {
5038
log.Error("unable to load BaseRepo for %-v during update-by-merge: %v", pr, err)
5139
return fmt.Errorf("unable to load BaseRepo for PR[%d] during update-by-merge: %w", pr.ID, err)
@@ -63,6 +51,13 @@ func Update(ctx context.Context, pr *issues_model.PullRequest, doer *user_model.
6351
return fmt.Errorf("unable to load HeadRepo for PR[%d] during update-by-merge: %w", pr.ID, err)
6452
}
6553

54+
diffCount, err := GetDiverging(ctx, pr)
55+
if err != nil {
56+
return err
57+
} else if diffCount.Behind == 0 {
58+
return fmt.Errorf("HeadBranch of PR %d is up to date", pr.Index)
59+
}
60+
6661
defer func() {
6762
go AddTestPullRequestTask(TestPullRequestOptions{
6863
RepoID: pr.BaseRepo.ID,

services/repository/branch.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
actions_service "code.gitea.io/gitea/services/actions"
3434
notify_service "code.gitea.io/gitea/services/notify"
3535
release_service "code.gitea.io/gitea/services/release"
36-
files_service "code.gitea.io/gitea/services/repository/files"
3736

3837
"xorm.io/builder"
3938
)
@@ -125,18 +124,18 @@ func getDivergenceCacheKey(repoID int64, branchName string) string {
125124
// getDivergenceFromCache gets the divergence from cache
126125
func getDivergenceFromCache(repoID int64, branchName string) (*gitrepo.DivergeObject, bool) {
127126
data, ok := cache.GetCache().Get(getDivergenceCacheKey(repoID, branchName))
128-
res := &gitrepo.DivergeObject{
127+
res := gitrepo.DivergeObject{
129128
Ahead: -1,
130129
Behind: -1,
131130
}
132131
if !ok || data == "" {
133-
return res, false
132+
return &res, false
134133
}
135134
if err := json.Unmarshal(util.UnsafeStringToBytes(data), &res); err != nil {
136135
log.Error("json.UnMarshal failed: %v", err)
137-
return res, false
136+
return &res, false
138137
}
139-
return res, true
138+
return &res, true
140139
}
141140

142141
func putDivergenceFromCache(repoID int64, branchName string, divergence *gitrepo.DivergeObject) error {
@@ -186,9 +185,9 @@ func loadOneBranch(ctx context.Context, repo *repo_model.Repository, dbBranch *g
186185
divergence, cached = getDivergenceFromCache(repo.ID, dbBranch.Name)
187186
if !cached {
188187
var err error
189-
divergence, err = files_service.CountDivergingCommits(ctx, repo, git.BranchPrefix+branchName)
188+
divergence, err = gitrepo.GetDivergingCommits(ctx, repo, repo.DefaultBranch, git.BranchPrefix+branchName)
190189
if err != nil {
191-
log.Error("CountDivergingCommits: %v", err)
190+
log.Error("GetDivergingCommits: %v", err)
192191
} else {
193192
if err = putDivergenceFromCache(repo.ID, dbBranch.Name, divergence); err != nil {
194193
log.Error("putDivergenceFromCache: %v", err)

services/repository/files/commit.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,11 @@ package files
66
import (
77
"context"
88

9-
repo_model "code.gitea.io/gitea/models/repo"
109
"code.gitea.io/gitea/modules/git"
11-
"code.gitea.io/gitea/modules/gitrepo"
1210
"code.gitea.io/gitea/modules/structs"
1311
asymkey_service "code.gitea.io/gitea/services/asymkey"
1412
)
1513

16-
// CountDivergingCommits determines how many commits a branch is ahead or behind the repository's base branch
17-
func CountDivergingCommits(ctx context.Context, repo *repo_model.Repository, branch string) (*gitrepo.DivergeObject, error) {
18-
return gitrepo.GetDivergingCommits(ctx, repo, repo.DefaultBranch, branch)
19-
}
20-
2114
// GetPayloadCommitVerification returns the verification information of a commit
2215
func GetPayloadCommitVerification(ctx context.Context, commit *git.Commit) *structs.PayloadCommitVerification {
2316
verification := &structs.PayloadCommitVerification{}

0 commit comments

Comments
 (0)