Skip to content

Commit 80d693f

Browse files
committed
Update Repository ListRemoteReferences Return Type
1 parent b456277 commit 80d693f

File tree

10 files changed

+132
-106
lines changed

10 files changed

+132
-106
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
<Compile Include="PatchStatsFixture.cs" />
6666
<Compile Include="RefSpecFixture.cs" />
6767
<Compile Include="EqualityFixture.cs" />
68-
<Compile Include="RemoteRepositoryFixture.cs" />
6968
<Compile Include="RevertFixture.cs" />
7069
<Compile Include="SetErrorFixture.cs" />
7170
<Compile Include="SignatureFixture.cs" />

LibGit2Sharp.Tests/RemoteRepositoryFixture.cs

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

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq;
55
using LibGit2Sharp.Tests.TestHelpers;
66
using Xunit;
7+
using Xunit.Extensions;
78

89
namespace LibGit2Sharp.Tests
910
{
@@ -651,5 +652,65 @@ public void CanDetectShallowness()
651652
Assert.False(repo.Info.IsShallow);
652653
}
653654
}
655+
656+
[SkippableFact]
657+
public void CanListRemoteReferencesWithCredentials()
658+
{
659+
InconclusiveIf(() => string.IsNullOrEmpty(Constants.PrivateRepoUrl),
660+
"Populate Constants.PrivateRepo* to run this test");
661+
662+
IEnumerable<Reference> references = Repository.ListRemoteReferences(Constants.PrivateRepoUrl,
663+
Constants.PrivateRepoCredentials);
664+
665+
foreach (var reference in references)
666+
{
667+
Assert.NotNull(reference);
668+
}
669+
}
670+
671+
[Theory]
672+
[InlineData("http://github.com/libgit2/TestGitRepository")]
673+
[InlineData("https://github.com/libgit2/TestGitRepository")]
674+
[InlineData("git://github.com/libgit2/TestGitRepository.git")]
675+
public void CanListRemoteReferences(string url)
676+
{
677+
IEnumerable<Reference> references = Repository.ListRemoteReferences(url);
678+
679+
List<Tuple<string, string>> actualRefs = references.
680+
Select(directRef => new Tuple<string, string>(directRef.CanonicalName, directRef.TargetIdentifier)).ToList();
681+
682+
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs.Count, actualRefs.Count);
683+
for (int i = 0; i < TestRemoteRefs.ExpectedRemoteRefs.Count; i++)
684+
{
685+
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item2, actualRefs[i].Item2);
686+
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs[i].Item1, actualRefs[i].Item1);
687+
}
688+
}
689+
690+
[Theory]
691+
[InlineData("http://github.com/libgit2/TestGitRepository")]
692+
public void ReadingReferenceRepositoryThroughListRemoteReferencesThrows(string url)
693+
{
694+
IEnumerable<Reference> references = Repository.ListRemoteReferences(url);
695+
696+
foreach (var reference in references)
697+
{
698+
IBelongToARepository repositoryReference = reference;
699+
Assert.Throws<InvalidOperationException>(() => repositoryReference.Repository);
700+
}
701+
}
702+
703+
[Theory]
704+
[InlineData("http://github.com/libgit2/TestGitRepository")]
705+
public void ReadingReferenceTargetFromListRemoteReferencesThrows(string url)
706+
{
707+
IEnumerable<Reference> references = Repository.ListRemoteReferences(url);
708+
709+
foreach (var reference in references)
710+
{
711+
var directReference = (DirectReference)reference;
712+
Assert.Throws<InvalidOperationException>(() => directReference.Target);
713+
}
714+
}
654715
}
655716
}

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,6 +1285,9 @@ internal static extern int git_repository_state(
12851285
[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(LaxFilePathNoCleanupMarshaler))]
12861286
internal static extern FilePath git_repository_workdir(RepositorySafeHandle repository);
12871287

1288+
[DllImport(libgit2)]
1289+
internal static extern int git_repository_new(out RepositorySafeHandle repo);
1290+
12881291
[DllImport(libgit2)]
12891292
internal static extern int git_reset(
12901293
RepositorySafeHandle repo,
@@ -1625,9 +1628,6 @@ internal static extern int git_treebuilder_insert(
16251628

16261629
[DllImport(libgit2)]
16271630
internal static extern int git_cherrypick(RepositorySafeHandle repo, GitObjectSafeHandle commit, GitCherryPickOptions options);
1628-
1629-
[DllImport(libgit2)]
1630-
internal static extern int git_repository_new(out RepositorySafeHandle repo);
16311631
}
16321632
}
16331633
// ReSharper restore InconsistentNaming

LibGit2Sharp/Core/Proxy.cs

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,35 +1931,7 @@ public static IList<string> git_remote_list(RepositorySafeHandle repo)
19311931
}
19321932
}
19331933

1934-
public static IEnumerable<RemoteHead> git_remote_ls(RemoteSafeHandle remote)
1935-
{
1936-
var heads = new List<RemoteHead>();
1937-
1938-
foreach (var remoteHead in git_remote_head_ls(remote))
1939-
{
1940-
string name = LaxUtf8Marshaler.FromNative(remoteHead.NamePtr);
1941-
var objectId = new ObjectId(remoteHead.Oid);
1942-
var head = new RemoteHead(objectId, name);
1943-
heads.Add(head);
1944-
}
1945-
1946-
return heads;
1947-
}
1948-
1949-
public static IEnumerable<DirectReference> git_remote_ls(IRepository repository, RemoteSafeHandle remote)
1950-
{
1951-
var refs = new List<DirectReference>();
1952-
1953-
foreach (var remoteHead in git_remote_head_ls(remote))
1954-
{
1955-
string name = LaxUtf8Marshaler.FromNative(remoteHead.NamePtr);
1956-
refs.Add(new DirectReference(name, repository, remoteHead.Oid));
1957-
}
1958-
1959-
return refs;
1960-
}
1961-
1962-
private static IEnumerable<GitRemoteHead> git_remote_head_ls(RemoteSafeHandle remote)
1934+
public static IEnumerable<DirectReference> git_remote_ls(Repository repository, RemoteSafeHandle remote)
19631935
{
19641936
IntPtr heads;
19651937
UIntPtr count;
@@ -1974,7 +1946,7 @@ private static IEnumerable<GitRemoteHead> git_remote_head_ls(RemoteSafeHandle re
19741946
throw new OverflowException();
19751947
}
19761948

1977-
var refs = new List<GitRemoteHead>();
1949+
var refs = new List<DirectReference>();
19781950
IntPtr currentHead = heads;
19791951

19801952
for (int i = 0; i < intCount; i++)
@@ -1988,7 +1960,8 @@ private static IEnumerable<GitRemoteHead> git_remote_head_ls(RemoteSafeHandle re
19881960
throw new InvalidOperationException("Not expecting null value for reference name.");
19891961
}
19901962

1991-
refs.Add(remoteHead);
1963+
string name = LaxUtf8Marshaler.FromNative(remoteHead.NamePtr);
1964+
refs.Add(new DirectReference(name, repository, remoteHead.Oid));
19921965

19931966
currentHead = IntPtr.Add(currentHead, IntPtr.Size);
19941967
}

LibGit2Sharp/DirectReference.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,19 @@ protected DirectReference()
1818
internal DirectReference(string canonicalName, IRepository repo, ObjectId targetId)
1919
: base(repo, canonicalName, targetId.Sha)
2020
{
21-
targetBuilder = new Lazy<GitObject>(() => repo.Lookup(targetId));
21+
targetBuilder = new Lazy<GitObject>(() =>
22+
{
23+
if (repo == null)
24+
throw new InvalidOperationException("Target requires a local repository");
25+
26+
return repo.Lookup(targetId);
27+
});
2228
}
2329

2430
/// <summary>
2531
/// Gets the target of this <see cref="DirectReference"/>
2632
/// </summary>
33+
/// <exception cref="InvalidOperationException">Throws if Local Repository is not set.</exception>
2734
public virtual GitObject Target
2835
{
2936
get { return targetBuilder.Value; }

LibGit2Sharp/LibGit2Sharp.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@
139139
<Compile Include="FilteringOptions.cs" />
140140
<Compile Include="MergeFetchHeadNotFoundException.cs" />
141141
<Compile Include="RemoteHead.cs" />
142-
<Compile Include="RemoteRepository.cs" />
143142
<Compile Include="RepositoryOperationContext.cs" />
144143
<Compile Include="ResetMode.cs" />
145144
<Compile Include="NoteCollectionExtensions.cs" />

LibGit2Sharp/Reference.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ private string DebuggerDisplay
199199
}
200200
}
201201

202-
IRepository IBelongToARepository.Repository { get { return repo; } }
202+
IRepository IBelongToARepository.Repository
203+
{
204+
get
205+
{
206+
if (repo == null)
207+
throw new InvalidOperationException("Repository requires a local repository");
208+
209+
return repo;
210+
}
211+
}
203212
}
204213
}

LibGit2Sharp/RemoteRepository.cs

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

LibGit2Sharp/Repository.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,52 @@ internal Commit LookupCommit(string committish)
545545
LookUpOptions.ThrowWhenCanNotBeDereferencedToACommit);
546546
}
547547

548+
/// <summary>
549+
/// Lists the Remote Repository References.
550+
/// </summary>
551+
/// <para>
552+
/// Does not require a local Repository. The retrieved
553+
/// <see cref="IBelongToARepository.Repository"/>
554+
/// throws <see cref="InvalidOperationException"/> in this case.
555+
/// </para>
556+
/// <param name="url">The url to list from.</param>
557+
/// <returns>The references in the remote repository.</returns>
558+
public static IEnumerable<Reference> ListRemoteReferences(string url)
559+
{
560+
return ListRemoteReferences(url, null);
561+
}
562+
563+
/// <summary>
564+
/// Lists the Remote Repository References.
565+
/// </summary>
566+
/// <para>
567+
/// Does not require a local Repository. The retrieved
568+
/// <see cref="IBelongToARepository.Repository"/>
569+
/// throws <see cref="InvalidOperationException"/> in this case.
570+
/// </para>
571+
/// <param name="url">The url to list from.</param>
572+
/// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
573+
/// <returns>The references in the remote repository.</returns>
574+
public static IEnumerable<Reference> ListRemoteReferences(string url, CredentialsHandler credentialsProvider)
575+
{
576+
Ensure.ArgumentNotNull(url, "url");
577+
578+
using (RepositorySafeHandle repositoryHandle = Proxy.git_repository_new())
579+
using (RemoteSafeHandle remoteHandle = Proxy.git_remote_create_anonymous(repositoryHandle, url))
580+
{
581+
var gitCallbacks = new GitRemoteCallbacks { version = 1 };
582+
583+
if (credentialsProvider != null)
584+
{
585+
var callbacks = new RemoteCallbacks(credentialsProvider);
586+
gitCallbacks = callbacks.GenerateCallbacks();
587+
}
588+
589+
Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks);
590+
return Proxy.git_remote_ls(null, remoteHandle);
591+
}
592+
}
593+
548594
/// <summary>
549595
/// Probe for a git repository.
550596
/// <para>The lookup start from <paramref name="startingPath"/> and walk upward parent directories if nothing has been found.</para>

0 commit comments

Comments
 (0)