Skip to content
Snippets Groups Projects
Commit e873bb84 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Merge pull request #19 from jacargentina/grep

Implemented grep support
parents 42297cdc cea676f1
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -47,6 +47,7 @@ require 'grit/status'
require 'grit/submodule'
require 'grit/blame'
require 'grit/merge'
require 'grit/grep'
 
module Grit
VERSION = '2.5.0'
Loading
Loading
module Grit
class Grep
attr_reader :repo
attr_reader :filename
attr_reader :startline
attr_reader :content
attr_reader :is_binary
def initialize(repo, filename, startline, content, is_binary)
@repo = repo
@filename = filename
@startline = startline.to_i
@content = content
@is_binary = is_binary
end
end
end
Loading
Loading
@@ -713,6 +713,44 @@ module Grit
end
end
 
def grep(searchtext, contextlines = 3, branch = 'master')
context_arg = '-C ' + contextlines.to_s
result = git.native(:grep, {}, '-n', '-E', '-i', '-z', '--heading', '--break', context_arg, searchtext, branch).force_encoding('UTF-8')
greps = []
filematches = result.split("\n\n")
filematches.each do |filematch|
binary = false
file = ''
matches = filematch.split("--\n")
matches.each_with_index do |match, i|
content = []
startline = 0
lines = match.split("\n")
if i == 0
text = lines.first
lines.slice!(0)
file = text[/^Binary file (.+) matches$/]
if file
binary = true
else
text.slice! /^#{branch}:/
file = text
end
end
lines.each_with_index do |line, j|
line.chomp!
number, text = line.split("\0", 2)
if j == 0
startline = number.to_i
end
content << text
end
greps << Grit::Grep.new(self, file, startline, content, binary)
end
end
greps
end
# Pretty object inspection
def inspect
%Q{#<Grit::Repo "#{@path}">}
Loading
Loading
Loading
Loading
@@ -416,4 +416,12 @@ class TestRepo < Test::Unit::TestCase
after = ['634396b2f541a9f2d58b00be1a07f0c358b999b3']
assert_equal after, @r.git.select_existing_objects(before)
end
def test_grep
res = @r.grep('def test_select_existing_objects', 1, 'master')
assert_equal 1, res.length
assert_equal 413, res.first.startline
assert_equal 'test/test_repo.rb', res.first.filename
assert_equal ' def test_select_existing_objects', res.first.content[1]
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment