diff options
author | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2015-12-21 12:08:04 +0100 |
---|---|---|
committer | Grzegorz Bizon <grzesiek.bizon@gmail.com> | 2016-01-14 12:48:13 +0100 |
commit | 37b2c5dd5521f25a7195e82538a0ffc528c3ec6d (patch) | |
tree | 7840e1f25d139d2422f3f54fa06381ee483356f4 /lib | |
parent | d382335dcd9285c9355ed04dc12c5314bca3c024 (diff) | |
download | gitlab-ce-37b2c5dd5521f25a7195e82538a0ffc528c3ec6d.tar.gz |
Add support for root path for `StringPath`
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/string_path.rb | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/lib/gitlab/string_path.rb b/lib/gitlab/string_path.rb index a2dc63db2f2..d165829132a 100644 --- a/lib/gitlab/string_path.rb +++ b/lib/gitlab/string_path.rb @@ -1,6 +1,6 @@ module Gitlab ## - # Class that represents a path to a file or directory + # Class that represents a simplified path to a file or directory # # This is IO-operations safe class, that does similar job to # Ruby's Pathname but without the risk of accessing filesystem. @@ -10,8 +10,9 @@ module Gitlab attr_reader :path, :universe def initialize(path, universe) - @path = path - @universe = universe + @path = prepare(path) + @universe = universe.map { |entry| prepare(entry) } + @universe.unshift('./') unless @universe.include?('./') end def to_s @@ -43,6 +44,15 @@ module Gitlab new(@path.sub(basename, '')) end + def basename + name = @path.split(::File::SEPARATOR).last + directory? ? name + ::File::SEPARATOR : name + end + + def has_descendants? + descendants.any? + end + def descendants return [] unless directory? children = @universe.select { |entry| entry =~ /^#{@path}.+/ } @@ -63,11 +73,6 @@ module Gitlab children.select { |child| child.file? } end - def basename - name = @path.split(::File::SEPARATOR).last - directory? ? name + ::File::SEPARATOR : name - end - def ==(other) @path == other.path && @universe == other.universe end @@ -77,5 +82,10 @@ module Gitlab def new(path) self.class.new(path, @universe) end + + def prepare(path) + return path if path =~ %r{^(/|\.|\.\.)} + path.dup.prepend('./') + end end end |