From 91148d809bc5b8d4eb87f36a5c1bcf979fcd6595 Mon Sep 17 00:00:00 2001 From: Artur Date: Tue, 19 Jan 2021 08:30:29 +0100 Subject: [PATCH] added IRemoteCollection --- .../Git/GitRepository.cs | 9 +++---- .../Git/GitRepositoryInfo.cs | 2 +- .../Git/ReferenceCollection.cs | 5 ++-- .../Git/RemoteCollection.cs | 27 +++++++++++++++++++ src/GitVersionCore/Git/IGitRepository.cs | 2 +- src/GitVersionCore/Git/IRemoteCollection.cs | 9 +++++++ 6 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs create mode 100644 src/GitVersionCore/Git/IRemoteCollection.cs diff --git a/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs b/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs index 5519a5e736..59f6b2e18c 100644 --- a/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs +++ b/src/GitVersion.LibGit2Sharp/Git/GitRepository.cs @@ -69,10 +69,7 @@ public void Dispose() public string Path => repositoryInstance.Info.Path; public string WorkingDirectory => repositoryInstance.Info.WorkingDirectory; public bool IsHeadDetached => repositoryInstance.Info.IsHeadDetached; - public IGitRepository CreateNew(string gitRootPath) - { - return new GitRepository(new NullLog(), () => gitRootPath); - } + public IGitRepository CreateNew(string gitRootPath) => new GitRepository(new NullLog(), () => gitRootPath); public int GetNumberOfUncommittedChanges() { // check if we have a branch tip at all to behave properly with empty repos @@ -116,9 +113,9 @@ public string ShortenObjectId(ICommit commit) public ITagCollection Tags => new TagCollection(repositoryInstance.Tags); public IReferenceCollection Refs => new ReferenceCollection(repositoryInstance.Refs); - public IBranchCollection Branches => new BranchCollection(repositoryInstance.Branches); public ICommitCollection Commits => new CommitCollection(repositoryInstance.Commits); + public IRemoteCollection Remotes => new RemoteCollection(repositoryInstance.Network.Remotes); public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) { @@ -176,7 +173,7 @@ public void CreateBranchForPullRequestBranch(AuthenticationInfo auth) log.Info($"Checking local branch '{fakeBranchName}' out."); Checkout(fakeBranchName); } - public bool GitRepoHasMatchingRemote(string targetUrl) + public bool AnyMatchingRemote(string targetUrl) { return repositoryInstance.Network.Remotes.Any(r => r.Url == targetUrl); } diff --git a/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs b/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs index 819e8950b6..76c75bc465 100644 --- a/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs +++ b/src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs @@ -102,7 +102,7 @@ private static bool GitRepoHasMatchingRemote(string possiblePath, string targetU { try { - return new GitRepository(new NullLog(), possiblePath).GitRepoHasMatchingRemote(targetUrl); + return new GitRepository(new NullLog(), possiblePath).AnyMatchingRemote(targetUrl); } catch (Exception) { diff --git a/src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs b/src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs index 1d995fe5b2..5fede01e50 100644 --- a/src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs +++ b/src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs @@ -25,12 +25,13 @@ public void UpdateTarget(IReference directRef, IObjectId targetId) } IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public IReference? this[string name] { get { - var branch = innerCollection[name]; - return branch is null ? null : new Reference(branch); + var reference = innerCollection[name]; + return reference is null ? null : new Reference(reference); } } diff --git a/src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs b/src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs new file mode 100644 index 0000000000..5752801594 --- /dev/null +++ b/src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs @@ -0,0 +1,27 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; + +namespace GitVersion +{ + internal sealed class RemoteCollection : IRemoteCollection + { + private readonly LibGit2Sharp.RemoteCollection innerCollection; + internal RemoteCollection(LibGit2Sharp.RemoteCollection collection) => innerCollection = collection; + + public IEnumerator GetEnumerator() + { + return innerCollection.Select(reference => new Remote(reference)).GetEnumerator(); + } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + + public IRemote? this[string name] + { + get + { + var remote = innerCollection[name]; + return remote is null ? null : new Remote(remote); + } + } + } +} diff --git a/src/GitVersionCore/Git/IGitRepository.cs b/src/GitVersionCore/Git/IGitRepository.cs index e722435656..afe20c627c 100644 --- a/src/GitVersionCore/Git/IGitRepository.cs +++ b/src/GitVersionCore/Git/IGitRepository.cs @@ -13,13 +13,13 @@ public interface IGitRepository : IDisposable IReferenceCollection Refs { get; } IBranchCollection Branches { get; } ICommitCollection Commits { get; } + IRemoteCollection Remotes { get; } IGitRepository CreateNew(string gitRootPath); int GetNumberOfUncommittedChanges(); string ShortenObjectId(ICommit commit); - bool GitRepoHasMatchingRemote(string targetUrl); void CleanupDuplicateOrigin(string gitRootPath, string remoteName); bool GetMatchingCommitBranch(ICommit baseVersionSource, IBranch branch, ICommit firstMatchingCommit); IRemote EnsureOnlyOneRemoteIsDefined(); diff --git a/src/GitVersionCore/Git/IRemoteCollection.cs b/src/GitVersionCore/Git/IRemoteCollection.cs new file mode 100644 index 0000000000..05df11f5ad --- /dev/null +++ b/src/GitVersionCore/Git/IRemoteCollection.cs @@ -0,0 +1,9 @@ +using System.Collections.Generic; + +namespace GitVersion +{ + public interface IRemoteCollection : IEnumerable + { + IRemote this[string name] { get; } + } +}