Skip to content

Show number of commits a branch is ahead or behind the default branch #5339

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 changes: 5 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,11 @@ commits.signed_by = Signed by
commits.signed_by_untrusted_user = Signed by untrusted user
commits.signed_by_untrusted_user_unmatched = Signed by untrusted user who does not match committer
commits.gpg_key_id = GPG Key ID
commits.count.ahead_1 = This branch is 1 commit ahead of %s.
commits.count.behind_1 = This branch is 1 commit behind %s.
commits.count.ahead_n = This branch is %d commits ahead of %s.
commits.count.behind_n = This branch is %d commits behind %s.
commits.count.even = This branch is even with %s.

ext_issues = Ext. Issues
ext_issues.desc = Link to an external issue tracker.
Expand Down
35 changes: 35 additions & 0 deletions routers/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/repofiles"
"code.gitea.io/gitea/modules/setting"
)

Expand Down Expand Up @@ -677,6 +678,40 @@ func renderCode(ctx *context.Context) {
ctx.Data["TreeLink"] = treeLink
ctx.Data["TreeNames"] = treeNames
ctx.Data["BranchLink"] = branchLink

if ctx.Repo.BranchName != ctx.Repo.Repository.DefaultBranch {

divergence, err := repofiles.CountDivergingCommits(ctx.Repo.Repository, ctx.Repo.BranchName)
if err != nil {
ctx.ServerError("CountDivergingCommits", err)
return
}

var msg string

if divergence.Ahead == 0 && divergence.Behind == 0 {
msg = ctx.Tr("repo.commits.count.even", ctx.Repo.Repository.DefaultBranch)
}

if divergence.Ahead > 0 {
if divergence.Ahead == 1 {
msg = ctx.Tr("repo.commits.count.ahead_1", ctx.Repo.Repository.DefaultBranch)
} else {
msg = ctx.Tr("repo.commits.count.ahead_n", divergence.Ahead, ctx.Repo.Repository.DefaultBranch)
}
}

if divergence.Behind > 0 {
if divergence.Behind == 1 {
msg = ctx.Tr("repo.commits.count.behind_1", ctx.Repo.Repository.DefaultBranch)
} else {
msg = ctx.Tr("repo.commits.count.behind_n", divergence.Behind, ctx.Repo.Repository.DefaultBranch)
}
}

ctx.Flash.Info(msg)
}

ctx.HTML(200, tplRepoHome)
}

Expand Down