Skip to content

Commit

Permalink
Fixes for handing git paths (#1822)
Browse files Browse the repository at this point in the history
  • Loading branch information
BernieWhite authored May 18, 2024
1 parent 0b06f98 commit d20ba33
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
33 changes: 21 additions & 12 deletions src/PSRule.Benchmark/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,14 @@
"version": 1,
"dependencies": {
"net8.0": {
"BenchmarkDotNet": {
"BenchmarkDotNet.Diagnostics.Windows": {
"type": "Direct",
"requested": "[0.13.12, )",
"resolved": "0.13.12",
"contentHash": "aKnzpUZJJfLBHG7zcfQZhCexZQKcJgElC8qcFUTXPMYFlVauJBobuOmtRnmrapqC2j7EjjZCsPxa3yLvFLx5/Q==",
"contentHash": "HdARbwcGFymgrSFoLVtuJaacyNW9WLuGljX0hsTarSO+80nI2OmbDS09R+I9kIeJ8Gn/1zJFjKgefmAlADhlww==",
"dependencies": {
"BenchmarkDotNet.Annotations": "0.13.12",
"CommandLineParser": "2.9.1",
"Gee.External.Capstone": "2.3.0",
"Iced": "1.17.0",
"Microsoft.CodeAnalysis.CSharp": "4.1.0",
"Microsoft.Diagnostics.Runtime": "2.2.332302",
"Microsoft.Diagnostics.Tracing.TraceEvent": "3.0.2",
"Microsoft.DotNet.PlatformAbstractions": "3.1.6",
"Perfolizer": "[0.2.1]",
"System.Management": "5.0.0"
"BenchmarkDotNet": "0.13.12",
"Microsoft.Diagnostics.Tracing.TraceEvent": "3.0.2"
}
},
"Microsoft.Extensions.CommandLineUtils": {
Expand Down Expand Up @@ -91,6 +83,23 @@
"System.Text.Encoding.CodePages": "8.0.0"
}
},
"BenchmarkDotNet": {
"type": "Transitive",
"resolved": "0.13.12",
"contentHash": "aKnzpUZJJfLBHG7zcfQZhCexZQKcJgElC8qcFUTXPMYFlVauJBobuOmtRnmrapqC2j7EjjZCsPxa3yLvFLx5/Q==",
"dependencies": {
"BenchmarkDotNet.Annotations": "0.13.12",
"CommandLineParser": "2.9.1",
"Gee.External.Capstone": "2.3.0",
"Iced": "1.17.0",
"Microsoft.CodeAnalysis.CSharp": "4.1.0",
"Microsoft.Diagnostics.Runtime": "2.2.332302",
"Microsoft.Diagnostics.Tracing.TraceEvent": "3.0.2",
"Microsoft.DotNet.PlatformAbstractions": "3.1.6",
"Perfolizer": "[0.2.1]",
"System.Management": "5.0.0"
}
},
"BenchmarkDotNet.Annotations": {
"type": "Transitive",
"resolved": "0.13.12",
Expand Down
13 changes: 7 additions & 6 deletions src/PSRule.Types/Environment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ public static CultureInfo GetCurrentCulture()
/// <returns>A absolute path.</returns>
internal static string GetRootedPath(string? path, bool normalize = false, string? basePath = null)
{
basePath ??= GetWorkingPath();
if (string.IsNullOrEmpty(path))
path = string.Empty;
path = normalize ? string.Empty : basePath;

basePath ??= GetWorkingPath();
var rootedPath = Path.IsPathRooted(path) ? Path.GetFullPath(path) : Path.GetFullPath(Path.Combine(basePath, path));
return normalize ? rootedPath.Replace(BACKSLASH, SLASH) : rootedPath;
}
Expand All @@ -120,20 +120,21 @@ internal static string GetRootedPath(string? path, bool normalize = false, strin
/// </summary>
/// <param name="path">A full or relative path.</param>
/// <param name="normalize">When set to <c>true</c> the returned path uses forward slashes instead of backslashes.</param>
/// <param name="basePath">A base path to use if the <c>path</c> is relative.</param>
/// <returns>A absolute base path.</returns>
/// <remarks>
/// A base path always includes a trailing <c>/</c>.
/// </remarks>
internal static string GetRootedBasePath(string path, bool normalize = false)
internal static string GetRootedBasePath(string path, bool normalize = false, string? basePath = null)
{
if (string.IsNullOrEmpty(path))
path = string.Empty;

var rootedPath = GetRootedPath(path);
var basePath = rootedPath.Length > 0 && IsPathSeparator(rootedPath[rootedPath.Length - 1])
var rootedPath = GetRootedPath(path, basePath: basePath);
var result = rootedPath.Length > 0 && IsPathSeparator(rootedPath[rootedPath.Length - 1])
? rootedPath
: string.Concat(rootedPath, Path.DirectorySeparatorChar);
return normalize ? basePath.Replace(BACKSLASH, SLASH) : basePath;
return normalize ? result.Replace(BACKSLASH, SLASH) : result;
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions src/PSRule/Common/GitHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,28 @@ private static bool TryGitFile(string file, out string filePath, string path = n
return File.Exists(filePath);
}

private static string GetGitDir(string path)
private static string GetGitDir(string path = null)
{
var gitDir = Environment.GetRootedBasePath(path ?? GIT_DEFAULT_PATH);
path = Environment.GetRootedPath(GIT_DEFAULT_PATH, basePath: Environment.GetRootedPath(path));

// Try the case of a submodule.
if (File.Exists(path) && TryReadGitDir(path, out gitDir))
if (File.Exists(path) && TryReadGitDirEntry(path, out var gitDir))
return gitDir;

// Try the simple case of .git/.
return gitDir;
return path;
}

private static bool TryReadGitDir(string path, out string value)
private static bool TryReadGitDirEntry(string filePath, out string value)
{
value = null;
if (!TryReadFirstLineFromGitFile(path, out var line))
if (!TryReadFirstLineFromGitFile(filePath, out var line))
return false;

if (!line.StartsWith(GIT_GITDIR_PREFIX, StringComparison.OrdinalIgnoreCase))
return false;

value = Environment.GetRootedBasePath(line.Substring(8));
value = Environment.GetRootedBasePath(line.Substring(8), basePath: Path.GetDirectoryName(filePath));
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/PSRule.Tests/GitHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private static string GetGitOutput()

private static string GetGitPath()
{
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../../../.git");
return System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../../../");
}

#endregion Helper methods
Expand Down

0 comments on commit d20ba33

Please sign in to comment.