Compare commits
2 changed files with 27 additions and 74 deletions
|
|
@ -10,16 +10,20 @@ namespace ModuleCore.Git;
|
|||
public class GitManager
|
||||
{
|
||||
private static readonly Lazy<GitManager> GitManagerInstance = new(() => new GitManager());
|
||||
private static Action<string>? _debugWriterDelegate;
|
||||
private readonly DatabaseManager _db;
|
||||
private readonly ConcurrentDictionary<string, InternalGitRegistration> _registrations;
|
||||
private readonly DatabaseManager _db;
|
||||
|
||||
private GitManager()
|
||||
{
|
||||
Debug.WriteLine($"{nameof(GitManager)} init");
|
||||
|
||||
_registrations = new ConcurrentDictionary<string, InternalGitRegistration>();
|
||||
_db = new DatabaseManager("git.db");
|
||||
|
||||
InitialiseRegistrations();
|
||||
_db.InConnection(conn =>
|
||||
{
|
||||
conn.CreateTable<InternalGitRegistration>();
|
||||
});
|
||||
}
|
||||
|
||||
public static GitManager Instance => GitManagerInstance.Value;
|
||||
|
|
@ -29,40 +33,6 @@ public class GitManager
|
|||
/// </summary>
|
||||
internal static GitManager InternalFreshInstance => new();
|
||||
|
||||
/// <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 =>
|
||||
{
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a git repository based on an absolute location. If <paramref name="registrationName" /> 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<string>(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}");
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// Used for internal git registration and handles getting the current branch
|
||||
/// </summary>
|
||||
[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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in a new issue