@@ -6,8 +6,6 @@ package pull
6
6
7
7
import (
8
8
"fmt"
9
- "strconv"
10
- "strings"
11
9
12
10
"code.gitea.io/gitea/models"
13
11
"code.gitea.io/gitea/modules/git"
@@ -74,54 +72,11 @@ func GetDiverging(pr *models.PullRequest) (*git.DivergeObject, error) {
74
72
return nil , err
75
73
}
76
74
77
- headRepoPath := pr .HeadRepo .RepoPath ()
78
- headGitRepo , err := git .OpenRepository (headRepoPath )
75
+ tmpRepo , err := createTemporaryRepo (pr )
79
76
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
110
78
}
111
79
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
127
82
}
0 commit comments