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

fix grep while it matches in binary files #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/grit/repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -718,11 +718,13 @@ def grep(searchtext, contextlines = 3, branch = 'master')
context_arg = '-C ' + contextlines.to_s
result = git.native(:grep, {pipeline: false}, '-n', '-E', '-i', '-z', '--heading', '--break', context_arg, searchtext, branch).encode('UTF-8', invalid: :replace, undef: :replace, replace: '')
greps = []
result.gsub!(/(^Binary file (.+) matches$)/, "\n\\1\n")
result.strip!
filematches = result.split("\n\n")
filematches.each do |filematch|
binary = false
file = ''
matches = filematch.split("--\n")
matches = filematch.strip.split("--\n")
matches.each_with_index do |match, i|
content = []
startline = 0
Expand All @@ -733,10 +735,10 @@ def grep(searchtext, contextlines = 3, branch = 'master')
file = text[/^Binary file (.+) matches$/]
if file
binary = true
else
text.slice! /^#{branch}:/
file = text
text = $1
end
text.slice! /^#{branch}:/
file = text
end
lines.each_with_index do |line, j|
line.chomp!
Expand Down
17 changes: 17 additions & 0 deletions test/test_repo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -424,4 +424,21 @@ def test_grep
assert_equal 'test/test_repo.rb', res.first.filename
assert_equal ' def test_select_existing_objects', res.first.content[1]
end

def test_grep_binary
res = @r.grep('origin/HEAD', 1, 'master')
assert_equal 5, res.length

assert_equal 874, res.first.startline
assert_equal 'test/dot_git/file-index', res.first.filename
assert_equal 'test/dot_git/refs/remotes/origin/HEAD', res.first.content[1]

assert_equal 0, res[1].startline
assert_equal 'test/dot_git/index', res[1].filename
assert_equal [], res[1].content

assert_equal 5, res[3].startline
assert_equal 'test/dot_git_iv2/info/refs', res[3].filename
assert_equal "ca8a30f5a7f0f163bbe3b6f0abf18a6c83b0687a\trefs/remotes/origin/HEAD", res[3].content[1]
end
end