Skip to content

Commit

Permalink
Handle paths that start with 'C:/'
Browse files Browse the repository at this point in the history
  • Loading branch information
robotdana committed Aug 7, 2023
1 parent d3f5956 commit 1267d50
Show file tree
Hide file tree
Showing 50 changed files with 859 additions and 469 deletions.
12 changes: 7 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ jobs:
bundler-cache: true
- run: gem install syntax_suggest
timeout-minutes: 5
- run: bundle exec rspec spec --dry-run
timeout-minutes: 1
- run: bundle exec rspec spec --tag=~gitls
timeout-minutes: 5
timeout-minutes: 10

compare:
strategy:
Expand All @@ -46,12 +44,16 @@ jobs:
run: |
Set-Location target
ruby ..\bin\compare
ruby ..\bin\time
# id: compare
timeout-minutes: 1
- if: matrix.platform != 'windows'
run: cd target && ../bin/compare
run: |
cd target
../bin/compare
../bin/time
id: compare
timeout-minutes: 1
timeout-minutes: 2
- run: cd target && ../bin/parse
if: failure() && steps.compare.outcome == 'failure'
- run: cd target && ../bin/ls
Expand Down
4 changes: 4 additions & 0 deletions .spellr_wordlists/english.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ aaaaaaaaaaaaaaaaaaaaaaaaaaazzz
allowlist
appendables
argv
ath
attributesfile
backport
barfoo
Expand Down Expand Up @@ -44,6 +45,7 @@ filetree
flamegraph
fnmatch
frotz
fsroot
gemfile
gitattributes
gitconfig
Expand All @@ -61,6 +63,7 @@ i'm
i've
idx
ignorecase
ime
includefile
includeif
initializable
Expand Down Expand Up @@ -141,6 +144,7 @@ unrecursive
unstaged
untr
upcase
urrent
usr
utf
warmup
Expand Down
9 changes: 0 additions & 9 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,13 @@ GEM
diff-lcs (1.5.0)
docile (1.4.0)
fast_ignore (0.17.4)
ffi (1.15.5)
get_process_mem (0.2.7)
ffi (~> 1.0)
io-console (0.6.0)
io-console (0.6.0-java)
irb (1.7.1)
reline (>= 0.3.0)
jar-dependencies (0.4.1)
jaro_winkler (1.5.6)
jaro_winkler (1.5.6-java)
jruby-win32ole (0.8.5)
json (2.6.3)
json (2.6.3-java)
language_server-protocol (3.17.0.3)
Expand Down Expand Up @@ -115,8 +111,6 @@ GEM
parallel (~> 1.0)
stringio (3.0.7)
syntax_suggest (1.1.0)
sys-proctable (1.3.0)
ffi (~> 1.1)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
unicode-display_width (2.4.2)
Expand All @@ -136,8 +130,6 @@ DEPENDENCIES
benchmark-ips
commonmarker
debug
get_process_mem
jruby-win32ole
leftovers (>= 0.4.0)
path_list!
pry
Expand All @@ -153,7 +145,6 @@ DEPENDENCIES
simplecov-console
spellr (>= 0.8.3)
syntax_suggest
sys-proctable
webrick
yard

Expand Down
40 changes: 40 additions & 0 deletions bin/benchmark
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,46 @@ benchmark('or-or-any') do
end
end


benchmark('bang-bang') do
Benchmark.ips do |x|
x.config(config)
x.report(:nil_bang_nil, '!nil.nil?')
x.report(:nil_bang_bang, '!!nil')
x.report(:nil_ternary, 'nil ? true : false')
x.report(:truthy_ref_bang_nil, '![].nil?')
x.report(:truthy_ref_bang_bang, '!![]')
x.report(:truthy_ref_ternary, '[] ? true : false')
x.report(:truthy_bang_nil, '!1.nil?')
x.report(:truthy_bang_bang, '!!1')
x.report(:truthy_ternary, '1 ? true : false')
x.report(:true_bang_nil, '!true.nil?')
x.report(:true_bang_bang, '!!true')
x.report(:true_ternary, 'true ? true : false')
x.report(:false_bang_nil, '!false.nil?')
x.report(:false_bang_bang, '!!false')
x.report(:false_ternary, 'false ? true : false')
x.compare!
end
end

benchmark('nil-not-false') do
Benchmark.ips do |x|
x.config(config)
x.report(:nil_nil, 'nil.nil?')
x.report(:nil_eq_false, 'nil || false == nil')
x.report(:true_nil, 'true.nil?')
x.report(:true_eq_false, 'true || false == true')
x.report(:false_nil, 'false.nil?')
x.report(:false_eq_false, 'false || false == false')
x.report(:truthy_nil, '1.nil?')
x.report(:truthy_eq_false, '1 || false == 1')
x.report(:truthy_ref_nil, '[].nil?')
x.report(:truthy_ref_eq_false, '[] || false == []')
x.compare!
end
end

benchmark('hash-merge') do
Benchmark.ips do |x|
x.config(config)
Expand Down
2 changes: 1 addition & 1 deletion lib/path_list/autoloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def autoload(klass)
def class_from_path(path)
name = ::File.basename(path).delete_suffix('.rb')

if name == 'version'
if name == 'version' || name == 'expandable_path'
name.upcase
else
name.gsub(/(?:^|_)(\w)/, &:upcase).delete('_')
Expand Down
49 changes: 33 additions & 16 deletions lib/path_list/candidate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(full_path, directory = nil, shebang = nil)
@full_path = full_path
@full_path_downcase = nil
@directory = directory
@exists = nil

@shebang = shebang

@child_candidates = nil
Expand All @@ -29,10 +29,8 @@ def full_path_downcase
# the containing directory as a Candidate,
# or nil if this is already the root
def parent
puts "#{__FILE__}:#{__LINE__}, @full_path: #{@full_path}"
return if @full_path.end_with?('/') # '/' on unix X:/ on win

puts "#{__FILE__}:#{__LINE__}, ::File.dirname(@full_path): #{::File.dirname(@full_path)}"
self.class.new(::File.dirname(@full_path), true)
end

Expand All @@ -56,23 +54,32 @@ def children
end
end

# @return [Boolean] whether this path is a directory (false for symlinks to directories)
def directory?
return @directory unless @directory.nil?
# :nocov:
if ::RUBY_PLATFORM == 'jruby' && ::RbConfig::CONFIG['host_os'].match?(/mswin|mingw/)
# @return [Boolean] whether this path is a directory (false for symlinks to directories)
puts 'WE ARE WINDOWS JRUBY'
def directory?
return @directory unless @directory.nil?

@directory = ::File.lstat(@full_path).directory?
rescue ::SystemCallError
@exists ||= false
@directory = false
@directory = if ::File.symlink?(@full_path)
false
else
lstat&.directory? || false
end
end
# :nocov:
else
# @return [Boolean] whether this path is a directory (false for symlinks to directories)
def directory?
return @directory unless @directory.nil?

@directory = lstat&.directory? || false
end
end

# @return [Boolean] whether this path exists
def exists?
return @exists unless @exists.nil?

@exists = ::File.exist?(@full_path)
rescue ::SystemCallError
@exists = false
lstat ? true : false
end

alias_method :original_inspect, :inspect # leftovers:keep
Expand Down Expand Up @@ -104,11 +111,21 @@ def shebang
''
end
rescue ::IOError, ::SystemCallError
@exists ||= false
@lstat ||= nil
''
ensure
file&.close
end
end

private

def lstat
return @lstat if defined?(@lstat)

@lstat = ::File.lstat(@full_path)
rescue ::SystemCallError
@lstat = nil
end
end
end
26 changes: 13 additions & 13 deletions lib/path_list/gitconfig/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def read_file(path)
file = ::StringScanner.new(::File.read(path))

until file.eos?
if file.skip(/(\s+|[#;].*\n)/)
if file.skip(/(\s+|[#;].*\r?\n)/)
# skip
elsif file.skip(/\[core\]/i)
self.section = :core
Expand All @@ -61,9 +61,9 @@ def read_file(path)
self.section = include_if(file) ? :include : :not_include
elsif file.skip(/\[[\w.]+( "([^\0\\"]|\\(\\{2})*"|\\{2}*)+")?\]/)
self.section = :other
elsif section == :core && file.skip(/excludesfile\s*=(\s|\\\n)*/i)
elsif section == :core && file.skip(/excludesfile\s*=(\s|\\\r?\n)*/i)
self.value = scan_value(file)
elsif section == :include && file.skip(/path\s*=(\s|\\\n)*/)
elsif section == :include && file.skip(/path\s*=(\s|\\\r?\n)*/)
include_path = scan_value(file)

value = self.class.parse(
Expand All @@ -73,9 +73,9 @@ def read_file(path)
)
self.value = value if value
self.section = :include
elsif file.skip(/[a-zA-Z0-9]\w*\s*([#;].*)?\n/)
elsif file.skip(/[a-zA-Z0-9]\w*\s*([#;].*)?\r?\n/)
nil
elsif file.skip(/[a-zA-Z0-9]\w*\s*=(\s|\\\n)*/)
elsif file.skip(/[a-zA-Z0-9]\w*\s*=(\s|\\\r?\n)*/)
skip_value(file)
else
raise ParseError.new('Unexpected character', scanner: file, path: path)
Expand All @@ -84,7 +84,7 @@ def read_file(path)
end

def scan_condition_value(file)
if file.scan(/([^\0\\\n"]|\\(\\{2})*"|\\{2}*)+(?="\])/)
if file.scan(/([^\0\\\r\n"]|\\(\\{2})*"|\\{2}*)+(?="\])/)
value = file.matched
file.skip(/"\]/)
value
Expand All @@ -94,7 +94,7 @@ def scan_condition_value(file)
end

def skip_condition_value(file)
unless file.skip(/([^\0\\\n"]|\\(\\{2})*"|\\{2}*)+"\]/)
unless file.skip(/([^\0\\\r\n"]|\\(\\{2})*"|\\{2}*)+"\]/)
raise ParseError.new('Unexpected character in condition', scanner: file, path: path)
end
end
Expand Down Expand Up @@ -135,7 +135,7 @@ def gitdir?(gitdir, path:, case_insensitive: false)
def scan_value(file)
value = +''
until file.eos?
if file.skip(/\\\n/)
if file.skip(/\\\r?\n/)
# continue
elsif file.skip(/\\\\/)
value << '\\'
Expand All @@ -152,7 +152,7 @@ def scan_value(file)
elsif within_quotes
if file.skip(/"/)
self.within_quotes = false
elsif file.scan(/[^"\\\n]+/)
elsif file.scan(/[^"\\\n\r]+/)
value << file.matched
else
raise ParseError.new('Unexpected character in quoted value', scanner: file, path: path)
Expand All @@ -161,7 +161,7 @@ def scan_value(file)
self.within_quotes = true
elsif file.scan(/[^;#"\s\\]+/)
value << file.matched
elsif file.skip(/\s*[;#\n]/)
elsif file.skip(/\s*[;#\n\r]/)
break
elsif file.scan(/\s+/) # rubocop:disable Lint/DuplicateBranch
value << file.matched
Expand All @@ -179,14 +179,14 @@ def scan_value(file)

def skip_value(file)
until file.eos?
if file.skip(/\\(?:\n|\\|n|t|b|")/)
if file.skip(/\\(?:\r?\n|\\|n|t|b|")/)
nil
elsif file.skip(/\\/)
raise ParseError.new('Unrecognized escape sequence in value', scanner: file, path: path)
elsif within_quotes
if file.skip(/"/)
self.within_quotes = false
elsif file.skip(/[^"\\\n]+/)
elsif file.skip(/[^"\\\n\r]+/)
nil
else
raise ParseError.new('Unexpected character in quoted value', scanner: file, path: path)
Expand All @@ -195,7 +195,7 @@ def skip_value(file)
self.within_quotes = true
elsif file.skip(/[^;#"\s\\]+/) # rubocop:disable Lint/DuplicateBranch
nil
elsif file.skip(/\s*[;#\n]/)
elsif file.skip(/\s*[;#\n\r]/)
break
elsif file.skip(/\s+/) # rubocop:disable Lint/DuplicateBranch
nil
Expand Down
2 changes: 1 addition & 1 deletion lib/path_list/gitconfig/parse_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def message
chars_before_our_line = @scanner.string.match(/\A(?:.*\n){#{lineno - 1}}/)[0].length
col = @scanner.pos - chars_before_our_line
@scanner.pos = chars_before_our_line
line = @scanner.scan(/^[^\n]*/)
line = @scanner.scan(/^[^\r\n]*/)
@scanner.pos = chars_before_our_line + col

<<~MESSAGE
Expand Down
2 changes: 1 addition & 1 deletion lib/path_list/gitignore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def find_root

loop do
return dir if ::File.exist?("#{dir}/.git")
return pwd if dir.casecmp(home).zero? || dir == '/'
return pwd if dir.casecmp(home).zero? || dir.end_with?('/')

dir = ::File.dirname(dir)
end
Expand Down
2 changes: 0 additions & 2 deletions lib/path_list/matcher/exact_string/case_insensitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def initialize(item, polarity)
# @param (see Matcher#match)
# @return (see Matcher#match)
def match(candidate)
puts "#{__FILE__}:#{__LINE__}, @item: #{@item}"
puts "#{__FILE__}:#{__LINE__}, candidate.full_path_downcase: #{candidate.full_path_downcase}"
return @polarity if @item == candidate.full_path_downcase
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/path_list/matcher/exact_string/set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def match(candidate)

# @return (see Matcher#inspect)
def inspect
"#{self.class}.new([#{@set.map(&:inspect).join(', ')}], #{@polarity.inspect})"
"#{self.class}.new([#{@set.to_a.sort.map(&:inspect).join(', ')}], #{@polarity.inspect})"
end

# @return set [Set]
Expand Down
2 changes: 0 additions & 2 deletions lib/path_list/matcher/exact_string/set/case_insensitive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ def initialize(set, polarity)
# @param (see Matcher#match)
# @return (see Matcher#match)
def match(candidate)
puts "#{__FILE__}:#{__LINE__}, @set: #{@set}"
puts "#{__FILE__}:#{__LINE__}, candidate.full_path_downcase: #{candidate.full_path_downcase}"
@polarity if @set.include?(candidate.full_path_downcase)
end
end
Expand Down
Loading

0 comments on commit 1267d50

Please sign in to comment.