diff options
author | nulltoken <emeric.fermas@gmail.com> | 2015-05-29 12:59:29 +0300 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2015-05-29 12:59:29 +0300 |
commit | 8960ead21580df6b5f1177e0837470cd4e781de7 (patch) | |
tree | 8defcbb1df8898ed643578111e30ed6623fb13d6 | |
parent | 27abed14f52809a7058b36a980778d8fc0caf50c (diff) |
Drop ThreadAffinity
Fix #953
-rw-r--r-- | LibGit2Sharp/Core/Proxy.cs | 1799 |
1 files changed, 675 insertions, 1124 deletions
diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index 4739d7bc..65a389a0 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -5,7 +5,6 @@ using System.IO; using System.Linq; using System.Runtime.InteropServices; using System.Text; -using System.Threading; using LibGit2Sharp.Core.Handles; using LibGit2Sharp.Handlers; @@ -100,13 +99,10 @@ namespace LibGit2Sharp.Core FilePath path, GitBlameOptions options) { - using (ThreadAffinity()) - { - BlameSafeHandle handle; - int res = NativeMethods.git_blame_file(out handle, repo, path, options); - Ensure.ZeroResult(res); - return handle; - } + BlameSafeHandle handle; + int res = NativeMethods.git_blame_file(out handle, repo, path, options); + Ensure.ZeroResult(res); + return handle; } public static GitBlameHunk git_blame_get_hunk_byindex(BlameSafeHandle blame, uint idx) @@ -125,44 +121,35 @@ namespace LibGit2Sharp.Core public static ObjectId git_blob_create_fromchunks(RepositorySafeHandle repo, FilePath hintpath, NativeMethods.source_callback fileCallback) { - using (ThreadAffinity()) - { - var oid = new GitOid(); - int res = NativeMethods.git_blob_create_fromchunks(ref oid, repo, hintpath, fileCallback, IntPtr.Zero); + var oid = new GitOid(); + int res = NativeMethods.git_blob_create_fromchunks(ref oid, repo, hintpath, fileCallback, IntPtr.Zero); - if (res == (int)GitErrorCode.User) - { - throw new EndOfStreamException("The stream ended unexpectedly"); - } + if (res == (int)GitErrorCode.User) + { + throw new EndOfStreamException("The stream ended unexpectedly"); + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return oid; - } + return oid; } public static ObjectId git_blob_create_fromdisk(RepositorySafeHandle repo, FilePath path) { - using (ThreadAffinity()) - { - var oid = new GitOid(); - int res = NativeMethods.git_blob_create_fromdisk(ref oid, repo, path); - Ensure.ZeroResult(res); + var oid = new GitOid(); + int res = NativeMethods.git_blob_create_fromdisk(ref oid, repo, path); + Ensure.ZeroResult(res); - return oid; - } + return oid; } public static ObjectId git_blob_create_fromfile(RepositorySafeHandle repo, FilePath path) { - using (ThreadAffinity()) - { - var oid = new GitOid(); - int res = NativeMethods.git_blob_create_fromworkdir(ref oid, repo, path); - Ensure.ZeroResult(res); + var oid = new GitOid(); + int res = NativeMethods.git_blob_create_fromworkdir(ref oid, repo, path); + Ensure.ZeroResult(res); - return oid; - } + return oid; } public static UnmanagedMemoryStream git_blob_filtered_content_stream(RepositorySafeHandle repo, ObjectId id, FilePath path, bool check_for_binary_data) @@ -204,27 +191,21 @@ namespace LibGit2Sharp.Core public static ReferenceSafeHandle git_branch_create_from_annotated(RepositorySafeHandle repo, string branch_name, string targetIdentifier, bool force) { - using (ThreadAffinity()) - { - ReferenceSafeHandle reference; + ReferenceSafeHandle reference; - using (var annotatedCommit = git_annotated_commit_from_revspec(repo, targetIdentifier)) - { - int res = NativeMethods.git_branch_create_from_annotated(out reference, repo, branch_name, annotatedCommit, force); - Ensure.ZeroResult(res); - } - - return reference; + using (var annotatedCommit = git_annotated_commit_from_revspec(repo, targetIdentifier)) + { + int res = NativeMethods.git_branch_create_from_annotated(out reference, repo, branch_name, annotatedCommit, force); + Ensure.ZeroResult(res); } + + return reference; } public static void git_branch_delete(ReferenceSafeHandle reference) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_branch_delete(reference); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_branch_delete(reference); + Ensure.ZeroResult(res); } public static IEnumerable<Branch> git_branch_iterator(Repository repo, GitBranchType branchType) @@ -253,18 +234,14 @@ namespace LibGit2Sharp.Core public static ReferenceSafeHandle git_branch_move(ReferenceSafeHandle reference, string new_branch_name, bool force) { - using (ThreadAffinity()) - { - ReferenceSafeHandle ref_out; - int res = NativeMethods.git_branch_move(out ref_out, reference, new_branch_name, force); - Ensure.ZeroResult(res); - return ref_out; - } + ReferenceSafeHandle ref_out; + int res = NativeMethods.git_branch_move(out ref_out, reference, new_branch_name, force); + Ensure.ZeroResult(res); + return ref_out; } public static string git_branch_remote_name(RepositorySafeHandle repo, string canonical_branch_name, bool shouldThrowIfNotFound) { - using (ThreadAffinity()) using (var buf = new GitBuf()) { int res = NativeMethods.git_branch_remote_name(buf, repo, canonical_branch_name); @@ -282,7 +259,6 @@ namespace LibGit2Sharp.Core public static string git_branch_upstream_name(RepositorySafeHandle handle, string canonicalReferenceName) { - using (ThreadAffinity()) using (var buf = new GitBuf()) { int res = NativeMethods.git_branch_upstream_name(buf, handle, canonicalReferenceName); @@ -314,7 +290,6 @@ namespace LibGit2Sharp.Core ObjectId treeId, ref GitCheckoutOpts opts) { - using (ThreadAffinity()) using (var osw = new ObjectSafeWrapper(treeId, repo)) { int res = NativeMethods.git_checkout_tree(repo, osw.ObjectPtr, ref opts); @@ -324,11 +299,8 @@ namespace LibGit2Sharp.Core public static void git_checkout_index(RepositorySafeHandle repo, GitObjectSafeHandle treeish, ref GitCheckoutOpts opts) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_checkout_index(repo, treeish, ref opts); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_checkout_index(repo, treeish, ref opts); + Ensure.ZeroResult(res); } #endregion @@ -337,7 +309,6 @@ namespace LibGit2Sharp.Core internal static void git_cherrypick(RepositorySafeHandle repo, ObjectId commit, GitCherryPickOptions options) { - using (ThreadAffinity()) using (var nativeCommit = git_object_lookup(repo, commit, GitObjectType.Commit)) { int res = NativeMethods.git_cherrypick(repo, nativeCommit, options); @@ -353,13 +324,10 @@ namespace LibGit2Sharp.Core string workdir, ref GitCloneOptions opts) { - using (ThreadAffinity()) - { - RepositorySafeHandle repo; - int res = NativeMethods.git_clone(out repo, url, workdir, ref opts); - Ensure.ZeroResult(res); - return repo; - } + RepositorySafeHandle repo; + int res = NativeMethods.git_clone(out repo, url, workdir, ref opts); + Ensure.ZeroResult(res); + return repo; } #endregion @@ -385,7 +353,6 @@ namespace LibGit2Sharp.Core Tree tree, GitOid[] parentIds) { - using (ThreadAffinity()) using (SignatureSafeHandle authorHandle = author.BuildHandle()) using (SignatureSafeHandle committerHandle = committer.BuildHandle()) using (var parentPtrs = new ArrayMarshaler<GitOid>(parentIds)) @@ -449,45 +416,36 @@ namespace LibGit2Sharp.Core public static void git_config_add_file_ondisk(ConfigurationSafeHandle config, FilePath path, ConfigurationLevel level) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_config_add_file_ondisk(config, path, (uint)level, true); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_config_add_file_ondisk(config, path, (uint)level, true); + Ensure.ZeroResult(res); } public static bool git_config_delete(ConfigurationSafeHandle config, string name) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_config_delete_entry(config, name); - - if (res == (int)GitErrorCode.NotFound) - { - return false; - } + int res = NativeMethods.git_config_delete_entry(config, name); - Ensure.ZeroResult(res); - return true; + if (res == (int)GitErrorCode.NotFound) + { + return false; } + + Ensure.ZeroResult(res); + return true; } const string anyValue = ".*"; public static bool git_config_delete_multivar(ConfigurationSafeHandle config, string name) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_config_delete_multivar(config, name, anyValue); + int res = NativeMethods.git_config_delete_multivar(config, name, anyValue); - if (res == (int)GitErrorCode.NotFound) - { - return false; - } - - Ensure.ZeroResult(res); - return true; + if (res == (int)GitErrorCode.NotFound) + { + return false; } + + Ensure.ZeroResult(res); + return true; } public static FilePath git_config_find_global() @@ -528,18 +486,15 @@ namespace LibGit2Sharp.Core try { - using (ThreadAffinity()) + var res = NativeMethods.git_config_get_entry(out handle, config, key); + if (res == (int)GitErrorCode.NotFound) { - var res = NativeMethods.git_config_get_entry(out handle, config, key); - if (res == (int)GitErrorCode.NotFound) - { - return null; - } + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - entry = handle.MarshalAsGitConfigEntry(); - } + entry = handle.MarshalAsGitConfigEntry(); } finally { @@ -553,104 +508,77 @@ namespace LibGit2Sharp.Core public static ConfigurationSafeHandle git_config_new() { - using (ThreadAffinity()) - { - ConfigurationSafeHandle handle; - int res = NativeMethods.git_config_new(out handle); - Ensure.ZeroResult(res); + ConfigurationSafeHandle handle; + int res = NativeMethods.git_config_new(out handle); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static ConfigurationSafeHandle git_config_open_level(ConfigurationSafeHandle parent, ConfigurationLevel level) { - using (ThreadAffinity()) - { - ConfigurationSafeHandle handle; - int res = NativeMethods.git_config_open_level(out handle, parent, (uint)level); + ConfigurationSafeHandle handle; + int res = NativeMethods.git_config_open_level(out handle, parent, (uint)level); - if (res == (int)GitErrorCode.NotFound) - { - return null; - } + if (res == (int)GitErrorCode.NotFound) + { + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static bool git_config_parse_bool(string value) { - using (ThreadAffinity()) - { - bool outVal; - var res = NativeMethods.git_config_parse_bool(out outVal, value); + bool outVal; + var res = NativeMethods.git_config_parse_bool(out outVal, value); - Ensure.ZeroResult(res); - return outVal; - } + Ensure.ZeroResult(res); + return outVal; } public static int git_config_parse_int32(string value) { - using (ThreadAffinity()) - { - int outVal; - var res = NativeMethods.git_config_parse_int32(out outVal, value); + int outVal; + var res = NativeMethods.git_config_parse_int32(out outVal, value); - Ensure.ZeroResult(res); - return outVal; - } + Ensure.ZeroResult(res); + return outVal; } public static long git_config_parse_int64(string value) { - using (ThreadAffinity()) - { - long outVal; - var res = NativeMethods.git_config_parse_int64(out outVal, value); + long outVal; + var res = NativeMethods.git_config_parse_int64(out outVal, value); - Ensure.ZeroResult(res); - return outVal; - } + Ensure.ZeroResult(res); + return outVal; } public static void git_config_set_bool(ConfigurationSafeHandle config, string name, bool value) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_config_set_bool(config, name, value); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_config_set_bool(config, name, value); + Ensure.ZeroResult(res); } public static void git_config_set_int32(ConfigurationSafeHandle config, string name, int value) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_config_set_int32(config, name, value); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_config_set_int32(config, name, value); + Ensure.ZeroResult(res); } public static void git_config_set_int64(ConfigurationSafeHandle config, string name, long value) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_config_set_int64(config, name, value); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_config_set_int64(config, name, value); + Ensure.ZeroResult(res); } public static void git_config_set_string(ConfigurationSafeHandle config, string name, string value) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_config_set_string(config, name, value); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_config_set_string(config, name, value); + Ensure.ZeroResult(res); } public static ICollection<TResult> git_config_foreach<TResult>( @@ -687,14 +615,11 @@ namespace LibGit2Sharp.Core public static ConfigurationSafeHandle git_config_snapshot(ConfigurationSafeHandle config) { - using (ThreadAffinity()) - { - ConfigurationSafeHandle handle; - int res = NativeMethods.git_config_snapshot(out handle, config); - Ensure.ZeroResult(res); + ConfigurationSafeHandle handle; + int res = NativeMethods.git_config_snapshot(out handle, config); + Ensure.ZeroResult(res); - return handle; - } + return handle; } #endregion @@ -709,7 +634,6 @@ namespace LibGit2Sharp.Core Ensure.ArgumentPositiveInt32(options.MinimumCommitIdAbbreviatedSize, "options.MinimumCommitIdAbbreviatedSize"); - using (ThreadAffinity()) using (var osw = new ObjectSafeWrapper(committishId, repo)) { GitDescribeOptions opts = new GitDescribeOptions @@ -772,7 +696,6 @@ namespace LibGit2Sharp.Core NativeMethods.git_diff_hunk_cb hunkCallback, NativeMethods.git_diff_line_cb lineCallback) { - using (ThreadAffinity()) using (var osw1 = new ObjectSafeWrapper(oldBlob, repo, true)) using (var osw2 = new ObjectSafeWrapper(newBlob, repo, true)) { @@ -790,11 +713,8 @@ namespace LibGit2Sharp.Core NativeMethods.git_diff_hunk_cb hunkCallback, NativeMethods.git_diff_line_cb lineCallback) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_diff_foreach(diff, fileCallback, hunkCallback, lineCallback, IntPtr.Zero); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_diff_foreach(diff, fileCallback, hunkCallback, lineCallback, IntPtr.Zero); + Ensure.ZeroResult(res); } public static DiffSafeHandle git_diff_tree_to_index( @@ -803,7 +723,6 @@ namespace LibGit2Sharp.Core ObjectId oldTree, GitDiffOptions options) { - using (ThreadAffinity()) using (var osw = new ObjectSafeWrapper(oldTree, repo, true)) { DiffSafeHandle diff; @@ -821,11 +740,8 @@ namespace LibGit2Sharp.Core public static void git_diff_merge(DiffSafeHandle onto, DiffSafeHandle from) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_diff_merge(onto, from); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_diff_merge(onto, from); + Ensure.ZeroResult(res); } public static DiffSafeHandle git_diff_tree_to_tree( @@ -834,7 +750,6 @@ namespace LibGit2Sharp.Core ObjectId newTree, GitDiffOptions options) { - using (ThreadAffinity()) using (var osw1 = new ObjectSafeWrapper(oldTree, repo, true)) using (var osw2 = new ObjectSafeWrapper(newTree, repo, true)) { @@ -851,14 +766,11 @@ namespace LibGit2Sharp.Core IndexSafeHandle index, GitDiffOptions options) { - using (ThreadAffinity()) - { - DiffSafeHandle diff; - int res = NativeMethods.git_diff_index_to_workdir(out diff, repo, index, options); - Ensure.ZeroResult(res); + DiffSafeHandle diff; + int res = NativeMethods.git_diff_index_to_workdir(out diff, repo, index, options); + Ensure.ZeroResult(res); - return diff; - } + return diff; } public static DiffSafeHandle git_diff_tree_to_workdir( @@ -866,7 +778,6 @@ namespace LibGit2Sharp.Core ObjectId oldTree, GitDiffOptions options) { - using (ThreadAffinity()) using (var osw = new ObjectSafeWrapper(oldTree, repo, true)) { DiffSafeHandle diff; @@ -879,11 +790,8 @@ namespace LibGit2Sharp.Core public static void git_diff_find_similar(DiffSafeHandle diff, GitDiffFindOptions options) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_diff_find_similar(diff, options); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_diff_find_similar(diff, options); + Ensure.ZeroResult(res); } public static int git_diff_num_deltas(DiffSafeHandle diff) @@ -910,32 +818,25 @@ namespace LibGit2Sharp.Core GitOid oid1 = first.Id.Oid; GitOid oid2 = second.Id.Oid; - using (ThreadAffinity()) - { - UIntPtr ahead; - UIntPtr behind; + UIntPtr ahead; + UIntPtr behind; - int res = NativeMethods.git_graph_ahead_behind(out ahead, out behind, repo, ref oid1, ref oid2); + int res = NativeMethods.git_graph_ahead_behind(out ahead, out behind, repo, ref oid1, ref oid2); - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return new Tuple<int?, int?>((int)ahead, (int)behind); - } + return new Tuple<int?, int?>((int)ahead, (int)behind); } public static bool git_graph_descendant_of(RepositorySafeHandle repo, ObjectId commitId, ObjectId ancestorId) { GitOid oid1 = commitId.Oid; GitOid oid2 = ancestorId.Oid; + int res = NativeMethods.git_graph_descendant_of(repo, ref oid1, ref oid2); - using (ThreadAffinity()) - { - int res = NativeMethods.git_graph_descendant_of(repo, ref oid1, ref oid2); - - Ensure.BooleanResult(res); + Ensure.BooleanResult(res); - return (res == 1); - } + return (res == 1); } #endregion @@ -944,32 +845,23 @@ namespace LibGit2Sharp.Core public static void git_ignore_add_rule(RepositorySafeHandle repo, string rules) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_ignore_add_rule(repo, rules); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_ignore_add_rule(repo, rules); + Ensure.ZeroResult(res); } public static void git_ignore_clear_internal_rules(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_ignore_clear_internal_rules(repo); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_ignore_clear_internal_rules(repo); + Ensure.ZeroResult(res); } public static bool git_ignore_path_is_ignored(RepositorySafeHandle repo, string path) { - using (ThreadAffinity()) - { - int ignored; - int res = NativeMethods.git_ignore_path_is_ignored(out ignored, repo, path); - Ensure.ZeroResult(res); + int ignored; + int res = NativeMethods.git_ignore_path_is_ignored(out ignored, repo, path); + Ensure.ZeroResult(res); - return (ignored != 0); - } + return (ignored != 0); } #endregion @@ -978,20 +870,14 @@ namespace LibGit2Sharp.Core public static void git_index_add(IndexSafeHandle index, GitIndexEntry entry) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_add(index, entry); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_index_add(index, entry); + Ensure.ZeroResult(res); } public static void git_index_add_bypath(IndexSafeHandle index, FilePath path) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_add_bypath(index, path); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_index_add_bypath(index, path); + Ensure.ZeroResult(res); } public static Conflict git_index_conflict_get( @@ -1018,40 +904,34 @@ namespace LibGit2Sharp.Core public static ConflictIteratorSafeHandle git_index_conflict_iterator_new(IndexSafeHandle index) { - using (ThreadAffinity()) - { - ConflictIteratorSafeHandle iter; - int res = NativeMethods.git_index_conflict_iterator_new(out iter, index); - Ensure.ZeroResult(res); + ConflictIteratorSafeHandle iter; + int res = NativeMethods.git_index_conflict_iterator_new(out iter, index); + Ensure.ZeroResult(res); - return iter; - } + return iter; } public static Conflict git_index_conflict_next(ConflictIteratorSafeHandle iterator) { IndexEntrySafeHandle ancestor, ours, theirs; - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_conflict_next(out ancestor, out ours, out theirs, iterator); + int res = NativeMethods.git_index_conflict_next(out ancestor, out ours, out theirs, iterator); - if (res == (int)GitErrorCode.IterOver) - { - return null; - } + if (res == (int)GitErrorCode.IterOver) + { + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - using (ancestor) - using (ours) - using (theirs) - { - return new Conflict( - IndexEntry.BuildFromPtr(ancestor), - IndexEntry.BuildFromPtr(ours), - IndexEntry.BuildFromPtr(theirs)); - } + using (ancestor) + using (ours) + using (theirs) + { + return new Conflict( + IndexEntry.BuildFromPtr(ancestor), + IndexEntry.BuildFromPtr(ours), + IndexEntry.BuildFromPtr(theirs)); } } @@ -1109,32 +989,23 @@ namespace LibGit2Sharp.Core public static IndexSafeHandle git_index_open(FilePath indexpath) { - using (ThreadAffinity()) - { - IndexSafeHandle handle; - int res = NativeMethods.git_index_open(out handle, indexpath); - Ensure.ZeroResult(res); + IndexSafeHandle handle; + int res = NativeMethods.git_index_open(out handle, indexpath); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static void git_index_read(IndexSafeHandle index) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_read(index, false); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_index_read(index, false); + Ensure.ZeroResult(res); } public static void git_index_remove_bypath(IndexSafeHandle index, FilePath path) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_remove_bypath(index, path); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_index_remove_bypath(index, path); + Ensure.ZeroResult(res); } public static int git_index_reuc_entrycount(IndexSafeHandle index) @@ -1155,53 +1026,38 @@ namespace LibGit2Sharp.Core public static void git_index_write(IndexSafeHandle index) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_write(index); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_index_write(index); + Ensure.ZeroResult(res); } public static ObjectId git_index_write_tree(IndexSafeHandle index) { - using (ThreadAffinity()) - { - GitOid treeOid; - int res = NativeMethods.git_index_write_tree(out treeOid, index); - Ensure.ZeroResult(res); + GitOid treeOid; + int res = NativeMethods.git_index_write_tree(out treeOid, index); + Ensure.ZeroResult(res); - return treeOid; - } + return treeOid; } public static ObjectId git_index_write_tree_to(IndexSafeHandle index, RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - GitOid treeOid; - int res = NativeMethods.git_index_write_tree_to(out treeOid, index, repo); - Ensure.ZeroResult(res); + GitOid treeOid; + int res = NativeMethods.git_index_write_tree_to(out treeOid, index, repo); + Ensure.ZeroResult(res); - return treeOid; - } + return treeOid; } public static void git_index_read_fromtree(Index index, GitObjectSafeHandle tree) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_read_tree(index.Handle, tree); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_index_read_tree(index.Handle, tree); + Ensure.ZeroResult(res); } public static void git_index_clear(Index index) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_index_clear(index.Handle); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_index_clear(index.Handle); + Ensure.ZeroResult(res); } #endregion @@ -1210,106 +1066,85 @@ namespace LibGit2Sharp.Core public static IndexSafeHandle git_merge_commits(RepositorySafeHandle repo, GitObjectSafeHandle ourCommit, GitObjectSafeHandle theirCommit, GitMergeOpts opts) { - using (ThreadAffinity()) - { - IndexSafeHandle index; - int res = NativeMethods.git_merge_commits(out index, repo, ourCommit, theirCommit, ref opts); - Ensure.ZeroResult(res); + IndexSafeHandle index; + int res = NativeMethods.git_merge_commits(out index, repo, ourCommit, theirCommit, ref opts); + Ensure.ZeroResult(res); - return index; - } + return index; } public static ObjectId git_merge_base_many(RepositorySafeHandle repo, GitOid[] commitIds) { - using (ThreadAffinity()) - { - GitOid ret; - int res = NativeMethods.git_merge_base_many(out ret, repo, commitIds.Length, commitIds); + GitOid ret; + int res = NativeMethods.git_merge_base_many(out ret, repo, commitIds.Length, commitIds); - if (res == (int)GitErrorCode.NotFound) - { - return null; - } + if (res == (int)GitErrorCode.NotFound) + { + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return ret; - } + return ret; } public static ObjectId git_merge_base_octopus(RepositorySafeHandle repo, GitOid[] commitIds) { - using (ThreadAffinity()) - { - GitOid ret; - int res = NativeMethods.git_merge_base_octopus(out ret, repo, commitIds.Length, commitIds); + GitOid ret; + int res = NativeMethods.git_merge_base_octopus(out ret, repo, commitIds.Length, commitIds); - if (res == (int)GitErrorCode.NotFound) - { - return null; - } + if (res == (int)GitErrorCode.NotFound) + { + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return ret; - } + return ret; } public static GitAnnotatedCommitHandle git_annotated_commit_from_fetchhead(RepositorySafeHandle repo, string branchName, string remoteUrl, GitOid oid) { - using (ThreadAffinity()) - { - GitAnnotatedCommitHandle merge_head; + GitAnnotatedCommitHandle merge_head; - int res = NativeMethods.git_annotated_commit_from_fetchhead(out merge_head, repo, branchName, remoteUrl, ref oid); + int res = NativeMethods.git_annotated_commit_from_fetchhead(out merge_head, repo, branchName, remoteUrl, ref oid); - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return merge_head; - } + return merge_head; } public static GitAnnotatedCommitHandle git_annotated_commit_lookup(RepositorySafeHandle repo, GitOid oid) { - using (ThreadAffinity()) - { - GitAnnotatedCommitHandle their_head; + GitAnnotatedCommitHandle their_head; - int res = NativeMethods.git_annotated_commit_lookup(out their_head, repo, ref oid); + int res = NativeMethods.git_annotated_commit_lookup(out their_head, repo, ref oid); - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return their_head; - } + return their_head; } public static GitAnnotatedCommitHandle git_annotated_commit_from_ref(RepositorySafeHandle repo, ReferenceSafeHandle reference) { - using (ThreadAffinity()) - { - GitAnnotatedCommitHandle their_head; + GitAnnotatedCommitHandle their_head; - int res = NativeMethods.git_annotated_commit_from_ref(out their_head, repo, reference); + int res = NativeMethods.git_annotated_commit_from_ref(out their_head, repo, reference); - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return their_head; - } + return their_head; } public static GitAnnotatedCommitHandle git_annotated_commit_from_revspec(RepositorySafeHandle repo, string revspec) { - using (ThreadAffinity()) - { - GitAnnotatedCommitHandle their_head; + GitAnnotatedCommitHandle their_head; - int res = NativeMethods.git_annotated_commit_from_revspec(out their_head, repo, revspec); + int res = NativeMethods.git_annotated_commit_from_revspec(out their_head, repo, revspec); - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return their_head; - } + return their_head; } public static ObjectId git_annotated_commit_id(GitAnnotatedCommitHandle mergeHead) @@ -1319,19 +1154,16 @@ namespace LibGit2Sharp.Core public static void git_merge(RepositorySafeHandle repo, GitAnnotatedCommitHandle[] heads, GitMergeOpts mergeOptions, GitCheckoutOpts checkoutOptions) { - using (ThreadAffinity()) - { - IntPtr[] their_heads = heads.Select(head => head.DangerousGetHandle()).ToArray(); + IntPtr[] their_heads = heads.Select(head => head.DangerousGetHandle()).ToArray(); - int res = NativeMethods.git_merge( - repo, - their_heads, - (UIntPtr)their_heads.Length, - ref mergeOptions, - ref checkoutOptions); + int res = NativeMethods.git_merge( + repo, + their_heads, + (UIntPtr)their_heads.Length, + ref mergeOptions, + ref checkoutOptions); - Ensure.ZeroResult(res); - } + Ensure.ZeroResult(res); } public static void git_merge_analysis( @@ -1340,19 +1172,16 @@ namespace LibGit2Sharp.Core out GitMergeAnalysis analysis_out, out GitMergePreference preference_out) { - using (ThreadAffinity()) - { - IntPtr[] their_heads = heads.Select(head => head.DangerousGetHandle()).ToArray(); + IntPtr[] their_heads = heads.Select(head => head.DangerousGetHandle()).ToArray(); - int res = NativeMethods.git_merge_analysis( - out analysis_out, - out preference_out, - repo, - their_heads, - their_heads.Length); + int res = NativeMethods.git_merge_analysis( + out analysis_out, + out preference_out, + repo, + their_heads, + their_heads.Length); - Ensure.ZeroResult(res); - } + Ensure.ZeroResult(res); } public static void git_annotated_commit_free(IntPtr handle) @@ -1377,7 +1206,6 @@ namespace LibGit2Sharp.Core throw new InvalidOperationException("Only single byte characters are allowed as commentary characters in a message (eg. '#')."); } - using (ThreadAffinity()) using (var buf = new GitBuf()) { int res = NativeMethods.git_message_prettify(buf, message, false, (sbyte)comment); @@ -1400,7 +1228,6 @@ namespace LibGit2Sharp.Core string note, bool force) { - using (ThreadAffinity()) using (SignatureSafeHandle authorHandle = author.BuildHandle()) using (SignatureSafeHandle committerHandle = committer.BuildHandle()) { @@ -1416,7 +1243,6 @@ namespace LibGit2Sharp.Core public static string git_note_default_ref(RepositorySafeHandle repo) { - using (ThreadAffinity()) using (var buf = new GitBuf()) { int res = NativeMethods.git_note_default_ref(buf, repo); @@ -1449,27 +1275,23 @@ namespace LibGit2Sharp.Core public static NoteSafeHandle git_note_read(RepositorySafeHandle repo, string notes_ref, ObjectId id) { - using (ThreadAffinity()) - { - GitOid oid = id.Oid; - NoteSafeHandle note; + GitOid oid = id.Oid; + NoteSafeHandle note; - int res = NativeMethods.git_note_read(out note, repo, notes_ref, ref oid); + int res = NativeMethods.git_note_read(out note, repo, notes_ref, ref oid); - if (res == (int)GitErrorCode.NotFound) - { - return null; - } + if (res == (int)GitErrorCode.NotFound) + { + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return note; - } + return note; } public static void git_note_remove(RepositorySafeHandle repo, string notes_ref, Signature author, Signature committer, ObjectId targetId) { - using (ThreadAffinity()) using (SignatureSafeHandle authorHandle = author.BuildHandle()) using (SignatureSafeHandle committerHandle = committer.BuildHandle()) { @@ -1502,52 +1324,46 @@ namespace LibGit2Sharp.Core public static GitObjectSafeHandle git_object_lookup(RepositorySafeHandle repo, ObjectId id, GitObjectType type) { - using (ThreadAffinity()) - { - GitObjectSafeHandle handle; - GitOid oid = id.Oid; - - int res = NativeMethods.git_object_lookup(out handle, repo, ref oid, type); - switch (res) - { - case (int)GitErrorCode.NotFound: - return null; + GitObjectSafeHandle handle; + GitOid oid = id.Oid; - default: - Ensure.ZeroResult(res); - break; - } + int res = NativeMethods.git_object_lookup(out handle, repo, ref oid, type); + switch (res) + { + case (int)GitErrorCode.NotFound: + return null; - return handle; + default: + Ensure.ZeroResult(res); + break; } + + return handle; } public static GitObjectSafeHandle git_object_peel(RepositorySafeHandle repo, ObjectId id, GitObjectType type, bool throwsIfCanNotPeel) { - using (ThreadAffinity()) - { - GitObjectSafeHandle peeled; - int res; - - using (var obj = new ObjectSafeWrapper(id, repo)) - { - res = NativeMethods.git_object_peel(out peeled, obj.ObjectPtr, type); - } + GitObjectSafeHandle peeled; + int res; - if (!throwsIfCanNotPeel && - (res == (int)GitErrorCode.NotFound || res == (int)GitErrorCode.Ambiguous || res == (int)GitErrorCode.InvalidSpecification || res == (int)GitErrorCode.Peel)) - { - return null; - } + using (var obj = new ObjectSafeWrapper(id, repo)) + { + res = NativeMethods.git_object_peel(out peeled, obj.ObjectPtr, type); + } - Ensure.ZeroResult(res); - return peeled; + if (!throwsIfCanNotPeel && + (res == (int)GitErrorCode.NotFound || res == (int)GitErrorCode.Ambiguous || + res == (int)GitErrorCode.InvalidSpecification || res == (int)GitErrorCode.Peel)) + { + return null; } + + Ensure.ZeroResult(res); + return peeled; } public static string git_object_short_id(RepositorySafeHandle repo, ObjectId id) { - using (ThreadAffinity()) using (var obj = new ObjectSafeWrapper(id, repo)) using (var buf = new GitBuf()) { @@ -1589,30 +1405,24 @@ namespace LibGit2Sharp.Core public static bool git_odb_exists(ObjectDatabaseSafeHandle odb, ObjectId id) { - using (ThreadAffinity()) - { - GitOid oid = id.Oid; + GitOid oid = id.Oid; - int res = NativeMethods.git_odb_exists(odb, ref oid); - Ensure.BooleanResult(res); + int res = NativeMethods.git_odb_exists(odb, ref oid); + Ensure.BooleanResult(res); - return (res == 1); - } + return (res == 1); } public static GitObjectMetadata git_odb_read_header(ObjectDatabaseSafeHandle odb, ObjectId id) { - using (ThreadAffinity()) - { - GitOid oid = id.Oid; - UIntPtr length; - GitObjectType objectType; + GitOid oid = id.Oid; + UIntPtr length; + GitObjectType objectType; - int res = NativeMethods.git_odb_read_header(out length, out objectType, odb, ref oid); - Ensure.ZeroResult(res); + int res = NativeMethods.git_odb_read_header(out length, out objectType, odb, ref oid); + Ensure.ZeroResult(res); - return new GitObjectMetadata((long)length, objectType); - } + return new GitObjectMetadata((long)length, objectType); } public static ICollection<TResult> git_odb_foreach<TResult>( @@ -1629,14 +1439,11 @@ namespace LibGit2Sharp.Core public static OdbStreamSafeHandle git_odb_open_wstream(ObjectDatabaseSafeHandle odb, long size, GitObjectType type) { - using (ThreadAffinity()) - { - OdbStreamSafeHandle stream; - int res = NativeMethods.git_odb_open_wstream(out stream, odb, size, type); - Ensure.ZeroResult(res); + OdbStreamSafeHandle stream; + int res = NativeMethods.git_odb_open_wstream(out stream, odb, size, type); + Ensure.ZeroResult(res); - return stream; - } + return stream; } public static void git_odb_free(IntPtr odb) @@ -1646,31 +1453,25 @@ namespace LibGit2Sharp.Core public static void git_odb_stream_write(OdbStreamSafeHandle stream, byte[] data, int len) { - using (ThreadAffinity()) + int res; + unsafe { - int res; - unsafe + fixed (byte* p = data) { - fixed (byte *p = data) - { - res = NativeMethods.git_odb_stream_write(stream, (IntPtr) p, (UIntPtr) len); - } + res = NativeMethods.git_odb_stream_write(stream, (IntPtr)p, (UIntPtr)len); } - - Ensure.ZeroResult(res); } + + Ensure.ZeroResult(res); } public static ObjectId git_odb_stream_finalize_write(OdbStreamSafeHandle stream) { - using (ThreadAffinity()) - { - GitOid id; - int res = NativeMethods.git_odb_stream_finalize_write(out id, stream); - Ensure.ZeroResult(res); + GitOid id; + int res = NativeMethods.git_odb_stream_finalize_write(out id, stream); + Ensure.ZeroResult(res); - return id; - } + return id; } public static void git_odb_stream_free(IntPtr stream) @@ -1689,33 +1490,24 @@ namespace LibGit2Sharp.Core public static PatchSafeHandle git_patch_from_diff(DiffSafeHandle diff, int idx) { - using (ThreadAffinity()) - { - PatchSafeHandle handle; - int res = NativeMethods.git_patch_from_diff(out handle, diff, (UIntPtr) idx); - Ensure.ZeroResult(res); - return handle; - } + PatchSafeHandle handle; + int res = NativeMethods.git_patch_from_diff(out handle, diff, (UIntPtr)idx); + Ensure.ZeroResult(res); + return handle; } public static void git_patch_print(PatchSafeHandle patch, NativeMethods.git_diff_line_cb printCallback) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_patch_print(patch, printCallback, IntPtr.Zero); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_patch_print(patch, printCallback, IntPtr.Zero); + Ensure.ZeroResult(res); } public static Tuple<int, int> git_patch_line_stats(PatchSafeHandle patch) { - using (ThreadAffinity()) - { - UIntPtr ctx, add, del; - int res = NativeMethods.git_patch_line_stats(out ctx, out add, out del, patch); - Ensure.ZeroResult(res); - return new Tuple<int, int>((int)add, (int)del); - } + UIntPtr ctx, add, del; + int res = NativeMethods.git_patch_line_stats(out ctx, out add, out del, patch); + Ensure.ZeroResult(res); + return new Tuple<int, int>((int)add, (int)del); } #endregion @@ -1725,29 +1517,24 @@ namespace LibGit2Sharp.Core public static ReferenceSafeHandle git_reference_create(RepositorySafeHandle repo, string name, ObjectId targetId, bool allowOverwrite, string logMessage) { - using (ThreadAffinity()) - { - GitOid oid = targetId.Oid; - ReferenceSafeHandle handle; + GitOid oid = targetId.Oid; + ReferenceSafeHandle handle; - int res = NativeMethods.git_reference_create(out handle, repo, name, ref oid, allowOverwrite, logMessage); - Ensure.ZeroResult(res); + int res = NativeMethods.git_reference_create(out handle, repo, name, ref oid, allowOverwrite, logMessage); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static ReferenceSafeHandle git_reference_symbolic_create(RepositorySafeHandle repo, string name, string target, bool allowOverwrite, string logMessage) { - using (ThreadAffinity()) - { - ReferenceSafeHandle handle; - int res = NativeMethods.git_reference_symbolic_create(out handle, repo, name, target, allowOverwrite, logMessage); - Ensure.ZeroResult(res); + ReferenceSafeHandle handle; + int res = NativeMethods.git_reference_symbolic_create(out handle, repo, name, target, allowOverwrite, + logMessage); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static ICollection<TResult> git_reference_foreach_glob<TResult>( @@ -1773,40 +1560,34 @@ namespace LibGit2Sharp.Core public static IList<string> git_reference_list(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - var array = new GitStrArrayNative(); + var array = new GitStrArrayNative(); - try - { - int res = NativeMethods.git_reference_list(out array.Array, repo); - Ensure.ZeroResult(res); + try + { + int res = NativeMethods.git_reference_list(out array.Array, repo); + Ensure.ZeroResult(res); - return array.ReadStrings(); - } - finally - { - array.Dispose(); - } + return array.ReadStrings(); + } + finally + { + array.Dispose(); } } public static ReferenceSafeHandle git_reference_lookup(RepositorySafeHandle repo, string name, bool shouldThrowIfNotFound) { - using (ThreadAffinity()) - { - ReferenceSafeHandle handle; - int res = NativeMethods.git_reference_lookup(out handle, repo, name); + ReferenceSafeHandle handle; + int res = NativeMethods.git_reference_lookup(out handle, repo, name); - if (!shouldThrowIfNotFound && res == (int)GitErrorCode.NotFound) - { - return null; - } + if (!shouldThrowIfNotFound && res == (int)GitErrorCode.NotFound) + { + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static string git_reference_name(ReferenceSafeHandle reference) @@ -1816,11 +1597,8 @@ namespace LibGit2Sharp.Core public static void git_reference_remove(RepositorySafeHandle repo, string name) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_reference_remove(repo, name); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_reference_remove(repo, name); + Ensure.ZeroResult(res); } public static ObjectId git_reference_target(ReferenceSafeHandle reference) @@ -1831,42 +1609,33 @@ namespace LibGit2Sharp.Core public static ReferenceSafeHandle git_reference_rename(ReferenceSafeHandle reference, string newName, bool allowOverwrite, string logMessage) { - using (ThreadAffinity()) - { - ReferenceSafeHandle ref_out; + ReferenceSafeHandle ref_out; - int res = NativeMethods.git_reference_rename(out ref_out, reference, newName, allowOverwrite, logMessage); - Ensure.ZeroResult(res); + int res = NativeMethods.git_reference_rename(out ref_out, reference, newName, allowOverwrite, logMessage); + Ensure.ZeroResult(res); - return ref_out; - } + return ref_out; } public static ReferenceSafeHandle git_reference_set_target(ReferenceSafeHandle reference, ObjectId id, string logMessage) { - using (ThreadAffinity()) - { - GitOid oid = id.Oid; - ReferenceSafeHandle ref_out; + GitOid oid = id.Oid; + ReferenceSafeHandle ref_out; - int res = NativeMethods.git_reference_set_target(out ref_out, reference, ref oid, logMessage); - Ensure.ZeroResult(res); + int res = NativeMethods.git_reference_set_target(out ref_out, reference, ref oid, logMessage); + Ensure.ZeroResult(res); - return ref_out; - } + return ref_out; } public static ReferenceSafeHandle git_reference_symbolic_set_target(ReferenceSafeHandle reference, string target, string logMessage) { - using (ThreadAffinity()) - { - ReferenceSafeHandle ref_out; + ReferenceSafeHandle ref_out; - int res = NativeMethods.git_reference_symbolic_set_target(out ref_out, reference, target, logMessage); - Ensure.ZeroResult(res); + int res = NativeMethods.git_reference_symbolic_set_target(out ref_out, reference, target, logMessage); + Ensure.ZeroResult(res); - return ref_out; - } + return ref_out; } public static string git_reference_symbolic_target(ReferenceSafeHandle reference) @@ -1881,11 +1650,8 @@ namespace LibGit2Sharp.Core public static void git_reference_ensure_log(RepositorySafeHandle repo, string refname) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_reference_ensure_log(repo, refname); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_reference_ensure_log(repo, refname); + Ensure.ZeroResult(res); } #endregion @@ -1899,15 +1665,12 @@ namespace LibGit2Sharp.Core public static ReflogSafeHandle git_reflog_read(RepositorySafeHandle repo, string canonicalName) { - using (ThreadAffinity()) - { - ReflogSafeHandle reflog_out; + ReflogSafeHandle reflog_out; - int res = NativeMethods.git_reflog_read(out reflog_out, repo, canonicalName); - Ensure.ZeroResult(res); + int res = NativeMethods.git_reflog_read(out reflog_out, repo, canonicalName); + Ensure.ZeroResult(res); - return reflog_out; - } + return reflog_out; } public static int git_reflog_entrycount(ReflogSafeHandle reflog) @@ -1946,7 +1709,6 @@ namespace LibGit2Sharp.Core public static string git_refspec_rtransform(GitRefSpecHandle refSpecPtr, string name) { - using (ThreadAffinity()) using (var buf = new GitBuf()) { int res = NativeMethods.git_refspec_rtransform(buf, refSpecPtr, name); @@ -1992,62 +1754,47 @@ namespace LibGit2Sharp.Core public static RemoteSafeHandle git_remote_create(RepositorySafeHandle repo, string name, string url) { - using (ThreadAffinity()) - { - RemoteSafeHandle handle; - int res = NativeMethods.git_remote_create(out handle, repo, name, url); - Ensure.ZeroResult(res); + RemoteSafeHandle handle; + int res = NativeMethods.git_remote_create(out handle, repo, name, url); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static RemoteSafeHandle git_remote_create_with_fetchspec(RepositorySafeHandle repo, string name, string url, string refspec) { - using (ThreadAffinity()) - { - RemoteSafeHandle handle; - int res = NativeMethods.git_remote_create_with_fetchspec(out handle, repo, name, url, refspec); - Ensure.ZeroResult(res); + RemoteSafeHandle handle; + int res = NativeMethods.git_remote_create_with_fetchspec(out handle, repo, name, url, refspec); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static RemoteSafeHandle git_remote_create_anonymous(RepositorySafeHandle repo, string url) { - using (ThreadAffinity()) - { - RemoteSafeHandle handle; - int res = NativeMethods.git_remote_create_anonymous(out handle, repo, url); - Ensure.ZeroResult(res); + RemoteSafeHandle handle; + int res = NativeMethods.git_remote_create_anonymous(out handle, repo, url); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static void git_remote_connect(RemoteSafeHandle remote, GitDirection direction, ref GitRemoteCallbacks remoteCallbacks) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_remote_connect(remote, direction, ref remoteCallbacks); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_remote_connect(remote, direction, ref remoteCallbacks); + Ensure.ZeroResult(res); } public static void git_remote_delete(RepositorySafeHandle repo, string name) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_remote_delete(repo, name); + int res = NativeMethods.git_remote_delete(repo, name); - if (res == (int)GitErrorCode.NotFound) - { - return; - } - - Ensure.ZeroResult(res); + if (res == (int)GitErrorCode.NotFound) + { + return; } + + Ensure.ZeroResult(res); } public static GitRefSpecHandle git_remote_get_refspec(RemoteSafeHandle remote, int n) @@ -2062,119 +1809,95 @@ namespace LibGit2Sharp.Core public static IList<string> git_remote_get_fetch_refspecs(RemoteSafeHandle remote) { - using (ThreadAffinity()) - { - var array = new GitStrArrayNative(); + var array = new GitStrArrayNative(); - try - { - int res = NativeMethods.git_remote_get_fetch_refspecs(out array.Array, remote); - Ensure.ZeroResult(res); + try + { + int res = NativeMethods.git_remote_get_fetch_refspecs(out array.Array, remote); + Ensure.ZeroResult(res); - return array.ReadStrings(); - } - finally - { - array.Dispose(); - } + return array.ReadStrings(); + } + finally + { + array.Dispose(); } } public static IList<string> git_remote_get_push_refspecs(RemoteSafeHandle remote) { - using (ThreadAffinity()) - { - var array = new GitStrArrayNative(); + var array = new GitStrArrayNative(); - try - { - int res = NativeMethods.git_remote_get_push_refspecs(out array.Array, remote); - Ensure.ZeroResult(res); + try + { + int res = NativeMethods.git_remote_get_push_refspecs(out array.Array, remote); + Ensure.ZeroResult(res); - return array.ReadStrings(); - } - finally - { - array.Dispose(); - } + return array.ReadStrings(); + } + finally + { + array.Dispose(); } } public static void git_remote_push(RemoteSafeHandle remote, IEnumerable<string> refSpecs, GitPushOptions opts) { - using (ThreadAffinity()) - { - var array = new GitStrArrayManaged(); + var array = new GitStrArrayManaged(); - try - { - array = GitStrArrayManaged.BuildFrom(refSpecs.ToArray()); + try + { + array = GitStrArrayManaged.BuildFrom(refSpecs.ToArray()); - int res = NativeMethods.git_remote_push(remote, ref array.Array, opts); - Ensure.ZeroResult(res); - } - finally - { - array.Dispose(); - } + int res = NativeMethods.git_remote_push(remote, ref array.Array, opts); + Ensure.ZeroResult(res); + } + finally + { + array.Dispose(); } } public static void git_remote_set_url(RepositorySafeHandle repo, string remote, string url) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_remote_set_url(repo, remote, url); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_remote_set_url(repo, remote, url); + Ensure.ZeroResult(res); } public static void git_remote_add_fetch(RepositorySafeHandle repo, string remote, string url) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_remote_add_fetch(repo, remote, url); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_remote_add_fetch(repo, remote, url); + Ensure.ZeroResult(res); } public static void git_remote_set_pushurl(RepositorySafeHandle repo, string remote, string url) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_remote_set_pushurl(repo, remote, url); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_remote_set_pushurl(repo, remote, url); + Ensure.ZeroResult(res); } public static void git_remote_add_push(RepositorySafeHandle repo, string remote, string url) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_remote_add_push(repo, remote, url); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_remote_add_push(repo, remote, url); + Ensure.ZeroResult(res); } public static void git_remote_fetch( RemoteSafeHandle remote, IEnumerable<string> refSpecs, GitFetchOptions fetchOptions, string logMessage) { - using (ThreadAffinity()) - { - var array = new GitStrArrayManaged(); + var array = new GitStrArrayManaged(); - try - { - array = GitStrArrayManaged.BuildFrom(refSpecs.ToArray()); + try + { + array = GitStrArrayManaged.BuildFrom(refSpecs.ToArray()); - int res = NativeMethods.git_remote_fetch(remote, ref array.Array, fetchOptions, logMessage); - Ensure.ZeroResult(res); - } - finally - { - array.Dispose(); - } + int res = NativeMethods.git_remote_fetch(remote, ref array.Array, fetchOptions, logMessage); + Ensure.ZeroResult(res); + } + finally + { + array.Dispose(); } } @@ -2193,21 +1916,18 @@ namespace LibGit2Sharp.Core public static IList<string> git_remote_list(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - var array = new GitStrArrayNative(); + var array = new GitStrArrayNative(); - try - { - int res = NativeMethods.git_remote_list(out array.Array, repo); - Ensure.ZeroResult(res); + try + { + int res = NativeMethods.git_remote_list(out array.Array, repo); + Ensure.ZeroResult(res); - return array.ReadStrings(); - } - finally - { - array.Dispose(); - } + return array.ReadStrings(); + } + finally + { + array.Dispose(); } } @@ -2216,11 +1936,8 @@ namespace LibGit2Sharp.Core IntPtr heads; UIntPtr count; - using (ThreadAffinity()) - { - int res = NativeMethods.git_remote_ls(out heads, out count, remote); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_remote_ls(out heads, out count, remote); + Ensure.ZeroResult(res); var intCount = (int)count.ToUInt32(); @@ -2254,19 +1971,16 @@ namespace LibGit2Sharp.Core public static RemoteSafeHandle git_remote_lookup(RepositorySafeHandle repo, string name, bool throwsIfNotFound) { - using (ThreadAffinity()) - { - RemoteSafeHandle handle; - int res = NativeMethods.git_remote_lookup(out handle, repo, name); + RemoteSafeHandle handle; + int res = NativeMethods.git_remote_lookup(out handle, repo, name); - if (res == (int)GitErrorCode.NotFound && !throwsIfNotFound) - { - return null; - } - - Ensure.ZeroResult(res); - return handle; + if (res == (int)GitErrorCode.NotFound && !throwsIfNotFound) + { + return null; } + + Ensure.ZeroResult(res); + return handle; } public static string git_remote_name(RemoteSafeHandle remote) @@ -2276,41 +1990,38 @@ namespace LibGit2Sharp.Core public static void git_remote_rename(RepositorySafeHandle repo, string name, string new_name, RemoteRenameFailureHandler callback) { - using (ThreadAffinity()) + if (callback == null) { - if (callback == null) - { - callback = problem => {}; - } + callback = problem => { }; + } - var array = new GitStrArrayNative(); + var array = new GitStrArrayNative(); - try - { - int res = NativeMethods.git_remote_rename( - ref array.Array, - repo, - name, - new_name); + try + { + int res = NativeMethods.git_remote_rename( + ref array.Array, + repo, + name, + new_name); - if (res == (int)GitErrorCode.NotFound) - { - throw new NotFoundException( - string.Format("Remote '{0}' does not exist and cannot be renamed.", name)); - } + if (res == (int)GitErrorCode.NotFound) + { + throw new NotFoundException( + string.Format("Remote '{0}' does not exist and cannot be renamed.", name)); + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - foreach (var item in array.ReadStrings()) - { - callback(item); - } - } - finally + foreach (var item in array.ReadStrings()) { - array.Dispose(); + callback(item); } } + finally + { + array.Dispose(); + } } public static void git_remote_set_autotag(RepositorySafeHandle repo, string remote, TagFetchMode value) @@ -2367,14 +2078,11 @@ namespace LibGit2Sharp.Core public static IndexSafeHandle git_repository_index(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - IndexSafeHandle handle; - int res = NativeMethods.git_repository_index(out handle, repo); - Ensure.ZeroResult(res); + IndexSafeHandle handle; + int res = NativeMethods.git_repository_index(out handle, repo); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static RepositorySafeHandle git_repository_init_ext( @@ -2382,7 +2090,6 @@ namespace LibGit2Sharp.Core FilePath gitdirPath, bool isBare) { - using (ThreadAffinity()) using (var opts = GitRepositoryInitOptions.BuildFrom(workdirPath, isBare)) { RepositorySafeHandle repo; @@ -2405,11 +2112,8 @@ namespace LibGit2Sharp.Core public static void git_repository_state_cleanup(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_repository_state_cleanup(repo); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_repository_state_cleanup(repo); + Ensure.ZeroResult(res); } public static ICollection<TResult> git_repository_mergehead_foreach<TResult>( @@ -2425,7 +2129,6 @@ namespace LibGit2Sharp.Core public static string git_repository_message(RepositorySafeHandle repo) { - using (ThreadAffinity()) using (var buf = new GitBuf()) { int res = NativeMethods.git_repository_message(buf, repo); @@ -2441,52 +2144,45 @@ namespace LibGit2Sharp.Core public static ObjectDatabaseSafeHandle git_repository_odb(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - ObjectDatabaseSafeHandle handle; - int res = NativeMethods.git_repository_odb(out handle, repo); - Ensure.ZeroResult(res); + ObjectDatabaseSafeHandle handle; + int res = NativeMethods.git_repository_odb(out handle, repo); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static RepositorySafeHandle git_repository_open(string path) { - using (ThreadAffinity()) - { - RepositorySafeHandle repo; - int res = NativeMethods.git_repository_open(out repo, path); + RepositorySafeHandle repo; + int res = NativeMethods.git_repository_open(out repo, path); - if (res == (int)GitErrorCode.NotFound) - { - throw new RepositoryNotFoundException(String.Format(CultureInfo.InvariantCulture, "Path '{0}' doesn't point at a valid Git repository or workdir.", path)); - } + if (res == (int)GitErrorCode.NotFound) + { + throw new RepositoryNotFoundException(String.Format(CultureInfo.InvariantCulture, + "Path '{0}' doesn't point at a valid Git repository or workdir.", path)); + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return repo; - } + return repo; } public static void git_repository_open_ext(string path, RepositoryOpenFlags flags, string ceilingDirs) { - using (ThreadAffinity()) - { - int res; + int res; - using (var repo = new NullRepositorySafeHandle()) - { - res = NativeMethods.git_repository_open_ext(repo, path, flags, ceilingDirs); - } - - if (res == (int)GitErrorCode.NotFound) - { - throw new RepositoryNotFoundException(String.Format(CultureInfo.InvariantCulture, "Path '{0}' doesn't point at a valid Git repository or workdir.", path)); - } + using (var repo = new NullRepositorySafeHandle()) + { + res = NativeMethods.git_repository_open_ext(repo, path, flags, ceilingDirs); + } - Ensure.ZeroResult(res); + if (res == (int)GitErrorCode.NotFound) + { + throw new RepositoryNotFoundException(String.Format(CultureInfo.InvariantCulture, + "Path '{0}' doesn't point at a valid Git repository or workdir.", path)); } + + Ensure.ZeroResult(res); } public static FilePath git_repository_path(RepositorySafeHandle repo) @@ -2501,11 +2197,8 @@ namespace LibGit2Sharp.Core public static void git_repository_set_ident(RepositorySafeHandle repo, string name, string email) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_repository_set_ident(repo, name, email); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_repository_set_ident(repo, name, email); + Ensure.ZeroResult(res); } public static void git_repository_set_index(RepositorySafeHandle repo, IndexSafeHandle index) @@ -2515,21 +2208,15 @@ namespace LibGit2Sharp.Core public static void git_repository_set_workdir(RepositorySafeHandle repo, FilePath workdir) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_repository_set_workdir(repo, workdir, false); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_repository_set_workdir(repo, workdir, false); + Ensure.ZeroResult(res); } public static CurrentOperation git_repository_state(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_repository_state(repo); - Ensure.Int32Result(res); - return (CurrentOperation)res; - } + int res = NativeMethods.git_repository_state(repo); + Ensure.Int32Result(res); + return (CurrentOperation)res; } public static FilePath git_repository_workdir(RepositorySafeHandle repo) @@ -2539,30 +2226,21 @@ namespace LibGit2Sharp.Core public static void git_repository_set_head_detached(RepositorySafeHandle repo, ObjectId commitish) { - using (ThreadAffinity()) - { - GitOid oid = commitish.Oid; - int res = NativeMethods.git_repository_set_head_detached(repo, ref oid); - Ensure.ZeroResult(res); - } + GitOid oid = commitish.Oid; + int res = NativeMethods.git_repository_set_head_detached(repo, ref oid); + Ensure.ZeroResult(res); } public static void git_repository_set_head_detached_from_annotated(RepositorySafeHandle repo, GitAnnotatedCommitHandle commit) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_repository_set_head_detached_from_annotated(repo, commit); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_repository_set_head_detached_from_annotated(repo, commit); + Ensure.ZeroResult(res); } public static void git_repository_set_head(RepositorySafeHandle repo, string refname) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_repository_set_head(repo, refname); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_repository_set_head(repo, refname); + Ensure.ZeroResult(res); } #endregion @@ -2575,7 +2253,6 @@ namespace LibGit2Sharp.Core ResetMode resetKind, ref GitCheckoutOpts checkoutOptions) { - using (ThreadAffinity()) using (var osw = new ObjectSafeWrapper(committishId, repo)) { int res = NativeMethods.git_reset(repo, osw.ObjectPtr, resetKind, ref checkoutOptions); @@ -2592,7 +2269,6 @@ namespace LibGit2Sharp.Core ObjectId commit, GitRevertOpts opts) { - using (ThreadAffinity()) using (var nativeCommit = git_object_lookup(repo, commit, GitObjectType.Commit)) { int res = NativeMethods.git_revert(repo, nativeCommit, opts); @@ -2606,27 +2282,25 @@ namespace LibGit2Sharp.Core public static Tuple<GitObjectSafeHandle, ReferenceSafeHandle> git_revparse_ext(RepositorySafeHandle repo, string objectish) { - using (ThreadAffinity()) - { - GitObjectSafeHandle obj; - ReferenceSafeHandle reference; - int res = NativeMethods.git_revparse_ext(out obj, out reference, repo, objectish); + GitObjectSafeHandle obj; + ReferenceSafeHandle reference; + int res = NativeMethods.git_revparse_ext(out obj, out reference, repo, objectish); - switch (res) - { - case (int)GitErrorCode.NotFound: - return null; - - case (int)GitErrorCode.Ambiguous: - throw new AmbiguousSpecificationException(string.Format(CultureInfo.InvariantCulture, "Provided abbreviated ObjectId '{0}' is too short.", objectish)); + switch (res) + { + case (int)GitErrorCode.NotFound: + return null; - default: - Ensure.ZeroResult(res); - break; - } + case (int)GitErrorCode.Ambiguous: + throw new AmbiguousSpecificationException(string.Format(CultureInfo.InvariantCulture, + "Provided abbreviated ObjectId '{0}' is too short.", objectish)); - return new Tuple<GitObjectSafeHandle, ReferenceSafeHandle>(obj, reference); + default: + Ensure.ZeroResult(res); + break; } + + return new Tuple<GitObjectSafeHandle, ReferenceSafeHandle>(obj, reference); } public static GitObjectSafeHandle git_revparse_single(RepositorySafeHandle repo, string objectish) @@ -2654,52 +2328,40 @@ namespace LibGit2Sharp.Core public static void git_revwalk_hide(RevWalkerSafeHandle walker, ObjectId commit_id) { - using (ThreadAffinity()) - { - GitOid oid = commit_id.Oid; - int res = NativeMethods.git_revwalk_hide(walker, ref oid); - Ensure.ZeroResult(res); - } + GitOid oid = commit_id.Oid; + int res = NativeMethods.git_revwalk_hide(walker, ref oid); + Ensure.ZeroResult(res); } public static RevWalkerSafeHandle git_revwalk_new(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - RevWalkerSafeHandle handle; - int res = NativeMethods.git_revwalk_new(out handle, repo); - Ensure.ZeroResult(res); + RevWalkerSafeHandle handle; + int res = NativeMethods.git_revwalk_new(out handle, repo); + Ensure.ZeroResult(res); - return handle; - } + return handle; } public static ObjectId git_revwalk_next(RevWalkerSafeHandle walker) { - using (ThreadAffinity()) - { - GitOid ret; - int res = NativeMethods.git_revwalk_next(out ret, walker); + GitOid ret; + int res = NativeMethods.git_revwalk_next(out ret, walker); - if (res == (int)GitErrorCode.IterOver) - { - return null; - } + if (res == (int)GitErrorCode.IterOver) + { + return null; + } - Ensure.ZeroResult(res); + Ensure.ZeroResult(res); - return ret; - } + return ret; } public static void git_revwalk_push(RevWalkerSafeHandle walker, ObjectId id) { - using (ThreadAffinity()) - { - GitOid oid = id.Oid; - int res = NativeMethods.git_revwalk_push(walker, ref oid); - Ensure.ZeroResult(res); - } + GitOid oid = id.Oid; + int res = NativeMethods.git_revwalk_push(walker, ref oid); + Ensure.ZeroResult(res); } public static void git_revwalk_reset(RevWalkerSafeHandle walker) @@ -2728,26 +2390,22 @@ namespace LibGit2Sharp.Core public static SignatureSafeHandle git_signature_new(string name, string email, DateTimeOffset when) { - using (ThreadAffinity()) - { - SignatureSafeHandle handle; - int res = NativeMethods.git_signature_new(out handle, name, email, when.ToSecondsSinceEpoch(), - (int)when.Offset.TotalMinutes); - Ensure.ZeroResult(res); + SignatureSafeHandle handle; - return handle; - } + int res = NativeMethods.git_signature_new(out handle, name, email, when.ToSecondsSinceEpoch(), + (int)when.Offset.TotalMinutes); + + Ensure.ZeroResult(res); + + return handle; } public static IntPtr git_signature_dup(IntPtr sig) { - using (ThreadAffinity()) - { - IntPtr handle; - int res = NativeMethods.git_signature_dup(out handle, sig); - Ensure.ZeroResult(res); - return handle; - } + IntPtr handle; + int res = NativeMethods.git_signature_dup(out handle, sig); + Ensure.ZeroResult(res); + return handle; } #endregion @@ -2760,7 +2418,6 @@ namespace LibGit2Sharp.Core string prettifiedMessage, StashModifiers options) { - using (ThreadAffinity()) using (SignatureSafeHandle sigHandle = stasher.BuildHandle()) { GitOid stashOid; @@ -2791,11 +2448,8 @@ namespace LibGit2Sharp.Core public static void git_stash_drop(RepositorySafeHandle repo, int index) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_stash_drop(repo, (UIntPtr) index); - Ensure.BooleanResult(res); - } + int res = NativeMethods.git_stash_drop(repo, (UIntPtr)index); + Ensure.BooleanResult(res); } #endregion @@ -2804,47 +2458,41 @@ namespace LibGit2Sharp.Core public static FileStatus git_status_file(RepositorySafeHandle repo, FilePath path) { - using (ThreadAffinity()) - { - FileStatus status; - int res = NativeMethods.git_status_file(out status, repo, path); - - switch (res) - { - case (int)GitErrorCode.NotFound: - return FileStatus.Nonexistent; + FileStatus status; + int res = NativeMethods.git_status_file(out status, repo, path); - case (int)GitErrorCode.Ambiguous: - throw new AmbiguousSpecificationException(string.Format(CultureInfo.InvariantCulture, "More than one file matches the pathspec '{0}'. You can either force a literal path evaluation (GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH), or use git_status_foreach().", path)); + switch (res) + { + case (int)GitErrorCode.NotFound: + return FileStatus.Nonexistent; - default: - Ensure.ZeroResult(res); - break; - } + case (int)GitErrorCode.Ambiguous: + throw new AmbiguousSpecificationException(string.Format(CultureInfo.InvariantCulture, + "More than one file matches the pathspec '{0}'. " + + "You can either force a literal path evaluation (GIT_STATUS_OPT_DISABLE_PATHSPEC_MATCH), or use git_status_foreach().", + path)); - return status; + default: + Ensure.ZeroResult(res); + break; } + + return status; } public static StatusListSafeHandle git_status_list_new(RepositorySafeHandle repo, GitStatusOptions options) { - using (ThreadAffinity()) - { - StatusListSafeHandle handle; - int res = NativeMethods.git_status_list_new(out handle, repo, options); - Ensure.ZeroResult(res); - return handle; - } + StatusListSafeHandle handle; + int res = NativeMethods.git_status_list_new(out handle, repo, options); + Ensure.ZeroResult(res); + return handle; } public static int git_status_list_entrycount(StatusListSafeHandle list) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_status_list_entrycount(list); - Ensure.Int32Result(res); - return res; - } + int res = NativeMethods.git_status_list_entrycount(list); + Ensure.Int32Result(res); + return res; } public static StatusEntrySafeHandle git_status_byindex(StatusListSafeHandle list, long idx) @@ -2867,28 +2515,24 @@ namespace LibGit2Sharp.Core /// </summary> public static SubmoduleSafeHandle git_submodule_lookup(RepositorySafeHandle repo, FilePath name) { - using (ThreadAffinity()) - { - SubmoduleSafeHandle reference; - var res = NativeMethods.git_submodule_lookup(out reference, repo, name); + SubmoduleSafeHandle reference; + var res = NativeMethods.git_submodule_lookup(out reference, repo, name); - switch (res) - { - case (int)GitErrorCode.NotFound: - case (int)GitErrorCode.Exists: - case (int)GitErrorCode.OrphanedHead: - return null; + switch (res) + { + case (int)GitErrorCode.NotFound: + case (int)GitErrorCode.Exists: + case (int)GitErrorCode.OrphanedHead: + return null; - default: - Ensure.ZeroResult(res); - return reference; - } + default: + Ensure.ZeroResult(res); + return reference; } } public static string git_submodule_resolve_url(RepositorySafeHandle repo, string url) { - using (ThreadAffinity()) using (var buf = new GitBuf()) { int res = NativeMethods.git_submodule_resolve_url(buf, repo, url); @@ -2905,29 +2549,20 @@ namespace LibGit2Sharp.Core public static void git_submodule_add_to_index(SubmoduleSafeHandle submodule, bool write_index) { - using (ThreadAffinity()) - { - var res = NativeMethods.git_submodule_add_to_index(submodule, write_index); - Ensure.ZeroResult(res); - } + var res = NativeMethods.git_submodule_add_to_index(submodule, write_index); + Ensure.ZeroResult(res); } public static void git_submodule_save(SubmoduleSafeHandle submodule) { - using (ThreadAffinity()) - { - var res = NativeMethods.git_submodule_save(submodule); - Ensure.ZeroResult(res); - } + var res = NativeMethods.git_submodule_save(submodule); + Ensure.ZeroResult(res); } public static void git_submodule_update(SubmoduleSafeHandle submodule, bool init, ref GitSubmoduleOptions options) { - using (ThreadAffinity()) - { - var res = NativeMethods.git_submodule_update(submodule, init, ref options); - Ensure.ZeroResult(res); - } + var res = NativeMethods.git_submodule_update(submodule, init, ref options); + Ensure.ZeroResult(res); } public static void git_submodule_free(IntPtr submodule) @@ -2977,31 +2612,22 @@ namespace LibGit2Sharp.Core public static void git_submodule_reload(SubmoduleSafeHandle submodule) { - using (ThreadAffinity()) - { - var res = NativeMethods.git_submodule_reload(submodule, false); - Ensure.ZeroResult(res); - } + var res = NativeMethods.git_submodule_reload(submodule, false); + Ensure.ZeroResult(res); } public static SubmoduleStatus git_submodule_status(SubmoduleSafeHandle submodule) { - using (ThreadAffinity()) - { - SubmoduleStatus status; - var res = NativeMethods.git_submodule_status(out status, submodule); - Ensure.ZeroResult(res); - return status; - } + SubmoduleStatus status; + var res = NativeMethods.git_submodule_status(out status, submodule); + Ensure.ZeroResult(res); + return status; } public static void git_submodule_init(SubmoduleSafeHandle submodule, bool overwrite) { - using (ThreadAffinity()) - { - var res = NativeMethods.git_submodule_init(submodule, overwrite); - Ensure.ZeroResult(res); - } + var res = NativeMethods.git_submodule_init(submodule, overwrite); + Ensure.ZeroResult(res); } #endregion @@ -3015,7 +2641,6 @@ namespace LibGit2Sharp.Core Signature tagger, string message) { - using (ThreadAffinity()) using (var objectPtr = new ObjectSafeWrapper(target.Id, repo)) using (SignatureSafeHandle sigHandle = tagger.BuildHandle()) { @@ -3035,7 +2660,6 @@ namespace LibGit2Sharp.Core string message, bool allowOverwrite) { - using (ThreadAffinity()) using (var objectPtr = new ObjectSafeWrapper(target.Id, repo)) using (SignatureSafeHandle sigHandle = tagger.BuildHandle()) { @@ -3049,7 +2673,6 @@ namespace LibGit2Sharp.Core public static ObjectId git_tag_create_lightweight(RepositorySafeHandle repo, string name, GitObject target, bool allowOverwrite) { - using (ThreadAffinity()) using (var objectPtr = new ObjectSafeWrapper(target.Id, repo)) { GitOid oid; @@ -3062,30 +2685,24 @@ namespace LibGit2Sharp.Core public static void git_tag_delete(RepositorySafeHandle repo, string name) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_tag_delete(repo, name); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_tag_delete(repo, name); + Ensure.ZeroResult(res); } public static IList<string> git_tag_list(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - var array = new GitStrArrayNative(); + var array = new GitStrArrayNative(); - try - { - int res = NativeMethods.git_tag_list(out array.Array, repo); - Ensure.ZeroResult(res); + try + { + int res = NativeMethods.git_tag_list(out array.Array, repo); + Ensure.ZeroResult(res); - return array.ReadStrings(); - } - finally - { - array.Dispose(); - } + return array.ReadStrings(); + } + finally + { + array.Dispose(); } } @@ -3142,11 +2759,8 @@ namespace LibGit2Sharp.Core /// </summary> public static void git_trace_set(LogLevel level, NativeMethods.git_trace_cb callback) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_trace_set(level, callback); - Ensure.ZeroResult(res); - } + int res = NativeMethods.git_trace_set(level, callback); + Ensure.ZeroResult(res); } #endregion @@ -3155,32 +2769,27 @@ namespace LibGit2Sharp.Core public static void git_transport_register(String prefix, IntPtr transport_cb, IntPtr param) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_transport_register(prefix, transport_cb, param); + int res = NativeMethods.git_transport_register(prefix, transport_cb, param); - if (res == (int)GitErrorCode.Exists) - { - throw new EntryExistsException(String.Format("A custom transport for '{0}' is already registered", prefix)); - } - - Ensure.ZeroResult(res); + if (res == (int)GitErrorCode.Exists) + { + throw new EntryExistsException(String.Format("A custom transport for '{0}' is already registered", + prefix)); } + + Ensure.ZeroResult(res); } public static void git_transport_unregister(String prefix) { - using (ThreadAffinity()) - { - int res = NativeMethods.git_transport_unregister(prefix); + int res = NativeMethods.git_transport_unregister(prefix); - if (res == (int)GitErrorCode.NotFound) - { - throw new NotFoundException("The given transport was not found"); - } - - Ensure.ZeroResult(res); + if (res == (int)GitErrorCode.NotFound) + { + throw new NotFoundException("The given transport was not found"); } + + Ensure.ZeroResult(res); } #endregion @@ -3199,7 +2808,6 @@ namespace LibGit2Sharp.Core public static TreeEntrySafeHandle_Owned git_tree_entry_bypath(RepositorySafeHandle repo, ObjectId id, FilePath treeentry_path) { - using (ThreadAffinity()) using (var obj = new ObjectSafeWrapper(id, repo)) { TreeEntrySafeHandle_Owned treeEntryPtr; @@ -3247,14 +2855,11 @@ namespace LibGit2Sharp.Core public static TreeBuilderSafeHandle git_treebuilder_new(RepositorySafeHandle repo) { - using (ThreadAffinity()) - { - TreeBuilderSafeHandle builder; - int res = NativeMethods.git_treebuilder_new(out builder, repo, IntPtr.Zero); - Ensure.ZeroResult(res); + TreeBuilderSafeHandle builder; + int res = NativeMethods.git_treebuilder_new(out builder, repo, IntPtr.Zero); + Ensure.ZeroResult(res); - return builder; - } + return builder; } public static void git_treebuilder_free(IntPtr bld) @@ -3264,24 +2869,19 @@ namespace LibGit2Sharp.Core public static void git_treebuilder_insert(TreeBuilderSafeHandle builder, string treeentry_name, TreeEntryDefinition treeEntryDefinition) { - using (ThreadAffinity()) - { - GitOid oid = treeEntryDefinition.TargetId.Oid; - int res = NativeMethods.git_treebuilder_insert(IntPtr.Zero, builder, treeentry_name, ref oid, (uint)treeEntryDefinition.Mode); - Ensure.ZeroResult(res); - } + GitOid oid = treeEntryDefinition.TargetId.Oid; + int res = NativeMethods.git_treebuilder_insert(IntPtr.Zero, builder, treeentry_name, ref oid, + (uint)treeEntryDefinition.Mode); + Ensure.ZeroResult(res); } public static ObjectId git_treebuilder_write(TreeBuilderSafeHandle bld) { - using (ThreadAffinity()) - { - GitOid oid; - int res = NativeMethods.git_treebuilder_write(out oid, bld); - Ensure.ZeroResult(res); + GitOid oid; + int res = NativeMethods.git_treebuilder_write(out oid, bld); + Ensure.ZeroResult(res); - return oid; - } + return oid; } #endregion @@ -3303,23 +2903,20 @@ namespace LibGit2Sharp.Core Func<Func<T, IntPtr, int>, int> iterator, params GitErrorCode[] ignoredErrorCodes) { - using (ThreadAffinity()) + var result = new List<TResult>(); + var res = iterator((x, payload) => { - var result = new List<TResult>(); - var res = iterator((x, payload) => - { - result.Add(resultSelector(x)); - return 0; - }); - - if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) - { - return new TResult[0]; - } + result.Add(resultSelector(x)); + return 0; + }); - Ensure.ZeroResult(res); - return result; + if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) + { + return new TResult[0]; } + + Ensure.ZeroResult(res); + return result; } private static ICollection<TResult> git_foreach<T1, T2, TResult>( @@ -3327,23 +2924,20 @@ namespace LibGit2Sharp.Core Func<Func<T1, T2, IntPtr, int>, int> iterator, params GitErrorCode[] ignoredErrorCodes) { - using (ThreadAffinity()) + var result = new List<TResult>(); + var res = iterator((x, y, payload) => { - var result = new List<TResult>(); - var res = iterator((x, y, payload) => - { - result.Add(resultSelector(x, y)); - return 0; - }); + result.Add(resultSelector(x, y)); + return 0; + }); - if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) - { - return new TResult[0]; - } - - Ensure.ZeroResult(res); - return result; + if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) + { + return new TResult[0]; } + + Ensure.ZeroResult(res); + return result; } private static ICollection<TResult> git_foreach<T1, T2, T3, TResult>( @@ -3351,23 +2945,20 @@ namespace LibGit2Sharp.Core Func<Func<T1, T2, T3, IntPtr, int>, int> iterator, params GitErrorCode[] ignoredErrorCodes) { - using (ThreadAffinity()) + var result = new List<TResult>(); + var res = iterator((w, x, y, payload) => { - var result = new List<TResult>(); - var res = iterator((w, x, y, payload) => - { - result.Add(resultSelector(w, x, y)); - return 0; - }); + result.Add(resultSelector(w, x, y)); + return 0; + }); - if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) - { - return new TResult[0]; - } - - Ensure.ZeroResult(res); - return result; + if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) + { + return new TResult[0]; } + + Ensure.ZeroResult(res); + return result; } public delegate TResult Func<T1, T2, T3, T4, T5, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5); @@ -3377,23 +2968,20 @@ namespace LibGit2Sharp.Core Func<Func<T1, T2, T3, T4, IntPtr, int>, int> iterator, params GitErrorCode[] ignoredErrorCodes) { - using (ThreadAffinity()) + var result = new List<TResult>(); + var res = iterator((w, x, y, z, payload) => { - var result = new List<TResult>(); - var res = iterator((w, x, y, z, payload) => - { - result.Add(resultSelector(w, x, y, z)); - return 0; - }); + result.Add(resultSelector(w, x, y, z)); + return 0; + }); - if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) - { - return new TResult[0]; - } - - Ensure.ZeroResult(res); - return result; + if (ignoredErrorCodes != null && ignoredErrorCodes.Contains((GitErrorCode)res)) + { + return new TResult[0]; } + + Ensure.ZeroResult(res); + return result; } private delegate int IteratorNew<THandle>(out THandle iter); @@ -3445,32 +3033,25 @@ namespace LibGit2Sharp.Core where TIterator : SafeHandleBase where THandle : SafeHandleBase { - using (ThreadAffinity()) + using (var iter = git_iterator_new(newFunc)) { - using (var iter = git_iterator_new(newFunc)) + foreach (var next in git_iterator_next(iter, nextFunc, resultSelector)) { - foreach (var next in git_iterator_next(iter, nextFunc, resultSelector)) - { - yield return next; - } + yield return next; } } } private static bool RepositoryStateChecker(RepositorySafeHandle repo, Func<RepositorySafeHandle, int> checker) { - using (ThreadAffinity()) - { - int res = checker(repo); - Ensure.BooleanResult(res); + int res = checker(repo); + Ensure.BooleanResult(res); - return (res == 1); - } + return (res == 1); } private static FilePath ConvertPath(Func<GitBuf, int> pathRetriever) { - using (ThreadAffinity()) using (var buf = new GitBuf()) { int result = pathRetriever(buf); @@ -3485,36 +3066,6 @@ namespace LibGit2Sharp.Core } } - private static Func<IDisposable> ThreadAffinity = WithoutThreadAffinity; - - internal static void EnableThreadAffinity() - { - ThreadAffinity = WithThreadAffinity; - } - - private static IDisposable WithoutThreadAffinity() - { - return null; - } - - private static IDisposable WithThreadAffinity() - { - return new DisposableThreadAffinityWrapper(); - } - - private class DisposableThreadAffinityWrapper : IDisposable - { - public DisposableThreadAffinityWrapper() - { - Thread.BeginThreadAffinity(); - } - - public void Dispose() - { - Thread.EndThreadAffinity(); - } - } - private static readonly IDictionary<Type, Func<string, object>> configurationParser = new Dictionary<Type, Func<string, object>> { { typeof(int), value => git_config_parse_int32(value) }, |