forked from jj-vcs/jj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_builder.rs
153 lines (134 loc) · 5.07 KB
/
tree_builder.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2020 The Jujutsu Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(missing_docs)]
use std::collections::BTreeMap;
use std::sync::Arc;
use crate::backend;
use crate::backend::{TreeId, TreeValue};
use crate::repo_path::{RepoPath, RepoPathBuf};
use crate::store::Store;
use crate::tree::Tree;
#[derive(Debug)]
enum Override {
Tombstone,
Replace(TreeValue),
}
#[derive(Debug)]
pub struct TreeBuilder {
store: Arc<Store>,
base_tree_id: TreeId,
overrides: BTreeMap<RepoPathBuf, Override>,
}
impl TreeBuilder {
pub fn new(store: Arc<Store>, base_tree_id: TreeId) -> TreeBuilder {
let overrides = BTreeMap::new();
TreeBuilder {
store,
base_tree_id,
overrides,
}
}
pub fn store(&self) -> &Store {
self.store.as_ref()
}
pub fn set(&mut self, path: RepoPathBuf, value: TreeValue) {
assert!(!path.is_root());
self.overrides.insert(path, Override::Replace(value));
}
pub fn remove(&mut self, path: RepoPathBuf) {
assert!(!path.is_root());
self.overrides.insert(path, Override::Tombstone);
}
pub fn set_or_remove(&mut self, path: RepoPathBuf, 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;
}
let mut trees_to_write = self.get_base_trees();
// Update entries in parent trees for file overrides
for (path, file_override) in self.overrides {
let (dir, basename) = path.split().unwrap();
let tree = trees_to_write.get_mut(dir).unwrap();
match file_override {
Override::Replace(value) => {
tree.set(basename.to_owned(), value);
}
Override::Tombstone => {
tree.remove(basename);
}
}
}
// Write trees in reverse lexicographical order, starting with trees without
// children.
let store = &self.store;
while let Some((dir, tree)) = trees_to_write.pop_last() {
if let Some((parent, basename)) = dir.split() {
let parent_tree = trees_to_write.get_mut(parent).unwrap();
if tree.is_empty() {
if let Some(TreeValue::Tree(_)) = parent_tree.value(basename) {
parent_tree.remove(basename);
} else {
// Entry would have been replaced with file (see above)
}
} else {
let tree = store.write_tree(&dir, tree).unwrap();
parent_tree.set(basename.to_owned(), TreeValue::Tree(tree.id().clone()));
}
} else {
// We're writing the root tree. Write it even if empty. Return its id.
assert!(trees_to_write.is_empty());
return store.write_tree(&dir, tree).unwrap().id().clone();
}
}
unreachable!("trees_to_write must contain the root tree");
}
fn get_base_trees(&self) -> BTreeMap<RepoPathBuf, backend::Tree> {
let store = &self.store;
let mut tree_cache = {
let dir = RepoPathBuf::root();
let tree = store.get_tree(&dir, &self.base_tree_id).unwrap();
BTreeMap::from([(dir, tree)])
};
fn populate_trees<'a>(
tree_cache: &'a mut BTreeMap<RepoPathBuf, Tree>,
store: &Arc<Store>,
dir: &RepoPath,
) -> &'a Tree {
// `if let Some(tree) = ...` doesn't pass lifetime check as of Rust 1.69.0
if tree_cache.contains_key(dir) {
return tree_cache.get(dir).unwrap();
}
let (parent, basename) = dir.split().expect("root must be populated");
let tree = populate_trees(tree_cache, store, parent)
.sub_tree(basename)
.unwrap_or_else(|| Tree::null(store.clone(), dir.to_owned()));
tree_cache.entry(dir.to_owned()).or_insert(tree)
}
for path in self.overrides.keys() {
let parent = path.parent().unwrap();
populate_trees(&mut tree_cache, store, parent);
}
tree_cache
.into_iter()
.map(|(dir, tree)| (dir, tree.data().clone()))
.collect()
}
}