Skip to content

Commit

Permalink
Support TreeEntry.Target for gitlink entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlbyk committed Apr 13, 2013
1 parent 6cc2854 commit 21696b8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
8 changes: 8 additions & 0 deletions LibGit2Sharp.Tests/SubmoduleFixture.cs
Expand Up @@ -64,9 +64,17 @@ public void CanRetrieveTheCommitIdsOfASubmodule(string name, string headId, stri
Assert.Equal((ObjectId)headId, submodule.HeadCommitId);
Assert.Equal((ObjectId)indexId, submodule.IndexCommitId);
Assert.Equal((ObjectId)workDirId, submodule.WorkDirCommitId);

AssertEntryId((ObjectId)headId, repo.Head[name], c => c.Target.Id);
AssertEntryId((ObjectId)indexId, repo.Index[name], i => i.Id);
}
}

private static void AssertEntryId<T>(ObjectId expected, T entry, Func<T, ObjectId> selector)
{
Assert.Equal(expected, ReferenceEquals(entry, null) ? null : selector(entry));
}

[Fact]
public void CanEnumerateRepositorySubmodules()
{
Expand Down
27 changes: 27 additions & 0 deletions LibGit2Sharp/GitLink.cs
@@ -0,0 +1,27 @@
using System.Diagnostics;

namespace LibGit2Sharp
{
/// <summary>
/// Represents a gitlink (a reference to a commit in another Git repository)
/// </summary>
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class GitLink : GitObject
{
/// <summary>
/// Needed for mocking purposes.
/// </summary>
protected GitLink()
{ }

internal GitLink(Repository repo, ObjectId id)
: base(repo, id)
{
}

private string DebuggerDisplay
{
get { return Id.ToString(); }
}
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp/LibGit2Sharp.csproj
Expand Up @@ -70,6 +70,7 @@
<Compile Include="Conflict.cs" />
<Compile Include="ConflictCollection.cs" />
<Compile Include="Core\Handles\GitRefSpecHandle.cs" />
<Compile Include="GitLink.cs" />
<Compile Include="UnmatchedPathException.cs" />
<Compile Include="Core\Handles\ReflogEntrySafeHandle.cs" />
<Compile Include="Core\Handles\ReflogSafeHandle.cs" />
Expand Down
15 changes: 11 additions & 4 deletions LibGit2Sharp/TreeEntry.cs
Expand Up @@ -72,12 +72,19 @@ internal ObjectId TargetId

private GitObject RetrieveTreeEntryTarget()
{
if (!Type.HasAny(new[]{GitObjectType.Tree, GitObjectType.Blob}))
switch (Type)
{
throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "TreeEntry target of type '{0}' are not supported.", Type));
case GitObjectType.Commit:
return new GitLink(repo, targetOid);
case GitObjectType.Blob:
case GitObjectType.Tree:
return GitObject.BuildFrom(repo, targetOid, Type, Path);
default:
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture,
"TreeEntry target of type '{0}' is not supported.",
Type));
}

return GitObject.BuildFrom(repo, targetOid, Type, Path);
}

/// <summary>
Expand Down

0 comments on commit 21696b8

Please sign in to comment.