jj_lib/
bisect.rs

1// Copyright 2025 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//! Bisect a range of commits.
16
17use std::collections::HashSet;
18use std::sync::Arc;
19
20use itertools::Itertools as _;
21use thiserror::Error;
22
23use crate::backend::CommitId;
24use crate::commit::Commit;
25use crate::repo::Repo;
26use crate::revset::ResolvedRevsetExpression;
27use crate::revset::RevsetEvaluationError;
28use crate::revset::RevsetExpression;
29use crate::revset::RevsetIteratorExt as _;
30
31/// An error that occurred while bisecting
32#[derive(Error, Debug)]
33pub enum BisectionError {
34    /// Failed to evaluate a revset
35    #[error("Failed to evaluate a revset involved in bisection")]
36    RevsetEvaluationError(#[from] RevsetEvaluationError),
37}
38
39/// Indicates whether a given commit was good, bad, or if it could not be
40/// determined.
41#[derive(Debug)]
42pub enum Evaluation {
43    /// The commit was good
44    Good,
45    /// The commit was bad
46    Bad,
47    /// It could not be determined whether the commit was good or bad
48    Skip,
49}
50
51impl Evaluation {
52    /// Maps the current evaluation to its inverse.
53    ///
54    /// Maps `Good`->`Bad`, `Bad`->`Good`, and keeps `Skip` as is.
55    pub fn invert(self) -> Self {
56        use Evaluation::*;
57        match self {
58            Good => Bad,
59            Bad => Good,
60            Skip => Skip,
61        }
62    }
63}
64
65/// Performs bisection to find the first bad commit in a range.
66pub struct Bisector<'repo> {
67    repo: &'repo dyn Repo,
68    input_range: Arc<ResolvedRevsetExpression>,
69    good_commits: HashSet<CommitId>,
70    bad_commits: HashSet<CommitId>,
71    skipped_commits: HashSet<CommitId>,
72}
73
74/// The result of bisection.
75#[derive(Debug, PartialEq, Eq, Clone)]
76pub enum BisectionResult {
77    /// Found the first bad commit(s). It should be exactly one unless the input
78    /// range had multiple disjoint heads.
79    Found(Vec<Commit>),
80    /// Could not determine the first bad commit because it was in a
81    /// skipped range.
82    Indeterminate,
83}
84
85/// The next bisection step.
86#[derive(Debug, PartialEq, Eq, Clone)]
87pub enum NextStep {
88    /// The commit must be evaluated.
89    Evaluate(Commit),
90    /// Bisection is complete.
91    Done(BisectionResult),
92}
93
94impl<'repo> Bisector<'repo> {
95    /// Create a new bisector. The range's heads are assumed to be bad.
96    /// Parents of the range's roots are assumed to be good.
97    pub fn new(
98        repo: &'repo dyn Repo,
99        input_range: Arc<ResolvedRevsetExpression>,
100    ) -> Result<Self, BisectionError> {
101        let bad_commits = input_range.heads().evaluate(repo)?.iter().try_collect()?;
102        Ok(Self {
103            repo,
104            input_range,
105            bad_commits,
106            good_commits: HashSet::new(),
107            skipped_commits: HashSet::new(),
108        })
109    }
110
111    /// Mark a commit good.
112    pub fn mark_good(&mut self, id: CommitId) {
113        assert!(!self.bad_commits.contains(&id));
114        assert!(!self.skipped_commits.contains(&id));
115        self.good_commits.insert(id);
116    }
117
118    /// Mark a commit bad.
119    pub fn mark_bad(&mut self, id: CommitId) {
120        assert!(!self.good_commits.contains(&id));
121        assert!(!self.skipped_commits.contains(&id));
122        self.bad_commits.insert(id);
123    }
124
125    /// Mark a commit as skipped (cannot be determined if it's good or bad).
126    pub fn mark_skipped(&mut self, id: CommitId) {
127        assert!(!self.good_commits.contains(&id));
128        assert!(!self.bad_commits.contains(&id));
129        self.skipped_commits.insert(id);
130    }
131
132    /// Mark a commit as good, bad, or skipped, according to the outcome in
133    /// `evaluation`.
134    pub fn mark(&mut self, id: CommitId, evaluation: Evaluation) {
135        match evaluation {
136            Evaluation::Good => self.mark_good(id),
137            Evaluation::Bad => self.mark_bad(id),
138            Evaluation::Skip => self.mark_skipped(id),
139        }
140    }
141
142    /// The commits that were marked good.
143    pub fn good_commits(&self) -> &HashSet<CommitId> {
144        &self.good_commits
145    }
146
147    /// The commits that were marked bad.
148    pub fn bad_commits(&self) -> &HashSet<CommitId> {
149        &self.bad_commits
150    }
151
152    /// The commits that were skipped.
153    pub fn skipped_commits(&self) -> &HashSet<CommitId> {
154        &self.skipped_commits
155    }
156
157    /// Find the next commit to evaluate, or determine that there are no more
158    /// steps.
159    pub fn next_step(&mut self) -> Result<NextStep, BisectionError> {
160        let good_expr = RevsetExpression::commits(self.good_commits.iter().cloned().collect());
161        let bad_expr = RevsetExpression::commits(self.bad_commits.iter().cloned().collect());
162        let skipped_expr =
163            RevsetExpression::commits(self.skipped_commits.iter().cloned().collect());
164        // Intersect the input range with the current bad range and then bisect it to
165        // find the next commit to evaluate.
166        // Skipped revisions are simply subtracted from the set.
167        // TODO: Handle long ranges of skipped revisions better
168        let to_evaluate_expr = self
169            .input_range
170            .intersection(&good_expr.heads().range(&bad_expr.roots()))
171            .minus(&bad_expr)
172            .minus(&skipped_expr)
173            .bisect()
174            .latest(1);
175        let to_evaluate_set = to_evaluate_expr.evaluate(self.repo)?;
176        if let Some(commit) = to_evaluate_set
177            .iter()
178            .commits(self.repo.store())
179            .next()
180            .transpose()?
181        {
182            Ok(NextStep::Evaluate(commit))
183        } else {
184            let bad_roots = bad_expr.roots().evaluate(self.repo)?;
185            let bad_commits: Vec<_> = bad_roots.iter().commits(self.repo.store()).try_collect()?;
186            if bad_commits.is_empty() {
187                Ok(NextStep::Done(BisectionResult::Indeterminate))
188            } else {
189                Ok(NextStep::Done(BisectionResult::Found(bad_commits)))
190            }
191        }
192    }
193}