diff options
author | nulltoken <emeric.fermas@gmail.com> | 2012-03-28 13:09:28 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-03-28 16:11:01 +0400 |
commit | 882ce61dc0b39ac9bf2303923fb90f4f6158753c (patch) | |
tree | 378bb837e2fdac7bfec32a8046603f46a802361e /LibGit2Sharp | |
parent | 38e2c62fd91c13288db604358689359f4703cb61 (diff) |
Replace usage of IntPtr holding references to git objects with GitObjectSafeHandle
Diffstat (limited to 'LibGit2Sharp')
-rw-r--r-- | LibGit2Sharp/Blob.cs | 3 | ||||
-rw-r--r-- | LibGit2Sharp/Commit.cs | 7 | ||||
-rw-r--r-- | LibGit2Sharp/CommitCollection.cs | 3 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 55 | ||||
-rwxr-xr-x | LibGit2Sharp/Core/ObjectSafeWrapper.cs | 19 | ||||
-rw-r--r-- | LibGit2Sharp/GitObject.cs | 36 | ||||
-rw-r--r-- | LibGit2Sharp/Repository.cs | 50 | ||||
-rw-r--r-- | LibGit2Sharp/TagAnnotation.cs | 3 | ||||
-rw-r--r-- | LibGit2Sharp/Tree.cs | 5 |
9 files changed, 91 insertions, 90 deletions
diff --git a/LibGit2Sharp/Blob.cs b/LibGit2Sharp/Blob.cs index ec6971eb..174d000b 100644 --- a/LibGit2Sharp/Blob.cs +++ b/LibGit2Sharp/Blob.cs @@ -3,6 +3,7 @@ using System.IO; using System.Runtime.InteropServices; using System.Text; using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { @@ -62,7 +63,7 @@ namespace LibGit2Sharp return Encoding.Unicode.GetString(Content); } - internal static Blob BuildFromPtr(IntPtr obj, ObjectId id, Repository repo) + internal static Blob BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo) { var blob = new Blob(repo, id) { diff --git a/LibGit2Sharp/Commit.cs b/LibGit2Sharp/Commit.cs index 99e3c346..779a1edd 100644 --- a/LibGit2Sharp/Commit.cs +++ b/LibGit2Sharp/Commit.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Runtime.InteropServices; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Compat; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { @@ -111,14 +112,14 @@ namespace LibGit2Sharp for (uint i = 0; i < parentsCount; i++) { - IntPtr parentCommit; + GitObjectSafeHandle parentCommit; Ensure.Success(NativeMethods.git_commit_parent(out parentCommit, obj.ObjectPtr, i)); yield return BuildFromPtr(parentCommit, ObjectIdOf(parentCommit), repo); } } } - internal static Commit BuildFromPtr(IntPtr obj, ObjectId id, Repository repo) + internal static Commit BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo) { var treeId = new ObjectId(NativeMethods.git_commit_tree_oid(obj).MarshalAsOid()); @@ -131,7 +132,7 @@ namespace LibGit2Sharp }; } - private static string RetrieveEncodingOf(IntPtr obj) + private static string RetrieveEncodingOf(GitObjectSafeHandle obj) { string encoding = NativeMethods.git_commit_message_encoding(obj); diff --git a/LibGit2Sharp/CommitCollection.cs b/LibGit2Sharp/CommitCollection.cs index 1bcc43e7..1853a987 100644 --- a/LibGit2Sharp/CommitCollection.cs +++ b/LibGit2Sharp/CommitCollection.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Globalization; using System.Linq; using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { @@ -156,7 +157,7 @@ namespace LibGit2Sharp { string encoding = null; //TODO: Handle the encoding of the commit to be created - IntPtr[] parentsPtrs = parentObjectPtrs.Select(o => o.ObjectPtr ).ToArray(); + IntPtr[] parentsPtrs = parentObjectPtrs.Select(o => o.ObjectPtr.DangerousGetHandle() ).ToArray(); res = NativeMethods.git_commit_create(out commitOid, repo.Handle, repo.Refs["HEAD"].CanonicalName, authorHandle, committerHandle, encoding, message, treePtr.ObjectPtr, parentObjectPtrs.Count(), parentsPtrs); Ensure.Success(res); diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 7ed80c1c..430ab4a6 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -3,6 +3,7 @@ using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.InteropServices; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp.Core { @@ -52,16 +53,16 @@ namespace LibGit2Sharp.Core } [DllImport(libgit2)] - public static extern IntPtr git_blob_rawcontent(IntPtr blob); + public static extern IntPtr git_blob_rawcontent(GitObjectSafeHandle blob); [DllImport(libgit2)] - public static extern int git_blob_rawsize(IntPtr blob); + public static extern int git_blob_rawsize(GitObjectSafeHandle blob); [DllImport(libgit2)] - public static extern IntPtr git_commit_author(IntPtr commit); + public static extern IntPtr git_commit_author(GitObjectSafeHandle commit); [DllImport(libgit2)] - public static extern IntPtr git_commit_committer(IntPtr commit); + public static extern IntPtr git_commit_committer(GitObjectSafeHandle commit); [DllImport(libgit2)] public static extern int git_commit_create( @@ -72,29 +73,29 @@ namespace LibGit2Sharp.Core SignatureSafeHandle committer, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string encoding, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message, - IntPtr tree, + GitObjectSafeHandle tree, int parentCount, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 7)] [In] IntPtr[] parents); [DllImport(libgit2)] [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] - public static extern string git_commit_message(IntPtr commit); + public static extern string git_commit_message(GitObjectSafeHandle commit); [DllImport(libgit2)] [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] - public static extern string git_commit_message_encoding(IntPtr commit); + public static extern string git_commit_message_encoding(GitObjectSafeHandle commit); [DllImport(libgit2)] - public static extern int git_commit_parent(out IntPtr parentCommit, IntPtr commit, uint n); + public static extern int git_commit_parent(out GitObjectSafeHandle parentCommit, GitObjectSafeHandle commit, uint n); [DllImport(libgit2)] - public static extern uint git_commit_parentcount(IntPtr commit); + public static extern uint git_commit_parentcount(GitObjectSafeHandle commit); [DllImport(libgit2)] - public static extern int git_commit_tree(out IntPtr tree, IntPtr commit); + public static extern int git_commit_tree(out GitObjectSafeHandle tree, GitObjectSafeHandle commit); [DllImport(libgit2)] - public static extern IntPtr git_commit_tree_oid(IntPtr commit); + public static extern IntPtr git_commit_tree_oid(GitObjectSafeHandle commit); [DllImport(libgit2)] public static extern int git_config_delete(ConfigurationSafeHandle cfg, string name); @@ -191,7 +192,7 @@ namespace LibGit2Sharp.Core public static extern IntPtr git_index_get(IndexSafeHandle index, uint n); [DllImport(libgit2)] - public static extern int git_index_read_tree(IndexSafeHandle index, IntPtr tree); + public static extern int git_index_read_tree(IndexSafeHandle index, GitObjectSafeHandle tree); [DllImport(libgit2)] public static extern int git_index_remove(IndexSafeHandle index, int n); @@ -207,16 +208,16 @@ namespace LibGit2Sharp.Core public static extern void git_object_free(IntPtr obj); [DllImport(libgit2)] - public static extern IntPtr git_object_id(IntPtr obj); + public static extern IntPtr git_object_id(GitObjectSafeHandle obj); [DllImport(libgit2)] - public static extern int git_object_lookup(out IntPtr obj, RepositorySafeHandle repo, ref GitOid id, GitObjectType type); + public static extern int git_object_lookup(out GitObjectSafeHandle obj, RepositorySafeHandle repo, ref GitOid id, GitObjectType type); [DllImport(libgit2)] - public static extern int git_object_lookup_prefix(out IntPtr obj, RepositorySafeHandle repo, ref GitOid id, uint len, GitObjectType type); + public static extern int git_object_lookup_prefix(out GitObjectSafeHandle obj, RepositorySafeHandle repo, ref GitOid id, uint len, GitObjectType type); [DllImport(libgit2)] - public static extern GitObjectType git_object_type(IntPtr obj); + public static extern GitObjectType git_object_type(GitObjectSafeHandle obj); [DllImport(libgit2)] public static extern int git_oid_cmp(ref GitOid a, ref GitOid b); @@ -405,7 +406,7 @@ namespace LibGit2Sharp.Core out GitOid oid, RepositorySafeHandle repo, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name, - IntPtr target, + GitObjectSafeHandle target, SignatureSafeHandle signature, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string message, [MarshalAs(UnmanagedType.Bool)] @@ -416,7 +417,7 @@ namespace LibGit2Sharp.Core out GitOid oid, RepositorySafeHandle repo, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string name, - IntPtr target, + GitObjectSafeHandle target, [MarshalAs(UnmanagedType.Bool)] bool force); @@ -427,17 +428,17 @@ namespace LibGit2Sharp.Core [DllImport(libgit2)] [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] - public static extern string git_tag_message(IntPtr tag); + public static extern string git_tag_message(GitObjectSafeHandle tag); [DllImport(libgit2)] [return : MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] - public static extern string git_tag_name(IntPtr tag); + public static extern string git_tag_name(GitObjectSafeHandle tag); [DllImport(libgit2)] - public static extern IntPtr git_tag_tagger(IntPtr tag); + public static extern IntPtr git_tag_tagger(GitObjectSafeHandle tag); [DllImport(libgit2)] - public static extern IntPtr git_tag_target_oid(IntPtr tag); + public static extern IntPtr git_tag_target_oid(GitObjectSafeHandle tag); [DllImport(libgit2)] public static extern void git_threads_init(); @@ -449,17 +450,17 @@ namespace LibGit2Sharp.Core public static extern int git_tree_create_fromindex(out GitOid treeOid, IndexSafeHandle index); [DllImport(libgit2)] - public static extern int git_tree_entry_2object(out IntPtr obj, RepositorySafeHandle repo, IntPtr entry); + public static extern int git_tree_entry_2object(out GitObjectSafeHandle obj, RepositorySafeHandle repo, IntPtr entry); [DllImport(libgit2)] public static extern uint git_tree_entry_attributes(IntPtr entry); [DllImport(libgit2)] - public static extern IntPtr git_tree_entry_byindex(IntPtr tree, uint idx); + public static extern IntPtr git_tree_entry_byindex(GitObjectSafeHandle tree, uint idx); [DllImport(libgit2)] public static extern IntPtr git_tree_entry_byname( - IntPtr tree, + GitObjectSafeHandle tree, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath filename); [DllImport(libgit2)] @@ -473,10 +474,10 @@ namespace LibGit2Sharp.Core public static extern GitObjectType git_tree_entry_type(IntPtr entry); [DllImport(libgit2)] - public static extern uint git_tree_entrycount(IntPtr tree); + public static extern uint git_tree_entrycount(GitObjectSafeHandle tree); [DllImport(libgit2)] - public static extern int git_tree_get_subtree(out IntPtr tree, IntPtr root, + public static extern int git_tree_get_subtree(out GitObjectSafeHandle tree, GitObjectSafeHandle root, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(FilePathMarshaler))] FilePath treeentry_path); } } diff --git a/LibGit2Sharp/Core/ObjectSafeWrapper.cs b/LibGit2Sharp/Core/ObjectSafeWrapper.cs index 04241b5d..c6ce7fad 100755 --- a/LibGit2Sharp/Core/ObjectSafeWrapper.cs +++ b/LibGit2Sharp/Core/ObjectSafeWrapper.cs @@ -1,24 +1,23 @@ using System; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp.Core { internal class ObjectSafeWrapper : IDisposable { - private IntPtr objectPtr = IntPtr.Zero; + private readonly GitObjectSafeHandle objectPtr; public ObjectSafeWrapper(ObjectId id, Repository repo) { - if (id == null) - { - return; - } + Ensure.ArgumentNotNull(id, "id"); + Ensure.ArgumentNotNull(repo, "repo"); GitOid oid = id.Oid; int res = NativeMethods.git_object_lookup(out objectPtr, repo.Handle, ref oid, GitObjectType.Any); Ensure.Success(res); } - public IntPtr ObjectPtr + public GitObjectSafeHandle ObjectPtr { get { return objectPtr; } } @@ -31,13 +30,7 @@ namespace LibGit2Sharp.Core private void Dispose(bool disposing) { - if (objectPtr == IntPtr.Zero) - { - return; - } - - NativeMethods.git_object_free(objectPtr); - objectPtr = IntPtr.Zero; + objectPtr.SafeDispose(); } ~ObjectSafeWrapper() diff --git a/LibGit2Sharp/GitObject.cs b/LibGit2Sharp/GitObject.cs index 87f53e4a..8c25e0b3 100644 --- a/LibGit2Sharp/GitObject.cs +++ b/LibGit2Sharp/GitObject.cs @@ -2,6 +2,7 @@ using System.Globalization; using System.Runtime.InteropServices; using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { @@ -45,32 +46,25 @@ namespace LibGit2Sharp get { return Id.Sha; } } - internal static GitObject CreateFromPtr(IntPtr obj, ObjectId id, Repository repo, FilePath path) + internal static GitObject CreateFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo, FilePath path) { - try + GitObjectType type = NativeMethods.git_object_type(obj); + switch (type) { - GitObjectType type = NativeMethods.git_object_type(obj); - switch (type) - { - case GitObjectType.Commit: - return Commit.BuildFromPtr(obj, id, repo); - case GitObjectType.Tree: - return Tree.BuildFromPtr(obj, id, repo, path); - case GitObjectType.Tag: - return TagAnnotation.BuildFromPtr(obj, id, repo); - case GitObjectType.Blob: - return Blob.BuildFromPtr(obj, id, repo); - default: - throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id)); - } - } - finally - { - NativeMethods.git_object_free(obj); + case GitObjectType.Commit: + return Commit.BuildFromPtr(obj, id, repo); + case GitObjectType.Tree: + return Tree.BuildFromPtr(obj, id, repo, path); + case GitObjectType.Tag: + return TagAnnotation.BuildFromPtr(obj, id, repo); + case GitObjectType.Blob: + return Blob.BuildFromPtr(obj, id, repo); + default: + throw new LibGit2Exception(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id)); } } - internal static ObjectId ObjectIdOf(IntPtr obj) + internal static ObjectId ObjectIdOf(GitObjectSafeHandle obj) { IntPtr ptr = NativeMethods.git_object_id(obj); return new ObjectId(ptr.MarshalAsOid()); diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index eced8fab..b2c35db3 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Compat; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { @@ -170,7 +171,7 @@ namespace LibGit2Sharp /// </summary> /// <param name = "path">The path to the working folder when initializing a standard ".git" repository. Otherwise, when initializing a bare repository, the path to the expected location of this later.</param> /// <param name = "isBare">true to initialize a bare repository. False otherwise, to initialize a standard ".git" repository.</param> - /// <returns> a new instance of the <see cref = "Repository" /> class. The client code is responsible for calling <see cref="Dispose()"/> on this instance.</returns> + /// <returns> a new instance of the <see cref = "Repository" /> class. The client code is responsible for calling <see cref = "Dispose()" /> on this instance.</returns> public static Repository Init(string path, bool isBare = false) { Ensure.ArgumentNotNullOrEmptyString(path, "path"); @@ -206,31 +207,38 @@ namespace LibGit2Sharp Ensure.ArgumentNotNull(id, "id"); GitOid oid = id.Oid; - IntPtr obj; - int res; + GitObjectSafeHandle obj = null; - if (id is AbbreviatedObjectId) + try { - res = NativeMethods.git_object_lookup_prefix(out obj, handle, ref oid, (uint)((AbbreviatedObjectId)id).Length, type); - } - else - { - res = NativeMethods.git_object_lookup(out obj, handle, ref oid, type); - } + int res; + if (id is AbbreviatedObjectId) + { + res = NativeMethods.git_object_lookup_prefix(out obj, handle, ref oid, (uint)((AbbreviatedObjectId)id).Length, type); + } + else + { + res = NativeMethods.git_object_lookup(out obj, handle, ref oid, type); + } - if (res == (int)GitErrorCode.GIT_ENOTFOUND || res == (int)GitErrorCode.GIT_EINVALIDTYPE) - { - return null; - } + if (res == (int)GitErrorCode.GIT_ENOTFOUND || res == (int)GitErrorCode.GIT_EINVALIDTYPE) + { + return null; + } - Ensure.Success(res); + Ensure.Success(res); - if (id is AbbreviatedObjectId) + if (id is AbbreviatedObjectId) + { + id = GitObject.ObjectIdOf(obj); + } + + return GitObject.CreateFromPtr(obj, id, this, knownPath); + } + finally { - id = GitObject.ObjectIdOf(obj); + obj.SafeDispose(); } - - return GitObject.CreateFromPtr(obj, id, this, knownPath); } /// <summary> @@ -248,7 +256,7 @@ namespace LibGit2Sharp { ObjectId id; - Reference reference = Refs[shaOrReferenceName]; + Reference reference = Refs[shaOrReferenceName]; if (reference != null) { id = reference.PeelToTargetObjectId(); @@ -308,7 +316,7 @@ namespace LibGit2Sharp } /// <summary> - /// Sets the current <see cref="Head"/> to the specified commit and optionally resets the <see cref="Index"/> and + /// Sets the current <see cref = "Head" /> to the specified commit and optionally resets the <see cref = "Index" /> and /// the content of the working tree to match. /// </summary> /// <param name = "resetOptions">Flavor of reset operation to perform.</param> diff --git a/LibGit2Sharp/TagAnnotation.cs b/LibGit2Sharp/TagAnnotation.cs index 4fb9a7ac..cc937c38 100644 --- a/LibGit2Sharp/TagAnnotation.cs +++ b/LibGit2Sharp/TagAnnotation.cs @@ -1,6 +1,7 @@ using System; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Compat; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { @@ -39,7 +40,7 @@ namespace LibGit2Sharp /// </summary> public Signature Tagger { get; private set; } - internal static TagAnnotation BuildFromPtr(IntPtr obj, ObjectId id, Repository repo) + internal static TagAnnotation BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo) { IntPtr oidPtr = NativeMethods.git_tag_target_oid(obj); var targetOid = new ObjectId(oidPtr.MarshalAsOid()); diff --git a/LibGit2Sharp/Tree.cs b/LibGit2Sharp/Tree.cs index 0e9df714..0fd4050c 100644 --- a/LibGit2Sharp/Tree.cs +++ b/LibGit2Sharp/Tree.cs @@ -3,6 +3,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using LibGit2Sharp.Core; +using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { @@ -46,7 +47,7 @@ namespace LibGit2Sharp using (var obj = new ObjectSafeWrapper(Id, repo)) { - IntPtr objectPtr; + GitObjectSafeHandle objectPtr; int res = NativeMethods.git_tree_get_subtree(out objectPtr, obj.ObjectPtr, relativePath); @@ -134,7 +135,7 @@ namespace LibGit2Sharp #endregion - internal static Tree BuildFromPtr(IntPtr obj, ObjectId id, Repository repo, FilePath path) + internal static Tree BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo, FilePath path) { var tree = new Tree(id, path, (int)NativeMethods.git_tree_entrycount(obj), repo); return tree; |