From 11db4c480ed4bfd2d30cde5a35617386424938e4 Mon Sep 17 00:00:00 2001 From: Jameson Miller Date: Tue, 26 Feb 2013 10:02:19 -0500 Subject: [PATCH 1/2] Allow commits on any unparented branch --- LibGit2Sharp.Tests/CommitFixture.cs | 23 +++++++++++++++++++++++ LibGit2Sharp/Repository.cs | 2 +- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index 1a20ab042..e832a3e29 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -814,5 +814,28 @@ public void CanCorrectlyDistinguishAuthorFromCommitter() Assert.Equal(committer, c.Committer); } } + + [Fact] + public void CanCommitOnOrphanedBranch() + { + string newBranchName = "refs/heads/newBranch"; + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + + using (var repo = Repository.Init(scd.DirectoryPath)) + { + // Set Head to point to branch other than master + repo.Refs.UpdateTarget("HEAD", newBranchName); + Assert.Equal(newBranchName, repo.Head.CanonicalName); + + const string relativeFilepath = "test.txt"; + string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); + + File.WriteAllText(filePath, "test\n"); + repo.Index.Stage(relativeFilepath); + + repo.Commit("Initial commit", DummySignature, DummySignature); + Assert.Equal(1, repo.Head.Commits.Count()); + } + } } } diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index f17276a6a..4b045a0a0 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -681,7 +681,7 @@ private IEnumerable RetrieveParentsOfTheCommitBeingCreated(bool amendPre return Head.Tip.Parents; } - if (Info.IsEmpty) + if (Info.IsHeadOrphaned) { return Enumerable.Empty(); } From 7ae59bc510204ddaf3e6c0dbfc6896738fe4dd18 Mon Sep 17 00:00:00 2001 From: Jameson Miller Date: Tue, 26 Feb 2013 10:54:42 -0500 Subject: [PATCH 2/2] Obsolete IsEmpty property on RepositoryInformation --- LibGit2Sharp.Tests/CommitFixture.cs | 2 +- LibGit2Sharp.Tests/RepositoryFixture.cs | 2 -- LibGit2Sharp.Tests/TagFixture.cs | 2 +- LibGit2Sharp/Repository.cs | 2 +- LibGit2Sharp/RepositoryInformation.cs | 4 +++- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index e832a3e29..268d7ca99 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -618,7 +618,7 @@ public void CanCommitALittleBit() AssertBlobContent(commit[relativeFilepath], "nulltoken\n"); Assert.Equal(0, commit.Parents.Count()); - Assert.False(repo.Info.IsEmpty); + Assert.False(repo.Info.IsHeadOrphaned); File.WriteAllText(filePath, "nulltoken commits!\n"); repo.Index.Stage(relativeFilepath); diff --git a/LibGit2Sharp.Tests/RepositoryFixture.cs b/LibGit2Sharp.Tests/RepositoryFixture.cs index c43607895..bd589e7b7 100644 --- a/LibGit2Sharp.Tests/RepositoryFixture.cs +++ b/LibGit2Sharp.Tests/RepositoryFixture.cs @@ -150,7 +150,6 @@ public void CreatingRepoWithBadParamsThrows() 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); @@ -215,7 +214,6 @@ public void CanOpenRepository() Assert.NotNull(repo.Info.Path); Assert.Null(repo.Info.WorkingDirectory); Assert.True(repo.Info.IsBare); - Assert.False(repo.Info.IsEmpty); Assert.False(repo.Info.IsHeadDetached); } } diff --git a/LibGit2Sharp.Tests/TagFixture.cs b/LibGit2Sharp.Tests/TagFixture.cs index 30272a43c..c3dfdd1f6 100644 --- a/LibGit2Sharp.Tests/TagFixture.cs +++ b/LibGit2Sharp.Tests/TagFixture.cs @@ -603,7 +603,7 @@ public void CanListAllTagsInAEmptyRepository() using (var repo = Repository.Init(scd.DirectoryPath)) { - Assert.True(repo.Info.IsEmpty); + Assert.True(repo.Info.IsHeadOrphaned); Assert.Equal(0, repo.Tags.Count()); } } diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index 4b045a0a0..15537a1af 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -657,7 +657,7 @@ public void Reset(Commit commit, IEnumerable paths = null) /// The generated . public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false) { - if (amendPreviousCommit && Info.IsEmpty) + if (amendPreviousCommit && Info.IsHeadOrphaned) { throw new LibGit2SharpException("Can not amend anything. The Head doesn't point at any commit."); } diff --git a/LibGit2Sharp/RepositoryInformation.cs b/LibGit2Sharp/RepositoryInformation.cs index 076277f11..8c055d83b 100644 --- a/LibGit2Sharp/RepositoryInformation.cs +++ b/LibGit2Sharp/RepositoryInformation.cs @@ -1,4 +1,5 @@ -using LibGit2Sharp.Core; +using System; +using LibGit2Sharp.Core; namespace LibGit2Sharp { @@ -51,6 +52,7 @@ internal RepositoryInformation(Repository repo, bool isBare) /// /// true if this repository is empty; otherwise, false. /// + [Obsolete("This method will be removed in the next release.")] public virtual bool IsEmpty { get { return Proxy.git_repository_is_empty(repo.Handle); }