diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-02-02 02:39:39 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-02-02 02:39:39 +0400 |
commit | 9196a4823cb75dea011a97b4f1b5a12b9d3c041b (patch) | |
tree | 08e7de9268a879c8f1b8d233bab6c6fbb249fc1c /LibGit2Sharp | |
parent | 839c659c35d8fe42407ef3a749a251eb4daff254 (diff) |
Simplify handling of native signature handle
Diffstat (limited to 'LibGit2Sharp')
-rw-r--r-- | LibGit2Sharp/CommitCollection.cs | 6 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 8 | ||||
-rw-r--r-- | LibGit2Sharp/Core/SignatureSafeHandle.cs | 13 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/Signature.cs | 47 | ||||
-rw-r--r-- | LibGit2Sharp/TagCollection.cs | 3 |
6 files changed, 44 insertions, 34 deletions
diff --git a/LibGit2Sharp/CommitCollection.cs b/LibGit2Sharp/CommitCollection.cs index 2a70fe4a..db62d31c 100644 --- a/LibGit2Sharp/CommitCollection.cs +++ b/LibGit2Sharp/CommitCollection.cs @@ -147,10 +147,12 @@ namespace LibGit2Sharp 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); diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 84c8ac8e..8b6173b3 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -59,8 +59,8 @@ namespace LibGit2Sharp.Core 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 @@ namespace LibGit2Sharp.Core [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 @@ namespace LibGit2Sharp.Core 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); diff --git a/LibGit2Sharp/Core/SignatureSafeHandle.cs b/LibGit2Sharp/Core/SignatureSafeHandle.cs new file mode 100644 index 00000000..bfedef4e --- /dev/null +++ b/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; + } + } +} diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 5d4fa779..90b05c27 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/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" /> diff --git a/LibGit2Sharp/Signature.cs b/LibGit2Sharp/Signature.cs index fe486740..32075609 100644 --- a/LibGit2Sharp/Signature.cs +++ b/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,30 +30,28 @@ namespace LibGit2Sharp /// <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> @@ -59,7 +59,7 @@ namespace LibGit2Sharp /// </summary> public string Email { - get { return handle.Email; } + get { return email; } } /// <summary> @@ -67,14 +67,7 @@ namespace LibGit2Sharp /// </summary> public DateTimeOffset When { - get - { - if (when == null) - { - when = Epoch.ToDateTimeOffset(handle.When.Time, handle.When.Offset); - } - return when.Value; - } + get { return when; } } } } diff --git a/LibGit2Sharp/TagCollection.cs b/LibGit2Sharp/TagCollection.cs index 9a821c57..1bf23f9d 100644 --- a/LibGit2Sharp/TagCollection.cs +++ b/LibGit2Sharp/TagCollection.cs @@ -83,9 +83,10 @@ namespace LibGit2Sharp 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); |