Skip to content

Commit

Permalink
Refactor the building of a Remote
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed Feb 25, 2012
1 parent 318e7ca commit 922d33c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
25 changes: 22 additions & 3 deletions LibGit2Sharp/Remote.cs
Expand Up @@ -11,15 +11,34 @@ public class Remote : IEquatable<Remote>
private static readonly LambdaEqualityHelper<Remote> equalityHelper =
new LambdaEqualityHelper<Remote>(new Func<Remote, object>[] { x => x.Name, x => x.Url });

internal static Remote CreateFromPtr(RemoteSafeHandle handle)
{
if (handle == null)
{
return null;
}

IntPtr namePtr = NativeMethods.git_remote_name(handle);
IntPtr urlPtr = NativeMethods.git_remote_url(handle);

var remote = new Remote
{
Name = namePtr.MarshallAsString(),
Url = urlPtr.MarshallAsString(),
};

return remote;
}

/// <summary>
/// Gets the alias of this remote repository.
/// </summary>
public string Name { get; internal set; }
public string Name { get; private set; }

/// <summary>
/// Gets the urls to use to communicate with this remote repository.
/// Gets the url to use to communicate with this remote repository.
/// </summary>
public string Url { get; internal set; }
public string Url { get; private set; }

/// <summary>
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Remote" />.
Expand Down
21 changes: 3 additions & 18 deletions LibGit2Sharp/RemoteCollection.cs
@@ -1,5 +1,4 @@
using System;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core;

namespace LibGit2Sharp
{
Expand Down Expand Up @@ -35,24 +34,10 @@ internal RemoteSafeHandle LoadRemote(string name, bool throwsIfNotFound)

private Remote RemoteForName(string name)
{
RemoteSafeHandle handle = LoadRemote(name, false);

if (handle == null)
using (RemoteSafeHandle handle = LoadRemote(name, false))
{
return null;
return Remote.CreateFromPtr(handle);
}

var remote = new Remote();
using (handle)
{
IntPtr ptr = NativeMethods.git_remote_name(handle);
remote.Name = ptr.MarshallAsString();

ptr = NativeMethods.git_remote_url(handle);
remote.Url = ptr.MarshallAsString();
}

return remote;
}
}
}

0 comments on commit 922d33c

Please sign in to comment.