diff --git a/src/ModuleCore/Git/GitManager.cs b/src/ModuleCore/Git/GitManager.cs index 8b2b6f4..a530c4e 100644 --- a/src/ModuleCore/Git/GitManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -10,16 +10,20 @@ namespace ModuleCore.Git; public class GitManager { private static readonly Lazy GitManagerInstance = new(() => new GitManager()); - private static Action? _debugWriterDelegate; - private readonly DatabaseManager _db; private readonly ConcurrentDictionary _registrations; + private readonly DatabaseManager _db; private GitManager() { + Debug.WriteLine($"{nameof(GitManager)} init"); + _registrations = new ConcurrentDictionary(); _db = new DatabaseManager("git.db"); - InitialiseRegistrations(); + _db.InConnection(conn => + { + conn.CreateTable(); + }); } public static GitManager Instance => GitManagerInstance.Value; @@ -29,40 +33,6 @@ public class GitManager /// internal static GitManager InternalFreshInstance => new(); - /// - /// Creates up any database tables and loads all previously saved git registrations. - /// - private void InitialiseRegistrations() - { - _debugWriterDelegate?.Invoke("Initialising GitManager from first run - this should only happen once."); - - _db.InConnection(conn => - { - var createTableResult = conn.CreateTable(); - - if (createTableResult == CreateTableResult.Created) - { - _debugWriterDelegate?.Invoke($"Created table {InternalGitRegistration.TableName}."); - } - }); - - _debugWriterDelegate?.Invoke("Loading previous registrations from database."); - - var registrations = _db.InConnection>(conn => - conn.Table() - .ToList() - ); - - foreach (var internalGitRegistration in registrations) - { - _debugWriterDelegate?.Invoke($"Loading {internalGitRegistration.Name} ({internalGitRegistration.Id}) from database..."); - if (!_registrations.TryAdd(internalGitRegistration.Name, internalGitRegistration)) - { - _debugWriterDelegate?.Invoke("...failed to restore - potential duplicate name."); - } - } - } - /// /// Registers a git repository based on an absolute location. If is null or empty, /// the registration will use the folder name for the git repo at the top level. @@ -85,22 +55,23 @@ public class GitManager return _db.InConnection(conn => { - // Query if we already have a registration either by name. Previously we also checked by location, but I - // decided to stick with constraining to the name only, same as the key used for the dictionary. + // Query if we already have a registration either by name or location. // tbh this is a bit of a janky way to do exists when I have to pass the query in anyway, but I just didn't // want to do null checks and a truthy check so I wrap it in a barely-valuable method. var registrationExists = _db.Exists( $""" SELECT 1 FROM {InternalGitRegistration.TableName} - WHERE Name = ? + WHERE Name = ? OR + Location = ? """, - gitRegistration.Name + gitRegistration.Name, + gitRegistration.Location ); if (registrationExists) { - throw new Exception($"A Git repo is already registered with the name {registrationName}."); + throw new Exception($"A Git repo is already registered with the name {registrationName} or location {absoluteRepositoryLocation}"); } // Insert the new record @@ -145,33 +116,15 @@ public class GitManager throw new Exception($"No git repo has been registered with the name {registeredName}"); } - /// - /// Registers an output for debug output. should be called as soon as the need for output - /// is no longer needed. - /// - /// - public static void SetDebugWriter(Action commandRuntime) - { - _debugWriterDelegate = commandRuntime; - } - - /// - /// Clears any output previously registered with - /// - public static void ClearDebugWriter() - { - _debugWriterDelegate = null; - } - /// /// Used for internal git registration and handles getting the current branch /// [Table(TableName)] private class InternalGitRegistration { - internal const string TableName = "GitRegistration"; private string _currentBranch = string.Empty; private long _nextCheckTime; + internal const string TableName = "GitRegistration"; [PrimaryKey] public Guid Id { get; set; } @@ -179,6 +132,7 @@ public class GitManager [Indexed(Unique = true)] public string Name { get; set; } = null!; + [Indexed(Unique = true)] public string Location { get; set; } = null!; public string CurrentBranch => GetCurrentBranch(); @@ -230,8 +184,7 @@ public class GitManager // Set the next check to be in the future so we don't hold up any list commands every time. _nextCheckTime = now.AddMinutes(15).Ticks; - // The branch name could (will) have a newline character at the end, so we trim that off - return _currentBranch.Trim(); + return _currentBranch; } } } \ No newline at end of file diff --git a/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs b/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs index 29f629a..103cffa 100644 --- a/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs +++ b/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs @@ -15,15 +15,20 @@ public sealed class NewGitRepoCommand : PSCmdlet HelpMessage = "Reference name for the repo")] public string? Name { get; set; } + public NewGitRepoCommand() + { + } + protected override void BeginProcessing() { - GitManager.SetDebugWriter(WriteDebug); + var pwd = this.SessionState.Path.CurrentLocation.Path; + WriteDebug("Checking if current directory is a git repository..."); - var repoFolder = IsGitRepo(SessionState.Path.CurrentLocation.Path); + var repoFolfder = IsGitRepo(pwd); - if (repoFolder is not null) + if (repoFolfder is not null) { - GitManager.Instance.RegisterRepo(repoFolder.Directory, Name ?? repoFolder.Folder); + GitManager.Instance.RegisterRepo(repoFolfder.Directory, Name ?? repoFolfder.Folder); } else { @@ -38,20 +43,17 @@ public sealed class NewGitRepoCommand : PSCmdlet ); } - GitManager.ClearDebugWriter(); - base.BeginProcessing(); } private ParsedGitFolderDetails? IsGitRepo(string path) { - WriteDebug("Checking if current directory is a git repository..."); - var ps = new ProcessStartInfo("git", - ["-C", path, "rev-parse", "--show-toplevel"]) + ["rev-parse", "--show-toplevel"]) { RedirectStandardOutput = true, RedirectStandardError = true, + WorkingDirectory = path }; // If the user doesn't have git on their path, this will throw an exception that I don't have to do anything @@ -73,8 +75,6 @@ public sealed class NewGitRepoCommand : PSCmdlet // Gotta trim what we get as it might already have a newline character at the end var dirInfo = new DirectoryInfo(directory.Trim()); - WriteDebug("...location is a git repo (duh)."); - var repoFolderInfo = new ParsedGitFolderDetails { Directory = dirInfo.FullName,