Skip to content
This repository has been archived by the owner on Apr 19, 2018. It is now read-only.

Commit

Permalink
Merge 56a3f33 into 42297cd
Browse files Browse the repository at this point in the history
  • Loading branch information
jacargentina committed Apr 15, 2013
2 parents 42297cd + 56a3f33 commit a9d1fcb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/grit.rb
Expand Up @@ -47,6 +47,7 @@
require 'grit/submodule'
require 'grit/blame'
require 'grit/merge'
require 'grit/grep'

module Grit
VERSION = '2.5.0'
Expand Down
1 change: 1 addition & 0 deletions lib/grit/git.rb
Expand Up @@ -335,6 +335,7 @@ def native(cmd, options = {}, *args, &block)
argv << Git.git_binary
argv << "--git-dir=#{git_dir}" if base
argv << cmd.to_s.tr('_', '-')
argv << "--no-color"
argv.concat(options_to_argv(options))
argv.concat(args)

Expand Down
21 changes: 21 additions & 0 deletions lib/grit/grep.rb
@@ -0,0 +1,21 @@
module Grit

class Grep

attr_reader :repo
attr_reader :filename
attr_reader :line
attr_reader :text
attr_reader :is_binary

def initialize(repo, filename, line, text, is_binary)
@repo = repo
@filename = filename
@line = line.to_i
@text = text
@is_binary = is_binary
end

end

end
19 changes: 19 additions & 0 deletions lib/grit/repo.rb
Expand Up @@ -713,6 +713,25 @@ def rename(name)
end
end

def grep(searchtext, branch = 'master')
result = git.native(:grep, {}, '-n', '-E', '-i', '-z', searchtext, branch)
greps = []
lines = result.split("\n")
lines.each do |line|
line.chomp!
file = line[/^Binary file (.+) matches$/]
binary = false
if file
binary = true
else
file, lno, ltext = line.split("\0", 3)
file[/^#{branch}:/] = ""
end
greps << Grit::Grep.new(self, file, lno, ltext, binary)
end
greps
end

# Pretty object inspection
def inspect
%Q{#<Grit::Repo "#{@path}">}
Expand Down
8 changes: 8 additions & 0 deletions test/test_repo.rb
Expand Up @@ -416,4 +416,12 @@ def test_select_existing_objects
after = ['634396b2f541a9f2d58b00be1a07f0c358b999b3']
assert_equal after, @r.git.select_existing_objects(before)
end

def test_grep
res = @r.grep('def test_select_existing_objects', 'master')
assert_equal 1, res.length
assert_equal 414, res.first.line
assert_equal 'test/test_repo.rb', res.first.filename
assert_equal ' def test_select_existing_objects', res.first.text
end
end

0 comments on commit a9d1fcb

Please sign in to comment.