mmz 0.13.0

memoized command runner
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Expansion of scope patterns into concrete input paths.

use std::path::{Path, PathBuf};

use globset::{GlobBuilder, GlobSet, GlobSetBuilder};
use ignore::{DirEntry, Walk, WalkBuilder};

use crate::error::{Error, Result};

/// Returns true if `pattern` contains glob metacharacters.
#[must_use]
pub fn is_glob(pattern: &str) -> bool {
    pattern.chars().any(|ch| matches!(ch, '*' | '?' | '['))
}

/// A set of patterns, the patterns left out of them, and the ignore setting
/// they expand under.
///
/// A scope can override the manifest's `gitignore` and carry its own
/// `exclude`, so one rule's patterns no longer share a single filter; callers
/// hand [`expand_groups`] the patterns already bucketed by both.
#[derive(Debug, Clone)]
pub struct GlobGroup {
    /// Patterns to expand — globs and literal paths, as in [`expand`].
    pub globs: Vec<String>,
    /// Patterns to leave out. A path is excluded when it, or any directory
    /// above it, matches one, so an excluded directory is pruned during the
    /// walk and nothing under it is listed or hashed. Empty excludes nothing.
    pub exclude: Vec<String>,
    /// Whether git-ignored paths are skipped while expanding these patterns.
    pub gitignore: bool,
}

/// Expands every group against `base` and unions the results into one sorted,
/// de-duplicated list of paths relative to `base`.
///
/// Each group carries its own ignore setting, so a rule mixing a filtered scope
/// with an artifact scope resolves both in a single call. The gitignore chain
/// prunes directories *during* traversal — an ignored tree is never descended
/// into — so it cannot be re-applied per pattern after one shared walk; this
/// runs a walk per group instead, which [`crate::manifest::Manifest::glob_groups`]
/// keeps to one per distinct setting and exclusion by bucketing scopes rather
/// than emitting one group each. A single group with no `exclude` costs
/// exactly what [`expand`] always did.
///
/// # Errors
///
/// Returns [`Error::Pattern`] if a glob or exclude pattern is invalid.
pub fn expand_groups(groups: &[GlobGroup], base: &Path) -> Result<Vec<String>> {
    if let [only] = groups {
        return expand_group(only, base);
    }
    let mut out: Vec<String> = Vec::new();
    for group in groups {
        out.extend(expand_group(group, base)?);
    }
    out.sort();
    out.dedup();
    Ok(out)
}

/// Expands `patterns` (interpreted relative to `base`) into a sorted,
/// de-duplicated list of input paths relative to `base`.
///
/// Glob patterns may match zero files. A literal path absent on disk is skipped
/// with a warning (a removed input then shifts the digest rather than erroring).
/// When `gitignore` is true, glob matches ignored by git are dropped; explicitly
/// listed literals are always kept. The `.git` directory is never traversed.
/// Symlinked directories are never traversed, but a symlink that resolves to a
/// regular file is treated as a file (matching literal-path resolution), so a
/// symlinked source is not silently dropped from a glob's input set. Paths use
/// forward slashes.
///
/// # Errors
///
/// Returns [`Error::Pattern`] if a glob pattern is invalid.
pub fn expand(patterns: &[String], base: &Path, gitignore: bool) -> Result<Vec<String>> {
    expand_with(patterns, &Exclusion::default(), base, gitignore)
}

fn expand_group(group: &GlobGroup, base: &Path) -> Result<Vec<String>> {
    let exclusion = Exclusion::new(&group.exclude)?;
    expand_with(&group.globs, &exclusion, base, group.gitignore)
}

fn expand_with(
    patterns: &[String],
    exclusion: &Exclusion,
    base: &Path,
    gitignore: bool,
) -> Result<Vec<String>> {
    let mut out: Vec<String> = Vec::new();
    let mut globs: Vec<&str> = Vec::new();
    for pattern in patterns {
        if is_glob(pattern) {
            globs.push(pattern);
        } else if !exclusion.covers(&normalize(pattern)) {
            expand_literal(pattern, base, &mut out);
        }
    }
    if !globs.is_empty() {
        expand_globs(&globs, exclusion, base, gitignore, &mut out)?;
    }
    out.sort();
    out.dedup();
    Ok(out)
}

/// The compiled `exclude` patterns of one [`GlobGroup`]. The default excludes
/// nothing and compiles nothing.
#[derive(Debug, Clone, Default)]
struct Exclusion {
    set: Option<GlobSet>,
}

impl Exclusion {
    fn new(patterns: &[String]) -> Result<Self> {
        if patterns.is_empty() {
            return Ok(Self::default());
        }
        let patterns: Vec<&str> = patterns.iter().map(String::as_str).collect();
        Ok(Self {
            set: Some(build_globset(&patterns)?),
        })
    }

    /// Whether `rel` itself matches an exclude pattern. The walker prunes a
    /// matching directory, so for a walked path this is the whole question.
    fn matches(&self, rel: &str) -> bool {
        self.set.as_ref().is_some_and(|set| set.is_match(rel))
    }

    /// Whether `rel` or any directory above it matches, for a literal path
    /// that never went through the walk's pruning.
    fn covers(&self, rel: &str) -> bool {
        rel.match_indices('/')
            .filter_map(|(end, _)| rel.get(..end))
            .chain(std::iter::once(rel))
            .any(|path| self.matches(path))
    }
}

fn expand_literal(pattern: &str, base: &Path, out: &mut Vec<String>) {
    if base.join(pattern).is_file() {
        out.push(normalize(pattern));
    } else {
        log::warn!("literal path `{pattern}` is missing; treating as removed");
    }
}

/// Compiles `globs` into a matcher, then walks `base` once (honouring the
/// gitignore chain when `gitignore` is set, and pruning whatever `exclusion`
/// matches) and collects every file that matches at least one pattern.
/// Patterns matching nothing are warned about.
fn expand_globs(
    globs: &[&str],
    exclusion: &Exclusion,
    base: &Path,
    gitignore: bool,
    out: &mut Vec<String>,
) -> Result<()> {
    let set = build_globset(globs)?;
    let mut matched = vec![false; globs.len()];
    for entry in build_walker(base, gitignore, exclusion) {
        let Ok(entry) = entry else { continue };
        let path = entry.path();
        // `follow_links(false)` keeps the walker from descending into a
        // symlinked directory (its own file_type stays "symlink", not
        // "file"), but a symlink that resolves to a regular file is still
        // accepted here so it matches `expand_literal`'s
        // `base.join(pattern).is_file()`, which follows the link.
        let is_regular_file = entry.file_type().is_some_and(|kind| kind.is_file());
        let is_symlinked_file = !is_regular_file
            && entry.file_type().is_some_and(|kind| kind.is_symlink())
            && path.is_file();
        if !is_regular_file && !is_symlinked_file {
            continue;
        }
        let rel = path.strip_prefix(base).unwrap_or(path);
        let candidate = normalize(&rel.to_string_lossy());
        let hits = set.matches(&candidate);
        if hits.is_empty() {
            continue;
        }
        for index in hits {
            if let Some(flag) = matched.get_mut(index) {
                *flag = true;
            }
        }
        out.push(candidate);
    }
    for (index, pattern) in globs.iter().enumerate() {
        if matched.get(index) == Some(&false) {
            log::warn!("pattern `{pattern}` matched no files");
        }
    }
    Ok(())
}

/// Builds a [`GlobSet`] from `globs`, treating `/` as a literal separator so
/// `*` does not cross directories and `**` does.
fn build_globset(globs: &[&str]) -> Result<GlobSet> {
    let mut builder = GlobSetBuilder::new();
    for pattern in globs {
        let glob = GlobBuilder::new(pattern)
            .literal_separator(true)
            .build()
            .map_err(|source| Error::Pattern {
                pattern: (*pattern).to_owned(),
                source,
            })?;
        builder.add(glob);
    }
    builder.build().map_err(|source| Error::Pattern {
        pattern: globs.join(", "),
        source,
    })
}

/// Builds a file walker rooted at `base`. Hidden files are included (only
/// ignore rules filter), the `.git` directory is always pruned, and when
/// `gitignore` is set the full gitignore chain applies — `.gitignore` is also
/// added as a custom ignore file so it is honoured outside a git repository.
/// An entry `exclusion` matches is pruned before it is read, so nothing under
/// an excluded directory is ever listed, stat-ed or hashed.
fn build_walker(base: &Path, gitignore: bool, exclusion: &Exclusion) -> Walk {
    let exclusion = exclusion.clone();
    let root: PathBuf = base.to_path_buf();
    let mut builder = WalkBuilder::new(base);
    builder
        .hidden(false)
        .follow_links(false)
        .ignore(false)
        .git_ignore(gitignore)
        .git_global(gitignore)
        .git_exclude(gitignore)
        .parents(gitignore)
        .filter_entry(move |entry| skip_git_dir(entry) && !excluded(entry, &root, &exclusion));
    if gitignore {
        builder.add_custom_ignore_filename(".gitignore");
    }
    builder.build()
}

fn skip_git_dir(entry: &DirEntry) -> bool {
    entry.file_name() != ".git"
}

fn excluded(entry: &DirEntry, root: &Path, exclusion: &Exclusion) -> bool {
    let Ok(rel) = entry.path().strip_prefix(root) else {
        return false;
    };
    let rel = normalize(&rel.to_string_lossy());
    !rel.is_empty() && exclusion.matches(&rel)
}

fn normalize(path: &str) -> String {
    path.replace('\\', "/")
}

#[cfg(test)]
mod tests {
    use std::path::Path;

    use super::{GlobGroup, expand, expand_groups, is_glob};

    fn touch(dir: &Path, name: &str) {
        std::fs::write(dir.join(name), b"x").expect("write");
    }

    fn group(pattern: &str, gitignore: bool) -> GlobGroup {
        GlobGroup {
            globs: vec![pattern.to_owned()],
            exclude: Vec::new(),
            gitignore,
        }
    }

    /// A tree with `/target` git-ignored: one tracked source, one ignored
    /// build artifact, and one ignored source next to it.
    fn artifact_tree() -> tempfile::TempDir {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(dir.path().join(".gitignore"), "/target\n").expect("gitignore");
        std::fs::create_dir(dir.path().join("target")).expect("mkdir");
        touch(dir.path(), "kept.rs");
        std::fs::write(dir.path().join("target/lcov.info"), b"x").expect("write");
        std::fs::write(dir.path().join("target/built.rs"), b"x").expect("write");
        dir
    }

    #[test]
    fn detects_glob_patterns() {
        assert!(is_glob("src/*.rs"));
        assert!(!is_glob("plain/path.rs"));
    }

    #[test]
    fn globs_sort_dedup_and_respect_depth() {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::create_dir(dir.path().join("sub")).expect("mkdir");
        touch(dir.path(), "top.rs");
        std::fs::write(dir.path().join("sub/nested.rs"), b"x").expect("write");

        let shallow = expand(&["*.rs".to_owned()], dir.path(), true).expect("resolve");
        assert_eq!(shallow, vec!["top.rs".to_owned()], "single star stays flat");

        let deep = expand(&["**/*.rs".to_owned()], dir.path(), true).expect("resolve");
        assert_eq!(deep, vec!["sub/nested.rs".to_owned(), "top.rs".to_owned()]);
    }

    #[test]
    fn gitignore_filters_globs_but_not_literals() {
        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::write(dir.path().join(".gitignore"), "/target\n").expect("gitignore");
        std::fs::create_dir(dir.path().join("target")).expect("mkdir");
        touch(dir.path(), "kept.rs");
        std::fs::write(dir.path().join("target/built.rs"), b"x").expect("write");

        let on = expand(&["**/*.rs".to_owned()], dir.path(), true).expect("resolve");
        assert_eq!(on, vec!["kept.rs".to_owned()], "ignored path dropped");

        let literal = expand(&["target/built.rs".to_owned()], dir.path(), true).expect("resolve");
        assert_eq!(
            literal,
            vec!["target/built.rs".to_owned()],
            "literal overrides ignore"
        );
    }

    #[test]
    fn a_group_that_opted_out_sees_an_ignored_file() {
        let dir = artifact_tree();
        let found = expand_groups(&[group("target/**", false)], dir.path()).expect("resolve");
        assert_eq!(
            found,
            vec!["target/built.rs".to_owned(), "target/lcov.info".to_owned()],
            "gitignore off, so the artifact tree is walked"
        );
    }

    #[test]
    fn a_sibling_group_keeps_filtering_in_the_same_call() {
        let dir = artifact_tree();
        let found = expand_groups(
            &[group("**/*.rs", true), group("target/*.info", false)],
            dir.path(),
        )
        .expect("resolve");
        assert_eq!(
            found,
            vec!["kept.rs".to_owned(), "target/lcov.info".to_owned()],
            "the artifact resolves while its neighbour `target/built.rs` stays filtered out"
        );
    }

    #[test]
    fn one_group_resolves_exactly_like_a_bare_expand() {
        let dir = artifact_tree();
        let patterns = ["**/*.rs".to_owned()];
        assert_eq!(
            expand_groups(&[group("**/*.rs", true)], dir.path()).expect("groups"),
            expand(&patterns, dir.path(), true).expect("bare"),
            "the array form's behaviour is unchanged"
        );
    }

    #[test]
    fn overlapping_groups_yield_each_path_once() {
        let dir = artifact_tree();
        let found = expand_groups(
            &[group("**/*.rs", true), group("**/*.rs", false)],
            dir.path(),
        )
        .expect("resolve");
        assert_eq!(
            found,
            vec!["kept.rs".to_owned(), "target/built.rs".to_owned()],
            "a path matched by both groups is not digested twice"
        );
    }

    #[test]
    fn missing_literal_is_skipped() {
        let dir = tempfile::tempdir().expect("tempdir");
        let gone = expand(&["absent.txt".to_owned()], dir.path(), true).expect("resolve");
        assert!(gone.is_empty(), "missing literal is not an error");
    }

    #[test]
    #[cfg(unix)]
    fn glob_includes_symlinked_file_like_literal_does() {
        use std::os::unix::fs::symlink;

        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::create_dir(dir.path().join("real")).expect("mkdir");
        touch(&dir.path().join("real"), "target.rs");
        symlink(
            dir.path().join("real/target.rs"),
            dir.path().join("link.rs"),
        )
        .expect("symlink");

        let via_glob = expand(&["*.rs".to_owned()], dir.path(), true).expect("resolve glob");
        let via_literal =
            expand(&["link.rs".to_owned()], dir.path(), true).expect("resolve literal");

        assert!(
            via_glob.contains(&"link.rs".to_owned()),
            "glob should include a symlink that resolves to a file, same as a literal path: {via_glob:?}"
        );
        assert_eq!(
            via_glob
                .iter()
                .filter(|path| *path == "link.rs")
                .collect::<Vec<_>>(),
            via_literal.iter().collect::<Vec<_>>(),
            "glob and literal resolution must agree on a symlinked file"
        );
    }

    #[test]
    #[cfg(unix)]
    fn glob_does_not_traverse_symlinked_directory() {
        use std::os::unix::fs::symlink;

        let dir = tempfile::tempdir().expect("tempdir");
        std::fs::create_dir(dir.path().join("real_dir")).expect("mkdir");
        touch(&dir.path().join("real_dir"), "inside.rs");
        symlink(dir.path().join("real_dir"), dir.path().join("linked_dir")).expect("symlink");
        touch(dir.path(), "top.rs");

        let matches = expand(&["**/*.rs".to_owned()], dir.path(), true).expect("resolve");

        assert!(
            !matches.iter().any(|path| path.starts_with("linked_dir/")),
            "walker must not descend into a symlinked directory: {matches:?}"
        );
        assert_eq!(
            matches,
            vec!["real_dir/inside.rs".to_owned(), "top.rs".to_owned()],
            "only the real tree and top-level file are found, not the symlinked dir's contents a second time"
        );
    }
}