Skip to content
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

Add implementation for git_remote_default_branch #1969

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 38 additions & 0 deletions LibGit2Sharp.Tests/NetworkFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,44 @@ namespace LibGit2Sharp.Tests
{
public class NetworkFixture : BaseFixture
{
[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
[InlineData("https://github.com/libgit2/TestGitRepository")]
public void CanFetchDefaultBranchName(string url)
{
string remoteName = "testRemote";

string expectedDefaultBranchName = TestRemoteRefs.ExpectedRemoteRefs
.First(remoteRef => remoteRef.Item3).Item1;

string repoPath = InitNewRepository();

using (var repo = new Repository(repoPath))
{
Remote remote = repo.Network.Remotes.Add(remoteName, url);

string defaultBranchName = repo.Network.DefaultBranchName(remote);
Assert.Equal(expectedDefaultBranchName, defaultBranchName);
}
}

[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
[InlineData("https://github.com/libgit2/TestGitRepository")]
public void CanFetchDefaultBranchNameFromUrl(string url)
{
string expectedDefaultBranchName = TestRemoteRefs.ExpectedRemoteRefs
.First(remoteRef => remoteRef.Item3).Item1;

string repoPath = InitNewRepository();

using (var repo = new Repository(repoPath))
{
string defaultBranchName = repo.Network.DefaultBranchName(url);
Assert.Equal(expectedDefaultBranchName, defaultBranchName);
}
}

[Theory]
[InlineData("http://github.com/libgit2/TestGitRepository")]
[InlineData("https://github.com/libgit2/TestGitRepository")]
Expand Down
20 changes: 10 additions & 10 deletions LibGit2Sharp.Tests/TestHelpers/TestRemoteRefs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ public class TestRemoteRefs
/// <summary>
/// Expected references on http://github.com/libgit2/TestGitRepository
/// </summary>
public static List<Tuple<string, string>> ExpectedRemoteRefs = new List<Tuple<string, string>>()
public static List<Tuple<string, string, bool>> ExpectedRemoteRefs = new List<Tuple<string, string, bool>>()
{
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"),
new Tuple<string, string, bool>("HEAD", "49322bb17d3acc9146f98c97d078513228bbf3c0", false),
new Tuple<string, string, bool>("refs/heads/first-merge", "0966a434eb1a025db6b71485ab63a3bfbea520b6", false),
new Tuple<string, string, bool>("refs/heads/master", "49322bb17d3acc9146f98c97d078513228bbf3c0", true),
new Tuple<string, string, bool>("refs/heads/no-parent", "42e4e7c5e507e113ebbb7801b16b52cf867b7ce1", false),
new Tuple<string, string, bool>("refs/tags/annotated_tag", "d96c4e80345534eccee5ac7b07fc7603b56124cb", false),
new Tuple<string, string, bool>("refs/tags/annotated_tag^{}", "c070ad8c08840c8116da865b2d65593a6bb9cd2a", false),
new Tuple<string, string, bool>("refs/tags/blob", "55a1a760df4b86a02094a904dfa511deb5655905", false),
new Tuple<string, string, bool>("refs/tags/commit_tree", "8f50ba15d49353813cc6e20298002c0d17b0a9ee", false),
new Tuple<string, string, bool>("refs/tags/nearly-dangling", "6e0c7bdb9b4ed93212491ee778ca1c65047cab4e", false),
};
}
}
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,9 @@ internal static extern unsafe int git_remote_create_with_fetchspec(
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string url,
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string refspec);

[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int git_remote_default_branch(GitBuf buf, git_remote* remote);

[DllImport(libgit2, CallingConvention = CallingConvention.Cdecl)]
internal static extern unsafe int git_remote_delete(
git_repository* repo,
Expand Down
17 changes: 17 additions & 0 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,23 @@ public static unsafe void git_remote_connect(RemoteHandle remote, GitDirection d
}
}

public static unsafe string git_remote_default_branch(RemoteHandle remote)
{
using (var buf = new GitBuf())
{
int res = NativeMethods.git_remote_default_branch(buf, remote);

if (res == (int)GitErrorCode.NotFound)
{
return null;
}

Ensure.ZeroResult(res);

return LaxUtf8Marshaler.FromNative(buf.ptr) ?? string.Empty;
}
}

public static unsafe void git_remote_delete(RepositoryHandle repo, string name)
{
int res = NativeMethods.git_remote_delete(repo, name);
Expand Down
70 changes: 70 additions & 0 deletions LibGit2Sharp/Network.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,76 @@ public virtual RemoteCollection Remotes
get { return remotes.Value; }
}

/// <summary>
/// Lookup the default branch name in a Remote repository
/// </summary>
/// <param name="remote"><The <see cref="Remote"/> to get the default branch from.</param>
/// <returns>The canonical name of the Remote repository's default branch.</returns>
public virtual string DefaultBranchName(Remote remote)
{
Ensure.ArgumentNotNull(remote, "remote");

return DefaultBranchNameInternal(remote.Url, null);
}

/// <summary>
/// Lookup the default branch name in a Remote repository
/// </summary>
/// <param name="remote"><The <see cref="Remote"/> to get the default branch from.</param>
/// <param name="remote"><The <see cref="Func{Credentials}"/> used to connect to the remote repository.</param>
/// <returns>The canonical name of the Remote repository's default branch.</returns>
public virtual string DefaultBranchName(Remote remote, CredentialsHandler credentialsProvider)
{
Ensure.ArgumentNotNull(remote, "remote");
Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider");

return DefaultBranchNameInternal(remote.Url, credentialsProvider);
}

/// <summary>
/// Lookup the default branch name in a Remote repository
/// </summary>
/// <param name="remote"><The url to get the default branch from.</param>
/// <returns>The canonical name of the Remote repository's default branch.</returns>
public virtual string DefaultBranchName(string url)
{
Ensure.ArgumentNotNull(url, "url");

return DefaultBranchNameInternal(url, null);
}

/// <summary>
/// Lookup the default branch name in a Remote repository
/// </summary>
/// <param name="remote"><The url to get the default branch from.</param>
/// <param name="remote"><The <see cref="Func{Credentials}"/> used to connect to the remote repository.</param>
/// <returns>The canonical name of the Remote repository's default branch.</returns>
public virtual string DefaultBranchName(string url, CredentialsHandler credentialsProvider)
{
Ensure.ArgumentNotNull(url, "url");
Ensure.ArgumentNotNull(credentialsProvider, "credentialsProvider");

return DefaultBranchNameInternal(url, credentialsProvider);
}

private string DefaultBranchNameInternal(string url, CredentialsHandler credentialsProvider)
{
using (RemoteHandle remoteHandle = BuildRemoteHandle(repository.Handle, url))
{
GitRemoteCallbacks gitCallbacks = new GitRemoteCallbacks { version = 1 };
GitProxyOptions proxyOptions = new GitProxyOptions { Version = 1 };

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

Proxy.git_remote_connect(remoteHandle, GitDirection.Fetch, ref gitCallbacks, ref proxyOptions);
return Proxy.git_remote_default_branch(remoteHandle);
}
}

/// <summary>
/// List references in a <see cref="Remote"/> repository.
/// <para>
Expand Down