| 1 |
// Copyright 2023 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::cell::RefCell; |
| 16 |
use std::cmp::Ordering; |
| 17 |
use std::cmp::Reverse; |
| 18 |
use std::collections::BinaryHeap; |
| 19 |
use std::collections::HashSet; |
| 20 |
use std::collections::hash_map::HashMap; |
| 21 |
use std::convert::Infallible; |
| 22 |
use std::fmt; |
| 23 |
use std::future::ready; |
| 24 |
use std::iter; |
| 25 |
use std::ops::Range; |
| 26 |
use std::rc::Rc; |
| 27 |
use std::sync::Arc; |
| 28 |
|
| 29 |
use bstr::BString; |
| 30 |
use futures::StreamExt as _; |
| 31 |
use futures::stream::LocalBoxStream; |
| 32 |
use itertools::Itertools as _; |
| 33 |
use pollster::FutureExt as _; |
| 34 |
|
| 35 |
use super::composite::AsCompositeIndex; |
| 36 |
use super::composite::CompositeIndex; |
| 37 |
use super::entry::GlobalCommitPosition; |
| 38 |
use super::rev_walk::EagerRevWalk; |
| 39 |
use super::rev_walk::PeekableRevWalk; |
| 40 |
use super::rev_walk::RevWalk; |
| 41 |
use super::rev_walk::RevWalkBuilder; |
| 42 |
use super::revset_graph_iterator::RevsetGraphWalk; |
| 43 |
use crate::backend::BackendResult; |
| 44 |
use crate::backend::ChangeId; |
| 45 |
use crate::backend::CommitId; |
| 46 |
use crate::backend::MillisSinceEpoch; |
| 47 |
use crate::commit::Commit; |
| 48 |
use crate::conflict_labels::ConflictLabels; |
| 49 |
use crate::conflicts::MaterializedTreeValue; |
| 50 |
use crate::conflicts::materialize_tree_value; |
| 51 |
use crate::default_index::bit_set::AncestorsBitSet; |
| 52 |
use crate::diff::ContentDiff; |
| 53 |
use crate::diff::DiffHunkKind; |
| 54 |
use crate::files; |
| 55 |
use crate::graph::GraphNode; |
| 56 |
use crate::matchers::FilesMatcher; |
| 57 |
use crate::matchers::Matcher; |
| 58 |
use crate::matchers::Visit; |
| 59 |
use crate::merge::Merge; |
| 60 |
use crate::object_id::HexPrefix; |
| 61 |
use crate::object_id::ObjectId as _; |
| 62 |
use crate::object_id::PrefixResolution; |
| 63 |
use crate::repo_path::RepoPath; |
| 64 |
use crate::revset::DiffMatchSide; |
| 65 |
use crate::revset::GENERATION_RANGE_FULL; |
| 66 |
use crate::revset::ResolvedExpression; |
| 67 |
use crate::revset::ResolvedPredicateExpression; |
| 68 |
use crate::revset::Revset; |
| 69 |
use crate::revset::RevsetContainingFn; |
| 70 |
use crate::revset::RevsetEvaluationError; |
| 71 |
use crate::revset::RevsetFilterPredicate; |
| 72 |
use crate::rewrite; |
| 73 |
use crate::store::Store; |
| 74 |
use crate::str_util::StringMatcher; |
| 75 |
use crate::tree_merge::MergeOptions; |
| 76 |
use crate::tree_merge::resolve_file_values; |
| 77 |
use crate::union_find; |
| 78 |
|
| 79 |
type BoxedPredicateFn<'a> = Box< |
| 80 |
dyn FnMut(&CompositeIndex, GlobalCommitPosition) -> Result<bool, RevsetEvaluationError> + 'a, |
| 81 |
>; |
| 82 |
pub(super) type BoxedRevWalk<'a> = Box< |
| 83 |
dyn RevWalk<CompositeIndex, Item = Result<GlobalCommitPosition, RevsetEvaluationError>> + 'a, |
| 84 |
>; |
| 85 |
|
| 86 |
trait ToPredicateFn: fmt::Debug { |
| 87 |
/// Creates function that tests if the given entry is included in the set. |
| 88 |
/// |
| 89 |
/// The predicate function is evaluated in order of `RevsetIterator`. |
| 90 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 91 |
where |
| 92 |
Self: 'a; |
| 93 |
} |
| 94 |
|
| 95 |
impl<T: ToPredicateFn + ?Sized> ToPredicateFn for Box<T> { |
| 96 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 97 |
where |
| 98 |
Self: 'a, |
| 99 |
{ |
| 100 |
<T as ToPredicateFn>::to_predicate_fn(self) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
trait InternalRevset: fmt::Debug + ToPredicateFn { |
| 105 |
// All revsets currently iterate in order of descending index position |
| 106 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 107 |
where |
| 108 |
Self: 'a; |
| 109 |
} |
| 110 |
|
| 111 |
impl<T: InternalRevset + ?Sized> InternalRevset for Box<T> { |
| 112 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 113 |
where |
| 114 |
Self: 'a, |
| 115 |
{ |
| 116 |
<T as InternalRevset>::positions(self) |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
pub(super) struct RevsetImpl<I> { |
| 121 |
inner: Box<dyn InternalRevset>, |
| 122 |
index: I, |
| 123 |
} |
| 124 |
|
| 125 |
impl<I: AsCompositeIndex + Clone> RevsetImpl<I> { |
| 126 |
fn new(inner: Box<dyn InternalRevset>, index: I) -> Self { |
| 127 |
Self { inner, index } |
| 128 |
} |
| 129 |
|
| 130 |
fn positions( |
| 131 |
&self, |
| 132 |
) -> impl Iterator<Item = Result<GlobalCommitPosition, RevsetEvaluationError>> { |
| 133 |
self.inner.positions().attach(self.index.as_composite()) |
| 134 |
} |
| 135 |
|
| 136 |
pub fn iter_graph_impl( |
| 137 |
&self, |
| 138 |
skip_transitive_edges: bool, |
| 139 |
) -> impl Iterator<Item = Result<GraphNode<CommitId>, RevsetEvaluationError>> + use<I> { |
| 140 |
let index = self.index.clone(); |
| 141 |
let walk = self.inner.positions(); |
| 142 |
let mut graph_walk = RevsetGraphWalk::new(walk, skip_transitive_edges); |
| 143 |
iter::from_fn(move || graph_walk.next(index.as_composite())) |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl<I> fmt::Debug for RevsetImpl<I> { |
| 148 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 149 |
f.debug_struct("RevsetImpl") |
| 150 |
.field("inner", &self.inner) |
| 151 |
.finish_non_exhaustive() |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
impl<I: AsCompositeIndex + Clone> Revset for RevsetImpl<I> { |
| 156 |
fn stream<'a>(&self) -> LocalBoxStream<'a, Result<CommitId, RevsetEvaluationError>> |
| 157 |
where |
| 158 |
Self: 'a, |
| 159 |
{ |
| 160 |
let index = self.index.clone(); |
| 161 |
let mut walk = self |
| 162 |
.inner |
| 163 |
.positions() |
| 164 |
.map(|index, pos| Ok(index.commits().entry_by_pos(pos?).commit_id())); |
| 165 |
futures::stream::iter(iter::from_fn(move || walk.next(index.as_composite()))).boxed_local() |
| 166 |
} |
| 167 |
|
| 168 |
fn commit_change_ids<'a>( |
| 169 |
&self, |
| 170 |
) -> LocalBoxStream<'a, Result<(CommitId, ChangeId), RevsetEvaluationError>> |
| 171 |
where |
| 172 |
Self: 'a, |
| 173 |
{ |
| 174 |
let index = self.index.clone(); |
| 175 |
let mut walk = self.inner.positions().map(|index, pos| { |
| 176 |
let entry = index.commits().entry_by_pos(pos?); |
| 177 |
Ok((entry.commit_id(), entry.change_id())) |
| 178 |
}); |
| 179 |
futures::stream::iter(iter::from_fn(move || walk.next(index.as_composite()))).boxed_local() |
| 180 |
} |
| 181 |
|
| 182 |
fn stream_graph<'a>( |
| 183 |
&self, |
| 184 |
) -> LocalBoxStream<'a, Result<GraphNode<CommitId>, RevsetEvaluationError>> |
| 185 |
where |
| 186 |
Self: 'a, |
| 187 |
{ |
| 188 |
let skip_transitive_edges = true; |
| 189 |
futures::stream::iter(self.iter_graph_impl(skip_transitive_edges)).boxed_local() |
| 190 |
} |
| 191 |
|
| 192 |
fn is_empty(&self) -> Result<bool, RevsetEvaluationError> { |
| 193 |
Ok(self.positions().next().transpose()?.is_none()) |
| 194 |
} |
| 195 |
|
| 196 |
fn count_estimate(&self) -> Result<(usize, Option<usize>), RevsetEvaluationError> { |
| 197 |
if cfg!(feature = "testing") { |
| 198 |
// Exercise the estimation feature in tests. (If we ever have a Revset |
| 199 |
// implementation in production code that returns estimates, we can probably |
| 200 |
// remove this and rewrite the associated tests.) |
| 201 |
let count = self |
| 202 |
.positions() |
| 203 |
.take(10) |
| 204 |
.process_results(|iter| iter.count())?; |
| 205 |
if count < 10 { |
| 206 |
Ok((count, Some(count))) |
| 207 |
} else { |
| 208 |
Ok((10, None)) |
| 209 |
} |
| 210 |
} else { |
| 211 |
let count = self.positions().process_results(|iter| iter.count())?; |
| 212 |
Ok((count, Some(count))) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
fn containing_fn<'a>(&self) -> Box<RevsetContainingFn<'a>> |
| 217 |
where |
| 218 |
Self: 'a, |
| 219 |
{ |
| 220 |
let positions = PositionsAccumulator::new(self.index.clone(), self.inner.positions()); |
| 221 |
Box::new(move |commit_id| { |
| 222 |
let result = positions.contains(commit_id); |
| 223 |
Box::pin(ready(result)) |
| 224 |
}) |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/// Incrementally consumes `RevWalk` of the revset collecting positions. |
| 229 |
struct PositionsAccumulator<'a, I> { |
| 230 |
index: I, |
| 231 |
inner: RefCell<PositionsAccumulatorInner<'a>>, |
| 232 |
} |
| 233 |
|
| 234 |
impl<'a, I: AsCompositeIndex> PositionsAccumulator<'a, I> { |
| 235 |
fn new(index: I, walk: BoxedRevWalk<'a>) -> Self { |
| 236 |
let inner = RefCell::new(PositionsAccumulatorInner { |
| 237 |
walk, |
| 238 |
consumed_positions: Vec::new(), |
| 239 |
}); |
| 240 |
Self { index, inner } |
| 241 |
} |
| 242 |
|
| 243 |
/// Checks whether the commit is in the revset. |
| 244 |
fn contains(&self, commit_id: &CommitId) -> Result<bool, RevsetEvaluationError> { |
| 245 |
let index = self.index.as_composite(); |
| 246 |
let Some(position) = index.commits().commit_id_to_pos(commit_id) else { |
| 247 |
return Ok(false); |
| 248 |
}; |
| 249 |
|
| 250 |
let mut inner = self.inner.borrow_mut(); |
| 251 |
inner.consume_to(index, position)?; |
| 252 |
let found = inner |
| 253 |
.consumed_positions |
| 254 |
.binary_search_by(|p| p.cmp(&position).reverse()) |
| 255 |
.is_ok(); |
| 256 |
Ok(found) |
| 257 |
} |
| 258 |
|
| 259 |
#[cfg(test)] |
| 260 |
fn consumed_len(&self) -> usize { |
| 261 |
self.inner.borrow().consumed_positions.len() |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/// Helper struct for [`PositionsAccumulator`] to simplify interior mutability. |
| 266 |
struct PositionsAccumulatorInner<'a> { |
| 267 |
walk: BoxedRevWalk<'a>, |
| 268 |
consumed_positions: Vec<GlobalCommitPosition>, |
| 269 |
} |
| 270 |
|
| 271 |
impl PositionsAccumulatorInner<'_> { |
| 272 |
/// Consumes `RevWalk` to a desired position but not deeper. |
| 273 |
fn consume_to( |
| 274 |
&mut self, |
| 275 |
index: &CompositeIndex, |
| 276 |
desired_position: GlobalCommitPosition, |
| 277 |
) -> Result<(), RevsetEvaluationError> { |
| 278 |
let last_position = self.consumed_positions.last(); |
| 279 |
if last_position.is_some_and(|&pos| pos <= desired_position) { |
| 280 |
return Ok(()); |
| 281 |
} |
| 282 |
while let Some(position) = self.walk.next(index).transpose()? { |
| 283 |
self.consumed_positions.push(position); |
| 284 |
if position <= desired_position { |
| 285 |
return Ok(()); |
| 286 |
} |
| 287 |
} |
| 288 |
Ok(()) |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/// Adapter for precomputed `GlobalCommitPosition`s. |
| 293 |
#[derive(Debug)] |
| 294 |
struct EagerRevset { |
| 295 |
positions: Vec<GlobalCommitPosition>, |
| 296 |
} |
| 297 |
|
| 298 |
impl EagerRevset { |
| 299 |
pub const fn empty() -> Self { |
| 300 |
Self { |
| 301 |
positions: Vec::new(), |
| 302 |
} |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
impl InternalRevset for EagerRevset { |
| 307 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 308 |
where |
| 309 |
Self: 'a, |
| 310 |
{ |
| 311 |
let walk = EagerRevWalk::new(self.positions.clone().into_iter()); |
| 312 |
Box::new(walk.map(|_index, pos| Ok(pos))) |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
impl ToPredicateFn for EagerRevset { |
| 317 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 318 |
where |
| 319 |
Self: 'a, |
| 320 |
{ |
| 321 |
let walk = EagerRevWalk::new(self.positions.clone().into_iter()); |
| 322 |
predicate_fn_from_rev_walk(walk) |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
/// Adapter for infallible `RevWalk` of `GlobalCommitPosition`s. |
| 327 |
struct RevWalkRevset<W> { |
| 328 |
walk: W, |
| 329 |
} |
| 330 |
|
| 331 |
impl<W> fmt::Debug for RevWalkRevset<W> { |
| 332 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 333 |
f.debug_struct("RevWalkRevset").finish_non_exhaustive() |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
impl<W> InternalRevset for RevWalkRevset<W> |
| 338 |
where |
| 339 |
W: RevWalk<CompositeIndex, Item = GlobalCommitPosition> + Clone, |
| 340 |
{ |
| 341 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 342 |
where |
| 343 |
Self: 'a, |
| 344 |
{ |
| 345 |
Box::new(self.walk.clone().map(|_index, pos| Ok(pos))) |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
impl<W> ToPredicateFn for RevWalkRevset<W> |
| 350 |
where |
| 351 |
W: RevWalk<CompositeIndex, Item = GlobalCommitPosition> + Clone, |
| 352 |
{ |
| 353 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 354 |
where |
| 355 |
Self: 'a, |
| 356 |
{ |
| 357 |
predicate_fn_from_rev_walk(self.walk.clone()) |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
fn predicate_fn_from_rev_walk<'a, W>(walk: W) -> BoxedPredicateFn<'a> |
| 362 |
where |
| 363 |
W: RevWalk<CompositeIndex, Item = GlobalCommitPosition> + 'a, |
| 364 |
{ |
| 365 |
let mut walk = walk.peekable(); |
| 366 |
Box::new(move |index, entry_pos| { |
| 367 |
while walk.next_if(index, |&pos| pos > entry_pos).is_some() { |
| 368 |
continue; |
| 369 |
} |
| 370 |
Ok(walk.next_if(index, |&pos| pos == entry_pos).is_some()) |
| 371 |
}) |
| 372 |
} |
| 373 |
|
| 374 |
#[derive(Debug)] |
| 375 |
struct FilterRevset<S, P> { |
| 376 |
candidates: S, |
| 377 |
predicate: P, |
| 378 |
} |
| 379 |
|
| 380 |
impl<S, P> InternalRevset for FilterRevset<S, P> |
| 381 |
where |
| 382 |
S: InternalRevset, |
| 383 |
P: ToPredicateFn, |
| 384 |
{ |
| 385 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 386 |
where |
| 387 |
Self: 'a, |
| 388 |
{ |
| 389 |
let mut p = self.predicate.to_predicate_fn(); |
| 390 |
Box::new(self.candidates.positions().filter_map(move |index, pos| { |
| 391 |
pos.and_then(|pos| Ok(p(index, pos)?.then_some(pos))) |
| 392 |
.transpose() |
| 393 |
})) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
impl<S, P> ToPredicateFn for FilterRevset<S, P> |
| 398 |
where |
| 399 |
S: ToPredicateFn, |
| 400 |
P: ToPredicateFn, |
| 401 |
{ |
| 402 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 403 |
where |
| 404 |
Self: 'a, |
| 405 |
{ |
| 406 |
let mut p1 = self.candidates.to_predicate_fn(); |
| 407 |
let mut p2 = self.predicate.to_predicate_fn(); |
| 408 |
Box::new(move |index, pos| Ok(p1(index, pos)? && p2(index, pos)?)) |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
#[derive(Debug)] |
| 413 |
struct NotInPredicate<S>(S); |
| 414 |
|
| 415 |
impl<S: ToPredicateFn> ToPredicateFn for NotInPredicate<S> { |
| 416 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 417 |
where |
| 418 |
Self: 'a, |
| 419 |
{ |
| 420 |
let mut p = self.0.to_predicate_fn(); |
| 421 |
Box::new(move |index, pos| Ok(!p(index, pos)?)) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
#[derive(Debug)] |
| 426 |
struct UnionRevset<S1, S2> { |
| 427 |
set1: S1, |
| 428 |
set2: S2, |
| 429 |
} |
| 430 |
|
| 431 |
impl<S1, S2> InternalRevset for UnionRevset<S1, S2> |
| 432 |
where |
| 433 |
S1: InternalRevset, |
| 434 |
S2: InternalRevset, |
| 435 |
{ |
| 436 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 437 |
where |
| 438 |
Self: 'a, |
| 439 |
{ |
| 440 |
Box::new(union_by( |
| 441 |
self.set1.positions(), |
| 442 |
self.set2.positions(), |
| 443 |
|pos1, pos2| pos1.cmp(pos2).reverse(), |
| 444 |
)) |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
impl<S1, S2> ToPredicateFn for UnionRevset<S1, S2> |
| 449 |
where |
| 450 |
S1: ToPredicateFn, |
| 451 |
S2: ToPredicateFn, |
| 452 |
{ |
| 453 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 454 |
where |
| 455 |
Self: 'a, |
| 456 |
{ |
| 457 |
let mut p1 = self.set1.to_predicate_fn(); |
| 458 |
let mut p2 = self.set2.to_predicate_fn(); |
| 459 |
Box::new(move |index, pos| Ok(p1(index, pos)? || p2(index, pos)?)) |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
/// `RevWalk` node that merges two sorted walk nodes. |
| 464 |
/// |
| 465 |
/// The input items should be sorted in ascending order by the `cmp` function. |
| 466 |
struct UnionRevWalk<I: ?Sized, W1: RevWalk<I>, W2: RevWalk<I>, C> { |
| 467 |
walk1: PeekableRevWalk<I, W1>, |
| 468 |
walk2: PeekableRevWalk<I, W2>, |
| 469 |
cmp: C, |
| 470 |
} |
| 471 |
|
| 472 |
impl<I, T, E, W1, W2, C> RevWalk<I> for UnionRevWalk<I, W1, W2, C> |
| 473 |
where |
| 474 |
I: ?Sized, |
| 475 |
W1: RevWalk<I, Item = Result<T, E>>, |
| 476 |
W2: RevWalk<I, Item = Result<T, E>>, |
| 477 |
C: FnMut(&T, &T) -> Ordering, |
| 478 |
{ |
| 479 |
type Item = W1::Item; |
| 480 |
|
| 481 |
fn next(&mut self, index: &I) -> Option<Self::Item> { |
| 482 |
match (self.walk1.peek(index), self.walk2.peek(index)) { |
| 483 |
(None, _) => self.walk2.next(index), |
| 484 |
(_, None) => self.walk1.next(index), |
| 485 |
(Some(Ok(item1)), Some(Ok(item2))) => match (self.cmp)(item1, item2) { |
| 486 |
Ordering::Less => self.walk1.next(index), |
| 487 |
Ordering::Equal => { |
| 488 |
self.walk2.next(index); |
| 489 |
self.walk1.next(index) |
| 490 |
} |
| 491 |
Ordering::Greater => self.walk2.next(index), |
| 492 |
}, |
| 493 |
(Some(Err(_)), _) => self.walk1.next(index), |
| 494 |
(_, Some(Err(_))) => self.walk2.next(index), |
| 495 |
} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
fn union_by<I, T, E, W1, W2, C>(walk1: W1, walk2: W2, cmp: C) -> UnionRevWalk<I, W1, W2, C> |
| 500 |
where |
| 501 |
I: ?Sized, |
| 502 |
W1: RevWalk<I, Item = Result<T, E>>, |
| 503 |
W2: RevWalk<I, Item = Result<T, E>>, |
| 504 |
C: FnMut(&T, &T) -> Ordering, |
| 505 |
{ |
| 506 |
UnionRevWalk { |
| 507 |
walk1: walk1.peekable(), |
| 508 |
walk2: walk2.peekable(), |
| 509 |
cmp, |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
#[derive(Debug)] |
| 514 |
struct IntersectionRevset<S1, S2> { |
| 515 |
set1: S1, |
| 516 |
set2: S2, |
| 517 |
} |
| 518 |
|
| 519 |
impl<S1, S2> InternalRevset for IntersectionRevset<S1, S2> |
| 520 |
where |
| 521 |
S1: InternalRevset, |
| 522 |
S2: InternalRevset, |
| 523 |
{ |
| 524 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 525 |
where |
| 526 |
Self: 'a, |
| 527 |
{ |
| 528 |
Box::new(intersection_by( |
| 529 |
self.set1.positions(), |
| 530 |
self.set2.positions(), |
| 531 |
|pos1, pos2| pos1.cmp(pos2).reverse(), |
| 532 |
)) |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
impl<S1, S2> ToPredicateFn for IntersectionRevset<S1, S2> |
| 537 |
where |
| 538 |
S1: ToPredicateFn, |
| 539 |
S2: ToPredicateFn, |
| 540 |
{ |
| 541 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 542 |
where |
| 543 |
Self: 'a, |
| 544 |
{ |
| 545 |
let mut p1 = self.set1.to_predicate_fn(); |
| 546 |
let mut p2 = self.set2.to_predicate_fn(); |
| 547 |
Box::new(move |index, pos| Ok(p1(index, pos)? && p2(index, pos)?)) |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/// `RevWalk` node that intersects two sorted walk nodes. |
| 552 |
/// |
| 553 |
/// The input items should be sorted in ascending order by the `cmp` function. |
| 554 |
struct IntersectionRevWalk<I: ?Sized, W1: RevWalk<I>, W2: RevWalk<I>, C> { |
| 555 |
walk1: PeekableRevWalk<I, W1>, |
| 556 |
walk2: PeekableRevWalk<I, W2>, |
| 557 |
cmp: C, |
| 558 |
} |
| 559 |
|
| 560 |
impl<I, T, E, W1, W2, C> RevWalk<I> for IntersectionRevWalk<I, W1, W2, C> |
| 561 |
where |
| 562 |
I: ?Sized, |
| 563 |
W1: RevWalk<I, Item = Result<T, E>>, |
| 564 |
W2: RevWalk<I, Item = Result<T, E>>, |
| 565 |
C: FnMut(&T, &T) -> Ordering, |
| 566 |
{ |
| 567 |
type Item = W1::Item; |
| 568 |
|
| 569 |
fn next(&mut self, index: &I) -> Option<Self::Item> { |
| 570 |
loop { |
| 571 |
match (self.walk1.peek(index), self.walk2.peek(index)) { |
| 572 |
(None, _) => { |
| 573 |
return None; |
| 574 |
} |
| 575 |
(_, None) => { |
| 576 |
return None; |
| 577 |
} |
| 578 |
(Some(Ok(item1)), Some(Ok(item2))) => match (self.cmp)(item1, item2) { |
| 579 |
Ordering::Less => { |
| 580 |
self.walk1.next(index); |
| 581 |
} |
| 582 |
Ordering::Equal => { |
| 583 |
self.walk2.next(index); |
| 584 |
return self.walk1.next(index); |
| 585 |
} |
| 586 |
Ordering::Greater => { |
| 587 |
self.walk2.next(index); |
| 588 |
} |
| 589 |
}, |
| 590 |
(Some(Err(_)), _) => { |
| 591 |
return self.walk1.next(index); |
| 592 |
} |
| 593 |
(_, Some(Err(_))) => { |
| 594 |
return self.walk2.next(index); |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
fn intersection_by<I, T, E, W1, W2, C>( |
| 602 |
walk1: W1, |
| 603 |
walk2: W2, |
| 604 |
cmp: C, |
| 605 |
) -> IntersectionRevWalk<I, W1, W2, C> |
| 606 |
where |
| 607 |
I: ?Sized, |
| 608 |
W1: RevWalk<I, Item = Result<T, E>>, |
| 609 |
W2: RevWalk<I, Item = Result<T, E>>, |
| 610 |
C: FnMut(&T, &T) -> Ordering, |
| 611 |
{ |
| 612 |
IntersectionRevWalk { |
| 613 |
walk1: walk1.peekable(), |
| 614 |
walk2: walk2.peekable(), |
| 615 |
cmp, |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
#[derive(Debug)] |
| 620 |
struct DifferenceRevset<S1, S2> { |
| 621 |
// The minuend (what to subtract from) |
| 622 |
set1: S1, |
| 623 |
// The subtrahend (what to subtract) |
| 624 |
set2: S2, |
| 625 |
} |
| 626 |
|
| 627 |
impl<S1, S2> InternalRevset for DifferenceRevset<S1, S2> |
| 628 |
where |
| 629 |
S1: InternalRevset, |
| 630 |
S2: InternalRevset, |
| 631 |
{ |
| 632 |
fn positions<'a>(&self) -> BoxedRevWalk<'a> |
| 633 |
where |
| 634 |
Self: 'a, |
| 635 |
{ |
| 636 |
Box::new(difference_by( |
| 637 |
self.set1.positions(), |
| 638 |
self.set2.positions(), |
| 639 |
|pos1, pos2| pos1.cmp(pos2).reverse(), |
| 640 |
)) |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
impl<S1, S2> ToPredicateFn for DifferenceRevset<S1, S2> |
| 645 |
where |
| 646 |
S1: ToPredicateFn, |
| 647 |
S2: ToPredicateFn, |
| 648 |
{ |
| 649 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 650 |
where |
| 651 |
Self: 'a, |
| 652 |
{ |
| 653 |
let mut p1 = self.set1.to_predicate_fn(); |
| 654 |
let mut p2 = self.set2.to_predicate_fn(); |
| 655 |
Box::new(move |index, pos| Ok(p1(index, pos)? && !p2(index, pos)?)) |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
/// `RevWalk` node that subtracts `walk2` items from `walk1`. |
| 660 |
/// |
| 661 |
/// The input items should be sorted in ascending order by the `cmp` function. |
| 662 |
struct DifferenceRevWalk<I: ?Sized, W1: RevWalk<I>, W2: RevWalk<I>, C> { |
| 663 |
walk1: PeekableRevWalk<I, W1>, |
| 664 |
walk2: PeekableRevWalk<I, W2>, |
| 665 |
cmp: C, |
| 666 |
} |
| 667 |
|
| 668 |
impl<I, T, E, W1, W2, C> RevWalk<I> for DifferenceRevWalk<I, W1, W2, C> |
| 669 |
where |
| 670 |
I: ?Sized, |
| 671 |
W1: RevWalk<I, Item = Result<T, E>>, |
| 672 |
W2: RevWalk<I, Item = Result<T, E>>, |
| 673 |
C: FnMut(&T, &T) -> Ordering, |
| 674 |
{ |
| 675 |
type Item = W1::Item; |
| 676 |
|
| 677 |
fn next(&mut self, index: &I) -> Option<Self::Item> { |
| 678 |
loop { |
| 679 |
match (self.walk1.peek(index), self.walk2.peek(index)) { |
| 680 |
(None, _) => { |
| 681 |
return None; |
| 682 |
} |
| 683 |
(_, None) => { |
| 684 |
return self.walk1.next(index); |
| 685 |
} |
| 686 |
(Some(Ok(item1)), Some(Ok(item2))) => match (self.cmp)(item1, item2) { |
| 687 |
Ordering::Less => { |
| 688 |
return self.walk1.next(index); |
| 689 |
} |
| 690 |
Ordering::Equal => { |
| 691 |
self.walk2.next(index); |
| 692 |
self.walk1.next(index); |
| 693 |
} |
| 694 |
Ordering::Greater => { |
| 695 |
self.walk2.next(index); |
| 696 |
} |
| 697 |
}, |
| 698 |
(Some(Err(_)), _) => { |
| 699 |
return self.walk1.next(index); |
| 700 |
} |
| 701 |
(_, Some(Err(_))) => { |
| 702 |
return self.walk2.next(index); |
| 703 |
} |
| 704 |
} |
| 705 |
} |
| 706 |
} |
| 707 |
} |
| 708 |
|
| 709 |
fn difference_by<I, T, E, W1, W2, C>( |
| 710 |
walk1: W1, |
| 711 |
walk2: W2, |
| 712 |
cmp: C, |
| 713 |
) -> DifferenceRevWalk<I, W1, W2, C> |
| 714 |
where |
| 715 |
I: ?Sized, |
| 716 |
W1: RevWalk<I, Item = Result<T, E>>, |
| 717 |
W2: RevWalk<I, Item = Result<T, E>>, |
| 718 |
C: FnMut(&T, &T) -> Ordering, |
| 719 |
{ |
| 720 |
DifferenceRevWalk { |
| 721 |
walk1: walk1.peekable(), |
| 722 |
walk2: walk2.peekable(), |
| 723 |
cmp, |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
pub(super) fn evaluate<I: AsCompositeIndex + Clone>( |
| 728 |
expression: &ResolvedExpression, |
| 729 |
store: &Arc<Store>, |
| 730 |
index: I, |
| 731 |
) -> Result<RevsetImpl<I>, RevsetEvaluationError> { |
| 732 |
let context = EvaluationContext { |
| 733 |
store: store.clone(), |
| 734 |
index: index.as_composite(), |
| 735 |
}; |
| 736 |
let internal_revset = context.evaluate(expression)?; |
| 737 |
Ok(RevsetImpl::new(internal_revset, index)) |
| 738 |
} |
| 739 |
|
| 740 |
struct EvaluationContext<'index> { |
| 741 |
store: Arc<Store>, |
| 742 |
index: &'index CompositeIndex, |
| 743 |
} |
| 744 |
|
| 745 |
fn to_u32_generation_range(range: &Range<u64>) -> Result<Range<u32>, RevsetEvaluationError> { |
| 746 |
let start = range.start.try_into().map_err(|_| { |
| 747 |
RevsetEvaluationError::Other( |
| 748 |
format!("Lower bound of generation ({}) is too large", range.start).into(), |
| 749 |
) |
| 750 |
})?; |
| 751 |
let end = range.end.try_into().unwrap_or(u32::MAX); |
| 752 |
Ok(start..end) |
| 753 |
} |
| 754 |
|
| 755 |
impl EvaluationContext<'_> { |
| 756 |
fn evaluate( |
| 757 |
&self, |
| 758 |
expression: &ResolvedExpression, |
| 759 |
) -> Result<Box<dyn InternalRevset>, RevsetEvaluationError> { |
| 760 |
let index = self.index; |
| 761 |
match expression { |
| 762 |
ResolvedExpression::Commits(commit_ids) => { |
| 763 |
Ok(Box::new(self.revset_for_commit_ids(commit_ids)?)) |
| 764 |
} |
| 765 |
ResolvedExpression::Ancestors { |
| 766 |
heads, |
| 767 |
generation, |
| 768 |
parents_range, |
| 769 |
} => { |
| 770 |
let head_set = self.evaluate(heads)?; |
| 771 |
let head_positions = head_set.positions().attach(index); |
| 772 |
let builder = RevWalkBuilder::new(index) |
| 773 |
.wanted_heads(head_positions.try_collect()?) |
| 774 |
.wanted_parents_range(parents_range.clone()); |
| 775 |
if generation == &GENERATION_RANGE_FULL { |
| 776 |
let walk = builder.ancestors().detach(); |
| 777 |
Ok(Box::new(RevWalkRevset { walk })) |
| 778 |
} else { |
| 779 |
let generation = to_u32_generation_range(generation)?; |
| 780 |
let walk = builder |
| 781 |
.ancestors_filtered_by_generation(generation) |
| 782 |
.detach(); |
| 783 |
Ok(Box::new(RevWalkRevset { walk })) |
| 784 |
} |
| 785 |
} |
| 786 |
ResolvedExpression::Range { |
| 787 |
roots, |
| 788 |
heads, |
| 789 |
generation, |
| 790 |
parents_range, |
| 791 |
} => { |
| 792 |
let root_set = self.evaluate(roots)?; |
| 793 |
let root_positions: Vec<_> = root_set.positions().attach(index).try_collect()?; |
| 794 |
// Pre-filter heads so queries like 'immutable_heads()..' can |
| 795 |
// terminate early. immutable_heads() usually includes some |
| 796 |
// visible heads, which can be trivially rejected. |
| 797 |
let head_set = self.evaluate(heads)?; |
| 798 |
let head_positions = difference_by( |
| 799 |
head_set.positions(), |
| 800 |
EagerRevWalk::new(root_positions.iter().copied().map(Ok)), |
| 801 |
|pos1, pos2| pos1.cmp(pos2).reverse(), |
| 802 |
) |
| 803 |
.attach(index); |
| 804 |
let builder = RevWalkBuilder::new(index) |
| 805 |
.wanted_heads(head_positions.try_collect()?) |
| 806 |
.wanted_parents_range(parents_range.clone()) |
| 807 |
.unwanted_roots(root_positions); |
| 808 |
if generation == &GENERATION_RANGE_FULL { |
| 809 |
let walk = builder.ancestors().detach(); |
| 810 |
Ok(Box::new(RevWalkRevset { walk })) |
| 811 |
} else { |
| 812 |
let generation = to_u32_generation_range(generation)?; |
| 813 |
let walk = builder |
| 814 |
.ancestors_filtered_by_generation(generation) |
| 815 |
.detach(); |
| 816 |
Ok(Box::new(RevWalkRevset { walk })) |
| 817 |
} |
| 818 |
} |
| 819 |
ResolvedExpression::DagRange { |
| 820 |
roots, |
| 821 |
heads, |
| 822 |
generation_from_roots, |
| 823 |
} => { |
| 824 |
let root_set = self.evaluate(roots)?; |
| 825 |
let root_positions = root_set.positions().attach(index); |
| 826 |
let head_set = self.evaluate(heads)?; |
| 827 |
let head_positions = head_set.positions().attach(index); |
| 828 |
let builder = |
| 829 |
RevWalkBuilder::new(index).wanted_heads(head_positions.try_collect()?); |
| 830 |
if generation_from_roots == &(1..2) { |
| 831 |
let root_positions: HashSet<_> = root_positions.try_collect()?; |
| 832 |
let walk = builder |
| 833 |
.ancestors_until_roots(root_positions.iter().copied()) |
| 834 |
.detach(); |
| 835 |
let candidates = RevWalkRevset { walk }; |
| 836 |
let predicate = as_pure_predicate_fn(move |index, pos| { |
| 837 |
Ok(index |
| 838 |
.commits() |
| 839 |
.entry_by_pos(pos) |
| 840 |
.parent_positions() |
| 841 |
.iter() |
| 842 |
.any(|parent_pos| root_positions.contains(parent_pos))) |
| 843 |
}); |
| 844 |
// TODO: Suppose heads include all visible heads, ToPredicateFn version can be |
| 845 |
// optimized to only test the predicate() |
| 846 |
Ok(Box::new(FilterRevset { |
| 847 |
candidates, |
| 848 |
predicate, |
| 849 |
})) |
| 850 |
} else if generation_from_roots == &GENERATION_RANGE_FULL { |
| 851 |
let mut positions = builder |
| 852 |
.descendants(root_positions.try_collect()?) |
| 853 |
.collect_vec(); |
| 854 |
positions.reverse(); |
| 855 |
Ok(Box::new(EagerRevset { positions })) |
| 856 |
} else { |
| 857 |
// For small generation range, it might be better to build a reachable map |
| 858 |
// with generation bit set, which can be calculated incrementally from roots: |
| 859 |
// reachable[pos] = (reachable[parent_pos] | ...) << 1 |
| 860 |
let mut positions = builder |
| 861 |
.descendants_filtered_by_generation( |
| 862 |
root_positions.try_collect()?, |
| 863 |
to_u32_generation_range(generation_from_roots)?, |
| 864 |
) |
| 865 |
.map(|Reverse(pos)| pos) |
| 866 |
.collect_vec(); |
| 867 |
positions.reverse(); |
| 868 |
Ok(Box::new(EagerRevset { positions })) |
| 869 |
} |
| 870 |
} |
| 871 |
ResolvedExpression::Reachable { sources, domain } => { |
| 872 |
let mut sets = union_find::UnionFind::<GlobalCommitPosition>::new(); |
| 873 |
|
| 874 |
// Compute all reachable subgraphs. |
| 875 |
let domain_revset = self.evaluate(domain)?; |
| 876 |
let domain_vec: Vec<_> = domain_revset.positions().attach(index).try_collect()?; |
| 877 |
let domain_set: HashSet<_> = domain_vec.iter().copied().collect(); |
| 878 |
for pos in &domain_set { |
| 879 |
for parent_pos in index.commits().entry_by_pos(*pos).parent_positions() { |
| 880 |
if domain_set.contains(&parent_pos) { |
| 881 |
sets.union(*pos, parent_pos); |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
// `UnionFind::find` is somewhat slow, so it's faster to only do this once and |
| 886 |
// then cache the result. |
| 887 |
let domain_reps = domain_vec.iter().map(|&pos| sets.find(pos)).collect_vec(); |
| 888 |
|
| 889 |
// Identify disjoint sets reachable from sources. Using a predicate here can be |
| 890 |
// significantly faster for cases like `reachable(filter, X)`, since the filter |
| 891 |
// can be checked for only commits in `X` instead of for all visible commits, |
| 892 |
// and the difference is usually negligible for non-filter revsets. |
| 893 |
let sources_revset = self.evaluate(sources)?; |
| 894 |
let mut sources_predicate = sources_revset.to_predicate_fn(); |
| 895 |
let mut set_reps = HashSet::new(); |
| 896 |
for (&pos, &rep) in domain_vec.iter().zip(&domain_reps) { |
| 897 |
// Skip evaluating predicate if `rep` has already been added. |
| 898 |
if set_reps.contains(&rep) { |
| 899 |
continue; |
| 900 |
} |
| 901 |
if sources_predicate(index, pos)? { |
| 902 |
set_reps.insert(rep); |
| 903 |
} |
| 904 |
} |
| 905 |
|
| 906 |
let positions = domain_vec |
| 907 |
.into_iter() |
| 908 |
.zip(domain_reps) |
| 909 |
.filter_map(|(pos, rep)| set_reps.contains(&rep).then_some(pos)) |
| 910 |
.collect_vec(); |
| 911 |
Ok(Box::new(EagerRevset { positions })) |
| 912 |
} |
| 913 |
ResolvedExpression::Heads(candidates) => { |
| 914 |
let candidate_set = self.evaluate(candidates)?; |
| 915 |
let positions = index |
| 916 |
.commits() |
| 917 |
.heads_pos(candidate_set.positions().attach(index).try_collect()?); |
| 918 |
Ok(Box::new(EagerRevset { positions })) |
| 919 |
} |
| 920 |
ResolvedExpression::HeadsRange { |
| 921 |
roots, |
| 922 |
heads, |
| 923 |
parents_range, |
| 924 |
filter, |
| 925 |
} => { |
| 926 |
let root_set = self.evaluate(roots)?; |
| 927 |
let root_positions: Vec<_> = root_set.positions().attach(index).try_collect()?; |
| 928 |
// Pre-filter heads so queries like 'immutable_heads()..' can |
| 929 |
// terminate early. immutable_heads() usually includes some |
| 930 |
// visible heads, which can be trivially rejected. |
| 931 |
let head_set = self.evaluate(heads)?; |
| 932 |
let head_positions = difference_by( |
| 933 |
head_set.positions(), |
| 934 |
EagerRevWalk::new(root_positions.iter().copied().map(Ok)), |
| 935 |
|pos1, pos2| pos1.cmp(pos2).reverse(), |
| 936 |
) |
| 937 |
.attach(index) |
| 938 |
.try_collect()?; |
| 939 |
let positions = if let Some(filter) = filter { |
| 940 |
let mut filter = self.evaluate_predicate(filter)?.to_predicate_fn(); |
| 941 |
index.commits().heads_from_range_and_filter( |
| 942 |
root_positions, |
| 943 |
head_positions, |
| 944 |
parents_range, |
| 945 |
|pos| filter(index, pos), |
| 946 |
)? |
| 947 |
} else { |
| 948 |
let Ok(positions) = index.commits().heads_from_range_and_filter::<Infallible>( |
| 949 |
root_positions, |
| 950 |
head_positions, |
| 951 |
parents_range, |
| 952 |
|_| Ok(true), |
| 953 |
); |
| 954 |
positions |
| 955 |
}; |
| 956 |
Ok(Box::new(EagerRevset { positions })) |
| 957 |
} |
| 958 |
ResolvedExpression::Roots(candidates) => { |
| 959 |
let mut positions: Vec<_> = self |
| 960 |
.evaluate(candidates)? |
| 961 |
.positions() |
| 962 |
.attach(index) |
| 963 |
.try_collect()?; |
| 964 |
let filled = RevWalkBuilder::new(index) |
| 965 |
.wanted_heads(positions.clone()) |
| 966 |
.descendants(positions.iter().copied().collect()) |
| 967 |
.collect_positions_set(); |
| 968 |
positions.retain(|&pos| { |
| 969 |
!index |
| 970 |
.commits() |
| 971 |
.entry_by_pos(pos) |
| 972 |
.parent_positions() |
| 973 |
.iter() |
| 974 |
.any(|parent| filled.contains(parent)) |
| 975 |
}); |
| 976 |
Ok(Box::new(EagerRevset { positions })) |
| 977 |
} |
| 978 |
ResolvedExpression::Forks { heads } => { |
| 979 |
let head_positions = self |
| 980 |
.evaluate(heads)? |
| 981 |
.positions() |
| 982 |
.attach(index) |
| 983 |
.try_collect()?; |
| 984 |
let mut child_counts: HashMap<GlobalCommitPosition, u32> = HashMap::new(); |
| 985 |
let walk = RevWalkBuilder::new(index) |
| 986 |
.wanted_heads(head_positions) |
| 987 |
.ancestors() |
| 988 |
.detach() |
| 989 |
.filter_map(move |index: &CompositeIndex, pos| { |
| 990 |
let is_fork = child_counts.remove(&pos).unwrap_or(0) >= 2; |
| 991 |
for parent in index.commits().entry_by_pos(pos).parent_positions() { |
| 992 |
*child_counts.entry(parent).or_insert(0) += 1; |
| 993 |
} |
| 994 |
is_fork.then_some(pos) |
| 995 |
}); |
| 996 |
Ok(Box::new(RevWalkRevset { walk })) |
| 997 |
} |
| 998 |
ResolvedExpression::ForkPoint(expression) => { |
| 999 |
let expression_set = self.evaluate(expression)?; |
| 1000 |
let mut expression_positions_iter = expression_set.positions().attach(index); |
| 1001 |
let Some(position) = expression_positions_iter.next() else { |
| 1002 |
return Ok(Box::new(EagerRevset::empty())); |
| 1003 |
}; |
| 1004 |
let mut positions = vec![position?]; |
| 1005 |
for position in expression_positions_iter { |
| 1006 |
positions = index |
| 1007 |
.commits() |
| 1008 |
.common_ancestors_pos(positions, vec![position?]); |
| 1009 |
} |
| 1010 |
Ok(Box::new(EagerRevset { positions })) |
| 1011 |
} |
| 1012 |
ResolvedExpression::MergePoint { |
| 1013 |
roots, |
| 1014 |
visible_heads, |
| 1015 |
} => { |
| 1016 |
let mut root_position_iter = self.evaluate(roots)?.positions().attach(index); |
| 1017 |
let Some(position) = root_position_iter.next() else { |
| 1018 |
return Ok(Box::new(EagerRevset::empty())); |
| 1019 |
}; |
| 1020 |
let visible_head_positions = self |
| 1021 |
.evaluate(visible_heads)? |
| 1022 |
.positions() |
| 1023 |
.attach(index) |
| 1024 |
.try_collect()?; |
| 1025 |
let mut candidates = RevWalkBuilder::new(index) |
| 1026 |
.wanted_heads(visible_head_positions) |
| 1027 |
.descendants(maplit::hashset![position?]) |
| 1028 |
.collect_vec(); |
| 1029 |
candidates.reverse(); |
| 1030 |
for position in root_position_iter { |
| 1031 |
let descendants = RevWalkBuilder::new(index) |
| 1032 |
.wanted_heads(candidates.clone()) |
| 1033 |
.descendants(maplit::hashset![position?]) |
| 1034 |
.collect_positions_set(); |
| 1035 |
candidates.retain(|pos| descendants.contains(pos)); |
| 1036 |
if candidates.is_empty() { |
| 1037 |
return Ok(Box::new(EagerRevset::empty())); |
| 1038 |
} |
| 1039 |
} |
| 1040 |
let candidate_set: HashSet<_> = candidates.iter().copied().collect(); |
| 1041 |
candidates.retain(|&pos| { |
| 1042 |
!index |
| 1043 |
.commits() |
| 1044 |
.entry_by_pos(pos) |
| 1045 |
.parent_positions() |
| 1046 |
.iter() |
| 1047 |
.any(|parent| candidate_set.contains(parent)) |
| 1048 |
}); |
| 1049 |
Ok(Box::new(EagerRevset { |
| 1050 |
positions: candidates, |
| 1051 |
})) |
| 1052 |
} |
| 1053 |
ResolvedExpression::Bisect(candidates) => { |
| 1054 |
let set = self.evaluate(candidates)?; |
| 1055 |
// TODO: Make this more correct in non-linear history |
| 1056 |
let candidate_positions: Vec<_> = set.positions().attach(index).try_collect()?; |
| 1057 |
let positions = if candidate_positions.is_empty() { |
| 1058 |
candidate_positions |
| 1059 |
} else { |
| 1060 |
vec![candidate_positions[candidate_positions.len() / 2]] |
| 1061 |
}; |
| 1062 |
Ok(Box::new(EagerRevset { positions })) |
| 1063 |
} |
| 1064 |
ResolvedExpression::Latest { candidates, count } => { |
| 1065 |
let candidate_set = self.evaluate(candidates)?; |
| 1066 |
Ok(Box::new(self.take_latest_revset(&*candidate_set, *count)?)) |
| 1067 |
} |
| 1068 |
ResolvedExpression::HasSize { candidates, count } => { |
| 1069 |
let set = self.evaluate(candidates)?; |
| 1070 |
let positions: Vec<_> = set |
| 1071 |
.positions() |
| 1072 |
.attach(index) |
| 1073 |
.take(count.saturating_add(1)) |
| 1074 |
.try_collect()?; |
| 1075 |
if positions.len() != *count { |
| 1076 |
let message = if positions.len() > *count { |
| 1077 |
// https://github.com/jj-vcs/jj/pull/7252#pullrequestreview-3236259998 |
| 1078 |
// In the default engine we have to evaluate the entire |
| 1079 |
// revset (which may be very large) to get an exact |
| 1080 |
// count; we would need to remove .take() above. Instead |
| 1081 |
// just give a vague error message. |
| 1082 |
format!("The revset has more than the expected {count} revisions") |
| 1083 |
} else { |
| 1084 |
format!( |
| 1085 |
"The revset has fewer than the expected {count} revisions (got {})", |
| 1086 |
positions.len() |
| 1087 |
) |
| 1088 |
}; |
| 1089 |
return Err(RevsetEvaluationError::Other(message.into())); |
| 1090 |
} |
| 1091 |
Ok(Box::new(EagerRevset { positions })) |
| 1092 |
} |
| 1093 |
ResolvedExpression::Coalesce(expression1, expression2) => { |
| 1094 |
let set1 = self.evaluate(expression1)?; |
| 1095 |
if set1.positions().attach(index).next().is_some() { |
| 1096 |
Ok(set1) |
| 1097 |
} else { |
| 1098 |
self.evaluate(expression2) |
| 1099 |
} |
| 1100 |
} |
| 1101 |
ResolvedExpression::Union(expression1, expression2) => { |
| 1102 |
let set1 = self.evaluate(expression1)?; |
| 1103 |
let set2 = self.evaluate(expression2)?; |
| 1104 |
Ok(Box::new(UnionRevset { set1, set2 })) |
| 1105 |
} |
| 1106 |
ResolvedExpression::FilterWithin { |
| 1107 |
candidates, |
| 1108 |
predicate, |
| 1109 |
} => Ok(Box::new(FilterRevset { |
| 1110 |
candidates: self.evaluate(candidates)?, |
| 1111 |
predicate: self.evaluate_predicate(predicate)?, |
| 1112 |
})), |
| 1113 |
ResolvedExpression::Intersection(expression1, expression2) => { |
| 1114 |
let set1 = self.evaluate(expression1)?; |
| 1115 |
let set2 = self.evaluate(expression2)?; |
| 1116 |
Ok(Box::new(IntersectionRevset { set1, set2 })) |
| 1117 |
} |
| 1118 |
ResolvedExpression::Difference(expression1, expression2) => { |
| 1119 |
let set1 = self.evaluate(expression1)?; |
| 1120 |
let set2 = self.evaluate(expression2)?; |
| 1121 |
Ok(Box::new(DifferenceRevset { set1, set2 })) |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
fn evaluate_predicate( |
| 1127 |
&self, |
| 1128 |
expression: &ResolvedPredicateExpression, |
| 1129 |
) -> Result<Box<dyn ToPredicateFn>, RevsetEvaluationError> { |
| 1130 |
match expression { |
| 1131 |
ResolvedPredicateExpression::Filter(predicate) => { |
| 1132 |
Ok(build_predicate_fn(self.store.clone(), predicate)) |
| 1133 |
} |
| 1134 |
ResolvedPredicateExpression::Divergent { visible_heads } => { |
| 1135 |
let composite = self.index.as_composite().commits(); |
| 1136 |
let mut reachable_set = AncestorsBitSet::with_capacity(composite.num_commits()); |
| 1137 |
for id in visible_heads { |
| 1138 |
reachable_set.add_head(composite.commit_id_to_pos(id).unwrap()); |
| 1139 |
} |
| 1140 |
let reachable_set = Rc::new(RefCell::new(reachable_set)); |
| 1141 |
Ok(box_pure_predicate_fn( |
| 1142 |
move |index: &CompositeIndex, pos: GlobalCommitPosition| { |
| 1143 |
let commits = index.commits(); |
| 1144 |
|
| 1145 |
match commits.resolve_change_id_prefix(&HexPrefix::from_id( |
| 1146 |
&commits.entry_by_pos(pos).change_id(), |
| 1147 |
)) { |
| 1148 |
PrefixResolution::NoMatch => { |
| 1149 |
panic!("the commit itself should be reachable") |
| 1150 |
} |
| 1151 |
PrefixResolution::SingleMatch((_change_id, positions)) => { |
| 1152 |
let mut reachable_set = reachable_set.borrow_mut(); |
| 1153 |
let targets = commits.resolve_change_targets_for_positions( |
| 1154 |
&positions, |
| 1155 |
&mut reachable_set, |
| 1156 |
); |
| 1157 |
Ok(targets.is_divergent()) |
| 1158 |
} |
| 1159 |
PrefixResolution::AmbiguousMatch => { |
| 1160 |
panic!("complete change_id should be unambiguous") |
| 1161 |
} |
| 1162 |
} |
| 1163 |
}, |
| 1164 |
)) |
| 1165 |
} |
| 1166 |
ResolvedPredicateExpression::Set(expression) => Ok(self.evaluate(expression)?), |
| 1167 |
ResolvedPredicateExpression::NotIn(complement) => { |
| 1168 |
let set = self.evaluate_predicate(complement)?; |
| 1169 |
Ok(Box::new(NotInPredicate(set))) |
| 1170 |
} |
| 1171 |
ResolvedPredicateExpression::Union(expression1, expression2) => { |
| 1172 |
let set1 = self.evaluate_predicate(expression1)?; |
| 1173 |
let set2 = self.evaluate_predicate(expression2)?; |
| 1174 |
Ok(Box::new(UnionRevset { set1, set2 })) |
| 1175 |
} |
| 1176 |
ResolvedPredicateExpression::Intersection(expression1, expression2) => { |
| 1177 |
let set1 = self.evaluate_predicate(expression1)?; |
| 1178 |
let set2 = self.evaluate_predicate(expression2)?; |
| 1179 |
Ok(Box::new(IntersectionRevset { set1, set2 })) |
| 1180 |
} |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
fn revset_for_commit_ids( |
| 1185 |
&self, |
| 1186 |
commit_ids: &[CommitId], |
| 1187 |
) -> Result<EagerRevset, RevsetEvaluationError> { |
| 1188 |
let mut positions: Vec<_> = commit_ids |
| 1189 |
.iter() |
| 1190 |
.map(|id| { |
| 1191 |
// Invalid commit IDs should be rejected by the revset frontend, |
| 1192 |
// but there are a few edge cases that break the precondition. |
| 1193 |
// For example, in jj <= 0.22, the root commit doesn't exist in |
| 1194 |
// the root operation. |
| 1195 |
self.index.commits().commit_id_to_pos(id).ok_or_else(|| { |
| 1196 |
RevsetEvaluationError::Other( |
| 1197 |
format!( |
| 1198 |
"Commit ID {} not found in index (index or view might be corrupted)", |
| 1199 |
id.hex() |
| 1200 |
) |
| 1201 |
.into(), |
| 1202 |
) |
| 1203 |
}) |
| 1204 |
}) |
| 1205 |
.try_collect()?; |
| 1206 |
positions.sort_unstable_by_key(|&pos| Reverse(pos)); |
| 1207 |
positions.dedup(); |
| 1208 |
Ok(EagerRevset { positions }) |
| 1209 |
} |
| 1210 |
|
| 1211 |
fn take_latest_revset( |
| 1212 |
&self, |
| 1213 |
candidate_set: &dyn InternalRevset, |
| 1214 |
count: usize, |
| 1215 |
) -> Result<EagerRevset, RevsetEvaluationError> { |
| 1216 |
if count == 0 { |
| 1217 |
return Ok(EagerRevset::empty()); |
| 1218 |
} |
| 1219 |
|
| 1220 |
#[derive(Clone, Eq, Ord, PartialEq, PartialOrd)] |
| 1221 |
struct Item { |
| 1222 |
timestamp: MillisSinceEpoch, |
| 1223 |
pos: GlobalCommitPosition, // tie-breaker |
| 1224 |
} |
| 1225 |
|
| 1226 |
let make_rev_item = |pos| -> Result<_, RevsetEvaluationError> { |
| 1227 |
let entry = self.index.commits().entry_by_pos(pos?); |
| 1228 |
let commit = self.store.get_commit(&entry.commit_id())?; |
| 1229 |
Ok(Reverse(Item { |
| 1230 |
timestamp: commit.committer().timestamp.timestamp, |
| 1231 |
pos: entry.position(), |
| 1232 |
})) |
| 1233 |
}; |
| 1234 |
|
| 1235 |
// Maintain min-heap containing the latest (greatest) count items. For small |
| 1236 |
// count and large candidate set, this is probably cheaper than building vec |
| 1237 |
// and applying selection algorithm. |
| 1238 |
let mut candidate_iter = candidate_set |
| 1239 |
.positions() |
| 1240 |
.attach(self.index) |
| 1241 |
.map(make_rev_item) |
| 1242 |
.fuse(); |
| 1243 |
let mut latest_items: BinaryHeap<_> = candidate_iter.by_ref().take(count).try_collect()?; |
| 1244 |
for item in candidate_iter { |
| 1245 |
let item = item?; |
| 1246 |
let mut earliest = latest_items.peek_mut().unwrap(); |
| 1247 |
if earliest.0 < item.0 { |
| 1248 |
*earliest = item; |
| 1249 |
} |
| 1250 |
} |
| 1251 |
|
| 1252 |
assert!(latest_items.len() <= count); |
| 1253 |
let mut positions = latest_items |
| 1254 |
.into_iter() |
| 1255 |
.map(|item| item.0.pos) |
| 1256 |
.collect_vec(); |
| 1257 |
positions.sort_unstable_by_key(|&pos| Reverse(pos)); |
| 1258 |
Ok(EagerRevset { positions }) |
| 1259 |
} |
| 1260 |
} |
| 1261 |
|
| 1262 |
struct PurePredicateFn<F>(F); |
| 1263 |
|
| 1264 |
impl<F> fmt::Debug for PurePredicateFn<F> { |
| 1265 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 1266 |
f.debug_struct("PurePredicateFn").finish_non_exhaustive() |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
impl<F> ToPredicateFn for PurePredicateFn<F> |
| 1271 |
where |
| 1272 |
F: Fn(&CompositeIndex, GlobalCommitPosition) -> Result<bool, RevsetEvaluationError> + Clone, |
| 1273 |
{ |
| 1274 |
fn to_predicate_fn<'a>(&self) -> BoxedPredicateFn<'a> |
| 1275 |
where |
| 1276 |
Self: 'a, |
| 1277 |
{ |
| 1278 |
Box::new(self.0.clone()) |
| 1279 |
} |
| 1280 |
} |
| 1281 |
|
| 1282 |
fn as_pure_predicate_fn<F>(f: F) -> PurePredicateFn<F> |
| 1283 |
where |
| 1284 |
F: Fn(&CompositeIndex, GlobalCommitPosition) -> Result<bool, RevsetEvaluationError> + Clone, |
| 1285 |
{ |
| 1286 |
PurePredicateFn(f) |
| 1287 |
} |
| 1288 |
|
| 1289 |
fn box_pure_predicate_fn<'a, F>(f: F) -> Box<dyn ToPredicateFn + 'a> |
| 1290 |
where |
| 1291 |
F: Fn(&CompositeIndex, GlobalCommitPosition) -> Result<bool, RevsetEvaluationError> |
| 1292 |
+ Clone |
| 1293 |
+ 'a, |
| 1294 |
{ |
| 1295 |
Box::new(PurePredicateFn(f)) |
| 1296 |
} |
| 1297 |
|
| 1298 |
fn build_predicate_fn( |
| 1299 |
store: Arc<Store>, |
| 1300 |
predicate: &RevsetFilterPredicate, |
| 1301 |
) -> Box<dyn ToPredicateFn> { |
| 1302 |
match predicate { |
| 1303 |
RevsetFilterPredicate::ParentCount(parent_count_range) => { |
| 1304 |
let parent_count_range = parent_count_range.clone(); |
| 1305 |
box_pure_predicate_fn(move |index, pos| { |
| 1306 |
let entry = index.commits().entry_by_pos(pos); |
| 1307 |
Ok(parent_count_range.contains(&entry.num_parents())) |
| 1308 |
}) |
| 1309 |
} |
| 1310 |
RevsetFilterPredicate::Description(expression) => { |
| 1311 |
let matcher = Rc::new(expression.to_matcher()); |
| 1312 |
box_pure_predicate_fn(move |index, pos| { |
| 1313 |
let entry = index.commits().entry_by_pos(pos); |
| 1314 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1315 |
Ok(matcher.is_match(commit.description())) |
| 1316 |
}) |
| 1317 |
} |
| 1318 |
RevsetFilterPredicate::Subject(expression) => { |
| 1319 |
let matcher = Rc::new(expression.to_matcher()); |
| 1320 |
box_pure_predicate_fn(move |index, pos| { |
| 1321 |
let entry = index.commits().entry_by_pos(pos); |
| 1322 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1323 |
Ok(matcher.is_match(commit.description().lines().next().unwrap_or_default())) |
| 1324 |
}) |
| 1325 |
} |
| 1326 |
RevsetFilterPredicate::AuthorName(expression) => { |
| 1327 |
let matcher = Rc::new(expression.to_matcher()); |
| 1328 |
box_pure_predicate_fn(move |index, pos| { |
| 1329 |
let entry = index.commits().entry_by_pos(pos); |
| 1330 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1331 |
Ok(matcher.is_match(&commit.author().name)) |
| 1332 |
}) |
| 1333 |
} |
| 1334 |
RevsetFilterPredicate::AuthorEmail(expression) => { |
| 1335 |
let matcher = Rc::new(expression.to_matcher()); |
| 1336 |
box_pure_predicate_fn(move |index, pos| { |
| 1337 |
let entry = index.commits().entry_by_pos(pos); |
| 1338 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1339 |
Ok(matcher.is_match(&commit.author().email)) |
| 1340 |
}) |
| 1341 |
} |
| 1342 |
RevsetFilterPredicate::AuthorDate(expression) => { |
| 1343 |
let expression = *expression; |
| 1344 |
box_pure_predicate_fn(move |index, pos| { |
| 1345 |
let entry = index.commits().entry_by_pos(pos); |
| 1346 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1347 |
let author_date = &commit.author().timestamp; |
| 1348 |
Ok(expression.matches(author_date)) |
| 1349 |
}) |
| 1350 |
} |
| 1351 |
RevsetFilterPredicate::CommitterName(expression) => { |
| 1352 |
let matcher = Rc::new(expression.to_matcher()); |
| 1353 |
box_pure_predicate_fn(move |index, pos| { |
| 1354 |
let entry = index.commits().entry_by_pos(pos); |
| 1355 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1356 |
Ok(matcher.is_match(&commit.committer().name)) |
| 1357 |
}) |
| 1358 |
} |
| 1359 |
RevsetFilterPredicate::CommitterEmail(expression) => { |
| 1360 |
let matcher = Rc::new(expression.to_matcher()); |
| 1361 |
box_pure_predicate_fn(move |index, pos| { |
| 1362 |
let entry = index.commits().entry_by_pos(pos); |
| 1363 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1364 |
Ok(matcher.is_match(&commit.committer().email)) |
| 1365 |
}) |
| 1366 |
} |
| 1367 |
RevsetFilterPredicate::CommitterDate(expression) => { |
| 1368 |
let expression = *expression; |
| 1369 |
box_pure_predicate_fn(move |index, pos| { |
| 1370 |
let entry = index.commits().entry_by_pos(pos); |
| 1371 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1372 |
let committer_date = &commit.committer().timestamp; |
| 1373 |
Ok(expression.matches(committer_date)) |
| 1374 |
}) |
| 1375 |
} |
| 1376 |
RevsetFilterPredicate::File(expr) => { |
| 1377 |
let matcher: Rc<dyn Matcher> = expr.to_matcher().into(); |
| 1378 |
box_pure_predicate_fn(move |index, pos| { |
| 1379 |
if let Some(mut paths) = index.changed_paths().changed_paths(pos) { |
| 1380 |
return Ok(paths.any(|path| matcher.matches(path))); |
| 1381 |
} |
| 1382 |
let entry = index.commits().entry_by_pos(pos); |
| 1383 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1384 |
Ok(has_diff_from_parent(&store, index, &commit, &*matcher).block_on()?) |
| 1385 |
}) |
| 1386 |
} |
| 1387 |
RevsetFilterPredicate::DiffLines { text, files, side } => { |
| 1388 |
let text_matcher = Rc::new(text.to_matcher()); |
| 1389 |
let files_matcher: Rc<dyn Matcher> = files.to_matcher().into(); |
| 1390 |
let side = *side; |
| 1391 |
box_pure_predicate_fn(move |index, pos| { |
| 1392 |
let narrowed_files_matcher; |
| 1393 |
let files_matcher = if let Some(paths) = index.changed_paths().changed_paths(pos) { |
| 1394 |
let matched_paths = paths |
| 1395 |
.filter(|path| files_matcher.matches(path)) |
| 1396 |
.collect_vec(); |
| 1397 |
if matched_paths.is_empty() { |
| 1398 |
return Ok(false); |
| 1399 |
} |
| 1400 |
narrowed_files_matcher = FilesMatcher::new(matched_paths); |
| 1401 |
&narrowed_files_matcher |
| 1402 |
} else { |
| 1403 |
&*files_matcher |
| 1404 |
}; |
| 1405 |
let entry = index.commits().entry_by_pos(pos); |
| 1406 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1407 |
Ok(matches_diff_from_parent( |
| 1408 |
&store, |
| 1409 |
index, |
| 1410 |
&commit, |
| 1411 |
&text_matcher, |
| 1412 |
files_matcher, |
| 1413 |
side, |
| 1414 |
) |
| 1415 |
.block_on()?) |
| 1416 |
}) |
| 1417 |
} |
| 1418 |
RevsetFilterPredicate::HasConflict => box_pure_predicate_fn(move |index, pos| { |
| 1419 |
let entry = index.commits().entry_by_pos(pos); |
| 1420 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1421 |
Ok(commit.has_conflict()) |
| 1422 |
}), |
| 1423 |
RevsetFilterPredicate::Signed => box_pure_predicate_fn(move |index, pos| { |
| 1424 |
let entry = index.commits().entry_by_pos(pos); |
| 1425 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1426 |
Ok(commit.is_signed()) |
| 1427 |
}), |
| 1428 |
RevsetFilterPredicate::Extension(ext) => { |
| 1429 |
let ext = ext.clone(); |
| 1430 |
box_pure_predicate_fn(move |index, pos| { |
| 1431 |
let entry = index.commits().entry_by_pos(pos); |
| 1432 |
let commit = store.get_commit(&entry.commit_id())?; |
| 1433 |
Ok(ext.matches_commit(&commit)) |
| 1434 |
}) |
| 1435 |
} |
| 1436 |
} |
| 1437 |
} |
| 1438 |
|
| 1439 |
async fn has_diff_from_parent( |
| 1440 |
store: &Arc<Store>, |
| 1441 |
index: &CompositeIndex, |
| 1442 |
commit: &Commit, |
| 1443 |
matcher: &dyn Matcher, |
| 1444 |
) -> BackendResult<bool> { |
| 1445 |
let parents = commit.parents().await?; |
| 1446 |
if let [parent] = parents.as_slice() { |
| 1447 |
// Fast path: no need to load the root tree |
| 1448 |
let unchanged = commit.tree_ids() == parent.tree_ids(); |
| 1449 |
if matcher.visit(RepoPath::root()) == Visit::AllRecursively { |
| 1450 |
return Ok(!unchanged); |
| 1451 |
} else if unchanged { |
| 1452 |
return Ok(false); |
| 1453 |
} |
| 1454 |
} |
| 1455 |
|
| 1456 |
// Conflict resolution is expensive, try that only for matched files. |
| 1457 |
let from_tree = |
| 1458 |
rewrite::merge_commit_trees_no_resolve_without_repo(store, index, &parents).await?; |
| 1459 |
let to_tree = commit.tree(); |
| 1460 |
// TODO: handle copy tracking |
| 1461 |
let mut tree_diff = from_tree.diff_stream(&to_tree, matcher); |
| 1462 |
// TODO: Resolve values concurrently |
| 1463 |
while let Some(entry) = tree_diff.next().await { |
| 1464 |
let mut values = entry.values?; |
| 1465 |
values.before = resolve_file_values(store, &entry.path, values.before).await?; |
| 1466 |
if !values.is_changed() { |
| 1467 |
continue; |
| 1468 |
} |
| 1469 |
return Ok(true); |
| 1470 |
} |
| 1471 |
Ok(false) |
| 1472 |
} |
| 1473 |
|
| 1474 |
async fn matches_diff_from_parent( |
| 1475 |
store: &Arc<Store>, |
| 1476 |
index: &CompositeIndex, |
| 1477 |
commit: &Commit, |
| 1478 |
text_matcher: &StringMatcher, |
| 1479 |
files_matcher: &dyn Matcher, |
| 1480 |
side: DiffMatchSide, |
| 1481 |
) -> BackendResult<bool> { |
| 1482 |
let parents = commit.parents().await?; |
| 1483 |
// Conflict resolution is expensive, try that only for matched files. |
| 1484 |
let from_tree = |
| 1485 |
rewrite::merge_commit_trees_no_resolve_without_repo(store, index, &parents).await?; |
| 1486 |
let to_tree = commit.tree(); |
| 1487 |
// TODO: handle copy tracking |
| 1488 |
let mut tree_diff = from_tree.diff_stream(&to_tree, files_matcher); |
| 1489 |
// TODO: Resolve values concurrently |
| 1490 |
while let Some(entry) = tree_diff.next().await { |
| 1491 |
let mut values = entry.values?; |
| 1492 |
values.before = resolve_file_values(store, &entry.path, values.before).await?; |
| 1493 |
if !values.is_changed() { |
| 1494 |
continue; |
| 1495 |
} |
| 1496 |
let conflict_labels = ConflictLabels::unlabeled(); |
| 1497 |
let left_future = |
| 1498 |
materialize_tree_value(store, &entry.path, values.before, &conflict_labels); |
| 1499 |
let right_future = |
| 1500 |
materialize_tree_value(store, &entry.path, values.after, &conflict_labels); |
| 1501 |
let (left_value, right_value) = futures::try_join!(left_future, right_future)?; |
| 1502 |
let left_contents = to_file_content(&entry.path, left_value).await?; |
| 1503 |
let right_contents = to_file_content(&entry.path, right_value).await?; |
| 1504 |
let merge_options = store.merge_options(); |
| 1505 |
if diff_match_lines( |
| 1506 |
&left_contents, |
| 1507 |
&right_contents, |
| 1508 |
text_matcher, |
| 1509 |
merge_options, |
| 1510 |
side, |
| 1511 |
)? { |
| 1512 |
return Ok(true); |
| 1513 |
} |
| 1514 |
} |
| 1515 |
Ok(false) |
| 1516 |
} |
| 1517 |
|
| 1518 |
fn diff_match_lines( |
| 1519 |
lefts: &Merge<BString>, |
| 1520 |
rights: &Merge<BString>, |
| 1521 |
matcher: &StringMatcher, |
| 1522 |
merge_options: &MergeOptions, |
| 1523 |
side: DiffMatchSide, |
| 1524 |
) -> BackendResult<bool> { |
| 1525 |
// Filter lines prior to comparison. This might produce inferior hunks due |
| 1526 |
// to lack of contexts, but is way faster than full diff. |
| 1527 |
if side == DiffMatchSide::Either |
| 1528 |
&& let (Some(left), Some(right)) = (lefts.as_resolved(), rights.as_resolved()) |
| 1529 |
{ |
| 1530 |
let left_lines = matcher.match_lines(left); |
| 1531 |
let right_lines = matcher.match_lines(right); |
| 1532 |
Ok(left_lines.ne(right_lines)) |
| 1533 |
} else { |
| 1534 |
let lefts: Merge<BString> = lefts.map(|text| matcher.match_lines(text).collect()); |
| 1535 |
let rights: Merge<BString> = rights.map(|text| matcher.match_lines(text).collect()); |
| 1536 |
let lefts = files::merge(&lefts, merge_options); |
| 1537 |
let rights = files::merge(&rights, merge_options); |
| 1538 |
let diff = ContentDiff::by_line(itertools::chain(&lefts, &rights)); |
| 1539 |
let mut hunks = files::conflict_diff_hunks(diff.hunks(), lefts.as_slice().len()); |
| 1540 |
let different = hunks.any(|hunk| { |
| 1541 |
hunk.kind == DiffHunkKind::Different |
| 1542 |
&& match side { |
| 1543 |
DiffMatchSide::Either => true, |
| 1544 |
DiffMatchSide::Left => hunk.lefts.iter().any(|lines| !lines.is_empty()), |
| 1545 |
DiffMatchSide::Right => hunk.rights.iter().any(|lines| !lines.is_empty()), |
| 1546 |
} |
| 1547 |
}); |
| 1548 |
Ok(different) |
| 1549 |
} |
| 1550 |
} |
| 1551 |
|
| 1552 |
async fn to_file_content( |
| 1553 |
path: &RepoPath, |
| 1554 |
value: MaterializedTreeValue, |
| 1555 |
) -> BackendResult<Merge<BString>> { |
| 1556 |
let empty = || Merge::resolved(BString::default()); |
| 1557 |
match value { |
| 1558 |
MaterializedTreeValue::Absent => Ok(empty()), |
| 1559 |
MaterializedTreeValue::AccessDenied(_) => Ok(empty()), |
| 1560 |
MaterializedTreeValue::File(mut file) => { |
| 1561 |
Ok(Merge::resolved(file.read_all(path).await?.into())) |
| 1562 |
} |
| 1563 |
MaterializedTreeValue::Symlink { id: _, target } => Ok(Merge::resolved(target.into())), |
| 1564 |
MaterializedTreeValue::GitSubmodule(_) => Ok(empty()), |
| 1565 |
MaterializedTreeValue::FileConflict(file) => Ok(file.contents), |
| 1566 |
MaterializedTreeValue::OtherConflict { .. } => Ok(empty()), |
| 1567 |
MaterializedTreeValue::Tree(id) => { |
| 1568 |
panic!("Unexpected tree with id {id:?} in diff at path {path:?}"); |
| 1569 |
} |
| 1570 |
} |
| 1571 |
} |
| 1572 |
|
| 1573 |
#[cfg(test)] |
| 1574 |
#[rustversion::attr( |
| 1575 |
since(1.89), |
| 1576 |
expect(clippy::cloned_ref_to_slice_refs, reason = "makes tests more readable") |
| 1577 |
)] |
| 1578 |
mod tests { |
| 1579 |
use indoc::indoc; |
| 1580 |
|
| 1581 |
use super::*; |
| 1582 |
use crate::default_index::DefaultMutableIndex; |
| 1583 |
use crate::default_index::readonly::FieldLengths; |
| 1584 |
use crate::files::FileMergeHunkLevel; |
| 1585 |
use crate::merge::SameChange; |
| 1586 |
use crate::str_util::StringPattern; |
| 1587 |
use crate::tests::TestResult; |
| 1588 |
|
| 1589 |
const TEST_FIELD_LENGTHS: FieldLengths = FieldLengths { |
| 1590 |
commit_id: 3, |
| 1591 |
change_id: 16, |
| 1592 |
}; |
| 1593 |
|
| 1594 |
/// Generator of unique 16-byte ChangeId excluding root id |
| 1595 |
fn change_id_generator() -> impl FnMut() -> ChangeId { |
| 1596 |
let mut iter = (1_u128..).map(|n| ChangeId::new(n.to_le_bytes().into())); |
| 1597 |
move || iter.next().unwrap() |
| 1598 |
} |
| 1599 |
|
| 1600 |
fn try_collect_vec<T, E>(iter: impl IntoIterator<Item = Result<T, E>>) -> Result<Vec<T>, E> { |
| 1601 |
iter.into_iter().collect() |
| 1602 |
} |
| 1603 |
|
| 1604 |
#[test] |
| 1605 |
fn test_revset_combinator() -> TestResult { |
| 1606 |
let mut new_change_id = change_id_generator(); |
| 1607 |
let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS); |
| 1608 |
let id_0 = CommitId::from_hex("000000"); |
| 1609 |
let id_1 = CommitId::from_hex("111111"); |
| 1610 |
let id_2 = CommitId::from_hex("222222"); |
| 1611 |
let id_3 = CommitId::from_hex("333333"); |
| 1612 |
let id_4 = CommitId::from_hex("444444"); |
| 1613 |
index.add_commit_data(id_0.clone(), new_change_id(), &[]); |
| 1614 |
index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]); |
| 1615 |
index.add_commit_data(id_2.clone(), new_change_id(), &[id_1.clone()]); |
| 1616 |
index.add_commit_data(id_3.clone(), new_change_id(), &[id_2.clone()]); |
| 1617 |
index.add_commit_data(id_4.clone(), new_change_id(), &[id_3.clone()]); |
| 1618 |
|
| 1619 |
let index = index.as_composite(); |
| 1620 |
let get_pos = |id: &CommitId| index.commits().commit_id_to_pos(id).unwrap(); |
| 1621 |
let make_positions = |ids: &[&CommitId]| ids.iter().copied().map(get_pos).collect_vec(); |
| 1622 |
let make_set = |ids: &[&CommitId]| -> Box<dyn InternalRevset> { |
| 1623 |
let positions = make_positions(ids); |
| 1624 |
Box::new(EagerRevset { positions }) |
| 1625 |
}; |
| 1626 |
|
| 1627 |
let set = make_set(&[&id_4, &id_3, &id_2, &id_0]); |
| 1628 |
let mut p = set.to_predicate_fn(); |
| 1629 |
assert!(p(index, get_pos(&id_4))?); |
| 1630 |
assert!(p(index, get_pos(&id_3))?); |
| 1631 |
assert!(p(index, get_pos(&id_2))?); |
| 1632 |
assert!(!p(index, get_pos(&id_1))?); |
| 1633 |
assert!(p(index, get_pos(&id_0))?); |
| 1634 |
// Uninteresting entries can be skipped |
| 1635 |
let mut p = set.to_predicate_fn(); |
| 1636 |
assert!(p(index, get_pos(&id_3))?); |
| 1637 |
assert!(!p(index, get_pos(&id_1))?); |
| 1638 |
assert!(p(index, get_pos(&id_0))?); |
| 1639 |
|
| 1640 |
let set = FilterRevset { |
| 1641 |
candidates: make_set(&[&id_4, &id_2, &id_0]), |
| 1642 |
predicate: as_pure_predicate_fn(|index, pos| { |
| 1643 |
Ok(index.commits().entry_by_pos(pos).commit_id() != id_4) |
| 1644 |
}), |
| 1645 |
}; |
| 1646 |
assert_eq!( |
| 1647 |
try_collect_vec(set.positions().attach(index))?, |
| 1648 |
make_positions(&[&id_2, &id_0]) |
| 1649 |
); |
| 1650 |
let mut p = set.to_predicate_fn(); |
| 1651 |
assert!(!p(index, get_pos(&id_4))?); |
| 1652 |
assert!(!p(index, get_pos(&id_3))?); |
| 1653 |
assert!(p(index, get_pos(&id_2))?); |
| 1654 |
assert!(!p(index, get_pos(&id_1))?); |
| 1655 |
assert!(p(index, get_pos(&id_0))?); |
| 1656 |
|
| 1657 |
// Intersection by FilterRevset |
| 1658 |
let set = FilterRevset { |
| 1659 |
candidates: make_set(&[&id_4, &id_2, &id_0]), |
| 1660 |
predicate: make_set(&[&id_3, &id_2, &id_1]), |
| 1661 |
}; |
| 1662 |
assert_eq!( |
| 1663 |
try_collect_vec(set.positions().attach(index))?, |
| 1664 |
make_positions(&[&id_2]) |
| 1665 |
); |
| 1666 |
let mut p = set.to_predicate_fn(); |
| 1667 |
assert!(!p(index, get_pos(&id_4))?); |
| 1668 |
assert!(!p(index, get_pos(&id_3))?); |
| 1669 |
assert!(p(index, get_pos(&id_2))?); |
| 1670 |
assert!(!p(index, get_pos(&id_1))?); |
| 1671 |
assert!(!p(index, get_pos(&id_0))?); |
| 1672 |
|
| 1673 |
let set = UnionRevset { |
| 1674 |
set1: make_set(&[&id_4, &id_2]), |
| 1675 |
set2: make_set(&[&id_3, &id_2, &id_1]), |
| 1676 |
}; |
| 1677 |
assert_eq!( |
| 1678 |
try_collect_vec(set.positions().attach(index))?, |
| 1679 |
make_positions(&[&id_4, &id_3, &id_2, &id_1]) |
| 1680 |
); |
| 1681 |
let mut p = set.to_predicate_fn(); |
| 1682 |
assert!(p(index, get_pos(&id_4))?); |
| 1683 |
assert!(p(index, get_pos(&id_3))?); |
| 1684 |
assert!(p(index, get_pos(&id_2))?); |
| 1685 |
assert!(p(index, get_pos(&id_1))?); |
| 1686 |
assert!(!p(index, get_pos(&id_0))?); |
| 1687 |
|
| 1688 |
let set = IntersectionRevset { |
| 1689 |
set1: make_set(&[&id_4, &id_2, &id_0]), |
| 1690 |
set2: make_set(&[&id_3, &id_2, &id_1]), |
| 1691 |
}; |
| 1692 |
assert_eq!( |
| 1693 |
try_collect_vec(set.positions().attach(index))?, |
| 1694 |
make_positions(&[&id_2]) |
| 1695 |
); |
| 1696 |
let mut p = set.to_predicate_fn(); |
| 1697 |
assert!(!p(index, get_pos(&id_4))?); |
| 1698 |
assert!(!p(index, get_pos(&id_3))?); |
| 1699 |
assert!(p(index, get_pos(&id_2))?); |
| 1700 |
assert!(!p(index, get_pos(&id_1))?); |
| 1701 |
assert!(!p(index, get_pos(&id_0))?); |
| 1702 |
|
| 1703 |
let set = DifferenceRevset { |
| 1704 |
set1: make_set(&[&id_4, &id_2, &id_0]), |
| 1705 |
set2: make_set(&[&id_3, &id_2, &id_1]), |
| 1706 |
}; |
| 1707 |
assert_eq!( |
| 1708 |
try_collect_vec(set.positions().attach(index))?, |
| 1709 |
make_positions(&[&id_4, &id_0]) |
| 1710 |
); |
| 1711 |
let mut p = set.to_predicate_fn(); |
| 1712 |
assert!(p(index, get_pos(&id_4))?); |
| 1713 |
assert!(!p(index, get_pos(&id_3))?); |
| 1714 |
assert!(!p(index, get_pos(&id_2))?); |
| 1715 |
assert!(!p(index, get_pos(&id_1))?); |
| 1716 |
assert!(p(index, get_pos(&id_0))?); |
| 1717 |
Ok(()) |
| 1718 |
} |
| 1719 |
|
| 1720 |
#[test] |
| 1721 |
fn test_revset_combinator_error_propagation() -> TestResult { |
| 1722 |
let mut new_change_id = change_id_generator(); |
| 1723 |
let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS); |
| 1724 |
let id_0 = CommitId::from_hex("000000"); |
| 1725 |
let id_1 = CommitId::from_hex("111111"); |
| 1726 |
let id_2 = CommitId::from_hex("222222"); |
| 1727 |
index.add_commit_data(id_0.clone(), new_change_id(), &[]); |
| 1728 |
index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]); |
| 1729 |
index.add_commit_data(id_2.clone(), new_change_id(), &[id_1.clone()]); |
| 1730 |
|
| 1731 |
let index = index.as_composite(); |
| 1732 |
let get_pos = |id: &CommitId| index.commits().commit_id_to_pos(id).unwrap(); |
| 1733 |
let make_positions = |ids: &[&CommitId]| ids.iter().copied().map(get_pos).collect_vec(); |
| 1734 |
let make_good_set = |ids: &[&CommitId]| -> Box<dyn InternalRevset> { |
| 1735 |
let positions = make_positions(ids); |
| 1736 |
Box::new(EagerRevset { positions }) |
| 1737 |
}; |
| 1738 |
let make_bad_set = |ids: &[&CommitId], bad_id: &CommitId| -> Box<dyn InternalRevset> { |
| 1739 |
let positions = make_positions(ids); |
| 1740 |
let bad_id = bad_id.clone(); |
| 1741 |
Box::new(FilterRevset { |
| 1742 |
candidates: EagerRevset { positions }, |
| 1743 |
predicate: as_pure_predicate_fn(move |index, pos| { |
| 1744 |
if index.commits().entry_by_pos(pos).commit_id() == bad_id { |
| 1745 |
Err(RevsetEvaluationError::Other("bad".into())) |
| 1746 |
} else { |
| 1747 |
Ok(true) |
| 1748 |
} |
| 1749 |
}), |
| 1750 |
}) |
| 1751 |
}; |
| 1752 |
|
| 1753 |
// Error from filter predicate |
| 1754 |
let set = make_bad_set(&[&id_2, &id_1, &id_0], &id_1); |
| 1755 |
assert_eq!( |
| 1756 |
try_collect_vec(set.positions().attach(index).take(1))?, |
| 1757 |
make_positions(&[&id_2]) |
| 1758 |
); |
| 1759 |
assert!(try_collect_vec(set.positions().attach(index).take(2)).is_err()); |
| 1760 |
let mut p = set.to_predicate_fn(); |
| 1761 |
assert!(p(index, get_pos(&id_2))?); |
| 1762 |
assert!(p(index, get_pos(&id_1)).is_err()); |
| 1763 |
|
| 1764 |
// Error from filter candidates |
| 1765 |
let set = FilterRevset { |
| 1766 |
candidates: make_bad_set(&[&id_2, &id_1, &id_0], &id_1), |
| 1767 |
predicate: as_pure_predicate_fn(|_, _| Ok(true)), |
| 1768 |
}; |
| 1769 |
assert_eq!( |
| 1770 |
try_collect_vec(set.positions().attach(index).take(1))?, |
| 1771 |
make_positions(&[&id_2]) |
| 1772 |
); |
| 1773 |
assert!(try_collect_vec(set.positions().attach(index).take(2)).is_err()); |
| 1774 |
let mut p = set.to_predicate_fn(); |
| 1775 |
assert!(p(index, get_pos(&id_2))?); |
| 1776 |
assert!(p(index, get_pos(&id_1)).is_err()); |
| 1777 |
|
| 1778 |
// Error from left side of union, immediately |
| 1779 |
let set = UnionRevset { |
| 1780 |
set1: make_bad_set(&[&id_1], &id_1), |
| 1781 |
set2: make_good_set(&[&id_2, &id_1]), |
| 1782 |
}; |
| 1783 |
assert!(try_collect_vec(set.positions().attach(index).take(1)).is_err()); |
| 1784 |
let mut p = set.to_predicate_fn(); |
| 1785 |
assert!(p(index, get_pos(&id_2))?); // works because bad id isn't visited |
| 1786 |
assert!(p(index, get_pos(&id_1)).is_err()); |
| 1787 |
|
| 1788 |
// Error from right side of union, lazily |
| 1789 |
let set = UnionRevset { |
| 1790 |
set1: make_good_set(&[&id_2, &id_1]), |
| 1791 |
set2: make_bad_set(&[&id_1, &id_0], &id_0), |
| 1792 |
}; |
| 1793 |
assert_eq!( |
| 1794 |
try_collect_vec(set.positions().attach(index).take(2))?, |
| 1795 |
make_positions(&[&id_2, &id_1]) |
| 1796 |
); |
| 1797 |
assert!(try_collect_vec(set.positions().attach(index).take(3)).is_err()); |
| 1798 |
let mut p = set.to_predicate_fn(); |
| 1799 |
assert!(p(index, get_pos(&id_2))?); |
| 1800 |
assert!(p(index, get_pos(&id_1))?); |
| 1801 |
assert!(p(index, get_pos(&id_0)).is_err()); |
| 1802 |
|
| 1803 |
// Error from left side of intersection, immediately |
| 1804 |
let set = IntersectionRevset { |
| 1805 |
set1: make_bad_set(&[&id_1], &id_1), |
| 1806 |
set2: make_good_set(&[&id_2, &id_1]), |
| 1807 |
}; |
| 1808 |
assert!(try_collect_vec(set.positions().attach(index).take(1)).is_err()); |
| 1809 |
let mut p = set.to_predicate_fn(); |
| 1810 |
assert!(!p(index, get_pos(&id_2))?); |
| 1811 |
assert!(p(index, get_pos(&id_1)).is_err()); |
| 1812 |
|
| 1813 |
// Error from right side of intersection, lazily |
| 1814 |
let set = IntersectionRevset { |
| 1815 |
set1: make_good_set(&[&id_2, &id_1, &id_0]), |
| 1816 |
set2: make_bad_set(&[&id_1, &id_0], &id_0), |
| 1817 |
}; |
| 1818 |
assert_eq!( |
| 1819 |
try_collect_vec(set.positions().attach(index).take(1))?, |
| 1820 |
make_positions(&[&id_1]) |
| 1821 |
); |
| 1822 |
assert!(try_collect_vec(set.positions().attach(index).take(2)).is_err()); |
| 1823 |
let mut p = set.to_predicate_fn(); |
| 1824 |
assert!(!p(index, get_pos(&id_2))?); |
| 1825 |
assert!(p(index, get_pos(&id_1))?); |
| 1826 |
assert!(p(index, get_pos(&id_0)).is_err()); |
| 1827 |
|
| 1828 |
// Error from left side of difference, immediately |
| 1829 |
let set = DifferenceRevset { |
| 1830 |
set1: make_bad_set(&[&id_1], &id_1), |
| 1831 |
set2: make_good_set(&[&id_2, &id_1]), |
| 1832 |
}; |
| 1833 |
assert!(try_collect_vec(set.positions().attach(index).take(1)).is_err()); |
| 1834 |
let mut p = set.to_predicate_fn(); |
| 1835 |
assert!(!p(index, get_pos(&id_2))?); |
| 1836 |
assert!(p(index, get_pos(&id_1)).is_err()); |
| 1837 |
|
| 1838 |
// Error from right side of difference, lazily |
| 1839 |
let set = DifferenceRevset { |
| 1840 |
set1: make_good_set(&[&id_2, &id_1, &id_0]), |
| 1841 |
set2: make_bad_set(&[&id_1, &id_0], &id_0), |
| 1842 |
}; |
| 1843 |
assert_eq!( |
| 1844 |
try_collect_vec(set.positions().attach(index).take(1))?, |
| 1845 |
make_positions(&[&id_2]) |
| 1846 |
); |
| 1847 |
assert!(try_collect_vec(set.positions().attach(index).take(2)).is_err()); |
| 1848 |
let mut p = set.to_predicate_fn(); |
| 1849 |
assert!(p(index, get_pos(&id_2))?); |
| 1850 |
assert!(!p(index, get_pos(&id_1))?); |
| 1851 |
assert!(p(index, get_pos(&id_0)).is_err()); |
| 1852 |
Ok(()) |
| 1853 |
} |
| 1854 |
|
| 1855 |
#[test] |
| 1856 |
fn test_positions_accumulator() -> TestResult { |
| 1857 |
let mut new_change_id = change_id_generator(); |
| 1858 |
let mut index = DefaultMutableIndex::full(TEST_FIELD_LENGTHS); |
| 1859 |
let id_0 = CommitId::from_hex("000000"); |
| 1860 |
let id_1 = CommitId::from_hex("111111"); |
| 1861 |
let id_2 = CommitId::from_hex("222222"); |
| 1862 |
let id_3 = CommitId::from_hex("333333"); |
| 1863 |
let id_4 = CommitId::from_hex("444444"); |
| 1864 |
index.add_commit_data(id_0.clone(), new_change_id(), &[]); |
| 1865 |
index.add_commit_data(id_1.clone(), new_change_id(), &[id_0.clone()]); |
| 1866 |
index.add_commit_data(id_2.clone(), new_change_id(), &[id_1.clone()]); |
| 1867 |
index.add_commit_data(id_3.clone(), new_change_id(), &[id_2.clone()]); |
| 1868 |
index.add_commit_data(id_4.clone(), new_change_id(), &[id_3.clone()]); |
| 1869 |
|
| 1870 |
let index = index.as_composite(); |
| 1871 |
let get_pos = |id: &CommitId| index.commits().commit_id_to_pos(id).unwrap(); |
| 1872 |
let make_positions = |ids: &[&CommitId]| ids.iter().copied().map(get_pos).collect_vec(); |
| 1873 |
let make_set = |ids: &[&CommitId]| -> Box<dyn InternalRevset> { |
| 1874 |
let positions = make_positions(ids); |
| 1875 |
Box::new(EagerRevset { positions }) |
| 1876 |
}; |
| 1877 |
|
| 1878 |
let full_set = make_set(&[&id_4, &id_3, &id_2, &id_1, &id_0]); |
| 1879 |
|
| 1880 |
// Consumes entries incrementally |
| 1881 |
let positions_accum = PositionsAccumulator::new(index, full_set.positions()); |
| 1882 |
|
| 1883 |
assert!(positions_accum.contains(&id_3)?); |
| 1884 |
assert_eq!(positions_accum.consumed_len(), 2); |
| 1885 |
|
| 1886 |
assert!(positions_accum.contains(&id_0)?); |
| 1887 |
assert_eq!(positions_accum.consumed_len(), 5); |
| 1888 |
|
| 1889 |
assert!(positions_accum.contains(&id_3)?); |
| 1890 |
assert_eq!(positions_accum.consumed_len(), 5); |
| 1891 |
|
| 1892 |
// Does not consume positions for unknown commits |
| 1893 |
let positions_accum = PositionsAccumulator::new(index, full_set.positions()); |
| 1894 |
|
| 1895 |
assert!(!positions_accum.contains(&CommitId::from_hex("999999"))?); |
| 1896 |
assert_eq!(positions_accum.consumed_len(), 0); |
| 1897 |
|
| 1898 |
// Does not consume without necessity |
| 1899 |
let set = make_set(&[&id_3, &id_2, &id_1]); |
| 1900 |
let positions_accum = PositionsAccumulator::new(index, set.positions()); |
| 1901 |
|
| 1902 |
assert!(!positions_accum.contains(&id_4)?); |
| 1903 |
assert_eq!(positions_accum.consumed_len(), 1); |
| 1904 |
|
| 1905 |
assert!(positions_accum.contains(&id_3)?); |
| 1906 |
assert_eq!(positions_accum.consumed_len(), 1); |
| 1907 |
|
| 1908 |
assert!(!positions_accum.contains(&id_0)?); |
| 1909 |
assert_eq!(positions_accum.consumed_len(), 3); |
| 1910 |
|
| 1911 |
assert!(positions_accum.contains(&id_1)?); |
| 1912 |
Ok(()) |
| 1913 |
} |
| 1914 |
|
| 1915 |
fn diff_match_lines_samples() -> (Merge<BString>, Merge<BString>) { |
| 1916 |
// left2 left1 base right1 right2 |
| 1917 |
// ---------- ---------- ---------- ----------- ----------- |
| 1918 |
// "left 1.1" "line 1" "line 1" "line 1" "line 1" |
| 1919 |
// "line 2" "line 2" "line 2" "line 2" "line 2" |
| 1920 |
// "left 3.1" "left 3.1" "line 3" "right 3.1" "right 3.1" |
| 1921 |
// "left 3.2" "left 3.2" |
| 1922 |
// "left 3.3" |
| 1923 |
// "line 4" "line 4" "line 4" "line 4" "line 4" |
| 1924 |
// "line 5" "line 5" "line 5" |
| 1925 |
let base = indoc! {" |
| 1926 |
line 1 |
| 1927 |
line 2 |
| 1928 |
line 3 |
| 1929 |
line 4 |
| 1930 |
"}; |
| 1931 |
let left1 = indoc! {" |
| 1932 |
line 1 |
| 1933 |
line 2 |
| 1934 |
left 3.1 |
| 1935 |
left 3.2 |
| 1936 |
line 4 |
| 1937 |
line 5 |
| 1938 |
"}; |
| 1939 |
let left2 = indoc! {" |
| 1940 |
left 1.1 |
| 1941 |
line 2 |
| 1942 |
left 3.1 |
| 1943 |
left 3.2 |
| 1944 |
left 3.3 |
| 1945 |
line 4 |
| 1946 |
line 5 |
| 1947 |
"}; |
| 1948 |
let right1 = indoc! {" |
| 1949 |
line 1 |
| 1950 |
line 2 |
| 1951 |
right 3.1 |
| 1952 |
line 4 |
| 1953 |
line 5 |
| 1954 |
"}; |
| 1955 |
let right2 = indoc! {" |
| 1956 |
line 1 |
| 1957 |
line 2 |
| 1958 |
right 3.1 |
| 1959 |
line 4 |
| 1960 |
"}; |
| 1961 |
|
| 1962 |
let conflict1 = Merge::from_vec([left1, base, right1].map(BString::from).to_vec()); |
| 1963 |
let conflict2 = Merge::from_vec([left2, base, right2].map(BString::from).to_vec()); |
| 1964 |
(conflict1, conflict2) |
| 1965 |
} |
| 1966 |
|
| 1967 |
#[test] |
| 1968 |
fn test_diff_match_lines_between_resolved() { |
| 1969 |
let (conflict1, conflict2) = diff_match_lines_samples(); |
| 1970 |
let left1 = Merge::resolved(conflict1.first().clone()); |
| 1971 |
let left2 = Merge::resolved(conflict2.first().clone()); |
| 1972 |
let options = MergeOptions { |
| 1973 |
hunk_level: FileMergeHunkLevel::Line, |
| 1974 |
same_change: SameChange::Accept, |
| 1975 |
}; |
| 1976 |
let diff = |needle: &str| { |
| 1977 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 1978 |
diff_match_lines(&left1, &left2, &matcher, &options, DiffMatchSide::Either).unwrap() |
| 1979 |
}; |
| 1980 |
let diff_left1 = |needle: &str| { |
| 1981 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 1982 |
diff_match_lines(&left1, &left2, &matcher, &options, DiffMatchSide::Left).unwrap() |
| 1983 |
}; |
| 1984 |
let diff_left2 = |needle: &str| { |
| 1985 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 1986 |
diff_match_lines(&left1, &left2, &matcher, &options, DiffMatchSide::Right).unwrap() |
| 1987 |
}; |
| 1988 |
|
| 1989 |
assert!(diff("")); |
| 1990 |
assert!(diff_left1("")); |
| 1991 |
assert!(diff_left2("")); |
| 1992 |
|
| 1993 |
assert!(!diff("no match")); |
| 1994 |
assert!(!diff_left1("no_match")); |
| 1995 |
assert!(!diff_left2("no_match")); |
| 1996 |
|
| 1997 |
assert!(diff("line ")); |
| 1998 |
assert!(diff_left1("line ")); |
| 1999 |
assert!(!diff_left2("line ")); |
| 2000 |
|
| 2001 |
assert!(diff(" 1")); |
| 2002 |
assert!(diff_left1(" 1")); |
| 2003 |
assert!(diff_left2(" 1")); |
| 2004 |
|
| 2005 |
assert!(!diff(" 2")); |
| 2006 |
assert!(!diff_left1(" 2")); |
| 2007 |
assert!(!diff_left2(" 2")); |
| 2008 |
|
| 2009 |
assert!(diff(" 3")); |
| 2010 |
assert!(!diff_left1(" 3")); |
| 2011 |
assert!(diff_left2(" 3")); |
| 2012 |
|
| 2013 |
assert!(!diff(" 3.1")); |
| 2014 |
assert!(!diff_left1(" 3.1")); |
| 2015 |
assert!(!diff_left2(" 3.1")); |
| 2016 |
|
| 2017 |
assert!(!diff(" 3.2")); |
| 2018 |
assert!(!diff_left1(" 3.2")); |
| 2019 |
assert!(!diff_left2(" 3.2")); |
| 2020 |
|
| 2021 |
assert!(diff(" 3.3")); |
| 2022 |
assert!(!diff_left1(" 3.3")); |
| 2023 |
assert!(diff_left2(" 3.3")); |
| 2024 |
|
| 2025 |
assert!(!diff(" 4")); |
| 2026 |
assert!(!diff_left1(" 4")); |
| 2027 |
assert!(!diff_left2(" 4")); |
| 2028 |
|
| 2029 |
assert!(!diff(" 5")); |
| 2030 |
assert!(!diff_left1(" 5")); |
| 2031 |
assert!(!diff_left2(" 5")); |
| 2032 |
} |
| 2033 |
|
| 2034 |
#[test] |
| 2035 |
fn test_diff_match_lines_between_conflicts() { |
| 2036 |
let (conflict1, conflict2) = diff_match_lines_samples(); |
| 2037 |
let options = MergeOptions { |
| 2038 |
hunk_level: FileMergeHunkLevel::Line, |
| 2039 |
same_change: SameChange::Accept, |
| 2040 |
}; |
| 2041 |
let diff = |needle: &str| { |
| 2042 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2043 |
diff_match_lines( |
| 2044 |
&conflict1, |
| 2045 |
&conflict2, |
| 2046 |
&matcher, |
| 2047 |
&options, |
| 2048 |
DiffMatchSide::Either, |
| 2049 |
) |
| 2050 |
.unwrap() |
| 2051 |
}; |
| 2052 |
let diff_c1 = |needle: &str| { |
| 2053 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2054 |
diff_match_lines( |
| 2055 |
&conflict1, |
| 2056 |
&conflict2, |
| 2057 |
&matcher, |
| 2058 |
&options, |
| 2059 |
DiffMatchSide::Left, |
| 2060 |
) |
| 2061 |
.unwrap() |
| 2062 |
}; |
| 2063 |
let diff_c2 = |needle: &str| { |
| 2064 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2065 |
diff_match_lines( |
| 2066 |
&conflict1, |
| 2067 |
&conflict2, |
| 2068 |
&matcher, |
| 2069 |
&options, |
| 2070 |
DiffMatchSide::Right, |
| 2071 |
) |
| 2072 |
.unwrap() |
| 2073 |
}; |
| 2074 |
|
| 2075 |
assert!(diff("")); |
| 2076 |
assert!(diff_c1("")); |
| 2077 |
assert!(diff_c2("")); |
| 2078 |
|
| 2079 |
assert!(!diff("no match")); |
| 2080 |
assert!(!diff_c1("no_match")); |
| 2081 |
assert!(!diff_c2("no_match")); |
| 2082 |
|
| 2083 |
assert!(diff("line ")); |
| 2084 |
assert!(diff_c1("line ")); |
| 2085 |
assert!(!diff_c2("line ")); |
| 2086 |
|
| 2087 |
assert!(diff(" 1")); |
| 2088 |
assert!(diff_c1(" 1")); |
| 2089 |
assert!(diff_c2(" 1")); |
| 2090 |
|
| 2091 |
assert!(!diff(" 2")); |
| 2092 |
assert!(!diff_c1(" 2")); |
| 2093 |
assert!(!diff_c2(" 2")); |
| 2094 |
|
| 2095 |
assert!(diff(" 3")); |
| 2096 |
assert!(diff_c1(" 3")); |
| 2097 |
assert!(diff_c2(" 3")); |
| 2098 |
|
| 2099 |
// " 3.1" and " 3.2" could be considered different because the hunk |
| 2100 |
// includes a changed line " 3.3". However, we filters out unmatched |
| 2101 |
// lines first, therefore the changed line is omitted from the hunk. |
| 2102 |
assert!(!diff(" 3.1")); |
| 2103 |
assert!(!diff_c1(" 3.1")); |
| 2104 |
assert!(!diff_c2(" 3.1")); |
| 2105 |
|
| 2106 |
assert!(!diff(" 3.2")); |
| 2107 |
assert!(!diff_c1(" 3.2")); |
| 2108 |
assert!(!diff_c2(" 3.2")); |
| 2109 |
|
| 2110 |
assert!(diff(" 3.3")); |
| 2111 |
assert!(!diff_c1(" 3.3")); |
| 2112 |
assert!(diff_c2(" 3.3")); |
| 2113 |
|
| 2114 |
assert!(!diff(" 4")); |
| 2115 |
assert!(!diff_c1(" 4")); |
| 2116 |
assert!(!diff_c2(" 4")); |
| 2117 |
|
| 2118 |
assert!(!diff(" 5")); // per A-B+A=A rule |
| 2119 |
assert!(!diff_c1(" 5")); |
| 2120 |
assert!(!diff_c2(" 5")); |
| 2121 |
} |
| 2122 |
|
| 2123 |
#[test] |
| 2124 |
fn test_diff_match_lines_between_resolved_and_conflict() { |
| 2125 |
let (_conflict1, conflict2) = diff_match_lines_samples(); |
| 2126 |
let base = Merge::resolved(conflict2.get_remove(0).unwrap().clone()); |
| 2127 |
let options = MergeOptions { |
| 2128 |
hunk_level: FileMergeHunkLevel::Line, |
| 2129 |
same_change: SameChange::Accept, |
| 2130 |
}; |
| 2131 |
let diff = |needle: &str| { |
| 2132 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2133 |
diff_match_lines(&base, &conflict2, &matcher, &options, DiffMatchSide::Either).unwrap() |
| 2134 |
}; |
| 2135 |
let diff_base = |needle: &str| { |
| 2136 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2137 |
diff_match_lines(&base, &conflict2, &matcher, &options, DiffMatchSide::Left).unwrap() |
| 2138 |
}; |
| 2139 |
let diff_c2 = |needle: &str| { |
| 2140 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2141 |
diff_match_lines(&base, &conflict2, &matcher, &options, DiffMatchSide::Right).unwrap() |
| 2142 |
}; |
| 2143 |
|
| 2144 |
assert!(diff("")); |
| 2145 |
assert!(diff_base("")); |
| 2146 |
assert!(diff_c2("")); |
| 2147 |
|
| 2148 |
assert!(!diff("no match")); |
| 2149 |
assert!(!diff_base("no match")); |
| 2150 |
assert!(!diff_c2("no match")); |
| 2151 |
|
| 2152 |
assert!(diff("line ")); |
| 2153 |
assert!(diff_base("line ")); |
| 2154 |
assert!(diff_c2("line ")); |
| 2155 |
|
| 2156 |
assert!(diff(" 1")); |
| 2157 |
assert!(diff_base(" 1")); |
| 2158 |
assert!(diff_c2(" 1")); |
| 2159 |
|
| 2160 |
assert!(!diff(" 2")); |
| 2161 |
assert!(!diff_base(" 2")); |
| 2162 |
assert!(!diff_c2(" 2")); |
| 2163 |
|
| 2164 |
assert!(diff(" 3")); |
| 2165 |
assert!(diff_base(" 3")); |
| 2166 |
assert!(diff_c2(" 3")); |
| 2167 |
|
| 2168 |
assert!(diff(" 3.1")); |
| 2169 |
assert!(!diff_base(" 3.1")); |
| 2170 |
assert!(diff_c2(" 3.1")); |
| 2171 |
|
| 2172 |
assert!(diff(" 3.2")); |
| 2173 |
assert!(!diff_base(" 3.2")); |
| 2174 |
assert!(diff_c2(" 3.2")); |
| 2175 |
|
| 2176 |
assert!(!diff(" 4")); |
| 2177 |
assert!(!diff_base(" 4")); |
| 2178 |
assert!(!diff_c2(" 4")); |
| 2179 |
|
| 2180 |
assert!(diff(" 5")); |
| 2181 |
assert!(!diff_base(" 5")); |
| 2182 |
assert!(diff_c2(" 5")); |
| 2183 |
} |
| 2184 |
|
| 2185 |
#[test] |
| 2186 |
fn test_diff_match_lines_with_repeated_lines() { |
| 2187 |
let left = Merge::resolved(BString::from(indoc! {" |
| 2188 |
twice_left |
| 2189 |
twice_left |
| 2190 |
twice_both |
| 2191 |
twice_both |
| 2192 |
twice_right |
| 2193 |
"})); |
| 2194 |
let right = Merge::resolved(BString::from(indoc! {" |
| 2195 |
twice_left |
| 2196 |
twice_both |
| 2197 |
twice_both |
| 2198 |
twice_right |
| 2199 |
twice_right |
| 2200 |
"})); |
| 2201 |
let options = MergeOptions { |
| 2202 |
hunk_level: FileMergeHunkLevel::Line, |
| 2203 |
same_change: SameChange::Accept, |
| 2204 |
}; |
| 2205 |
let diff = |needle: &str| { |
| 2206 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2207 |
diff_match_lines(&left, &right, &matcher, &options, DiffMatchSide::Either).unwrap() |
| 2208 |
}; |
| 2209 |
let diff_left = |needle: &str| { |
| 2210 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2211 |
diff_match_lines(&left, &right, &matcher, &options, DiffMatchSide::Left).unwrap() |
| 2212 |
}; |
| 2213 |
let diff_right = |needle: &str| { |
| 2214 |
let matcher = StringPattern::substring(needle).to_matcher(); |
| 2215 |
diff_match_lines(&left, &right, &matcher, &options, DiffMatchSide::Right).unwrap() |
| 2216 |
}; |
| 2217 |
|
| 2218 |
assert!(diff("twice_left")); |
| 2219 |
assert!(diff_left("twice_left")); |
| 2220 |
assert!(!diff_right("twice_left")); |
| 2221 |
|
| 2222 |
assert!(!diff("twice_both")); |
| 2223 |
assert!(!diff_left("twice_both")); |
| 2224 |
assert!(!diff_right("twice_both")); |
| 2225 |
|
| 2226 |
assert!(diff("twice_right")); |
| 2227 |
assert!(!diff_left("twice_right")); |
| 2228 |
assert!(diff_right("twice_right")); |
| 2229 |
} |
| 2230 |
} |