Welcome to mirror list, hosted at ThFree Co, Russian Federation.

merge_base.rb « git « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 905d72cadbf2342d6ed751ea3fea70edff971f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# frozen_string_literal: true

module Gitlab
  module Git
    class MergeBase
      include Gitlab::Utils::StrongMemoize

      def initialize(repository, refs)
        @repository = repository
        @refs = refs
      end

      # Returns the SHA of the first common ancestor
      def sha
        if unknown_refs.any?
          raise UnknownRef, "Can't find merge base for unknown refs: #{unknown_refs.inspect}"
        end

        strong_memoize(:sha) do
          @repository.merge_base(*commits_for_refs)
        end
      end

      # Returns the merge base as a Gitlab::Git::Commit
      def commit
        return unless sha

        @commit ||= @repository.commit_by(oid: sha)
      end

      # Returns the refs passed on initialization that aren't found in
      # the repository, and thus cannot be used to find a merge base.
      def unknown_refs
        @unknown_refs ||= Hash[@refs.zip(commits_for_refs)]
                            .select { |ref, commit| commit.nil? }.keys
      end

      private

      def commits_for_refs
        @commits_for_refs ||= @repository.commits_by(oids: @refs)
      end
    end
  end
end