Browse files

Simplify handling of native signature handle

  • Loading branch information...
1 parent 839c659 commit 9196a4823cb75dea011a97b4f1b5a12b9d3c041b @nulltoken committed Feb 1, 2012
View
1 BACKLOG.md
@@ -9,7 +9,6 @@
- Launch Code Analysis (Issues related to interop and marshaling will be worked on once we're able to succesffully exchange non ascii encoded data with libgit2)
- https://bugzilla.novell.com/show_bug.cgi?id=566247 prevents MonoDevelop users from benefiting from optional parameters while still target at 3.5
- https://bugzilla.novell.com/show_bug.cgi?id=324680 generates false-positive warnings regarding xml documentation when LibGit2Sharp is built with xbuild
- - The freeing of a newly created signature pointer doesn't "feel" to be done at the right place.
- Favor overloads over optional parameters (http://msdn.microsoft.com/en-us/library/ms182135.aspx)
- Ensure that types that are not supposed to be built by the Consumer do not expose a constructor.
- Escape as early as possible from a method. Fight against the arrowhead effect (cf. http://elegantcode.com/2009/08/14/observations-on-the-if-statement/)
View
6 LibGit2Sharp/CommitCollection.cs
@@ -147,10 +147,12 @@ public Commit Create(string message, Signature author, Signature committer)
GitOid commitOid;
using (var treePtr = new ObjectSafeWrapper(new ObjectId(treeOid), repo))
using (ObjectSafeWrapper headPtr = RetrieveHeadCommitPtr(head))
+ using (SignatureSafeHandle authorHandle = author.BuildHandle())
+ using (SignatureSafeHandle committerHandle = committer.BuildHandle())
{
IntPtr[] parentPtrs = BuildArrayFrom(headPtr);
- res = NativeMethods.git_commit_create(out commitOid, repo.Handle, head.CanonicalName, author.Handle,
- committer.Handle, encoding, message, treePtr.ObjectPtr, parentPtrs.Count(), parentPtrs);
+ res = NativeMethods.git_commit_create(out commitOid, repo.Handle, head.CanonicalName, authorHandle,
+ committerHandle, encoding, message, treePtr.ObjectPtr, parentPtrs.Count(), parentPtrs);
}
Ensure.Success(res);
View
8 LibGit2Sharp/Core/NativeMethods.cs
@@ -59,8 +59,8 @@ private static bool IsRunningOnLinux()
out GitOid oid,
RepositorySafeHandle repo,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string updateRef,
- GitSignature author,
- GitSignature committer,
+ SignatureSafeHandle author,
+ SignatureSafeHandle committer,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string encoding,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message,
IntPtr tree,
@@ -347,7 +347,7 @@ private static bool IsRunningOnLinux()
[DllImport(libgit2)]
public static extern int git_signature_new(
- out IntPtr signature,
+ out SignatureSafeHandle signature,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string email,
long time,
@@ -373,7 +373,7 @@ private static bool IsRunningOnLinux()
RepositorySafeHandle repo,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name,
IntPtr target,
- GitSignature signature,
+ SignatureSafeHandle signature,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message,
bool force);
View
13 LibGit2Sharp/Core/SignatureSafeHandle.cs
@@ -0,0 +1,13 @@
+using System;
+
+namespace LibGit2Sharp.Core
+{
+ internal class SignatureSafeHandle : SafeHandleBase
+ {
+ protected override bool ReleaseHandle()
+ {
+ NativeMethods.git_signature_free(handle);
+ return true;
+ }
+ }
+}
View
1 LibGit2Sharp/LibGit2Sharp.csproj
@@ -56,6 +56,7 @@
<Compile Include="Core\EnumExtensions.cs" />
<Compile Include="Core\GitObjectExtensions.cs" />
<Compile Include="Core\ReferenceExtensions.cs" />
+ <Compile Include="Core\SignatureSafeHandle.cs" />
<Compile Include="DetachedHead.cs" />
<Compile Include="LibGit2Exception.cs" />
<Compile Include="Core\ConfigurationSafeHandle.cs" />
View
47 LibGit2Sharp/Signature.cs
@@ -9,16 +9,18 @@ namespace LibGit2Sharp
/// </summary>
public class Signature
{
- private readonly GitSignature handle = new GitSignature();
- private DateTimeOffset? when;
+ private readonly DateTimeOffset when;
+ private readonly string name;
+ private readonly string email;
- internal Signature(IntPtr signaturePtr, bool ownedByRepo = true)
+ internal Signature(IntPtr signaturePtr)
{
+ var handle = new GitSignature();
Marshal.PtrToStructure(signaturePtr, handle);
- if (!ownedByRepo)
- {
- NativeMethods.git_signature_free(signaturePtr);
- }
+
+ name = handle.Name;
+ email = handle.Email;
+ when = Epoch.ToDateTimeOffset(handle.When.Time, handle.When.Offset);
}
/// <summary>
@@ -28,53 +30,44 @@ internal Signature(IntPtr signaturePtr, bool ownedByRepo = true)
/// <param name = "email">The email.</param>
/// <param name = "when">The when.</param>
public Signature(string name, string email, DateTimeOffset when)
- : this(CreateSignature(name, email, when), false)
{
+ this.name = name;
+ this.email = email;
+ this.when = when;
}
- private static IntPtr CreateSignature(string name, string email, DateTimeOffset when)
+ internal SignatureSafeHandle BuildHandle()
{
- IntPtr signature;
- int result = NativeMethods.git_signature_new(out signature, name, email, when.ToSecondsSinceEpoch(), (int)when.Offset.TotalMinutes);
- Ensure.Success(result);
+ SignatureSafeHandle signature;
+ int result = NativeMethods.git_signature_new(out signature, name, email, when.ToSecondsSinceEpoch(),
+ (int) when.Offset.TotalMinutes);
+ Ensure.Success(result);
return signature;
}
- internal GitSignature Handle
- {
- get { return handle; }
- }
-
/// <summary>
/// Gets the name.
/// </summary>
public string Name
{
- get { return handle.Name; }
+ get { return name; }
}
/// <summary>
/// Gets the email.
/// </summary>
public string Email
{
- get { return handle.Email; }
+ get { return email; }
}
/// <summary>
/// Gets the date when this signature happened.
/// </summary>
public DateTimeOffset When
{
- get
- {
- if (when == null)
- {
- when = Epoch.ToDateTimeOffset(handle.When.Time, handle.When.Offset);
- }
- return when.Value;
- }
+ get { return when; }
}
}
}
View
3 LibGit2Sharp/TagCollection.cs
@@ -83,9 +83,10 @@ public Tag Create(string name, string target, Signature tagger, string message,
int res;
using (var objectPtr = new ObjectSafeWrapper(objectToTag.Id, repo))
+ using (SignatureSafeHandle taggerHandle = tagger.BuildHandle())
{
GitOid oid;
- res = NativeMethods.git_tag_create(out oid, repo.Handle, name, objectPtr.ObjectPtr, tagger.Handle, message, allowOverwrite);
+ res = NativeMethods.git_tag_create(out oid, repo.Handle, name, objectPtr.ObjectPtr, taggerHandle, message, allowOverwrite);
}
Ensure.Success(res);

0 comments on commit 9196a48

Please sign in to comment.