summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzegorz@gitlab.com>2019-07-01 08:38:16 +0000
committerGrzegorz Bizon <grzegorz@gitlab.com>2019-07-01 08:38:16 +0000
commit8775e4a1faf13a01451e71ea9ef729dc52e6d3c1 (patch)
tree8632c5465f6b3dee57bb0ccd64f5162c35b8ab15 /app/services
parentf63dc06cb4572db92bb91c936e9862c55f1f365e (diff)
parentca5cd7b7fb5108d30d0f6b74e31da736024592dd (diff)
downloadgitlab-ce-8775e4a1faf13a01451e71ea9ef729dc52e6d3c1.tar.gz
Merge branch 'id-stale-branches' into 'master'
Add endpoint for fetching diverging commit counts See merge request gitlab-org/gitlab-ce!29802
Diffstat (limited to 'app/services')
-rw-r--r--app/services/branches/diverging_commit_counts_service.rb54
1 files changed, 54 insertions, 0 deletions
diff --git a/app/services/branches/diverging_commit_counts_service.rb b/app/services/branches/diverging_commit_counts_service.rb
new file mode 100644
index 00000000000..f947cec1663
--- /dev/null
+++ b/app/services/branches/diverging_commit_counts_service.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+module Branches
+ class DivergingCommitCountsService
+ def initialize(repository)
+ @repository = repository
+ @cache = Gitlab::RepositoryCache.new(repository)
+ end
+
+ def call(branch)
+ if Feature.enabled?('gitaly_count_diverging_commits_no_max')
+ diverging_commit_counts_without_max(branch)
+ else
+ diverging_commit_counts(branch)
+ end
+ end
+
+ private
+
+ attr_reader :repository, :cache
+
+ delegate :raw_repository, to: :repository
+
+ def diverging_commit_counts(branch)
+ ## TODO: deprecate the below code after 12.0
+ @root_ref_hash ||= raw_repository.commit(repository.root_ref).id
+ cache.fetch(:"diverging_commit_counts_#{branch.name}") do
+ number_commits_behind, number_commits_ahead =
+ repository.raw_repository.diverging_commit_count(
+ @root_ref_hash,
+ branch.dereferenced_target.sha,
+ max_count: Repository::MAX_DIVERGING_COUNT)
+
+ if number_commits_behind + number_commits_ahead >= Repository::MAX_DIVERGING_COUNT
+ { distance: Repository::MAX_DIVERGING_COUNT }
+ else
+ { behind: number_commits_behind, ahead: number_commits_ahead }
+ end
+ end
+ end
+
+ def diverging_commit_counts_without_max(branch)
+ @root_ref_hash ||= raw_repository.commit(repository.root_ref).id
+ cache.fetch(:"diverging_commit_counts_without_max_#{branch.name}") do
+ number_commits_behind, number_commits_ahead =
+ raw_repository.diverging_commit_count(
+ @root_ref_hash,
+ branch.dereferenced_target.sha)
+
+ { behind: number_commits_behind, ahead: number_commits_ahead }
+ end
+ end
+ end
+end