diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index 1d302023c..d92e796f2 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -623,7 +623,7 @@ public void CanCommitALittleBit() Assert.Equal(0, commit.Parents.Count()); Assert.False(repo.Info.IsHeadOrphaned); - // Assert a reflog entry is created + // Assert a reflog entry is created on HEAD Assert.Equal(1, repo.Refs.Log("HEAD").Count()); var reflogEntry = repo.Refs.Log("HEAD").First(); Assert.Equal(author, reflogEntry.Commiter); @@ -631,6 +631,11 @@ public void CanCommitALittleBit() Assert.Equal(ObjectId.Zero, reflogEntry.From); Assert.Equal(string.Format("commit (initial): {0}", commitMessage), reflogEntry.Message); + // Assert a reflog entry is created on HEAD target + var targetCanonicalName = repo.Refs.Head.TargetIdentifier; + Assert.Equal(1, repo.Refs.Log(targetCanonicalName).Count()); + Assert.Equal(commit.Id, repo.Refs.Log(targetCanonicalName).First().To); + File.WriteAllText(filePath, "nulltoken commits!\n"); repo.Index.Stage(relativeFilepath); @@ -783,7 +788,7 @@ public void CanNotAmendAnEmptyRepository() using (Repository repo = Repository.Init(scd.DirectoryPath)) { - Assert.Throws(() => repo.Commit("I can not amend anything !:(", DummySignature, DummySignature, true)); + Assert.Throws(() => repo.Commit("I can not amend anything !:(", DummySignature, DummySignature, true)); } } diff --git a/LibGit2Sharp.Tests/ReflogFixture.cs b/LibGit2Sharp.Tests/ReflogFixture.cs index a5f287f4c..a10bf3a9d 100644 --- a/LibGit2Sharp.Tests/ReflogFixture.cs +++ b/LibGit2Sharp.Tests/ReflogFixture.cs @@ -102,5 +102,36 @@ public void CommitOnUnbornReferenceShouldCreateReflogEntryWithInitialTag() Assert.Equal(string.Format("commit (initial): {0}", commitMessage), repo.Refs.Log("HEAD").First().Message); } } + + [Fact] + public void CommitOnDetachedHeadShouldInsertReflogEntry() + { + string repoPath = CloneStandardTestRepo(); + + using (var repo = new Repository(repoPath)) + { + Assert.False(repo.Info.IsHeadDetached); + + var parentCommit = repo.Head.Tip.Parents.First(); + repo.Checkout(parentCommit.Sha); + Assert.True(repo.Info.IsHeadDetached); + + const string relativeFilepath = "new.txt"; + string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); + + File.WriteAllText(filePath, "content\n"); + repo.Index.Stage(relativeFilepath); + + var author = DummySignature; + const string commitMessage = "Commit on detached head"; + var commit = repo.Commit(commitMessage, author, author); + + // Assert a reflog entry is created on HEAD + var reflogEntry = repo.Refs.Log("HEAD").First(); + Assert.Equal(author, reflogEntry.Commiter); + Assert.Equal(commit.Id, reflogEntry.To); + Assert.Equal(string.Format("commit: {0}", commitMessage), repo.Refs.Log("HEAD").First().Message); + } + } } } diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index b6532070d..826f72b44 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -670,9 +670,11 @@ public void Reset(Commit commit, IEnumerable paths = null, ExplicitPaths /// The generated . public Commit Commit(string message, Signature author, Signature committer, bool amendPreviousCommit = false) { - if (amendPreviousCommit && Info.IsHeadOrphaned) + bool isHeadOrphaned = Info.IsHeadOrphaned; + + if (amendPreviousCommit && isHeadOrphaned) { - throw new LibGit2SharpException("Can not amend anything. The Head doesn't point at any commit."); + throw new OrphanedHeadException("Can not amend anything. The Head doesn't point at any commit."); } var treeId = Proxy.git_tree_create_fromindex(Index); @@ -685,16 +687,16 @@ public Commit Commit(string message, Signature author, Signature committer, bool Proxy.git_repository_merge_cleanup(handle); // Insert reflog entry - LogCommit(result, amendPreviousCommit, parents.Count() == 0); + LogCommit(result, amendPreviousCommit, isHeadOrphaned); return result; } - private void LogCommit(Commit commit, bool amendPreviousCommit, bool isInitialCommit) + private void LogCommit(Commit commit, bool amendPreviousCommit, bool isHeadOrphaned) { // Compute reflog message string reflogMessage = "commit"; - if (isInitialCommit) + if (isHeadOrphaned) { reflogMessage += " (initial)"; }