Skip to content

Ref slash prefix workaround (empty branches collection) #108

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

Merged
merged 1 commit into from
Feb 3, 2012
Merged
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
15 changes: 15 additions & 0 deletions LibGit2Sharp.Tests/BranchFixture.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Linq;
using LibGit2Sharp.Tests.TestHelpers;
using NUnit.Framework;
Expand Down Expand Up @@ -167,6 +168,20 @@ public void CanListAllBranches()
}
}

[Test]
public void CanListAllBranchesWhenGivenWorkingDir()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.DirectoryPath))
{
var expectedWdBranches = new[] { "master", "origin/HEAD", "origin/br2", "origin/master", "origin/packed-test", "origin/test" };

CollectionAssert.AreEqual(expectedWdBranches, repo.Branches.Select(b => b.Name).ToArray());

repo.Branches.Count().ShouldEqual(6);
}
}

[Test]
public void CanListAllBranchesIncludingRemoteRefs()
{
Expand Down
20 changes: 18 additions & 2 deletions LibGit2Sharp/Repository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using LibGit2Sharp.Core;
using LibGit2Sharp.Core.Compat;

Expand All @@ -22,13 +23,28 @@ public class Repository : IDisposable

/// <summary>
/// Initializes a new instance of the <see cref = "Repository" /> class.
/// <para>For a standard repository, <paramref name = "path" /> should point to the ".git" folder. For a bare repository, <paramref name = "path" /> should directly point to the repository folder.</para>
/// <para>For a standard repository, <paramref name = "path" /> should either point to the ".git" folder or to the working directory. For a bare repository, <paramref name = "path" /> should directly point to the repository folder.</para>
/// </summary>
/// <param name = "path">The path to the git repository to open.</param>
/// <param name = "path">
/// The path to the git repository to open, can be either the path to the git directory (for non-bare repositories this
/// would be the ".git" folder inside the working directory) or the path to the working directory.
/// </param>
public Repository(string path)
{
Ensure.ArgumentNotNullOrEmptyString(path, "path");

// Check if the path points to the working directory instead of the git directory
// by checking if the directory contains a .git directory. The same test is done
// in libgit2 but if it gets to add .git to the path it will mess up the ref paths
// returned from git_reference_listall (and more?) by prefixing them with a '/' such
// that what would normally be refs/heads/master becomes /refs/heads/master and
// LibGit2Sharp doesn't expect that. This is a workaround.
// See https://github.com/libgit2/libgit2sharp/pull/108
string gitDirPath = Path.Combine(path, ".git");

if (Directory.Exists(gitDirPath))
path = gitDirPath;

int res = NativeMethods.git_repository_open(out handle, PosixPathHelper.ToPosix(path));
Ensure.Success(res);

Expand Down