rust/hg-core/src/narrow.rs
author Manuel Jacob <me@manueljacob.de>
Thu, 15 Sep 2022 01:48:38 +0200
changeset 49494 c96ed4029fda
parent 49488 7c93e38a0bbd
child 49915 c8ef85ace216
permissions -rw-r--r--
templates: add filter to reverse list The filter supports only lists because for lists, it’s straightforward to implement. Reversing text doesn’t seem very useful and is hard to implement. Reversing the bytes would break multi-bytes encodings. Reversing the code points would break characters consisting of multiple code points. Reversing graphemes is non-trivial without using a library not included in the standard library.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49488
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.
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
const VALID_PREFIXES: [&str; 2] = ["path:", "rootfilesin:"];
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".
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
    let store_spec = repo.store_vfs().try_read(FILENAME)?.unwrap_or(vec![]);
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
    let working_copy_spec =
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
        repo.hg_vfs().try_read(DIRSTATE_FILENAME)?.unwrap_or(vec![]);
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
    if store_spec != working_copy_spec {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
        return Err(HgError::abort(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
            "working copy's narrowspec is stale",
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
            exit_codes::STATE_ERROR,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
            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
    48
        )
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
        .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
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
    let config = sparse::parse_config(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
        &store_spec,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
        sparse::SparseConfigContext::Narrow,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
    )?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
    warnings.extend(config.warnings);
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
    if !config.profiles.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
        // 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
    61
        return Err(SparseConfigError::IncludesInNarrow);
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
    validate_patterns(&config.includes)?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
    validate_patterns(&config.excludes)?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
    if config.includes.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
        return Ok((Box::new(NeverMatcher), warnings));
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
    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
    71
        &config.includes,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
        Path::new(""),
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
        None,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
        false,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
    )?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    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
    77
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
    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
    79
        Box::new(IncludeMatcher::new(patterns)?);
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 (patterns, subwarnings) = parse_pattern_file_contents(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
        &config.excludes,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    83
        Path::new(""),
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
        None,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
        false,
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    86
    )?;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
    if !patterns.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    88
        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
    89
        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
    90
        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
    91
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    93
    Ok((m, warnings))
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
}
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
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
    97
    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
    98
        if pattern.is_empty() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
            continue;
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   100
        }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   101
        for prefix in VALID_PREFIXES.iter() {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   102
            if pattern.starts_with(prefix.as_bytes()) {
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   103
                break;
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
            return Err(SparseConfigError::InvalidNarrowPrefix(
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   106
                pattern.to_owned(),
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   107
            ));
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
    }
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   110
    Ok(())
7c93e38a0bbd rhg-status: add support for narrow clones
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   111
}