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

Skip to content

Commit 3bc68d5

Browse files
committed
Get rid of Reflog{,Entry}SafeHandle
1 parent ec88896 commit 3bc68d5

10 files changed

+96
-48
lines changed

LibGit2Sharp/Core/Handles/Objects.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,4 +763,73 @@ public void Dispose()
763763
}
764764
}
765765

766+
internal unsafe class ReflogHandle : IDisposable
767+
{
768+
git_reflog* ptr;
769+
internal git_reflog* Handle
770+
{
771+
get
772+
{
773+
return ptr;
774+
}
775+
}
776+
777+
bool owned;
778+
bool disposed;
779+
780+
public unsafe ReflogHandle(git_reflog* handle, bool owned)
781+
{
782+
this.ptr = handle;
783+
this.owned = owned;
784+
}
785+
786+
public unsafe ReflogHandle(IntPtr ptr, bool owned)
787+
{
788+
this.ptr = (git_reflog*) ptr.ToPointer();
789+
this.owned = owned;
790+
}
791+
792+
~ReflogHandle()
793+
{
794+
Dispose(false);
795+
}
796+
797+
internal bool IsNull
798+
{
799+
get
800+
{
801+
return ptr == null;
802+
}
803+
}
804+
805+
internal IntPtr AsIntPtr()
806+
{
807+
return new IntPtr(ptr);
808+
}
809+
810+
void Dispose(bool disposing)
811+
{
812+
if (!disposed)
813+
{
814+
if (owned)
815+
{
816+
NativeMethods.git_reflog_free(ptr);
817+
ptr = null;
818+
}
819+
}
820+
821+
disposed = true;
822+
}
823+
824+
public void Dispose()
825+
{
826+
Dispose(true);
827+
}
828+
829+
public static implicit operator git_reflog*(ReflogHandle handle)
830+
{
831+
return handle.Handle;
832+
}
833+
}
834+
766835
}

LibGit2Sharp/Core/Handles/Objects.tt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ var cNames = new[] {
2323
"git_config",
2424
"git_index_conflict_iterator",
2525
"git_index",
26+
"git_reflog",
2627
};
2728

2829
var csNames = new[] {
@@ -37,6 +38,7 @@ var csNames = new[] {
3738
"ConfigurationHandle",
3839
"ConflictIteratorHandle",
3940
"IndexHandle",
41+
"ReflogHandle",
4042
};
4143

4244
for (var i = 0; i < cNames.Length; i++)

LibGit2Sharp/Core/Handles/ReflogEntrySafeHandle.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.

LibGit2Sharp/Core/Handles/ReflogSafeHandle.cs

Lines changed: 0 additions & 11 deletions
This file was deleted.

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,39 +1075,38 @@ internal static extern unsafe int git_reference_ensure_log(
10751075
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refname);
10761076

10771077
[DllImport(libgit2)]
1078-
internal static extern void git_reflog_free(
1079-
IntPtr reflog);
1078+
internal static extern unsafe void git_reflog_free(git_reflog* reflog);
10801079

10811080
[DllImport(libgit2)]
10821081
internal static extern unsafe int git_reflog_read(
1083-
out ReflogSafeHandle ref_out,
1082+
out git_reflog* ref_out,
10841083
git_repository* repo,
10851084
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string name);
10861085

10871086
[DllImport(libgit2)]
1088-
internal static extern UIntPtr git_reflog_entrycount
1089-
(ReflogSafeHandle reflog);
1087+
internal static extern unsafe UIntPtr git_reflog_entrycount
1088+
(git_reflog* reflog);
10901089

10911090
[DllImport(libgit2)]
1092-
internal static extern ReflogEntrySafeHandle git_reflog_entry_byindex(
1093-
ReflogSafeHandle reflog,
1091+
internal static extern unsafe git_reflog_entry* git_reflog_entry_byindex(
1092+
git_reflog* reflog,
10941093
UIntPtr idx);
10951094

10961095
[DllImport(libgit2)]
10971096
internal static extern unsafe git_oid* git_reflog_entry_id_old(
1098-
SafeHandle entry);
1097+
git_reflog_entry* entry);
10991098

11001099
[DllImport(libgit2)]
11011100
internal static extern unsafe git_oid* git_reflog_entry_id_new(
1102-
SafeHandle entry);
1101+
git_reflog_entry* entry);
11031102

11041103
[DllImport(libgit2)]
11051104
internal static extern unsafe git_signature* git_reflog_entry_committer(
1106-
SafeHandle entry);
1105+
git_reflog_entry* entry);
11071106

11081107
[DllImport(libgit2)]
11091108
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))]
1110-
internal static extern string git_reflog_entry_message(SafeHandle entry);
1109+
internal static extern unsafe string git_reflog_entry_message(git_reflog_entry* entry);
11111110

11121111
[DllImport(libgit2)]
11131112
internal static extern unsafe int git_refspec_rtransform(

LibGit2Sharp/Core/Opaques.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ internal struct git_patch {}
1313
internal struct git_config {}
1414
internal struct git_index_conflict_iterator {}
1515
internal struct git_index {}
16+
internal struct git_reflog {}
17+
internal struct git_reflog_entry {}
1618
}
1719

LibGit2Sharp/Core/Proxy.cs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,47 +1906,42 @@ public static unsafe void git_reference_ensure_log(RepositoryHandle repo, string
19061906

19071907
#region git_reflog_
19081908

1909-
public static void git_reflog_free(IntPtr reflog)
1909+
public static unsafe ReflogHandle git_reflog_read(RepositoryHandle repo, string canonicalName)
19101910
{
1911-
NativeMethods.git_reflog_free(reflog);
1912-
}
1913-
1914-
public static unsafe ReflogSafeHandle git_reflog_read(RepositoryHandle repo, string canonicalName)
1915-
{
1916-
ReflogSafeHandle reflog_out;
1911+
git_reflog* reflog_out;
19171912

19181913
int res = NativeMethods.git_reflog_read(out reflog_out, repo, canonicalName);
19191914
Ensure.ZeroResult(res);
19201915

1921-
return reflog_out;
1916+
return new ReflogHandle(reflog_out, true);
19221917
}
19231918

1924-
public static int git_reflog_entrycount(ReflogSafeHandle reflog)
1919+
public static unsafe int git_reflog_entrycount(ReflogHandle reflog)
19251920
{
19261921
return (int)NativeMethods.git_reflog_entrycount(reflog);
19271922
}
19281923

1929-
public static ReflogEntrySafeHandle git_reflog_entry_byindex(ReflogSafeHandle reflog, int idx)
1924+
public static unsafe git_reflog_entry* git_reflog_entry_byindex(ReflogHandle reflog, int idx)
19301925
{
19311926
return NativeMethods.git_reflog_entry_byindex(reflog, (UIntPtr)idx);
19321927
}
19331928

1934-
public static unsafe ObjectId git_reflog_entry_id_old(SafeHandle entry)
1929+
public static unsafe ObjectId git_reflog_entry_id_old(git_reflog_entry* entry)
19351930
{
19361931
return ObjectId.BuildFromPtr(NativeMethods.git_reflog_entry_id_old(entry));
19371932
}
19381933

1939-
public static unsafe ObjectId git_reflog_entry_id_new(SafeHandle entry)
1934+
public static unsafe ObjectId git_reflog_entry_id_new(git_reflog_entry* entry)
19401935
{
19411936
return ObjectId.BuildFromPtr(NativeMethods.git_reflog_entry_id_new(entry));
19421937
}
19431938

1944-
public static unsafe Signature git_reflog_entry_committer(SafeHandle entry)
1939+
public static unsafe Signature git_reflog_entry_committer(git_reflog_entry* entry)
19451940
{
19461941
return new Signature(NativeMethods.git_reflog_entry_committer(entry));
19471942
}
19481943

1949-
public static string git_reflog_entry_message(SafeHandle entry)
1944+
public static unsafe string git_reflog_entry_message(git_reflog_entry* entry)
19501945
{
19511946
return NativeMethods.git_reflog_entry_message(entry);
19521947
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,6 @@
192192
<Compile Include="StashModifiers.cs" />
193193
<Compile Include="TreeEntryTargetType.cs" />
194194
<Compile Include="UnmatchedPathException.cs" />
195-
<Compile Include="Core\Handles\ReflogEntrySafeHandle.cs" />
196-
<Compile Include="Core\Handles\ReflogSafeHandle.cs" />
197195
<Compile Include="ReflogCollection.cs" />
198196
<Compile Include="Core\PathCase.cs" />
199197
<Compile Include="MatchedPathsAggregator.cs" />

LibGit2Sharp/ReflogCollection.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,17 @@ internal ReflogCollection(Repository repo, string canonicalName)
5353
/// </para>
5454
/// </summary>
5555
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
56-
public virtual IEnumerator<ReflogEntry> GetEnumerator()
56+
public virtual unsafe IEnumerator<ReflogEntry> GetEnumerator()
5757
{
5858
var entries = new List<ReflogEntry>();
5959

60-
using (ReflogSafeHandle reflog = Proxy.git_reflog_read(repo.Handle, canonicalName))
60+
using (ReflogHandle reflog = Proxy.git_reflog_read(repo.Handle, canonicalName))
6161
{
6262
var entriesCount = Proxy.git_reflog_entrycount(reflog);
6363

6464
for (int i = 0; i < entriesCount; i++)
6565
{
66-
ReflogEntrySafeHandle handle = Proxy.git_reflog_entry_byindex(reflog, i);
66+
git_reflog_entry* handle = Proxy.git_reflog_entry_byindex(reflog, i);
6767
entries.Add(new ReflogEntry(handle));
6868
}
6969
}

LibGit2Sharp/ReflogEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected ReflogEntry()
2525
/// Initializes a new instance of the <see cref="ReflogEntry"/> class.
2626
/// </summary>
2727
/// <param name="entryHandle">a <see cref="SafeHandle"/> to the reflog entry</param>
28-
public ReflogEntry(SafeHandle entryHandle)
28+
internal unsafe ReflogEntry(git_reflog_entry* entryHandle)
2929
{
3030
_from = Proxy.git_reflog_entry_id_old(entryHandle);
3131
_to = Proxy.git_reflog_entry_id_new(entryHandle);

0 commit comments

Comments
 (0)