Skip to content

Commit

Permalink
added IRemoteCollection
Browse files Browse the repository at this point in the history
  • Loading branch information
arturcic committed Jan 19, 2021
1 parent 001deaf commit 91148d8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 10 deletions.
9 changes: 3 additions & 6 deletions src/GitVersion.LibGit2Sharp/Git/GitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/GitVersion.LibGit2Sharp/Git/GitRepositoryInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions src/GitVersion.LibGit2Sharp/Git/ReferenceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/GitVersion.LibGit2Sharp/Git/RemoteCollection.cs
Original file line number Diff line number Diff line change
@@ -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<IRemote> 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);
}
}
}
}
2 changes: 1 addition & 1 deletion src/GitVersionCore/Git/IGitRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
9 changes: 9 additions & 0 deletions src/GitVersionCore/Git/IRemoteCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace GitVersion
{
public interface IRemoteCollection : IEnumerable<IRemote>
{
IRemote this[string name] { get; }
}
}

0 comments on commit 91148d8

Please sign in to comment.