8000 Allow retrieval of the Remote of a non local branch · mm201/libgit2sharp@4e43723 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4e43723

Browse files
committed
Allow retrieval of the Remote of a non local branch
1 parent 8cbfcb3 commit 4e43723

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

LibGit2Sharp.Tests/BranchFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ public void RemoteBranchesDoNotTrackAnything()
675675
foreach (var branch in branches)
676676
{
677677
Assert.True(branch.IsRemote);
678-
Assert.Null(branch.Remote);
678+
Assert.NotNull(branch.Remote);
679679
Assert.False(branch.IsTracking);
680680
Assert.Null(branch.TrackedBranch);
681681
Assert.Null(branch.AheadBy);

LibGit2Sharp/Branch.cs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Globalization;
3-
using System.Linq;
43
using LibGit2Sharp.Core;
54
using LibGit2Sharp.Core.Compat;
65
using LibGit2Sharp.Core.Handles;
@@ -174,25 +173,55 @@ public virtual Remote Remote
174173
{
175174
get
176175
{
177-
ConfigurationEntry<string> remoteEntry = repo.Config.Get<string>("branch", Name, "remote");
176+
string remoteName;
178177

179-
if (remoteEntry == null)
178+
if (IsRemote)
180179
{
181-
return null;
180+
remoteName = RemoteNameFromRemoteTrackingBranch();
182181
}
183-
184-
string remoteName = remoteEntry.Value;
185-
186-
if (string.IsNullOrEmpty(remoteName) ||
187-
string.Equals(remoteName, ".", StringComparison.Ordinal))
182+
else
188183
{
189-
return null;
184+
remoteName = RemoteNameFromLocalBranch();
185+
186+
if (remoteName == null)
187+
{
188+
return null;
189+
}
190190
}
191191

192192
return repo.Network.Remotes[remoteName];
193193
}
194194
}
195195

196+
private string RemoteNameFromLocalBranch()
197+
{
198+
ConfigurationEntry<string> remoteEntry = repo.Config.Get<string>("branch", Name, "remote");
199+
200+
if (remoteEntry == null)
201+
{
202+
return null;
203+
}
204+
205+
string remoteName = remoteEntry.Value;
206+
207+
if (string.IsNullOrEmpty(remoteName) ||
208+
string.Equals(remoteName, ".", StringComparison.Ordinal))
209+
{
210+
return null;
211+
}
212+
213+
return remoteName;
214+
215+
}
216+
217+
private string RemoteNameFromRemoteTrackingBranch()
218+
{
219+
using (ReferenceSafeHandle branchPtr = repo.Refs.RetrieveReferencePtr(CanonicalName))
220+
{
221+
return Proxy.git_branch_remote_name(repo.Handle, branchPtr);
222+
}
223+
}
224+
196225
/// <summary>
197226
/// Checkout the tip commit of this <see cref = "Branch" /> object.
198227
/// If this commit is the current tip of the branch, will checkout

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,13 @@ internal static extern int git_branch_move(
141141
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string new_branch_name,
142142
[MarshalAs(UnmanagedType.Bool)] bool force);
143143

144+
[DllImport(libgit2)]
145+
internal static extern int git_branch_remote_name(
146+
byte[] remote_name_out,
147+
UIntPtr buffer_size,
148+
RepositorySafeHandle repo,
149+
ReferenceSafeHandle branch);
150+
144151
[DllImport(libgit2)]
145152
internal static extern int git_branch_tracking_name(
146153
byte[] tracking_branch_name_out, // NB: This is more properly a StringBuilder, but it's UTF8

LibGit2Sharp/Core/Proxy.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ public static void git_branch_move(ReferenceSafeHandle reference, string new_bra
144144
}
145145
}
146146

147+
public static string git_branch_remote_name(RepositorySafeHandle repo, ReferenceSafeHandle branch)
148+
{
149+
using (ThreadAffinity())
150+
{
151+
int bufSize = NativeMethods.git_branch_remote_name(null, UIntPtr.Zero, repo, branch);
152+
Ensure.Int32Result(bufSize);
153+
154+
var buffer = new byte[bufSize];
155+
156+
int res = NativeMethods.git_branch_remote_name(buffer, (UIntPtr)buffer.Length, repo, branch);
157+
Ensure.Int32Result(res);
158+
159+
return Utf8Marshaler.Utf8FromBuffer(buffer) ?? string.Empty;
160+
}
161+
}
162+
147163
public static string git_branch_tracking_name(RepositorySafeHandle handle, string canonicalReferenceName)
148164
{
149165
using (ThreadAffinity())

0 commit comments

Comments
 (0)
0