status.rs
154 lines
| 4.9 KiB
| application/rls-services+xml
|
RustLexer
|
r43565 | // status.rs | ||
// | ||||
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net> | ||||
// | ||||
// This software may be used and distributed according to the terms of the | ||||
// GNU General Public License version 2 or any later version. | ||||
//! Rust implementation of dirstate.status (dirstate.py). | ||||
//! It is currently missing a lot of functionality compared to the Python one | ||||
//! and will only be triggered in narrow cases. | ||||
|
r48126 | use crate::dirstate_tree::on_disk::DirstateV2ParseError; | ||
|
r48882 | |||
|
r44003 | use crate::{ | ||
|
r48882 | utils::hg_path::{HgPath, HgPathError}, | ||
|
r45015 | PatternError, | ||
|
r44003 | }; | ||
|
r48882 | |||
use std::{borrow::Cow, fmt}; | ||||
|
r43565 | |||
|
r45013 | /// Wrong type of file from a `BadMatch` | ||
/// Note: a lot of those don't exist on all platforms. | ||||
|
r45023 | #[derive(Debug, Copy, Clone)] | ||
|
r45013 | pub enum BadType { | ||
CharacterDevice, | ||||
BlockDevice, | ||||
FIFO, | ||||
Socket, | ||||
Directory, | ||||
Unknown, | ||||
} | ||||
|
r47173 | impl fmt::Display for BadType { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||
f.write_str(match self { | ||||
|
r45016 | BadType::CharacterDevice => "character device", | ||
BadType::BlockDevice => "block device", | ||||
BadType::FIFO => "fifo", | ||||
BadType::Socket => "socket", | ||||
BadType::Directory => "directory", | ||||
BadType::Unknown => "unknown", | ||||
|
r47173 | }) | ||
|
r45016 | } | ||
} | ||||
|
r45013 | /// Was explicitly matched but cannot be found/accessed | ||
|
r45023 | #[derive(Debug, Copy, Clone)] | ||
|
r45013 | pub enum BadMatch { | ||
OsError(i32), | ||||
BadType(BadType), | ||||
} | ||||
|
r47883 | /// `Box<dyn Trait>` is syntactic sugar for `Box<dyn Trait + 'static>`, so add | ||
|
r45088 | /// an explicit lifetime here to not fight `'static` bounds "out of nowhere". | ||
|
r47882 | pub type IgnoreFnType<'a> = | ||
Box<dyn for<'r> Fn(&'r HgPath) -> bool + Sync + 'a>; | ||||
|
r44367 | |||
|
r45672 | /// We have a good mix of owned (from directory traversal) and borrowed (from | ||
/// the dirstate/explicit) paths, this comes up a lot. | ||||
|
r45673 | pub type HgPathCow<'a> = Cow<'a, HgPath>; | ||
|
r45672 | |||
|
r45011 | #[derive(Debug, Copy, Clone)] | ||
pub struct StatusOptions { | ||||
/// Whether we are on a filesystem with UNIX-like exec flags | ||||
pub check_exec: bool, | ||||
pub list_clean: bool, | ||||
|
r45014 | pub list_unknown: bool, | ||
pub list_ignored: bool, | ||||
|
r49285 | /// Whether to populate `StatusPath::copy_source` | ||
pub list_copies: bool, | ||||
|
r45353 | /// Whether to collect traversed dirs for applying a callback later. | ||
/// Used by `hg purge` for example. | ||||
pub collect_traversed_dirs: bool, | ||||
|
r45014 | } | ||
|
r49285 | #[derive(Default)] | ||
|
r45012 | pub struct DirstateStatus<'a> { | ||
|
r49250 | /// The current time at the start of the `status()` algorithm, as measured | ||
/// and possibly truncated by the filesystem. | ||||
pub filesystem_time_at_status_start: Option<std::time::SystemTime>, | ||||
|
r47881 | /// Tracked files whose contents have changed since the parent revision | ||
|
r49285 | pub modified: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
/// Newly-tracked files that were not present in the parent | ||||
|
r49285 | pub added: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
/// Previously-tracked files that have been (re)moved with an hg command | ||||
|
r49285 | pub removed: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
/// (Still) tracked files that are missing, (re)moved with an non-hg | ||||
/// command | ||||
|
r49285 | pub deleted: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
/// Tracked files that are up to date with the parent. | ||||
/// Only pupulated if `StatusOptions::list_clean` is true. | ||||
|
r49285 | pub clean: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
/// Files in the working directory that are ignored with `.hgignore`. | ||||
/// Only pupulated if `StatusOptions::list_ignored` is true. | ||||
|
r49285 | pub ignored: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
/// Files in the working directory that are neither tracked nor ignored. | ||||
/// Only pupulated if `StatusOptions::list_unknown` is true. | ||||
|
r49285 | pub unknown: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
/// Was explicitly matched but cannot be found/accessed | ||||
|
r45672 | pub bad: Vec<(HgPathCow<'a>, BadMatch)>, | ||
|
r47881 | |||
|
r47880 | /// Either clean or modified, but we can’t tell from filesystem metadata | ||
/// alone. The file contents need to be read and compared with that in | ||||
/// the parent. | ||||
|
r49285 | pub unsure: Vec<StatusPath<'a>>, | ||
|
r47881 | |||
|
r45353 | /// Only filled if `collect_traversed_dirs` is `true` | ||
|
r48136 | pub traversed: Vec<HgPathCow<'a>>, | ||
|
r48139 | |||
/// Whether `status()` made changed to the `DirstateMap` that should be | ||||
/// written back to disk | ||||
pub dirty: bool, | ||||
|
r43565 | } | ||
|
r49285 | #[derive(Clone, PartialEq, Eq, PartialOrd, Ord)] | ||
pub struct StatusPath<'a> { | ||||
pub path: HgPathCow<'a>, | ||||
pub copy_source: Option<HgPathCow<'a>>, | ||||
} | ||||
|
r47164 | #[derive(Debug, derive_more::From)] | ||
|
r45671 | pub enum StatusError { | ||
|
r45672 | /// Generic IO error | ||
|
r45671 | IO(std::io::Error), | ||
|
r45672 | /// An invalid path that cannot be represented in Mercurial was found | ||
|
r45671 | Path(HgPathError), | ||
|
r45672 | /// An invalid "ignore" pattern was found | ||
|
r45671 | Pattern(PatternError), | ||
|
r48126 | /// Corrupted dirstate | ||
DirstateV2ParseError(DirstateV2ParseError), | ||||
|
r45671 | } | ||
pub type StatusResult<T> = Result<T, StatusError>; | ||||
|
r47173 | impl fmt::Display for StatusError { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||||
|
r45671 | match self { | ||
|
r47173 | StatusError::IO(error) => error.fmt(f), | ||
StatusError::Path(error) => error.fmt(f), | ||||
StatusError::Pattern(error) => error.fmt(f), | ||||
|
r48126 | StatusError::DirstateV2ParseError(_) => { | ||
f.write_str("dirstate-v2 parse error") | ||||
} | ||||
|
r45671 | } | ||
} | ||||
} | ||||