Skip to content

Commit 2e3b534

Browse files
committed
Drop TreeChanges path indexer
Fix #1004
1 parent c0afb34 commit 2e3b534

File tree

2 files changed

+63
-70
lines changed

2 files changed

+63
-70
lines changed

LibGit2Sharp.Tests/DiffTreeToTreeFixture.cs

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void RetrievingANonExistentFileChangeReturnsNull()
3939

4040
var changes = repo.Diff.Compare<TreeChanges>(tree, tree);
4141

42-
Assert.Null(changes["batman"]);
42+
Assert.Equal(0, changes.Count(c => c.Path == "batman"));
4343
}
4444
}
4545

@@ -62,7 +62,7 @@ public void CanCompareACommitTreeAgainstItsParent()
6262
Assert.Equal(1, changes.Count());
6363
Assert.Equal(1, changes.Added.Count());
6464

65-
TreeEntryChanges treeEntryChanges = changes["1.txt"];
65+
TreeEntryChanges treeEntryChanges = changes.Single(c => c.Path == "1.txt");
6666

6767
var patch = repo.Diff.Compare<Patch>(parentCommitTree, commitTree);
6868
Assert.False(patch["1.txt"].IsBinaryComparison);
@@ -272,8 +272,7 @@ public void DetectsTheRenamingOfAModifiedFileByDefault()
272272
var changes = repo.Diff.Compare<TreeChanges>(rootCommitTree, commitTreeWithRenamedFile);
273273

274274
Assert.Equal(1, changes.Count());
275-
Assert.Equal("super-file.txt", changes["super-file.txt"].Path);
276-
Assert.Equal("my-name-does-not-feel-right.txt", changes["super-file.txt"].OldPath);
275+
Assert.Equal("my-name-does-not-feel-right.txt", changes.Single(c => c.Path == "super-file.txt").OldPath);
277276
Assert.Equal(1, changes.Renamed.Count());
278277
}
279278
}
@@ -888,7 +887,7 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks(int contextLines, in
888887
Assert.Equal(1, changes.Deleted.Count());
889888
Assert.Equal(1, changes.Added.Count());
890889

891-
Assert.Equal(Mode.Nonexistent, changes["my-name-does-not-feel-right.txt"].Mode);
890+
Assert.Equal(Mode.Nonexistent, changes.Single(c => c.Path =="my-name-does-not-feel-right.txt").Mode);
892891

893892
var patch = repo.Diff.Compare<Patch>(rootCommitTree, mergedCommitTree, compareOptions: compareOptions);
894893

@@ -904,17 +903,16 @@ public void CanCompareTwoVersionsOfAFileWithADiffOfTwoHunks(int contextLines, in
904903
}
905904
}
906905

907-
[Fact]
908-
public void CanHandleTwoTreeEntryChangesWithTheSamePath()
906+
private void CanHandleTwoTreeEntryChangesWithTheSamePath(SimilarityOptions similarity, Action<string, TreeChanges> verifier)
909907
{
910908
string repoPath = InitNewRepository();
911909

912910
using (var repo = new Repository(repoPath))
913911
{
914-
Blob mainContent = OdbHelper.CreateBlob(repo, "awesome content\n");
912+
Blob mainContent = OdbHelper.CreateBlob(repo, "awesome content\n" + new string('b', 4096));
915913
Blob linkContent = OdbHelper.CreateBlob(repo, "../../objc/Nu.h");
916914

917-
string path = string.Format("include{0}Nu{0}Nu.h", Path.DirectorySeparatorChar);
915+
string path = Path.Combine("include", "Nu", "Nu.h");
918916

919917
var tdOld = new TreeDefinition()
920918
.Add(path, linkContent, Mode.SymbolicLink)
@@ -930,45 +928,61 @@ public void CanHandleTwoTreeEntryChangesWithTheSamePath()
930928
var changes = repo.Diff.Compare<TreeChanges>(treeOld, treeNew,
931929
compareOptions: new CompareOptions
932930
{
933-
Similarity = SimilarityOptions.None,
931+
Similarity = similarity,
934932
});
935933

936-
/*
937-
* $ git diff-tree -p 5c87b67 d5278d0
938-
* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
939-
* deleted file mode 120000
940-
* index 19bf568..0000000
941-
* --- a/include/Nu/Nu.h
942-
* +++ /dev/null
943-
* @@ -1 +0,0 @@
944-
* -../../objc/Nu.h
945-
* \ No newline at end of file
946-
* diff --git a/include/Nu/Nu.h b/include/Nu/Nu.h
947-
* new file mode 100644
948-
* index 0000000..f9e6561
949-
* --- /dev/null
950-
* +++ b/include/Nu/Nu.h
951-
* @@ -0,0 +1 @@
952-
* +awesome content
953-
* diff --git a/objc/Nu.h b/objc/Nu.h
954-
* deleted file mode 100644
955-
* index f9e6561..0000000
956-
* --- a/objc/Nu.h
957-
* +++ /dev/null
958-
* @@ -1 +0,0 @@
959-
* -awesome content
960-
*/
934+
verifier(path, changes);
935+
}
936+
}
961937

962-
Assert.Equal(1, changes.Deleted.Count());
963-
Assert.Equal(0, changes.Modified.Count());
964-
Assert.Equal(1, changes.TypeChanged.Count());
938+
[Fact]
939+
public void CanHandleTwoTreeEntryChangesWithTheSamePathUsingSimilarityNone()
940+
{
941+
// $ git diff-tree --name-status --no-renames -r 2ccccf8 e829333
942+
// T include/Nu/Nu.h
943+
// D objc/Nu.h
965944

966-
TreeEntryChanges change = changes[path];
967-
Assert.Equal(Mode.SymbolicLink, change.OldMode);
968-
Assert.Equal(Mode.NonExecutableFile, change.Mode);
969-
Assert.Equal(ChangeKind.TypeChanged, change.Status);
970-
Assert.Equal(path, change.Path);
971-
}
945+
CanHandleTwoTreeEntryChangesWithTheSamePath(SimilarityOptions.None,
946+
(path, changes) =>
947+
{
948+
Assert.Equal(2, changes.Count());
949+
Assert.Equal(1, changes.Deleted.Count());
950+
Assert.Equal(1, changes.TypeChanged.Count());
951+
952+
TreeEntryChanges change = changes.Single(c => c.Path== path);
953+
Assert.Equal(Mode.SymbolicLink, change.OldMode);
954+
Assert.Equal(Mode.NonExecutableFile, change.Mode);
955+
Assert.Equal(ChangeKind.TypeChanged, change.Status);
956+
Assert.Equal(path, change.Path);
957+
});
958+
}
959+
960+
[Fact]
961+
public void CanHandleTwoTreeEntryChangesWithTheSamePathUsingSimilarityDefault()
962+
{
963+
// $ git diff-tree --name-status --find-renames -r 2ccccf8 e829333
964+
// T include/Nu/Nu.h
965+
// D objc/Nu.h
966+
967+
CanHandleTwoTreeEntryChangesWithTheSamePath(SimilarityOptions.Default,
968+
(path, changes) =>
969+
{
970+
Assert.Equal(2, changes.Count());
971+
Assert.Equal(1, changes.Deleted.Count());
972+
Assert.Equal(1, changes.Renamed.Count());
973+
974+
TreeEntryChanges renamed = changes.Renamed.Single();
975+
Assert.Equal(Mode.NonExecutableFile, renamed.OldMode);
976+
Assert.Equal(Mode.NonExecutableFile, renamed.Mode);
977+
Assert.Equal(ChangeKind.Renamed, renamed.Status);
978+
Assert.Equal(path, renamed.Path);
979+
980+
TreeEntryChanges deleted = changes.Deleted.Single();
981+
Assert.Equal(Mode.SymbolicLink, deleted.OldMode);
982+
Assert.Equal(Mode.Nonexistent, deleted.Mode);
983+
Assert.Equal(ChangeKind.Deleted, deleted.Status);
984+
Assert.Equal(path, deleted.Path);
985+
});
972986
}
973987

974988
[Fact]
@@ -1104,8 +1118,8 @@ public void RetrievingDiffChangesMustAlwaysBeCaseSensitive()
11041118
{
11051119
var changes = repo.Diff.Compare<TreeChanges>(repo.Lookup<Tree>(treeOldOid), repo.Lookup<Tree>(treeNewOid));
11061120

1107-
Assert.Equal(ChangeKind.Modified, changes["a.txt"].Status);
1108-
Assert.Equal(ChangeKind.Modified, changes["A.TXT"].Status);
1121+
Assert.Equal(ChangeKind.Modified, changes.Single(c => c.Path == "a.txt").Status);
1122+
Assert.Equal(ChangeKind.Modified, changes.Single(c => c.Path == "A.TXT").Status);
11091123
}
11101124
}
11111125

LibGit2Sharp/TreeChanges.cs

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace LibGit2Sharp
1717
[DebuggerDisplay("{DebuggerDisplay,nq}")]
1818
public class TreeChanges : IEnumerable<TreeEntryChanges>
1919
{
20-
private readonly IDictionary<FilePath, TreeEntryChanges> changes = new Dictionary<FilePath, TreeEntryChanges>();
20+
private readonly List<TreeEntryChanges> changes = new List<TreeEntryChanges>();
2121
private readonly List<TreeEntryChanges> added = new List<TreeEntryChanges>();
2222
private readonly List<TreeEntryChanges> deleted = new List<TreeEntryChanges>();
2323
private readonly List<TreeEntryChanges> modified = new List<TreeEntryChanges>();
@@ -64,7 +64,7 @@ private void AddFileChange(GitDiffDelta delta)
6464
var treeEntryChanges = new TreeEntryChanges(delta);
6565

6666
fileDispatcher[treeEntryChanges.Status](this, treeEntryChanges);
67-
changes.Add(treeEntryChanges.Path, treeEntryChanges);
67+
changes.Add(treeEntryChanges);
6868
}
6969

7070
#region IEnumerable<TreeEntryChanges> Members
@@ -75,7 +75,7 @@ private void AddFileChange(GitDiffDelta delta)
7575
/// <returns>An <see cref="IEnumerator{T}"/> object that can be used to iterate through the collection.</returns>
7676
public virtual IEnumerator<TreeEntryChanges> GetEnumerator()
7777
{
78-
return changes.Values.GetEnumerator();
78+
return changes.GetEnumerator();
7979
}
8080

8181
/// <summary>
@@ -89,27 +89,6 @@ IEnumerator IEnumerable.GetEnumerator()
8989

9090
#endregion
9191

92-
/// <summary>
93-
/// Gets the <see cref="TreeEntryChanges"/> corresponding to the specified <paramref name="path"/>.
94-
/// </summary>
95-
public virtual TreeEntryChanges this[string path]
96-
{
97-
get { return this[(FilePath)path]; }
98-
}
99-
100-
private TreeEntryChanges this[FilePath path]
101-
{
102-
get
103-
{
104-
TreeEntryChanges treeEntryChanges;
105-
if (changes.TryGetValue(path, out treeEntryChanges))
106-
{
107-
return treeEntryChanges;
108-
}
109-
110-
return null;
111-
}
112-
}
11392

11493
/// <summary>
11594
/// List of <see cref="TreeEntryChanges"/> that have been been added.

0 commit comments

Comments
 (0)