diff options
author | Edward Thomson <ethomson@microsoft.com> | 2014-04-08 06:53:35 +0400 |
---|---|---|
committer | Edward Thomson <ethomson@microsoft.com> | 2014-04-09 21:00:50 +0400 |
commit | 8a303f2b24a053816d8d88c3733447ae5565e662 (patch) | |
tree | 4fa187b62202172b927fc4f73f838b36c4e75af3 /LibGit2Sharp.Tests/CommitFixture.cs | |
parent | aa94e9d9326e9318feb89e8c9f4300b0bf60f6ca (diff) |
Introduce EmptyCommitException
By default, we now throw an exception when trying to commit an
"empty commit" - that is, one that does not change any items.
This check only applies when doing a normal commit, or an amend
of a normal commit; it does not apply when performing a merge or
an amend of a merge commit. This check can optionally be overridden.
Diffstat (limited to 'LibGit2Sharp.Tests/CommitFixture.cs')
-rw-r--r-- | LibGit2Sharp.Tests/CommitFixture.cs | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/CommitFixture.cs b/LibGit2Sharp.Tests/CommitFixture.cs index f060ee0c..c34bd4bb 100644 --- a/LibGit2Sharp.Tests/CommitFixture.cs +++ b/LibGit2Sharp.Tests/CommitFixture.cs @@ -885,5 +885,124 @@ namespace LibGit2Sharp.Tests Assert.Equal(1, repo.Head.Commits.Count()); } } + + [Fact] + public void CanNotCommitAnEmptyCommit() + { + string path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + repo.Reset(ResetMode.Hard); + repo.RemoveUntrackedFiles(); + + Assert.Throws<EmptyCommitException>(() => repo.Commit("Empty commit!", Constants.Signature, Constants.Signature)); + } + } + + [Fact] + public void CanCommitAnEmptyCommitWhenForced() + { + string path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + repo.Reset(ResetMode.Hard); + repo.RemoveUntrackedFiles(); + + repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true); + } + } + + [Fact] + public void CanNotAmendAnEmptyCommit() + { + string path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + repo.Reset(ResetMode.Hard); + repo.RemoveUntrackedFiles(); + + repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true); + + Assert.Throws<EmptyCommitException>(() => repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, true, false)); + } + } + + [Fact] + public void CanAmendAnEmptyCommitWhenForced() + { + string path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + repo.Reset(ResetMode.Hard); + repo.RemoveUntrackedFiles(); + + Commit emptyCommit = repo.Commit("Empty commit!", Constants.Signature, Constants.Signature, false, true); + + Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true, true); + AssertCommitHasBeenAmended(repo, amendedCommit, emptyCommit); + } + } + + [Fact] + public void CanCommitAnEmptyCommitWhenMerging() + { + string path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + repo.Reset(ResetMode.Hard); + repo.RemoveUntrackedFiles(); + + Touch(repo.Info.Path, "MERGE_HEAD", "f705abffe7015f2beacf2abe7a36583ebee3487e\n"); + + Assert.Equal(CurrentOperation.Merge, repo.Info.CurrentOperation); + + Commit newMergedCommit = repo.Commit("Merge commit", Constants.Signature, Constants.Signature); + + Assert.Equal(2, newMergedCommit.Parents.Count()); + Assert.Equal(newMergedCommit.Parents.First().Sha, "32eab9cb1f450b5fe7ab663462b77d7f4b703344"); + Assert.Equal(newMergedCommit.Parents.Skip(1).First().Sha, "f705abffe7015f2beacf2abe7a36583ebee3487e"); + } + } + + [Fact] + public void CanAmendAnEmptyMergeCommit() + { + string path = CloneStandardTestRepo(); + using (var repo = new Repository(path)) + { + repo.Reset(ResetMode.Hard); + repo.RemoveUntrackedFiles(); + + Touch(repo.Info.Path, "MERGE_HEAD", "f705abffe7015f2beacf2abe7a36583ebee3487e\n"); + Commit newMergedCommit = repo.Commit("Merge commit", Constants.Signature, Constants.Signature); + + Commit amendedCommit = repo.Commit("I'm rewriting the history!", Constants.Signature, Constants.Signature, true); + AssertCommitHasBeenAmended(repo, amendedCommit, newMergedCommit); + } + } + + [Fact] + public void CanNotAmendACommitInAWayThatWouldLeadTheNewCommitToBecomeEmpty() + { + string repoPath = InitNewRepository(); + + using (var repo = new Repository(repoPath)) + { + Touch(repo.Info.WorkingDirectory, "test.txt", "test\n"); + repo.Index.Stage("test.txt"); + + repo.Commit("Initial commit", Constants.Signature, Constants.Signature); + + Touch(repo.Info.WorkingDirectory, "new.txt", "content\n"); + repo.Index.Stage("new.txt"); + + repo.Commit("One commit", Constants.Signature, Constants.Signature); + + repo.Index.Remove("new.txt"); + + Assert.Throws<EmptyCommitException>(() => repo.Commit("Oops", Constants.Signature, Constants.Signature, + new CommitOptions { AmendPreviousCommit = true })); + } + } } } |