2023-03-04 22:38:38 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Projects
|
|
|
|
module Forks
|
|
|
|
# Class for calculating the divergence of a fork with the source project
|
2023-05-27 22:25:52 +05:30
|
|
|
class Details
|
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
LATEST_COMMITS_COUNT = 10
|
2023-05-27 22:25:52 +05:30
|
|
|
LEASE_TIMEOUT = 15.minutes.to_i
|
2023-03-04 22:38:38 +05:30
|
|
|
EXPIRATION_TIME = 8.hours
|
|
|
|
|
|
|
|
def initialize(project, ref)
|
|
|
|
@project = project
|
|
|
|
@fork_repo = project.repository
|
|
|
|
@source_repo = project.fork_source.repository
|
|
|
|
@ref = ref
|
|
|
|
end
|
|
|
|
|
|
|
|
def counts
|
|
|
|
ahead, behind = divergence_counts
|
|
|
|
|
|
|
|
{ ahead: ahead, behind: behind }
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def exclusive_lease
|
|
|
|
key = ['project_details', project.id, ref].join(':')
|
|
|
|
uuid = Gitlab::ExclusiveLease.get_uuid(key)
|
|
|
|
|
|
|
|
Gitlab::ExclusiveLease.new(key, uuid: uuid, timeout: LEASE_TIMEOUT)
|
|
|
|
end
|
|
|
|
strong_memoize_attr :exclusive_lease
|
|
|
|
|
|
|
|
def syncing?
|
|
|
|
exclusive_lease.exists?
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_conflicts?
|
|
|
|
!(attrs && attrs[:has_conflicts]).nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def update!(params)
|
|
|
|
Rails.cache.write(cache_key, params, expires_in: EXPIRATION_TIME)
|
|
|
|
|
|
|
|
@attrs = nil
|
|
|
|
end
|
|
|
|
|
2023-03-04 22:38:38 +05:30
|
|
|
private
|
|
|
|
|
|
|
|
attr_reader :project, :fork_repo, :source_repo, :ref
|
|
|
|
|
|
|
|
def cache_key
|
2023-05-27 22:25:52 +05:30
|
|
|
@cache_key ||= ['project_fork_details', project.id, ref].join(':')
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def divergence_counts
|
2023-05-27 22:25:52 +05:30
|
|
|
sha = fork_repo.commit(ref)&.sha
|
|
|
|
source_sha = source_repo.commit&.sha
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
return if sha.blank? || source_sha.blank?
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
return attrs[:counts] if attrs.present? && attrs[:source_sha] == source_sha && attrs[:sha] == sha
|
2023-03-04 22:38:38 +05:30
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
counts = calculate_divergence_counts(sha, source_sha)
|
|
|
|
|
|
|
|
update!({ sha: sha, source_sha: source_sha, counts: counts })
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
counts
|
|
|
|
end
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
def calculate_divergence_counts(sha, source_sha)
|
2023-03-04 22:38:38 +05:30
|
|
|
# If the upstream latest commit exists in the fork repo, then
|
|
|
|
# it's possible to calculate divergence counts within the fork repository.
|
2023-05-27 22:25:52 +05:30
|
|
|
return fork_repo.diverging_commit_count(sha, source_sha) if fork_repo.commit(source_sha)
|
2023-03-04 22:38:38 +05:30
|
|
|
|
|
|
|
# Otherwise, we need to find a commit that exists both in the fork and upstream
|
|
|
|
# in order to use this commit as a base for calculating divergence counts.
|
|
|
|
# Considering the fact that a user usually creates a fork to contribute to the upstream,
|
|
|
|
# it is expected that they have a limited number of commits ahead of upstream.
|
|
|
|
# Let's take the latest N commits and check their existence upstream.
|
|
|
|
last_commits_shas = fork_repo.commits(ref, limit: LATEST_COMMITS_COUNT).map(&:sha)
|
|
|
|
existence_hash = source_repo.check_objects_exist(last_commits_shas)
|
|
|
|
first_matched_commit_sha = last_commits_shas.find { |sha| existence_hash[sha] }
|
|
|
|
|
|
|
|
# If we can't find such a commit, we return early and tell the user that the branches
|
|
|
|
# have diverged and action is required.
|
|
|
|
return unless first_matched_commit_sha
|
|
|
|
|
|
|
|
# Otherwise, we use upstream to calculate divergence counts from the matched commit
|
|
|
|
ahead, behind = source_repo.diverging_commit_count(first_matched_commit_sha, source_sha)
|
|
|
|
# And add the number of commits a fork is ahead of the first matched commit
|
|
|
|
ahead += last_commits_shas.index(first_matched_commit_sha)
|
|
|
|
|
|
|
|
[ahead, behind]
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
def attrs
|
|
|
|
@attrs ||= Rails.cache.read(cache_key)
|
|
|
|
end
|
2023-03-04 22:38:38 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|