public void TrackedBranchExistsFromDefaultConfigInEmptyClone() { string repoPath = InitNewRepository(true); Uri uri; using (var emptyRepo = new Repository(repoPath)) { uri = new Uri(emptyRepo.Info.Path); } SelfCleaningDirectory scd2 = BuildSelfCleaningDirectory(); string clonedRepoPath = Repository.Clone(uri.AbsoluteUri, scd2.DirectoryPath); using (var repo = new Repository(clonedRepoPath)) { Assert.Empty(Directory.GetFiles(scd2.RootedDirectoryPath)); Assert.Equal(repo.Head.FriendlyName, "master"); Assert.Null(repo.Head.Tip); Assert.NotNull(repo.Head.TrackedBranch); Assert.Null(repo.Head.TrackedBranch.Tip); Assert.NotNull(repo.Head.TrackingDetails); Assert.Null(repo.Head.TrackingDetails.AheadBy); Assert.Null(repo.Head.TrackingDetails.BehindBy); Assert.Null(repo.Head.TrackingDetails.CommonAncestor); Assert.NotNull(repo.Head.Remote); Assert.Equal("origin", repo.Head.Remote.Name); Touch(repo.Info.WorkingDirectory, "a.txt", "a"); repo.Stage("a.txt"); repo.Commit("A file", Constants.Signature, Constants.Signature); Assert.NotNull(repo.Head.Tip); Assert.NotNull(repo.Head.TrackedBranch); Assert.Null(repo.Head.TrackedBranch.Tip); Assert.NotNull(repo.Head.TrackingDetails); Assert.Null(repo.Head.TrackingDetails.AheadBy); Assert.Null(repo.Head.TrackingDetails.BehindBy); Assert.Null(repo.Head.TrackingDetails.CommonAncestor); } }
public void CanSpecifyFileConflictStrategy() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (Repository repo = new Repository(path)) { ConstructRebaseTestRepository(repo); repo.Checkout(topicBranch1Name); Assert.False(repo.RetrieveStatus().IsDirty); Branch branch = repo.Branches[topicBranch1Name]; Branch upstream = repo.Branches[conflictBranch1Name]; Branch onto = repo.Branches[conflictBranch1Name]; RebaseOptions options = new RebaseOptions() { FileConflictStrategy = CheckoutFileConflictStrategy.Ours, }; RebaseResult rebaseResult = repo.Rebase.Start(branch, upstream, onto, Constants.Identity, options); // Verify that we have a conflict. Assert.Equal(CurrentOperation.RebaseMerge, repo.Info.CurrentOperation); Assert.Equal(RebaseStatus.Conflicts, rebaseResult.Status); Assert.True(repo.RetrieveStatus().IsDirty); Assert.False(repo.Index.IsFullyMerged); Assert.Equal(0, rebaseResult.CompletedStepCount); Assert.Equal(3, rebaseResult.TotalStepCount); string conflictFile = filePathB; // Get the information on the conflict. Conflict conflict = repo.Index.Conflicts[conflictFile]; Assert.NotNull(conflict); Assert.NotNull(conflict.Theirs); Assert.NotNull(conflict.Ours); Blob expectedBlob = repo.Lookup <Blob>(conflict.Ours.Id); // Check the content of the file on disk matches what is expected. string expectedContent = expectedBlob.GetContentText(new FilteringOptions(conflictFile)); Assert.Equal(expectedContent, File.ReadAllText(Path.Combine(repo.Info.WorkingDirectory, conflictFile))); } }
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")); AssertReflogEntryIsCreated(repo.Refs.Log(repo.Refs.Head), tag.Target.Sha, string.Format("reset: moving to {0}", tag.Target.Sha)); } }
public void CanCommitALittleBit() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); string dir = Repository.Init(scd.DirectoryPath); Path.IsPathRooted(dir).ShouldBeTrue(); Directory.Exists(dir).ShouldBeTrue(); using (var repo = new Repository(dir)) { string filePath = Path.Combine(repo.Info.WorkingDirectory, "new.txt"); File.WriteAllText(filePath, "null"); repo.Index.Stage("new.txt"); File.AppendAllText(filePath, "token\n"); repo.Index.Stage("new.txt"); var author = new Signature("Author N. Ame", "*****@*****.**", DateTimeOffset.Now.AddSeconds(-10)); Commit commit = repo.Commit(author, author, "Initial egotistic commit"); commit.Parents.Count().ShouldEqual(0); repo.Info.IsEmpty.ShouldBeFalse(); File.WriteAllText(filePath, "nulltoken commits!\n"); repo.Index.Stage("new.txt"); var author2 = new Signature(author.Name, author.Email, author.When.AddSeconds(5)); Commit commit2 = repo.Commit(author2, author2, "Are you trying to fork me?"); commit2.Parents.Count().ShouldEqual(1); commit2.Parents.First().Id.ShouldEqual(commit.Id); repo.CreateBranch("davidfowl-rules", commit.Id.Sha); //TODO: This cries for a shortcut method :-/ repo.Branches.Checkout("davidfowl-rules"); //TODO: This cries for a shortcut method :-/ File.WriteAllText(filePath, "davidfowl commits!\n"); var author3 = new Signature("David Fowler", "*****@*****.**", author.When.AddSeconds(2)); repo.Index.Stage("new.txt"); Commit commit3 = repo.Commit(author3, author3, "I'm going to branch you backwards in time!"); commit3.Parents.Count().ShouldEqual(1); commit3.Parents.First().Id.ShouldEqual(commit.Id); } }
public void CanAbortRebase() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (Repository repo = new Repository(path)) { ConstructRebaseTestRepository(repo); Commands.Checkout(repo, topicBranch1Name); Assert.False(repo.RetrieveStatus().IsDirty); Branch branch = repo.Branches[topicBranch1Name]; Branch upstream = repo.Branches[conflictBranch1Name]; Branch onto = repo.Branches[conflictBranch1Name]; RebaseResult rebaseResult = repo.Rebase.Start(branch, upstream, onto, Constants.Identity, null); // Verify that we have a conflict. Assert.Equal(RebaseStatus.Conflicts, rebaseResult.Status); Assert.True(repo.RetrieveStatus().IsDirty); Assert.False(repo.Index.IsFullyMerged); Assert.Equal(0, rebaseResult.CompletedStepCount); Assert.Equal(3, rebaseResult.TotalStepCount); // Set up the callbacks to verify that checkout progress / notify // callbacks are called. bool wasCheckoutProgressCalled = false; bool wasCheckoutNotifyCalled = false; RebaseOptions options = new RebaseOptions() { OnCheckoutProgress = (x, y, z) => wasCheckoutProgressCalled = true, OnCheckoutNotify = (x, y) => { wasCheckoutNotifyCalled = true; return(true); }, CheckoutNotifyFlags = CheckoutNotifyFlags.Updated, }; repo.Rebase.Abort(options); Assert.False(repo.RetrieveStatus().IsDirty, "Repository workdir is dirty after Rebase.Abort."); Assert.True(repo.Index.IsFullyMerged, "Repository index is not fully merged after Rebase.Abort."); Assert.Equal(CurrentOperation.None, repo.Info.CurrentOperation); Assert.True(wasCheckoutProgressCalled, "Checkout progress callback was not called during Rebase.Abort."); Assert.True(wasCheckoutNotifyCalled, "Checkout notify callback was not called during Rebase.Abort."); } }
public void CanUnstageUntrackedFileAgainstAnOrphanedHead() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { string relativePath = "a.txt"; string absolutePath = Path.Combine(repo.Info.WorkingDirectory, relativePath); File.WriteAllText(absolutePath, "hello test file\n", Encoding.ASCII); repo.Index.Stage(absolutePath); repo.Index.Unstage(relativePath); RepositoryStatus status = repo.Index.RetrieveStatus(); Assert.Equal(0, status.Staged.Count()); Assert.Equal(1, status.Untracked.Count()); } }
public void CanUnsetAnEntryFromTheGlobalConfiguration() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var options = BuildFakeConfigs(scd); using (var repo = new Repository(BareTestRepoPath, options)) { Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global)); Assert.Equal(42, repo.Config.Get <int>("Wow.Man-I-am-totally-global").Value); repo.Config.Unset("Wow.Man-I-am-totally-global"); Assert.Equal(42, repo.Config.Get <int>("Wow.Man-I-am-totally-global").Value); repo.Config.Unset("Wow.Man-I-am-totally-global", ConfigurationLevel.Global); Assert.Null(repo.Config.Get <int>("Wow.Man-I-am-totally-global")); } }
public void CanCreateBareRepo() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); string dir = Repository.Init(scd.DirectoryPath, true); Path.IsPathRooted(dir).ShouldBeTrue(); Directory.Exists(dir).ShouldBeTrue(); CheckGitConfigFile(dir); using (var repo = new Repository(dir)) { repo.Info.WorkingDirectory.ShouldBeNull(); repo.Info.Path.ShouldEqual(scd.RootedDirectoryPath + Path.DirectorySeparatorChar); repo.Info.IsBare.ShouldBeTrue(); AssertInitializedRepository(repo); } }
public void CanGeneratePredictableObjectShas() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); CommitToANewRepository(scd.DirectoryPath); using (var repo = new Repository(scd.DirectoryPath)) { Commit commit = repo.Commits.Single(); Assert.Equal("1fe3126578fc4eca68c193e4a3a0a14a0704624d", commit.Sha); Tree tree = commit.Tree; Assert.Equal("2b297e643c551e76cfa1f93810c50811382f9117", tree.Sha); GitObject blob = tree.Single().Target; Assert.IsAssignableFrom <Blob>(blob); Assert.Equal("9daeafb9864cf43055ae93beb0afd6c7d144bfa4", blob.Sha); } }
public void RebaseOperationsWithoutRebasingThrow() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (Repository repo = new Repository(path)) { ConstructRebaseTestRepository(repo); Commands.Checkout(repo, topicBranch1Name); Assert.Throws <NotFoundException>(() => repo.Rebase.Continue(Constants.Identity, new RebaseOptions())); Assert.Throws <NotFoundException>(() => repo.Rebase.Abort()); } }
public void CanProvideDifferentConfigurationFilesOnInit() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var options = BuildFakeConfigs(scd); using (var repo = Repository.Init(scd.DirectoryPath, false, options)) { Assert.True(repo.Config.HasConfig(ConfigurationLevel.Global)); Assert.Equal("global", repo.Config.Get <string>("woot.this-rocks").Value); Assert.Equal(42, repo.Config.Get <int>("wow.man-I-am-totally-global").Value); Assert.True(repo.Config.HasConfig(ConfigurationLevel.Xdg)); Assert.Equal("xdg", repo.Config.Get <string>("woot.this-rocks", ConfigurationLevel.Xdg).Value); Assert.True(repo.Config.HasConfig(ConfigurationLevel.System)); Assert.Equal("system", repo.Config.Get <string>("woot.this-rocks", ConfigurationLevel.System).Value); } }
public void CanCreateBareRepo() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath, true)) { string dir = repo.Info.Path; Assert.True(Path.IsPathRooted(dir)); Assert.True(Directory.Exists(dir)); CheckGitConfigFile(dir); Assert.Null(repo.Info.WorkingDirectory); Assert.Equal(scd.RootedDirectoryPath + Path.DirectorySeparatorChar, repo.Info.Path); Assert.True(repo.Info.IsBare); AssertInitializedRepository(repo); } }
public void ComparingReliesOnProvidedConfigEntriesIfAny() { TemporaryCloneOfTestRepo path = BuildTemporaryCloneOfTestRepo(StandardTestRepoWorkingDirPath); const string file = "1/branch_file.txt"; using (var repo = new Repository(path.DirectoryPath)) { TreeEntry entry = repo.Head[file]; Assert.Equal(Mode.ExecutableFile, entry.Mode); // Recreate the file in the workdir without the executable bit string fullpath = Path.Combine(repo.Info.WorkingDirectory, file); File.Delete(fullpath); File.WriteAllBytes(fullpath, ((Blob)(entry.Target)).Content); // Unset the local core.filemode, if any. repo.Config.Unset("core.filemode", ConfigurationLevel.Local); } SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var options = BuildFakeSystemConfigFilemodeOption(scd, true); using (var repo = new Repository(path.DirectoryPath, options)) { TreeChanges changes = repo.Diff.Compare(new [] { file }); Assert.Equal(1, changes.Count()); var change = changes.Modified.Single(); Assert.Equal(Mode.ExecutableFile, change.OldMode); Assert.Equal(Mode.NonExecutableFile, change.Mode); } options = BuildFakeSystemConfigFilemodeOption(scd, false); using (var repo = new Repository(path.DirectoryPath, options)) { TreeChanges changes = repo.Diff.Compare(new[] { file }); Assert.Equal(0, changes.Count()); } }
public void TrackedBranchExistsFromDefaultConfigInEmptyClone() { SelfCleaningDirectory scd1 = BuildSelfCleaningDirectory(); Uri uri; using (var emptyRepo = Repository.Init(scd1.DirectoryPath, true)) { uri = new Uri(emptyRepo.Info.Path); } SelfCleaningDirectory scd2 = BuildSelfCleaningDirectory(); using (Repository repo = Repository.Clone(uri.AbsoluteUri, scd2.RootedDirectoryPath)) { Assert.Empty(Directory.GetFiles(scd2.RootedDirectoryPath)); Assert.Equal(repo.Head.Name, "master"); Assert.Null(repo.Head.Tip); Assert.NotNull(repo.Head.TrackedBranch); Assert.Null(repo.Head.TrackedBranch.Tip); Assert.NotNull(repo.Head.TrackingDetails); Assert.Null(repo.Head.TrackingDetails.AheadBy); Assert.Null(repo.Head.TrackingDetails.BehindBy); Assert.Null(repo.Head.TrackingDetails.CommonAncestor); Assert.NotNull(repo.Head.Remote); Assert.Equal("origin", repo.Head.Remote.Name); File.WriteAllText(Path.Combine(scd2.RootedDirectoryPath, "a.txt"), "a"); repo.Index.Stage("a.txt"); repo.Commit("A file", DummySignature, DummySignature); Assert.NotNull(repo.Head.Tip); Assert.NotNull(repo.Head.TrackedBranch); Assert.Null(repo.Head.TrackedBranch.Tip); Assert.NotNull(repo.Head.TrackingDetails); Assert.Null(repo.Head.TrackingDetails.AheadBy); Assert.Null(repo.Head.TrackingDetails.BehindBy); Assert.Null(repo.Head.TrackingDetails.CommonAncestor); } }
public void CanRebaseBranchOntoItself() { // Maybe we should have an "up-to-date" return type for scenarios such as these, // but for now this test is to make sure we do something reasonable SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (Repository repo = new Repository(path)) { ConstructRebaseTestRepository(repo); Commands.Checkout(repo, topicBranch2Name); Branch b = repo.Branches[topicBranch2Name]; RebaseResult result = repo.Rebase.Start(b, b, null, Constants.Identity, new RebaseOptions()); Assert.Equal(0, result.TotalStepCount); Assert.Equal(RebaseStatus.Complete, result.Status); Assert.Equal(0, result.CompletedStepCount); } }
public void CanRetrieveTheStatusOfANewRepository() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (Repository repo = Repository.Init(scd.DirectoryPath)) { RepositoryStatus status = repo.Index.RetrieveStatus(); Assert.NotNull(status); Assert.Equal(0, status.Count()); Assert.False(status.IsDirty); Assert.Equal(0, status.Untracked.Count()); Assert.Equal(0, status.Modified.Count()); Assert.Equal(0, status.Missing.Count()); Assert.Equal(0, status.Added.Count()); Assert.Equal(0, status.Staged.Count()); Assert.Equal(0, status.Removed.Count()); } }
public void CanRetrieveTheStatusOfANewRepository() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { RepositoryStatus status = repo.Index.RetrieveStatus(); status.ShouldNotBeNull(); status.Count().ShouldEqual(0); status.IsDirty.ShouldBeFalse(); status.Untracked.Count().ShouldEqual(0); status.Modified.Count().ShouldEqual(0); status.Missing.Count().ShouldEqual(0); status.Added.Count().ShouldEqual(0); status.Staged.Count().ShouldEqual(0); status.Removed.Count().ShouldEqual(0); } }
public void CanCreateStandardRepo() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); string dir = Repository.Init(scd.DirectoryPath); Path.IsPathRooted(dir).ShouldBeTrue(); Directory.Exists(dir).ShouldBeTrue(); CheckGitConfigFile(dir); using (var repo = new Repository(dir)) { repo.Info.WorkingDirectory.ShouldNotBeNull(); repo.Info.Path.ShouldEqual(Path.Combine(scd.RootedDirectoryPath, ".git" + Path.DirectorySeparatorChar)); repo.Info.IsBare.ShouldBeFalse(); AssertIsHidden(repo.Info.Path); AssertInitializedRepository(repo); } }
public void CanCreateStandardRepo() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { string dir = repo.Info.Path; Assert.True(Path.IsPathRooted(dir)); Assert.True(Directory.Exists(dir)); CheckGitConfigFile(dir); Assert.NotNull(repo.Info.WorkingDirectory); Assert.Equal(Path.Combine(scd.RootedDirectoryPath, ".git" + Path.DirectorySeparatorChar), repo.Info.Path); Assert.False(repo.Info.IsBare); AssertIsHidden(repo.Info.Path); AssertInitializedRepository(repo); } }
private RepositoryOptions BuildFakeSystemConfigFilemodeOption( SelfCleaningDirectory scd, bool value) { Directory.CreateDirectory(scd.DirectoryPath); var options = new RepositoryOptions { SystemConfigurationLocation = Path.Combine( scd.RootedDirectoryPath, "fake-system.config") }; StringBuilder sb = new StringBuilder() .AppendFormat("[core]{0}", Environment.NewLine) .AppendFormat("filemode = {1}{0}", Environment.NewLine, value); File.WriteAllText(options.SystemConfigurationLocation, sb.ToString()); return(options); }
protected string CreateConfigurationWithDummyUser(string name, string email) { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); string configFilePath = Touch(scd.DirectoryPath, "fake-config"); using (Configuration config = Configuration.BuildFrom(configFilePath)) { if (name != null) { config.Set("user.name", name); } if (email != null) { config.Set("user.email", email); } } return(configFilePath); }
public void CommitOnUnbornReferenceShouldCreateReflogEntryWithInitialTag() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { 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 = "First commit should be logged as initial"; repo.Commit(commitMessage, author, author); // Assert the reflog entry message is correct Assert.Equal(1, repo.Refs.Log("HEAD").Count()); Assert.Equal(string.Format("commit (initial): {0}", commitMessage), repo.Refs.Log("HEAD").First().Message); } }
public void CanCommitOnOrphanedBranch() { string newBranchName = "refs/heads/newBranch"; SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { // Set Head to point to branch other than master repo.Refs.UpdateTarget("HEAD", newBranchName); Assert.Equal(newBranchName, repo.Head.CanonicalName); const string relativeFilepath = "test.txt"; string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); File.WriteAllText(filePath, "test\n"); repo.Index.Stage(relativeFilepath); repo.Commit("Initial commit", DummySignature, DummySignature); Assert.Equal(1, repo.Head.Commits.Count()); } }
public void CommitCleansUpMergeMetadata() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { string dir = repo.Info.Path; Assert.True(Path.IsPathRooted(dir)); Assert.True(Directory.Exists(dir)); const string relativeFilepath = "new.txt"; string filePath = Path.Combine(repo.Info.WorkingDirectory, relativeFilepath); File.WriteAllText(filePath, "this is a new file"); repo.Index.Stage(relativeFilepath); string mergeHeadPath = Path.Combine(repo.Info.Path, "MERGE_HEAD"); string mergeMsgPath = Path.Combine(repo.Info.Path, "MERGE_MSG"); string mergeModePath = Path.Combine(repo.Info.Path, "MERGE_MODE"); string origHeadPath = Path.Combine(repo.Info.Path, "ORIG_HEAD"); File.WriteAllText(mergeHeadPath, "abcdefabcdefabcdefabcdefabcdefabcdefabcd"); File.WriteAllText(mergeMsgPath, "This is a dummy merge.\n"); File.WriteAllText(mergeModePath, "no-ff"); File.WriteAllText(origHeadPath, "beefbeefbeefbeefbeefbeefbeefbeefbeefbeef"); Assert.True(File.Exists(mergeHeadPath)); Assert.True(File.Exists(mergeMsgPath)); Assert.True(File.Exists(mergeModePath)); Assert.True(File.Exists(origHeadPath)); var author = DummySignature; repo.Commit("Initial egotistic commit", author, author); Assert.False(File.Exists(mergeHeadPath)); Assert.False(File.Exists(mergeMsgPath)); Assert.False(File.Exists(mergeModePath)); Assert.True(File.Exists(origHeadPath)); } }
public void RenameThresholdsAreObeyed() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { const string originalPath = "original.txt"; const string renamedPath = "renamed.txt"; // 4 lines Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Index.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); // 8 lines, 50% are from original file Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\ne\nf\ng\nh\n"); repo.Index.Stage(originalPath); repo.Index.Move(originalPath, renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var compareOptions = new CompareOptions { Similarity = new SimilarityOptions { RenameDetectionMode = RenameDetectionMode.Renames, }, }; compareOptions.Similarity.RenameThreshold = 30; var changes = repo.Diff.Compare <TreeChanges>(old.Tree, @new.Tree, compareOptions: compareOptions); Assert.True(changes.All(x => x.Status == ChangeKind.Renamed)); compareOptions.Similarity.RenameThreshold = 90; changes = repo.Diff.Compare <TreeChanges>(old.Tree, @new.Tree, compareOptions: compareOptions); Assert.False(changes.Any(x => x.Status == ChangeKind.Renamed)); } }
public void CommitShouldCreateReflogEntryOnHeadandOnTargetedDirectReference() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { // setup refs as HEAD => unit_test => master var newRef = repo.Refs.Add("refs/heads/unit_test", "refs/heads/master"); Assert.NotNull(newRef); repo.Refs.UpdateTarget(repo.Refs.Head, newRef); 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 = "Hope reflog behaves as it should"; Commit commit = repo.Commit(commitMessage, author, author); // 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); Assert.Equal(commit.Id, reflogEntry.To); Assert.Equal(ObjectId.Zero, reflogEntry.From); // Assert the same reflog entry is created on refs/heads/master Assert.Equal(1, repo.Refs.Log("refs/heads/master").Count()); reflogEntry = repo.Refs.Log("HEAD").First(); Assert.Equal(author, reflogEntry.Commiter); Assert.Equal(commit.Id, reflogEntry.To); Assert.Equal(ObjectId.Zero, reflogEntry.From); // Assert no reflog entry is created on refs/heads/unit_test Assert.Equal(0, repo.Refs.Log("refs/heads/unit_test").Count()); } }
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); var oldHeadSha = repo.Head.Tip.Sha; 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")); AssertReflogEntryIsCreated(repo.Refs.Log(repo.Refs.Head), tag.Target.Sha, string.Format("reset: moving to {0}", tag.Target.Sha), oldHeadSha); /* 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")); AssertReflogEntryIsCreated(repo.Refs.Log(repo.Refs.Head), branch.Tip.Sha, string.Format("reset: moving to {0}", branch.Tip.Sha), tag.Target.Sha); } }
public void CanAmendARootCommit() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); CommitToANewRepository(scd.DirectoryPath); using (var repo = new Repository(scd.DirectoryPath)) { Assert.Equal(1, repo.Head.Commits.Count()); Commit originalCommit = repo.Head.Tip; Assert.Equal(0, originalCommit.Parents.Count()); CreateAndStageANewFile(repo); Commit amendedCommit = repo.Commit("I'm rewriting the history!", DummySignature, DummySignature, true); Assert.Equal(1, repo.Head.Commits.Count()); AssertCommitHasBeenAmended(repo, amendedCommit, originalCommit); } }
public void CanDetectTheExactCopyingOfModifiedFilesWhenEnabled() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { const string originalPath = "original.txt"; const string copiedPath = "copied.txt"; string originalFullPath = Path.Combine(repo.Info.WorkingDirectory, originalPath); string copiedFullPath = Path.Combine(repo.Info.WorkingDirectory, copiedPath); Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Index.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); File.Copy(originalFullPath, copiedFullPath); Touch(repo.Info.WorkingDirectory, originalPath, "e\n"); repo.Index.Stage(originalPath); repo.Index.Stage(copiedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare <TreeChanges>(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.Copies, }); Assert.Equal(2, changes.Count()); Assert.Equal(1, changes.Copied.Count()); Assert.Equal(originalPath, changes.Copied.Single().OldPath); Assert.Equal(copiedPath, changes.Copied.Single().Path); } }
public void CheckoutRetainsUnstagedChanges() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); using (var repo = Repository.Init(scd.DirectoryPath)) { PopulateBasicRepository(repo); // Generate an unstaged change. string fullPathFileA = Path.Combine(repo.Info.WorkingDirectory, originalFilePath); File.WriteAllText(fullPathFileA, alternateFileContent); // Verify that there is a modified entry. Assert.Equal(1, repo.Index.RetrieveStatus().Modified.Count()); Assert.Equal(FileStatus.Modified, repo.Index.RetrieveStatus(fullPathFileA)); repo.Checkout(otherBranchName); // Verify modified entry still exists. Assert.Equal(1, repo.Index.RetrieveStatus().Modified.Count()); Assert.Equal(FileStatus.Modified, repo.Index.RetrieveStatus(fullPathFileA)); } }