rust/hg-core/src/narrow.rs
author Raphaël Gomès <rgomes@octobus.net>
Mon, 04 Nov 2024 11:00:58 +0100
changeset 52302 db065b33fa56
parent 52043 ae1ab6d71f4a
permissions -rw-r--r--
rust-dirstate: merge `dirstate_tree` module into `dirstate` The historical reasonning for `dirstate_tree` existing in the first place is that a new approach was needed for the tree-like dirstate and it was easier to start somewhat fresh. Now that the former dirstate is (long) gone, we can merge those two modules to avoid the confusion that even the module creators sometimes get.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
use std::path::Path;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
use crate::{
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
    errors::HgError,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
    exit_codes,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
    filepatterns::parse_pattern_file_contents,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
    matchers::{
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
        AlwaysMatcher, DifferenceMatcher, IncludeMatcher, Matcher,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
        NeverMatcher,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
    },
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    11
    repo::Repo,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
    requirements::NARROW_REQUIREMENT,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
    sparse::{self, SparseConfigError, SparseWarning},
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
};
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    15
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    16
/// The file in .hg/store/ that indicates which paths exit in the store
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
const FILENAME: &str = "narrowspec";
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
/// The file in .hg/ that indicates which paths exit in the dirstate
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
const DIRSTATE_FILENAME: &str = "narrowspec.dirstate";
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
/// Pattern prefixes that are allowed in narrow patterns. This list MUST
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
/// only contain patterns that are fast and safe to evaluate. Keep in mind
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
/// that patterns are supplied by clients and executed on remote servers
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
/// as part of wire protocol commands. That means that changes to this
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
/// data structure influence the wire protocol and should not be taken
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
/// lightly - especially removals.
52043
ae1ab6d71f4a rust: implement `From<SparseConfigWarning>` for `HgError`
Raphaël Gomès <rgomes@octobus.net>
parents: 50857
diff changeset
    27
pub const VALID_PREFIXES: [&str; 2] = ["path:", "rootfilesin:"];
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
/// Return the matcher for the current narrow spec, and all configuration
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
/// warnings to display.
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
pub fn matcher(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
    repo: &Repo,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
) -> Result<(Box<dyn Matcher + Sync>, Vec<SparseWarning>), SparseConfigError> {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
    let mut warnings = vec![];
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
    if !repo.requirements().contains(NARROW_REQUIREMENT) {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
        return Ok((Box::new(AlwaysMatcher), warnings));
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
    // Treat "narrowspec does not exist" the same as "narrowspec file exists
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
    // and is empty".
49930
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49915
diff changeset
    40
    let store_spec = repo.store_vfs().try_read(FILENAME)?.unwrap_or_default();
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49915
diff changeset
    41
    let working_copy_spec = repo
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49915
diff changeset
    42
        .hg_vfs()
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49915
diff changeset
    43
        .try_read(DIRSTATE_FILENAME)?
e98fd81bb151 rust-clippy: fix most warnings in `hg-core`
Raphaël Gomès <rgomes@octobus.net>
parents: 49915
diff changeset
    44
        .unwrap_or_default();
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
    if store_spec != working_copy_spec {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
        return Err(HgError::abort(
49982
7faedeb24eb2 rhg: fix user-facing error message so it matches Python implementation
Raphaël Gomès <rgomes@octobus.net>
parents: 49930
diff changeset
    47
            "abort: working copy's narrowspec is stale",
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
            exit_codes::STATE_ERROR,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
            Some("run 'hg tracked --update-working-copy'".into()),
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
        )
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
        .into());
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
    let config = sparse::parse_config(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
        &store_spec,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
        sparse::SparseConfigContext::Narrow,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
    )?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
    warnings.extend(config.warnings);
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
    if !config.profiles.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
        // TODO (from Python impl) maybe do something with profiles?
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
        return Err(SparseConfigError::IncludesInNarrow);
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
    validate_patterns(&config.includes)?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
    validate_patterns(&config.excludes)?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
    if config.includes.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
        return Ok((Box::new(NeverMatcher), warnings));
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
    let (patterns, subwarnings) = parse_pattern_file_contents(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
        &config.includes,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
        Path::new(""),
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
        None,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
        false,
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 49982
diff changeset
    77
        true,
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
    )?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
    warnings.extend(subwarnings.into_iter().map(From::from));
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
    let mut m: Box<dyn Matcher + Sync> =
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
        Box::new(IncludeMatcher::new(patterns)?);
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    83
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
    let (patterns, subwarnings) = parse_pattern_file_contents(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
        &config.excludes,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    86
        Path::new(""),
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
        None,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    88
        false,
50857
796b5d6693a4 rust: simplify pattern file parsing
Spencer Baugh <sbaugh@janestreet.com>
parents: 49982
diff changeset
    89
        true,
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    90
    )?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    91
    if !patterns.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
        warnings.extend(subwarnings.into_iter().map(From::from));
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    93
        let exclude_matcher = Box::new(IncludeMatcher::new(patterns)?);
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
        m = Box::new(DifferenceMatcher::new(m, exclude_matcher));
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    96
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
    Ok((m, warnings))
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
}
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   100
fn validate_patterns(patterns: &[u8]) -> Result<(), SparseConfigError> {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   101
    for pattern in patterns.split(|c| *c == b'\n') {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   102
        if pattern.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   103
            continue;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   104
        }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   105
        for prefix in VALID_PREFIXES.iter() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   106
            if pattern.starts_with(prefix.as_bytes()) {
49915
c8ef85ace216 rust-narrow: fix loop that never loops
Raphaël Gomès <rgomes@octobus.net>
parents: 49489
diff changeset
   107
                return Ok(());
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   108
            }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   109
        }
49915
c8ef85ace216 rust-narrow: fix loop that never loops
Raphaël Gomès <rgomes@octobus.net>
parents: 49489
diff changeset
   110
        return Err(SparseConfigError::InvalidNarrowPrefix(
c8ef85ace216 rust-narrow: fix loop that never loops
Raphaël Gomès <rgomes@octobus.net>
parents: 49489
diff changeset
   111
            pattern.to_owned(),
c8ef85ace216 rust-narrow: fix loop that never loops
Raphaël Gomès <rgomes@octobus.net>
parents: 49489
diff changeset
   112
        ));
49489
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   113
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   114
    Ok(())
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   115
}