From b4bdb1618a69cc4b5b707429b69d1b01c43b4ddf Mon Sep 17 00:00:00 2001 From: Metalrom Date: Thu, 11 Apr 2013 14:55:46 +0200 Subject: [PATCH 1/4] Small code refactoring --- LibGit2Sharp/Repository.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/LibGit2Sharp/Repository.cs b/LibGit2Sharp/Repository.cs index b6532070d..b4b5766fa 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -670,7 +670,9 @@ 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."); } @@ -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)"; } From b4b0839497e86de014ed2b8987dcfd44de341429 Mon Sep 17 00:00:00 2001 From: Metalrom Date: Thu, 11 Apr 2013 14:59:21 +0200 Subject: [PATCH 2/4] Make amend on orphaned head throw OrphanedHeadException --- LibGit2Sharp.Tests/CommitFixture.cs | 2 +- LibGit2Sharp/Repository.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index 1d302023c..ec93f8de1 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -783,7 +783,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/Repository.cs b/LibGit2Sharp/Repository.cs index b4b5766fa..826f72b44 100644 --- a/LibGit2Sharp/Repository.cs +++ b/LibGit2Sharp/Repository.cs @@ -674,7 +674,7 @@ public Commit Commit(string message, Signature author, Signature committer, bool 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); From f23d0ed1a9c495c21e71e242e994bd167c331020 Mon Sep 17 00:00:00 2001 From: Metalrom Date: Thu, 11 Apr 2013 15:09:48 +0200 Subject: [PATCH 3/4] Enforce reflog writing test coverage --- LibGit2Sharp.Tests/CommitFixture.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index ec93f8de1..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); From 3f35bbbeaaf8cb1dd5a4a3e65cae59a5a85ce5ab Mon Sep 17 00:00:00 2001 From: Metalrom Date: Thu, 11 Apr 2013 16:39:25 +0200 Subject: [PATCH 4/4] Test reflog writing when HEAD is detached --- LibGit2Sharp.Tests/ReflogFixture.cs | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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); + } + } } }