Skip to content

Commit

Permalink
Fix bug when grepping lines that contain numbers surrounded by colons (
Browse files Browse the repository at this point in the history
…#566)

Signed-off-by: James Fairbairn <jamesaf112@gmail.com>
  • Loading branch information
4-Eyes committed Apr 13, 2022
1 parent c04d16e commit 018d919
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/git/lib.rb
Expand Up @@ -419,7 +419,7 @@ def grep(string, opts = {})

hsh = {}
command_lines('grep', grep_opts).each do |line|
if m = /(.*)\:(\d+)\:(.*)/.match(line)
if m = /(.*?)\:(\d+)\:(.*)/.match(line)
hsh[m[1]] ||= []
hsh[m[1]] << [m[2].to_i, m[3]]
end
Expand Down
1 change: 1 addition & 0 deletions tests/files/working/colon_numbers.txt
@@ -0,0 +1 @@
Grep regex doesn't like this:4342: because it is bad
Binary file modified tests/files/working/dot_git/index
Binary file not shown.
1 change: 1 addition & 0 deletions tests/files/working/dot_git/logs/HEAD
Expand Up @@ -79,3 +79,4 @@ a3db7143944dcfa006fefe7fb49c48793cb29ade 34a566d193dc4702f03149969a2aad144323156
6f09de178a27f7702c37907fd614c3c122d33c30 faf8d899a0f123c3c5def10857920be1c930e8ed Scott Chacon <schacon@gmail.com> 1378910110 -0400 commit (merge): Merge commit '4ce44a75510cbfe200b131fdbcc56a86f1b2dc08' into cherry
faf8d899a0f123c3c5def10857920be1c930e8ed 5e392652a881999392c2757cf9b783c5d47b67f7 Scott Chacon <schacon@gmail.com> 1378910135 -0400 checkout: moving from cherry to master
5e392652a881999392c2757cf9b783c5d47b67f7 5e53019b3238362144c2766f02a2c00d91fcc023 Scott Chacon <schacon@gmail.com> 1378910138 -0400 checkout: moving from master to git_grep
5e53019b3238362144c2766f02a2c00d91fcc023 46abbf07e3c564c723c7c039a43ab3a39e5d02dd Scott Chacon <schacon@gmail.com> 1647231179 +1300 commit: add example for grep with colon and numbers
1 change: 1 addition & 0 deletions tests/files/working/dot_git/logs/refs/heads/git_grep
Expand Up @@ -3,3 +3,4 @@
a3db7143944dcfa006fefe7fb49c48793cb29ade 34a566d193dc4702f03149969a2aad1443231560 scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194632975 -0800 commit: modified to not show up
34a566d193dc4702f03149969a2aad1443231560 935badc874edd62a8629aaf103418092c73f0a56 scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194633382 -0800 commit: more search help
935badc874edd62a8629aaf103418092c73f0a56 5e53019b3238362144c2766f02a2c00d91fcc023 scott Chacon <schacon@agadorsparticus.(none)> 1194720731 -0800 commit: diff test
5e53019b3238362144c2766f02a2c00d91fcc023 46abbf07e3c564c723c7c039a43ab3a39e5d02dd Scott Chacon <schacon@gmail.com> 1647231179 +1300 commit: add example for grep with colon and numbers
@@ -0,0 +1 @@
x��Qj�0D��S��H+�A(��'X�V��������G��0<�I-e��zS"Y�Ɯ2�ėe�����#K����9��с��uq��/>��&�9�l�Q�M�e��𳯵����ᶲ���+_����!�|� ӌ޹9»�֚A�a��l������7=����À�dz,��/�RL
Expand Down
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/files/working/dot_git/refs/heads/git_grep
@@ -1 +1 @@
5e53019b3238362144c2766f02a2c00d91fcc023
46abbf07e3c564c723c7c039a43ab3a39e5d02dd
1 change: 1 addition & 0 deletions tests/files/working/dot_git/refs/tags/grep_colon_numbers
@@ -0,0 +1 @@
46abbf07e3c564c723c7c039a43ab3a39e5d02dd
4 changes: 2 additions & 2 deletions tests/units/test_describe.rb
Expand Up @@ -3,14 +3,14 @@
require File.dirname(__FILE__) + '/../test_helper'

class TestDescribe < Test::Unit::TestCase

def setup
set_file_paths
@git = Git.open(@wdir)
end

def test_describe
assert_equal(@git.describe(nil, {:tags => true}), 'v2.8')
assert_equal(@git.describe(nil, {:tags => true}), 'grep_colon_numbers')
end

end
9 changes: 7 additions & 2 deletions tests/units/test_lib.rb
Expand Up @@ -19,7 +19,7 @@ def test_fetch_unshallow
git = Git.clone("file://#{@wdir}", "shallow", path: dir, depth: 1).lib
assert_equal(1, git.log_commits.length)
git.fetch("file://#{@wdir}", unshallow: true)
assert_equal(71, git.log_commits.length)
assert_equal(72, git.log_commits.length)
end
end

Expand Down Expand Up @@ -282,10 +282,15 @@ def test_grep
match = @lib.grep('search', :object => 'gitsearch1', :invert_match => true)
assert_equal(6, match['gitsearch1:scott/text.txt'].size)
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)
end

def test_show
assert(/^commit 5e53019b3238362144c2766f02a2c00d91fcc023.+\+replace with new text - diff test$/m.match(@lib.show))
puts @lib.show
assert_match(/^commit 46abbf07e3c564c723c7c039a43ab3a39e5d02dd.+\+Grep regex doesn't like this:4342: because it is bad\n$/m, @lib.show)
assert(/^commit 935badc874edd62a8629aaf103418092c73f0a56.+\+nothing!$/m.match(@lib.show('gitsearch1')))
assert(/^hello.+nothing!$/m.match(@lib.show('gitsearch1', 'scott/text.txt')))
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")
Expand Down
24 changes: 12 additions & 12 deletions tests/units/test_log.rb
Expand Up @@ -12,13 +12,13 @@ def setup
def test_get_fisrt_and_last_entries
log = @git.log
assert(log.first.is_a?(Git::Object::Commit))
assert_equal('5e53019b3238362144c2766f02a2c00d91fcc023', log.first.objectish)
assert_equal('46abbf07e3c564c723c7c039a43ab3a39e5d02dd', log.first.objectish)

assert(log.last.is_a?(Git::Object::Commit))
assert_equal('f1410f8735f6f73d3599eb9b5cdd2fb70373335c', log.last.objectish)
assert_equal('b03003311ad3fa368b475df58390353868e13c91', log.last.objectish)
end
def test_get_log_entries

def test_get_log_entries
assert_equal(30, @git.log.size)
assert_equal(50, @git.log(50).size)
assert_equal(10, @git.log(10).size)
Expand All @@ -35,15 +35,15 @@ def test_log_skip
assert_equal(three2.sha, three3.sha)
assert_equal(three1.sha, three2.sha)
end

def test_get_log_since
l = @git.log.since("2 seconds ago")
assert_equal(0, l.size)

l = @git.log.since("#{Date.today.year - 2006} years ago")
assert_equal(30, l.size)
end

def test_get_log_grep
l = @git.log.grep("search")
assert_equal(2, l.size)
Expand All @@ -55,11 +55,11 @@ def test_get_log_author
l = @git.log(5).author("lazySusan")
assert_equal(0, l.size)
end
def test_get_log_since_file

def test_get_log_since_file
l = @git.log.path('example.txt')
assert_equal(30, l.size)

l = @git.log.between('v2.5', 'test').path('example.txt')
assert_equal(1, l.size)
end
Expand All @@ -72,7 +72,7 @@ def test_get_log_path
log = @git.log.path(['example.txt','scott/text.txt'])
assert_equal(30, log.size)
end

def test_log_file_noexist
assert_raise Git::GitExecuteError do
@git.log.object('no-exist.txt').size
Expand All @@ -96,5 +96,5 @@ def test_log_cherry
l = @git.log.between( 'master', 'cherry').cherry
assert_equal( 1, l.size )
end

end
4 changes: 3 additions & 1 deletion tests/units/test_status.rb
Expand Up @@ -12,7 +12,9 @@ def setup
def test_status_pretty
in_temp_dir do |path|
git = Git.clone(@wdir, 'test_dot_files_status')
string = "ex_dir/ex.txt\n\tsha(r) \n\tsha(i) e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 " \
string = "colon_numbers.txt\n\tsha(r) \n\tsha(i) " \
"e76778b73006b0dda0dd56e9257c5bf6b6dd3373 100644\n\ttype \n\tstage 0\n\tuntrac \n" \
"ex_dir/ex.txt\n\tsha(r) \n\tsha(i) e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 " \
"100644\n\ttype \n\tstage 0\n\tuntrac \nexample.txt\n\tsha(r) \n\tsha(i) " \
"8dc79ae7616abf1e2d4d5d97d566f2b2f6cee043 100644\n\ttype \n\tstage 0\n\tuntrac " \
"\nscott/newfile\n\tsha(r) \n\tsha(i) 5d4606820736043f9eed2a6336661d6892c820a5 " \
Expand Down

0 comments on commit 018d919

Please sign in to comment.