Backport search_helper changes from EE
This commit is contained in:
parent
eab72755cd
commit
6355041aff
6 changed files with 66 additions and 60 deletions
|
@ -30,6 +30,37 @@ module SearchHelper
|
|||
"Showing #{from} - #{to} of #{count} #{scope.humanize(capitalize: false)} for \"#{term}\""
|
||||
end
|
||||
|
||||
def parse_search_result(result)
|
||||
ref = nil
|
||||
filename = nil
|
||||
basename = nil
|
||||
startline = 0
|
||||
|
||||
result.each_line.each_with_index do |line, index|
|
||||
if line =~ /^.*:.*:\d+:/
|
||||
ref, filename, startline = line.split(':')
|
||||
startline = startline.to_i - index
|
||||
extname = Regexp.escape(File.extname(filename))
|
||||
basename = filename.sub(/#{extname}$/, '')
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
data = ""
|
||||
|
||||
result.each_line do |line|
|
||||
data << line.sub(ref, '').sub(filename, '').sub(/^:-\d+-/, '').sub(/^::\d+:/, '')
|
||||
end
|
||||
|
||||
OpenStruct.new(
|
||||
filename: filename,
|
||||
basename: basename,
|
||||
ref: ref,
|
||||
startline: startline,
|
||||
data: data
|
||||
)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Autocomplete results for various settings pages
|
||||
|
|
|
@ -990,37 +990,6 @@ class Repository
|
|||
Gitlab::Popen.popen(args, path_to_repo).first.scrub.split(/^--$/)
|
||||
end
|
||||
|
||||
def parse_search_result(result)
|
||||
ref = nil
|
||||
filename = nil
|
||||
basename = nil
|
||||
startline = 0
|
||||
|
||||
result.each_line.each_with_index do |line, index|
|
||||
if line =~ /^.*:.*:\d+:/
|
||||
ref, filename, startline = line.split(':')
|
||||
startline = startline.to_i - index
|
||||
extname = Regexp.escape(File.extname(filename))
|
||||
basename = filename.sub(/#{extname}$/, '')
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
data = ""
|
||||
|
||||
result.each_line do |line|
|
||||
data << line.sub(ref, '').sub(filename, '').sub(/^:-\d+-/, '').sub(/^::\d+:/, '')
|
||||
end
|
||||
|
||||
OpenStruct.new(
|
||||
filename: filename,
|
||||
basename: basename,
|
||||
ref: ref,
|
||||
startline: startline,
|
||||
data: data
|
||||
)
|
||||
end
|
||||
|
||||
def fetch_ref(source_path, source_ref, target_ref)
|
||||
args = %W(#{Gitlab.config.git.bin_path} fetch --no-tags -f #{source_path} #{source_ref}:#{target_ref})
|
||||
Gitlab::Popen.popen(args, path_to_repo)
|
||||
|
@ -1048,7 +1017,7 @@ class Repository
|
|||
|
||||
GitHooksService.new.execute(current_user, path_to_repo, oldrev, newrev, ref) do
|
||||
update_ref!(ref, newrev, oldrev)
|
||||
|
||||
|
||||
if was_empty || !target_branch
|
||||
# If repo was empty expire cache
|
||||
after_create if was_empty
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
- blob = @project.repository.parse_search_result(blob)
|
||||
- blob = parse_search_result(blob)
|
||||
.blob-result
|
||||
.file-holder
|
||||
.file-title
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
- wiki_blob = @project.repository.parse_search_result(wiki_blob)
|
||||
- wiki_blob = parse_search_result(wiki_blob)
|
||||
.blob-result
|
||||
.file-holder
|
||||
.file-title
|
||||
|
|
|
@ -6,6 +6,38 @@ describe SearchHelper do
|
|||
str
|
||||
end
|
||||
|
||||
describe 'parsing result' do
|
||||
let(:project) { create(:project) }
|
||||
let(:repository) { project.repository }
|
||||
let(:results) { repository.search_files('feature', 'master') }
|
||||
let(:search_result) { results.first }
|
||||
|
||||
subject { helper.parse_search_result(search_result) }
|
||||
|
||||
it "returns a valid OpenStruct object" do
|
||||
is_expected.to be_an OpenStruct
|
||||
expect(subject.filename).to eq('CHANGELOG')
|
||||
expect(subject.basename).to eq('CHANGELOG')
|
||||
expect(subject.ref).to eq('master')
|
||||
expect(subject.startline).to eq(186)
|
||||
expect(subject.data.lines[2]).to eq(" - Feature: Replace teams with group membership\n")
|
||||
end
|
||||
|
||||
context "when filename has extension" do
|
||||
let(:search_result) { "master:CONTRIBUTE.md:5:- [Contribute to GitLab](#contribute-to-gitlab)\n" }
|
||||
|
||||
it { expect(subject.filename).to eq('CONTRIBUTE.md') }
|
||||
it { expect(subject.basename).to eq('CONTRIBUTE') }
|
||||
end
|
||||
|
||||
context "when file under directory" do
|
||||
let(:search_result) { "master:a/b/c.md:5:a b c\n" }
|
||||
|
||||
it { expect(subject.filename).to eq('a/b/c.md') }
|
||||
it { expect(subject.basename).to eq('a/b/c') }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'search_autocomplete_source' do
|
||||
context "with no current user" do
|
||||
before do
|
||||
|
|
|
@ -186,32 +186,6 @@ describe Repository, models: true do
|
|||
it { is_expected.to be_an String }
|
||||
it { expect(subject.lines[2]).to eq("master:CHANGELOG:188: - Feature: Replace teams with group membership\n") }
|
||||
end
|
||||
|
||||
describe 'parsing result' do
|
||||
subject { repository.parse_search_result(search_result) }
|
||||
let(:search_result) { results.first }
|
||||
|
||||
it { is_expected.to be_an OpenStruct }
|
||||
it { expect(subject.filename).to eq('CHANGELOG') }
|
||||
it { expect(subject.basename).to eq('CHANGELOG') }
|
||||
it { expect(subject.ref).to eq('master') }
|
||||
it { expect(subject.startline).to eq(186) }
|
||||
it { expect(subject.data.lines[2]).to eq(" - Feature: Replace teams with group membership\n") }
|
||||
|
||||
context "when filename has extension" do
|
||||
let(:search_result) { "master:CONTRIBUTE.md:5:- [Contribute to GitLab](#contribute-to-gitlab)\n" }
|
||||
|
||||
it { expect(subject.filename).to eq('CONTRIBUTE.md') }
|
||||
it { expect(subject.basename).to eq('CONTRIBUTE') }
|
||||
end
|
||||
|
||||
context "when file under directory" do
|
||||
let(:search_result) { "master:a/b/c.md:5:a b c\n" }
|
||||
|
||||
it { expect(subject.filename).to eq('a/b/c.md') }
|
||||
it { expect(subject.basename).to eq('a/b/c') }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "#changelog" do
|
||||
|
|
Loading…
Reference in a new issue