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

Reduce noisy errors when viewing git diff on VS code #9407

Merged
merged 3 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -3,6 +3,7 @@

using System;
using System.Net;
using Microsoft.AspNetCore.Razor.Utilities;

namespace Microsoft.CodeAnalysis.Razor;

Expand All @@ -22,6 +23,27 @@ public static string GetAbsoluteOrUNCPath(this Uri uri)
}

// Absolute paths are usually encoded.
return uri.AbsolutePath.Contains("%") ? WebUtility.UrlDecode(uri.AbsolutePath) : uri.AbsolutePath;
var absolutePath = uri.AbsolutePath.Contains("%") ? WebUtility.UrlDecode(uri.AbsolutePath) : uri.AbsolutePath;

if (uri.Scheme.ToLower() == "git")
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
{
// return a normalized path when we want to add to a fake git directory path (hacky fix for https://github.com/dotnet/razor/issues/9365)
return AddToFakeGitDirectoryAtRoot(absolutePath);
}

return absolutePath;
}

private static string AddToFakeGitDirectoryAtRoot(string decodedAbsolutePath)
{
var normalizedPath = FilePathNormalizer.Normalize(decodedAbsolutePath);
var firstSeparatorIndex = normalizedPath.IndexOf('/');
if (firstSeparatorIndex < 0)
{
// no-op
return decodedAbsolutePath;
}

return normalizedPath[..(firstSeparatorIndex + 1)] + "_git_" + normalizedPath[firstSeparatorIndex..];
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,17 @@ public void GetAbsoluteOrUNCPath_UNCPath_HandlesSpacePaths()
// Assert
Assert.Equal(@"\\some\path\to\file path.cshtml", path);
}

[Fact, WorkItem("https://github.com/dotnet/razor/issues/9365")]
public void GetAbsoluteAndUNCPath_HasGitScheme_ReturnsANormalizedFakeGitFilePath()
{
// Arrange
var uri = new Uri("git:/c%3A/myFolder/Index.cshtml?%7B%22path%22%3A%22c%3A%5C%5CmyFolder%5C%5CIndex.cshtml%22%2C%22ref%22%3A%22~%22%7D");
maryamariyan marked this conversation as resolved.
Show resolved Hide resolved

// Act
var path = uri.GetAbsoluteOrUNCPath();

// Assert
Assert.Equal("c:/_git_/myFolder/Index.cshtml", path);
}
}