-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathevolution.rs
More file actions
372 lines (348 loc) · 13.5 KB
/
evolution.rs
File metadata and controls
372 lines (348 loc) · 13.5 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
// Copyright 2025 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.
//! Utility for commit evolution history.
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::collections::VecDeque;
use std::collections::hash_map::Entry;
use std::pin::pin;
use std::slice;
use futures::Stream;
use futures::StreamExt as _;
use futures::future::join_all;
use itertools::Itertools as _;
use pollster::FutureExt as _;
use thiserror::Error;
use crate::backend::BackendError;
use crate::backend::BackendResult;
use crate::backend::CommitId;
use crate::commit::Commit;
use crate::dag_walk;
use crate::index::IndexError;
use crate::op_store::OpStoreError;
use crate::op_store::OpStoreResult;
use crate::op_walk;
use crate::operation::Operation;
use crate::repo::ReadonlyRepo;
use crate::repo::Repo as _;
/// Commit with predecessor information.
#[derive(Clone, Debug, serde::Serialize)]
pub struct CommitEvolutionEntry {
/// Commit id and metadata.
pub commit: Commit,
/// Operation where the commit was created or rewritten.
pub operation: Option<Operation>,
/// Reachable predecessor ids reconstructed from the commit metadata. This
/// should be set if the associated `operation` is unknown.
// TODO: remove with legacy commit.predecessors support
#[serde(skip)]
reachable_predecessors: Option<Vec<CommitId>>,
}
impl CommitEvolutionEntry {
/// Predecessor ids of this commit.
pub fn predecessor_ids(&self) -> &[CommitId] {
match &self.operation {
Some(op) => op.predecessors_for_commit(self.commit.id()).unwrap(),
None => self.reachable_predecessors.as_ref().unwrap(),
}
}
/// Predecessor commit objects of this commit.
pub fn predecessors(&self) -> impl ExactSizeIterator<Item = BackendResult<Commit>> {
let store = self.commit.store();
self.predecessor_ids().iter().map(|id| store.get_commit(id))
}
}
#[expect(missing_docs)]
#[derive(Debug, Error)]
pub enum WalkPredecessorsError {
#[error(transparent)]
Backend(#[from] BackendError),
#[error(transparent)]
Index(#[from] IndexError),
#[error(transparent)]
OpStore(#[from] OpStoreError),
#[error("Predecessors cycle detected around commit {0}")]
CycleDetected(CommitId),
}
/// Walks operations to emit commit predecessors in reverse topological order.
pub fn walk_predecessors<'repo>(
repo: &'repo ReadonlyRepo,
start_commits: &[CommitId],
) -> impl Iterator<Item = Result<CommitEvolutionEntry, WalkPredecessorsError>> + use<'repo> {
let op_ancestors = Box::pin(op_walk::walk_ancestors(slice::from_ref(repo.operation())));
WalkPredecessors {
repo,
op_ancestors,
to_visit: start_commits.to_vec(),
queued: VecDeque::new(),
}
}
struct WalkPredecessors<'repo, I> {
repo: &'repo ReadonlyRepo,
op_ancestors: I,
to_visit: Vec<CommitId>,
queued: VecDeque<CommitEvolutionEntry>,
}
impl<I> WalkPredecessors<'_, I>
where
I: Stream<Item = OpStoreResult<Operation>> + Unpin,
{
async fn try_next(&mut self) -> Result<Option<CommitEvolutionEntry>, WalkPredecessorsError> {
while !self.to_visit.is_empty() && self.queued.is_empty() {
let Some(op) = self.op_ancestors.next().await.transpose()? else {
// Scanned all operations, no fallback needed.
self.flush_commits().await?;
break;
};
if !op.stores_commit_predecessors() {
// There may be concurrent ops, but let's simply switch to the
// legacy commit traversal. Operation history should be mostly
// linear.
self.scan_commits().await?;
break;
}
self.visit_op(&op).await?;
}
Ok(self.queued.pop_front())
}
/// Looks for predecessors within the given operation.
async fn visit_op(&mut self, op: &Operation) -> Result<(), WalkPredecessorsError> {
let mut to_emit = Vec::new(); // transitive edges should be short
let mut has_dup = false;
let mut i = 0;
while let Some(cur_id) = self.to_visit.get(i) {
if let Some(next_ids) = op.predecessors_for_commit(cur_id) {
if to_emit.contains(cur_id) {
self.to_visit.remove(i);
has_dup = true;
continue;
}
to_emit.extend(self.to_visit.splice(i..=i, next_ids.iter().cloned()));
} else {
i += 1;
}
}
let store = self.repo.store();
let mut emit = async |id: &CommitId| -> BackendResult<()> {
let commit = store.get_commit_async(id).await?;
self.queued.push_back(CommitEvolutionEntry {
commit,
operation: Some(op.clone()),
reachable_predecessors: None,
});
Ok(())
};
match &*to_emit {
[] => {}
[id] if !has_dup => emit(id).await?,
_ => {
let sorted_ids = dag_walk::topo_order_reverse_ok(
to_emit.iter().map(Ok),
|&id| id,
async |&id| op.predecessors_for_commit(id).into_iter().flatten().map(Ok),
|id| id, // Err(&CommitId) if graph has cycle
)
.await
.map_err(|id| WalkPredecessorsError::CycleDetected(id.clone()))?;
for &id in &sorted_ids {
if op.predecessors_for_commit(id).is_some() {
emit(id).await?;
}
}
}
}
Ok(())
}
/// Traverses predecessors from remainder commits.
async fn scan_commits(&mut self) -> Result<(), WalkPredecessorsError> {
let store = self.repo.store();
let index = self.repo.index();
let mut commit_predecessors: HashMap<CommitId, Vec<CommitId>> = HashMap::new();
let commits = dag_walk::topo_order_reverse_ok(
join_all(self.to_visit.drain(..).map(async |id| {
store
.get_commit_async(&id)
.await
.map_err(WalkPredecessorsError::Backend)
}))
.await,
|commit: &Commit| commit.id().clone(),
async |commit: &Commit| {
let ids = match commit_predecessors.entry(commit.id().clone()) {
Entry::Occupied(entry) => entry.into_mut(),
Entry::Vacant(entry) => {
let mut filtered = vec![];
for id in &commit.store_commit().predecessors {
match index.has_id(id) {
Ok(true) => {
filtered.push(id.clone());
}
Ok(false) => {
// Ignore unreachable predecessors
}
Err(err) => {
return vec![Err(WalkPredecessorsError::Index(err))];
}
}
}
entry.insert(filtered)
}
};
join_all(ids.iter().map(async |id| {
store
.get_commit_async(id)
.await
.map_err(WalkPredecessorsError::Backend)
}))
.await
},
|_| panic!("graph has cycle"),
)
.await?;
self.queued.extend(commits.into_iter().map(|commit| {
let predecessors = commit_predecessors
.remove(commit.id())
.expect("commit must be visited once");
CommitEvolutionEntry {
commit,
operation: None,
reachable_predecessors: Some(predecessors),
}
}));
Ok(())
}
/// Moves remainder commits to output queue.
async fn flush_commits(&mut self) -> BackendResult<()> {
self.queued.reserve(self.to_visit.len());
for id in self.to_visit.drain(..) {
let commit = self.repo.store().get_commit_async(&id).await?;
self.queued.push_back(CommitEvolutionEntry {
commit,
operation: None,
// There were no legacy operations, so the commit should have no
// predecessors.
reachable_predecessors: Some(vec![]),
});
}
Ok(())
}
}
// TODO: Convert to `Stream`.
impl<I> Iterator for WalkPredecessors<'_, I>
where
I: Stream<Item = OpStoreResult<Operation>> + Unpin,
{
type Item = Result<CommitEvolutionEntry, WalkPredecessorsError>;
fn next(&mut self) -> Option<Self::Item> {
self.try_next().block_on().transpose()
}
}
/// Collects predecessor records from `new_ops` to `old_ops`, and resolves
/// transitive entries.
///
/// This function assumes that there exists a single greatest common ancestors
/// between `old_ops` and `new_ops`. If `old_ops` and `new_ops` have ancestors
/// and descendants each other, or if criss-crossed merges exist between these
/// operations, the returned mapping would be lossy.
pub async fn accumulate_predecessors(
new_ops: &[Operation],
old_ops: &[Operation],
) -> Result<BTreeMap<CommitId, Vec<CommitId>>, WalkPredecessorsError> {
if new_ops.is_empty() || old_ops.is_empty() {
return Ok(BTreeMap::new()); // No common ancestor exists
}
// Fast path for the single forward operation case.
if let [op] = new_ops
&& op.parent_ids().iter().eq(old_ops.iter().map(|op| op.id()))
{
let Some(map) = &op.store_operation().commit_predecessors else {
return Ok(BTreeMap::new());
};
return resolve_transitive_edges(map, map.keys())
.await
.map_err(|id| WalkPredecessorsError::CycleDetected(id.clone()));
}
// Follow reverse edges from the common ancestor to old_ops. Here we use
// BTreeMap to stabilize order of the reversed edges.
let mut accumulated = BTreeMap::new();
let reverse_ops = op_walk::walk_ancestors_range(old_ops, new_ops);
if !try_collect_predecessors_into(&mut accumulated, reverse_ops).await? {
return Ok(BTreeMap::new());
}
let mut accumulated = reverse_edges(accumulated);
// Follow forward edges from new_ops to the common ancestor.
let forward_ops = op_walk::walk_ancestors_range(new_ops, old_ops);
if !try_collect_predecessors_into(&mut accumulated, forward_ops).await? {
return Ok(BTreeMap::new());
}
let new_commit_ids = new_ops
.iter()
.filter_map(|op| op.store_operation().commit_predecessors.as_ref())
.flat_map(|map| map.keys());
resolve_transitive_edges(&accumulated, new_commit_ids)
.await
.map_err(|id| WalkPredecessorsError::CycleDetected(id.clone()))
}
async fn try_collect_predecessors_into(
collected: &mut BTreeMap<CommitId, Vec<CommitId>>,
ops: impl Stream<Item = OpStoreResult<Operation>>,
) -> OpStoreResult<bool> {
let mut ops = pin!(ops);
while let Some(op) = ops.next().await {
let op = op?;
let Some(map) = &op.store_operation().commit_predecessors else {
return Ok(false);
};
// Just insert. There should be no duplicate entries.
collected.extend(map.iter().map(|(k, v)| (k.clone(), v.clone())));
}
Ok(true)
}
/// Resolves transitive edges in `graph` starting from the `start` nodes,
/// returns new DAG. The returned DAG only includes edges reachable from the
/// `start` nodes.
async fn resolve_transitive_edges<'a: 'b, 'b>(
graph: &'a BTreeMap<CommitId, Vec<CommitId>>,
start: impl IntoIterator<Item = &'b CommitId>,
) -> Result<BTreeMap<CommitId, Vec<CommitId>>, &'b CommitId> {
let mut new_graph: BTreeMap<CommitId, Vec<CommitId>> = BTreeMap::new();
let sorted_ids = dag_walk::topo_order_forward_ok(
start.into_iter().map(Ok),
|&id| id,
async |&id| graph.get(id).into_iter().flatten().map(Ok),
|id| id, // Err(&CommitId) if graph has cycle
)
.await?;
for cur_id in sorted_ids {
let Some(neighbors) = graph.get(cur_id) else {
continue;
};
let lookup = |id| new_graph.get(id).map_or(slice::from_ref(id), Vec::as_slice);
let new_neighbors = match &neighbors[..] {
[id] => lookup(id).to_vec(), // unique() not needed
ids => ids.iter().flat_map(lookup).unique().cloned().collect(),
};
new_graph.insert(cur_id.clone(), new_neighbors);
}
Ok(new_graph)
}
fn reverse_edges(graph: BTreeMap<CommitId, Vec<CommitId>>) -> BTreeMap<CommitId, Vec<CommitId>> {
let mut new_graph: BTreeMap<CommitId, Vec<CommitId>> = BTreeMap::new();
for (node1, neighbors) in graph {
for node2 in neighbors {
new_graph.entry(node2).or_default().push(node1.clone());
}
}
new_graph
}