diff --git a/src/ModuleCore/Git/GitManager.cs b/src/ModuleCore/Git/GitManager.cs index a530c4e..8b2b6f4 100644 --- a/src/ModuleCore/Git/GitManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -10,20 +10,16 @@ namespace ModuleCore.Git; public class GitManager { private static readonly Lazy GitManagerInstance = new(() => new GitManager()); - private readonly ConcurrentDictionary _registrations; + private static Action? _debugWriterDelegate; private readonly DatabaseManager _db; + private readonly ConcurrentDictionary _registrations; private GitManager() { - Debug.WriteLine($"{nameof(GitManager)} init"); - _registrations = new ConcurrentDictionary(); _db = new DatabaseManager("git.db"); - _db.InConnection(conn => - { - conn.CreateTable(); - }); + InitialiseRegistrations(); } public static GitManager Instance => GitManagerInstance.Value; @@ -33,6 +29,40 @@ 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. @@ -55,23 +85,22 @@ public class GitManager return _db.InConnection(conn => { - // Query if we already have a registration either by name or location. + // 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. // 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 = ? OR - Location = ? + WHERE Name = ? """, - gitRegistration.Name, - gitRegistration.Location + gitRegistration.Name ); if (registrationExists) { - throw new Exception($"A Git repo is already registered with the name {registrationName} or location {absoluteRepositoryLocation}"); + throw new Exception($"A Git repo is already registered with the name {registrationName}."); } // Insert the new record @@ -116,15 +145,33 @@ 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; } @@ -132,7 +179,6 @@ 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(); @@ -184,7 +230,8 @@ 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; - return _currentBranch; + // The branch name could (will) have a newline character at the end, so we trim that off + return _currentBranch.Trim(); } } } \ No newline at end of file diff --git a/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs b/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs index 103cffa..29f629a 100644 --- a/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs +++ b/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs @@ -15,20 +15,15 @@ public sealed class NewGitRepoCommand : PSCmdlet HelpMessage = "Reference name for the repo")] public string? Name { get; set; } - public NewGitRepoCommand() - { - } - protected override void BeginProcessing() { - var pwd = this.SessionState.Path.CurrentLocation.Path; - WriteDebug("Checking if current directory is a git repository..."); + GitManager.SetDebugWriter(WriteDebug); - var repoFolfder = IsGitRepo(pwd); + var repoFolder = IsGitRepo(SessionState.Path.CurrentLocation.Path); - if (repoFolfder is not null) + if (repoFolder is not null) { - GitManager.Instance.RegisterRepo(repoFolfder.Directory, Name ?? repoFolfder.Folder); + GitManager.Instance.RegisterRepo(repoFolder.Directory, Name ?? repoFolder.Folder); } else { @@ -43,17 +38,20 @@ 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", - ["rev-parse", "--show-toplevel"]) + ["-C", path, "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 @@ -75,6 +73,8 @@ 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,