Skip to content

Code review on making the message retrieval thread-safe #208

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 3 commits 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
18 changes: 18 additions & 0 deletions LibGit2Sharp.Tests/StatusFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,23 @@ public void RetrievingTheStatusOfTheRepositoryHonorsTheGitIgnoreDirectives()
Assert.Equal(new[] { relativePath, "new_untracked_file.txt" }, newStatus.Ignored);
}
}

[Fact(Skip = "This test needs libgit2/libgit2@ffbc689")]
public void RetrievingTheStatusOfAnAmbiguousFileThrows()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
{
string relativePath = Path.Combine("1", "ambiguous1.txt");
string fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);
File.WriteAllText(fullFilePath, "I don't like brackets.");

relativePath = Path.Combine("1", "ambiguous[1].txt");
fullFilePath = Path.Combine(repo.Info.WorkingDirectory, relativePath);
File.WriteAllText(fullFilePath, "Brackets all the way.");

repo.Index.RetrieveStatus(relativePath);
}
}
}
}
4 changes: 1 addition & 3 deletions LibGit2Sharp/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,7 @@ private ConfigurationSafeHandle RetrieveConfigurationHandle(ConfigurationLevel l
return h;
}

private delegate T ConfigGetter<T>(ConfigurationSafeHandle handle, string name);

private static Func<string, object, ConfigurationSafeHandle, object> GetRetriever<T>(ConfigGetter<T> getter)
private static Func<string, object, ConfigurationSafeHandle, object> GetRetriever<T>(Func<ConfigurationSafeHandle, string, T> getter)
{
return (key, defaultValue, handle) =>
{
Expand Down
14 changes: 10 additions & 4 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1284,13 +1284,19 @@ public static FileStatus git_status_file(RepositorySafeHandle repo, FilePath pat
FileStatus status;
int res = NativeMethods.git_status_file(out status, repo, path);

if (res == (int)GitErrorCode.NotFound)
switch (res)
{
return FileStatus.Nonexistent;
case (int)GitErrorCode.NotFound:
return FileStatus.Nonexistent;

case (int)GitErrorCode.Ambiguous:
throw new AmbiguousException(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));

default:
Ensure.Success(res);
break;
}

Ensure.Success(res);

return status;
}
}
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/GitObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public virtual string Sha
get { return Id.Sha; }
}

internal static GitObject CreateFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo, FilePath path)
internal static GitObject BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo, FilePath path)
{
GitObjectType type = Proxy.git_object_type(obj);
switch (type)
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private IndexEntry this[uint index]
get
{
IndexEntrySafeHandle entryHandle = Proxy.git_index_get(handle, index);
return IndexEntry.CreateFromPtr(repo, entryHandle);
return IndexEntry.BuildFromPtr(repo, entryHandle);
}
}

Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/IndexEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public FileStatus State
/// </summary>
public ObjectId Id { get; private set; }

internal static IndexEntry CreateFromPtr(Repository repo, IndexEntrySafeHandle handle)
internal static IndexEntry BuildFromPtr(Repository repo, IndexEntrySafeHandle handle)
{
GitIndexEntry entry = handle.MarshalAsGitIndexEntry();

Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private Note(ObjectId blobId, string message, ObjectId targetObjectId, string @n
/// </summary>
public virtual ObjectId TargetObjectId { get; private set; }

internal static Note BuildFromPtr(string @namespace, ObjectId targetObjectId, NoteSafeHandle note)
internal static Note BuildFromPtr(NoteSafeHandle note, string @namespace, ObjectId targetObjectId)
{
ObjectId oid = Proxy.git_note_oid(note);
string message = Proxy.git_note_message(note);
Expand Down
6 changes: 3 additions & 3 deletions LibGit2Sharp/NoteCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ internal Note RetrieveNote(ObjectId targetObjectId, string canonicalNamespace)
{
using (NoteSafeHandle noteHandle = Proxy.git_note_read(repo.Handle, canonicalNamespace, targetObjectId))
{
return noteHandle == null ? null :
Note.BuildFromPtr(UnCanonicalizeName(canonicalNamespace), targetObjectId, noteHandle);
return noteHandle == null ? null :
Note.BuildFromPtr(noteHandle, UnCanonicalizeName(canonicalNamespace), targetObjectId);
}
}

Expand Down Expand Up @@ -211,7 +211,7 @@ public virtual void Remove(ObjectId targetId, Signature author, Signature commit
Ensure.ArgumentNotNullOrEmptyString(@namespace, "@namespace");

string canonicalNamespace = NormalizeToCanonicalName(@namespace);

Proxy.git_note_remove(repo.Handle, canonicalNamespace, author, committer, targetId);
}

Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp/ReferenceCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ internal T Resolve<T>(string name) where T : Reference

using (ReferenceSafeHandle referencePtr = RetrieveReferencePtr(name, false))
{
return referencePtr == null ? null : Reference.BuildFromPtr<T>(referencePtr, repo);
return referencePtr == null ? null :
Reference.BuildFromPtr<T>(referencePtr, repo);
}
}

Expand Down
7 changes: 1 addition & 6 deletions LibGit2Sharp/Remote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@ public class Remote : IEquatable<Remote>
protected Remote()
{ }

internal static Remote CreateFromPtr(RemoteSafeHandle handle)
internal static Remote BuildFromPtr(RemoteSafeHandle handle)
{
if (handle == null)
{
return null;
}

string name = Proxy.git_remote_name(handle);
string url = Proxy.git_remote_url(handle);

Expand Down
6 changes: 3 additions & 3 deletions LibGit2Sharp/RemoteCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private Remote RemoteForName(string name)
{
using (RemoteSafeHandle handle = Proxy.git_remote_load(repository.Handle, name, false))
{
return Remote.CreateFromPtr(handle);
return handle == null ? null : Remote.BuildFromPtr(handle);
}
}

Expand Down Expand Up @@ -80,7 +80,7 @@ public virtual Remote Add(string name, string url)

using (RemoteSafeHandle handle = Proxy.git_remote_add(repository.Handle, name, url))
{
return Remote.CreateFromPtr(handle);
return Remote.BuildFromPtr(handle);
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public virtual Remote Add(string name, string url, string fetchRefSpec)
using (RemoteSafeHandle handle = Proxy.git_remote_new(repository.Handle, name, url, fetchRefSpec))
{
Proxy.git_remote_save(handle);
return Remote.CreateFromPtr(handle);
return Remote.BuildFromPtr(handle);
}
}

Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ internal GitObject LookupInternal(ObjectId id, GitObjectType type, FilePath know
obj = Proxy.git_object_lookup(handle, id, type);
}

return obj == null ? null : GitObject.CreateFromPtr(obj, id, this, knownPath);
return obj == null ? null : GitObject.BuildFromPtr(obj, id, this, knownPath);
}
finally
{
Expand Down Expand Up @@ -363,7 +363,7 @@ internal GitObject Lookup(string objectish, GitObjectType type, LookUpOptions lo
return null;
}

obj = GitObject.CreateFromPtr(sh, GitObject.ObjectIdOf(sh), this, PathFromRevparseSpec(objectish));
obj = GitObject.BuildFromPtr(sh, GitObject.ObjectIdOf(sh), this, PathFromRevparseSpec(objectish));
}

if (lookUpOptions.Has(LookUpOptions.DereferenceResultToCommit))
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Tree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ IEnumerator IEnumerable.GetEnumerator()

#endregion

internal static Tree BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo, FilePath path)
internal new static Tree BuildFromPtr(GitObjectSafeHandle obj, ObjectId id, Repository repo, FilePath path)
{
var tree = new Tree(id, path, Proxy.git_tree_entrycount(obj), repo);
return tree;
Expand Down