@@ -244,35 +244,28 @@ type DivergeObject struct {
244
244
Behind int
245
245
}
246
246
247
- func checkDivergence (ctx context.Context , repoPath , baseBranch , targetBranch string ) (int , error ) {
248
- branches := fmt .Sprintf ("%s..%s" , baseBranch , targetBranch )
249
- cmd := NewCommand (ctx , "rev-list" , "--count" ).AddDynamicArguments (branches )
247
+ // GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
248
+ func GetDivergingCommits (ctx context.Context , repoPath , baseBranch , targetBranch string ) (do DivergeObject , err error ) {
249
+ cmd := NewCommand (ctx , "rev-list" , "--count" , "--left-right" ).
250
+ AddDynamicArguments (baseBranch + "..." + targetBranch )
250
251
stdout , _ , err := cmd .RunStdString (& RunOpts {Dir : repoPath })
251
252
if err != nil {
252
- return - 1 , err
253
+ return do , err
253
254
}
254
- outInteger , errInteger := strconv . Atoi (strings .Trim (stdout , "\n " ))
255
- if errInteger != nil {
256
- return - 1 , errInteger
255
+ left , right , found := strings . Cut (strings .Trim (stdout , "\n " ), " \t " )
256
+ if ! found {
257
+ return do , fmt . Errorf ( "git rev-list output is missing a tab: %q" , stdout )
257
258
}
258
- return outInteger , nil
259
- }
260
259
261
- // GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
262
- func GetDivergingCommits (ctx context.Context , repoPath , baseBranch , targetBranch string ) (DivergeObject , error ) {
263
- // $(git rev-list --count master..feature) commits ahead of master
264
- ahead , errorAhead := checkDivergence (ctx , repoPath , baseBranch , targetBranch )
265
- if errorAhead != nil {
266
- return DivergeObject {}, errorAhead
260
+ do .Behind , err = strconv .Atoi (left )
261
+ if err != nil {
262
+ return do , err
267
263
}
268
-
269
- // $(git rev-list --count feature..master) commits behind master
270
- behind , errorBehind := checkDivergence (ctx , repoPath , targetBranch , baseBranch )
271
- if errorBehind != nil {
272
- return DivergeObject {}, errorBehind
264
+ do .Ahead , err = strconv .Atoi (right )
265
+ if err != nil {
266
+ return do , err
273
267
}
274
-
275
- return DivergeObject {ahead , behind }, nil
268
+ return do , nil
276
269
}
277
270
278
271
// CreateBundle create bundle content to the target path
0 commit comments