git_revision/spec/
mod.rs

1use crate::Spec;
2
3/// How to interpret a revision specification, or `revspec`.
4#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Ord, Eq, Hash)]
5#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
6pub enum Kind {
7    /// Include commits reachable from this revision, the default when parsing revision `a` for example, i.e. `a` and its ancestors.
8    /// Example: `a`.
9    IncludeReachable,
10    /// Exclude commits reachable from this revision, i.e. `a` and its ancestors. Example: `^a`.
11    ExcludeReachable,
12    /// Every commit that is reachable from `b` but not from `a`. Example: `a..b`.
13    RangeBetween,
14    /// Every commit reachable through either `a` or `b` but no commit that is reachable by both. Example: `a...b`.
15    ReachableToMergeBase,
16    /// Include every commit of all parents of `a`, but not `a` itself. Example: `a^@`.
17    IncludeReachableFromParents,
18    /// Exclude every commit of all parents of `a`, but not `a` itself. Example: `a^!`.
19    ExcludeReachableFromParents,
20}
21
22impl Default for Kind {
23    fn default() -> Self {
24        Kind::IncludeReachable
25    }
26}
27
28impl Spec {
29    /// Return the kind of this specification.
30    pub fn kind(&self) -> Kind {
31        match self {
32            Spec::Include(_) => Kind::IncludeReachable,
33            Spec::Exclude(_) => Kind::ExcludeReachable,
34            Spec::Range { .. } => Kind::RangeBetween,
35            Spec::Merge { .. } => Kind::ReachableToMergeBase,
36            Spec::IncludeOnlyParents { .. } => Kind::IncludeReachableFromParents,
37            Spec::ExcludeParents { .. } => Kind::ExcludeReachableFromParents,
38        }
39    }
40}
41
42mod _impls {
43    use std::fmt::{Display, Formatter};
44
45    use crate::Spec;
46
47    impl Display for Spec {
48        fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
49            match self {
50                Spec::Include(oid) => Display::fmt(oid, f),
51                Spec::Exclude(oid) => write!(f, "^{oid}"),
52                Spec::Range { from, to } => write!(f, "{from}..{to}"),
53                Spec::Merge { theirs, ours } => write!(f, "{theirs}...{ours}"),
54                Spec::IncludeOnlyParents(from_exclusive) => write!(f, "{from_exclusive}^@"),
55                Spec::ExcludeParents(oid) => write!(f, "{oid}^!"),
56            }
57        }
58    }
59}
60
61///
62pub mod parse;
63pub use parse::function::parse;