Skip to content

grep: Allow the use of an array of path limiters and extended_regexp as an available option. #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2023
Merged
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
9 changes: 9 additions & 0 deletions lib/git/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Array>] a hash of arrays
# ```Ruby
# {
Expand Down
4 changes: 3 additions & 1 deletion lib/git/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
11 changes: 11 additions & 0 deletions tests/units/test_lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down