using System; using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using LibGit2Sharp.Core; using LibGit2Sharp.Core.Handles; namespace LibGit2Sharp { /// /// A GitObject /// [DebuggerDisplay("{DebuggerDisplay,nq}")] public abstract class GitObject : IEquatable { internal static IDictionary TypeToKindMap = new Dictionary { { typeof(Commit), ObjectType.Commit }, { typeof(Tree), ObjectType.Tree }, { typeof(Blob), ObjectType.Blob }, { typeof(TagAnnotation), ObjectType.Tag }, }; private static readonly LambdaEqualityHelper equalityHelper = new LambdaEqualityHelper(x => x.Id); /// /// The containing the object. /// protected readonly Repository repo; /// /// Needed for mocking purposes. /// protected GitObject() { } /// /// Initializes a new instance of the class. /// /// The containing the object. /// The it should be identified by. protected GitObject(Repository repo, ObjectId id) { this.repo = repo; Id = id; } /// /// Gets the id of this object /// public virtual ObjectId Id { get; private set; } /// /// Gets the 40 character sha1 of this object. /// public virtual string Sha { get { return Id.Sha; } } internal static GitObject BuildFrom(Repository repo, ObjectId id, GitObjectType type, FilePath path) { switch (type) { case GitObjectType.Commit: return new Commit(repo, id); case GitObjectType.Tree: return new Tree(repo, id, path); case GitObjectType.Tag: return new TagAnnotation(repo, id); case GitObjectType.Blob: return new Blob(repo, id); default: throw new LibGit2SharpException(string.Format(CultureInfo.InvariantCulture, "Unexpected type '{0}' for object '{1}'.", type, id)); } } internal Commit DereferenceToCommit(bool throwsIfCanNotBeDereferencedToACommit) { using (GitObjectSafeHandle peeledHandle = Proxy.git_object_peel(repo.Handle, Id, GitObjectType.Commit, throwsIfCanNotBeDereferencedToACommit)) { if (peeledHandle == null) { return null; } return (Commit)BuildFrom(repo, Proxy.git_object_id(peeledHandle), GitObjectType.Commit, null); } } /// /// 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 GitObject); } /// /// 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(GitObject 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 ==(GitObject left, GitObject 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 !=(GitObject left, GitObject right) { return !Equals(left, right); } /// /// Returns the , a representation of the current . /// /// The that represents the current . public override string ToString() { return Id.ToString(); } private string DebuggerDisplay { get { return Id.ToString(7); } } } }