Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2013-04-30 23:59:03 +0400
committernulltoken <emeric.fermas@gmail.com>2013-05-03 22:06:34 +0400
commit82a8d05417dc804c114376140be94ae69e3c5af4 (patch)
tree51849360ff3c554af7aff84311db2d8e3fee394b
parentb4aae4b72586a64159a1f8f10bdf7787d091d3fa (diff)
Introduce ObjectType
-rw-r--r--LibGit2Sharp/Core/GitObjectTypeMap.cs13
-rw-r--r--LibGit2Sharp/GitObject.cs14
-rw-r--r--LibGit2Sharp/IRepository.cs30
-rw-r--r--LibGit2Sharp/LibGit2Sharp.csproj2
-rw-r--r--LibGit2Sharp/ObjectType.cs54
-rw-r--r--LibGit2Sharp/Repository.cs42
-rw-r--r--LibGit2Sharp/RepositoryExtensions.cs14
7 files changed, 146 insertions, 23 deletions
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<Type, GitObjectType>
- {
- 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<GitObject>
{
- internal static GitObjectTypeMap TypeToTypeMap =
- new GitObjectTypeMap
+ internal static IDictionary<Type, ObjectType> TypeToKindMap =
+ new Dictionary<Type, ObjectType>
{
- { 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<GitObject> equalityHelper =
diff --git a/LibGit2Sharp/IRepository.cs b/LibGit2Sharp/IRepository.cs
index fcc45b7f..24f41031 100644
--- a/LibGit2Sharp/IRepository.cs
+++ b/LibGit2Sharp/IRepository.cs
@@ -106,6 +106,36 @@ namespace LibGit2Sharp
GitObject Lookup(string objectish, GitObjectType type = GitObjectType.Any);
/// <summary>
+ /// Try to lookup an object by its <see cref = "ObjectId" />. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "id">The id to lookup.</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ GitObject Lookup(ObjectId id);
+
+ /// <summary>
+ /// Try to lookup an object by its sha or a reference canonical name. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "objectish">A revparse spec for the object to lookup.</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ GitObject Lookup(string objectish);
+
+ /// <summary>
+ /// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "id">The id to lookup.</param>
+ /// <param name = "type">The kind of GitObject being looked up</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ GitObject Lookup(ObjectId id, ObjectType type);
+
+ /// <summary>
+ /// Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "objectish">A revparse spec for the object to lookup.</param>
+ /// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ GitObject Lookup(string objectish, ObjectType type);
+
+ /// <summary>
/// Stores the content of the <see cref = "Repository.Index" /> as a new <see cref = "Commit" /> into the repository.
/// The tip of the <see cref = "Repository.Head"/> will be used as the parent of this new Commit.
/// Once the commit is created, the <see cref = "Repository.Head"/> will move forward to point at it.
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 @@
<Compile Include="Changes.cs" />
<Compile Include="CheckoutCallbacks.cs" />
<Compile Include="CheckoutOptions.cs" />
+ <Compile Include="ObjectType.cs" />
<Compile Include="ReferenceExtensions.cs" />
<Compile Include="Conflict.cs" />
<Compile Include="ConflictCollection.cs" />
@@ -185,7 +186,6 @@
<Compile Include="Core\Ensure.cs" />
<Compile Include="Core\Epoch.cs" />
<Compile Include="Core\GitErrorCode.cs" />
- <Compile Include="Core\GitObjectTypeMap.cs" />
<Compile Include="Core\GitOid.cs" />
<Compile Include="Core\GitReferenceType.cs" />
<Compile Include="Core\GitSignature.cs" />
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
+{
+ /// <summary>
+ /// Underlying type of a <see cref = "GitObject" />
+ /// </summary>
+ public enum ObjectType
+ {
+ /// <summary>
+ /// A commit object.
+ /// </summary>
+ Commit = 1,
+
+ /// <summary>
+ /// A tree (directory listing) object.
+ /// </summary>
+ Tree = 2,
+
+ /// <summary>
+ /// A file revision object.
+ /// </summary>
+ Blob = 3,
+
+ /// <summary>
+ /// An annotated tag object.
+ /// </summary>
+ 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
@@ -386,6 +386,48 @@ namespace LibGit2Sharp
}
/// <summary>
+ /// Try to lookup an object by its <see cref = "ObjectId" />. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "id">The id to lookup.</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ public GitObject Lookup(ObjectId id)
+ {
+ return LookupInternal(id, GitObjectType.Any, null);
+ }
+
+ /// <summary>
+ /// Try to lookup an object by its sha or a reference canonical name. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "objectish">A revparse spec for the object to lookup.</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ public GitObject Lookup(string objectish)
+ {
+ return Lookup(objectish, GitObjectType.Any, LookUpOptions.None);
+ }
+
+ /// <summary>
+ /// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "id">The id to lookup.</param>
+ /// <param name = "type">The kind of GitObject being looked up</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ public GitObject Lookup(ObjectId id, ObjectType type)
+ {
+ return LookupInternal(id, type.ToGitObjectType(), null);
+ }
+
+ /// <summary>
+ /// Try to lookup an object by its sha or a reference canonical name and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
+ /// </summary>
+ /// <param name = "objectish">A revparse spec for the object to lookup.</param>
+ /// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
+ /// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
+ public GitObject Lookup(string objectish, ObjectType type)
+ {
+ return Lookup(objectish, type.ToGitObjectType(), LookUpOptions.None);
+ }
+
+ /// <summary>
/// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
/// </summary>
/// <param name = "id">The id to lookup.</param>
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<T>();
- 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)]);
}
/// <summary>
@@ -37,7 +42,12 @@ namespace LibGit2Sharp
{
EnsureNoGitLink<T>();
- 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<T>() where T : GitObject