rust/hg-core/src/sparse.rs
author Raphaël Gomès <rgomes@octobus.net>
Tue, 19 Jul 2022 15:37:45 +0200
changeset 49485 ffd4b1f1c9cb
child 49488 7c93e38a0bbd
permissions -rw-r--r--
rhg: add sparse support
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49485
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
use std::{collections::HashSet, path::Path};
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
use format_bytes::{write_bytes, DisplayBytes};
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
use crate::{
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
    errors::HgError,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
    filepatterns::parse_pattern_file_contents,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
    matchers::{
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     9
        AlwaysMatcher, DifferenceMatcher, IncludeMatcher, Matcher,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    10
        UnionMatcher,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    11
    },
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
    operations::cat,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
    repo::Repo,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
    requirements::SPARSE_REQUIREMENT,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    15
    utils::{hg_path::HgPath, SliceExt},
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    16
    IgnorePattern, PatternError, PatternFileWarning, PatternSyntax, Revision,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
    NULL_REVISION,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
};
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
/// Command which is triggering the config read
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
#[derive(Copy, Clone, Debug)]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
pub enum SparseConfigContext {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
    Sparse,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
    Narrow,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
impl DisplayBytes for SparseConfigContext {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
    fn display_bytes(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
        &self,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
        output: &mut dyn std::io::Write,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
    ) -> std::io::Result<()> {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
        match self {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
            SparseConfigContext::Sparse => write_bytes!(output, b"sparse"),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
            SparseConfigContext::Narrow => write_bytes!(output, b"narrow"),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
        }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    37
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    38
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    39
/// Possible warnings when reading sparse configuration
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    40
#[derive(Debug, derive_more::From)]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
pub enum SparseWarning {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
    /// Warns about improper paths that start with "/"
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
    RootWarning {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
        context: SparseConfigContext,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
        line: Vec<u8>,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
    },
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
    /// Warns about a profile missing from the given changelog revision
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
    ProfileNotFound { profile: Vec<u8>, rev: Revision },
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
    #[from]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
    Pattern(PatternFileWarning),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
/// Parsed sparse config
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
#[derive(Debug, Default)]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
pub struct SparseConfig {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    56
    // Line-separated
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    57
    includes: Vec<u8>,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    58
    // Line-separated
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    59
    excludes: Vec<u8>,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
    profiles: HashSet<Vec<u8>>,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    61
    warnings: Vec<SparseWarning>,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    62
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    63
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    64
/// All possible errors when reading sparse config
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    65
#[derive(Debug, derive_more::From)]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    66
pub enum SparseConfigError {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    67
    IncludesAfterExcludes {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    68
        context: SparseConfigContext,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    69
    },
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    70
    EntryOutsideSection {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    71
        context: SparseConfigContext,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    72
        line: Vec<u8>,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    73
    },
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    74
    #[from]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    75
    HgError(HgError),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    76
    #[from]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    77
    PatternError(PatternError),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    78
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    79
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    80
/// Parse sparse config file content.
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    81
fn parse_config(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    82
    raw: &[u8],
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    83
    context: SparseConfigContext,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    84
) -> Result<SparseConfig, SparseConfigError> {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    85
    let mut includes = vec![];
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    86
    let mut excludes = vec![];
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    87
    let mut profiles = HashSet::new();
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    88
    let mut warnings = vec![];
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    89
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    90
    #[derive(PartialEq, Eq)]
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    91
    enum Current {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    92
        Includes,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    93
        Excludes,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    94
        None,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    95
    };
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    96
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    97
    let mut current = Current::None;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    98
    let mut in_section = false;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    99
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   100
    for line in raw.split(|c| *c == b'\n') {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   101
        let line = line.trim();
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   102
        if line.is_empty() || line[0] == b'#' {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   103
            // empty or comment line, skip
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   104
            continue;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   105
        }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   106
        if line.starts_with(b"%include ") {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   107
            let profile = line[b"%include ".len()..].trim();
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   108
            if !profile.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   109
                profiles.insert(profile.into());
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   110
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   111
        } else if line == b"[include]" {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   112
            if in_section && current == Current::Includes {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   113
                return Err(SparseConfigError::IncludesAfterExcludes {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   114
                    context,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   115
                });
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   116
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   117
            in_section = true;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   118
            current = Current::Includes;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   119
            continue;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   120
        } else if line == b"[exclude]" {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   121
            in_section = true;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   122
            current = Current::Excludes;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   123
        } else {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   124
            if current == Current::None {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   125
                return Err(SparseConfigError::EntryOutsideSection {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   126
                    context,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   127
                    line: line.into(),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   128
                });
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   129
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   130
            if line.trim().starts_with(b"/") {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   131
                warnings.push(SparseWarning::RootWarning {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   132
                    context,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   133
                    line: line.into(),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   134
                });
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   135
                continue;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   136
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   137
            match current {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   138
                Current::Includes => {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   139
                    includes.push(b'\n');
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   140
                    includes.extend(line.iter());
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   141
                }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   142
                Current::Excludes => {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   143
                    excludes.push(b'\n');
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   144
                    excludes.extend(line.iter());
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   145
                }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   146
                Current::None => unreachable!(),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   147
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   148
        }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   149
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   150
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   151
    Ok(SparseConfig {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   152
        includes,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   153
        excludes,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   154
        profiles,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   155
        warnings,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   156
    })
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   157
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   158
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   159
fn read_temporary_includes(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   160
    repo: &Repo,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   161
) -> Result<Vec<Vec<u8>>, SparseConfigError> {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   162
    let raw = repo.hg_vfs().try_read("tempsparse")?.unwrap_or(vec![]);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   163
    if raw.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   164
        return Ok(vec![]);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   165
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   166
    Ok(raw.split(|c| *c == b'\n').map(ToOwned::to_owned).collect())
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   167
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   168
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   169
/// Obtain sparse checkout patterns for the given revision
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   170
fn patterns_for_rev(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   171
    repo: &Repo,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   172
    rev: Revision,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   173
) -> Result<Option<SparseConfig>, SparseConfigError> {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   174
    if !repo.has_sparse() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   175
        return Ok(None);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   176
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   177
    let raw = repo.hg_vfs().try_read("sparse")?.unwrap_or(vec![]);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   178
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   179
    if raw.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   180
        return Ok(None);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   181
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   182
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   183
    let mut config = parse_config(&raw, SparseConfigContext::Sparse)?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   184
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   185
    if !config.profiles.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   186
        let mut profiles: Vec<Vec<u8>> = config.profiles.into_iter().collect();
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   187
        let mut visited = HashSet::new();
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   188
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   189
        while let Some(profile) = profiles.pop() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   190
            if visited.contains(&profile) {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   191
                continue;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   192
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   193
            visited.insert(profile.to_owned());
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   194
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   195
            let output =
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   196
                cat(repo, &rev.to_string(), vec![HgPath::new(&profile)])
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   197
                    .map_err(|_| {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   198
                        HgError::corrupted(format!(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   199
                            "dirstate points to non-existent parent node"
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   200
                        ))
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   201
                    })?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   202
            if output.results.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   203
                config.warnings.push(SparseWarning::ProfileNotFound {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   204
                    profile: profile.to_owned(),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   205
                    rev,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   206
                })
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   207
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   208
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   209
            let subconfig = parse_config(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   210
                &output.results[0].1,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   211
                SparseConfigContext::Sparse,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   212
            )?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   213
            if !subconfig.includes.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   214
                config.includes.push(b'\n');
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   215
                config.includes.extend(&subconfig.includes);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   216
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   217
            if !subconfig.includes.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   218
                config.includes.push(b'\n');
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   219
                config.excludes.extend(&subconfig.excludes);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   220
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   221
            config.warnings.extend(subconfig.warnings.into_iter());
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   222
            profiles.extend(subconfig.profiles.into_iter());
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   223
        }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   224
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   225
        config.profiles = visited;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   226
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   227
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   228
    if !config.includes.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   229
        config.includes.extend(b"\n.hg*");
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   230
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   231
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   232
    Ok(Some(config))
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   233
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   234
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   235
/// Obtain a matcher for sparse working directories.
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   236
pub fn matcher(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   237
    repo: &Repo,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   238
) -> Result<(Box<dyn Matcher + Sync>, Vec<SparseWarning>), SparseConfigError> {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   239
    let mut warnings = vec![];
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   240
    if !repo.requirements().contains(SPARSE_REQUIREMENT) {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   241
        return Ok((Box::new(AlwaysMatcher), warnings));
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   242
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   243
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   244
    let parents = repo.dirstate_parents()?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   245
    let mut revs = vec![];
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   246
    let p1_rev =
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   247
        repo.changelog()?
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   248
            .rev_from_node(parents.p1.into())
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   249
            .map_err(|_| {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   250
                HgError::corrupted(format!(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   251
                    "dirstate points to non-existent parent node"
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   252
                ))
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   253
            })?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   254
    if p1_rev != NULL_REVISION {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   255
        revs.push(p1_rev)
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   256
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   257
    let p2_rev =
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   258
        repo.changelog()?
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   259
            .rev_from_node(parents.p2.into())
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   260
            .map_err(|_| {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   261
                HgError::corrupted(format!(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   262
                    "dirstate points to non-existent parent node"
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   263
                ))
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   264
            })?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   265
    if p2_rev != NULL_REVISION {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   266
        revs.push(p2_rev)
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   267
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   268
    let mut matchers = vec![];
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   269
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   270
    for rev in revs.iter() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   271
        let config = patterns_for_rev(repo, *rev);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   272
        if let Ok(Some(config)) = config {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   273
            warnings.extend(config.warnings);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   274
            let mut m: Box<dyn Matcher + Sync> = Box::new(AlwaysMatcher);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   275
            if !config.includes.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   276
                let (patterns, subwarnings) = parse_pattern_file_contents(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   277
                    &config.includes,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   278
                    Path::new(""),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   279
                    Some(b"relglob:".as_ref()),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   280
                    false,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   281
                )?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   282
                warnings.extend(subwarnings.into_iter().map(From::from));
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   283
                m = Box::new(IncludeMatcher::new(patterns)?);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   284
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   285
            if !config.excludes.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   286
                let (patterns, subwarnings) = parse_pattern_file_contents(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   287
                    &config.excludes,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   288
                    Path::new(""),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   289
                    Some(b"relglob:".as_ref()),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   290
                    false,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   291
                )?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   292
                warnings.extend(subwarnings.into_iter().map(From::from));
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   293
                m = Box::new(DifferenceMatcher::new(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   294
                    m,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   295
                    Box::new(IncludeMatcher::new(patterns)?),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   296
                ));
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   297
            }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   298
            matchers.push(m);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   299
        }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   300
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   301
    let result: Box<dyn Matcher + Sync> = match matchers.len() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   302
        0 => Box::new(AlwaysMatcher),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   303
        1 => matchers.pop().expect("1 is equal to 0"),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   304
        _ => Box::new(UnionMatcher::new(matchers)),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   305
    };
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   306
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   307
    let matcher =
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   308
        force_include_matcher(result, &read_temporary_includes(repo)?)?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   309
    Ok((matcher, warnings))
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   310
}
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   311
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   312
/// Returns a matcher that returns true for any of the forced includes before
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   313
/// testing against the actual matcher
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   314
fn force_include_matcher(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   315
    result: Box<dyn Matcher + Sync>,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   316
    temp_includes: &[Vec<u8>],
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   317
) -> Result<Box<dyn Matcher + Sync>, PatternError> {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   318
    if temp_includes.is_empty() {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   319
        return Ok(result);
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   320
    }
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   321
    let forced_include_matcher = IncludeMatcher::new(
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   322
        temp_includes
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   323
            .into_iter()
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   324
            .map(|include| {
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   325
                IgnorePattern::new(PatternSyntax::Path, include, Path::new(""))
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   326
            })
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   327
            .collect(),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   328
    )?;
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   329
    Ok(Box::new(UnionMatcher::new(vec![
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   330
        Box::new(forced_include_matcher),
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   331
        result,
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   332
    ])))
ffd4b1f1c9cb rhg: add sparse support
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
   333
}