Skip to content

Commit 92fc8f5

Browse files
committed
Use pointers to read the index entries
1 parent ed25e69 commit 92fc8f5

File tree

5 files changed

+59
-44
lines changed

5 files changed

+59
-44
lines changed

LibGit2Sharp/Core/GitIndexEntry.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,30 @@
33

44
namespace LibGit2Sharp.Core
55
{
6+
[StructLayout(LayoutKind.Sequential)]
7+
internal unsafe struct git_index_mtime
8+
{
9+
public int seconds;
10+
public uint nanoseconds;
11+
}
12+
13+
[StructLayout(LayoutKind.Sequential)]
14+
internal unsafe struct git_index_entry
15+
{
16+
public git_index_mtime ctime;
17+
public git_index_mtime mtime;
18+
public uint dev;
19+
public uint ino;
20+
public uint mode;
21+
public uint uid;
22+
public uint gid;
23+
public uint file_size;
24+
public git_oid id;
25+
public ushort flags;
26+
public ushort extended_flags;
27+
public char* path;
28+
}
29+
630
[StructLayout(LayoutKind.Sequential)]
731
internal class GitIndexEntry
832
{

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,10 @@ internal static extern int git_index_add(
634634
GitIndexEntry entry);
635635

636636
[DllImport(libgit2)]
637-
internal static extern int git_index_conflict_get(
638-
out IndexEntrySafeHandle ancestor,
639-
out IndexEntrySafeHandle ours,
640-
out IndexEntrySafeHandle theirs,
637+
internal static extern unsafe int git_index_conflict_get(
638+
out git_index_entry* ancestor,
639+
out git_index_entry* ours,
640+
out git_index_entry* theirs,
641641
IndexSafeHandle index,
642642
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path);
643643

@@ -647,10 +647,10 @@ internal static extern int git_index_conflict_iterator_new(
647647
IndexSafeHandle index);
648648

649649
[DllImport(libgit2)]
650-
internal static extern int git_index_conflict_next(
651-
out IndexEntrySafeHandle ancestor,
652-
out IndexEntrySafeHandle ours,
653-
out IndexEntrySafeHandle theirs,
650+
internal static extern unsafe int git_index_conflict_next(
651+
out git_index_entry* ancestor,
652+
out git_index_entry* ours,
653+
out git_index_entry* theirs,
654654
ConflictIteratorSafeHandle iterator);
655655

656656
[DllImport(libgit2)]
@@ -661,16 +661,16 @@ internal static extern void git_index_conflict_iterator_free(
661661
internal static extern UIntPtr git_index_entrycount(IndexSafeHandle index);
662662

663663
[DllImport(libgit2)]
664-
internal static extern int git_index_entry_stage(IndexEntrySafeHandle indexentry);
664+
internal static extern unsafe int git_index_entry_stage(git_index_entry* indexentry);
665665

666666
[DllImport(libgit2)]
667667
internal static extern void git_index_free(IntPtr index);
668668

669669
[DllImport(libgit2)]
670-
internal static extern IndexEntrySafeHandle git_index_get_byindex(IndexSafeHandle index, UIntPtr n);
670+
internal static extern unsafe git_index_entry* git_index_get_byindex(IndexSafeHandle index, UIntPtr n);
671671

672672
[DllImport(libgit2)]
673-
internal static extern IndexEntrySafeHandle git_index_get_bypath(
673+
internal static extern unsafe git_index_entry* git_index_get_bypath(
674674
IndexSafeHandle index,
675675
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictFilePathMarshaler))] FilePath path,
676676
int stage);

LibGit2Sharp/Core/Proxy.cs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -905,11 +905,11 @@ public static void git_index_add_bypath(IndexSafeHandle index, FilePath path)
905905
Ensure.ZeroResult(res);
906906
}
907907

908-
public static Conflict git_index_conflict_get(
908+
public static unsafe Conflict git_index_conflict_get(
909909
IndexSafeHandle index,
910910
FilePath path)
911911
{
912-
IndexEntrySafeHandle ancestor, ours, theirs;
912+
git_index_entry* ancestor, ours, theirs;
913913

914914
int res = NativeMethods.git_index_conflict_get(out ancestor,
915915
out ours,
@@ -938,9 +938,9 @@ public static ConflictIteratorSafeHandle git_index_conflict_iterator_new(IndexSa
938938
return iter;
939939
}
940940

941-
public static Conflict git_index_conflict_next(ConflictIteratorSafeHandle iterator)
941+
public static unsafe Conflict git_index_conflict_next(ConflictIteratorSafeHandle iterator)
942942
{
943-
IndexEntrySafeHandle ancestor, ours, theirs;
943+
git_index_entry* ancestor, ours, theirs;
944944

945945
int res = NativeMethods.git_index_conflict_next(out ancestor, out ours, out theirs, iterator);
946946

@@ -951,14 +951,9 @@ public static Conflict git_index_conflict_next(ConflictIteratorSafeHandle iterat
951951

952952
Ensure.ZeroResult(res);
953953

954-
using (ancestor)
955-
using (ours)
956-
using (theirs)
957-
{
958-
return new Conflict(IndexEntry.BuildFromPtr(ancestor),
959-
IndexEntry.BuildFromPtr(ours),
960-
IndexEntry.BuildFromPtr(theirs));
961-
}
954+
return new Conflict(IndexEntry.BuildFromPtr(ancestor),
955+
IndexEntry.BuildFromPtr(ours),
956+
IndexEntry.BuildFromPtr(theirs));
962957
}
963958

964959
public static void git_index_conflict_iterator_free(IntPtr iterator)
@@ -972,26 +967,24 @@ public static int git_index_entrycount(IndexSafeHandle index)
972967
.ConvertToInt();
973968
}
974969

975-
public static StageLevel git_index_entry_stage(IndexEntrySafeHandle index)
970+
public static unsafe StageLevel git_index_entry_stage(git_index_entry* entry)
976971
{
977-
return (StageLevel)NativeMethods.git_index_entry_stage(index);
972+
return (StageLevel)NativeMethods.git_index_entry_stage(entry);
978973
}
979974

980975
public static void git_index_free(IntPtr index)
981976
{
982977
NativeMethods.git_index_free(index);
983978
}
984979

985-
public static IndexEntrySafeHandle git_index_get_byindex(IndexSafeHandle index, UIntPtr n)
980+
public static unsafe git_index_entry* git_index_get_byindex(IndexSafeHandle index, UIntPtr n)
986981
{
987982
return NativeMethods.git_index_get_byindex(index, n);
988983
}
989984

990-
public static IndexEntrySafeHandle git_index_get_bypath(IndexSafeHandle index, FilePath path, int stage)
985+
public static unsafe git_index_entry* git_index_get_bypath(IndexSafeHandle index, FilePath path, int stage)
991986
{
992-
IndexEntrySafeHandle handle = NativeMethods.git_index_get_bypath(index, path, stage);
993-
994-
return handle.IsZero ? null : handle;
987+
return NativeMethods.git_index_get_bypath(index, path, stage);
995988
}
996989

997990
public static bool git_index_has_conflicts(IndexSafeHandle index)

LibGit2Sharp/Index.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,22 @@ public virtual bool IsFullyMerged
7070
/// <summary>
7171
/// Gets the <see cref="IndexEntry"/> with the specified relative path.
7272
/// </summary>
73-
public virtual IndexEntry this[string path]
73+
public virtual unsafe IndexEntry this[string path]
7474
{
7575
get
7676
{
7777
Ensure.ArgumentNotNullOrEmptyString(path, "path");
7878

79-
IndexEntrySafeHandle entryHandle = Proxy.git_index_get_bypath(handle, path, 0);
80-
return IndexEntry.BuildFromPtr(entryHandle);
79+
git_index_entry* entry = Proxy.git_index_get_bypath(handle, path, 0);
80+
return IndexEntry.BuildFromPtr(entry);
8181
}
8282
}
8383

84-
private IndexEntry this[int index]
84+
private unsafe IndexEntry this[int index]
8585
{
8686
get
8787
{
88-
IndexEntrySafeHandle entryHandle = Proxy.git_index_get_byindex(handle, (UIntPtr)index);
88+
git_index_entry* entryHandle = Proxy.git_index_get_byindex(handle, (UIntPtr)index);
8989
return IndexEntry.BuildFromPtr(entryHandle);
9090
}
9191
}

LibGit2Sharp/IndexEntry.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,22 @@ public class IndexEntry : IEquatable<IndexEntry>
4040
/// </summary>
4141
public virtual ObjectId Id { get; private set; }
4242

43-
internal static IndexEntry BuildFromPtr(IndexEntrySafeHandle handle)
43+
internal static unsafe IndexEntry BuildFromPtr(git_index_entry* entry)
4444
{
45-
if (handle == null || handle.IsZero)
45+
if (entry == null)
4646
{
4747
return null;
4848
}
4949

50-
GitIndexEntry entry = handle.MarshalAsGitIndexEntry();
51-
52-
FilePath path = LaxFilePathMarshaler.FromNative(entry.Path);
50+
FilePath path = LaxFilePathMarshaler.FromNative(entry->path);
5351

5452
return new IndexEntry
5553
{
5654
Path = path.Native,
57-
Id = entry.Id,
58-
StageLevel = Proxy.git_index_entry_stage(handle),
59-
Mode = (Mode)entry.Mode,
60-
AssumeUnchanged = (GitIndexEntry.GIT_IDXENTRY_VALID & entry.Flags) == GitIndexEntry.GIT_IDXENTRY_VALID
55+
Id = new ObjectId(entry->id.Id),
56+
StageLevel = Proxy.git_index_entry_stage(entry),
57+
Mode = (Mode)entry->mode,
58+
AssumeUnchanged = (GitIndexEntry.GIT_IDXENTRY_VALID & entry->flags) == GitIndexEntry.GIT_IDXENTRY_VALID
6159
};
6260
}
6361

0 commit comments

Comments
 (0)