2017-11-10 14:57:11 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-06-26 14:09:03 -04:00
|
|
|
# A collection of Commit instances for a specific container and Git reference.
|
2017-11-10 14:57:11 -05:00
|
|
|
class CommitCollection
|
|
|
|
include Enumerable
|
2019-02-06 07:33:11 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2017-11-10 14:57:11 -05:00
|
|
|
|
2020-02-04 10:08:40 -05:00
|
|
|
attr_reader :container, :ref, :commits
|
2017-11-10 14:57:11 -05:00
|
|
|
|
2020-06-26 14:09:03 -04:00
|
|
|
delegate :repository, to: :container, allow_nil: true
|
|
|
|
delegate :project, to: :repository, allow_nil: true
|
|
|
|
|
|
|
|
# container - The object the commits belong to.
|
2017-11-10 14:57:11 -05:00
|
|
|
# commits - The Commit instances to store.
|
|
|
|
# ref - The name of the ref (e.g. "master").
|
2020-02-04 10:08:40 -05:00
|
|
|
def initialize(container, commits, ref = nil)
|
|
|
|
@container = container
|
2017-11-10 14:57:11 -05:00
|
|
|
@commits = commits
|
|
|
|
@ref = ref
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
commits.each(&block)
|
|
|
|
end
|
|
|
|
|
2019-04-02 07:01:07 -04:00
|
|
|
def committers
|
|
|
|
emails = without_merge_commits.map(&:committer_email).uniq
|
2019-01-03 05:59:14 -05:00
|
|
|
|
|
|
|
User.by_any_email(emails)
|
|
|
|
end
|
|
|
|
|
2019-02-06 07:33:11 -05:00
|
|
|
def without_merge_commits
|
|
|
|
strong_memoize(:without_merge_commits) do
|
2019-03-14 00:20:40 -04:00
|
|
|
# `#enrich!` the collection to ensure all commits contain
|
|
|
|
# the necessary parent data
|
|
|
|
enrich!.commits.reject(&:merge_commit?)
|
2019-02-06 07:33:11 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-30 11:08:09 -04:00
|
|
|
# Returns the collection with the latest pipeline for every commit pre-set.
|
|
|
|
#
|
|
|
|
# Setting the pipeline for each commit ahead of time removes the need for running
|
|
|
|
# a query for every commit we're displaying.
|
|
|
|
def with_latest_pipeline(ref = nil)
|
2020-06-26 14:09:03 -04:00
|
|
|
return self unless project
|
|
|
|
|
|
|
|
pipelines = project.ci_pipelines.latest_pipeline_per_commit(map(&:id), ref)
|
2019-09-30 11:08:09 -04:00
|
|
|
|
|
|
|
each do |commit|
|
2020-06-26 14:09:03 -04:00
|
|
|
commit.set_latest_pipeline_for_ref(ref, pipelines[commit.id])
|
2019-09-30 11:08:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2019-03-14 00:20:40 -04:00
|
|
|
def unenriched
|
|
|
|
commits.reject(&:gitaly_commit?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fully_enriched?
|
|
|
|
unenriched.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Batch load any commits that are not backed by full gitaly data, and
|
|
|
|
# replace them in the collection.
|
|
|
|
def enrich!
|
2020-02-04 10:08:40 -05:00
|
|
|
# A container is needed in order to fetch data from gitaly. Containers
|
2019-03-19 19:07:51 -04:00
|
|
|
# can be absent from commits in certain rare situations (like when
|
|
|
|
# viewing a MR of a deleted fork). In these cases, assume that the
|
|
|
|
# enriched data is not needed.
|
2020-06-26 14:09:03 -04:00
|
|
|
return self if container.blank? || fully_enriched?
|
|
|
|
|
|
|
|
# Batch load full Commits from the repository
|
|
|
|
# and map to a Hash of id => Commit
|
|
|
|
replacements = Hash[unenriched.map do |c|
|
|
|
|
[c.id, Commit.lazy(container, c.id)]
|
2019-03-14 00:20:40 -04:00
|
|
|
end.compact]
|
|
|
|
|
|
|
|
# Replace the commits, keeping the same order
|
2019-10-02 20:05:59 -04:00
|
|
|
@commits = @commits.map do |original_commit|
|
|
|
|
# Return the original instance: if it didn't need to be batchloaded, it was
|
|
|
|
# already enriched.
|
|
|
|
batch_loaded_commit = replacements.fetch(original_commit.id, original_commit)
|
|
|
|
|
|
|
|
# If batch loading the commit failed, fall back to the original commit.
|
|
|
|
# We need to explicitly check `.nil?` since otherwise a `BatchLoader` instance
|
|
|
|
# that looks like `nil` is returned.
|
|
|
|
batch_loaded_commit.nil? ? original_commit : batch_loaded_commit
|
2019-03-14 00:20:40 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2017-11-10 14:57:11 -05:00
|
|
|
def respond_to_missing?(message, inc_private = false)
|
|
|
|
commits.respond_to?(message, inc_private)
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
def method_missing(message, *args, &block)
|
|
|
|
commits.public_send(message, *args, &block)
|
|
|
|
end
|
|
|
|
end
|