From 3649da699db290e98b4e842496e3612da4a335fa Mon Sep 17 00:00:00 2001 From: Brandon Ording Date: Fri, 22 Nov 2024 00:32:26 -0500 Subject: [PATCH] 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);