From 4cf8731efb39ae5e4a36d8f45ad59f81bf171506 Mon Sep 17 00:00:00 2001 From: Robert Dailey Date: Sat, 18 Jun 2022 15:32:20 -0500 Subject: [PATCH] fix: Set repo URL from settings If a user changed the repo URL after a clone was performed, it would not get used. Fixes #90 --- CHANGELOG.md | 1 + src/TrashLib/Repo/RepoUpdater.cs | 2 ++ src/VersionControl.Tests/GitRepositoryFactoryTest.cs | 9 +++++++++ src/VersionControl/GitRepository.cs | 5 +++++ src/VersionControl/GitRepositoryFactory.cs | 4 +++- src/VersionControl/IGitRepository.cs | 1 + 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bff3bc3d..79d7dc8b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Sonarr: Invalid release profile JSON files no longer cause the program to exit. Instead, it just skips them and prints a warning to the user. (#87) - Radarr: Do not crash when `quality_profiles` is empty. (#89) +- Settings: Use repo URL after initial clone (#90) ## [2.2.0] - 2022-06-03 diff --git a/src/TrashLib/Repo/RepoUpdater.cs b/src/TrashLib/Repo/RepoUpdater.cs index ef56ce6b..7f8b5f7b 100644 --- a/src/TrashLib/Repo/RepoUpdater.cs +++ b/src/TrashLib/Repo/RepoUpdater.cs @@ -54,6 +54,8 @@ public class RepoUpdater : IRepoUpdater var cloneUrl = repoSettings.CloneUrl; const string branch = "master"; + _log.Debug("Using Branch & Clone URL: {Branch}, {Url}", branch, cloneUrl); + try { using var repo = _repositoryFactory.CreateAndCloneIfNeeded(cloneUrl, RepoPath, branch); diff --git a/src/VersionControl.Tests/GitRepositoryFactoryTest.cs b/src/VersionControl.Tests/GitRepositoryFactoryTest.cs index 1c1a427b..5791f894 100644 --- a/src/VersionControl.Tests/GitRepositoryFactoryTest.cs +++ b/src/VersionControl.Tests/GitRepositoryFactoryTest.cs @@ -47,4 +47,13 @@ public class GitRepositoryFactoryTest fileUtils.DidNotReceiveWithAnyArgs().DeleteReadOnlyDirectory(default!); wrapper.DidNotReceiveWithAnyArgs().Clone(default!, default!, default!); } + + [Test, AutoMockData] + public void Set_remote_when_creating_repository( + [Frozen] IGitRepository repo, + GitRepositoryFactory sut) + { + sut.CreateAndCloneIfNeeded("repo_url", "repo_path", "branch"); + repo.Received().SetRemote("origin", "repo_url"); + } } diff --git a/src/VersionControl/GitRepository.cs b/src/VersionControl/GitRepository.cs index e1994449..93a507a9 100644 --- a/src/VersionControl/GitRepository.cs +++ b/src/VersionControl/GitRepository.cs @@ -40,4 +40,9 @@ public sealed class GitRepository : IGitRepository var commit = _repo.Value.Branches[toBranch].Tip; _repo.Value.Reset(ResetMode.Hard, commit); } + + public void SetRemote(string name, string newUrl) + { + _repo.Value.Network.Remotes.Update(name, updater => updater.Url = newUrl); + } } diff --git a/src/VersionControl/GitRepositoryFactory.cs b/src/VersionControl/GitRepositoryFactory.cs index 81751d6c..b7464e48 100644 --- a/src/VersionControl/GitRepositoryFactory.cs +++ b/src/VersionControl/GitRepositoryFactory.cs @@ -27,7 +27,9 @@ public class GitRepositoryFactory : IGitRepositoryFactory DeleteAndCloneRepo(repoUrl, repoPath, branch); } - return _repoFactory(repoPath); + var repo = _repoFactory(repoPath); + repo.SetRemote("origin", repoUrl); + return repo; } private void DeleteAndCloneRepo(string repoUrl, string repoPath, string branch) diff --git a/src/VersionControl/IGitRepository.cs b/src/VersionControl/IGitRepository.cs index 32d03e43..2cf3e285 100644 --- a/src/VersionControl/IGitRepository.cs +++ b/src/VersionControl/IGitRepository.cs @@ -5,4 +5,5 @@ public interface IGitRepository : IDisposable void ForceCheckout(string branch); void Fetch(string remote = "origin"); void ResetHard(string toBranch); + void SetRemote(string name, string newUrl); }