diff --git a/src/ModuleCore/Git/GitManager.cs b/src/ModuleCore/Git/GitManager.cs index a530c4e..36f2886 100644 --- a/src/ModuleCore/Git/GitManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -12,18 +12,48 @@ public class GitManager private static readonly Lazy GitManagerInstance = new(() => new GitManager()); private readonly ConcurrentDictionary _registrations; private readonly DatabaseManager _db; + private static Action? _debugWriterDelegate; private GitManager() { - Debug.WriteLine($"{nameof(GitManager)} init"); - _registrations = new ConcurrentDictionary(); _db = new DatabaseManager("git.db"); + InitialiseRegistrations(); + } + + /// + /// 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 => { - conn.CreateTable(); + 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."); + } + } } public static GitManager Instance => GitManagerInstance.Value; @@ -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,6 +145,24 @@ 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 /// @@ -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(); diff --git a/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs b/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs index 103cffa..9be33a4 100644 --- a/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs +++ b/src/PowershellModule/Git/Commands/NewGitRepoCommand.cs @@ -24,6 +24,8 @@ public sealed class NewGitRepoCommand : PSCmdlet var pwd = this.SessionState.Path.CurrentLocation.Path; WriteDebug("Checking if current directory is a git repository..."); + GitManager.SetDebugWriter(WriteDebug); + var repoFolfder = IsGitRepo(pwd); if (repoFolfder is not null) @@ -43,6 +45,8 @@ public sealed class NewGitRepoCommand : PSCmdlet ); } + GitManager.ClearDebugWriter(); + base.BeginProcessing(); }