Pathspec functions correctly

This commit is contained in:
Yessiest 2023-04-15 00:52:25 +04:00
parent b006e0f672
commit d8cddf7479
1 changed files with 11 additions and 13 deletions

24
hyde.rb
View File

@ -8,14 +8,16 @@ module Hyde
end end
def match?(path) def match?(path)
raise Exception, "Not permitted" if @lock_methods raise Exception, "Not permitted" if @lock_methods
split_path = path.split("/") return true if @path == ""
puts "#{path}, #{@path}, #{normalize_input(path)}"
if @path.kind_of? Regexp then if @path.kind_of? Regexp then
anchored_regex = Regexp.new("^"+@path.to_s+"$") return Regexp.new("^"+@path.to_s+"$").match normalize_input(path)
return anchored_regex.match split_path[0] unless @full_regexp
return anchored_regex.match normalize_input(path)
else else
return split_path[0] == @path split_path = path.split("/").filter { |x| x != "" }
@path.split("/").filter { |x| x != "" }
.each_with_index { |x,i|
return false if x != split_path[i]
}
return true
end end
end end
def normalize_input(path) def normalize_input(path)
@ -24,8 +26,8 @@ module Hyde
end end
def normalize(path) def normalize(path)
raise Exception, "Not permitted" if @lock_methods raise Exception, "Not permitted" if @lock_methods
# remove multiple slashes in one spot # remove multiple slashes in one spot and trim edge slashes
path = (path.split "/").filter {|x| x != ""}.join("/") path = normalize_input(path)
# globbing behaviour simulated with regexp # globbing behaviour simulated with regexp
if path.match /(?<!\\)[\*\?\[]/ then if path.match /(?<!\\)[\*\?\[]/ then
path = Regexp.new(path path = Regexp.new(path
@ -50,7 +52,6 @@ module Hyde
@lock_methods = false @lock_methods = false
end end
def match(path) def match(path)
puts (match? path)
if match? path then if match? path then
exec if defined? exec exec if defined? exec
end end
@ -58,7 +59,6 @@ module Hyde
end end
class PrintProbe < Hyde::Probe class PrintProbe < Hyde::Probe
def match(path) def match(path)
puts (match? path)
if match? path then if match? path then
puts "#{path} matched!" puts "#{path} matched!"
end end
@ -78,13 +78,12 @@ module Hyde
class Pathspec class Pathspec
include Hyde::PatternMatching include Hyde::PatternMatching
include Hyde::Handlers include Hyde::Handlers
def initialize (path, root_path: nil, full_regexp: false, &block) def initialize (path, root_path: nil, &block)
prep_path path prep_path path
@chain = [] @chain = []
@root_override = root_path @root_override = root_path
@remap = false @remap = false
@lock_methods = true @lock_methods = true
@full_regexp = full_regexp
self.instance_eval &block self.instance_eval &block
@lock_methods = false @lock_methods = false
end end
@ -103,7 +102,6 @@ module Hyde
if match? path then if match? path then
cut_path = normalize_input(path).sub(@path, "") cut_path = normalize_input(path).sub(@path, "")
next_pathspec = @chain.find { |x| x.match? cut_path } next_pathspec = @chain.find { |x| x.match? cut_path }
puts next_pathspec.inspect
next_pathspec.match cut_path if next_pathspec next_pathspec.match cut_path if next_pathspec
end end
end end