Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions LibGit2Sharp/RefSpecCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,36 @@ namespace LibGit2Sharp
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RefSpecCollection : IEnumerable<RefSpec>
{
private readonly Lazy<IList<RefSpec>> refSpecsLazy;
readonly IList<RefSpec> refspecs;

/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected RefSpecCollection()
{ }

internal RefSpecCollection(Remote remote)
internal RefSpecCollection(RemoteSafeHandle handle)
{
Ensure.ArgumentNotNull(remote, "remote");
Ensure.ArgumentNotNull(handle, "handle");

refSpecsLazy = new Lazy<IList<RefSpec>>(() => RetrieveRefSpecs(remote));
refspecs = RetrieveRefSpecs(handle);
}

private static IList<RefSpec> RetrieveRefSpecs(Remote remote)
static IList<RefSpec> RetrieveRefSpecs(RemoteSafeHandle remoteHandle)
{
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_load(remote.repository.Handle, remote.Name, true))
{
int count = Proxy.git_remote_refspec_count(remoteHandle);
List<RefSpec> refSpecs = new List<RefSpec>();
int count = Proxy.git_remote_refspec_count(remoteHandle);
List<RefSpec> refSpecs = new List<RefSpec>();

for (int i = 0; i < count; i++)
for (int i = 0; i < count; i++)
{
using (GitRefSpecHandle handle = Proxy.git_remote_get_refspec(remoteHandle, i))
{
using (GitRefSpecHandle handle = Proxy.git_remote_get_refspec(remoteHandle, i))
{
refSpecs.Add(RefSpec.BuildFromPtr(handle));
}
refSpecs.Add(RefSpec.BuildFromPtr(handle));
}

return refSpecs;
}

return refSpecs;

}

/// <summary>
Expand All @@ -56,7 +54,7 @@ private static IList<RefSpec> RetrieveRefSpecs(Remote remote)
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
public virtual IEnumerator<RefSpec> GetEnumerator()
{
return refSpecsLazy.Value.GetEnumerator();
return refspecs.GetEnumerator();
}

/// <summary>
Expand Down
16 changes: 6 additions & 10 deletions LibGit2Sharp/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,18 @@ public class Remote : IEquatable<Remote>
protected Remote()
{ }

private Remote(Repository repository, string name, string url, TagFetchMode tagFetchMode)
private Remote(RemoteSafeHandle handle, Repository repository)
{
this.repository = repository;
Name = name;
Url = url;
TagFetchMode = tagFetchMode;
refSpecs = new RefSpecCollection(this);
Name = Proxy.git_remote_name(handle);
Url = Proxy.git_remote_url(handle);
TagFetchMode = Proxy.git_remote_autotag(handle);
refSpecs = new RefSpecCollection(handle);
}

internal static Remote BuildFromPtr(RemoteSafeHandle handle, Repository repo)
{
string name = Proxy.git_remote_name(handle);
string url = Proxy.git_remote_url(handle);
TagFetchMode tagFetchMode = Proxy.git_remote_autotag(handle);

var remote = new Remote(repo, name, url, tagFetchMode);
var remote = new Remote(handle, repo);

return remote;
}
Expand Down