| 1 |
// Copyright 2021 The Jujutsu Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::cmp::Ordering; |
| 16 |
use std::cmp::min; |
| 17 |
use std::collections::BTreeMap; |
| 18 |
use std::collections::VecDeque; |
| 19 |
use std::rc::Rc; |
| 20 |
|
| 21 |
use itertools::Itertools as _; |
| 22 |
|
| 23 |
use super::bit_set::PositionsBitSet; |
| 24 |
use super::composite::CompositeCommitIndex; |
| 25 |
use super::composite::CompositeIndex; |
| 26 |
use super::entry::CommitIndexEntry; |
| 27 |
use super::entry::GlobalCommitPosition; |
| 28 |
use super::rev_walk::RevWalk; |
| 29 |
use super::revset_engine::BoxedRevWalk; |
| 30 |
use crate::backend::CommitId; |
| 31 |
use crate::graph::GraphEdge; |
| 32 |
use crate::graph::GraphNode; |
| 33 |
use crate::revset::RevsetEvaluationError; |
| 34 |
|
| 35 |
// This can be cheaply allocated and hashed compared to `CommitId`-based type. |
| 36 |
type CommitGraphEdge = GraphEdge<GlobalCommitPosition>; |
| 37 |
|
| 38 |
/// Given a `RevWalk` over some set of revisions, yields the same revisions with |
| 39 |
/// associated edge types. |
| 40 |
/// |
| 41 |
/// If a revision's parent is in the input set, then the edge will be "direct". |
| 42 |
/// Otherwise, there will be one "indirect" edge for each closest ancestor in |
| 43 |
/// the set, and one "missing" edge for each edge leading outside the set. |
| 44 |
/// |
| 45 |
/// Example (uppercase characters are in the input set): |
| 46 |
/// |
| 47 |
/// A A |
| 48 |
/// |\ |\ |
| 49 |
/// B c B : |
| 50 |
/// |\| => |\: |
| 51 |
/// d E ~ E |
| 52 |
/// |/ ~ |
| 53 |
/// root |
| 54 |
/// |
| 55 |
/// The implementation works by walking the input set one commit at a time. It |
| 56 |
/// then considers all parents of the commit. It looks ahead in the input set |
| 57 |
/// far enough that all the parents will have been consumed if they are in the |
| 58 |
/// input (and puts them away so we can emit them later). If a parent of the |
| 59 |
/// current commit is not in the input set (i.e. it was not in the look-ahead), |
| 60 |
/// we walk these external commits until we end up back back in the input set. |
| 61 |
/// That walk may result in consuming more elements from the input `RevWalk`. |
| 62 |
/// In the example above, when we consider "A", we will initially look ahead to |
| 63 |
/// "B" and "c". When we consider edges from the external commit "c", we will |
| 64 |
/// further consume the input `RevWalk` to "E". |
| 65 |
/// |
| 66 |
/// Missing edges are those that don't lead back into the input set. If all |
| 67 |
/// edges from an external commit are missing, we consider the edge to that |
| 68 |
/// commit to also be missing. In the example above, that means that "B" will |
| 69 |
/// have a missing edge to "d" rather than to the root. |
| 70 |
/// |
| 71 |
/// `RevsetGraphWalk` can be configured to skip transitive edges that it would |
| 72 |
/// otherwise return. In this mode (which is the default), the edge from "A" to |
| 73 |
/// "E" in the example above would be excluded because there's also a transitive |
| 74 |
/// path from "A" to "E" via "B". The implementation of that mode |
| 75 |
/// adds a filtering step just before yielding the edges for a commit. The |
| 76 |
/// filtering works by doing a DFS in the simplified graph. That may require |
| 77 |
/// even more look-ahead. Consider this example (uppercase characters are in the |
| 78 |
/// input set): |
| 79 |
/// |
| 80 |
/// J |
| 81 |
/// /| |
| 82 |
/// | i |
| 83 |
/// | |\ |
| 84 |
/// | | H |
| 85 |
/// G | | |
| 86 |
/// | e f |
| 87 |
/// | \|\ |
| 88 |
/// | D | |
| 89 |
/// \ / c |
| 90 |
/// b / |
| 91 |
/// |/ |
| 92 |
/// A |
| 93 |
/// | |
| 94 |
/// root |
| 95 |
/// |
| 96 |
/// When walking from "J", we'll find indirect edges to "H", "G", and "D". This |
| 97 |
/// is our unfiltered set of edges, before removing transitive edges. In order |
| 98 |
/// to know that "D" is an ancestor of "H", we need to also walk from "H". We |
| 99 |
/// use the same search for finding edges from "H" as we used from "J". That |
| 100 |
/// results in looking ahead all the way to "A". We could reduce the amount of |
| 101 |
/// look-ahead by stopping at "c" since we're only interested in edges that |
| 102 |
/// could lead to "D", but that would require extra book-keeping to remember for |
| 103 |
/// later that the edges from "f" and "H" are only partially computed. |
| 104 |
pub(super) struct RevsetGraphWalk<'a> { |
| 105 |
input_set_walk: BoxedRevWalk<'a>, |
| 106 |
/// Commits in the input set we had to take out of the `RevWalk` while |
| 107 |
/// walking external edges. Does not necessarily include the commit |
| 108 |
/// we're currently about to emit. |
| 109 |
look_ahead: VecDeque<GlobalCommitPosition>, |
| 110 |
/// The last consumed position. This is always the smallest key in the |
| 111 |
/// look_ahead set, but it's faster to keep a separate field for it. |
| 112 |
min_position: GlobalCommitPosition, |
| 113 |
/// Edges for commits not in the input set. |
| 114 |
edges: BTreeMap<GlobalCommitPosition, Rc<[CommitGraphEdge]>>, |
| 115 |
skip_transitive_edges: bool, |
| 116 |
} |
| 117 |
|
| 118 |
impl<'a> RevsetGraphWalk<'a> { |
| 119 |
pub fn new(input_set_walk: BoxedRevWalk<'a>, skip_transitive_edges: bool) -> Self { |
| 120 |
Self { |
| 121 |
input_set_walk, |
| 122 |
look_ahead: VecDeque::new(), |
| 123 |
min_position: GlobalCommitPosition::MAX, |
| 124 |
edges: Default::default(), |
| 125 |
skip_transitive_edges, |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
fn next_index_position( |
| 130 |
&mut self, |
| 131 |
index: &CompositeIndex, |
| 132 |
) -> Result<Option<GlobalCommitPosition>, RevsetEvaluationError> { |
| 133 |
match self.look_ahead.pop_back() { |
| 134 |
Some(position) => Ok(Some(position)), |
| 135 |
None => self.input_set_walk.next(index).transpose(), |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
fn pop_edges_from_internal_commit( |
| 140 |
&mut self, |
| 141 |
index: &CompositeIndex, |
| 142 |
index_entry: &CommitIndexEntry, |
| 143 |
) -> Result<Rc<[CommitGraphEdge]>, RevsetEvaluationError> { |
| 144 |
let position = index_entry.position(); |
| 145 |
while let Some(entry) = self.edges.last_entry() { |
| 146 |
match entry.key().cmp(&position) { |
| 147 |
Ordering::Less => break, // no cached edges found |
| 148 |
Ordering::Equal => return Ok(entry.remove()), |
| 149 |
Ordering::Greater => entry.remove(), |
| 150 |
}; |
| 151 |
} |
| 152 |
self.new_edges_from_internal_commit(index, index_entry) |
| 153 |
} |
| 154 |
|
| 155 |
fn new_edges_from_internal_commit( |
| 156 |
&mut self, |
| 157 |
index: &CompositeIndex, |
| 158 |
index_entry: &CommitIndexEntry, |
| 159 |
) -> Result<Rc<[CommitGraphEdge]>, RevsetEvaluationError> { |
| 160 |
let mut parent_entries = index_entry.parents(); |
| 161 |
if parent_entries.len() == 1 { |
| 162 |
let parent = parent_entries.next().unwrap(); |
| 163 |
let parent_position = parent.position(); |
| 164 |
self.consume_to(index, parent_position)?; |
| 165 |
if self.look_ahead.binary_search(&parent_position).is_ok() { |
| 166 |
Ok([CommitGraphEdge::direct(parent_position)].into()) |
| 167 |
} else { |
| 168 |
let parent_edges = self.edges_from_external_commit(index, parent)?; |
| 169 |
if parent_edges.iter().all(|edge| edge.is_missing()) { |
| 170 |
Ok([CommitGraphEdge::missing(parent_position)].into()) |
| 171 |
} else { |
| 172 |
Ok(parent_edges.clone()) |
| 173 |
} |
| 174 |
} |
| 175 |
} else { |
| 176 |
let mut edges = Vec::new(); |
| 177 |
let mut known_ancestors = PositionsBitSet::with_max_pos(index_entry.position()); |
| 178 |
for parent in parent_entries { |
| 179 |
let parent_position = parent.position(); |
| 180 |
self.consume_to(index, parent_position)?; |
| 181 |
if self.look_ahead.binary_search(&parent_position).is_ok() { |
| 182 |
edges.push(CommitGraphEdge::direct(parent_position)); |
| 183 |
} else { |
| 184 |
let parent_edges = self.edges_from_external_commit(index, parent)?; |
| 185 |
if parent_edges.iter().all(|edge| edge.is_missing()) { |
| 186 |
edges.push(CommitGraphEdge::missing(parent_position)); |
| 187 |
} else { |
| 188 |
edges.extend( |
| 189 |
parent_edges |
| 190 |
.iter() |
| 191 |
.filter(|edge| !known_ancestors.get_set(edge.target)), |
| 192 |
); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
if self.skip_transitive_edges { |
| 197 |
self.remove_transitive_edges(index.commits(), &mut edges); |
| 198 |
} |
| 199 |
Ok(edges.into()) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
fn edges_from_external_commit( |
| 204 |
&mut self, |
| 205 |
index: &CompositeIndex, |
| 206 |
index_entry: CommitIndexEntry<'_>, |
| 207 |
) -> Result<&Rc<[CommitGraphEdge]>, RevsetEvaluationError> { |
| 208 |
let position = index_entry.position(); |
| 209 |
let mut stack = vec![index_entry]; |
| 210 |
while let Some(entry) = stack.last() { |
| 211 |
let position = entry.position(); |
| 212 |
if self.edges.contains_key(&position) { |
| 213 |
stack.pop().unwrap(); |
| 214 |
continue; |
| 215 |
} |
| 216 |
let mut parent_entries = entry.parents(); |
| 217 |
let complete_parent_edges = if parent_entries.len() == 1 { |
| 218 |
let parent = parent_entries.next().unwrap(); |
| 219 |
let parent_position = parent.position(); |
| 220 |
self.consume_to(index, parent_position)?; |
| 221 |
if self.look_ahead.binary_search(&parent_position).is_ok() { |
| 222 |
// We have found a path back into the input set |
| 223 |
Some([CommitGraphEdge::indirect(parent_position)].into()) |
| 224 |
} else if let Some(parent_edges) = self.edges.get(&parent_position) { |
| 225 |
if parent_edges.iter().all(|edge| edge.is_missing()) { |
| 226 |
Some([CommitGraphEdge::missing(parent_position)].into()) |
| 227 |
} else { |
| 228 |
Some(parent_edges.clone()) |
| 229 |
} |
| 230 |
} else if parent_position < self.min_position { |
| 231 |
// The parent is not in the input set |
| 232 |
Some([CommitGraphEdge::missing(parent_position)].into()) |
| 233 |
} else { |
| 234 |
// The parent is not in the input set but it's somewhere in the range |
| 235 |
// where we have commits in the input set, so continue searching. |
| 236 |
stack.push(parent); |
| 237 |
None |
| 238 |
} |
| 239 |
} else { |
| 240 |
let mut edges = Vec::new(); |
| 241 |
let mut known_ancestors = PositionsBitSet::with_max_pos(position); |
| 242 |
let mut parents_complete = true; |
| 243 |
for parent in parent_entries { |
| 244 |
let parent_position = parent.position(); |
| 245 |
self.consume_to(index, parent_position)?; |
| 246 |
if self.look_ahead.binary_search(&parent_position).is_ok() { |
| 247 |
// We have found a path back into the input set |
| 248 |
edges.push(CommitGraphEdge::indirect(parent_position)); |
| 249 |
} else if let Some(parent_edges) = self.edges.get(&parent_position) { |
| 250 |
if parent_edges.iter().all(|edge| edge.is_missing()) { |
| 251 |
edges.push(CommitGraphEdge::missing(parent_position)); |
| 252 |
} else { |
| 253 |
edges.extend( |
| 254 |
parent_edges |
| 255 |
.iter() |
| 256 |
.filter(|edge| !known_ancestors.get_set(edge.target)), |
| 257 |
); |
| 258 |
} |
| 259 |
} else if parent_position < self.min_position { |
| 260 |
// The parent is not in the input set |
| 261 |
edges.push(CommitGraphEdge::missing(parent_position)); |
| 262 |
} else { |
| 263 |
// The parent is not in the input set but it's somewhere in the range |
| 264 |
// where we have commits in the input set, so continue searching. |
| 265 |
stack.push(parent); |
| 266 |
parents_complete = false; |
| 267 |
} |
| 268 |
} |
| 269 |
parents_complete.then(|| { |
| 270 |
if self.skip_transitive_edges { |
| 271 |
self.remove_transitive_edges(index.commits(), &mut edges); |
| 272 |
} |
| 273 |
edges.into() |
| 274 |
}) |
| 275 |
}; |
| 276 |
if let Some(edges) = complete_parent_edges { |
| 277 |
stack.pop().unwrap(); |
| 278 |
self.edges.insert(position, edges); |
| 279 |
} |
| 280 |
} |
| 281 |
Ok(self.edges.get(&position).unwrap()) |
| 282 |
} |
| 283 |
|
| 284 |
fn remove_transitive_edges( |
| 285 |
&self, |
| 286 |
index: &CompositeCommitIndex, |
| 287 |
edges: &mut Vec<CommitGraphEdge>, |
| 288 |
) { |
| 289 |
if !edges.iter().any(|edge| edge.is_indirect()) { |
| 290 |
return; |
| 291 |
} |
| 292 |
let Some((min_pos, max_pos)) = reachable_positions(edges).minmax().into_option() else { |
| 293 |
return; |
| 294 |
}; |
| 295 |
|
| 296 |
let enqueue_parents = |work: &mut Vec<GlobalCommitPosition>, entry: &CommitIndexEntry| { |
| 297 |
if let Some(edges) = self.edges.get(&entry.position()) { |
| 298 |
// Edges to internal commits are known. Skip external commits |
| 299 |
// which should never be in the input edges. |
| 300 |
work.extend(reachable_positions(edges).filter(|&pos| pos >= min_pos)); |
| 301 |
} else { |
| 302 |
// The commit isn't visited yet. Cannot skip external commits. |
| 303 |
let positions = entry.parent_positions(); |
| 304 |
work.extend(positions.into_iter().filter(|&pos| pos >= min_pos)); |
| 305 |
} |
| 306 |
}; |
| 307 |
|
| 308 |
let mut min_generation = u32::MAX; |
| 309 |
let mut initial_targets = PositionsBitSet::with_max_pos(max_pos); |
| 310 |
let mut work = vec![]; |
| 311 |
// To start with, add the edges one step after the input edges. |
| 312 |
for pos in reachable_positions(edges) { |
| 313 |
initial_targets.set(pos); |
| 314 |
let entry = index.entry_by_pos(pos); |
| 315 |
min_generation = min(min_generation, entry.generation_number()); |
| 316 |
enqueue_parents(&mut work, &entry); |
| 317 |
} |
| 318 |
// Find commits reachable transitively and add them to the `unwanted` set. |
| 319 |
let mut unwanted = PositionsBitSet::with_max_pos(max_pos); |
| 320 |
while let Some(pos) = work.pop() { |
| 321 |
if unwanted.get_set(pos) { |
| 322 |
// Already visited |
| 323 |
continue; |
| 324 |
} |
| 325 |
if initial_targets.get(pos) { |
| 326 |
// Already visited |
| 327 |
continue; |
| 328 |
} |
| 329 |
let entry = index.entry_by_pos(pos); |
| 330 |
if entry.generation_number() < min_generation { |
| 331 |
continue; |
| 332 |
} |
| 333 |
enqueue_parents(&mut work, &entry); |
| 334 |
} |
| 335 |
|
| 336 |
edges.retain(|edge| edge.is_missing() || !unwanted.get(edge.target)); |
| 337 |
} |
| 338 |
|
| 339 |
fn consume_to( |
| 340 |
&mut self, |
| 341 |
index: &CompositeIndex, |
| 342 |
pos: GlobalCommitPosition, |
| 343 |
) -> Result<(), RevsetEvaluationError> { |
| 344 |
while pos < self.min_position { |
| 345 |
if let Some(next_position) = self.input_set_walk.next(index).transpose()? { |
| 346 |
self.look_ahead.push_front(next_position); |
| 347 |
self.min_position = next_position; |
| 348 |
} else { |
| 349 |
break; |
| 350 |
} |
| 351 |
} |
| 352 |
Ok(()) |
| 353 |
} |
| 354 |
|
| 355 |
fn try_next( |
| 356 |
&mut self, |
| 357 |
index: &CompositeIndex, |
| 358 |
) -> Result<Option<GraphNode<CommitId>>, RevsetEvaluationError> { |
| 359 |
let Some(position) = self.next_index_position(index)? else { |
| 360 |
return Ok(None); |
| 361 |
}; |
| 362 |
let entry = index.commits().entry_by_pos(position); |
| 363 |
let edges = self.pop_edges_from_internal_commit(index, &entry)?; |
| 364 |
let edges = edges |
| 365 |
.iter() |
| 366 |
.map(|edge| edge.map(|pos| index.commits().entry_by_pos(pos).commit_id())) |
| 367 |
.collect(); |
| 368 |
Ok(Some((entry.commit_id(), edges))) |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
impl RevWalk<CompositeIndex> for RevsetGraphWalk<'_> { |
| 373 |
type Item = Result<GraphNode<CommitId>, RevsetEvaluationError>; |
| 374 |
|
| 375 |
fn next(&mut self, index: &CompositeIndex) -> Option<Self::Item> { |
| 376 |
self.try_next(index).transpose() |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
fn reachable_positions( |
| 381 |
edges: &[CommitGraphEdge], |
| 382 |
) -> impl DoubleEndedIterator<Item = GlobalCommitPosition> { |
| 383 |
edges |
| 384 |
.iter() |
| 385 |
.filter(|edge| !edge.is_missing()) |
| 386 |
.map(|edge| edge.target) |
| 387 |
} |