Skip to content

Add List Remote References without creating a Repository #1065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<Compile Include="TestHelpers\SignatureExtensions.cs" />
<Compile Include="TestHelpers\SkippableFactAttribute.cs" />
<Compile Include="LogFixture.cs" />
<Compile Include="TestHelpers\TestRemoteRefs.cs" />
<Compile Include="TreeDefinitionFixture.cs" />
<Compile Include="TreeFixture.cs" />
<Compile Include="UnstageFixture.cs" />
Expand Down
52 changes: 12 additions & 40 deletions LibGit2Sharp.Tests/NetworkFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ public void CanListRemoteReferences(string url)
List<Tuple<string, string>> actualRefs = references.
Select(directRef => new Tuple<string, string>(directRef.CanonicalName, directRef.TargetIdentifier)).ToList();

Assert.Equal(ExpectedRemoteRefs.Count, actualRefs.Count);
for (int i = 0; i < ExpectedRemoteRefs.Count; i++)
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs.Count, actualRefs.Count);
for (int i = 0; i < TestRemoteRefs.ExpectedRemoteRefs.Count; i++)
{
Assert.Equal(ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
Assert.Equal(ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
}
}
}
Expand All @@ -65,11 +65,11 @@ public void CanListRemoteReferencesFromUrl(string url)
List<Tuple<string, string>> actualRefs = references.
Select(directRef => new Tuple<string, string>(directRef.CanonicalName, directRef.TargetIdentifier)).ToList();

Assert.Equal(ExpectedRemoteRefs.Count, actualRefs.Count);
for (int i = 0; i < ExpectedRemoteRefs.Count; i++)
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs.Count, actualRefs.Count);
for (int i = 0; i < TestRemoteRefs.ExpectedRemoteRefs.Count; i++)
{
Assert.Equal(ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
Assert.Equal(ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
}
}
}
Expand Down Expand Up @@ -98,11 +98,11 @@ public void CanListRemoteReferenceObjects()
actualRefs.Add(new Tuple<string, string>(reference.CanonicalName, reference.Target.Id.Sha));
}

Assert.Equal(ExpectedRemoteRefs.Count, actualRefs.Count);
for (int i = 0; i < ExpectedRemoteRefs.Count; i++)
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs.Count, actualRefs.Count);
for (int i = 0; i < TestRemoteRefs.ExpectedRemoteRefs.Count; i++)
{
Assert.Equal(ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
Assert.Equal(ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
}
}
}
Expand Down Expand Up @@ -254,33 +254,5 @@ public void CanMergeFetchedRefs()
Assert.Equal(mergeResult.Status, MergeStatus.NonFastForward);
}
}

/*
* git ls-remote http://github.com/libgit2/TestGitRepository
* 49322bb17d3acc9146f98c97d078513228bbf3c0 HEAD
* 0966a434eb1a025db6b71485ab63a3bfbea520b6 refs/heads/first-merge
* 49322bb17d3acc9146f98c97d078513228bbf3c0 refs/heads/master
* 42e4e7c5e507e113ebbb7801b16b52cf867b7ce1 refs/heads/no-parent
* d96c4e80345534eccee5ac7b07fc7603b56124cb refs/tags/annotated_tag
* c070ad8c08840c8116da865b2d65593a6bb9cd2a refs/tags/annotated_tag^{}
* 55a1a760df4b86a02094a904dfa511deb5655905 refs/tags/blob
* 8f50ba15d49353813cc6e20298002c0d17b0a9ee refs/tags/commit_tree
* 6e0c7bdb9b4ed93212491ee778ca1c65047cab4e refs/tags/nearly-dangling
*/
/// <summary>
/// Expected references on http://github.com/libgit2/TestGitRepository
/// </summary>
private static List<Tuple<string, string>> ExpectedRemoteRefs = new List<Tuple<string, string>>()
{
new Tuple<string, string>("HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0"),
new Tuple<string, string>("refs/heads/first-merge", "0966a434eb1a025db6b71485ab63a3bfbea520b6"),
new Tuple<string, string>("refs/heads/master", "49322bb17d3acc9146f98c97d078513228bbf3c0"),
new Tuple<string, string>("refs/heads/no-parent", "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1"),
new Tuple<string, string>("refs/tags/annotated_tag", "d96c4e80345534eccee5ac7b07fc7603b56124cb"),
new Tuple<string, string>("refs/tags/annotated_tag^{}", "c070ad8c08840c8116da865b2d65593a6bb9cd2a"),
new Tuple<string, string>("refs/tags/blob", "55a1a760df4b86a02094a904dfa511deb5655905"),
new Tuple<string, string>("refs/tags/commit_tree", "8f50ba15d49353813cc6e20298002c0d17b0a9ee"),
new Tuple<string, string>("refs/tags/nearly-dangling", "6e0c7bdb9b4ed93212491ee778ca1c65047cab4e"),
};
}
}
60 changes: 60 additions & 0 deletions LibGit2Sharp.Tests/RepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;
using Xunit.Extensions;

namespace LibGit2Sharp.Tests
{
Expand Down Expand Up @@ -651,5 +652,64 @@ public void CanDetectShallowness()
Assert.False(repo.Info.IsShallow);
}
}

[SkippableFact]
public void CanListRemoteReferencesWithCredentials()
{
InconclusiveIf(() => string.IsNullOrEmpty(Constants.PrivateRepoUrl),
"Populate Constants.PrivateRepo* to run this test");

IEnumerable<DirectReference> references = Repository.ListRemoteReferences(Constants.PrivateRepoUrl,
Constants.PrivateRepoCredentials);

foreach (var reference in references)
{
Assert.NotNull(reference);
}
}

[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
[InlineData("https://github.com/libgit2/TestGitRepository")]
[InlineData("git://github.com/libgit2/TestGitRepository.git")]
public void CanListRemoteReferences(string url)
{
IEnumerable<DirectReference> references = Repository.ListRemoteReferences(url);

List<Tuple<string, string>> actualRefs = references.
Select(directRef => new Tuple<string, string>(directRef.CanonicalName, directRef.TargetIdentifier)).ToList();

Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs.Count, actualRefs.Count);
for (int i = 0; i < TestRemoteRefs.ExpectedRemoteRefs.Count; i++)
{
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
}
}

[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
public void ReadingReferenceRepositoryThroughListRemoteReferencesThrows(string url)
{
IEnumerable<DirectReference> references = Repository.ListRemoteReferences(url);

foreach (var reference in references)
{
IBelongToARepository repositoryReference = reference;
Assert.Throws<InvalidOperationException>(() => repositoryReference.Repository);
}
}

[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
public void ReadingReferenceTargetFromListRemoteReferencesThrows(string url)
{
IEnumerable<DirectReference> references = Repository.ListRemoteReferences(url);

foreach (var reference in references)
{
Assert.Throws<InvalidOperationException>(() => reference.Target);
}
}
}
}
38 changes: 38 additions & 0 deletions LibGit2Sharp.Tests/TestHelpers/TestRemoteRefs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LibGit2Sharp.Tests.TestHelpers
{
public class TestRemoteRefs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice touch!

{
/*
* git ls-remote http://github.com/libgit2/TestGitRepository
* 49322bb17d3acc9146f98c97d078513228bbf3c0 HEAD
* 0966a434eb1a025db6b71485ab63a3bfbea520b6 refs/heads/first-merge
* 49322bb17d3acc9146f98c97d078513228bbf3c0 refs/heads/master
* 42e4e7c5e507e113ebbb7801b16b52cf867b7ce1 refs/heads/no-parent
* d96c4e80345534eccee5ac7b07fc7603b56124cb refs/tags/annotated_tag
* c070ad8c08840c8116da865b2d65593a6bb9cd2a refs/tags/annotated_tag^{}
* 55a1a760df4b86a02094a904dfa511deb5655905 refs/tags/blob
* 8f50ba15d49353813cc6e20298002c0d17b0a9ee refs/tags/commit_tree
* 6e0c7bdb9b4ed93212491ee778ca1c65047cab4e refs/tags/nearly-dangling
*/
/// <summary>
/// Expected references on http://github.com/libgit2/TestGitRepository
/// </summary>
public static List<Tuple<string, string>> ExpectedRemoteRefs = new List<Tuple<string, string>>()
{
new Tuple<string, string>("HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0"),
new Tuple<string, string>("refs/heads/first-merge", "0966a434eb1a025db6b71485ab63a3bfbea520b6"),
new Tuple<string, string>("refs/heads/master", "49322bb17d3acc9146f98c97d078513228bbf3c0"),
new Tuple<string, string>("refs/heads/no-parent", "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1"),
new Tuple<string, string>("refs/tags/annotated_tag", "d96c4e80345534eccee5ac7b07fc7603b56124cb"),
new Tuple<string, string>("refs/tags/annotated_tag^{}", "c070ad8c08840c8116da865b2d65593a6bb9cd2a"),
new Tuple<string, string>("refs/tags/blob", "55a1a760df4b86a02094a904dfa511deb5655905"),
new Tuple<string, string>("refs/tags/commit_tree", "8f50ba15d49353813cc6e20298002c0d17b0a9ee"),
new Tuple<string, string>("refs/tags/nearly-dangling", "6e0c7bdb9b4ed93212491ee778ca1c65047cab4e"),
};
}
}
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,9 @@ internal static extern int git_repository_state(
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxFilePathNoCleanupMarshaler))]
internal static extern FilePath git_repository_workdir(RepositorySafeHandle repository);

[DllImport(libgit2)]
internal static extern int git_repository_new(out RepositorySafeHandle repo);

[DllImport(libgit2)]
internal static extern int git_reset(
RepositorySafeHandle repo,
Expand Down
10 changes: 10 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2167,6 +2167,16 @@ public static RepositorySafeHandle git_repository_open(string path)
return repo;
}

public static RepositorySafeHandle git_repository_new()
{
RepositorySafeHandle repo;
int res = NativeMethods.git_repository_new(out repo);

Ensure.ZeroResult(res);

return repo;
}

public static void git_repository_open_ext(string path, RepositoryOpenFlags flags, string ceilingDirs)
{
int res;
Expand Down
11 changes: 10 additions & 1 deletion LibGit2Sharp/DirectReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,21 @@ protected DirectReference()
internal DirectReference(string canonicalName, IRepository repo, ObjectId targetId)
: base(repo, canonicalName, targetId.Sha)
{
targetBuilder = new Lazy<GitObject>(() => repo.Lookup(targetId));
targetBuilder = new Lazy<GitObject>(() =>
{
if (repo == null)
{
throw new InvalidOperationException("Target requires a local repository");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, braces here as well

}

return repo.Lookup(targetId);
});
}

/// <summary>
/// Gets the target of this <see cref="DirectReference"/>
/// </summary>
/// <exception cref="InvalidOperationException">Throws if Local Repository is not set.</exception>
public virtual GitObject Target
{
get { return targetBuilder.Value; }
Expand Down
13 changes: 12 additions & 1 deletion LibGit2Sharp/Reference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ private string DebuggerDisplay
}
}

IRepository IBelongToARepository.Repository { get { return repo; } }
IRepository IBelongToARepository.Repository
{
get
{
if (repo == null)
{
throw new InvalidOperationException("Repository requires a local repository");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please wrap this in braces?

}

return repo;
}
}
}
}
46 changes: 46 additions & 0 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,52 @@ internal Commit LookupCommit(string committish)
LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit);
}

/// <summary>
/// Lists the Remote Repository References.
/// </summary>
/// <para>
/// Does not require a local Repository. The retrieved
/// <see cref="IBelongToARepository.Repository"/>
/// throws <see cref="InvalidOperationException"/> in this case.
/// </para>
/// <param name="url">The url to list from.</param>
/// <returns>The references in the remote repository.</returns>
public static IEnumerable<DirectReference> ListRemoteReferences(string url)
{
return ListRemoteReferences(url, null);
}

/// <summary>
/// Lists the Remote Repository References.
/// </summary>
/// <para>
/// Does not require a local Repository. The retrieved
/// <see cref="IBelongToARepository.Repository"/>
/// throws <see cref="InvalidOperationException"/> in this case.
/// </para>
/// <param name="url">The url to list from.</param>
/// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
/// <returns>The references in the remote repository.</returns>
public static IEnumerable<DirectReference> ListRemoteReferences(string url, CredentialsHandler credentialsProvider)
{
Ensure.ArgumentNotNull(url, "url");

using (RepositorySafeHandle repositoryHandle = Proxy.git_repository_new())
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_create_anonymous(repositoryHandle, url))
{
var gitCallbacks = new GitRemoteCallbacks { version = 1 };

if (credentialsProvider != null)
{
var callbacks = new RemoteCallbacks(credentialsProvider);
gitCallbacks = callbacks.GenerateCallbacks();
}

Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks);
return Proxy.git_remote_ls(null, remoteHandle);
}
}

/// <summary>
/// Probe for a git repository.
/// <para>The lookup start from <paramref name="startingPath"/> and walk upward parent directories if nothing has been found.</para>
Expand Down