Skip to content

Commit abd3639

Browse files
committed
fix by using createTemporaryRepo
1 parent 8e26807 commit abd3639

File tree

1 file changed

+4
-49
lines changed

1 file changed

+4
-49
lines changed

services/pull/update.go

Lines changed: 4 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ package pull
66

77
import (
88
"fmt"
9-
"strconv"
10-
"strings"
119

1210
"code.gitea.io/gitea/models"
1311
"code.gitea.io/gitea/modules/git"
@@ -74,54 +72,11 @@ func GetDiverging(pr *models.PullRequest) (*git.DivergeObject, error) {
7472
return nil, err
7573
}
7674

77-
headRepoPath := pr.HeadRepo.RepoPath()
78-
headGitRepo, err := git.OpenRepository(headRepoPath)
75+
tmpRepo, err := createTemporaryRepo(pr)
7976
if err != nil {
80-
return nil, fmt.Errorf("OpenRepository: %v", err)
81-
}
82-
defer headGitRepo.Close()
83-
84-
if pr.IsSameRepo() {
85-
diff, err := git.GetDivergingCommits(pr.HeadRepo.RepoPath(), pr.BaseBranch, pr.HeadBranch)
86-
return &diff, err
87-
}
88-
89-
tmpRemoteName := fmt.Sprintf("tmp-pull-%d-base", pr.ID)
90-
if err = headGitRepo.AddRemote(tmpRemoteName, pr.BaseRepo.RepoPath(), true); err != nil {
91-
return nil, fmt.Errorf("headGitRepo.AddRemote: %v", err)
92-
}
93-
// Make sure to remove the remote even if the push fails
94-
defer func() {
95-
if err := headGitRepo.RemoveRemote(tmpRemoteName); err != nil {
96-
log.Error("CountDiverging: RemoveRemote: %s", err)
97-
}
98-
}()
99-
100-
// $(git rev-list --count tmp-pull-1-base/master..feature) commits ahead of master
101-
ahead, errorAhead := checkDivergence(headRepoPath, fmt.Sprintf("%s/%s", tmpRemoteName, pr.BaseBranch), pr.HeadBranch)
102-
if errorAhead != nil {
103-
return &git.DivergeObject{}, errorAhead
104-
}
105-
106-
// $(git rev-list --count feature..tmp-pull-1-base/master) commits behind master
107-
behind, errorBehind := checkDivergence(headRepoPath, pr.HeadBranch, fmt.Sprintf("%s/%s", tmpRemoteName, pr.BaseBranch))
108-
if errorBehind != nil {
109-
return &git.DivergeObject{}, errorBehind
77+
return nil, err
11078
}
11179

112-
return &git.DivergeObject{Ahead: ahead, Behind: behind}, nil
113-
}
114-
115-
func checkDivergence(repoPath string, baseBranch string, targetBranch string) (int, error) {
116-
branches := fmt.Sprintf("%s..%s", baseBranch, targetBranch)
117-
cmd := git.NewCommand("rev-list", "--count", branches)
118-
stdout, err := cmd.RunInDir(repoPath)
119-
if err != nil {
120-
return -1, err
121-
}
122-
outInteger, errInteger := strconv.Atoi(strings.Trim(stdout, "\n"))
123-
if errInteger != nil {
124-
return -1, errInteger
125-
}
126-
return outInteger, nil
80+
diff, err := git.GetDivergingCommits(tmpRepo, "base", "tracking")
81+
return &diff, err
12782
}

0 commit comments

Comments
 (0)