8000 Prevent repo.Lookup() to accept GitLink as a target type · repo-archive/libgit2sharp@023eaf4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 023eaf4

Browse files
committed
Prevent repo.Lookup() to accept GitLink as a target type
1 parent 1465bf3 commit 023eaf4

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,15 @@ public void LookingUpWithATooShortShaThrows()
412412
}
413413
}
414414

415+
[Fact]
416+
public void LookingUpAGitLinkThrows()
417+
{
418+
using (var repo = new Repository(BareTestRepoPath))
419+
{
420+
Assert.Throws<ArgumentException>(() => repo.Lookup<GitLink>("e90810b"));
421+
}
422+
}
423+
415424
[Fact]
416425
public void CanDiscoverABareRepoGivenTheRepoPath()
417426
{

LibGit2Sharp/RepositoryExtensions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static class RepositoryExtensions
2121
/// <returns></returns>
2222
public static T Lookup<T>(this IRepository repository, string objectish) where T : GitObject
2323
{
24+
EnsureNoGitLink<T>();
25+
2426
return (T)repository.Lookup(objectish, GitObject.TypeToTypeMap[typeof (T)]);
2527
}
2628

@@ -33,9 +35,21 @@ public static T Lookup<T>(this IRepository repository, string objectish) where T
3335
/// <returns></returns>
3436
public static T Lookup<T>(this IRepository repository, ObjectId id) where T : GitObject
3537
{
38+
EnsureNoGitLink<T>();
39+
3640
return (T)repository.Lookup(id, GitObject.TypeToTypeMap[typeof(T)]);
3741
}
3842

43+
private static void EnsureNoGitLink<T>() where T : GitObject
44+
{
45+
if (typeof(T) != typeof(GitLink))
46+
{
47+
return;
48+
}
49+
50+
throw new ArgumentException("A GitObject of type 'GitLink' cannot be looked up.");
51+
}
52+
3953
/// <summary>
4054
/// Creates a lightweight tag with the specified name. This tag will point at the commit pointed at by the <see cref = "Repository.Head" />.
4155
/// </summary>

0 commit comments

Comments
 (0)
0