Skip to content

Commit 922d33c

Browse files
committed
Refactor the building of a Remote
1 parent 318e7ca commit 922d33c

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

LibGit2Sharp/Remote.cs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,34 @@ public class Remote : IEquatable<Remote>
1111
private static readonly LambdaEqualityHelper<Remote> equalityHelper =
1212
new LambdaEqualityHelper<Remote>(new Func<Remote, object>[] { x => x.Name, x => x.Url });
1313

14+
internal static Remote CreateFromPtr(RemoteSafeHandle handle)
15+
{
16+
if (handle == null)
17+
{
18+
return null;
19+
}
20+
21+
IntPtr namePtr = NativeMethods.git_remote_name(handle);
22+
IntPtr urlPtr = NativeMethods.git_remote_url(handle);
23+
24+
var remote = new Remote
25+
{
26+
Name = namePtr.MarshallAsString(),
27+
Url = urlPtr.MarshallAsString(),
28+
};
29+
30+
return remote;
31+
}
32+
1433
/// <summary>
1534
/// Gets the alias of this remote repository.
1635
/// </summary>
17-
public string Name { get; internal set; }
36+
public string Name { get; private set; }
1837

1938
/// <summary>
20-
/// Gets the urls to use to communicate with this remote repository.
39+
/// Gets the url to use to communicate with this remote repository.
2140
/// </summary>
22-
public string Url { get; internal set; }
41+
public string Url { get; private set; }
2342

2443
/// <summary>
2544
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "Remote" />.

LibGit2Sharp/RemoteCollection.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using LibGit2Sharp.Core;
1+
using LibGit2Sharp.Core;
32

43
namespace LibGit2Sharp
54
{
@@ -35,24 +34,10 @@ internal RemoteSafeHandle LoadRemote(string name, bool throwsIfNotFound)
3534

3635
private Remote RemoteForName(string name)
3736
{
38-
RemoteSafeHandle handle = LoadRemote(name, false);
39-
40-
if (handle == null)
37+
using (RemoteSafeHandle handle = LoadRemote(name, false))
4138
{
42-
return null;
39+
return Remote.CreateFromPtr(handle);
4340
}
44-
45-
var remote = new Remote();
46-
using (handle)
47-
{
48-
IntPtr ptr = NativeMethods.git_remote_name(handle);
49-
remote.Name = ptr.MarshallAsString();
50-
51-
ptr = NativeMethods.git_remote_url(handle);
52-
remote.Url = ptr.MarshallAsString();
53-
}
54-
55-
return remote;
5641
}
5742
}
5843
}

0 commit comments

Comments
 (0)