Skip to content
Snippets Groups Projects
Commit 8d987626 authored by Yorick Peterse's avatar Yorick Peterse
Browse files

Merge branch 'add-repository-find-branch' into 'master'

Add Repository#find_branch to speed up branch lookups

See merge request !119
parents 1d1d6642 6cb92440
No related branches found
No related tags found
1 merge request!119Add Repository#find_branch to speed up branch lookups
Pipeline #
v 10.5.0
- Add Repository#find_branch to speed up branch lookups
- Add Repository#reload_rugged to force a refresh of the Rugged repository
v 10.4.7
- Remove unneeded call to Repository#root_ref
 
Loading
Loading
Loading
Loading
@@ -70,6 +70,24 @@ module Gitlab
end.compact.sort_by(&:name)
end
 
def reload_rugged
@rugged = nil
end
# Directly find a branch with a simple name (e.g. master)
#
# force_reload causes a new Rugged repository to be instantiated
#
# This is to work around a bug in libgit2 that causes in-memory refs to
# be stale/invalid when packed-refs is changed.
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/15392#note_14538333
def find_branch(name, force_reload = false)
reload_rugged if force_reload
rugged_ref = rugged.branches[name]
Branch.new(self, rugged_ref.name, rugged_ref.target) if rugged_ref
end
def local_branches
rugged.branches.each(:local).map do |branch|
Branch.new(self, branch.name, branch.target)
Loading
Loading
Loading
Loading
@@ -813,6 +813,31 @@ describe Gitlab::Git::Repository do
end
end
 
describe '#find_branch' do
it 'should return a Branch for master' do
branch = repository.find_branch('master')
expect(branch).to be_a_kind_of(Gitlab::Git::Branch)
expect(branch.name).to eq('master')
end
it 'should handle non-existent branch' do
branch = repository.find_branch('this-is-garbage')
expect(branch).to eq(nil)
end
it 'should reload Rugged::Repository and return master' do
expect(Rugged::Repository).to receive(:new).twice.and_call_original
branch = repository.find_branch('master')
branch = repository.find_branch('master', force_reload: true)
expect(branch).to be_a_kind_of(Gitlab::Git::Branch)
expect(branch.name).to eq('master')
end
end
describe '#branches with deleted branch' do
before(:each) do
ref = double()
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment