diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/git/repo.go | 39 | ||||
-rw-r--r-- | modules/repofiles/commit.go | 19 | ||||
-rw-r--r-- | modules/templates/helper.go | 7 |
3 files changed, 65 insertions, 0 deletions
diff --git a/modules/git/repo.go b/modules/git/repo.go index f86c4aae5c..8355f8811f 100644 --- a/modules/git/repo.go +++ b/modules/git/repo.go @@ -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 +} diff --git a/modules/repofiles/commit.go b/modules/repofiles/commit.go new file mode 100644 index 0000000000..371e6cf3ab --- /dev/null +++ b/modules/repofiles/commit.go @@ -0,0 +1,19 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package repofiles + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/git" +) + +// CountDivergingCommits determines how many commits a branch is ahead or behind the repository's base branch +func CountDivergingCommits(repo *models.Repository, branch string) (*git.DivergeObject, error) { + divergence, err := git.GetDivergingCommits(repo.RepoPath(), repo.DefaultBranch, branch) + if err != nil { + return nil, err + } + return &divergence, nil +} diff --git a/modules/templates/helper.go b/modules/templates/helper.go index 6d964e88a5..3176684d82 100644 --- a/modules/templates/helper.go +++ b/modules/templates/helper.go @@ -223,6 +223,13 @@ func NewFuncMap() []template.FuncMap { } return dict, nil }, + "percentage": func(n int, values ...int) float32 { + var sum = 0 + for i := 0; i < len(values); i++ { + sum += values[i] + } + return float32(n) * 100 / float32(sum) + }, }} } |