Skip to content

Commit 21696b8

Browse files
committed
Support TreeEntry.Target for gitlink entries
1 parent 6cc2854 commit 21696b8

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed

LibGit2Sharp.Tests/SubmoduleFixture.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,17 @@ public void CanRetrieveTheCommitIdsOfASubmodule(string name, string headId, stri
6464
Assert.Equal((ObjectId)headId, submodule.HeadCommitId);
6565
Assert.Equal((ObjectId)indexId, submodule.IndexCommitId);
6666
Assert.Equal((ObjectId)workDirId, submodule.WorkDirCommitId);
67+
68+
AssertEntryId((ObjectId)headId, repo.Head[name], c => c.Target.Id);
69+
AssertEntryId((ObjectId)indexId, repo.Index[name], i => i.Id);
6770
}
6871
}
6972

73+
private static void AssertEntryId<T>(ObjectId expected, T entry, Func<T, ObjectId> selector)
74+
{
75+
Assert.Equal(expected, ReferenceEquals(entry, null) ? null : selector(entry));
76+
}
77+
7078
[Fact]
7179
public void CanEnumerateRepositorySubmodules()
7280
{

LibGit2Sharp/GitLink.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Diagnostics;
2+
3+
namespace LibGit2Sharp
4+
{
5+
/// <summary>
6+
/// Represents a gitlink (a reference to a commit in another Git repository)
7+
/// </summary>
8+
[DebuggerDisplay("{DebuggerDisplay,nq}")]
9+
public class GitLink : GitObject
10+
{
11+
/// <summary>
12+
/// Needed for mocking purposes.
13+
/// </summary>
14+
protected GitLink()
15+
{ }
16+
17+
internal GitLink(Repository repo, ObjectId id)
18+
: base(repo, id)
19+
{
20+
}
21+
22+
private string DebuggerDisplay
23+
{
24+
get { return Id.ToString(); }
25+
}
26+
}
27+
}

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
<Compile Include="Conflict.cs" />
7171
<Compile Include="ConflictCollection.cs" />
7272
<Compile Include="Core\Handles\GitRefSpecHandle.cs" />
73+
<Compile Include="GitLink.cs" />
7374
<Compile Include="UnmatchedPathException.cs" />
7475
<Compile Include="Core\Handles\ReflogEntrySafeHandle.cs" />
7576
<Compile Include="Core\Handles\ReflogSafeHandle.cs" />

LibGit2Sharp/TreeEntry.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,19 @@ internal ObjectId TargetId
7272

7373
private GitObject RetrieveTreeEntryTarget()
7474
{
75-
if (!Type.HasAny(new[]{GitObjectType.Tree, GitObjectType.Blob}))
75+
switch (Type)
7676
{
77-
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "TreeEntry target of type '{0}' are not supported.", Type));
77+
case GitObjectType.Commit:
78+
return new GitLink(repo, targetOid);
79+
case GitObjectType.Blob:
80+
case GitObjectType.Tree:
81+
return GitObject.BuildFrom(repo, targetOid, Type, Path);
82+
default:
83+
throw new InvalidOperationException(
84+
string.Format(CultureInfo.InvariantCulture,
85+
"TreeEntry target of type '{0}' is not supported.",
86+
Type));
7887
}
79-
80-
return GitObject.BuildFrom(repo, targetOid, Type, Path);
8188
}
8289

8390
/// <summary>

0 commit comments

Comments
 (0)