Skip to main content

grit_lib/error/
revision.rs

1//! [`RevisionError`]: revision lookup, parsing, and walking failures.
2
3use thiserror::Error;
4
5/// Revision failure: a name that resolves to nothing, a revision expression that does not
6/// parse, or a revision walk whose options are invalid.
7///
8/// Surfaced on the umbrella as [`Error::Revision`](crate::error::Error::Revision).
9#[derive(Debug, Error)]
10#[non_exhaustive]
11pub enum RevisionError {
12    /// A revision expression (branch, tag, object id, `x~n`, `ORIG_HEAD`, …) names nothing in
13    /// the repository.
14    #[error("unknown revision '{spec}'")]
15    NotFound {
16        /// The expression as the caller wrote it.
17        spec: String,
18    },
19
20    /// Two commits share no common ancestor.
21    #[error("no merge base between {left} and {right}")]
22    NoMergeBase {
23        /// First commit.
24        left: crate::objects::ObjectId,
25        /// Second commit.
26        right: crate::objects::ObjectId,
27    },
28
29    /// Two commits have more than one best common ancestor (criss-cross history), and the
30    /// operation needs exactly one.
31    #[error("multiple merge bases between {left} and {right}")]
32    MultipleMergeBases {
33        /// First commit.
34        left: crate::objects::ObjectId,
35        /// Second commit.
36        right: crate::objects::ObjectId,
37    },
38
39    /// A revision spec could not be resolved to an object for inspection.
40    #[error(transparent)]
41    Resolve(#[from] crate::object_inspect::ResolveError),
42
43    /// Revision parsing failed.
44    #[error(transparent)]
45    Parse(#[from] crate::rev_parse_error::RevParseError),
46
47    /// Revision walking or `rev-list` option parsing failed.
48    #[error(transparent)]
49    List(#[from] crate::rev_list_error::RevListError),
50}
51
52impl RevisionError {
53    /// Stable snake_case slug for this failure (the former umbrella variant names).
54    #[must_use]
55    pub fn kind_name(&self) -> &'static str {
56        match self {
57            Self::NotFound { .. } => "revision_not_found",
58            Self::NoMergeBase { .. } => "no_merge_base",
59            Self::MultipleMergeBases { .. } => "multiple_merge_bases",
60            Self::Parse(..) => "rev_parse",
61            Self::Resolve(..) => "resolve",
62            Self::List(..) => "rev_list",
63        }
64    }
65}