diff --git a/src/ModuleCore/Git/GitManager.cs b/src/ModuleCore/Git/GitManager.cs index cfdf2d8..dfeb1f7 100644 --- a/src/ModuleCore/Git/GitManager.cs +++ b/src/ModuleCore/Git/GitManager.cs @@ -117,6 +117,54 @@ public class GitManager }); } + public void UnregisterRepo(string registrationName) + { + _db.InConnection(conn => + { + var existingRegistration = conn.Query( + $""" + SELECT {nameof(InternalGitRegistration.Id)} + ,{nameof(InternalGitRegistration.Name)} + ,{nameof(InternalGitRegistration.Location)} + FROM {InternalGitRegistration.TableName} + WHERE {nameof(InternalGitRegistration.Name)} = ? + """, + registrationName) + .FirstOrDefault(); + + if (existingRegistration is null) + { + throw new Exception($"No registration exists for '{registrationName}'.") + { + Source = "unregister-repository", + }; + } + + var deleted = conn.Delete(existingRegistration.Id); + + // If we somehow found a registration but delete returned nothing, just return and assume we've already + // removed it from registrations. + // Seems a bit risky when you read it logically, but by this point the registration shouldn't exist so it doesn't + // matter. + if (deleted == 0) + { + return; + } + + // Remove by the name we get from the database instead of what was passed in + if (_registrations.TryRemove(registrationName, out var removedItem)) + { + _debugWriterDelegate?.Invoke($"Removed {registrationName}."); + } + + // Weird error to throw, but by this stage we shouldn't have a git repo registered under this name + throw new Exception("Failed to remove registration - no registration exists.") + { + Source = "unregister-repository", + }; + }); + } + public List ListRepos() { return _registrations.Select(x => diff --git a/src/PowershellModule/Git/Commands/RemoveGitRepoCommand.cs b/src/PowershellModule/Git/Commands/RemoveGitRepoCommand.cs new file mode 100644 index 0000000..f7c73a3 --- /dev/null +++ b/src/PowershellModule/Git/Commands/RemoveGitRepoCommand.cs @@ -0,0 +1,36 @@ +using System; +using System.Management.Automation; +using ModuleCore.Git; + +namespace PowershellModule.Git.Commands; + +[Cmdlet(VerbsCommon.Remove, GitCommands.GitRepoNoun)] +public class RemoveGitRepoCommand : PSCmdlet +{ + [Parameter( + Position = 0, + ValueFromPipeline = true, + HelpMessage = "Reference name for the repo")] + public string? Name { get; set; } + + protected override void BeginProcessing() + { + try + { + GitManager.SetDebugWriter(WriteDebug); + + var repoFolder = GitManager.IsGitRepo(SessionState.Path.CurrentLocation.Path); + + // Removing a registration works similar to registering a new one - we either remove by exact name, or by + // the folder if no name is given (so a user can remove a registration from a git repo they're currently in) + GitManager.Instance.UnregisterRepo(Name ?? repoFolder.Folder); + GitManager.ClearDebugWriter(); + + base.BeginProcessing(); + } + catch (Exception ex) + { + WriteError(new ErrorRecord(ex, ex.Source, ErrorCategory.FromStdErr, null)); + } + } +} \ No newline at end of file