Permalink
Browse files

Delegate tracking reference retrieval to libgit2

  • Loading branch information...
1 parent ed35167 commit 38fdee6877c7cbdc73daffe32997cb3f6c540ee0 @nulltoken nulltoken committed Sep 10, 2012
Showing with 33 additions and 22 deletions.
  1. +10 −22 LibGit2Sharp/Branch.cs
  2. +5 −0 LibGit2Sharp/Core/NativeMethods.cs
  3. +18 −0 LibGit2Sharp/Core/Proxy.cs
View
@@ -1,7 +1,9 @@
using System;
using System.Globalization;
using System.Linq;
+using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;
+using LibGit2Sharp.Core.Handles;
namespace LibGit2Sharp
{
@@ -138,31 +140,17 @@ public virtual ICommitLog Commits
private Branch ResolveTrackedBranch()
{
- var trackedRemote = repo.Config.Get<string>("branch", Name, "remote", null);
- if (trackedRemote == null)
+ using (ReferenceSafeHandle branchPtr = repo.Refs.RetrieveReferencePtr(CanonicalName))
+ using (ReferenceSafeHandle referencePtr = Proxy.git_branch_tracking(branchPtr))
{
- return null;
- }
-
- var trackedRefName = repo.Config.Get<string>("branch", Name, "merge", null);
- if (trackedRefName == null)
- {
- return null;
- }
-
- var remoteRefName = ResolveTrackedReference(trackedRemote, trackedRefName);
- return repo.Branches[remoteRefName];
- }
+ if (referencePtr == null)
+ {
+ return null;
+ }
- private static string ResolveTrackedReference(string trackedRemote, string trackedRefName)
- {
- if (trackedRemote == ".")
- {
- return trackedRefName;
+ var reference = Reference.BuildFromPtr<Reference>(referencePtr, repo);
+ return repo.Branches[reference.CanonicalName];
}
-
- //TODO: To be replaced by native libgit2 git_branch_tracked_reference() when available.
- return trackedRefName.Replace("refs/heads/", string.Concat("refs/remotes/", trackedRemote, "/"));
}
private static bool IsRemoteBranch(string canonicalName)
@@ -130,6 +130,11 @@ private static bool IsRunningOnLinux()
[MarshalAs(UnmanagedType.Bool)] bool force);
[DllImport(libgit2)]
+ internal static extern int git_branch_tracking(
+ out ReferenceSafeHandle reference,
+ ReferenceSafeHandle branch);
+
+ [DllImport(libgit2)]
internal static extern IntPtr git_commit_author(GitObjectSafeHandle commit);
[DllImport(libgit2)]
View
@@ -125,6 +125,24 @@ public static void git_branch_move(ReferenceSafeHandle reference, string new_bra
}
}
+ public static ReferenceSafeHandle git_branch_tracking(ReferenceSafeHandle branch)
+ {
+ using (ThreadAffinity())
+ {
+ ReferenceSafeHandle reference;
+ int res = NativeMethods.git_branch_tracking(out reference, branch);
+
+ if (res == (int)GitErrorCode.NotFound)
+ {
+ return null;
+ }
+
+ Ensure.Success(res);
+
+ return reference;
+ }
+ }
+
#endregion
#region git_commit_

0 comments on commit 38fdee6

Please sign in to comment.