Skip to content

Add repo.Info.IsHeadOrphaned #179

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 2 commits into from
Jun 12, 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
19 changes: 19 additions & 0 deletions LibGit2Sharp.Tests/RepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ private static void AssertInitializedRepository(Repository repo)
Assert.NotNull(repo.Info.Path);
Assert.True(repo.Info.IsEmpty);
Assert.False(repo.Info.IsHeadDetached);
Assert.True(repo.Info.IsHeadOrphaned);

Reference headRef = repo.Refs["HEAD"];
Assert.NotNull(headRef);
Expand Down Expand Up @@ -356,5 +357,23 @@ public void DiscoverReturnsNullWhenNoRepoCanBeFound()

File.Delete(path);
}

[Fact]
public void CanDetectIfTheHeadIsOrphaned()
{
TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath);
using (var repo = new Repository(path.RepositoryPath))
{
string branchName = repo.Head.CanonicalName;

Assert.False(repo.Info.IsHeadOrphaned);

repo.Refs.Add("HEAD", "refs/heads/orphan", true);
Assert.True(repo.Info.IsHeadOrphaned);

repo.Refs.Add("HEAD", branchName, true);
Assert.False(repo.Info.IsHeadOrphaned);
}
}
}
}
2 changes: 2 additions & 0 deletions LibGit2Sharp/Core/Compat/Lazy.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.Diagnostics;

namespace LibGit2Sharp.Core.Compat
{
/// <summary>
/// Provides support for lazy initialization.
/// </summary>
/// <typeparam name = "TType">Specifies the type of object that is being lazily initialized.</typeparam>
[DebuggerStepThrough]
public class Lazy<TType>
{
private readonly Func<TType> evaluator;
Expand Down
3 changes: 3 additions & 0 deletions LibGit2Sharp/Core/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,9 @@ public static extern int git_repository_discover(
[DllImport(libgit2)]
public static extern int git_repository_head_detached(RepositorySafeHandle repo);

[DllImport(libgit2)]
public static extern int git_repository_head_orphan(RepositorySafeHandle repo);

[DllImport(libgit2)]
public static extern int git_repository_index(out IndexSafeHandle index, RepositorySafeHandle repo);

Expand Down
8 changes: 8 additions & 0 deletions LibGit2Sharp/RepositoryInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,13 @@ public bool IsHeadDetached
{
get { return NativeMethods.RepositoryStateChecker(repo.Handle, NativeMethods.git_repository_head_detached); }
}

/// <summary>
/// Indicates whether the Head points to a reference which doesn't exist.
/// </summary>
public bool IsHeadOrphaned
{
get { return NativeMethods.RepositoryStateChecker(repo.Handle, NativeMethods.git_repository_head_orphan); }
}
}
}