diff --git a/src/ModuleCore/Git/GitRepoRegistrationManager.cs b/src/ModuleCore/Git/GitManager.cs similarity index 93% rename from src/ModuleCore/Git/GitRepoRegistrationManager.cs rename to src/ModuleCore/Git/GitManager.cs index d6ddd57..f16a313 100644 --- a/src/ModuleCore/Git/GitRepoRegistrationManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -6,17 +6,15 @@ using SQLite; namespace ModuleCore.Git; -/// -/// Manages git repo registration, including creating any registration persistence via a backing -/// -public class GitRepoRegistrationManager +// TODO: better name for this +public class GitManager { - private static readonly Lazy GitManagerInstance = new(() => new GitRepoRegistrationManager()); + private static readonly Lazy GitManagerInstance = new(() => new GitManager()); private static Action? _debugWriterDelegate; private readonly DatabaseManager _db; private readonly ConcurrentDictionary _registrations; - private GitRepoRegistrationManager(string? databaseName = null) + private GitManager(string? databaseName = null) { _registrations = new ConcurrentDictionary(); // Regular usage of this constructor will never pass a database name in. Currently only tests should be hitting @@ -27,15 +25,15 @@ public class GitRepoRegistrationManager } /// - /// Returns the current instance. If no instance has been created, returns a new instance + /// Returns the current instance. If no instance has been created, returns a new instance /// and then the same instance every call after. /// - public static GitRepoRegistrationManager Instance => GitManagerInstance.Value; + public static GitManager Instance => GitManagerInstance.Value; /// /// Always returns a new clean instance of GitManager /// - internal static GitRepoRegistrationManager InternalFreshInstance(string databaseName) => new(databaseName); + internal static GitManager InternalFreshInstance(string databaseName) => new(databaseName); /// /// Deletes the underlying database file. @@ -390,4 +388,20 @@ public class GitRepoRegistrationManager return _currentBranch.Trim(); } } +} + +/// +/// The directory details of the directory returned from git rev-parse --show-toplevel +/// +public class ParsedGitFolderDetails +{ + /// + /// The full path to the top level folder containing a git repository + /// + public string Directory { get; init; } = null!; + + /// + /// The last folder name of the directory + /// + public string Folder { get; init; } = null!; } \ No newline at end of file diff --git a/src/ModuleCore/Git/Models/ParsedGitFolderDetails.cs b/src/ModuleCore/Git/Models/ParsedGitFolderDetails.cs deleted file mode 100644 index a785be0..0000000 --- a/src/ModuleCore/Git/Models/ParsedGitFolderDetails.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace ModuleCore.Git.Models; - -/// -/// The directory details of the directory returned from git rev-parse --show-toplevel -/// -public class ParsedGitFolderDetails -{ - /// - /// The full path to the top level folder containing a git repository - /// - public string Directory { get; init; } = null!; - - /// - /// The last folder name of the directory - /// - public string Folder { get; init; } = null!; -} \ No newline at end of file diff --git a/src/PowershellModule/Git/Commands/GetGitRepoRegistrationCommand.cs b/src/PowershellModule/Git/Commands/GetGitRepoRegistrationCommand.cs index 506aee2..1b5f193 100644 --- a/src/PowershellModule/Git/Commands/GetGitRepoRegistrationCommand.cs +++ b/src/PowershellModule/Git/Commands/GetGitRepoRegistrationCommand.cs @@ -13,7 +13,7 @@ public class GetGitRepoRegistrationCommand : PSCmdlet { protected override void BeginProcessing() { - var repos = GitRepoRegistrationManager.Instance.ListRepos(); + var repos = GitManager.Instance.ListRepos(); WriteObject(repos); diff --git a/src/PowershellModule/Git/Commands/NewGitRepoRegistrationCommand.cs b/src/PowershellModule/Git/Commands/NewGitRepoRegistrationCommand.cs index 9e0729f..47578dd 100644 --- a/src/PowershellModule/Git/Commands/NewGitRepoRegistrationCommand.cs +++ b/src/PowershellModule/Git/Commands/NewGitRepoRegistrationCommand.cs @@ -17,19 +17,19 @@ public sealed class NewGitRepoRegistrationCommand : PSCmdlet { try { - GitRepoRegistrationManager.SetDebugWriter(WriteDebug); + GitManager.SetDebugWriter(WriteDebug); // Test that we're in a git repo first. If we aren't (or git isn't available), this method will throw // so we don't need to handle for null (yet). - var repoFolder = GitRepoRegistrationManager.IsGitRepo(SessionState.Path.CurrentLocation.Path); + var repoFolder = GitManager.IsGitRepo(SessionState.Path.CurrentLocation.Path); if (string.IsNullOrWhiteSpace(Name)) { WriteDebug("No name given for registration, defaulting to git folder root."); } - GitRepoRegistrationManager.Instance.RegisterRepo(repoFolder.Directory, Name ?? repoFolder.Folder); - GitRepoRegistrationManager.ClearDebugWriter(); + GitManager.Instance.RegisterRepo(repoFolder.Directory, Name ?? repoFolder.Folder); + GitManager.ClearDebugWriter(); base.BeginProcessing(); } diff --git a/src/PowershellModule/Git/Commands/RemoveGitRepoRegistrationCommand.cs b/src/PowershellModule/Git/Commands/RemoveGitRepoRegistrationCommand.cs index a79060e..bfd1834 100644 --- a/src/PowershellModule/Git/Commands/RemoveGitRepoRegistrationCommand.cs +++ b/src/PowershellModule/Git/Commands/RemoveGitRepoRegistrationCommand.cs @@ -17,21 +17,21 @@ public class RemoveGitRepoRegistrationCommand : PSCmdlet { try { - GitRepoRegistrationManager.SetDebugWriter(WriteDebug); + GitManager.SetDebugWriter(WriteDebug); // If we aren't given a value for the Name argument, default behaviour is to attempt to remove a registration // by the current git repo folder name for the current location. // If we have a name, don't bother testing for a git repo, just attempt to remove the registration by name // regardless of where we're being called from var registrationNameToRemove = string.IsNullOrEmpty(Name) - ? GitRepoRegistrationManager.IsGitRepo(SessionState.Path.CurrentLocation.Path).Folder + ? GitManager.IsGitRepo(SessionState.Path.CurrentLocation.Path).Folder : Name; - GitRepoRegistrationManager.Instance.UnregisterRepo(registrationNameToRemove); + GitManager.Instance.UnregisterRepo(registrationNameToRemove); // Removing a registration works similar to registering a new one - we either remove by exact name, or by // the folder if no name is given (so a user can remove a registration from a git repo they're currently in) - GitRepoRegistrationManager.ClearDebugWriter(); + GitManager.ClearDebugWriter(); base.BeginProcessing(); } diff --git a/src/PowershellModule/Git/Commands/ShowGitRepoRegistrationCommand.cs b/src/PowershellModule/Git/Commands/ShowGitRepoRegistrationCommand.cs index e4eb647..4300f71 100644 --- a/src/PowershellModule/Git/Commands/ShowGitRepoRegistrationCommand.cs +++ b/src/PowershellModule/Git/Commands/ShowGitRepoRegistrationCommand.cs @@ -21,7 +21,7 @@ public class ShowGitRepoRegistrationCommand : PSCmdlet protected override void BeginProcessing() { - var location = GitRepoRegistrationManager.Instance.GetDirectoryForRegisteredRepo(Name); + var location = GitManager.Instance.GetDirectoryForRegisteredRepo(Name); // By default instead of doing the same as cd, we instead do pushd so a user can popd straight back to where // they came from. diff --git a/tests/ModuleTests/Git/AddRegistrationTests.cs b/tests/ModuleTests/Git/AddRegistrationTests.cs index c862773..883e595 100644 --- a/tests/ModuleTests/Git/AddRegistrationTests.cs +++ b/tests/ModuleTests/Git/AddRegistrationTests.cs @@ -23,7 +23,7 @@ public class AddRegistrationTests public Task BasicRepoRegistration((int testId, string path) testData) { Settings.UseFileName($"{nameof(BasicRepoRegistration)}_{testData.testId}"); - var gitManager = GitRepoRegistrationManager.InternalFreshInstance(nameof(BasicRepoRegistration)); + var gitManager = GitManager.InternalFreshInstance(nameof(BasicRepoRegistration)); try { @@ -44,7 +44,7 @@ public class AddRegistrationTests public void RepoRegistrationWithEmptyName() { Settings.UseFileName(nameof(RepoRegistrationWithEmptyName)); - var gitManager = GitRepoRegistrationManager.InternalFreshInstance(nameof(RepoRegistrationWithEmptyName)); + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithEmptyName)); var testRepoAbsolutePath = "Test:/some/test/repo"; try @@ -63,7 +63,7 @@ public class AddRegistrationTests public void RepoRegistrationWithNullName() { Settings.UseFileName(nameof(RepoRegistrationWithNullName)); - var gitManager = GitRepoRegistrationManager.InternalFreshInstance(nameof(RepoRegistrationWithNullName)); + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithNullName)); var testRepoAbsolutePath = "Test:/some/test/repo"; try { @@ -84,7 +84,7 @@ public class AddRegistrationTests { Settings.UseFileName(nameof(RepoRegistrationWithWhitespaceName)); - var gitManager = GitRepoRegistrationManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); var testRepoAbsolutePath = "Test:/some/test/repo"; try { @@ -102,7 +102,7 @@ public class AddRegistrationTests public void DuplicateRepoRegistrationShouldFail() { Settings.UseFileName(nameof(RepoRegistrationWithWhitespaceName)); - var gitManager = GitRepoRegistrationManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); var testRepoAbsolutePath = "Test:/some/test/repo"; string[] paths = ["test", "nested", "path"]; var names = (NormalSeparator: string.Join(Path.DirectorySeparatorChar, paths), AltSeparator: string.Join(Path.AltDirectorySeparatorChar, paths)); @@ -110,6 +110,7 @@ public class AddRegistrationTests try { var firstRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, names.NormalSeparator); + // TODO: make nested registrations fail in both directions and test var secondRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, Path.Combine(paths[..1])); Assert.Equal(Path.Combine(paths), firstRegistration); @@ -127,7 +128,7 @@ public class AddRegistrationTests public void DuplicateRepoRegistrationDifferentSlashShouldNotFail() { Settings.UseFileName(nameof(RepoRegistrationWithWhitespaceName)); - var gitManager = GitRepoRegistrationManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); + var gitManager = GitManager.InternalFreshInstance(nameof(RepoRegistrationWithWhitespaceName)); var testRepoAbsolutePath = "Test:/some/test/repo"; string[] paths = ["test", "nested", "path"]; var names = (NormalSeparator: string.Join(Path.DirectorySeparatorChar, paths), AltSeparator: string.Join(Path.AltDirectorySeparatorChar, paths)); @@ -135,6 +136,7 @@ public class AddRegistrationTests try { var firstRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, names.NormalSeparator); + // TODO: make nested registrations fail in both directions and test var secondRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, Path.Combine(paths[..1])); var differentPathSeparatorRegistration = gitManager.RegisterRepo(testRepoAbsolutePath, names.AltSeparator);