using System; using System.Runtime.InteropServices; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { /// /// A reference to a known by the . /// public class IndexEntry : IEquatable { private static readonly LambdaEqualityHelper equalityHelper = new LambdaEqualityHelper(new Func[] { x => x.Path, x => x.Id, x => x.State }); private Func state; private static readonly Utf8Marshaler marshaler = (Utf8Marshaler)Utf8Marshaler.GetInstance(string.Empty); /// /// State of the version of the pointed at by this , /// compared against the known from the and the file in the working directory. /// public FileStatus State { get { return state(); } } /// /// Gets the relative path to the file within the working directory. /// public string Path { get; private set; } /// /// Gets the id of the pointed at by this index entry. /// public ObjectId Id { get; private set; } internal static IndexEntry CreateFromPtr(Repository repo, IndexEntrySafeHandle handle) { GitIndexEntry entry = handle.MarshalAsGitIndexEntry(); FilePath path = (string)marshaler.MarshalNativeToManaged(entry.Path); return new IndexEntry { Path = path.Native, Id = new ObjectId(entry.oid), state = () => repo.Index.RetrieveStatus(path.Native) }; } /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// True if the specified is equal to the current ; otherwise, false. public override bool Equals(object obj) { return Equals(obj as IndexEntry); } /// /// Determines whether the specified is equal to the current . /// /// The to compare with the current . /// True if the specified is equal to the current ; otherwise, false. public bool Equals(IndexEntry other) { return equalityHelper.Equals(this, other); } /// /// Returns the hash code for this instance. /// /// A 32-bit signed integer hash code. public override int GetHashCode() { return equalityHelper.GetHashCode(this); } /// /// Tests if two are equal. /// /// First to compare. /// Second to compare. /// True if the two objects are equal; false otherwise. public static bool operator ==(IndexEntry left, IndexEntry right) { return Equals(left, right); } /// /// Tests if two are different. /// /// First to compare. /// Second to compare. /// True if the two objects are different; false otherwise. public static bool operator !=(IndexEntry left, IndexEntry right) { return !Equals(left, right); } } }