From dedea9213b540dd3142d2a399ffec03717ada123 Mon Sep 17 00:00:00 2001 From: Scott Date: Fri, 7 Aug 2026 14:58:16 +1000 Subject: [PATCH] refactor(git-provider): Basic manager class implementation and dummy Set-GitRepo command --- src/PowershellModule/Git/NewGitRepoCommand.cs | 116 ++++++++++++++++-- 1 file changed, 104 insertions(+), 12 deletions(-) diff --git a/src/PowershellModule/Git/NewGitRepoCommand.cs b/src/PowershellModule/Git/NewGitRepoCommand.cs index 89891f6..583a616 100644 --- a/src/PowershellModule/Git/NewGitRepoCommand.cs +++ b/src/PowershellModule/Git/NewGitRepoCommand.cs @@ -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; + } + + /// + /// The directory details of the directory returned from git rev-parse --show-toplevel + /// + private class ParsedGitFolderDetails + { + /// + /// The full path to the top level folder containing a git repository + /// + public string Directory { get; set; } = null!; + + /// + /// The last folder name of the directory + /// + 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 GitManagerInstance = new(() => new GitManager()); + public static GitManager Instance => GitManagerInstance.Value; + + private GitManager() + { + Console.WriteLine($"{nameof(GitManager)} init"); + } } \ No newline at end of file