Skip to content
Merged
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 @@ -103,6 +103,8 @@ protected override void ProcessRecord()

if (_relative)
{
ReadOnlySpan<char> baseCache = null;
ReadOnlySpan<char> adjustedBaseCache = null;
foreach (PathInfo currentPath in result)
{
// When result path and base path is on different PSDrive
Expand All @@ -116,8 +118,18 @@ protected override void ProcessRecord()
continue;
}

int leafIndex = currentPath.Path.LastIndexOf(currentPath.Provider.ItemSeparator);
var basePath = currentPath.Path.AsSpan(0, leafIndex);
if (basePath == baseCache)
{
WriteObject(string.Concat(adjustedBaseCache, currentPath.Path.AsSpan(leafIndex + 1)), enumerateCollection: false);
continue;
}

baseCache = basePath;
string adjustedPath = SessionState.Path.NormalizeRelativePath(currentPath.Path,
SessionState.Path.CurrentLocation.ProviderPath);

// Do not insert './' if result path is not relative
if (!adjustedPath.StartsWith(
currentPath.Drive?.Root ?? currentPath.Path, StringComparison.OrdinalIgnoreCase) &&
Expand All @@ -126,6 +138,9 @@ protected override void ProcessRecord()
adjustedPath = SessionState.Path.Combine(".", adjustedPath);
}

leafIndex = adjustedPath.LastIndexOf(currentPath.Provider.ItemSeparator);
adjustedBaseCache = adjustedPath.AsSpan(0, leafIndex + 1);

WriteObject(adjustedPath, enumerateCollection: false);
}
}
Expand Down