Skip to content

Commit

Permalink
tree_builder: add a set_or_remove() and simplify callers
Browse files Browse the repository at this point in the history
Many of the `TreeBuilder` users have an `Option<TreeValue>` and call
either `set()` or `remove()` or the builder depending on whether the
value is present. Let's centralize this logic in a new
`TreeBuilder::set_or_remove()`.
  • Loading branch information
martinvonz committed Aug 24, 2023
1 parent 386f002 commit 6b5544f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 24 deletions.
9 changes: 1 addition & 8 deletions cli/src/cli_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,14 +1498,7 @@ impl WorkspaceCommandTransaction<'_> {
} else {
let mut tree_builder = self.repo().store().tree_builder(left_tree.id().clone());
for (repo_path, diff) in left_tree.diff(right_tree, matcher) {
match diff.into_options().1 {
Some(value) => {
tree_builder.set(repo_path, value);
}
None => {
tree_builder.remove(repo_path);
}
}
tree_builder.set_or_remove(repo_path, diff.into_options().1);
}
Ok(tree_builder.write_tree())
}
Expand Down
9 changes: 1 addition & 8 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2896,14 +2896,7 @@ fn cmd_restore(
.store()
.tree_builder(to_commit.tree_id().clone());
for (repo_path, diff) in from_tree.diff(&to_commit.tree(), matcher.as_ref()) {
match diff.into_options().0 {
Some(value) => {
tree_builder.set(repo_path, value);
}
None => {
tree_builder.remove(repo_path);
}
}
tree_builder.set_or_remove(repo_path, diff.into_options().0);
}
tree_builder.write_tree()
};
Expand Down
10 changes: 2 additions & 8 deletions lib/src/merged_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,10 @@ impl MergedTree {
}
// Now add the terms that were present in the conflict to the appropriate trees.
for (i, term) in conflict.removes().iter().enumerate() {
match term {
Some(value) => removes[i].set(path.clone(), value.clone()),
None => removes[i].remove(path.clone()),
}
removes[i].set_or_remove(path.clone(), term.clone());
}
for (i, term) in conflict.adds().iter().enumerate() {
match term {
Some(value) => adds[i].set(path.clone(), value.clone()),
None => adds[i].remove(path.clone()),
}
adds[i].set_or_remove(path.clone(), term.clone());
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/src/tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ impl TreeBuilder {
self.overrides.insert(path, Override::Tombstone);
}

pub fn set_or_remove(&mut self, path: RepoPath, value: Option<TreeValue>) {
assert!(!path.is_root());
if let Some(value) = value {
self.overrides.insert(path, Override::Replace(value));
} else {
self.overrides.insert(path, Override::Tombstone);
}
}

pub fn write_tree(self) -> TreeId {
if self.overrides.is_empty() {
return self.base_tree_id;
Expand Down

0 comments on commit 6b5544f

Please sign in to comment.