Class: Gitlab::Git::MergeBase

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/git/merge_base.rb

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(repository, refs) ⇒ MergeBase

Returns a new instance of MergeBase.


8
9
10
# File 'lib/gitlab/git/merge_base.rb', line 8

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

Instance Method Details

#commitObject

Returns the merge base as a Gitlab::Git::Commit


24
25
26
27
28
# File 'lib/gitlab/git/merge_base.rb', line 24

def commit
  return unless sha

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

#shaObject

Returns the SHA of the first common ancestor


13
14
15
16
17
18
19
20
21
# File 'lib/gitlab/git/merge_base.rb', line 13

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

#unknown_refsObject

Returns the refs passed on initialization that aren't found in the repository, and thus cannot be used to find a merge base.


32
33
34
35
# File 'lib/gitlab/git/merge_base.rb', line 32

def unknown_refs
  @unknown_refs ||= Hash[@refs.zip(commits_for_refs)]
                      .select { |ref, commit| commit.nil? }.keys
end