From 38e2fc1fb364cb08bc0b54f7d532a3a2d5735928 Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Thu, 15 Nov 2018 01:39:23 +0100 Subject: [PATCH 1/3] Show number of commits a branch is ahead or behind default --- options/locale/locale_en-US.ini | 5 ++++ routers/repo/view.go | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 4e2ae51538736..090dfaaae57f6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -712,6 +712,11 @@ commits.older = Older commits.newer = Newer commits.signed_by = Signed by commits.gpg_key_id = GPG Key ID +commits.count.singular.ahead_of_master = This branch is 1 commit ahead of %s. +commits.count.singular.behind_of_master = This branch is 1 commit behind %s. +commits.count.plural.ahead_of_master = This branch is %d commits ahead of %s. +commits.count.plural.behind_of_master = 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. diff --git a/routers/repo/view.go b/routers/repo/view.go index 3483a53a0d31f..8dd45444d2294 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -446,6 +446,56 @@ 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 { + + defaultBranch := ctx.Repo.Repository.DefaultBranch + + master, err := ctx.Repo.Repository.GetBranch(defaultBranch) + if err != nil { + ctx.ServerError("GetBranch", err) + return + } + + commit, err := master.GetCommit() + if err != nil { + ctx.ServerError("GetCommit", err) + return + } + + commitCountInMaster, err := commit.CommitsCount() + if err != nil { + ctx.ServerError("CommitsCount", err) + return + } + + n := commitCountInMaster - ctx.Repo.CommitsCount + + var msg string + + if n == 0 { + msg = ctx.Tr("repo.commits.count.even", defaultBranch) + } + + if n == -1 { + msg = ctx.Tr("repo.commits.count.singular.ahead_of_master", defaultBranch) + } + + if n == 1 { + msg = ctx.Tr("repo.commits.count.singular.behind_of_master", defaultBranch) + } + + if n < -1 { + msg = ctx.Tr("repo.commits.count.plural.ahead_of_master", n, defaultBranch) + } + + if n > 1 { + msg = ctx.Tr("repo.commits.count.plural.behind_of_master", n, defaultBranch) + } + + ctx.Flash.Info(msg) + } + ctx.HTML(200, tplRepoHome) } From 8903f23ca230579d558011700ab184294512e74c Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Sun, 5 May 2019 18:19:38 +0100 Subject: [PATCH 2/3] simplify translation and switch to reusable function --- options/locale/locale_en-US.ini | 8 +++---- routers/repo/view.go | 41 ++++++++------------------------- 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 090dfaaae57f6..9dfe50b93e7e6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -712,10 +712,10 @@ commits.older = Older commits.newer = Newer commits.signed_by = Signed by commits.gpg_key_id = GPG Key ID -commits.count.singular.ahead_of_master = This branch is 1 commit ahead of %s. -commits.count.singular.behind_of_master = This branch is 1 commit behind %s. -commits.count.plural.ahead_of_master = This branch is %d commits ahead of %s. -commits.count.plural.behind_of_master = This branch is %d commits behind %s. +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 diff --git a/routers/repo/view.go b/routers/repo/view.go index 8dd45444d2294..0db6ced4d395f 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -22,6 +22,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" "code.gitea.io/gitea/modules/templates" ) @@ -449,48 +450,24 @@ func renderCode(ctx *context.Context) { if ctx.Repo.BranchName != ctx.Repo.Repository.DefaultBranch { - defaultBranch := ctx.Repo.Repository.DefaultBranch - - master, err := ctx.Repo.Repository.GetBranch(defaultBranch) - if err != nil { - ctx.ServerError("GetBranch", err) - return - } - - commit, err := master.GetCommit() - if err != nil { - ctx.ServerError("GetCommit", err) - return - } - - commitCountInMaster, err := commit.CommitsCount() + divergence, err := repofiles.CountDivergingCommits(ctx.Repo.Repository, ctx.Repo.BranchName) if err != nil { - ctx.ServerError("CommitsCount", err) + ctx.ServerError("CountDivergingCommits", err) return } - n := commitCountInMaster - ctx.Repo.CommitsCount - var msg string - if n == 0 { - msg = ctx.Tr("repo.commits.count.even", defaultBranch) - } - - if n == -1 { - msg = ctx.Tr("repo.commits.count.singular.ahead_of_master", defaultBranch) - } - - if n == 1 { - msg = ctx.Tr("repo.commits.count.singular.behind_of_master", defaultBranch) + if divergence.Ahead == 0 && divergence.Behind == 0 { + msg = ctx.Tr("repo.commits.count.even", ctx.Repo.Repository.DefaultBranch) } - if n < -1 { - msg = ctx.Tr("repo.commits.count.plural.ahead_of_master", n, defaultBranch) + if divergence.Ahead > 0 { + msg = ctx.Tr(templates.TrN(ctx.Language(), divergence.Ahead, "repo.commits.count.ahead_1", "repo.commits.count.ahead_n"), ctx.Repo.Repository.DefaultBranch) } - if n > 1 { - msg = ctx.Tr("repo.commits.count.plural.behind_of_master", n, defaultBranch) + if divergence.Behind > 0 { + msg = ctx.Tr(templates.TrN(ctx.Language(), divergence.Ahead, "repo.commits.count.behind_1", "repo.commits.count.behind_n"), ctx.Repo.Repository.DefaultBranch) } ctx.Flash.Info(msg) From ca36a183f32cce70bb65a97b9064932c5ab4e4b9 Mon Sep 17 00:00:00 2001 From: Lanre Adelowo Date: Sun, 5 May 2019 18:26:55 +0100 Subject: [PATCH 3/3] fix translation --- routers/repo/view.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/routers/repo/view.go b/routers/repo/view.go index 0db6ced4d395f..3db7cc06c220c 100644 --- a/routers/repo/view.go +++ b/routers/repo/view.go @@ -463,11 +463,19 @@ func renderCode(ctx *context.Context) { } if divergence.Ahead > 0 { - msg = ctx.Tr(templates.TrN(ctx.Language(), divergence.Ahead, "repo.commits.count.ahead_1", "repo.commits.count.ahead_n"), ctx.Repo.Repository.DefaultBranch) + 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 { - msg = ctx.Tr(templates.TrN(ctx.Language(), divergence.Ahead, "repo.commits.count.behind_1", "repo.commits.count.behind_n"), ctx.Repo.Repository.DefaultBranch) + 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)