Class: Gitlab::Git::MergeBase

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

Instance Method Summary collapse

Constructor Details

#initialize(repository, refs) ⇒ MergeBase

Returns a new instance of MergeBase.



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

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

Instance Method Details

#commitObject

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



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

def commit
  return unless sha

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

#shaObject

Returns the SHA of the first common ancestor



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

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.



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

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