Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add TreeDefinition.Remove()

  • Loading branch information...
commit 973d1abd4d1c2d76b1dbd2bad023c69f3cf3d1ef 1 parent 660d27a
@yorah yorah authored committed
View
45 LibGit2Sharp.Tests/ObjectDatabaseFixture.cs
@@ -69,6 +69,51 @@ public void CanCreateATreeByAlteringAnExistingOne(string targetPath)
}
[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();
View
40 LibGit2Sharp/TreeDefinition.cs
@@ -46,6 +46,46 @@ private void AddEntry(string targetTreeEntryName, TreeEntryDefinition treeEntryD
}
/// <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>
Please sign in to comment.
Something went wrong with that request. Please try again.