Skip to content

IndexEntry: enhance! Also, R# is fun. #336

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 9 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
1 change: 0 additions & 1 deletion LibGit2Sharp.Tests/ConfigurationFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
Expand Down
5 changes: 1 addition & 4 deletions LibGit2Sharp.Tests/IgnoreFixture.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO;
using System.Linq;
using System.Text;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

Expand Down
3 changes: 1 addition & 2 deletions LibGit2Sharp.Tests/MergeFixture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp.Tests/OdbBackendFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public override int ReadStream(byte[] oid, out OdbBackendStream stream)
throw new NotImplementedException();
}

public override int ForEach(OdbBackend.ForEachCallback callback)
public override int ForEach(ForEachCallback callback)
{
throw new NotImplementedException();
}
Expand Down
4 changes: 1 addition & 3 deletions LibGit2Sharp.Tests/RemoteFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;
Expand Down
5 changes: 1 addition & 4 deletions LibGit2Sharp.Tests/TestHelpers/ExpectedFetchState.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xunit;

namespace LibGit2Sharp.Tests.TestHelpers
Expand Down Expand Up @@ -41,7 +39,6 @@ internal class ExpectedFetchState
/// Constructor.
/// </summary>
/// <param name="remoteName">Name of the remote being updated.</param>
/// <param name="url">Url of the remote.</param>
public ExpectedFetchState(string remoteName)
{
RemoteName = remoteName;
Expand Down
6 changes: 1 addition & 5 deletions LibGit2Sharp.Tests/TestHelpers/TestRemoteInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibGit2Sharp.Core.Compat;
using System.Collections.Generic;

namespace LibGit2Sharp.Tests.TestHelpers
{
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/CheckoutCallbacks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class CheckoutCallbacks
/// <summary>
/// Managed delegate to call in response to checkout progress_cb callback.
/// </summary>
private CheckoutProgressHandler onCheckoutProgress;
private readonly CheckoutProgressHandler onCheckoutProgress;

/// <summary>
/// Constructor to set up native callback for given managed delegate.
Expand Down
7 changes: 3 additions & 4 deletions LibGit2Sharp/Commit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,19 @@ public ParentsCollection(Repository repo, ObjectId commitId)

private ICollection<Commit> RetrieveParentsOfCommit(Repository repo, ObjectId commitId)
{
var parents = new List<Commit>();

using (var obj = new ObjectSafeWrapper(commitId, repo.Handle))
{
int parentsCount = _count.Value;
var parents = new List<Commit>(parentsCount);

for (uint i = 0; i < parentsCount; i++)
{
ObjectId parentCommitId = Proxy.git_commit_parent_oid(obj.ObjectPtr, i);
parents.Add(new Commit(repo, parentCommitId));
}
}

return parents;
return parents;
}
}

public IEnumerator<Commit> GetEnumerator()
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/Core/GitOdbBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ static GitOdbBackend()
/// This field is populated by libgit2 at backend addition time, and exists for its
/// use only. From this side of the interop, it is unreferenced.
/// </summary>
private IntPtr Odb;
private readonly IntPtr Odb;

#pragma warning restore 169

Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ private sealed class LibraryLifetimeObject : CriticalFinalizerObject
// Ensure mono can JIT the .cctor and adjust the PATH before trying to load the native library.
// See https://github.com/libgit2/libgit2sharp/pull/190
[MethodImpl(MethodImplOptions.NoInlining)]
public LibraryLifetimeObject() { Ensure.ZeroResult(NativeMethods.git_threads_init()); }
~LibraryLifetimeObject() { NativeMethods.git_threads_shutdown(); }
public LibraryLifetimeObject() { Ensure.ZeroResult(git_threads_init()); }
~LibraryLifetimeObject() { git_threads_shutdown(); }
}

static NativeMethods()
Expand Down
9 changes: 4 additions & 5 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ public static IntPtr git_odb_backend_malloc(IntPtr backend, UIntPtr len)
{
throw new LibGit2SharpException(String.Format(CultureInfo.InvariantCulture,
"Unable to allocate {0} bytes; out of memory",
len.ToString()),
len),
GitErrorCode.Error, GitErrorCategory.NoMemory);
}

Expand Down Expand Up @@ -1969,27 +1969,26 @@ private static unsafe class Libgit2UnsafeHelper
{
public static IList<string> BuildListOf(UnSafeNativeMethods.git_strarray strArray)
{
var list = new List<string>();

try
{
UnSafeNativeMethods.git_strarray* gitStrArray = &strArray;

uint numberOfEntries = (uint)gitStrArray->size;
var numberOfEntries = (int)gitStrArray->size;
var list = new List<string>(numberOfEntries);
for (uint i = 0; i < numberOfEntries; i++)
{
var name = Utf8Marshaler.FromNative((IntPtr)gitStrArray->strings[i]);
list.Add(name);
}

list.Sort(StringComparer.Ordinal);
return list;
}
finally
{
UnSafeNativeMethods.git_strarray_free(ref strArray);
}

return list;
}
}

Expand Down
4 changes: 2 additions & 2 deletions LibGit2Sharp/Core/Utf8Marshaler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public IntPtr MarshalManagedToNative(Object managedObj)
throw new MarshalDirectiveException("Utf8Marshaler must be used on a string.");
}

return Utf8Marshaler.FromManaged(str);
return FromManaged(str);
}

public Object MarshalNativeToManaged(IntPtr pNativeData)
{
return Utf8Marshaler.FromNative(pNativeData);
return FromNative(pNativeData);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/DirectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected DirectReference()
internal DirectReference(string canonicalName, Repository repo, ObjectId targetId)
: base(canonicalName, targetId.Sha)
{
targetBuilder = new Lazy<GitObject>(() => repo.Lookup(targetId)); ;
targetBuilder = new Lazy<GitObject>(() => repo.Lookup(targetId));
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions LibGit2Sharp/FetchHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ internal FetchHead(Repository repo, string remoteCanonicalName,
RemoteCanonicalName = remoteCanonicalName;
}

/// <summary>
/// Returns "FETCH_HEAD[i]", where i is the index of this fetch head.
/// </summary>
protected override string Shorten()
{
return CanonicalName;
Expand Down
22 changes: 4 additions & 18 deletions LibGit2Sharp/Index.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,31 +91,17 @@ private IndexEntry this[int index]

#region IEnumerable<IndexEntry> Members

private class OrdinalComparer<T> : IComparer<T>
{
Func<T, string> accessor;

public OrdinalComparer(Func<T, string> accessor)
{
this.accessor = accessor;
}

public int Compare(T x, T y)
{
return string.CompareOrdinal(accessor(x), accessor(y));
}
}

private List<IndexEntry> AllIndexEntries()
{
var list = new List<IndexEntry>();
var entryCount = Count;
var list = new List<IndexEntry>(entryCount);

for (int i = 0; i < Count; i++)
for (int i = 0; i < entryCount; i++)
{
list.Add(this[i]);
}

list.Sort(new OrdinalComparer<IndexEntry>(i => i.Path));
list.Sort();
return list;
}

Expand Down
14 changes: 12 additions & 2 deletions LibGit2Sharp/IndexEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace LibGit2Sharp
/// A reference to a <see cref = "Blob" /> known by the <see cref = "Index" />.
/// </summary>
[DebuggerDisplayAttribute("{DebuggerDisplay,nq}")]
public class IndexEntry : IEquatable<IndexEntry>
public class IndexEntry : IEquatable<IndexEntry>, IComparable<IndexEntry>
{
private static readonly LambdaEqualityHelper<IndexEntry> equalityHelper =
new LambdaEqualityHelper<IndexEntry>(x => x.Path, x => x.Id, x => x.Mode, x => x.StageLevel);
Expand Down Expand Up @@ -68,6 +68,16 @@ internal static IndexEntry BuildFromPtr(Repository repo, IndexEntrySafeHandle ha
};
}

/// <summary>
/// Compares the current instance with another <see cref="IndexEntry"/>.
/// </summary>
/// <param name="other">The other index entry.</param>
/// <returns>A member of Z.</returns>
public int CompareTo(IndexEntry other)
{
return other == null ? 1 : StringComparer.Ordinal.Compare(Path, other.Path);
}

/// <summary>
/// Determines whether the specified <see cref = "Object" /> is equal to the current <see cref = "IndexEntry" />.
/// </summary>
Expand Down Expand Up @@ -124,7 +134,7 @@ private string DebuggerDisplay
get
{
return string.Format(CultureInfo.InvariantCulture,
"{0} => \"{1}\"", Path, Id.ToString(7));
"{0} ({1}) => \"{2}\"", Path, StageLevel, Id.ToString(7));
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions LibGit2Sharp/MergeHead.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public virtual Commit Tip
get { return TargetObject; }
}

/// <summary>
/// Returns "MERGE_HEAD[i]", where i is the index of this merge head.
/// </summary>
protected override string Shorten()
{
return CanonicalName;
Expand Down
8 changes: 4 additions & 4 deletions LibGit2Sharp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ public virtual void Push(
/// </summary>
private class PushCallbacks
{
PushStatusErrorHandler OnError;
readonly PushStatusErrorHandler onError;

public PushCallbacks(PushStatusErrorHandler onError)
{
OnError = onError;
this.onError = onError;
}

public int Callback(IntPtr referenceNamePtr, IntPtr msgPtr, IntPtr payload)
{
// Exit early if there is no callback.
if (OnError == null)
if (onError == null)
{
return 0;
}
Expand All @@ -233,7 +233,7 @@ public int Callback(IntPtr referenceNamePtr, IntPtr msgPtr, IntPtr payload)
{
string referenceName = Utf8Marshaler.FromNative(referenceNamePtr);
string msg = Utf8Marshaler.FromNative(msgPtr);
OnError(new PushStatusError(referenceName, msg));
onError(new PushStatusError(referenceName, msg));
}

return 0;
Expand Down
3 changes: 0 additions & 3 deletions LibGit2Sharp/NetworkExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System.Globalization;
using LibGit2Sharp.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibGit2Sharp
{
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/ObjectDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public virtual Blob CreateBlob(string path)
public virtual void AddBackend(OdbBackend backend, int priority)
{
Ensure.ArgumentNotNull(backend, "backend");
Ensure.ArgumentConformsTo<int>(priority, s => s > 0, "priority");
Ensure.ArgumentConformsTo(priority, s => s > 0, "priority");

Proxy.git_odb_add_backend(this.handle, backend.GitOdbBackendPointer, priority);
}
Expand Down
28 changes: 14 additions & 14 deletions LibGit2Sharp/OdbBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,15 @@ private static class BackendEntryPoints
// to native memory with StructureToPtr), we need to bind to static delegates. If at construction time
// we were to bind to the methods directly, that's the same as newing up a fresh delegate every time.
// Those delegates won't be rooted in the object graph and can be collected as soon as StructureToPtr finishes.
public static GitOdbBackend.read_callback ReadCallback = new GitOdbBackend.read_callback(Read);
public static GitOdbBackend.read_prefix_callback ReadPrefixCallback = new GitOdbBackend.read_prefix_callback(ReadPrefix);
public static GitOdbBackend.read_header_callback ReadHeaderCallback = new GitOdbBackend.read_header_callback(ReadHeader);
public static GitOdbBackend.readstream_callback ReadStreamCallback = new GitOdbBackend.readstream_callback(ReadStream);
public static GitOdbBackend.write_callback WriteCallback = new GitOdbBackend.write_callback(Write);
public static GitOdbBackend.writestream_callback WriteStreamCallback = new GitOdbBackend.writestream_callback(WriteStream);
public static GitOdbBackend.exists_callback ExistsCallback = new GitOdbBackend.exists_callback(Exists);
public static GitOdbBackend.foreach_callback ForEachCallback = new GitOdbBackend.foreach_callback(Foreach);
public static GitOdbBackend.free_callback FreeCallback = new GitOdbBackend.free_callback(Free);
public static readonly GitOdbBackend.read_callback ReadCallback = Read;
public static readonly GitOdbBackend.read_prefix_callback ReadPrefixCallback = ReadPrefix;
public static readonly GitOdbBackend.read_header_callback ReadHeaderCallback = ReadHeader;
public static readonly GitOdbBackend.readstream_callback ReadStreamCallback = ReadStream;
public static readonly GitOdbBackend.write_callback WriteCallback = Write;
public static readonly GitOdbBackend.writestream_callback WriteStreamCallback = WriteStream;
public static readonly GitOdbBackend.exists_callback ExistsCallback = Exists;
public static readonly GitOdbBackend.foreach_callback ForEachCallback = Foreach;
public static readonly GitOdbBackend.free_callback FreeCallback = Free;

private unsafe static int Read(
out IntPtr buffer_p,
Expand Down Expand Up @@ -365,7 +365,7 @@ private static unsafe int Write(
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;

if (odbBackend != null &&
len.ToUInt64() < (ulong)long.MaxValue)
len.ToUInt64() < long.MaxValue)
{
try
{
Expand Down Expand Up @@ -403,7 +403,7 @@ private static int WriteStream(
OdbBackend odbBackend = GCHandle.FromIntPtr(Marshal.ReadIntPtr(backend, GitOdbBackend.GCHandleOffset)).Target as OdbBackend;

if (odbBackend != null &&
length.ToUInt64() < (ulong)long.MaxValue)
length.ToUInt64() < long.MaxValue)
{
OdbBackendStream stream;

Expand Down Expand Up @@ -539,10 +539,10 @@ private int CallbackMethod(byte[] oid)
return cb(ref gitOid, data);
}

public ForEachCallback ManagedCallback;
public readonly ForEachCallback ManagedCallback;

private GitOdbBackend.foreach_callback_callback cb;
private IntPtr data;
private readonly GitOdbBackend.foreach_callback_callback cb;
private readonly IntPtr data;
}
}

Expand Down
Loading