Skip to content

Make retrieval of error message thread-safe #207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions LibGit2Sharp/Blob.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;

Expand Down Expand Up @@ -37,12 +35,7 @@ public virtual byte[] Content
{
get
{
using (var obj = new ObjectSafeWrapper(Id, repo))
{
var arr = new byte[Size];
Marshal.Copy(NativeMethods.git_blob_rawcontent(obj.ObjectPtr), arr, 0, Size);
return arr;
}
return Proxy.git_blob_rawcontent(repo.Handle, Id, Size);
}
}

Expand All @@ -53,22 +46,15 @@ public virtual Stream ContentStream
{
get
{
using (var obj = new ObjectSafeWrapper(Id, repo))
{
IntPtr ptr = NativeMethods.git_blob_rawcontent(obj.ObjectPtr);
unsafe
{
return new UnmanagedMemoryStream((byte*)ptr.ToPointer(), Size);
}
}
return Proxy.git_blob_rawcontent_stream(repo.Handle, Id, Size);
}
}

internal static Blob BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo)
{
var blob = new Blob(repo, id)
{
Size = NativeMethods.git_blob_rawsize(obj)
Size = Proxy.git_blob_rawsize(obj)
};
return blob;
}
Expand Down
19 changes: 4 additions & 15 deletions LibGit2Sharp/BranchCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ private class BranchNameEnumerable : IEnumerable<string>

public BranchNameEnumerable(RepositorySafeHandle handle, GitBranchType gitBranchType)
{
Ensure.Success(NativeMethods.git_branch_foreach(handle, gitBranchType, Callback, IntPtr.Zero));
Proxy.git_branch_foreach(handle, gitBranchType, Callback);
}

private int Callback(IntPtr branchName, GitBranchType branchType, IntPtr payload)
Expand Down Expand Up @@ -146,11 +146,7 @@ public virtual Branch Add(string name, string commitish, bool allowOverwrite = f

ObjectId commitId = repo.LookupCommit(commitish).Id;

using (var osw = new ObjectSafeWrapper(commitId, repo))
{
GitOid oid;
Ensure.Success(NativeMethods.git_branch_create(out oid, repo.Handle, name, osw.ObjectPtr, allowOverwrite));
}
Proxy.git_branch_create(repo.Handle, name, commitId, allowOverwrite);

return this[ShortToLocalName(name)];
}
Expand All @@ -177,14 +173,7 @@ public virtual void Remove(string name, bool isRemote = false)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");

int res = NativeMethods.git_branch_delete(repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);

if (res == (int)GitErrorCode.NotFound)
{
return;
}

Ensure.Success(res);
Proxy.git_branch_delete(repo.Handle, name, isRemote ? GitBranchType.GIT_BRANCH_REMOTE : GitBranchType.GIT_BRANCH_LOCAL);
}

/// <summary>
Expand All @@ -210,7 +199,7 @@ public virtual Branch Move(string currentName, string newName, bool allowOverwri
Ensure.ArgumentNotNullOrEmptyString(currentName, "currentName");
Ensure.ArgumentNotNullOrEmptyString(newName, "name");

Ensure.Success(NativeMethods.git_branch_move(repo.Handle, currentName, newName, allowOverwrite));
Proxy.git_branch_move(repo.Handle, currentName, newName, allowOverwrite);

return this[newName];
}
Expand Down
28 changes: 9 additions & 19 deletions LibGit2Sharp/Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,7 @@ public virtual int ParentsCount
{
get
{
using (var obj = new ObjectSafeWrapper(Id, repo))
{
return (int)NativeMethods.git_commit_parentcount(obj.ObjectPtr);
}
return Proxy.git_commit_parentcount(repo.Handle, Id);
}
}

Expand All @@ -121,13 +118,13 @@ public virtual IEnumerable<Note> Notes

private IEnumerable<Commit> RetrieveParentsOfCommit(ObjectId oid)
{
using (var obj = new ObjectSafeWrapper(oid, repo))
using (var obj = new ObjectSafeWrapper(oid, repo.Handle))
{
uint parentsCount = NativeMethods.git_commit_parentcount(obj.ObjectPtr);
int parentsCount = Proxy.git_commit_parentcount(obj);

for (uint i = 0; i < parentsCount; i++)
{
using (var parentCommit = GetParentCommitHandle(i, obj))
using (var parentCommit = Proxy.git_commit_parent(obj, i))
{
yield return BuildFromPtr(parentCommit, ObjectIdOf(parentCommit), repo);
}
Expand All @@ -142,27 +139,20 @@ private IEnumerable<Note> RetrieveNotesOfCommit(ObjectId oid)

internal static Commit BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo)
{
ObjectId treeId = NativeMethods.git_commit_tree_oid(obj).MarshalAsObjectId();
ObjectId treeId = Proxy.git_commit_tree_oid(obj);

return new Commit(id, treeId, repo)
{
Message = NativeMethods.git_commit_message(obj),
Message = Proxy.git_commit_message(obj),
Encoding = RetrieveEncodingOf(obj),
Author = new Signature(NativeMethods.git_commit_author(obj)),
Committer = new Signature(NativeMethods.git_commit_committer(obj)),
Author = Proxy.git_commit_author(obj),
Committer = Proxy.git_commit_committer(obj),
};
}

private static GitObjectSafeHandle GetParentCommitHandle(uint i, ObjectSafeWrapper obj)
{
GitObjectSafeHandle parentCommit;
Ensure.Success(NativeMethods.git_commit_parent(out parentCommit, obj.ObjectPtr, i));
return parentCommit;
}

private static string RetrieveEncodingOf(GitObjectSafeHandle obj)
{
string encoding = NativeMethods.git_commit_message_encoding(obj);
string encoding = Proxy.git_commit_message_encoding(obj);

return encoding ?? "UTF-8";
}
Expand Down
43 changes: 12 additions & 31 deletions LibGit2Sharp/CommitLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,21 +108,9 @@ public virtual Commit FindCommonAncestor(Commit first, Commit second)
Ensure.ArgumentNotNull(first, "first");
Ensure.ArgumentNotNull(second, "second");

using (var osw1 = new ObjectSafeWrapper(first.Id, repo))
using (var osw2 = new ObjectSafeWrapper(second.Id, repo))
{
GitOid ret;
int result = NativeMethods.git_merge_base(out ret, repo.Handle, osw1.ObjectPtr, osw2.ObjectPtr);

if (result == (int)GitErrorCode.NotFound)
{
return null;
}
ObjectId id = Proxy.git_merge_base(repo.Handle, first, second);

Ensure.Success(result);

return repo.Lookup<Commit>(new ObjectId(ret));
}
return id == null ? null : repo.Lookup<Commit>(id);
}

/// <summary>
Expand Down Expand Up @@ -192,11 +180,9 @@ private class CommitEnumerator : IEnumerator<Commit>
public CommitEnumerator(Repository repo, Filter filter)
{
this.repo = repo;
int res = NativeMethods.git_revwalk_new(out handle, repo.Handle);
handle = Proxy.git_revwalk_new(repo.Handle);
repo.RegisterForCleanup(handle);

Ensure.Success(res);

Sort(filter.SortBy);
Push(filter.SinceList);
Hide(filter.UntilList);
Expand All @@ -216,24 +202,21 @@ object IEnumerator.Current

public bool MoveNext()
{
GitOid oid;
int res = NativeMethods.git_revwalk_next(out oid, handle);
ObjectId id = Proxy.git_revwalk_next(handle);

if (res == (int)GitErrorCode.RevWalkOver)
if (id == null)
{
return false;
}

Ensure.Success(res);

currentOid = new ObjectId(oid);
currentOid = id;

return true;
}

public void Reset()
{
NativeMethods.git_revwalk_reset(handle);
Proxy.git_revwalk_reset(handle);
}

#endregion
Expand All @@ -249,23 +232,21 @@ private void Dispose(bool disposing)
handle.SafeDispose();
}

private delegate int HidePushSignature(RevWalkerSafeHandle handle, ref GitOid oid);
private delegate void HidePushSignature(RevWalkerSafeHandle handle, ObjectId id);

private void InternalHidePush(IList<object> identifier, HidePushSignature hidePush)
{
IEnumerable<ObjectId> oids = RetrieveCommitOids(identifier).TakeWhile(o => o != null);

foreach (ObjectId actedOn in oids)
{
GitOid oid = actedOn.Oid;
int res = hidePush(handle, ref oid);
Ensure.Success(res);
hidePush(handle, actedOn);
}
}

private void Push(IList<object> identifier)
{
InternalHidePush(identifier, NativeMethods.git_revwalk_push);
InternalHidePush(identifier, Proxy.git_revwalk_push);
}

private void Hide(IList<object> identifier)
Expand All @@ -275,12 +256,12 @@ private void Hide(IList<object> identifier)
return;
}

InternalHidePush(identifier, NativeMethods.git_revwalk_hide);
InternalHidePush(identifier, Proxy.git_revwalk_hide);
}

private void Sort(GitSortOptions options)
{
NativeMethods.git_revwalk_sorting(handle, options);
Proxy.git_revwalk_sorting(handle, options);
}

private ObjectId DereferenceToCommit(string identifier)
Expand Down
Loading