Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed25e69

Browse files
committed
Use pointers for getting the list of advertised refs
1 parent 7445d4c commit ed25e69

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

LibGit2Sharp/Core/GitOid.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
namespace LibGit2Sharp.Core
44
{
5+
internal struct git_oid
6+
{
7+
public const int Size = 20;
8+
public unsafe fixed byte Id[20];
9+
}
10+
511
/// <summary>
612
/// Represents a unique id in git which is the sha1 hash of this id's content.
713
/// </summary>

LibGit2Sharp/Core/GitRemoteHead.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
namespace LibGit2Sharp.Core
55
{
66
[StructLayout(LayoutKind.Sequential)]
7-
internal struct GitRemoteHead
7+
internal unsafe struct git_remote_head
88
{
9-
public bool Local;
10-
public GitOid Oid;
11-
public GitOid Loid;
12-
public IntPtr NamePtr;
13-
public IntPtr SymRefTargetPtr;
9+
public int Local;
10+
public git_oid Oid;
11+
public git_oid Loid;
12+
public char* Name;
13+
public char* SymrefTarget;
1414
}
1515
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,10 +1244,8 @@ internal static extern int git_remote_lookup(
12441244
RepositorySafeHandle repo,
12451245
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name);
12461246

1247-
internal delegate int git_headlist_cb(ref GitRemoteHead remoteHeadPtr, IntPtr payload);
1248-
12491247
[DllImport(libgit2)]
1250-
internal static extern int git_remote_ls(out IntPtr heads, out UIntPtr size, RemoteSafeHandle remote);
1248+
internal static extern unsafe int git_remote_ls(out git_remote_head** heads, out UIntPtr size, RemoteSafeHandle remote);
12511249

12521250
[DllImport(libgit2)]
12531251
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]

LibGit2Sharp/Core/Proxy.cs

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,32 +2214,23 @@ public static IList<string> git_remote_list(RepositorySafeHandle repo)
22142214
}
22152215
}
22162216

2217-
public static IEnumerable<Reference> git_remote_ls(Repository repository, RemoteSafeHandle remote)
2217+
public static unsafe IEnumerable<Reference> git_remote_ls(Repository repository, RemoteSafeHandle remote)
22182218
{
2219-
IntPtr heads;
2219+
git_remote_head** heads;
22202220
UIntPtr count;
22212221

22222222
int res = NativeMethods.git_remote_ls(out heads, out count, remote);
22232223
Ensure.ZeroResult(res);
22242224

2225-
var intCount = (int)count.ToUInt32();
2226-
2227-
if (intCount < 0)
2228-
{
2229-
throw new OverflowException();
2230-
}
2231-
2225+
var intCount = checked(count.ToUInt32());
22322226
var directRefs = new Dictionary<string, Reference>();
22332227
var symRefs = new Dictionary<string, string>();
22342228

2235-
IntPtr currentHead = heads;
2236-
22372229
for (int i = 0; i < intCount; i++)
22382230
{
2239-
var remoteHead = Marshal.ReadIntPtr(currentHead).MarshalAs<GitRemoteHead>();
2240-
2241-
string name = LaxUtf8Marshaler.FromNative(remoteHead.NamePtr);
2242-
string symRefTarget = LaxUtf8Marshaler.FromNative(remoteHead.SymRefTargetPtr);
2231+
git_remote_head* currentHead = heads[i];
2232+
string name = LaxUtf8Marshaler.FromNative(currentHead->Name);
2233+
string symRefTarget = LaxUtf8Marshaler.FromNative(currentHead->SymrefTarget);
22432234

22442235
// The name pointer should never be null - if it is,
22452236
// this indicates a bug somewhere (libgit2, server, etc).
@@ -2254,10 +2245,8 @@ public static IEnumerable<Reference> git_remote_ls(Repository repository, Remote
22542245
}
22552246
else
22562247
{
2257-
directRefs.Add(name, new DirectReference(name, repository, remoteHead.Oid));
2248+
directRefs.Add(name, new DirectReference(name, repository, new ObjectId(currentHead->Oid.Id)));
22582249
}
2259-
2260-
currentHead = IntPtr.Add(currentHead, IntPtr.Size);
22612250
}
22622251

22632252
for (int i = 0; i < symRefs.Count; i++)

LibGit2Sharp/ObjectId.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ public ObjectId(byte[] rawId)
5757
Ensure.ArgumentConformsTo(rawId, b => b.Length == rawSize, "rawId");
5858
}
5959

60+
internal unsafe ObjectId(byte* rawId)
61+
{
62+
byte[] id = new byte[GitOid.Size];
63+
64+
fixed(byte* p = id)
65+
{
66+
for (int i = 0; i < rawSize; i++)
67+
{
68+
p[i] = rawId[i];
69+
}
70+
}
71+
72+
this.oid = new GitOid { Id = id };
73+
this.sha = ToString(oid.Id, oid.Id.Length * 2);
74+
}
75+
6076
/// <summary>
6177
/// Initializes a new instance of the <see cref="ObjectId"/> class.
6278
/// </summary>

0 commit comments

Comments
 (0)