Skip to content
Snippets Groups Projects
Commit 1a99e0c2d6d5 authored by Mitchell Kember's avatar Mitchell Kember
Browse files

rust-revlog: add RevisionOrWdir

This type represents either a checked Revision or the wdir() pseudo-revision
(revision 0x7fffffff, node ffffffffffffffffffffffffffffffffffffffff).

You construct it with revision.into() or Revision::wdir(), and destructure it
with rev.exclude_wdir() which returns Option<Revision>.

I considered something like `enum RevisionOrWdir { Wdir, Revision(Revision) }`,
but decided on `struct RevisionOrWdir(BaseRevision)` for a few reasons:

- It's more ergonomic for the ways it actually gets used, in my opinion.
- It also avoids the possibility of an invalid value Revision(0x7fffffff).
- It remains 4 bytes rather than 8.
- It maintains the ordering: wdir is greater than all other revisions.

I'm planning to use this for 'rhg annotate -r wdir()'.
parent 879029f03324
No related branches found
No related tags found
2 merge requests!1306rust-annotate: support -Tjson,!1255rust-annotate: add support for wdir
......@@ -126,6 +126,39 @@
pub const WORKING_DIRECTORY_HEX: &str =
"ffffffffffffffffffffffffffffffffffffffff";
/// Either a checked revision or the working directory.
/// Note that [`Revision`] will never hold [`WORKING_DIRECTORY_REVISION`]
/// because that is not a valid revision in any revlog.
#[derive(Copy, Clone, Hash, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub struct RevisionOrWdir(BaseRevision);
impl From<Revision> for RevisionOrWdir {
fn from(value: Revision) -> Self {
RevisionOrWdir(value.0)
}
}
impl RevisionOrWdir {
/// Creates a [`RevisionOrWdir`] representing the working directory.
pub fn wdir() -> Self {
RevisionOrWdir(WORKING_DIRECTORY_REVISION.0)
}
/// Returns the revision, or `None` if this is the working directory.
pub fn exclude_wdir(self) -> Option<Revision> {
if self.0 == WORKING_DIRECTORY_REVISION.0 {
None
} else {
Some(Revision(self.0))
}
}
/// Returns true if this is the working directory.
pub fn is_wdir(&self) -> bool {
*self == Self::wdir()
}
}
/// The simplest expression of what we need of Mercurial DAGs.
pub trait Graph {
/// Return the two parents of the given `Revision`.
......@@ -974,4 +1007,10 @@
}
};
}
#[test]
fn test_revision_or_wdir_ord() {
let highest: RevisionOrWdir = Revision(i32::MAX - 1).into();
assert!(highest < RevisionOrWdir::wdir());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment