diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-07-10 17:50:50 +0200 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-07-10 17:50:50 +0200 |
commit | f6609c9882c1d85b7f31072e6341fe5c91b37b41 (patch) | |
tree | e9f58390c7c4ac220406e9012f016dceeea6bca4 /app/models/repository.rb | |
parent | 77025ad8e33cc99c645febe9b51fbf9931a62bcf (diff) | |
download | gitlab-ce-f6609c9882c1d85b7f31072e6341fe5c91b37b41.tar.gz |
Implement search of code via git grepsearch-git-grep
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Diffstat (limited to 'app/models/repository.rb')
-rw-r--r-- | app/models/repository.rb | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index c767d1051d1..a7b990b1ba5 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -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 |