From 82a8d05417dc804c114376140be94ae69e3c5af4 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Tue, 30 Apr 2013 21:59:03 +0200 Subject: Introduce ObjectType --- LibGit2Sharp/Core/GitObjectTypeMap.cs | 13 --------- LibGit2Sharp/GitObject.cs | 14 ++++----- LibGit2Sharp/IRepository.cs | 30 +++++++++++++++++++ LibGit2Sharp/LibGit2Sharp.csproj | 2 +- LibGit2Sharp/ObjectType.cs | 54 +++++++++++++++++++++++++++++++++++ LibGit2Sharp/Repository.cs | 42 +++++++++++++++++++++++++++ LibGit2Sharp/RepositoryExtensions.cs | 14 +++++++-- 7 files changed, 146 insertions(+), 23 deletions(-) delete mode 100644 LibGit2Sharp/Core/GitObjectTypeMap.cs create mode 100644 LibGit2Sharp/ObjectType.cs diff --git a/LibGit2Sharp/Core/GitObjectTypeMap.cs b/LibGit2Sharp/Core/GitObjectTypeMap.cs deleted file mode 100644 index df33787e..00000000 --- a/LibGit2Sharp/Core/GitObjectTypeMap.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace LibGit2Sharp.Core -{ - internal class GitObjectTypeMap : Dictionary - { - public new GitObjectType this[Type type] - { - get { return !ContainsKey(type) ? GitObjectType.Any : base[type]; } - } - } -} diff --git a/LibGit2Sharp/GitObject.cs b/LibGit2Sharp/GitObject.cs index f42ecd88..e8420926 100644 --- a/LibGit2Sharp/GitObject.cs +++ b/LibGit2Sharp/GitObject.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using LibGit2Sharp.Core; @@ -12,14 +13,13 @@ namespace LibGit2Sharp [DebuggerDisplay("{DebuggerDisplay,nq}")] public abstract class GitObject : IEquatable { - internal static GitObjectTypeMap TypeToTypeMap = - new GitObjectTypeMap + internal static IDictionary TypeToKindMap = + new Dictionary { - { typeof(Commit), GitObjectType.Commit }, - { typeof(Tree), GitObjectType.Tree }, - { typeof(Blob), GitObjectType.Blob }, - { typeof(TagAnnotation), GitObjectType.Tag }, - { typeof(GitObject), GitObjectType.Any }, + { typeof(Commit), ObjectType.Commit }, + { typeof(Tree), ObjectType.Tree }, + { typeof(Blob), ObjectType.Blob }, + { typeof(TagAnnotation), ObjectType.Tag }, }; private static readonly LambdaEqualityHelper equalityHelper = diff --git a/LibGit2Sharp/IRepository.cs b/LibGit2Sharp/IRepository.cs index fcc45b7f..24f41031 100644 --- a/LibGit2Sharp/IRepository.cs +++ b/LibGit2Sharp/IRepository.cs @@ -105,6 +105,36 @@ namespace LibGit2Sharp /// The or null if it was not found. GitObject Lookup(string objectish, GitObjectType type = GitObjectType.Any); + /// + /// Try to lookup an object by its . If no matching object is found, null will be returned. + /// + /// The id to lookup. + /// The or null if it was not found. + GitObject Lookup(ObjectId id); + + /// + /// Try to lookup an object by its sha or a reference canonical name. If no matching object is found, null will be returned. + /// + /// A revparse spec for the object to lookup. + /// The or null if it was not found. + GitObject Lookup(string objectish); + + /// + /// Try to lookup an object by its and . If no matching object is found, null will be returned. + /// + /// The id to lookup. + /// The kind of GitObject being looked up + /// The or null if it was not found. + GitObject Lookup(ObjectId id, ObjectType type); + + /// + /// Try to lookup an object by its sha or a reference canonical name and . If no matching object is found, null will be returned. + /// + /// A revparse spec for the object to lookup. + /// The kind of being looked up + /// The or null if it was not found. + GitObject Lookup(string objectish, ObjectType type); + /// /// Stores the content of the as a new into the repository. /// The tip of the will be used as the parent of this new Commit. diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index ff377b8d..cf4fc0a3 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -67,6 +67,7 @@ + @@ -185,7 +186,6 @@ - diff --git a/LibGit2Sharp/ObjectType.cs b/LibGit2Sharp/ObjectType.cs new file mode 100644 index 00000000..efe4e251 --- /dev/null +++ b/LibGit2Sharp/ObjectType.cs @@ -0,0 +1,54 @@ +using System; + +namespace LibGit2Sharp +{ + /// + /// Underlying type of a + /// + public enum ObjectType + { + /// + /// A commit object. + /// + Commit = 1, + + /// + /// A tree (directory listing) object. + /// + Tree = 2, + + /// + /// A file revision object. + /// + Blob = 3, + + /// + /// An annotated tag object. + /// + Tag = 4, + } + + internal static class ObjectTypeExtensions + { + public static GitObjectType ToGitObjectType(this ObjectType type) + { + switch (type) + { + case ObjectType.Commit: + return GitObjectType.Commit; + + case ObjectType.Tree: + return GitObjectType.Tree; + + case ObjectType.Blob: + return GitObjectType.Blob; + + case ObjectType.Tag: + return GitObjectType.Tag; + + default: + throw new InvalidOperationException(string.Format("Cannot map {0} to a GitObjectType.", type)); + } + } + } +} diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 4e06ea6f..54707803 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -385,6 +385,48 @@ namespace LibGit2Sharp } } + /// + /// Try to lookup an object by its . If no matching object is found, null will be returned. + /// + /// The id to lookup. + /// The or null if it was not found. + public GitObject Lookup(ObjectId id) + { + return LookupInternal(id, GitObjectType.Any, null); + } + + /// + /// Try to lookup an object by its sha or a reference canonical name. If no matching object is found, null will be returned. + /// + /// A revparse spec for the object to lookup. + /// The or null if it was not found. + public GitObject Lookup(string objectish) + { + return Lookup(objectish, GitObjectType.Any, LookUpOptions.None); + } + + /// + /// Try to lookup an object by its and . If no matching object is found, null will be returned. + /// + /// The id to lookup. + /// The kind of GitObject being looked up + /// The or null if it was not found. + public GitObject Lookup(ObjectId id, ObjectType type) + { + return LookupInternal(id, type.ToGitObjectType(), null); + } + + /// + /// Try to lookup an object by its sha or a reference canonical name and . If no matching object is found, null will be returned. + /// + /// A revparse spec for the object to lookup. + /// The kind of being looked up + /// The or null if it was not found. + public GitObject Lookup(string objectish, ObjectType type) + { + return Lookup(objectish, type.ToGitObjectType(), LookUpOptions.None); + } + /// /// Try to lookup an object by its and . If no matching object is found, null will be returned. /// diff --git a/LibGit2Sharp/RepositoryExtensions.cs b/LibGit2Sharp/RepositoryExtensions.cs index ae62fab2..1cc638b8 100644 --- a/LibGit2Sharp/RepositoryExtensions.cs +++ b/LibGit2Sharp/RepositoryExtensions.cs @@ -23,7 +23,12 @@ namespace LibGit2Sharp { EnsureNoGitLink(); - return (T)repository.Lookup(objectish, GitObject.TypeToTypeMap[typeof (T)]); + if (typeof (T) == typeof (GitObject)) + { + return (T)repository.Lookup(objectish); + } + + return (T)repository.Lookup(objectish, GitObject.TypeToKindMap[typeof(T)]); } /// @@ -37,7 +42,12 @@ namespace LibGit2Sharp { EnsureNoGitLink(); - return (T)repository.Lookup(id, GitObject.TypeToTypeMap[typeof(T)]); + if (typeof(T) == typeof(GitObject)) + { + return (T)repository.Lookup(id); + } + + return (T)repository.Lookup(id, GitObject.TypeToKindMap[typeof(T)]); } private static void EnsureNoGitLink() where T : GitObject -- cgit v1.2.3