8000 Introduce ObjectType · SinghVarun/libgit2sharp@82a8d05 · GitHub
[go: up one dir, main page]

Skip to content

Commit 82a8d05

Browse files
committed
Introduce ObjectType
1 parent b4aae4b commit 82a8d05

File tree

7 files changed

+146
-23
lines changed

7 files changed

+146
-23
lines changed

LibGit2Sharp/Core/GitObjectTypeMap.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

LibGit2Sharp/GitObject.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Globalization;
45
using LibGit2Sharp.Core;
@@ -12,14 +13,13 @@ namespace LibGit2Sharp
1213
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1314
public abstract class GitObject : IEquatable<GitObject>
1415
{
15-
internal static GitObjectTypeMap TypeToTypeMap =
16-
new GitObjectTypeMap
16+
internal static IDictionary<Type, ObjectType> TypeToKindMap =
17+
new Dictionary<Type, ObjectType>
1718
{
18-
{ typeof(Commit), GitObjectType.Commit },
19-
{ typeof(Tree), GitObjectType.Tree },
20-
{ typeof(Blob), GitObjectType.Blob },
21-
{ typeof(TagAnnotation), GitObjectType.Tag },
22-
{ typeof(GitObject), GitObjectType.Any },
19+
{ typeof(Commit), ObjectType.Commit },
20+
{ typeof(Tree), ObjectType.Tree },
21+
{ typeof(Blob), ObjectType.Blob },
22+
{ typeof(TagAnnotation), ObjectType.Tag },
2323
};
2424

2525
private static readonly LambdaEqualityHelper<GitObject> equalityHelper =

LibGit2Sharp/IRepository.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,36 @@ public interface IRepository : IDisposable
105105
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
106106
GitObject Lookup(string objectish, GitObjectType type = GitObjectType.Any);
107107

108+
/// <summary>
109+
/// Try to lookup an object by its <see cref = "ObjectId" />. If no matching object is found, null will be returned.
110+
/// </summary>
111+
/// <param name = "id">The id to lookup.</param>
112+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
113+
GitObject Lookup(ObjectId id);
114+
115+
/// <summary>
116+
/// Try to lookup an object by its sha or a reference canonical name. If no matching object is found, null will be returned.
117+
/// </summary>
118+
/// <param name = "objectish">A revparse spec for the object to lookup.</param>
119+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
120+
GitObject Lookup(string objectish);
121+
122+
/// <summary>
123+
/// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
124+
/// </summary>
125+
/// <param name = "id">The id to lookup.</param>
126+
/// <param name = "type">The kind of GitObject being looked up</param>
127+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
128+
GitObject Lookup(ObjectId id, ObjectType type);
129+
130+
/// <summary>
131+
/// 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.
132+
/// </summary>
133+
/// <param name = "objectish">A revparse spec for the object to lookup.</param>
134+
/// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
135+
/// <returns>The <see cref = "GitObject" /> or null if it wa 6D47 s not found.</returns>
136+
GitObject Lookup(string objectish, ObjectType type);
137+
108138
/// <summary>
109139
/// Stores the content of the <see cref = "Repository.Index" /> as a new <see cref = "Commit" /> into the repository.
110140
/// The tip of the <see cref = "Repository.Head"/> will be used as the parent of this new Commit.

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="Changes.cs" />
6868
<Compile Include="CheckoutCallbacks.cs" />
6969
<Compile Include="CheckoutOptions.cs" />
70+
<Compile Include="ObjectType.cs" />
7071
<Compile Include="ReferenceExtensions.cs" />
7172
<Compile Include="Conflict.cs" />
7273
<Compile Include="ConflictCollection.cs" />
@@ -185,7 +186,6 @@
185186
<Compile Include="Core\Ensure.cs" />
186187
<Compile Include="Core\Epoch.cs" />
187188
<Compile Include="Core\GitErrorCode.cs" />
188-
<Compile Include="Core\GitObjectTypeMap.cs" />
189189
<Compile Include="Core\GitOid.cs" />
190190
<Compile Include="Core\GitReferenceType.cs" />
191191
<Compile Include="Core\GitSignature.cs" />

LibGit2Sharp/ObjectType.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using System;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Underlying type of a <see cref = "GitObject" />
7+
/// </summary>
8+
public enum ObjectType
9+
{
10+
/// <summary>
11+
/// A commit object.
12+
/// </summary>
13+
Commit = 1,
14+
15+
/// <summary>
16+
/// A tree (directory listing) object.
17+
/// </summary>
18+
Tree = 2,
19+
20+
/// <summary>
21+
/// A file revision object.
22+
/// </summary>
23+
Blob = 3,
24+
25+
/// <summary>
26+
/// An annotated tag object.
27+
/// </summary>
28+
Tag = 4,
29+
}
30+
31+
internal static class ObjectTypeExtensions
32+
{
33+
public static GitObjectType ToGitObjectType(this ObjectType type)
34+
{
35+
switch (type)
36+
{
37+
case ObjectType.Commit:
38+
return GitObjectType.Commit;
39+
40+
case ObjectType.Tree:
41+
return GitObjectType.Tree;
42+
43+
case ObjectType.Blob:
44+
return GitObjectType.Blob;
45+
46+
case ObjectType.Tag:
47+
return GitObjectType.Tag;
48+
49+
default:
50+
throw new InvalidOperationException(string.Format("Cannot map {0} to a GitObjectType.", type));
51+
}
52+
}
53+
}
54+
}

LibGit2Sharp/Repository.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,48 @@ public static Repository Init(string path, bool isBare = false, RepositoryOption
385385
}
386386
}
387387

388+
/// <summary>
389+
/// Try to lookup an object by its <see cref = "ObjectId" />. If no matching object is found, null will be returned.
390+
/// </summary>
391+
/// <param name = "id">The id to lookup.</param>
392+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
393+
public GitObject Lookup(ObjectId id)
394+
{
395+
return LookupInternal(id, GitObjectType.Any, null);
396+
}
397+
398+
/// <summary>
399+
/// Try to lookup an object by its sha or a reference canonical name. If no matching object is found, null will be returned.
400+
/// </summary>
401+
/// <param name = "objectish">A revparse spec for the object to lookup.</param>
402+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
403+
public GitObject Lookup(string objectish)
404+
{
405+
return Lookup(objectish, GitObjectType.Any, LookUpOptions.None);
406+
}
407+
408+
/// <summary>
409+
/// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
410+
/// </summary>
411+
/// <param name = "id">The id to lookup.</param>
412+
/// <param name = "type">The kind of GitObject being looked up</param>
413+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
414+
public GitObject Lookup(ObjectId id, ObjectType type)
415+
{
416+
return LookupInternal(id, type.ToGitObjectType(), null);
417+
}
418+
419+
/// <summary>
420+
/// 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.
421+
/// </summary>
422+
/// <param name = "objectish">A revparse spec for the object to lookup.</param>
423+
/// <param name = "type">The kind of <see cref = "GitObject" /> being looked up</param>
424+
/// <returns>The <see cref = "GitObject" /> or null if it was not found.</returns>
425+
public GitObject Lookup(string objectish, ObjectType type)
426+
{
427+
return Lookup(objectish, type.ToGitObjectType(), LookUpOptions.None);
428+
}
429+
388430
/// <summary>
389431
/// Try to lookup an object by its <see cref = "ObjectId" /> and <see cref = "GitObjectType" />. If no matching object is found, null will be returned.
390432
/// </summary>

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ public static T Lookup<T>(this IRepository repository, string objectish) where T
2323
{
2424
EnsureNoGitLink<T>();
2525

26-
return (T)repository.Lookup(objectish, GitObject.TypeToTypeMap[typeof (T)]);
26+
if (typeof (T) == typeof (GitObject))
27+
{
28+
return (T)repository.Lookup(objectish);
29+
}
30+
31+
return (T)repository.Lookup(objectish, GitObject.TypeToKindMap[typeof(T)]);
2732
}
2833

2934
/// <summary>
@@ -37,7 +42,12 @@ public static T Lookup<T>(this IRepository repository, ObjectId id) where T : Gi
3742
{
3843
EnsureNoGitLink<T>();
3944

40-
return (T)repository.Lookup(id, GitObject.TypeToTypeMap[typeof(T)]);
45+
if (typeof(T) == typeof(GitObject))
46+
{
47+
return (T)repository.Lookup(id);
48+
}
49+
50+
return (T)repository.Lookup(id, GitObject.TypeToKindMap[typeof(T)]);
4151
}
4252

4353
private static void EnsureNoGitLink<T>() where T : GitObject

0 commit comments

Comments
 (0)
0