From b93a9e88cd99daad720e75793261b750df24c997 Mon Sep 17 00:00:00 2001 From: nulltoken Date: Wed, 6 Jun 2012 19:46:02 +0200 Subject: Add repo.Reset() overload to allow replacing of index entries with commit entries Fix #165 --- LibGit2Sharp.Tests/ResetHeadFixture.cs | 156 +++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 LibGit2Sharp.Tests/ResetHeadFixture.cs (limited to 'LibGit2Sharp.Tests/ResetHeadFixture.cs') diff --git a/LibGit2Sharp.Tests/ResetHeadFixture.cs b/LibGit2Sharp.Tests/ResetHeadFixture.cs new file mode 100644 index 00000000..44d43f06 --- /dev/null +++ b/LibGit2Sharp.Tests/ResetHeadFixture.cs @@ -0,0 +1,156 @@ +using System; +using System.IO; +using LibGit2Sharp.Tests.TestHelpers; +using Xunit; +using Xunit.Extensions; + +namespace LibGit2Sharp.Tests +{ + public class ResetHeadFixture : BaseFixture + { + [Theory] + [InlineData(true)] + [InlineData(false)] + public void ResetANewlyInitializedRepositoryThrows(bool isBare) + { + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + + using (var repo = Repository.Init(scd.DirectoryPath, isBare)) + { + Assert.Throws(() => repo.Reset(ResetOptions.Soft)); + } + } + + [Fact] + public void SoftResetToTheHeadOfARepositoryDoesNotChangeTheTargetOfTheHead() + { + using (var repo = new Repository(BareTestRepoPath)) + { + Branch oldHead = repo.Head; + + repo.Reset(ResetOptions.Soft); + + Assert.Equal(oldHead, repo.Head); + } + } + + [Fact] + public void SoftResetSetsTheHeadToTheDereferencedCommitOfAChainedTag() + { + TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(); + + using (var repo = new Repository(path.RepositoryPath)) + { + Tag tag = repo.Tags["test"]; + repo.Reset(ResetOptions.Soft, tag.CanonicalName); + Assert.Equal("e90810b8df3e80c413d903f631643c716887138d", repo.Head.Tip.Sha); + } + } + + [Fact] + public void ResettingWithBadParamsThrows() + { + using (var repo = new Repository(BareTestRepoPath)) + { + Assert.Throws(() => repo.Reset(ResetOptions.Soft, null)); + Assert.Throws(() => repo.Reset(ResetOptions.Soft, "")); + Assert.Throws(() => repo.Reset(ResetOptions.Soft, Constants.UnknownSha)); + } + } + + [Fact] + public void SoftResetSetsTheHeadToTheSpecifiedCommit() + { + /* Make the Head point to a branch through its name */ + AssertSoftReset(b => b.Name, false, b => b.Name); + } + + [Fact] + public void SoftResetSetsTheDetachedHeadToTheSpecifiedCommit() + { + /* Make the Head point to a commit through its sha (Detaches the Head) */ + AssertSoftReset(b => b.Tip.Sha, true, b => "(no branch)"); + } + + private void AssertSoftReset(Func branchIdentifierRetriever, bool shouldHeadBeDetached, Func expectedHeadNameRetriever) + { + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + + using (var repo = Repository.Init(scd.DirectoryPath)) + { + FeedTheRepository(repo); + + Tag tag = repo.Tags["mytag"]; + Branch branch = repo.Branches["mybranch"]; + + string branchIdentifier = branchIdentifierRetriever(branch); + repo.Checkout(branchIdentifier); + Assert.Equal(shouldHeadBeDetached, repo.Info.IsHeadDetached); + + string expectedHeadName = expectedHeadNameRetriever(branch); + Assert.Equal(expectedHeadName, repo.Head.Name); + Assert.Equal(branch.Tip.Sha, repo.Head.Tip.Sha); + + /* Reset --soft the Head to a tag through its canonical name */ + repo.Reset(ResetOptions.Soft, tag.CanonicalName); + Assert.Equal(expectedHeadName, repo.Head.Name); + Assert.Equal(tag.Target.Id, repo.Head.Tip.Id); + + Assert.Equal(FileStatus.Staged, repo.Index.RetrieveStatus("a.txt")); + + /* Reset --soft the Head to a commit through its sha */ + repo.Reset(ResetOptions.Soft, branch.Tip.Sha); + Assert.Equal(expectedHeadName, repo.Head.Name); + Assert.Equal(branch.Tip.Sha, repo.Head.Tip.Sha); + + Assert.Equal(FileStatus.Unaltered, repo.Index.RetrieveStatus("a.txt")); + } + } + + private static void FeedTheRepository(Repository repo) + { + string fullPath = Path.Combine(repo.Info.WorkingDirectory, "a.txt"); + File.WriteAllText(fullPath, "Hello\n"); + repo.Index.Stage(fullPath); + repo.Commit("Initial commit", Constants.Signature, Constants.Signature); + repo.ApplyTag("mytag"); + + File.AppendAllText(fullPath, "World\n"); + repo.Index.Stage(fullPath); + + Signature shiftedSignature = Constants.Signature.TimeShift(TimeSpan.FromMinutes(1)); + repo.Commit("Update file", shiftedSignature, shiftedSignature); + repo.CreateBranch("mybranch"); + + repo.Checkout("mybranch"); + + Assert.False(repo.Index.RetrieveStatus().IsDirty); + } + + [Fact] + public void MixedResetRefreshesTheIndex() + { + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + + using (var repo = Repository.Init(scd.DirectoryPath)) + { + FeedTheRepository(repo); + + Tag tag = repo.Tags["mytag"]; + + repo.Reset(ResetOptions.Mixed, tag.CanonicalName); + + Assert.Equal(FileStatus.Modified, repo.Index.RetrieveStatus("a.txt")); + } + } + + [Fact] + public void MixedResetInABareRepositoryThrows() + { + using (var repo = new Repository(BareTestRepoPath)) + { + Assert.Throws(() => repo.Reset(ResetOptions.Mixed)); + } + } + } +} -- cgit v1.2.3