Skip to content

Commit 0f7c4a5

Browse files
authored
Allow the use of an array of path_limiters and add extended_regexp option to grep (#624)
* grep: multiple path_limiters and extended_regexp Signed-off-by: Bryce Lanham <blanham@gmail.com> * Document Base#grep parameters Signed-off-by: James Couball <jcouball@yahoo.com>
1 parent 8992701 commit 0f7c4a5

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/git/base.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,15 @@ def lib
209209
# end
210210
# end
211211
#
212+
# @param string [String] the string to search for
213+
# @param path_limiter [String, Array] a path or array of paths to limit the search to or nil for no limit
214+
# @param opts [Hash] options to pass to the underlying `git grep` command
215+
#
216+
# @option opts [Boolean] :ignore_case (false) ignore case when matching
217+
# @option opts [Boolean] :invert_match (false) select non-matching lines
218+
# @option opts [Boolean] :extended_regexp (false) use extended regular expressions
219+
# @option opts [String] :object (HEAD) the object to search from
220+
#
212221
# @return [Hash<String, Array>] a hash of arrays
213222
# ```Ruby
214223
# {

lib/git/lib.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,12 @@ def grep(string, opts = {})
413413
grep_opts = ['-n']
414414
grep_opts << '-i' if opts[:ignore_case]
415415
grep_opts << '-v' if opts[:invert_match]
416+
grep_opts << '-E' if opts[:extended_regexp]
416417
grep_opts << '-e'
417418
grep_opts << string
418419
grep_opts << opts[:object] if opts[:object].is_a?(String)
419-
grep_opts << '--' << opts[:path_limiter] if opts[:path_limiter].is_a? String
420+
grep_opts.push('--', opts[:path_limiter]) if opts[:path_limiter].is_a?(String)
421+
grep_opts.push('--', *opts[:path_limiter]) if opts[:path_limiter].is_a?(Array)
420422

421423
hsh = {}
422424
command_lines('grep', *grep_opts).each do |line|

tests/units/test_lib.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ def test_grep
291291
assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
292292
assert_equal(1, match.size)
293293

294+
match = @lib.grep('search', :object => 'gitsearch1', :path_limiter => ['scott/new*', 'scott/text.*'])
295+
assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
296+
assert_equal('to search one', match['gitsearch1:scott/text.txt'].assoc(6)[1])
297+
assert_equal(2, match['gitsearch1:scott/text.txt'].size)
298+
assert_equal(2, match.size)
299+
294300
match = @lib.grep('SEARCH', :object => 'gitsearch1')
295301
assert_equal(0, match.size)
296302

@@ -302,6 +308,11 @@ def test_grep
302308
assert_equal(6, match['gitsearch1:scott/text.txt'].size)
303309
assert_equal(2, match.size)
304310

311+
match = @lib.grep("you can't search me!|nothing!", :object => 'gitsearch1', :extended_regexp => true)
312+
assert_equal("you can't search me!", match["gitsearch1:scott/newfile"].first[1])
313+
assert_equal("nothing!", match["gitsearch1:scott/text.txt"].first[1])
314+
assert_equal(2, match.size)
315+
305316
match = @lib.grep('Grep', :object => 'grep_colon_numbers')
306317
assert_equal("Grep regex doesn't like this:4342: because it is bad", match['grep_colon_numbers:colon_numbers.txt'].first[1])
307318
assert_equal(1, match.size)

0 commit comments

Comments
 (0)