From 46e315c3218ffda7f707c16000ce442ee6f3646f Mon Sep 17 00:00:00 2001 From: Bryce Lanham Date: Thu, 23 Feb 2023 07:48:53 -0600 Subject: [PATCH 1/2] grep: multiple path_limiters and extended_regexp Signed-off-by: Bryce Lanham --- lib/git/lib.rb | 4 +++- tests/units/test_lib.rb | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/git/lib.rb b/lib/git/lib.rb index 92172ed7..82156eda 100644 --- a/lib/git/lib.rb +++ b/lib/git/lib.rb @@ -413,10 +413,12 @@ def grep(string, opts = {}) grep_opts = ['-n'] grep_opts << '-i' if opts[:ignore_case] grep_opts << '-v' if opts[:invert_match] + grep_opts << '-E' if opts[:extended_regexp] grep_opts << '-e' grep_opts << string grep_opts << opts[:object] if opts[:object].is_a?(String) - grep_opts << '--' << opts[:path_limiter] if opts[:path_limiter].is_a? String + grep_opts.push('--', opts[:path_limiter]) if opts[:path_limiter].is_a?(String) + grep_opts.push('--', *opts[:path_limiter]) if opts[:path_limiter].is_a?(Array) hsh = {} command_lines('grep', *grep_opts).each do |line| diff --git a/tests/units/test_lib.rb b/tests/units/test_lib.rb index fdeeba06..577d7d73 100644 --- a/tests/units/test_lib.rb +++ b/tests/units/test_lib.rb @@ -291,6 +291,12 @@ def test_grep assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1]) assert_equal(1, match.size) + match = @lib.grep('search', :object => 'gitsearch1', :path_limiter => ['scott/new*', 'scott/text.*']) + assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1]) + assert_equal('to search one', match['gitsearch1:scott/text.txt'].assoc(6)[1]) + assert_equal(2, match['gitsearch1:scott/text.txt'].size) + assert_equal(2, match.size) + match = @lib.grep('SEARCH', :object => 'gitsearch1') assert_equal(0, match.size) @@ -302,6 +308,11 @@ def test_grep assert_equal(6, match['gitsearch1:scott/text.txt'].size) assert_equal(2, match.size) + match = @lib.grep("you can't search me!|nothing!", :object => 'gitsearch1', :extended_regexp => true) + assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1]) + assert_equal("nothing!", match["gitsearch1:scott/text.txt"].first[1]) + assert_equal(2, match.size) + match = @lib.grep('Grep', :object => 'grep_colon_numbers') assert_equal("Grep regex doesn't like this:4342: because it is bad", match['grep_colon_numbers:colon_numbers.txt'].first[1]) assert_equal(1, match.size) From 60758f36a3e2804ff4330f4f534aa19f37b6dd48 Mon Sep 17 00:00:00 2001 From: James Couball Date: Thu, 23 Feb 2023 16:12:12 -0800 Subject: [PATCH 2/2] Document Base#grep parameters Signed-off-by: James Couball --- lib/git/base.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/git/base.rb b/lib/git/base.rb index bddab413..8e77403a 100644 --- a/lib/git/base.rb +++ b/lib/git/base.rb @@ -209,6 +209,15 @@ def lib # end # end # + # @param string [String] the string to search for + # @param path_limiter [String, Array] a path or array of paths to limit the search to or nil for no limit + # @param opts [Hash] options to pass to the underlying `git grep` command + # + # @option opts [Boolean] :ignore_case (false) ignore case when matching + # @option opts [Boolean] :invert_match (false) select non-matching lines + # @option opts [Boolean] :extended_regexp (false) use extended regular expressions + # @option opts [String] :object (HEAD) the object to search from + # # @return [Hash] a hash of arrays # ```Ruby # {