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; }
///
/// Whether the file is marked as assume-unchanged
///
public virtual bool AssumeUnchanged { get; private set; }
///
/// Gets the id of the pointed at by this index entry.
///
public virtual ObjectId Id { get; private set; }
internal static unsafe IndexEntry BuildFromPtr(git_index_entry* entry)
{
if (entry == null)
{
return null;
}
FilePath path = LaxFilePathMarshaler.FromNative(entry->path);
return new IndexEntry
{
Path = path.Native,
Id = new ObjectId(entry->id.Id),
StageLevel = Proxy.git_index_entry_stage(entry),
Mode = (Mode)entry->mode,
AssumeUnchanged = (git_index_entry.GIT_IDXENTRY_VALID & entry->flags) == git_index_entry.GIT_IDXENTRY_VALID
};
}
///
/// 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));
}
}
}
}