diff options
author | yorah <yoram.harmelin@gmail.com> | 2012-04-18 19:56:47 +0400 |
---|---|---|
committer | nulltoken <emeric.fermas@gmail.com> | 2012-04-28 05:32:18 +0400 |
commit | 973d1abd4d1c2d76b1dbd2bad023c69f3cf3d1ef (patch) | |
tree | b448dbec2266834ff824ed789ab8edb6348c4b05 | |
parent | 660d27aff85f01c608dbe1b014c3d1bc9faa3939 (diff) |
Add TreeDefinition.Remove()
-rw-r--r-- | LibGit2Sharp.Tests/ObjectDatabaseFixture.cs | 45 | ||||
-rw-r--r-- | LibGit2Sharp/TreeDefinition.cs | 40 |
2 files changed, 85 insertions, 0 deletions
diff --git a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs index fbece8f0..b5e1735c 100644 --- a/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs +++ b/LibGit2Sharp.Tests/ObjectDatabaseFixture.cs @@ -69,6 +69,51 @@ namespace LibGit2Sharp.Tests } [Fact] + public void CanCreateATreeByRemovingEntriesFromExistingOne() + { + TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo(); + + using (var repo = new Repository(scd.RepositoryPath)) + { + TreeDefinition td = TreeDefinition.From(repo.Head.Tip.Tree) + .Remove("branch_file.txt") + .Remove("1/branch_file.txt"); + + Tree tree = repo.ObjectDatabase.CreateTree(td); + Assert.NotNull(tree); + + Assert.Null(tree["branch_file.txt"]); + Assert.Null(tree["1/branch_file.txt"]); + Assert.Null(tree["1"]); + + Assert.Equal("814889a078c031f61ed08ab5fa863aea9314344d", tree.Sha); + } + } + + [Fact] + public void RemovingANonExistingEntryFromATreeDefinitionHasNoSideEffect() + { + TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo(); + + using (var repo = new Repository(scd.RepositoryPath)) + { + Tree head = repo.Head.Tip.Tree; + + TreeDefinition td = TreeDefinition.From(head) + .Remove("123") + .Remove("nope") + .Remove("not/here") + .Remove("neither/in/here") + .Remove("README/is/a-Blob/not-a-Tree"); + + Tree tree = repo.ObjectDatabase.CreateTree(td); + Assert.NotNull(tree); + + tree.ShouldEqual(head); + } + } + + [Fact] public void CanCreateAnEmptyTree() { TemporaryCloneOfTestRepo scd = BuildTemporaryCloneOfTestRepo(); diff --git a/LibGit2Sharp/TreeDefinition.cs b/LibGit2Sharp/TreeDefinition.cs index 8ebe90e7..e781ab11 100644 --- a/LibGit2Sharp/TreeDefinition.cs +++ b/LibGit2Sharp/TreeDefinition.cs @@ -46,6 +46,46 @@ namespace LibGit2Sharp } /// <summary> + /// Removes a <see cref="TreeEntryDefinition"/> located the specified <paramref name="treeEntryPath"/> path. + /// </summary> + /// <param name="treeEntryPath">The path within this <see cref="TreeDefinition"/>.</param> + /// <returns>The current <see cref="TreeDefinition"/>.</returns> + public TreeDefinition Remove(string treeEntryPath) + { + Ensure.ArgumentNotNullOrEmptyString(treeEntryPath, "treeEntryPath"); + + if (this[treeEntryPath] == null) + { + return this; + } + + Tuple<string, string> segments = ExtractPosixLeadingSegment(treeEntryPath); + + if (segments.Item2 == null) + { + entries.Remove(segments.Item1); + } + + if (!unwrappedTrees.ContainsKey(segments.Item1)) + { + return this; + } + + if (segments.Item2 != null) + { + unwrappedTrees[segments.Item1].Remove(segments.Item2); + } + + if (unwrappedTrees[segments.Item1].entries.Count == 0) + { + unwrappedTrees.Remove(segments.Item1); + entries.Remove(segments.Item1); + } + + return this; + } + + /// <summary> /// Adds or replaces a <see cref="TreeEntryDefinition"/> at the specified <paramref name="targetTreeEntryPath"/> location. /// </summary> /// <param name="targetTreeEntryPath">The path within this <see cref="TreeDefinition"/>.</param> |