From d20ba33d6f0d83d9f689920f7e3cf27471347c3f Mon Sep 17 00:00:00 2001 From: Bernie White Date: Sat, 18 May 2024 15:17:08 +1000 Subject: [PATCH] Fixes for handing git paths (#1822) --- src/PSRule.Benchmark/packages.lock.json | 33 ++++++++++++++++--------- src/PSRule.Types/Environment.cs | 13 +++++----- src/PSRule/Common/GitHelper.cs | 14 +++++------ tests/PSRule.Tests/GitHelperTests.cs | 2 +- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/PSRule.Benchmark/packages.lock.json b/src/PSRule.Benchmark/packages.lock.json index 6c0ce30334..8912b43798 100644 --- a/src/PSRule.Benchmark/packages.lock.json +++ b/src/PSRule.Benchmark/packages.lock.json @@ -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": { @@ -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", diff --git a/src/PSRule.Types/Environment.cs b/src/PSRule.Types/Environment.cs index 2f05eaa809..f1a3c70ac4 100644 --- a/src/PSRule.Types/Environment.cs +++ b/src/PSRule.Types/Environment.cs @@ -107,10 +107,10 @@ public static CultureInfo GetCurrentCulture() /// A absolute path. 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; } @@ -120,20 +120,21 @@ internal static string GetRootedPath(string? path, bool normalize = false, strin /// /// A full or relative path. /// When set to true the returned path uses forward slashes instead of backslashes. + /// A base path to use if the path is relative. /// A absolute base path. /// /// A base path always includes a trailing /. /// - 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; } /// diff --git a/src/PSRule/Common/GitHelper.cs b/src/PSRule/Common/GitHelper.cs index a4b8f00c09..1e9b0e28f0 100644 --- a/src/PSRule/Common/GitHelper.cs +++ b/src/PSRule/Common/GitHelper.cs @@ -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; } diff --git a/tests/PSRule.Tests/GitHelperTests.cs b/tests/PSRule.Tests/GitHelperTests.cs index f762e8d340..2cbf3b5298 100644 --- a/tests/PSRule.Tests/GitHelperTests.cs +++ b/tests/PSRule.Tests/GitHelperTests.cs @@ -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