feat(git-provider): Load previous registrations, add debug hook

- add way to redirect debug output
- remove unique constraint on Location
This commit is contained in:
Scott 2026-08-24 16:45:42 +10:00
commit cf336da271
2 changed files with 60 additions and 10 deletions

View file

@ -12,18 +12,48 @@ public class GitManager
private static readonly Lazy<GitManager> GitManagerInstance = new(() => new GitManager()); private static readonly Lazy<GitManager> GitManagerInstance = new(() => new GitManager());
private readonly ConcurrentDictionary<string, InternalGitRegistration> _registrations; private readonly ConcurrentDictionary<string, InternalGitRegistration> _registrations;
private readonly DatabaseManager _db; private readonly DatabaseManager _db;
private static Action<string>? _debugWriterDelegate;
private GitManager() private GitManager()
{ {
Debug.WriteLine($"{nameof(GitManager)} init");
_registrations = new ConcurrentDictionary<string, InternalGitRegistration>(); _registrations = new ConcurrentDictionary<string, InternalGitRegistration>();
_db = new DatabaseManager("git.db"); _db = new DatabaseManager("git.db");
InitialiseRegistrations();
}
/// <summary>
/// Creates up any database tables and loads all previously saved git registrations.
/// </summary>
private void InitialiseRegistrations()
{
_debugWriterDelegate?.Invoke("Initialising GitManager from first run - this should only happen once.");
_db.InConnection(conn => _db.InConnection(conn =>
{ {
conn.CreateTable<InternalGitRegistration>(); var createTableResult = conn.CreateTable<InternalGitRegistration>();
if (createTableResult == CreateTableResult.Created)
{
_debugWriterDelegate?.Invoke($"Created table {InternalGitRegistration.TableName}.");
}
}); });
_debugWriterDelegate?.Invoke("Loading previous registrations from database.");
var registrations = _db.InConnection<List<InternalGitRegistration>>(conn =>
conn.Table<InternalGitRegistration>()
.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; public static GitManager Instance => GitManagerInstance.Value;
@ -55,23 +85,22 @@ public class GitManager
return _db.InConnection<string>(conn => return _db.InConnection<string>(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 // 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. // want to do null checks and a truthy check so I wrap it in a barely-valuable method.
var registrationExists = _db.Exists( var registrationExists = _db.Exists(
$""" $"""
SELECT 1 SELECT 1
FROM {InternalGitRegistration.TableName} FROM {InternalGitRegistration.TableName}
WHERE Name = ? OR WHERE Name = ?
Location = ?
""", """,
gitRegistration.Name, gitRegistration.Name
gitRegistration.Location
); );
if (registrationExists) 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 // Insert the new record
@ -116,6 +145,24 @@ public class GitManager
throw new Exception($"No git repo has been registered with the name {registeredName}"); throw new Exception($"No git repo has been registered with the name {registeredName}");
} }
/// <summary>
/// Registers an output for debug output. <see cref="ClearDebugWriter"/> should be called as soon as the need for output
/// is no longer needed.
/// </summary>
/// <param name="commandRuntime"></param>
public static void SetDebugWriter(Action<string> commandRuntime)
{
_debugWriterDelegate = commandRuntime;
}
/// <summary>
/// Clears any output previously registered with <see cref="SetDebugWriter"/>
/// </summary>
public static void ClearDebugWriter()
{
_debugWriterDelegate = null;
}
/// <summary> /// <summary>
/// Used for internal git registration and handles getting the current branch /// Used for internal git registration and handles getting the current branch
/// </summary> /// </summary>
@ -132,7 +179,6 @@ public class GitManager
[Indexed(Unique = true)] [Indexed(Unique = true)]
public string Name { get; set; } = null!; public string Name { get; set; } = null!;
[Indexed(Unique = true)]
public string Location { get; set; } = null!; public string Location { get; set; } = null!;
public string CurrentBranch => GetCurrentBranch(); public string CurrentBranch => GetCurrentBranch();

View file

@ -24,6 +24,8 @@ public sealed class NewGitRepoCommand : PSCmdlet
var pwd = this.SessionState.Path.CurrentLocation.Path; var pwd = this.SessionState.Path.CurrentLocation.Path;
WriteDebug("Checking if current directory is a git repository..."); WriteDebug("Checking if current directory is a git repository...");
GitManager.SetDebugWriter(WriteDebug);
var repoFolfder = IsGitRepo(pwd); var repoFolfder = IsGitRepo(pwd);
if (repoFolfder is not null) if (repoFolfder is not null)
@ -43,6 +45,8 @@ public sealed class NewGitRepoCommand : PSCmdlet
); );
} }
GitManager.ClearDebugWriter();
base.BeginProcessing(); base.BeginProcessing();
} }