Skip to content

Improve marshaling of git oid #682

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion LibGit2Sharp/Core/GitOid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ namespace LibGit2Sharp.Core
/// </summary>
internal struct GitOid
{
/// <summary>
/// Number of bytes in the Id.
/// </summary>
public const int Size = 20;

/// <summary>
/// The raw binary 20 byte Id.
/// </summary>
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = Size)]
public byte[] Id;

public static implicit operator ObjectId(GitOid oid)
Expand Down
9 changes: 8 additions & 1 deletion LibGit2Sharp/Core/Handles/OidSafeHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ internal class OidSafeHandle : NotOwnedSafeHandleBase
{
private GitOid? MarshalAsGitOid()
{
return IsInvalid ? null : (GitOid?)handle.MarshalAs<GitOid>();
return IsInvalid ? null : (GitOid?)MarshalAsGitOid(handle);
}

private static GitOid MarshalAsGitOid(System.IntPtr data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a bool throwWhenNull = true parameter (à la IntPtrExtensions)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nulltoken I don't see why that is needed, the code already returns null when the handle is invalid.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duh.... You're right! I overlooked that this was a private function (unlike MarshalAs<>()).

{
var gitOid = new GitOid { Id = new byte[GitOid.Size] };
Marshal.Copy(data, gitOid.Id, 0, GitOid.Size);
return gitOid;
}

public ObjectId MarshalAsObjectId()
Expand Down