From 54d5dd25b9c451510c0c77628a9af28d7d6f71ab Mon Sep 17 00:00:00 2001 From: Scott Date: Mon, 24 Aug 2026 10:17:08 +1000 Subject: [PATCH] refactor(git-provider): Rename GitReg to GitRegistration - rename private GitRegistration to InternalGitRegistration --- src/ModuleCore/Git/GitManager.cs | 121 +++++++++---------- src/ModuleCore/Git/Models/GitRegistration.cs | 8 ++ 2 files changed, 67 insertions(+), 62 deletions(-) create mode 100644 src/ModuleCore/Git/Models/GitRegistration.cs diff --git a/src/ModuleCore/Git/GitManager.cs b/src/ModuleCore/Git/GitManager.cs index 14b58db..264ff5a 100644 --- a/src/ModuleCore/Git/GitManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -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 /// internal static GitManager InternalFreshInstance => new(); - private class GitRegistration + private readonly ConcurrentDictionary _registrations; + + private GitManager() + { + Debug.WriteLine($"{nameof(GitManager)} init"); + + _registrations = new ConcurrentDictionary(); + } + + /// + /// Registers a git repository based on an absolute location. If is null or empty, + /// the registration will use the folder name for the git repo at the top level. + /// + /// + /// + /// The normalised string the repository was registered against + 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 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}"); + } + + /// + /// Used for internal git registration and handles getting the current branch + /// + 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 _registrations; - - private GitManager() - { - Debug.WriteLine($"{nameof(GitManager)} init"); - - _registrations = new ConcurrentDictionary(); - } - - /// - /// Registers a git repository based on an absolute location. If is null or empty, - /// the registration will use the folder name for the git repo at the top level. - /// - /// - /// - /// The normalised string the repository was registered against - 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 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; } } \ No newline at end of file diff --git a/src/ModuleCore/Git/Models/GitRegistration.cs b/src/ModuleCore/Git/Models/GitRegistration.cs new file mode 100644 index 0000000..11eea11 --- /dev/null +++ b/src/ModuleCore/Git/Models/GitRegistration.cs @@ -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; } +} \ No newline at end of file