diff options
author | nulltoken <emeric.fermas@gmail.com> | 2013-01-23 21:13:47 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2013-01-29 02:08:46 +0400 |
commit | 51d0754a90aad8d38e5e43624c5ded4363198751 (patch) | |
tree | fbee9fdeae8917ebdfba94dac078c980f56720ed | |
parent | 258fa7e3412f63e1c620850943f8705128c491ae (diff) |
Teach Branch to cope with unborn tracked branches
-rw-r--r-- | LibGit2Sharp.Tests/BranchFixture.cs | 19 | ||||
-rw-r--r-- | LibGit2Sharp/Branch.cs | 30 | ||||
-rw-r--r-- | LibGit2Sharp/Core/NativeMethods.cs | 8 | ||||
-rw-r--r-- | LibGit2Sharp/Core/Proxy.cs | 18 | ||||
-rw-r--r-- | LibGit2Sharp/LibGit2Sharp.csproj | 1 | ||||
-rw-r--r-- | LibGit2Sharp/VoidReference.cs | 14 |
6 files changed, 67 insertions, 23 deletions
diff --git a/LibGit2Sharp.Tests/BranchFixture.cs b/LibGit2Sharp.Tests/BranchFixture.cs index 5e6fbea8..34155144 100644 --- a/LibGit2Sharp.Tests/BranchFixture.cs +++ b/LibGit2Sharp.Tests/BranchFixture.cs @@ -603,5 +603,24 @@ namespace LibGit2Sharp.Tests Assert.Null(repo.Head.TrackedBranch); } } + + [Fact] + public void RemoteBranchesDoNotTrackAnything() + { + using (var repo = new Repository(StandardTestRepoPath)) + { + var branches = repo.Branches.Where(b => b.IsRemote); + + foreach (var branch in branches) + { + Assert.True(branch.IsRemote); + Assert.Null(branch.Remote); + Assert.False(branch.IsTracking); + Assert.Null(branch.TrackedBranch); + Assert.Null(branch.AheadBy); + Assert.Null(branch.BehindBy); + } + } + } } } diff --git a/LibGit2Sharp/Branch.cs b/LibGit2Sharp/Branch.cs index da7e8dd7..97a2c270 100644 --- a/LibGit2Sharp/Branch.cs +++ b/LibGit2Sharp/Branch.cs @@ -214,24 +214,26 @@ namespace LibGit2Sharp private Branch ResolveTrackedBranch() { - using (ReferenceSafeHandle branchPtr = repo.Refs.RetrieveReferencePtr(CanonicalName, false)) + if (IsRemote) { - if (branchPtr == null) - { - return null; - } + return null; + } - using (ReferenceSafeHandle referencePtr = Proxy.git_branch_tracking(branchPtr)) - { - if (referencePtr == null) - { - return null; - } + string trackedReferenceName = Proxy.git_branch_tracking_name(repo.Handle, CanonicalName); - var reference = Reference.BuildFromPtr<Reference>(referencePtr, repo); - return repo.Branches[reference.CanonicalName]; - } + if (trackedReferenceName == null) + { + return null; + } + + Branch branch = repo.Branches[trackedReferenceName]; + + if (branch != null) + { + return branch; } + + return new Branch(repo, new VoidReference(trackedReferenceName), trackedReferenceName); } private static bool IsRemoteBranch(string canonicalName) diff --git a/LibGit2Sharp/Core/NativeMethods.cs b/LibGit2Sharp/Core/NativeMethods.cs index 7554ea0d..22a08581 100644 --- a/LibGit2Sharp/Core/NativeMethods.cs +++ b/LibGit2Sharp/Core/NativeMethods.cs @@ -142,9 +142,11 @@ namespace LibGit2Sharp.Core [MarshalAs(UnmanagedType.Bool)] bool force); [DllImport(libgit2)] - internal static extern int git_branch_tracking( - out ReferenceSafeHandle reference, - ReferenceSafeHandle branch); + internal static extern int git_branch_tracking_name( + byte[] tracking_branch_name_out, // NB: This is more properly a StringBuilder, but it's UTF8 + UIntPtr buffer_size, + RepositorySafeHandle repo, + [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(Utf8Marshaler))] string referenceName); [DllImport(libgit2)] internal static extern int git_checkout_tree( diff --git a/LibGit2Sharp/Core/Proxy.cs b/LibGit2Sharp/Core/Proxy.cs index c26dcdae..cb45a1b1 100644 --- a/LibGit2Sharp/Core/Proxy.cs +++ b/LibGit2Sharp/Core/Proxy.cs @@ -148,21 +148,27 @@ namespace LibGit2Sharp.Core } } - public static ReferenceSafeHandle git_branch_tracking(ReferenceSafeHandle branch) + public static string git_branch_tracking_name(RepositorySafeHandle handle, string canonicalReferenceName) { using (ThreadAffinity()) { - ReferenceSafeHandle reference; - int res = NativeMethods.git_branch_tracking(out reference, branch); + int bufSize = NativeMethods.git_branch_tracking_name( + null, UIntPtr.Zero, handle, canonicalReferenceName); - if (res == (int)GitErrorCode.NotFound) + if (bufSize == (int)GitErrorCode.NotFound) { return null; } - Ensure.Success(res); + Ensure.Success(bufSize, true); - return reference; + var buffer = new byte[bufSize]; + + int res = NativeMethods.git_branch_tracking_name( + buffer, (UIntPtr)buffer.Length, handle, canonicalReferenceName); + Ensure.Success(res, true); + + return Utf8Marshaler.Utf8FromBuffer(buffer); } } diff --git a/LibGit2Sharp/LibGit2Sharp.csproj b/LibGit2Sharp/LibGit2Sharp.csproj index 38be381e..b659dbcb 100644 --- a/LibGit2Sharp/LibGit2Sharp.csproj +++ b/LibGit2Sharp/LibGit2Sharp.csproj @@ -209,6 +209,7 @@ <Compile Include="TreeDefinition.cs" /> <Compile Include="TreeEntry.cs" /> <Compile Include="TreeEntryDefinition.cs" /> + <Compile Include="VoidReference.cs" /> </ItemGroup> <ItemGroup> <CodeAnalysisDictionary Include="CustomDictionary.xml" /> diff --git a/LibGit2Sharp/VoidReference.cs b/LibGit2Sharp/VoidReference.cs new file mode 100644 index 00000000..b8defc8c --- /dev/null +++ b/LibGit2Sharp/VoidReference.cs @@ -0,0 +1,14 @@ +namespace LibGit2Sharp +{ + internal class VoidReference : Reference + { + internal VoidReference(string canonicalName) + : base(canonicalName, null) + { } + + public override DirectReference ResolveToDirectReference() + { + return null; + } + } +} |