Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

childrenOfPath exclusion now obeys wildcard paths for exceptions. #371

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,15 @@ public PredicateResult Evaluate(IItemData itemData)
// you may preserve certain children from exclusion
foreach (var exception in _exceptions)
{
var fullPath = exception.Item1;
var exceptionPath = exception.Item1;
var exceptionRule = exception.Item2;
var escapedItemPath = itemPath.Replace("*", @"\*");

var unescapedExceptionPath = fullPath.Replace(@"\*", "*");

if (exceptionRule.IncludeChildren)
{
if (itemPath.StartsWith(unescapedExceptionPath, StringComparison.OrdinalIgnoreCase))
{
return new PredicateResult(true);
}
}
else
if (PathTool.ComparePathSegments(PathTool.ExplodePath(escapedItemPath),
PathTool.ExplodePath(exceptionPath),
exceptionRule.IncludeChildren))
{
if (itemPath.Equals(unescapedExceptionPath, StringComparison.OrdinalIgnoreCase))
{
return new PredicateResult(true);
}
return new PredicateResult(true);
}
}

Expand Down
46 changes: 45 additions & 1 deletion src/Unicorn/Predicates/Exclusions/PathTool.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
namespace Unicorn.Predicates.Exclusions
using System;

namespace Unicorn.Predicates.Exclusions
{
internal static class PathTool
{
internal static bool ComparePathSegments(string[] path1, string[] path2, bool allowPartialMatch = false)
{
if (!allowPartialMatch && path1?.Length != path2?.Length)
{
return false;
}

var segments = Math.Min(path1.Length, path2.Length);
for (var i = 0; i < segments; i++)
{
var segment1 = path1[i];
var segment2 = path2[i];

const string wildcard = "*";
if (wildcard.Equals(segment1, StringComparison.Ordinal)
|| wildcard.Equals(segment2, StringComparison.Ordinal))
{
continue;
}

if (!string.Equals(segment1, segment2, StringComparison.OrdinalIgnoreCase))
{
return false;
}
}

return true;
}

internal static string[] ExplodePath(string path)
{
if (string.IsNullOrWhiteSpace(path))
{
return new string[] {};
}

var safePath = EnsureTrailingSlash(path);
var pathSegments = safePath.Split(new[] { '/' } , StringSplitOptions.RemoveEmptyEntries);

return pathSegments;
}

internal static string EnsureTrailingSlash(string path)
{
if (path.EndsWith("/")) return path;
Expand Down