feat(git-provider): Display current branch when listing registered repos
- change setters to init when parsing git folders - change Console.WriteLine to Debug.WriteLine in GitManager constructor
This commit is contained in:
parent
52f7260bf4
commit
39022e4186
3 changed files with 64 additions and 4 deletions
|
|
@ -1,4 +1,5 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ModuleCore.Git;
|
||||
|
||||
|
|
@ -17,13 +18,67 @@ public class GitManager
|
|||
{
|
||||
public required string Name { get; set; }
|
||||
public required string Location { get; set; }
|
||||
public string CurrentBranch => GetCurrentBranch();
|
||||
|
||||
private string _currentBranch = string.Empty;
|
||||
private long _nextCheckTime;
|
||||
|
||||
// TODO: not fully decided on if I want this feature or not, but keeping it in for now
|
||||
private string GetCurrentBranch()
|
||||
{
|
||||
var now = DateTime.Now;
|
||||
// git branch should be quick enough that even with a large number of registrations this shouldn't be that slow
|
||||
// when doing Get-GitRepo, but regardless we still only get the current branch via git if it's been some amount
|
||||
// of time since the last time we did.
|
||||
if (now.Ticks < _nextCheckTime)
|
||||
{
|
||||
return _currentBranch;
|
||||
}
|
||||
|
||||
// use -C for the git command so we don't need to set the working directory and the git command can be run
|
||||
// from anywhere against the appropriate location
|
||||
var ps = new ProcessStartInfo("git",
|
||||
["-C", Location, "branch", "--show-current"])
|
||||
{
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
};
|
||||
|
||||
// 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)
|
||||
{
|
||||
_currentBranch = gitProcess.StandardOutput.ReadToEnd();
|
||||
}
|
||||
|
||||
if (!gitProcess.StandardError.EndOfStream)
|
||||
{
|
||||
_currentBranch = gitProcess.StandardError.ReadToEnd();
|
||||
}
|
||||
|
||||
// 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;
|
||||
|
||||
return _currentBranch;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly ConcurrentDictionary<string, GitRegistration> _registrations;
|
||||
|
||||
private GitManager()
|
||||
{
|
||||
Console.WriteLine($"{nameof(GitManager)} init");
|
||||
Debug.WriteLine($"{nameof(GitManager)} init");
|
||||
|
||||
_registrations = new ConcurrentDictionary<string, GitRegistration>();
|
||||
}
|
||||
|
|
@ -55,7 +110,7 @@ public class GitManager
|
|||
|
||||
public List<GitReg> ListRepos()
|
||||
{
|
||||
return _registrations.Select(x => new GitReg() { Name = x.Value.Name, Location = x.Value.Location }).ToList();
|
||||
return _registrations.Select(x => new GitReg() { Name = x.Value.Name, Location = x.Value.Location, CurrentBranch = x.Value.CurrentBranch }).ToList();
|
||||
}
|
||||
|
||||
public string GetRepo(string? registeredName)
|
||||
|
|
@ -78,4 +133,5 @@ public class GitReg
|
|||
{
|
||||
public required string Name { get; set; }
|
||||
public required string Location { get; set; }
|
||||
public required string CurrentBranch { get; set; }
|
||||
}
|
||||
Loading…
Reference in a new issue