From fe60ca408b7bbec43bdba18d0eded2ed586ddfdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=A7=88=EB=88=84=EC=97=98?= Date: Wed, 27 Jul 2016 10:57:32 +0200 Subject: [PATCH] routers/repo/release: Use correct branch reference (#3330) When calculating the current behind commit count the calculation should use the current release target to get the total commit count. Should the release target not exist anymore the calculation will return zero for the newest release on that target. Older releases on that target will then use that calculated commit count as reference. The only use case that is now somehow invalid is when the release target was merged / deleted after a tag on that release target: master 1 - - - - - - - 6 \ / branch 2 - 3 - 4 - 5 When `4` is the last tag on branch `branch` and the branch `branch` is not yet deleted the calculated numbers would be: 1 commits to branch since this release Now if the branch `branch` gets deleted the calculation function will not find the branch and use the commit count of the newest release (`4`) as reference resulting in: 0 commit to branch since this release This fixes #3326 --- routers/repo/release.go | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/routers/repo/release.go b/routers/repo/release.go index 0df4a1f1d940..672741c5ba1d 100644 --- a/routers/repo/release.go +++ b/routers/repo/release.go @@ -20,7 +20,7 @@ const ( RELEASE_NEW base.TplName = "repo/release/new" ) -// calReleaseNumCommitsBehind calculates given release has how many commits behind default branch. +// calReleaseNumCommitsBehind calculates given release has how many commits behind release target. func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Release, countCache map[string]int64) error { // Fast return if release target is same as default branch. if repoCtx.BranchName == release.Target { @@ -30,16 +30,21 @@ func calReleaseNumCommitsBehind(repoCtx *context.Repository, release *models.Rel // Get count if not exists if _, ok := countCache[release.Target]; !ok { - commit, err := repoCtx.GitRepo.GetBranchCommit(repoCtx.BranchName) - if err != nil { - return fmt.Errorf("GetBranchCommit: %v", err) - } - countCache[repoCtx.BranchName], err = commit.CommitsCount() - if err != nil { - return fmt.Errorf("CommitsCount: %v", err) + if repoCtx.GitRepo.IsBranchExist(release.Target) { + commit, err := repoCtx.GitRepo.GetBranchCommit(release.Target) + if err != nil { + return fmt.Errorf("GetBranchCommit: %v", err) + } + countCache[release.Target], err = commit.CommitsCount() + if err != nil { + return fmt.Errorf("CommitsCount: %v", err) + } + } else { + // Use NumCommits of the newest release on that target + countCache[release.Target] = release.NumCommits } } - release.NumCommitsBehind = countCache[repoCtx.BranchName] - release.NumCommits + release.NumCommitsBehind = countCache[release.Target] - release.NumCommits return nil }