Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mono/libgit2sharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornulltoken <emeric.fermas@gmail.com>2012-06-06 21:46:02 +0400
committernulltoken <emeric.fermas@gmail.com>2012-06-06 22:39:26 +0400
commitb93a9e88cd99daad720e75793261b750df24c997 (patch)
treefe285394e2840e23952faa7e6b4db7b2959966c1 /LibGit2Sharp.Tests/ResetHeadFixture.cs
parent9ea588955c8c971f39447a6cdcf7036caff654f3 (diff)
Add repo.Reset() overload to allow replacing of index entries with commit entries
Fix #165
Diffstat (limited to 'LibGit2Sharp.Tests/ResetHeadFixture.cs')
-rw-r--r--LibGit2Sharp.Tests/ResetHeadFixture.cs156
1 files changed, 156 insertions, 0 deletions
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<LibGit2SharpException>(() => 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<ArgumentNullException>(() => repo.Reset(ResetOptions.Soft, null));
+ Assert.Throws<ArgumentException>(() => repo.Reset(ResetOptions.Soft, ""));
+ Assert.Throws<LibGit2SharpException>(() => 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<Branch, string> branchIdentifierRetriever, bool shouldHeadBeDetached, Func<Branch, string> 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<LibGit2SharpException>(() => repo.Reset(ResetOptions.Mixed));
+ }
+ }
+ }
+}