mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-28 23:32:41 +00:00
tree_builder: add a set_or_remove()
and simplify callers
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()`.
This commit is contained in:
parent
386f002f5a
commit
6b5544f335
4 changed files with 13 additions and 24 deletions
|
@ -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())
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
};
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue