Commit 16dac73b authored by Tan Le's avatar Tan Le

Fix Ruby 2.7 keyword args on Repository model

This change improves the `method_missing` implementation.

- The `lookup` delegation is extracted out of `method_missing` to a
separate method and handle caching for `nil` value.
- Other method delegation is handled by `delegate_missing_to` which can
handle keyword arguments for both Ruby 2.7 and 3.0.
parent 1f1f7509
......@@ -24,10 +24,10 @@ class Repository
attr_accessor :full_path, :shard, :disk_path, :container, :repo_type
delegate :ref_name_for_sha, to: :raw_repository
delegate :bundle_to_disk, to: :raw_repository
delegate :lfs_enabled?, to: :container
delegate_missing_to :raw_repository
CreateTreeError = Class.new(StandardError)
AmbiguousRefError = Class.new(StandardError)
......@@ -386,10 +386,6 @@ class Repository
raw_repository.expire_has_local_branches_cache
end
def lookup_cache
@lookup_cache ||= {}
end
def expire_exists_cache
expire_method_caches(%i(exists?))
end
......@@ -494,19 +490,12 @@ class Repository
expire_branches_cache if expire_cache
end
def method_missing(msg, *args, &block)
if msg == :lookup && !block_given?
lookup_cache[msg] ||= {}
lookup_cache[msg][args.join(":")] ||= raw_repository.__send__(msg, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
else
raw_repository.__send__(msg, *args, &block) # rubocop:disable GitlabSecurity/PublicSend
def lookup(sha)
strong_memoize("lookup_#{sha}") do
raw_repository.lookup(sha)
end
end
def respond_to_missing?(method, include_private = false)
raw_repository.respond_to?(method, include_private) || super
end
def blob_at(sha, path)
blob = Blob.decorate(raw_repository.blob_at(sha, path), container)
......
......@@ -123,7 +123,7 @@ RSpec.describe Repository do
options = { message: 'test tag message\n',
tagger: { name: 'John Smith', email: 'john@gmail.com' } }
rugged_repo(repository).tags.create(annotated_tag_name, 'a48e4fc218069f68ef2e769dd8dfea3991362175', options)
rugged_repo(repository).tags.create(annotated_tag_name, 'a48e4fc218069f68ef2e769dd8dfea3991362175', **options)
double_first = double(committed_date: Time.current - 1.second)
double_last = double(committed_date: Time.current)
......@@ -2028,6 +2028,22 @@ RSpec.describe Repository do
end
end
describe '#lookup' do
before do
allow(repository.raw_repository).to receive(:lookup).and_return('interesting_blob')
end
it 'uses the lookup cache' do
2.times.each { repository.lookup('sha1') }
expect(repository.raw_repository).to have_received(:lookup).once
end
it 'returns the correct value' do
expect(repository.lookup('sha1')).to eq('interesting_blob')
end
end
describe '#after_create' do
it 'calls expire_status_cache' do
expect(repository).to receive(:expire_status_cache)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment