Commit 32c5dfbc authored by Robert Speicher's avatar Robert Speicher

Merge branch 'optimise-extractsref-when-there-is-no-path' into 'master'

Optimise ExtractsRef when there is no slash

See merge request gitlab-org/gitlab!38343
parents 45d5c4d3 b2de3273
---
title: Remove some unnecessary Redis calls on commit lists
merge_request: 38343
author:
type: performance
......@@ -47,6 +47,10 @@ module ExtractsRef
if id =~ /^(\h{40})(.+)/
# If the ref appears to be a SHA, we're done, just split the string
pair = $~.captures
elsif id.exclude?('/')
# If the ID contains no slash, we must have a ref and no path, so
# we can skip the Redis calls below
pair = [id, '']
else
# Otherwise, attempt to detect the ref using a list of the repository_container's
# branches and tags
......@@ -71,12 +75,10 @@ module ExtractsRef
end
end
pair[0] = pair[0].strip
# Remove ending slashes from path
pair[1].gsub!(%r{^/|/$}, '')
pair
[
pair[0].strip,
pair[1].gsub(%r{^/|/$}, '') # Remove leading and trailing slashes from path
]
end
# Assigns common instance variables for views working with Git tree-ish objects
......
......@@ -88,9 +88,16 @@ RSpec.shared_examples 'extracts refs' do
expect(extract_ref('stable')).to eq(['stable', ''])
end
it 'extracts the longest matching ref' do
expect(extract_ref('release/app/v1.0.0/README.md')).to eq(
['release/app/v1.0.0', 'README.md'])
it 'does not fetch ref names when there is no slash' do
expect(self).not_to receive(:ref_names)
extract_ref('master')
end
it 'fetches ref names when there is a slash' do
expect(self).to receive(:ref_names).and_call_original
extract_ref('release/app/v1.0.0')
end
end
......@@ -113,6 +120,11 @@ RSpec.shared_examples 'extracts refs' do
it 'falls back to a primitive split for an invalid ref' do
expect(extract_ref('stable/CHANGELOG')).to eq(%w(stable CHANGELOG))
end
it 'extracts the longest matching ref' do
expect(extract_ref('release/app/v1.0.0/README.md')).to eq(
['release/app/v1.0.0', 'README.md'])
end
end
end
end
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