Skip to content

Commit

Permalink
Remove AsIntPtr
Browse files Browse the repository at this point in the history
  • Loading branch information
bording committed Nov 23, 2024
1 parent d1c26a3 commit 123dc91
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 51 deletions.
2 changes: 0 additions & 2 deletions LibGit2Sharp/Core/Handles/Libgit2Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 25 additions & 25 deletions LibGit2Sharp/Core/Handles/Objects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand All @@ -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();
}
}

Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/Handles/Objects.tt
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down
16 changes: 8 additions & 8 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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);
Expand Down
18 changes: 9 additions & 9 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public static unsafe void git_branch_delete(ReferenceHandle reference)
public static IEnumerable<Branch> 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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -624,7 +624,7 @@ public static IEnumerable<ConfigurationEntry<string>> 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
{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -3779,9 +3779,9 @@ private static ICollection<TResult> git_foreach<T1, T2, T3, T4, TResult>(
return result;
}

private static unsafe bool RepositoryStateChecker(RepositoryHandle repo, Func<IntPtr, int> checker)
private static unsafe bool RepositoryStateChecker(RepositoryHandle repo, Func<RepositoryHandle, int> checker)
{
int res = checker(repo.AsIntPtr());
int res = checker(repo);
Ensure.BooleanResult(res);

return (res == 1);
Expand Down
6 changes: 0 additions & 6 deletions LibGit2Sharp/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(ReferenceHandle handle, Repository repo) where T : Reference
{
return BuildFromPtr<T>((git_reference*)handle.AsIntPtr(), repo);
}

internal static unsafe T BuildFromPtr<T>(git_reference* handle, Repository repo) where T : Reference
{
GitReferenceType type = Proxy.git_reference_type(handle);
string name = Proxy.git_reference_name(handle);
Expand Down

0 comments on commit 123dc91

Please sign in to comment.