From 1e028f7718dc8f7a3db95aa8d95a3a4687b3dfbf Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Thu, 21 Nov 2024 23:58:49 -0500 Subject: [PATCH 1/3] Use SafeHandle as base class --- LibGit2Sharp/Core/Handles/Libgit2Object.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/LibGit2Sharp/Core/Handles/Libgit2Object.cs b/LibGit2Sharp/Core/Handles/Libgit2Object.cs index a96d99e10..29cebe14e 100644 --- a/LibGit2Sharp/Core/Handles/Libgit2Object.cs +++ b/LibGit2Sharp/Core/Handles/Libgit2Object.cs @@ -18,7 +18,7 @@ //#define LEAKS_TRACKING using System; -using Microsoft.Win32.SafeHandles; +using System.Runtime.InteropServices; #if LEAKS_IDENTIFYING namespace LibGit2Sharp.Core @@ -83,7 +83,7 @@ namespace LibGit2Sharp.Core.Handles using System.Globalization; #endif - internal unsafe abstract class Libgit2Object : SafeHandleZeroOrMinusOneIsInvalid + internal unsafe abstract class Libgit2Object : SafeHandle { #if LEAKS_TRACKING private readonly string trace; @@ -96,7 +96,7 @@ internal unsafe Libgit2Object(void* ptr, bool owned) } internal unsafe Libgit2Object(IntPtr ptr, bool owned) - : base(owned) + : base(IntPtr.Zero, owned) { SetHandle(ptr); @@ -108,12 +108,14 @@ internal unsafe Libgit2Object(IntPtr ptr, bool owned) #endif } + public override bool IsInvalid => handle == IntPtr.Zero; + internal IntPtr AsIntPtr() => DangerousGetHandle(); protected override void Dispose(bool disposing) { #if LEAKS_IDENTIFYING - bool leaked = !disposing && DangerousGetHandle() != IntPtr.Zero; + bool leaked = !disposing && !IsInvalid; if (leaked) { From d1c26a30b0370364ae5aed0e00632f7c5fae08cf Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Fri, 22 Nov 2024 00:00:00 -0500 Subject: [PATCH 2/3] Use handle directly in ReleaseHandle --- LibGit2Sharp/Core/Handles/Objects.cs | 50 ++++++++++++++-------------- LibGit2Sharp/Core/Handles/Objects.tt | 2 +- 2 files changed, 26 insertions(+), 26 deletions(-) diff --git a/LibGit2Sharp/Core/Handles/Objects.cs b/LibGit2Sharp/Core/Handles/Objects.cs index ddca49bee..18b79b616 100644 --- a/LibGit2Sharp/Core/Handles/Objects.cs +++ b/LibGit2Sharp/Core/Handles/Objects.cs @@ -18,7 +18,7 @@ internal TreeEntryHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_tree_entry_free((git_tree_entry*)AsIntPtr()); + NativeMethods.git_tree_entry_free((git_tree_entry*)handle); return true; } @@ -43,7 +43,7 @@ internal ReferenceHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_reference_free((git_reference*)AsIntPtr()); + NativeMethods.git_reference_free((git_reference*)handle); return true; } @@ -68,7 +68,7 @@ internal RepositoryHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_repository_free((git_repository*)AsIntPtr()); + NativeMethods.git_repository_free((git_repository*)handle); return true; } @@ -93,7 +93,7 @@ internal SignatureHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_signature_free((git_signature*)AsIntPtr()); + NativeMethods.git_signature_free((git_signature*)handle); return true; } @@ -118,7 +118,7 @@ internal StatusListHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_status_list_free((git_status_list*)AsIntPtr()); + NativeMethods.git_status_list_free((git_status_list*)handle); return true; } @@ -143,7 +143,7 @@ internal BlameHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_blame_free((git_blame*)AsIntPtr()); + NativeMethods.git_blame_free((git_blame*)handle); return true; } @@ -168,7 +168,7 @@ internal DiffHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_diff_free((git_diff*)AsIntPtr()); + NativeMethods.git_diff_free((git_diff*)handle); return true; } @@ -193,7 +193,7 @@ internal PatchHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_patch_free((git_patch*)AsIntPtr()); + NativeMethods.git_patch_free((git_patch*)handle); return true; } @@ -218,7 +218,7 @@ internal ConfigurationHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_config_free((git_config*)AsIntPtr()); + NativeMethods.git_config_free((git_config*)handle); return true; } @@ -243,7 +243,7 @@ internal ConflictIteratorHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_index_conflict_iterator_free((git_index_conflict_iterator*)AsIntPtr()); + NativeMethods.git_index_conflict_iterator_free((git_index_conflict_iterator*)handle); return true; } @@ -268,7 +268,7 @@ internal IndexHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_index_free((git_index*)AsIntPtr()); + NativeMethods.git_index_free((git_index*)handle); return true; } @@ -293,7 +293,7 @@ internal ReflogHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_reflog_free((git_reflog*)AsIntPtr()); + NativeMethods.git_reflog_free((git_reflog*)handle); return true; } @@ -318,7 +318,7 @@ internal TreeBuilderHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_treebuilder_free((git_treebuilder*)AsIntPtr()); + NativeMethods.git_treebuilder_free((git_treebuilder*)handle); return true; } @@ -343,7 +343,7 @@ internal PackBuilderHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_packbuilder_free((git_packbuilder*)AsIntPtr()); + NativeMethods.git_packbuilder_free((git_packbuilder*)handle); return true; } @@ -368,7 +368,7 @@ internal NoteHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_note_free((git_note*)AsIntPtr()); + NativeMethods.git_note_free((git_note*)handle); return true; } @@ -393,7 +393,7 @@ internal DescribeResultHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_describe_result_free((git_describe_result*)AsIntPtr()); + NativeMethods.git_describe_result_free((git_describe_result*)handle); return true; } @@ -418,7 +418,7 @@ internal SubmoduleHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_submodule_free((git_submodule*)AsIntPtr()); + NativeMethods.git_submodule_free((git_submodule*)handle); return true; } @@ -443,7 +443,7 @@ internal AnnotatedCommitHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_annotated_commit_free((git_annotated_commit*)AsIntPtr()); + NativeMethods.git_annotated_commit_free((git_annotated_commit*)handle); return true; } @@ -468,7 +468,7 @@ internal ObjectDatabaseHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_odb_free((git_odb*)AsIntPtr()); + NativeMethods.git_odb_free((git_odb*)handle); return true; } @@ -493,7 +493,7 @@ internal RevWalkerHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_revwalk_free((git_revwalk*)AsIntPtr()); + NativeMethods.git_revwalk_free((git_revwalk*)handle); return true; } @@ -518,7 +518,7 @@ internal RemoteHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_remote_free((git_remote*)AsIntPtr()); + NativeMethods.git_remote_free((git_remote*)handle); return true; } @@ -543,7 +543,7 @@ internal ObjectHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_object_free((git_object*)AsIntPtr()); + NativeMethods.git_object_free((git_object*)handle); return true; } @@ -568,7 +568,7 @@ internal RebaseHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_rebase_free((git_rebase*)AsIntPtr()); + NativeMethods.git_rebase_free((git_rebase*)handle); return true; } @@ -593,7 +593,7 @@ internal OdbStreamHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_odb_stream_free((git_odb_stream*)AsIntPtr()); + NativeMethods.git_odb_stream_free((git_odb_stream*)handle); return true; } @@ -618,7 +618,7 @@ internal WorktreeHandle(IntPtr ptr, bool owned) protected override bool ReleaseHandle() { - NativeMethods.git_worktree_free((git_worktree*)AsIntPtr()); + NativeMethods.git_worktree_free((git_worktree*)handle); return true; } diff --git a/LibGit2Sharp/Core/Handles/Objects.tt b/LibGit2Sharp/Core/Handles/Objects.tt index e522bd859..7202fa819 100644 --- a/LibGit2Sharp/Core/Handles/Objects.tt +++ b/LibGit2Sharp/Core/Handles/Objects.tt @@ -84,7 +84,7 @@ for (var i = 0; i < cNames.Length; i++) protected override bool ReleaseHandle() { - NativeMethods.<#= cNames[i] #>_free((<#= cNames[i] #>*)AsIntPtr()); + NativeMethods.<#= cNames[i] #>_free((<#= cNames[i] #>*)handle); return true; } From 123dc913db42d9f8dcd9d9a5929ed3596a34c2f8 Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Fri, 22 Nov 2024 00:32:26 -0500 Subject: [PATCH 3/3] Remove AsIntPtr --- LibGit2Sharp/Core/Handles/Libgit2Object.cs | 2 - LibGit2Sharp/Core/Handles/Objects.cs | 50 +++++++++++----------- LibGit2Sharp/Core/Handles/Objects.tt | 2 +- LibGit2Sharp/Core/NativeMethods.cs | 16 +++---- LibGit2Sharp/Core/Proxy.cs | 18 ++++---- LibGit2Sharp/Reference.cs | 6 --- 6 files changed, 43 insertions(+), 51 deletions(-) diff --git a/LibGit2Sharp/Core/Handles/Libgit2Object.cs b/LibGit2Sharp/Core/Handles/Libgit2Object.cs index 29cebe14e..e912d2137 100644 --- a/LibGit2Sharp/Core/Handles/Libgit2Object.cs +++ b/LibGit2Sharp/Core/Handles/Libgit2Object.cs @@ -110,8 +110,6 @@ internal unsafe Libgit2Object(IntPtr ptr, bool owned) public override bool IsInvalid => handle == IntPtr.Zero; - internal IntPtr AsIntPtr() => DangerousGetHandle(); - protected override void Dispose(bool disposing) { #if LEAKS_IDENTIFYING diff --git a/LibGit2Sharp/Core/Handles/Objects.cs b/LibGit2Sharp/Core/Handles/Objects.cs index 18b79b616..6ae208a7c 100644 --- a/LibGit2Sharp/Core/Handles/Objects.cs +++ b/LibGit2Sharp/Core/Handles/Objects.cs @@ -25,7 +25,7 @@ protected override bool ReleaseHandle() public static implicit operator git_tree_entry*(TreeEntryHandle handle) { - return (git_tree_entry*)handle.AsIntPtr(); + return (git_tree_entry*)handle.DangerousGetHandle(); } } @@ -50,7 +50,7 @@ protected override bool ReleaseHandle() public static implicit operator git_reference*(ReferenceHandle handle) { - return (git_reference*)handle.AsIntPtr(); + return (git_reference*)handle.DangerousGetHandle(); } } @@ -75,7 +75,7 @@ protected override bool ReleaseHandle() public static implicit operator git_repository*(RepositoryHandle handle) { - return (git_repository*)handle.AsIntPtr(); + return (git_repository*)handle.DangerousGetHandle(); } } @@ -100,7 +100,7 @@ protected override bool ReleaseHandle() public static implicit operator git_signature*(SignatureHandle handle) { - return (git_signature*)handle.AsIntPtr(); + return (git_signature*)handle.DangerousGetHandle(); } } @@ -125,7 +125,7 @@ protected override bool ReleaseHandle() public static implicit operator git_status_list*(StatusListHandle handle) { - return (git_status_list*)handle.AsIntPtr(); + return (git_status_list*)handle.DangerousGetHandle(); } } @@ -150,7 +150,7 @@ protected override bool ReleaseHandle() public static implicit operator git_blame*(BlameHandle handle) { - return (git_blame*)handle.AsIntPtr(); + return (git_blame*)handle.DangerousGetHandle(); } } @@ -175,7 +175,7 @@ protected override bool ReleaseHandle() public static implicit operator git_diff*(DiffHandle handle) { - return (git_diff*)handle.AsIntPtr(); + return (git_diff*)handle.DangerousGetHandle(); } } @@ -200,7 +200,7 @@ protected override bool ReleaseHandle() public static implicit operator git_patch*(PatchHandle handle) { - return (git_patch*)handle.AsIntPtr(); + return (git_patch*)handle.DangerousGetHandle(); } } @@ -225,7 +225,7 @@ protected override bool ReleaseHandle() public static implicit operator git_config*(ConfigurationHandle handle) { - return (git_config*)handle.AsIntPtr(); + return (git_config*)handle.DangerousGetHandle(); } } @@ -250,7 +250,7 @@ protected override bool ReleaseHandle() public static implicit operator git_index_conflict_iterator*(ConflictIteratorHandle handle) { - return (git_index_conflict_iterator*)handle.AsIntPtr(); + return (git_index_conflict_iterator*)handle.DangerousGetHandle(); } } @@ -275,7 +275,7 @@ protected override bool ReleaseHandle() public static implicit operator git_index*(IndexHandle handle) { - return (git_index*)handle.AsIntPtr(); + return (git_index*)handle.DangerousGetHandle(); } } @@ -300,7 +300,7 @@ protected override bool ReleaseHandle() public static implicit operator git_reflog*(ReflogHandle handle) { - return (git_reflog*)handle.AsIntPtr(); + return (git_reflog*)handle.DangerousGetHandle(); } } @@ -325,7 +325,7 @@ protected override bool ReleaseHandle() public static implicit operator git_treebuilder*(TreeBuilderHandle handle) { - return (git_treebuilder*)handle.AsIntPtr(); + return (git_treebuilder*)handle.DangerousGetHandle(); } } @@ -350,7 +350,7 @@ protected override bool ReleaseHandle() public static implicit operator git_packbuilder*(PackBuilderHandle handle) { - return (git_packbuilder*)handle.AsIntPtr(); + return (git_packbuilder*)handle.DangerousGetHandle(); } } @@ -375,7 +375,7 @@ protected override bool ReleaseHandle() public static implicit operator git_note*(NoteHandle handle) { - return (git_note*)handle.AsIntPtr(); + return (git_note*)handle.DangerousGetHandle(); } } @@ -400,7 +400,7 @@ protected override bool ReleaseHandle() public static implicit operator git_describe_result*(DescribeResultHandle handle) { - return (git_describe_result*)handle.AsIntPtr(); + return (git_describe_result*)handle.DangerousGetHandle(); } } @@ -425,7 +425,7 @@ protected override bool ReleaseHandle() public static implicit operator git_submodule*(SubmoduleHandle handle) { - return (git_submodule*)handle.AsIntPtr(); + return (git_submodule*)handle.DangerousGetHandle(); } } @@ -450,7 +450,7 @@ protected override bool ReleaseHandle() public static implicit operator git_annotated_commit*(AnnotatedCommitHandle handle) { - return (git_annotated_commit*)handle.AsIntPtr(); + return (git_annotated_commit*)handle.DangerousGetHandle(); } } @@ -475,7 +475,7 @@ protected override bool ReleaseHandle() public static implicit operator git_odb*(ObjectDatabaseHandle handle) { - return (git_odb*)handle.AsIntPtr(); + return (git_odb*)handle.DangerousGetHandle(); } } @@ -500,7 +500,7 @@ protected override bool ReleaseHandle() public static implicit operator git_revwalk*(RevWalkerHandle handle) { - return (git_revwalk*)handle.AsIntPtr(); + return (git_revwalk*)handle.DangerousGetHandle(); } } @@ -525,7 +525,7 @@ protected override bool ReleaseHandle() public static implicit operator git_remote*(RemoteHandle handle) { - return (git_remote*)handle.AsIntPtr(); + return (git_remote*)handle.DangerousGetHandle(); } } @@ -550,7 +550,7 @@ protected override bool ReleaseHandle() public static implicit operator git_object*(ObjectHandle handle) { - return (git_object*)handle.AsIntPtr(); + return (git_object*)handle.DangerousGetHandle(); } } @@ -575,7 +575,7 @@ protected override bool ReleaseHandle() public static implicit operator git_rebase*(RebaseHandle handle) { - return (git_rebase*)handle.AsIntPtr(); + return (git_rebase*)handle.DangerousGetHandle(); } } @@ -600,7 +600,7 @@ protected override bool ReleaseHandle() public static implicit operator git_odb_stream*(OdbStreamHandle handle) { - return (git_odb_stream*)handle.AsIntPtr(); + return (git_odb_stream*)handle.DangerousGetHandle(); } } @@ -625,7 +625,7 @@ protected override bool ReleaseHandle() public static implicit operator git_worktree*(WorktreeHandle handle) { - return (git_worktree*)handle.AsIntPtr(); + return (git_worktree*)handle.DangerousGetHandle(); } } diff --git a/LibGit2Sharp/Core/Handles/Objects.tt b/LibGit2Sharp/Core/Handles/Objects.tt index 7202fa819..2b7bfbab4 100644 --- a/LibGit2Sharp/Core/Handles/Objects.tt +++ b/LibGit2Sharp/Core/Handles/Objects.tt @@ -91,7 +91,7 @@ for (var i = 0; i < cNames.Length; i++) public static implicit operator <#= cNames[i] #>*(<#= csNames[i] #> handle) { - return (<#= cNames[i] #>*)handle.AsIntPtr(); + return (<#= cNames[i] #>*)handle.DangerousGetHandle(); } } diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index e8e59843e..7f2801021 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -254,7 +254,7 @@ internal static extern void git_branch_iterator_free( [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern int git_branch_iterator_new( out IntPtr iter_out, - IntPtr repo, + RepositoryHandle repo, GitBranchType branch_type); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] @@ -560,7 +560,7 @@ internal static extern unsafe int git_config_foreach( [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern int git_config_iterator_glob_new( out IntPtr iter, - IntPtr cfg, + ConfigurationHandle cfg, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string regexp); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] @@ -1236,7 +1236,7 @@ internal static extern unsafe int git_reference_lookup( [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxUtf8NoCleanupMarshaler))] - internal static extern unsafe string git_reference_name(git_reference* reference); + internal static extern unsafe string git_reference_name(ReferenceHandle reference); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_reference_remove( @@ -1273,7 +1273,7 @@ internal static extern unsafe int git_reference_symbolic_set_target( internal static extern unsafe string git_reference_symbolic_target(git_reference* reference); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern unsafe GitReferenceType git_reference_type(git_reference* reference); + internal static extern unsafe GitReferenceType git_reference_type(ReferenceHandle reference); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_reference_ensure_log( @@ -1533,10 +1533,10 @@ internal static extern unsafe int git_repository_fetchhead_foreach( internal static extern unsafe void git_repository_free(git_repository* repo); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern int git_repository_head_detached(IntPtr repo); + internal static extern int git_repository_head_detached(RepositoryHandle repo); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern int git_repository_head_unborn(IntPtr repo); + internal static extern int git_repository_head_unborn(RepositoryHandle repo); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_repository_ident( @@ -1554,10 +1554,10 @@ internal static extern unsafe int git_repository_init_ext( GitRepositoryInitOptions options); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern int git_repository_is_bare(IntPtr handle); + internal static extern int git_repository_is_bare(RepositoryHandle handle); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] - internal static extern int git_repository_is_shallow(IntPtr repo); + internal static extern int git_repository_is_shallow(RepositoryHandle repo); [DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)] internal static extern unsafe int git_repository_state_cleanup(git_repository* repo); diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index cf0a7e3d0..46ae253f8 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -131,7 +131,7 @@ public static unsafe void git_branch_delete(ReferenceHandle reference) public static IEnumerable git_branch_iterator(Repository repo, GitBranchType branchType) { IntPtr iter; - var res = NativeMethods.git_branch_iterator_new(out iter, repo.Handle.AsIntPtr(), branchType); + var res = NativeMethods.git_branch_iterator_new(out iter, repo.Handle, branchType); Ensure.ZeroResult(res); try @@ -345,7 +345,7 @@ public static unsafe string git_commit_create_buffer( try { handles = parents.Select(c => Proxy.git_object_lookup(c.repo.Handle, c.Id, GitObjectType.Commit)).ToArray(); - var ptrs = handles.Select(p => p.AsIntPtr()).ToArray(); + var ptrs = handles.Select(p => p.DangerousGetHandle()).ToArray(); int res; fixed (IntPtr* objs = ptrs) { @@ -624,7 +624,7 @@ public static IEnumerable> git_config_iterator_glob( string regexp) { IntPtr iter; - var res = NativeMethods.git_config_iterator_glob_new(out iter, config.AsIntPtr(), regexp); + var res = NativeMethods.git_config_iterator_glob_new(out iter, config, regexp); Ensure.ZeroResult(res); try { @@ -1302,7 +1302,7 @@ public static unsafe ObjectId git_annotated_commit_id(AnnotatedCommitHandle merg public static unsafe void git_merge(RepositoryHandle repo, AnnotatedCommitHandle[] heads, GitMergeOpts mergeOptions, GitCheckoutOpts checkoutOptions, out bool earlyStop) { - IntPtr[] their_heads = heads.Select(head => head.AsIntPtr()).ToArray(); + IntPtr[] their_heads = heads.Select(head => head.DangerousGetHandle()).ToArray(); int res = NativeMethods.git_merge(repo, their_heads, @@ -1327,7 +1327,7 @@ public static unsafe void git_merge_analysis( out GitMergeAnalysis analysis_out, out GitMergePreference preference_out) { - IntPtr[] their_heads = heads.Select(head => head.AsIntPtr()).ToArray(); + IntPtr[] their_heads = heads.Select(head => head.DangerousGetHandle()).ToArray(); int res = NativeMethods.git_merge_analysis(out analysis_out, out preference_out, @@ -1955,7 +1955,7 @@ public static unsafe ReferenceHandle git_reference_lookup(RepositoryHandle repo, return new ReferenceHandle(handle, true); } - public static unsafe string git_reference_name(git_reference* reference) + public static unsafe string git_reference_name(ReferenceHandle reference) { return NativeMethods.git_reference_name(reference); } @@ -2011,7 +2011,7 @@ public static unsafe string git_reference_symbolic_target(git_reference* referen return NativeMethods.git_reference_symbolic_target(reference); } - public static unsafe GitReferenceType git_reference_type(git_reference* reference) + public static unsafe GitReferenceType git_reference_type(ReferenceHandle reference) { return NativeMethods.git_reference_type(reference); } @@ -3779,9 +3779,9 @@ private static ICollection git_foreach( return result; } - private static unsafe bool RepositoryStateChecker(RepositoryHandle repo, Func checker) + private static unsafe bool RepositoryStateChecker(RepositoryHandle repo, Func checker) { - int res = checker(repo.AsIntPtr()); + int res = checker(repo); Ensure.BooleanResult(res); return (res == 1); diff --git a/LibGit2Sharp/Reference.cs b/LibGit2Sharp/Reference.cs index 9a86195d1..0f69a93ae 100644 --- a/LibGit2Sharp/Reference.cs +++ b/LibGit2Sharp/Reference.cs @@ -32,13 +32,7 @@ private protected Reference(IRepository repo, string canonicalName, string targe this.targetIdentifier = targetIdentifier; } - // This overload lets public-facing methods avoid having to use the pointers directly internal static unsafe T BuildFromPtr(ReferenceHandle handle, Repository repo) where T : Reference - { - return BuildFromPtr((git_reference*)handle.AsIntPtr(), repo); - } - - internal static unsafe T BuildFromPtr(git_reference* handle, Repository repo) where T : Reference { GitReferenceType type = Proxy.git_reference_type(handle); string name = Proxy.git_reference_name(handle);