Skip to content
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
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ group(:development) do
gem 'ruby-debug'
else
gem 'pry', :require => 'pry'
gem 'pry-debugger'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pry-debugger was removed since it doesn't work with Ruby 2.X.

end
end
25 changes: 22 additions & 3 deletions lib/pathspec/gitignorespec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ def initialize(pattern)
@regex = nil
@inclusive = nil

# EDGE CASE: According to git check-ignore (v2.4.1)), a single '/'
# does not match any file
elsif pattern == '/'
@regex = nil
@inclusive = nil

# We have a valid pattern!
else
# A pattern starting with an exclamation mark ('!') negates the
Expand Down Expand Up @@ -58,12 +64,15 @@ def initialize(pattern)
# to root.
if pattern_segs[0].empty?
pattern_segs.shift
else
elsif pattern_segs.length == 1 ||
pattern_segs.length == 2 && pattern_segs[-1].empty?
# A pattern without a beginning slash ('/') will match any
# descendant path. This is equivilent to "**/{pattern}". So,
# prepend with double-asterisks to make pattern relative to
# root.
if pattern_segs.length == 1 && pattern_segs[0] != '**'
# EDGE CASE: This also holds for a single pattern with a
# trailing slash (e.g. dir/).
if pattern_segs[0] != '**'
pattern_segs.insert(0, '**')
end
end
Expand All @@ -72,7 +81,7 @@ def initialize(pattern)
# paths of if it is a directory but not if it is a regular file.
# This is equivilent to "{pattern}/**". So, set last segment to
# double asterisks to include all descendants.
if pattern_segs[-1].empty?
if pattern_segs[-1].empty? && pattern_segs.length > 1
pattern_segs[-1] = '**'
end

Expand Down Expand Up @@ -131,6 +140,16 @@ def initialize(pattern)
end

regex.concat(translate_segment_glob(seg))

if i == regex_end && @inclusive
# A pattern ending without a slash ('/') will match a file
# or a directory (with paths underneath it).
# e.g. foo matches: foo, foo/bar, foo/bar/baz, etc.
# EDGE CASE: However, this does not hold for exclusion cases
# according to `git check-ignore` (v2.4.1).
regex.concat("(?:#{path_sep}.*)?")
end

need_slash = true
end
end
Expand Down
52 changes: 50 additions & 2 deletions spec/unit/pathspec/gitignorespec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
subject { GitIgnoreSpec.new 'foo/' }
it { is_expected.to match('foo/') }
it { is_expected.to match('foo/bar') }
it { is_expected.to match('baz/foo/bar') }
it { is_expected.to_not match('foo') }
it { is_expected.to be_inclusive }
end
Expand All @@ -88,10 +89,31 @@
describe 'handles basic globbing' do
subject { GitIgnoreSpec.new '*.tmp' }
it { is_expected.to match('foo.tmp') }
it { is_expected.to match('foo/bar.tmp') }
it { is_expected.to match('foo/bar.tmp/baz') }
it { is_expected.to_not match('foo.rb') }
it { is_expected.to be_inclusive }
end

describe 'handles inner globs' do
subject { GitIgnoreSpec.new 'foo-*-bar' }
it { is_expected.to match('foo--bar') }
it { is_expected.to match('foo-hello-bar') }
it { is_expected.to match('a/foo-hello-bar') }
it { is_expected.to match('foo-hello-bar/b') }
it { is_expected.to match('a/foo-hello-bar/b') }
it { is_expected.to_not match('foo.tmp') }
end

describe 'handles postfix globs' do
subject { GitIgnoreSpec.new '~temp-*' }
it { is_expected.to match('~temp-') }
it { is_expected.to match('~temp-foo') }
it { is_expected.to match('foo/~temp-bar') }
it { is_expected.to match('foo/~temp-bar/baz') }
it { is_expected.to_not match('~temp') }
end

describe 'handles multiple globs' do
subject { GitIgnoreSpec.new '*.middle.*' }
it { is_expected.to match('hello.middle.rb') }
Expand Down Expand Up @@ -182,6 +204,16 @@
it { is_expected.to be_inclusive }
end

describe 'handles simple single paths' do
subject { GitIgnoreSpec.new 'spam' }
it { is_expected.to match('spam') }
it { is_expected.to match('spam/') }
it { is_expected.to match('foo/spam') }
it { is_expected.to match('spam/foo') }
it { is_expected.to match('foo/spam/bar') }
it { is_expected.to_not match('foo') }
end

# Two consecutive asterisks ("**") in patterns matched against full pathname
# may have special meaning:

Expand All @@ -203,6 +235,7 @@
it { is_expected.to_not match('foo') }
it { is_expected.to match('foo/bar') }
it { is_expected.to match('baz/foo/bar') }
it { is_expected.to match('baz/foo/bar/sub') }
it { is_expected.to_not match('baz/foo/bar.rb') }
it { is_expected.to_not match('baz/bananafoo/bar') }
it { is_expected.to be_inclusive }
Expand All @@ -215,8 +248,8 @@
subject { GitIgnoreSpec.new 'abc/**' }
it { is_expected.to match('abc/') }
it { is_expected.to match('abc/def') }
it { is_expected.to_not match('123/abc/def') } # TODO: Maybe?
it { is_expected.to_not match('123/456/abc/') } # TODO: Maybe?
it { is_expected.to_not match('123/abc/def') }
it { is_expected.to_not match('123/456/abc/') }
it { is_expected.to be_inclusive }
end

Expand Down Expand Up @@ -254,4 +287,19 @@
it { is_expected.to_not be_inclusive }
end

describe 'does not match single absolute paths' do
subject { GitIgnoreSpec.new "/" }
it { is_expected.to_not match('foo.tmp') }
it { is_expected.to_not match(' ') }
it { is_expected.to_not match('a/b') }
end

describe 'nested paths are relative to the file' do
subject { GitIgnoreSpec.new 'foo/spam' }
it { is_expected.to match('foo/spam') }
it { is_expected.to match('foo/spam/bar') }
it { is_expected.to_not match('bar/foo/spam') }
end


end