Skip to content

Commit 2bb95e7

Browse files
committed
Initial remote ls symref commit,
still work in progress
1 parent 49346f8 commit 2bb95e7

File tree

6 files changed

+71
-27
lines changed

6 files changed

+71
-27
lines changed

LibGit2Sharp.Tests/NetworkFixture.cs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ public void CanListRemoteReferences(string url)
2222
using (var repo = new Repository(repoPath))
2323
{
2424
Remote remote = repo.Network.Remotes.Add(remoteName, url);
25-
IList<DirectReference> references = repo.Network.ListReferences(remote).ToList();
25+
IList<Reference> references = repo.Network.ListReferences(remote).ToList();
2626

27-
foreach (var directReference in references)
27+
foreach (var reference in references)
2828
{
2929
// None of those references point to an existing
3030
// object in this brand new repository
31-
Assert.Null(directReference.Target);
31+
var directReference = reference as DirectReference;
32+
33+
if (directReference != null)
34+
{
35+
Assert.Null(directReference.Target);
36+
}
3237
}
3338

3439
List<Tuple<string, string>> actualRefs = references.
@@ -53,13 +58,18 @@ public void CanListRemoteReferencesFromUrl(string url)
5358

5459
using (var repo = new Repository(repoPath))
5560
{
56-
IList<DirectReference> references = repo.Network.ListReferences(url).ToList();
61+
IList<Reference> references = repo.Network.ListReferences(url).ToList();
5762

58-
foreach (var directReference in references)
63+
foreach (var reference in references)
5964
{
6065
// None of those references point to an existing
6166
// object in this brand new repository
62-
Assert.Null(directReference.Target);
67+
var directReference = reference as DirectReference;
68+
69+
if (directReference != null)
70+
{
71+
Assert.Null(directReference.Target);
72+
}
6373
}
6474

6575
List<Tuple<string, string>> actualRefs = references.
@@ -87,15 +97,21 @@ public void CanListRemoteReferenceObjects()
8797
using (var repo = new Repository(clonedRepoPath))
8898
{
8999
Remote remote = repo.Network.Remotes[remoteName];
90-
IEnumerable<DirectReference> references = repo.Network.ListReferences(remote);
100+
IEnumerable<Reference> references = repo.Network.ListReferences(remote);
91101

92102
var actualRefs = new List<Tuple<string,string>>();
93103

94-
foreach(DirectReference reference in references)
104+
foreach(Reference reference in references)
95105
{
96106
Assert.NotNull(reference.CanonicalName);
97-
Assert.NotNull(reference.Target);
98-
actualRefs.Add(new Tuple<string, string>(reference.CanonicalName, reference.Target.Id.Sha));
107+
108+
var directReference = reference as DirectReference;
109+
110+
if (directReference != null)
111+
{
112+
Assert.NotNull(directReference.Target);
113+
actualRefs.Add(new Tuple<string, string>(reference.CanonicalName, directReference.Target.Id.Sha));
114+
}
99115
}
100116

101117
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs.Count, actualRefs.Count);

LibGit2Sharp.Tests/PushFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void CanForcePush()
199199
private static void AssertRemoteHeadTipEquals(IRepository localRepo, string sha)
200200
{
201201
var remoteReferences = localRepo.Network.ListReferences(localRepo.Network.Remotes.Single());
202-
DirectReference remoteHead = remoteReferences.Single(r => r.CanonicalName == "HEAD");
202+
Reference remoteHead = remoteReferences.Single(r => r is SymbolicReference && r.CanonicalName == "HEAD");
203203

204204
Assert.Equal(sha, remoteHead.TargetIdentifier);
205205
}

LibGit2Sharp.Tests/RepositoryFixture.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ public void CanListRemoteReferencesWithCredentials()
659659
InconclusiveIf(() => string.IsNullOrEmpty(Constants.PrivateRepoUrl),
660660
"Populate Constants.PrivateRepo* to run this test");
661661

662-
IEnumerable<DirectReference> references = Repository.ListRemoteReferences(Constants.PrivateRepoUrl,
662+
IEnumerable<Reference> references = Repository.ListRemoteReferences(Constants.PrivateRepoUrl,
663663
Constants.PrivateRepoCredentials);
664664

665665
foreach (var reference in references)
@@ -674,11 +674,12 @@ public void CanListRemoteReferencesWithCredentials()
674674
[InlineData("git://github.com/libgit2/TestGitRepository.git")]
675675
public void CanListRemoteReferences(string url)
676676
{
677-
IEnumerable<DirectReference> references = Repository.ListRemoteReferences(url);
677+
IEnumerable<Reference> references = Repository.ListRemoteReferences(url).ToList();
678678

679679
List<Tuple<string, string>> actualRefs = references.
680-
Select(directRef => new Tuple<string, string>(directRef.CanonicalName, directRef.TargetIdentifier)).ToList();
680+
Select(reference => new Tuple<string, string>(reference.CanonicalName, reference.TargetIdentifier)).ToList();
681681

682+
Assert.True(references.Any(reference => reference is SymbolicReference));
682683
Assert.Equal(TestRemoteRefs.ExpectedRemoteRefs.Count, actualRefs.Count);
683684
for (int i = 0; i < TestRemoteRefs.ExpectedRemoteRefs.Count; i++)
684685
{
@@ -691,7 +692,7 @@ public void CanListRemoteReferences(string url)
691692
[InlineData("http://github.com/libgit2/TestGitRepository")]
692693
public void ReadingReferenceRepositoryThroughListRemoteReferencesThrows(string url)
693694
{
694-
IEnumerable<DirectReference> references = Repository.ListRemoteReferences(url);
695+
IEnumerable<Reference> references = Repository.ListRemoteReferences(url);
695696

696697
foreach (var reference in references)
697698
{
@@ -704,11 +705,21 @@ public void ReadingReferenceRepositoryThroughListRemoteReferencesThrows(string u
704705
[InlineData("http://github.com/libgit2/TestGitRepository")]
705706
public void ReadingReferenceTargetFromListRemoteReferencesThrows(string url)
706707
{
707-
IEnumerable<DirectReference> references = Repository.ListRemoteReferences(url);
708+
IEnumerable<Reference> references = Repository.ListRemoteReferences(url);
708709

709710
foreach (var reference in references)
710711
{
711-
Assert.Throws<InvalidOperationException>(() => reference.Target);
712+
Assert.Throws<InvalidOperationException>(() =>
713+
{
714+
var directReference = reference as DirectReference;
715+
716+
if (directReference != null)
717+
{
718+
var target = directReference.Target;
719+
}
720+
721+
throw new InvalidOperationException();
722+
});
712723
}
713724
}
714725
}

LibGit2Sharp/Core/Proxy.cs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,7 +1958,7 @@ public static IList<string> git_remote_list(RepositorySafeHandle repo)
19581958
}
19591959
}
19601960

1961-
public static IEnumerable<DirectReference> git_remote_ls(Repository repository, RemoteSafeHandle remote)
1961+
public static IEnumerable<Reference> git_remote_ls(Repository repository, RemoteSafeHandle remote)
19621962
{
19631963
IntPtr heads;
19641964
UIntPtr count;
@@ -1973,22 +1973,39 @@ public static IEnumerable<DirectReference> git_remote_ls(Repository repository,
19731973
throw new OverflowException();
19741974
}
19751975

1976-
var refs = new List<DirectReference>();
1976+
var refs = new List<Reference>();
19771977
IntPtr currentHead = heads;
19781978

19791979
for (int i = 0; i < intCount; i++)
19801980
{
19811981
var remoteHead = Marshal.ReadIntPtr(currentHead).MarshalAs<GitRemoteHead>();
1982+
string name = LaxUtf8Marshaler.FromNative(remoteHead.NamePtr);
19821983

19831984
// The name pointer should never be null - if it is,
19841985
// this indicates a bug somewhere (libgit2, server, etc).
1985-
if (remoteHead.NamePtr == IntPtr.Zero)
1986+
if (string.IsNullOrEmpty(name))
19861987
{
19871988
throw new InvalidOperationException("Not expecting null value for reference name.");
19881989
}
19891990

1990-
string name = LaxUtf8Marshaler.FromNative(remoteHead.NamePtr);
1991-
refs.Add(new DirectReference(name, repository, remoteHead.Oid));
1991+
string symRefTarget = LaxUtf8Marshaler.FromNative(remoteHead.SymRefTargetPtr);
1992+
1993+
if (!string.IsNullOrEmpty(symRefTarget) && Reference.IsValidName(name))
1994+
{
1995+
//ReferenceSafeHandle handle;
1996+
//NativeMethods.git_reference_lookup(out handle, repository.Handle, symRefTarget);
1997+
using (var handle = Proxy.git_reference_lookup(repository.Handle, symRefTarget, false))
1998+
{
1999+
var reference = NativeMethods.git_reference_symbolic_target(handle);
2000+
}
2001+
2002+
//var symbolicReference = new SymbolicReference(repository, name, symRefTarget, reference);
2003+
//refs.Add(symbolicReference);
2004+
}
2005+
else
2006+
{
2007+
refs.Add(new DirectReference(name, repository, remoteHead.Oid));
2008+
}
19922009

19932010
currentHead = IntPtr.Add(currentHead, IntPtr.Size);
19942011
}

LibGit2Sharp/Network.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public virtual RemoteCollection Remotes
4848
/// </summary>
4949
/// <param name="remote">The <see cref="Remote"/> to list from.</param>
5050
/// <returns>The references in the <see cref="Remote"/> repository.</returns>
51-
public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
51+
public virtual IEnumerable<Reference> ListReferences(Remote remote)
5252
{
5353
return ListReferences(remote, null);
5454
}
@@ -65,7 +65,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote)
6565
/// <param name="remote">The <see cref="Remote"/> to list from.</param>
6666
/// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
6767
/// <returns>The references in the <see cref="Remote"/> repository.</returns>
68-
public virtual IEnumerable<DirectReference> ListReferences(Remote remote, CredentialsHandler credentialsProvider)
68+
public virtual IEnumerable<Reference> ListReferences(Remote remote, CredentialsHandler credentialsProvider)
6969
{
7070
Ensure.ArgumentNotNull(remote, "remote");
7171

@@ -95,7 +95,7 @@ public virtual IEnumerable<DirectReference> ListReferences(Remote remote, Creden
9595
/// </summary>
9696
/// <param name="url">The url to list from.</param>
9797
/// <returns>The references in the remote repository.</returns>
98-
public virtual IEnumerable<DirectReference> ListReferences(string url)
98+
public virtual IEnumerable<Reference> ListReferences(string url)
9999
{
100100
Ensure.ArgumentNotNull(url, "url");
101101

LibGit2Sharp/Repository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ internal Commit LookupCommit(string committish)
555555
/// </para>
556556
/// <param name="url">The url to list from.</param>
557557
/// <returns>The references in the remote repository.</returns>
558-
public static IEnumerable<DirectReference> ListRemoteReferences(string url)
558+
public static IEnumerable<Reference> ListRemoteReferences(string url)
559559
{
560560
return ListRemoteReferences(url, null);
561561
}
@@ -571,7 +571,7 @@ public static IEnumerable<DirectReference> ListRemoteReferences(string url)
571571
/// <param name="url">The url to list from.</param>
572572
/// <param name="credentialsProvider">The <see cref="Func{Credentials}"/> used to connect to remote repository.</param>
573573
/// <returns>The references in the remote repository.</returns>
574-
public static IEnumerable<DirectReference> ListRemoteReferences(string url, CredentialsHandler credentialsProvider)
574+
public static IEnumerable<Reference> ListRemoteReferences(string url, CredentialsHandler credentialsProvider)
575575
{
576576
Ensure.ArgumentNotNull(url, "url");
577577

0 commit comments

Comments
 (0)