diff options
author | Ben Straub <bs@github.com> | 2013-11-26 08:36:21 +0400 |
---|---|---|
committer | Ben Straub <bs@github.com> | 2013-12-06 03:14:07 +0400 |
commit | f5d9d0ee2803b408ad850ea72c75484876a8c871 (patch) | |
tree | 9d7de13ec2be20b27f4a8be78684df1295328712 /LibGit2Sharp.Tests | |
parent | eb935c1976c52b0b0609bdf9297917b8dee766c6 (diff) |
Revise API for rename/copy detection
Diffstat (limited to 'LibGit2Sharp.Tests')
-rw-r--r-- | LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs | 369 |
1 files changed, 292 insertions, 77 deletions
diff --git a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs index e0c7f314..04d58461 100644 --- a/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs +++ b/LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs @@ -189,18 +189,14 @@ namespace LibGit2Sharp.Tests * 1 file changed, 1 insertion(+) */ [Fact] - public void CanDetectTheRenamingOfAModifiedFile() + public void DetectsTheRenamingOfAModifiedFileByDefault() { using (var repo = new Repository(StandardTestRepoPath)) { Tree rootCommitTree = repo.Lookup<Commit>("f8d44d7").Tree; Tree commitTreeWithRenamedFile = repo.Lookup<Commit>("4be51d6").Tree; - var changes = repo.Diff.Compare<TreeChanges>(rootCommitTree, commitTreeWithRenamedFile, - compareOptions: new CompareOptions - { - DetectRenames = true, - }); + var changes = repo.Diff.Compare<TreeChanges>(rootCommitTree, commitTreeWithRenamedFile); Assert.Equal(1, changes.Count()); Assert.Equal("super-file.txt", changes["super-file.txt"].Path); @@ -210,16 +206,92 @@ namespace LibGit2Sharp.Tests } [Fact] - public void CanDetectTheExactRenamingOfFilesWhenEnabled() + public void DetectsTheExactRenamingOfFilesByDefault() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { - string originalPath = Path.Combine(repo.Info.WorkingDirectory, "original.txt"); - string renamedPath = Path.Combine(repo.Info.WorkingDirectory, "renamed.txt"); + const string originalPath = "original.txt"; + const string renamedPath = "renamed.txt"; - File.WriteAllText(originalPath, "a\nb\nc\nd\n"); + Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); + + repo.Index.Stage(originalPath); + + Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); + + repo.Index.Move(originalPath, renamedPath); + + Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); + + TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree); + + Assert.Equal(1, changes.Count()); + Assert.Equal(1, changes.Renamed.Count()); + Assert.Equal("original.txt", changes.Renamed.Single().OldPath); + Assert.Equal("renamed.txt", changes.Renamed.Single().Path); + } + } + + [Fact(Skip = "Not supported by libgit2 as yet")] + public void RenameDetectionObeysConfigurationSetting() + { + // TODO: set the repo's diff.renames setting, and pass a structure that adjusts thresholds + } + + [Fact] + 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; + TreeChanges 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)); + } + } + + [Fact] + public void ExactModeDetectsExactRenames() + { + 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.Index.Stage(originalPath); @@ -230,11 +302,10 @@ namespace LibGit2Sharp.Tests Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, - compareOptions: - new CompareOptions - { - DetectRenames = true, - }); + compareOptions: new CompareOptions + { + Similarity = SimilarityOptions.Exact, + }); Assert.Equal(1, changes.Count()); Assert.Equal(1, changes.Renamed.Count()); @@ -244,16 +315,122 @@ namespace LibGit2Sharp.Tests } [Fact] + public void ExactModeDetectsExactCopies() + { + 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"; + var originalFullPath = Path.Combine(repo.Info.WorkingDirectory, originalPath); + var 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); + repo.Index.Stage(copiedPath); + + Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); + + TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, + compareOptions: new CompareOptions + { + Similarity = SimilarityOptions.Exact, + }); + + Assert.Equal(1, changes.Count()); + Assert.Equal(1, changes.Copied.Count()); + } + } + + [Fact] + 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.Index.Stage(originalPath); + + Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); + + repo.Index.Move(originalPath, renamedPath); + File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, renamedPath), "e\nf\n"); + repo.Index.Stage(renamedPath); + + Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); + + TreeChanges 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()); + } + } + + [Fact] + public void CanIncludeUnmodifiedEntriesWhenDetectingTheExactRenamingOfFilesWhenEnabled() + { + 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); + repo.Index.Stage(copiedPath); + + Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); + + TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, + compareOptions: + new CompareOptions + { + Similarity = SimilarityOptions.CopiesHarder, + IncludeUnmodified = true, + }); + + Assert.Equal(2, changes.Count()); + Assert.Equal(1, changes.Unmodified.Count()); + Assert.Equal(1, changes.Copied.Count()); + Assert.Equal("original.txt", changes.Copied.Single().OldPath); + Assert.Equal("copied.txt", changes.Copied.Single().Path); + } + } + + [Fact] public void CanNotDetectTheExactRenamingFilesWhenNotEnabled() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { - string originalPath = Path.Combine(repo.Info.WorkingDirectory, "original.txt"); - string renamedPath = Path.Combine(repo.Info.WorkingDirectory, "renamed.txt"); + const string originalPath = "original.txt"; + const string renamedPath = "renamed.txt"; - File.WriteAllText(originalPath, "a\nb\nc\nd\n"); + Touch(repo.Info.WorkingDirectory, originalPath, "a\nb\nc\nd\n"); repo.Index.Stage(originalPath); @@ -264,11 +441,11 @@ namespace LibGit2Sharp.Tests Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, - compareOptions: - new CompareOptions - { - DetectRenames = false, - }); + compareOptions: + new CompareOptions + { + Similarity = SimilarityOptions.None, + }); Assert.Equal(2, changes.Count()); Assert.Equal(0, changes.Renamed.Count()); @@ -282,26 +459,28 @@ namespace LibGit2Sharp.Tests var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { - string originalPath = Path.Combine(repo.Info.WorkingDirectory, "original.txt"); - string copiedPath = Path.Combine(repo.Info.WorkingDirectory, "copied.txt"); + 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); - File.WriteAllText(originalPath, "a\nb\nc\nd\n"); + 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(originalPath, copiedPath); + File.Copy(originalFullPath, copiedFullPath); repo.Index.Stage(copiedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, - compareOptions: - new CompareOptions - { - DetectCopiesFromUnmodified = true, - }); + compareOptions: + new CompareOptions + { + Similarity = SimilarityOptions.CopiesHarder, + }); Assert.Equal(1, changes.Count()); Assert.Equal(1, changes.Copied.Count()); @@ -317,26 +496,23 @@ namespace LibGit2Sharp.Tests var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { - string originalPath = Path.Combine(repo.Info.WorkingDirectory, "original.txt"); - string copiedPath = Path.Combine(repo.Info.WorkingDirectory, "copied.txt"); + 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); - File.WriteAllText(originalPath, "a\nb\nc\nd\n"); + 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(originalPath, copiedPath); + File.Copy(originalFullPath, copiedFullPath); repo.Index.Stage(copiedPath); Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); - TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, - compareOptions: - new CompareOptions - { - DetectCopies = false, - }); + TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree); Assert.Equal(1, changes.Count()); Assert.Equal(0, changes.Copied.Count()); @@ -350,17 +526,19 @@ namespace LibGit2Sharp.Tests var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { - string originalPath = Path.Combine(repo.Info.WorkingDirectory, "original.txt"); - string copiedPath = Path.Combine(repo.Info.WorkingDirectory, "copied.txt"); + 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); - File.WriteAllText(originalPath, "a\nb\nc\nd\n"); + 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(originalPath, copiedPath); - File.AppendAllText(originalPath, "e\n"); + File.Copy(originalFullPath, copiedFullPath); + Touch(repo.Info.WorkingDirectory, originalPath, "e\n"); repo.Index.Stage(originalPath); repo.Index.Stage(copiedPath); @@ -368,11 +546,11 @@ namespace LibGit2Sharp.Tests Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, - compareOptions: - new CompareOptions - { - DetectCopies = true, - }); + compareOptions: + new CompareOptions + { + Similarity = SimilarityOptions.Copies, + }); Assert.Equal(2, changes.Count()); Assert.Equal(1, changes.Copied.Count()); @@ -388,17 +566,19 @@ namespace LibGit2Sharp.Tests var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { - string originalPath = Path.Combine(repo.Info.WorkingDirectory, "original.txt"); - string copiedPath = Path.Combine(repo.Info.WorkingDirectory, "copied.txt"); + 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); - File.WriteAllText(originalPath, "a\nb\nc\nd\n"); + 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(originalPath, copiedPath); - File.AppendAllText(originalPath, "e\n"); + File.Copy(originalFullPath, copiedFullPath); + File.AppendAllText(originalFullPath, "e\n"); repo.Index.Stage(originalPath); repo.Index.Stage(copiedPath); @@ -413,22 +593,48 @@ namespace LibGit2Sharp.Tests } [Fact] + public void CanIncludeUnmodifiedEntriesWhenEnabled() + { + SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); + var path = Repository.Init(scd.DirectoryPath); + using (var repo = new Repository(path)) + { + Touch(repo.Info.WorkingDirectory, "a.txt", "abc\ndef\n"); + Touch(repo.Info.WorkingDirectory, "b.txt", "abc\ndef\n"); + + repo.Index.Stage(new[] {"a.txt", "b.txt"}); + Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); + + File.AppendAllText(Path.Combine(repo.Info.WorkingDirectory, "b.txt"), "ghi\njkl\n"); + repo.Index.Stage("b.txt"); + Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); + + TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, + compareOptions: new CompareOptions {IncludeUnmodified = true}); + + Assert.Equal(2, changes.Count()); + Assert.Equal(1, changes.Unmodified.Count()); + Assert.Equal(1, changes.Modified.Count()); + } + } + + [Fact] public void CanDetectTheExactRenamingExactCopyingOfNonModifiedAndModifiedFilesWhenEnabled() { SelfCleaningDirectory scd = BuildSelfCleaningDirectory(); var path = Repository.Init(scd.DirectoryPath); using (var repo = new Repository(path)) { - string originalPath = Path.Combine(repo.Info.WorkingDirectory, "original.txt"); - string renamedPath = Path.Combine(repo.Info.WorkingDirectory, "renamed.txt"); - string originalPath2 = Path.Combine(repo.Info.WorkingDirectory, "original2.txt"); - string copiedPath1 = Path.Combine(repo.Info.WorkingDirectory, "copied.txt"); - string originalPath3 = Path.Combine(repo.Info.WorkingDirectory, "original3.txt"); - string copiedPath2 = Path.Combine(repo.Info.WorkingDirectory, "copied2.txt"); + 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"; - File.WriteAllText(originalPath, "a\nb\nc\nd\n"); - File.WriteAllText(originalPath2, "1\n2\n3\n4\n"); - File.WriteAllText(originalPath3, "5\n6\n7\n8\n"); + 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.Index.Stage(originalPath); repo.Index.Stage(originalPath2); @@ -436,9 +642,13 @@ namespace LibGit2Sharp.Tests Commit old = repo.Commit("Initial", Constants.Signature, Constants.Signature); - File.Copy(originalPath2, copiedPath1); - File.Copy(originalPath3, copiedPath2); - File.AppendAllText(originalPath3, "9\n"); + 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.Index.Stage(originalPath3); repo.Index.Stage(copiedPath1); @@ -448,11 +658,11 @@ namespace LibGit2Sharp.Tests Commit @new = repo.Commit("Updated", Constants.Signature, Constants.Signature); TreeChanges changes = repo.Diff.Compare<TreeChanges>(old.Tree, @new.Tree, - compareOptions: - new CompareOptions - { - DetectCopiesFromUnmodified = true, - }); + compareOptions: + new CompareOptions + { + Similarity = SimilarityOptions.CopiesHarder, + }); Assert.Equal(4, changes.Count()); Assert.Equal(1, changes.Modified.Count()); @@ -587,6 +797,7 @@ namespace LibGit2Sharp.Tests { ContextLines = contextLines, InterhunkLines = interhunkLines, + Similarity = SimilarityOptions.None, }; using (var repo = new Repository(StandardTestRepoPath)) @@ -594,7 +805,7 @@ namespace LibGit2Sharp.Tests Tree rootCommitTree = repo.Lookup<Commit>("f8d44d7").Tree; Tree mergedCommitTree = repo.Lookup<Commit>("7252fe2").Tree; - var changes = repo.Diff.Compare<TreeChanges>(rootCommitTree, mergedCommitTree); + var changes = repo.Diff.Compare<TreeChanges>(rootCommitTree, mergedCommitTree, compareOptions: compareOptions); Assert.Equal(3, changes.Count()); Assert.Equal(1, changes.Modified.Count()); @@ -639,7 +850,11 @@ namespace LibGit2Sharp.Tests Tree treeNew = repo.ObjectDatabase.CreateTree(tdNew); - var changes = repo.Diff.Compare<TreeChanges>(treeOld, treeNew); + var changes = repo.Diff.Compare<TreeChanges>(treeOld, treeNew, + compareOptions: new CompareOptions + { + Similarity = SimilarityOptions.None, + }); /* * $ git diff-tree -p 5c87b67 d5278d0 @@ -776,7 +991,7 @@ namespace LibGit2Sharp.Tests StringBuilder sb = new StringBuilder() .AppendFormat("[core]{0}", Environment.NewLine) .AppendFormat("filemode = {1}{0}", Environment.NewLine, value); - File.WriteAllText(options.SystemConfigurationLocation, sb.ToString()); + Touch("", options.SystemConfigurationLocation, sb.ToString()); return options; } |