using System;
using System.Diagnostics;
using System.Globalization;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
///
/// A reference to a known by the .
///
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class IndexEntry : IEquatable
{
private static readonly LambdaEqualityHelper equalityHelper =
new LambdaEqualityHelper(x => x.Path, x => x.Id, x => x.Mode, x => x.StageLevel);
///
/// Gets the relative path to the file within the working directory.
///
public virtual string Path { get; private set; }
///
/// Gets the file mode.
///
public virtual Mode Mode { get; private set; }
///
/// Gets the stage number.
///
public virtual StageLevel StageLevel { get; private set; }
///
/// Gets the id of the pointed at by this index entry.
///
public virtual ObjectId Id { get; private set; }
internal static IndexEntry BuildFromPtr(IndexEntrySafeHandle handle)
{
if (handle == null || handle.IsZero)
{
return null;
}
GitIndexEntry entry = handle.MarshalAsGitIndexEntry();
FilePath path = LaxFilePathMarshaler.FromNative(entry.Path);
return new IndexEntry
{
Path = path.Native,
Id = entry.oid,
StageLevel = Proxy.git_index_entry_stage(handle),
Mode = (Mode)entry.Mode
};
}
///
/// 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);
}
private string DebuggerDisplay
{
get
{
return string.Format(CultureInfo.InvariantCulture,
"{0} ({1}) => \"{2}\"", Path, StageLevel, Id.ToString(7));
}
}
}
}