diff --git a/lib/grit/repo.rb b/lib/grit/repo.rb index 3d948fab..071b8e31 100644 --- a/lib/grit/repo.rb +++ b/lib/grit/repo.rb @@ -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 @@ -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! diff --git a/test/test_repo.rb b/test/test_repo.rb index d15ea6d2..5810fc4a 100644 --- a/test/test_repo.rb +++ b/test/test_repo.rb @@ -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