Skip to content

Commit 38e2fc1

Browse files
committed
Show number of commits a branch is ahead or behind default
1 parent 55a8e12 commit 38e2fc1

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

options/locale/locale_en-US.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,11 @@ commits.older = Older
712712
commits.newer = Newer
713713
commits.signed_by = Signed by
714714
commits.gpg_key_id = GPG Key ID
715+
commits.count.singular.ahead_of_master = This branch is 1 commit ahead of %s.
716+
commits.count.singular.behind_of_master = This branch is 1 commit behind %s.
717+
commits.count.plural.ahead_of_master = This branch is %d commits ahead of %s.
718+
commits.count.plural.behind_of_master = This branch is %d commits behind %s.
719+
commits.count.even = This branch is even with %s.
715720
716721
ext_issues = Ext. Issues
717722
ext_issues.desc = Link to an external issue tracker.

routers/repo/view.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,56 @@ func renderCode(ctx *context.Context) {
446446
ctx.Data["TreeLink"] = treeLink
447447
ctx.Data["TreeNames"] = treeNames
448448
ctx.Data["BranchLink"] = branchLink
449+
450+
if ctx.Repo.BranchName != ctx.Repo.Repository.DefaultBranch {
451+
452+
defaultBranch := ctx.Repo.Repository.DefaultBranch
453+
454+
master, err := ctx.Repo.Repository.GetBranch(defaultBranch)
455+
if err != nil {
456+
ctx.ServerError("GetBranch", err)
457+
return
458+
}
459+
460+
commit, err := master.GetCommit()
461+
if err != nil {
462+
ctx.ServerError("GetCommit", err)
463+
return
464+
}
465+
466+
commitCountInMaster, err := commit.CommitsCount()
467+
if err != nil {
468+
ctx.ServerError("CommitsCount", err)
469+
return
470+
}
471+
472+
n := commitCountInMaster - ctx.Repo.CommitsCount
473+
474+
var msg string
475+
476+
if n == 0 {
477+
msg = ctx.Tr("repo.commits.count.even", defaultBranch)
478+
}
479+
480+
if n == -1 {
481+
msg = ctx.Tr("repo.commits.count.singular.ahead_of_master", defaultBranch)
482+
}
483+
484+
if n == 1 {
485+
msg = ctx.Tr("repo.commits.count.singular.behind_of_master", defaultBranch)
486+
}
487+
488+
if n < -1 {
489+
msg = ctx.Tr("repo.commits.count.plural.ahead_of_master", n, defaultBranch)
490+
}
491+
492+
if n > 1 {
493+
msg = ctx.Tr("repo.commits.count.plural.behind_of_master", n, defaultBranch)
494+
}
495+
496+
ctx.Flash.Info(msg)
497+
}
498+
449499
ctx.HTML(200, tplRepoHome)
450500
}
451501

0 commit comments

Comments
 (0)