Process rsync exclude regex less

This change will prepend "^" and append "/" if a start anchor is
detected in the regex string. This allows users to specify
relative paths to exclude.

It also removes replacing occurences of "*" with "[^/]*". "*"
already expresses itself accurately.
This commit is contained in:
sophia 2023-01-19 16:05:02 -08:00
parent 56d6cbaa8e
commit 0939af9a76
2 changed files with 15 additions and 10 deletions

View File

@ -27,18 +27,15 @@ module VagrantPlugins
if exclude.start_with?("/")
start_anchor = true
exclude = exclude[1..-1]
end
exclude = "#{exclude}/" if !exclude.end_with?("/")
exclude = "^#{exclude}"
exclude += ".*" if !start_anchor
exclude = "#{exclude}/" if !exclude.end_with?("/") if start_anchor
exclude = "^#{exclude}" if start_anchor
exclude += ".*" if (!start_anchor && !exclude.end_with?("*"))
# This is not an ideal solution, but it's a start. We can improve and
# keep unit tests passing in the future.
exclude = exclude.gsub("**", "|||GLOBAL|||")
exclude = exclude.gsub("*", "|||PATH|||")
exclude = exclude.gsub("|||PATH|||", "[^/]*")
exclude = exclude.gsub("|||GLOBAL|||", ".*")
Regexp.new(exclude)

View File

@ -32,23 +32,31 @@ describe VagrantPlugins::SyncedFolderRSync::RsyncHelper do
let(:path) { "/foo/bar" }
it "converts a directory match" do
expected_regex = /foo\/.*/
expect(described_class.exclude_to_regexp("foo/")).
to eq(/^foo\/.[^\/]*/)
to eq(/foo\/.*/)
expect(path).to match(expected_regex)
end
it "converts the start anchor" do
expected_regex = /^\/foo\//
expect(described_class.exclude_to_regexp("/foo")).
to eq(/^foo\//)
to eq(expected_regex)
expect(path).to match(expected_regex)
end
it "converts the **" do
expected_regex = /fo.*o.*/
expect(described_class.exclude_to_regexp("fo**o")).
to eq(/^fo.*o\/.[^\/]*/)
to eq(expected_regex)
expect(path).to match(expected_regex)
end
it "converts the *" do
expected_regex = /fo*o.*/
expect(described_class.exclude_to_regexp("fo*o")).
to eq(/^fo[^\/]*o\/.[^\/]*/)
to eq(expected_regex)
expect(path).to match(expected_regex)
end
end