refactor(git-provider): Basic manager class implementation and dummy Set-GitRepo command

This commit is contained in:
Scott 2026-08-07 14:58:16 +10:00
commit dedea9213b

View file

@ -1,5 +1,6 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Management.Automation;
using SQLite;
@ -17,23 +18,114 @@ public class NewGitRepoCommand : PSCmdlet
HelpMessage = "Reference name for the repo")]
public string Name { get; set; }
public NewGitRepoCommand()
{
Console.WriteLine($"{nameof(NewGitRepoCommand)} init");
var gm = GitManager.Instance;
}
protected override void BeginProcessing()
{
using var conn = new SQLiteConnection("./test.db");
//using var conn = new SQLiteConnection("./test.db");
var pwd = this.SessionState.Path.CurrentLocation.Path;
WriteObject("Checking if current directory is a git repository...");
var ps = new ProcessStartInfo( //$"git -C \"{pwd}\" rev-parse --show-toplevel")
"git", ["rev-parse", "--show-toplevel"])
{
RedirectStandardOutput = true,
RedirectStandardError = true,
WorkingDirectory = pwd
};
var a = Process.Start(ps);
Console.WriteLine(a.StandardOutput.ReadToEnd());
Console.WriteLine(a.StandardError.ReadToEnd());
var repoFolfder = IsGitRepo(pwd);
base.BeginProcessing();
}
private ParsedGitFolderDetails? IsGitRepo(string path)
{
var ps = new ProcessStartInfo("git",
["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
// special with, it'll be unhandled and powershell will handle it
var gitProcess = Process.Start(ps);
// This probably shouldn't be possible? Not really sure of the conditions where the process could be started
// but return null, but I'm going to consider that unrecoverable error territory
if (gitProcess is null)
{
throw new Exception("git failed to start");
}
gitProcess.WaitForExit();
if (!gitProcess.StandardOutput.EndOfStream)
{
var directory = gitProcess.StandardOutput.ReadToEnd();
// Gotta trim what we get as it might already have a newline character at the end
var dirInfo = new DirectoryInfo(directory.Trim());
var repoFolderInfo = new ParsedGitFolderDetails
{
Directory = dirInfo.FullName,
Folder = dirInfo.Name,
};
return repoFolderInfo;
}
if (!gitProcess.StandardError.EndOfStream)
{
var errorAsException = new Exception(gitProcess.StandardError.ReadToEnd());
WriteError(new ErrorRecord(errorAsException, "git-not-found", ErrorCategory.FromStdErr, null));
}
return null;
}
/// <summary>
/// The directory details of the directory returned from git rev-parse --show-toplevel
/// </summary>
private class ParsedGitFolderDetails
{
/// <summary>
/// The full path to the top level folder containing a git repository
/// </summary>
public string Directory { get; set; } = null!;
/// <summary>
/// The last folder name of the directory
/// </summary>
public string Folder { get; set; } = null!;
}
}
[Cmdlet(VerbsCommon.Set, Noun)]
public class SetGitRepoCommand : PSCmdlet
{
private const string Noun = "GitRepo";
public SetGitRepoCommand()
{
Console.WriteLine($"{nameof(NewGitRepoCommand)} init");
var a = GitManager.Instance;
}
protected override void BeginProcessing()
{
base.BeginProcessing();
}
}
// TODO: better name for this
public class GitManager
{
private static readonly Lazy<GitManager> GitManagerInstance = new(() => new GitManager());
public static GitManager Instance => GitManagerInstance.Value;
private GitManager()
{
Console.WriteLine($"{nameof(GitManager)} init");
}
}