8000 Add implicit cast from GitOid(?) to ObjectId · JanKrivanek/libgit2sharp@09beddc · GitHub
[go: up one dir, main page]

Skip to content

Commit 09beddc

Browse files
dahlbyknulltoken
authored andcommitted
Add implicit cast from GitOid(?) to ObjectId
Also, support marshaling NULL in OidSafeHandle
1 parent fd218c0 commit 09beddc

File tree

3 files changed

+14
-18
lines changed

3 files changed

+14
-18
lines changed

LibGit2Sharp/Core/GitOid.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,15 @@ internal struct GitOid
1212
/// </summary>
1313
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 20)]
1414
public byte[] Id;
15+
16+
public static implicit operator ObjectId(GitOid oid)
17+
{
18+
return new ObjectId(oid);
19+
}
20+
21+
public static implicit operator ObjectId(GitOid? oid)
22+
{
23+
return oid == null ? null : new ObjectId(oid.Value);
24+
}
1525
}
1626
}

LibGit2Sharp/Core/Handles/OidSafeHandle.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ namespace LibGit2Sharp.Core.Handles
44
{
55
internal class OidSafeHandle : NotOwnedSafeHandleBase
66
{
7-
private GitOid MarshalAsGitOid()
7+
private GitOid? MarshalAsGitOid()
88
{
9-
return (GitOid)Marshal.PtrToStructure(handle, typeof(GitOid));
9+
return (GitOid?)Marshal.PtrToStructure(handle, typeof(GitOid));
1010
}
1111

1212
public ObjectId MarshalAsObjectId()
1313
{
14-
return new ObjectId(MarshalAsGitOid());
14+
return MarshalAsGitOid();
1515
}
1616
}
1717
}

LibGit2Sharp/ObjectId.cs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public virtual string Sha
9898
/// <returns>true if the <paramref name = "sha" /> parameter was converted successfully; otherwise, false.</returns>
9999
public static bool TryParse(string sha, out ObjectId result)
100100
{
101-
result = BuildFrom(sha, false);
101+
result = BuildOidFrom(sha, false);
102102

103103
return result != null;
104104
}
@@ -113,20 +113,6 @@ public static bool TryParse(string sha, out ObjectId result)
113113
return ToOid(sha);
114114
}
115115

116-
private static ObjectId BuildFrom(string sha, bool shouldThrowIfInvalid)
117-
{
118-
GitOid? oid = BuildOidFrom(sha, shouldThrowIfInvalid);
119-
120-
if (!oid.HasValue)
121-
{
122-
return null;
123-
}
124-
125-
var objectId = new ObjectId(oid.Value);
126-
127-
return objectId;
128-
}
129-
130116
/// <summary>
131117
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "ObjectId" />.
132118
/// </summary>

0 commit comments

Comments
 (0)
0