private void MoveFile() { if (!Directory.Exists(Path.GetDirectoryName(repo.Info.WorkingDirectory + targetPattern))) { Directory.CreateDirectory(Path.GetDirectoryName(repo.Info.WorkingDirectory + targetPattern)); } repo.Move(sourcePattern, targetPattern); }
private void MoveFile() { // Why move does not create the directory, I have no idea. But it killed about two days figuring it out // given that the error message is crazy bad. if (!Directory.Exists(Path.GetDirectoryName(repo.Info.WorkingDirectory + targetPattern))) { Directory.CreateDirectory(Path.GetDirectoryName(repo.Info.WorkingDirectory + targetPattern)); } repo.Move(sourcePattern, targetPattern); }
public void ExactModeDoesntDetectRenamesWithEdits() { 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"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); repo.Move(originalPath, renamedPath); File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, renamedPath), "e\nf\n"); repo.Stage(renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare <TreeChanges>(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.Exact, }); Assert.Equal(2, changes.Count()); Assert.Equal(0, changes.Renamed.Count()); Assert.Equal(1, changes.Added.Count()); Assert.Equal(1, changes.Deleted.Count()); } }
public void CanNotDetectTheExactRenamingFilesWhenNotEnabled() { 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"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); repo.Move(originalPath, renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare <TreeChanges>(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.None, }); Assert.Equal(2, changes.Count()); Assert.Equal(0, changes.Renamed.Count()); } }
public void DetectsTheExactRenamingOfFilesByDefault() { 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"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); repo.Move(originalPath, renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare <TreeChanges>(old.Tree, @new.Tree); Assert.Equal(1, changes.Count()); Assert.Equal(1, changes.Renamed.Count()); Assert.Equal(originalPath, changes.Renamed.Single().OldPath); Assert.Equal(renamedPath, changes.Renamed.Single().Path); } }
public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWhenEnabled() { 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"; const string originalPath2 = "original2.txt"; const string copiedPath1 = "copied.txt"; const string originalPath3 = "original3.txt"; const string copiedPath2 = "copied2.txt"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); Touch(repo.Info.WorkingDirectory, originalPath2, "1\n2\n3\n4\n"); Touch(repo.Info.WorkingDirectory, originalPath3, "5\n6\n7\n8\n"); repo.Stage(originalPath); repo.Stage(originalPath2); repo.Stage(originalPath3); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); var originalFullPath2 = Path.Combine(repo.Info.WorkingDirectory, originalPath2); var originalFullPath3 = Path.Combine(repo.Info.WorkingDirectory, originalPath3); var copiedFullPath1 = Path.Combine(repo.Info.WorkingDirectory, copiedPath1); var copiedFullPath2 = Path.Combine(repo.Info.WorkingDirectory, copiedPath2); File.Copy(originalFullPath2, copiedFullPath1); File.Copy(originalFullPath3, copiedFullPath2); File.AppendAllText(originalFullPath3, "9\n"); repo.Stage(originalPath3); repo.Stage(copiedPath1); repo.Stage(copiedPath2); repo.Move(originalPath, renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare <TreeChanges>(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.CopiesHarder, }); Assert.Equal(4, changes.Count()); Assert.Equal(1, changes.Modified.Count()); Assert.Equal(1, changes.Renamed.Count()); Assert.Equal(originalPath, changes.Renamed.Single().OldPath); Assert.Equal(renamedPath, changes.Renamed.Single().Path); Assert.Equal(2, changes.Copied.Count()); Assert.Equal(originalPath2, changes.Copied.ElementAt(0).OldPath); Assert.Equal(copiedPath1, changes.Copied.ElementAt(0).Path); Assert.Equal(originalPath3, changes.Copied.ElementAt(1).OldPath); Assert.Equal(copiedPath2, changes.Copied.ElementAt(1).Path); } }
public void CanResetTheIndexWhenARenameExists() { using (var repo = new Repository(SandboxStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); repo.Reset(repo.Lookup <Commit>("32eab9c")); RepositoryStatus status = repo.RetrieveStatus(); Assert.Equal(0, status.Where(IsStaged).Count()); } }
public void CanUnstageBothSidesOfARename() { using (var repo = new Repository(SandboxStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); repo.Unstage(new string[] { "branch_file.txt", "renamed_branch_file.txt" }); RepositoryStatus status = repo.RetrieveStatus(); Assert.Equal(FileStatus.Missing, status["branch_file.txt"].State); Assert.Equal(FileStatus.Untracked, status["renamed_branch_file.txt"].State); } }
public void CanUnstageBothSidesOfARename() { using (var repo = new Repository(CloneStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); repo.Unstage(new string[] { "branch_file.txt", "renamed_branch_file.txt" }); RepositoryStatus status = repo.RetrieveStatus(); Assert.Equal(FileStatus.Missing, status["branch_file.txt"].State); Assert.Equal(FileStatus.Untracked, status["renamed_branch_file.txt"].State); } }
private static void InvalidMoveUseCases(string sourcePath, FileStatus sourceStatus, IEnumerable <string> destPaths) { using (var repo = new Repository(StandardTestRepoPath)) { Assert.Equal(sourceStatus, repo.RetrieveStatus(sourcePath)); foreach (var destPath in destPaths) { string path = destPath; Assert.Throws <LibGit2SharpException>(() => repo.Move(sourcePath, path)); } } }
public Attempt <OperationResult <MoveOperationStatusType> > Move(TItem moving, int containerId) { var evtMsgs = EventMessagesFactory.Get(); var moveInfo = new List <MoveEventInfo <TItem> >(); using (var scope = ScopeProvider.CreateScope()) { var moveEventInfo = new MoveEventInfo <TItem>(moving, moving.Path, containerId); var moveEventArgs = new MoveEventArgs <TItem>(evtMsgs, moveEventInfo); if (OnMovingCancelled(scope, moveEventArgs)) { scope.Complete(); return(OperationResult.Attempt.Fail(MoveOperationStatusType.FailedCancelledByEvent, evtMsgs)); } scope.WriteLock(WriteLockIds); // also for containers try { EntityContainer container = null; if (containerId > 0) { container = _containerRepository.Get(containerId); if (container == null) { throw new DataOperationException <MoveOperationStatusType>(MoveOperationStatusType.FailedParentNotFound); // causes rollback } } moveInfo.AddRange(Repository.Move(moving, container)); scope.Complete(); } catch (DataOperationException <MoveOperationStatusType> ex) { scope.Complete(); return(OperationResult.Attempt.Fail(ex.Operation, evtMsgs)); } // note: not raising any Changed event here because moving a content type under another container // has no impact on the published content types - would be entirely different if we were to support // moving a content type under another content type. moveEventArgs.MoveInfoCollection = moveInfo; moveEventArgs.CanCancel = false; OnMoved(scope, moveEventArgs); } return(OperationResult.Attempt.Succeed(MoveOperationStatusType.Success, evtMsgs)); }
public void CanMoveAnExistingFileOverANonExistingFile(string sourcePath, FileStatus sourceStatus, string destPath, FileStatus destStatus, FileStatus sourcePostStatus, FileStatus destPostStatus) { string path = SandboxStandardTestRepo(); using (var repo = new Repository(path)) { Assert.Equal(sourceStatus, repo.RetrieveStatus(sourcePath)); Assert.Equal(destStatus, repo.RetrieveStatus(destPath)); repo.Move(sourcePath, destPath); Assert.Equal(sourcePostStatus, repo.RetrieveStatus(sourcePath)); Assert.Equal(destPostStatus, repo.RetrieveStatus(destPath)); } }
public void CanRenameAFile() { string repoPath = InitNewRepository(); using (var repo = new Repository(repoPath)) { Assert.Equal(0, repo.Index.Count); const string oldName = "polite.txt"; Assert.Equal(FileStatus.Nonexistent, repo.RetrieveStatus(oldName)); Touch(repo.Info.WorkingDirectory, oldName, "hello test file\n"); Assert.Equal(FileStatus.NewInWorkdir, repo.RetrieveStatus(oldName)); repo.Stage(oldName); Assert.Equal(FileStatus.NewInIndex, repo.RetrieveStatus(oldName)); // Generated through // $ echo "hello test file" | git hash-object --stdin const string expectedHash = "88df547706c30fa19f02f43cb2396e8129acfd9b"; Assert.Equal(expectedHash, repo.Index[oldName].Id.Sha); Assert.Equal(1, repo.Index.Count); Signature who = Constants.Signature; repo.Commit("Initial commit", who, who); Assert.Equal(FileStatus.Unaltered, repo.RetrieveStatus(oldName)); const string newName = "being.frakking.polite.txt"; repo.Move(oldName, newName); Assert.Equal(FileStatus.DeletedFromIndex, repo.RetrieveStatus(oldName)); Assert.Equal(FileStatus.NewInIndex, repo.RetrieveStatus(newName)); Assert.Equal(1, repo.Index.Count); Assert.Equal(expectedHash, repo.Index[newName].Id.Sha); who = who.TimeShift(TimeSpan.FromMinutes(5)); Commit commit = repo.Commit("Fix file name", who, who); Assert.Equal(FileStatus.Nonexistent, repo.RetrieveStatus(oldName)); Assert.Equal(FileStatus.Unaltered, repo.RetrieveStatus(newName)); Assert.Equal(expectedHash, commit.Tree[newName].Target.Id.Sha); } }
public void CanResetTargetOfARenameInIndex() { using (var repo = new Repository(CloneStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Reset(repo.Lookup<Commit>("32eab9c"), new string[] { "renamed_branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Untracked, newStatus["renamed_branch_file.txt"].State); Assert.Equal(FileStatus.Removed, newStatus["branch_file.txt"].State); } }
public void CanUnstageTargetOfARename() { using (var repo = new Repository(CloneStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Unstage(new string[] { "renamed_branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Untracked, newStatus["renamed_branch_file.txt"].State); Assert.Equal(FileStatus.Removed, newStatus["branch_file.txt"].State); } }
public void CanResetTargetOfARenameInIndex() { using (var repo = new Repository(SandboxStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Reset(repo.Lookup <Commit>("32eab9c"), new string[] { "renamed_branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Untracked, newStatus["renamed_branch_file.txt"].State); Assert.Equal(FileStatus.Removed, newStatus["branch_file.txt"].State); } }
public void CanResetSourceOfARenameInIndex() { using (var repo = new Repository(CloneStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Reset(repo.Lookup <Commit>("32eab9c"), new string[] { "branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Missing, newStatus["branch_file.txt"].State); Assert.Equal(FileStatus.Added, newStatus["renamed_branch_file.txt"].State); } }
public void CanTellComplexCommitHistory() { var repoPath = CreateEmptyRepository(); const string path1 = "Test1.txt"; const string path2 = "Test2.txt"; using (var repo = new Repository(repoPath)) { // Make initial changes. var commit1 = MakeAndCommitChange(repo, repoPath, path1, "Hello World"); MakeAndCommitChange(repo, repoPath, path2, "Second file's contents"); var commit2 = MakeAndCommitChange(repo, repoPath, path1, "Hello World again"); // Move the first file to a new directory. var newPath1 = Path.Combine(SubFolderPath1, path1); repo.Move(path1, newPath1); var commit3 = repo.Commit("Moved " + path1 + " to " + newPath1, Constants.Signature, Constants.Signature); // Make further changes. MakeAndCommitChange(repo, repoPath, path2, "Changed second file's contents"); var commit4 = MakeAndCommitChange(repo, repoPath, newPath1, "I have done it again!"); // Perform tests. var fileHistoryEntries = repo.Commits.QueryBy(newPath1).ToList(); var changedBlobs = fileHistoryEntries.Blobs().Distinct().ToList(); Assert.Equal(4, fileHistoryEntries.Count()); Assert.Equal(3, changedBlobs.Count()); Assert.Equal(2, fileHistoryEntries.Count(e => e.Path == newPath1)); Assert.Equal(2, fileHistoryEntries.Count(e => e.Path == path1)); Assert.Equal(commit4, fileHistoryEntries[0].Commit); Assert.Equal(commit3, fileHistoryEntries[1].Commit); Assert.Equal(commit2, fileHistoryEntries[2].Commit); Assert.Equal(commit1, fileHistoryEntries[3].Commit); Assert.Equal(commit4.Tree[newPath1].Target, changedBlobs[0]); Assert.Equal(commit2.Tree[path1].Target, changedBlobs[1]); Assert.Equal(commit1.Tree[path1].Target, changedBlobs[2]); } }
public void CanUnstageSourceOfARename() { using (var repo = new Repository(SandboxStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Unstage(new string[] { "branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Missing, newStatus["branch_file.txt"].State); Assert.Equal(FileStatus.Added, newStatus["renamed_branch_file.txt"].State); } }
public void CanUnstageSourceOfARename() { using (var repo = new Repository(CloneStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Unstage(new string[] { "branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Missing, newStatus["branch_file.txt"].State); Assert.Equal(FileStatus.Added, newStatus["renamed_branch_file.txt"].State); } }
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.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.Stage(originalPath); repo.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 CanNotDetectTheExactRenamingFilesWhenNotEnabled() { 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"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); repo.Move(originalPath, renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.None, }); Assert.Equal(2, changes.Count()); Assert.Equal(0, changes.Renamed.Count()); } }
public void CanResetSourceOfARenameInIndex() { using (var repo = new Repository(SandboxStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Nonexistent, oldStatus["branch_file.txt"].State); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Reset(repo.Lookup<Commit>("32eab9c"), new string[] { "branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Missing, newStatus["branch_file.txt"].State); Assert.Equal(FileStatus.Added, newStatus["renamed_branch_file.txt"].State); } }
public void CanResetTheIndexWhenARenameExists() { using (var repo = new Repository(CloneStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); repo.Reset(repo.Lookup<Commit>("32eab9c")); RepositoryStatus status = repo.RetrieveStatus(); Assert.Equal(0, status.Where(IsStaged).Count()); } }
public void CanUnstageTargetOfARename() { using (var repo = new Repository(SandboxStandardTestRepo())) { repo.Move("branch_file.txt", "renamed_branch_file.txt"); RepositoryStatus oldStatus = repo.RetrieveStatus(); Assert.Equal(1, oldStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.RenamedInIndex, oldStatus["renamed_branch_file.txt"].State); repo.Unstage(new string[] { "renamed_branch_file.txt" }); RepositoryStatus newStatus = repo.RetrieveStatus(); Assert.Equal(0, newStatus.RenamedInIndex.Count()); Assert.Equal(FileStatus.Untracked, newStatus["renamed_branch_file.txt"].State); Assert.Equal(FileStatus.Removed, newStatus["branch_file.txt"].State); } }
public void CanRenameAFile() { string repoPath = InitNewRepository(); using (var repo = new Repository(repoPath)) { Assert.Equal(0, repo.Index.Count); const string oldName = "polite.txt"; Assert.Equal(FileStatus.Nonexistent, repo.RetrieveStatus(oldName)); Touch(repo.Info.WorkingDirectory, oldName, "hello test file\n"); Assert.Equal(FileStatus.Untracked, repo.RetrieveStatus(oldName)); repo.Stage(oldName); Assert.Equal(FileStatus.Added, repo.RetrieveStatus(oldName)); // Generated through // $ echo "hello test file" | git hash-object --stdin const string expectedHash = "88df547706c30fa19f02f43cb2396e8129acfd9b"; Assert.Equal(expectedHash, repo.Index[oldName].Id.Sha); Assert.Equal(1, repo.Index.Count); Signature who = Constants.Signature; repo.Commit("Initial commit", who, who); Assert.Equal(FileStatus.Unaltered, repo.RetrieveStatus(oldName)); const string newName = "being.frakking.polite.txt"; repo.Move(oldName, newName); Assert.Equal(FileStatus.Removed, repo.RetrieveStatus(oldName)); Assert.Equal(FileStatus.Added, repo.RetrieveStatus(newName)); Assert.Equal(1, repo.Index.Count); Assert.Equal(expectedHash, repo.Index[newName].Id.Sha); who = who.TimeShift(TimeSpan.FromMinutes(5)); Commit commit = repo.Commit("Fix file name", who, who); Assert.Equal(FileStatus.Nonexistent, repo.RetrieveStatus(oldName)); Assert.Equal(FileStatus.Unaltered, repo.RetrieveStatus(newName)); Assert.Equal(expectedHash, commit.Tree[newName].Target.Id.Sha); } }
public void DetectsTheExactRenamingOfFilesByDefault() { 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"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); repo.Move(originalPath, renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree); Assert.Equal(1, changes.Count()); Assert.Equal(1, changes.Renamed.Count()); Assert.Equal(originalPath, changes.Renamed.Single().OldPath); Assert.Equal(renamedPath, changes.Renamed.Single().Path); } }
public void ExactModeDoesntDetectRenamesWithEdits() { 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"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Stage(originalPath); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); repo.Move(originalPath, renamedPath); File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, renamedPath), "e\nf\n"); repo.Stage(renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.Exact, }); Assert.Equal(2, changes.Count()); Assert.Equal(0, changes.Renamed.Count()); Assert.Equal(1, changes.Added.Count()); Assert.Equal(1, changes.Deleted.Count()); } }
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.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.Stage(originalPath); repo.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 CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWhenEnabled() { 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"; const string originalPath2 = "original2.txt"; const string copiedPath1 = "copied.txt"; const string originalPath3 = "original3.txt"; const string copiedPath2 = "copied2.txt"; Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); Touch(repo.Info.WorkingDirectory, originalPath2, "1\n2\n3\n4\n"); Touch(repo.Info.WorkingDirectory, originalPath3, "5\n6\n7\n8\n"); repo.Stage(originalPath); repo.Stage(originalPath2); repo.Stage(originalPath3); Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); var originalFullPath2 = Path.Combine(repo.Info.WorkingDirectory, originalPath2); var originalFullPath3 = Path.Combine(repo.Info.WorkingDirectory, originalPath3); var copiedFullPath1 = Path.Combine(repo.Info.WorkingDirectory, copiedPath1); var copiedFullPath2 = Path.Combine(repo.Info.WorkingDirectory, copiedPath2); File.Copy(originalFullPath2, copiedFullPath1); File.Copy(originalFullPath3, copiedFullPath2); File.AppendAllText(originalFullPath3, "9\n"); repo.Stage(originalPath3); repo.Stage(copiedPath1); repo.Stage(copiedPath2); repo.Move(originalPath, renamedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); var changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, compareOptions: new CompareOptions { Similarity = SimilarityOptions.CopiesHarder, }); Assert.Equal(4, changes.Count()); Assert.Equal(1, changes.Modified.Count()); Assert.Equal(1, changes.Renamed.Count()); Assert.Equal(originalPath, changes.Renamed.Single().OldPath); Assert.Equal(renamedPath, changes.Renamed.Single().Path); Assert.Equal(2, changes.Copied.Count()); Assert.Equal(originalPath2, changes.Copied.ElementAt(0).OldPath); Assert.Equal(copiedPath1, changes.Copied.ElementAt(0).Path); Assert.Equal(originalPath3, changes.Copied.ElementAt(1).OldPath); Assert.Equal(copiedPath2, changes.Copied.ElementAt(1).Path); } }
private void InvalidMoveUseCases(string sourcePath, FileStatus sourceStatus, IEnumerable<string> destPaths) { var repoPath = SandboxStandardTestRepoGitDir(); using (var repo = new Repository(repoPath)) { Assert.Equal(sourceStatus, repo.RetrieveStatus(sourcePath)); foreach (var destPath in destPaths) { string path = destPath; Assert.Throws<LibGit2SharpException>(() => repo.Move(sourcePath, path)); } } }