using System; using System.Diagnostics; using System.IO; using System.Management.Automation; using SQLite; namespace PowershellModule.Git; [Cmdlet(VerbsCommon.New, Noun)] public class NewGitRepoCommand : PSCmdlet { private const string Noun = "GitRepo"; [Parameter( Mandatory = true, Position = 0, ValueFromPipeline = true, 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"); var pwd = this.SessionState.Path.CurrentLocation.Path; WriteObject("Checking if current directory is a git repository..."); 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"); } }