mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-03 08:02:36 +09:00 
			
		
		
		
	Number of commits ahead/behind in branch overview (#6695)
* Call Git API to determine divergence of a branch and its base branch Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Show commit divergance in branch list Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds missing comment Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adds test for diverging commits Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Try comparing commits instead of branches Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Removes test as CI can't run it Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Adjusts signature of percentage function to allow providing multiple integers as numerator Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com> * Moves CountDivergingCommits function into repofiles module Signed-off-by: Mario Lubenka <mario.lubenka@googlemail.com>
This commit is contained in:
		@@ -9,9 +9,11 @@ import (
 | 
			
		||||
	"bytes"
 | 
			
		||||
	"container/list"
 | 
			
		||||
	"errors"
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
@@ -306,3 +308,40 @@ func GetLatestCommitTime(repoPath string) (time.Time, error) {
 | 
			
		||||
	commitTime := strings.TrimSpace(stdout)
 | 
			
		||||
	return time.Parse(GitTimeLayout, commitTime)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DivergeObject represents commit count diverging commits
 | 
			
		||||
type DivergeObject struct {
 | 
			
		||||
	Ahead  int
 | 
			
		||||
	Behind int
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func checkDivergence(repoPath string, baseBranch string, targetBranch string) (int, error) {
 | 
			
		||||
	branches := fmt.Sprintf("%s..%s", baseBranch, targetBranch)
 | 
			
		||||
	cmd := NewCommand("rev-list", "--count", branches)
 | 
			
		||||
	stdout, err := cmd.RunInDir(repoPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return -1, err
 | 
			
		||||
	}
 | 
			
		||||
	outInteger, errInteger := strconv.Atoi(strings.Trim(stdout, "\n"))
 | 
			
		||||
	if errInteger != nil {
 | 
			
		||||
		return -1, errInteger
 | 
			
		||||
	}
 | 
			
		||||
	return outInteger, nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetDivergingCommits returns the number of commits a targetBranch is ahead or behind a baseBranch
 | 
			
		||||
func GetDivergingCommits(repoPath string, baseBranch string, targetBranch string) (DivergeObject, error) {
 | 
			
		||||
	// $(git rev-list --count master..feature) commits ahead of master
 | 
			
		||||
	ahead, errorAhead := checkDivergence(repoPath, baseBranch, targetBranch)
 | 
			
		||||
	if errorAhead != nil {
 | 
			
		||||
		return DivergeObject{}, errorAhead
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// $(git rev-list --count feature..master) commits behind master
 | 
			
		||||
	behind, errorBehind := checkDivergence(repoPath, targetBranch, baseBranch)
 | 
			
		||||
	if errorBehind != nil {
 | 
			
		||||
		return DivergeObject{}, errorBehind
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return DivergeObject{ahead, behind}, nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user