Implement search of code via git grep

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
This commit is contained in:
Dmitriy Zaporozhets 2015-07-10 17:50:50 +02:00
parent 77025ad8e3
commit f6609c9882
No known key found for this signature in database
GPG Key ID: 161B5D6A44D3D88A
2 changed files with 35 additions and 0 deletions

View File

@ -431,6 +431,40 @@ class Repository
end
end
def search_files(query, ref)
offset = 2
args = %W(git grep -i -n --before-context #{offset} --after-context #{offset} #{query} #{ref || root_ref})
Gitlab::Popen.popen(args, path_to_repo).first.scrub.split(/^--$/)
end
def search_to_blob(result)
ref = nil
filename = nil
startline = 0
lines = result.lstrip.lines
lines.each_with_index do |line, index|
if line =~ /^.*:.*:\d+:/
ref, filename, startline = line.split(':')
startline = startline.to_i - index
break
end
end
data = lines.map do |line|
line.sub(ref, '').sub(filename, '').sub(/^:-\d+-/, '').sub(/^::\d+:/, '')
end
data = data.join("")
OpenStruct.new(
filename: filename,
ref: ref,
startline: startline,
data: data
)
end
private
def cache

View File

@ -1,3 +1,4 @@
- blob = @project.repository.search_to_blob(blob)
.blob-result
.file-holder
.file-title