Skip to content

Commit 018d919

Browse files
authored
Fix bug when grepping lines that contain numbers surrounded by colons (#566)
Signed-off-by: James Fairbairn <jamesaf112@gmail.com>
1 parent c04d16e commit 018d919

14 files changed

+31
-19
lines changed

lib/git/lib.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def grep(string, opts = {})
419419

420420
hsh = {}
421421
command_lines('grep', grep_opts).each do |line|
422-
if m = /(.*)\:(\d+)\:(.*)/.match(line)
422+
if m = /(.*?)\:(\d+)\:(.*)/.match(line)
423423
hsh[m[1]] ||= []
424424
hsh[m[1]] << [m[2].to_i, m[3]]
425425
end

tests/files/working/colon_numbers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Grep regex doesn't like this:4342: because it is bad

tests/files/working/dot_git/index

174 Bytes
Binary file not shown.

tests/files/working/dot_git/logs/HEAD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,4 @@ a3db7143944dcfa006fefe7fb49c48793cb29ade 34a566d193dc4702f03149969a2aad144323156
7979
6f09de178a27f7702c37907fd614c3c122d33c30 faf8d899a0f123c3c5def10857920be1c930e8ed Scott Chacon <schacon@gmail.com> 1378910110 -0400 commit (merge): Merge commit '4ce44a75510cbfe200b131fdbcc56a86f1b2dc08' into cherry
8080
faf8d899a0f123c3c5def10857920be1c930e8ed 5e392652a881999392c2757cf9b783c5d47b67f7 Scott Chacon <schacon@gmail.com> 1378910135 -0400 checkout: moving from cherry to master
8181
5e392652a881999392c2757cf9b783c5d47b67f7 5e53019b3238362144c2766f02a2c00d91fcc023 Scott Chacon <schacon@gmail.com> 1378910138 -0400 checkout: moving from master to git_grep
82+
5e53019b3238362144c2766f02a2c00d91fcc023 46abbf07e3c564c723c7c039a43ab3a39e5d02dd Scott Chacon <schacon@gmail.com> 1647231179 +1300 commit: add example for grep with colon and numbers

tests/files/working/dot_git/logs/refs/heads/git_grep

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
a3db7143944dcfa006fefe7fb49c48793cb29ade 34a566d193dc4702f03149969a2aad1443231560 scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194632975 -0800 commit: modified to not show up
44
34a566d193dc4702f03149969a2aad1443231560 935badc874edd62a8629aaf103418092c73f0a56 scott Chacon <schacon@agadorsparticus.corp.reactrix.com> 1194633382 -0800 commit: more search help
55
935badc874edd62a8629aaf103418092c73f0a56 5e53019b3238362144c2766f02a2c00d91fcc023 scott Chacon <schacon@agadorsparticus.(none)> 1194720731 -0800 commit: diff test
6+
5e53019b3238362144c2766f02a2c00d91fcc023 46abbf07e3c564c723c7c039a43ab3a39e5d02dd Scott Chacon <schacon@gmail.com> 1647231179 +1300 commit: add example for grep with colon and numbers

tests/files/working/dot_git/objects/46/abbf07e3c564c723c7c039a43ab3a39e5d02dd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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
Binary file not shown.
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
5e53019b3238362144c2766f02a2c00d91fcc023
1+
46abbf07e3c564c723c7c039a43ab3a39e5d02dd
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
46abbf07e3c564c723c7c039a43ab3a39e5d02dd

tests/units/test_describe.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
require File.dirname(__FILE__) + '/../test_helper'
44

55
class TestDescribe < Test::Unit::TestCase
6-
6+
77
def setup
88
set_file_paths
99
@git = Git.open(@wdir)
1010
end
1111

1212
def test_describe
13-
assert_equal(@git.describe(nil, {:tags => true}), 'v2.8')
13+
assert_equal(@git.describe(nil, {:tags => true}), 'grep_colon_numbers')
1414
end
1515

1616
end

tests/units/test_lib.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_fetch_unshallow
1919
git = Git.clone("file://#{@wdir}", "shallow", path: dir, depth: 1).lib
2020
assert_equal(1, git.log_commits.length)
2121
git.fetch("file://#{@wdir}", unshallow: true)
22-
assert_equal(71, git.log_commits.length)
22+
assert_equal(72, git.log_commits.length)
2323
end
2424
end
2525

@@ -282,10 +282,15 @@ def test_grep
282282
match = @lib.grep('search', :object => 'gitsearch1', :invert_match => true)
283283
assert_equal(6, match['gitsearch1:scott/text.txt'].size)
284284
assert_equal(2, match.size)
285+
286+
match = @lib.grep('Grep', :object => 'grep_colon_numbers')
287+
assert_equal("Grep regex doesn't like this:4342: because it is bad", match['grep_colon_numbers:colon_numbers.txt'].first[1])
288+
assert_equal(1, match.size)
285289
end
286290

287291
def test_show
288-
assert(/^commit 5e53019b3238362144c2766f02a2c00d91fcc023.+\+replace with new text - diff test$/m.match(@lib.show))
292+
puts @lib.show
293+
assert_match(/^commit 46abbf07e3c564c723c7c039a43ab3a39e5d02dd.+\+Grep regex doesn't like this:4342: because it is bad\n$/m, @lib.show)
289294
assert(/^commit 935badc874edd62a8629aaf103418092c73f0a56.+\+nothing!$/m.match(@lib.show('gitsearch1')))
290295
assert(/^hello.+nothing!$/m.match(@lib.show('gitsearch1', 'scott/text.txt')))
291296
assert(@lib.show('gitsearch1', 'scott/text.txt') == "hello\nthis is\na file\nthat is\nput here\nto search one\nto search two\nnothing!\n")

tests/units/test_log.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ def setup
1212
def test_get_fisrt_and_last_entries
1313
log = @git.log
1414
assert(log.first.is_a?(Git::Object::Commit))
15-
assert_equal('5e53019b3238362144c2766f02a2c00d91fcc023', log.first.objectish)
15+
assert_equal('46abbf07e3c564c723c7c039a43ab3a39e5d02dd', log.first.objectish)
1616

1717
assert(log.last.is_a?(Git::Object::Commit))
18-
assert_equal('f1410f8735f6f73d3599eb9b5cdd2fb70373335c', log.last.objectish)
18+
assert_equal('b03003311ad3fa368b475df58390353868e13c91', log.last.objectish)
1919
end
20-
21-
def test_get_log_entries
20+
21+
def test_get_log_entries
2222
assert_equal(30, @git.log.size)
2323
assert_equal(50, @git.log(50).size)
2424
assert_equal(10, @git.log(10).size)
@@ -35,15 +35,15 @@ def test_log_skip
3535
assert_equal(three2.sha, three3.sha)
3636
assert_equal(three1.sha, three2.sha)
3737
end
38-
38+
3939
def test_get_log_since
4040
l = @git.log.since("2 seconds ago")
4141
assert_equal(0, l.size)
42-
42+
4343
l = @git.log.since("#{Date.today.year - 2006} years ago")
4444
assert_equal(30, l.size)
4545
end
46-
46+
4747
def test_get_log_grep
4848
l = @git.log.grep("search")
4949
assert_equal(2, l.size)
@@ -55,11 +55,11 @@ def test_get_log_author
5555
l = @git.log(5).author("lazySusan")
5656
assert_equal(0, l.size)
5757
end
58-
59-
def test_get_log_since_file
58+
59+
def test_get_log_since_file
6060
l = @git.log.path('example.txt')
6161
assert_equal(30, l.size)
62-
62+
6363
l = @git.log.between('v2.5', 'test').path('example.txt')
6464
assert_equal(1, l.size)
6565
end
@@ -72,7 +72,7 @@ def test_get_log_path
7272
log = @git.log.path(['example.txt','scott/text.txt'])
7373
assert_equal(30, log.size)
7474
end
75-
75+
7676
def test_log_file_noexist
7777
assert_raise Git::GitExecuteError do
7878
@git.log.object('no-exist.txt').size
@@ -96,5 +96,5 @@ def test_log_cherry
9696
l = @git.log.between( 'master', 'cherry').cherry
9797
assert_equal( 1, l.size )
9898
end
99-
99+
100100
end

tests/units/test_status.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ def setup
1212
def test_status_pretty
1313
in_temp_dir do |path|
1414
git = Git.clone(@wdir, 'test_dot_files_status')
15-
string = "ex_dir/ex.txt\n\tsha(r) \n\tsha(i) e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 " \
15+
string = "colon_numbers.txt\n\tsha(r) \n\tsha(i) " \
16+
"e76778b73006b0dda0dd56e9257c5bf6b6dd3373 100644\n\ttype \n\tstage 0\n\tuntrac \n" \
17+
"ex_dir/ex.txt\n\tsha(r) \n\tsha(i) e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 " \
1618
"100644\n\ttype \n\tstage 0\n\tuntrac \nexample.txt\n\tsha(r) \n\tsha(i) " \
1719
"8dc79ae7616abf1e2d4d5d97d566f2b2f6cee043 100644\n\ttype \n\tstage 0\n\tuntrac " \
1820
"\nscott/newfile\n\tsha(r) \n\tsha(i) 5d4606820736043f9eed2a6336661d6892c820a5 " \

0 commit comments

Comments
 (0)