refactor(git-provider): Rename GitReg to GitRegistration
- rename private GitRegistration to InternalGitRegistration
This commit is contained in:
parent
39022e4186
commit
54d5dd25b9
2 changed files with 67 additions and 62 deletions
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Concurrent;
|
||||
using System.Diagnostics;
|
||||
using ModuleCore.Git.Models;
|
||||
|
||||
namespace ModuleCore.Git;
|
||||
|
||||
|
|
@ -14,7 +15,64 @@ public class GitManager
|
|||
/// </summary>
|
||||
internal static GitManager InternalFreshInstance => new();
|
||||
|
||||
private class GitRegistration
|
||||
private readonly ConcurrentDictionary<string, InternalGitRegistration> _registrations;
|
||||
|
||||
private GitManager()
|
||||
{
|
||||
Debug.WriteLine($"{nameof(GitManager)} init");
|
||||
|
||||
_registrations = new ConcurrentDictionary<string, InternalGitRegistration>();
|
||||
}
|
||||
|
||||
/// <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.
|
||||
/// </summary>
|
||||
/// <param name="absoluteRepositoryLocation"></param>
|
||||
/// <param name="registrationName"></param>
|
||||
/// <returns>The normalised string the repository was registered against</returns>
|
||||
public string RegisterRepo(string absoluteRepositoryLocation, string registrationName)
|
||||
{
|
||||
registrationName = string.IsNullOrWhiteSpace(registrationName)
|
||||
? new DirectoryInfo(absoluteRepositoryLocation).Name
|
||||
: registrationName;
|
||||
|
||||
if (_registrations.TryAdd(registrationName, new InternalGitRegistration()
|
||||
{
|
||||
Name = registrationName,
|
||||
Location = absoluteRepositoryLocation,
|
||||
}))
|
||||
{
|
||||
return registrationName;
|
||||
}
|
||||
|
||||
throw new Exception($"Git repo already registered with the name {registrationName}");
|
||||
}
|
||||
|
||||
public List<GitRegistration> ListRepos()
|
||||
{
|
||||
return _registrations.Select(x => new GitRegistration() { Name = x.Value.Name, Location = x.Value.Location, CurrentBranch = x.Value.CurrentBranch }).ToList();
|
||||
}
|
||||
|
||||
public string GetRepo(string? registeredName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(registeredName))
|
||||
{
|
||||
throw new Exception("Name cannot be null");
|
||||
}
|
||||
|
||||
if (_registrations.TryGetValue(registeredName, out var registration))
|
||||
{
|
||||
return registration.Location;
|
||||
}
|
||||
|
||||
throw new Exception($"No git repo has been registered with the name {registeredName}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used for internal git registration and handles getting the current branch
|
||||
/// </summary>
|
||||
private class InternalGitRegistration
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public required string Location { get; set; }
|
||||
|
|
@ -73,65 +131,4 @@ public class GitManager
|
|||
return _currentBranch;
|
||||
}
|
||||
}
|
||||
|
||||
private readonly ConcurrentDictionary<string, GitRegistration> _registrations;
|
||||
|
||||
private GitManager()
|
||||
{
|
||||
Debug.WriteLine($"{nameof(GitManager)} init");
|
||||
|
||||
_registrations = new ConcurrentDictionary<string, GitRegistration>();
|
||||
}
|
||||
|
||||
/// <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.
|
||||
/// </summary>
|
||||
/// <param name="absoluteRepositoryLocation"></param>
|
||||
/// <param name="registrationName"></param>
|
||||
/// <returns>The normalised string the repository was registered against</returns>
|
||||
public string RegisterRepo(string absoluteRepositoryLocation, string registrationName)
|
||||
{
|
||||
registrationName = string.IsNullOrWhiteSpace(registrationName)
|
||||
? new DirectoryInfo(absoluteRepositoryLocation).Name
|
||||
: registrationName;
|
||||
|
||||
if (_registrations.TryAdd(registrationName, new GitRegistration()
|
||||
{
|
||||
Name = registrationName,
|
||||
Location = absoluteRepositoryLocation,
|
||||
}))
|
||||
{
|
||||
return registrationName;
|
||||
}
|
||||
|
||||
throw new Exception($"Git repo already registered with the name {registrationName}");
|
||||
}
|
||||
|
||||
public List<GitReg> ListRepos()
|
||||
{
|
||||
return _registrations.Select(x => new GitReg() { Name = x.Value.Name, Location = x.Value.Location, CurrentBranch = x.Value.CurrentBranch }).ToList();
|
||||
}
|
||||
|
||||
public string GetRepo(string? registeredName)
|
||||
{
|
||||
if (string.IsNullOrEmpty(registeredName))
|
||||
{
|
||||
throw new Exception("Name cannot be null");
|
||||
}
|
||||
|
||||
if (_registrations.TryGetValue(registeredName, out var registration))
|
||||
{
|
||||
return registration.Location;
|
||||
}
|
||||
|
||||
throw new Exception($"No git repo has been registered with the name {registeredName}");
|
||||
}
|
||||
}
|
||||
|
||||
public class GitReg
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public required string Location { get; set; }
|
||||
public required string CurrentBranch { get; set; }
|
||||
}
|
||||
8
src/ModuleCore/Git/Models/GitRegistration.cs
Normal file
8
src/ModuleCore/Git/Models/GitRegistration.cs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
namespace ModuleCore.Git.Models;
|
||||
|
||||
public class GitRegistration
|
||||
{
|
||||
public required string Name { get; set; }
|
||||
public required string Location { get; set; }
|
||||
public required string CurrentBranch { get; set; }
|
||||
}
|
||||
Loading…
Reference in a new issue