rust/rhg/src/commands/debugrhgsparse.rs
author Raphaël Gomès <rgomes@octobus.net>
Tue, 15 Nov 2022 00:02:43 +0100
changeset 49639 37bc3edef76f
parent 49484 85f5d11c77dd
child 49914 58074252db3c
permissions -rw-r--r--
rhg: upgrade `clap` dependency This one is the worst one to upgrade since v2 -> v4 broke a ton of API, which thankfully seems saner now. Contrary to what was done in the `hg-core/src/examples/nodemap` rewrite, we're not switching from the "builder" pattern to the "derive" pattern, since that would imply a much larger diff. It can be done incrementally.

use std::{
    ffi::{OsStr, OsString},
    os::unix::prelude::OsStrExt,
};

use crate::error::CommandError;
use hg::{self, utils::hg_path::HgPath};

pub const HELP_TEXT: &str = "";

pub fn args() -> clap::Command {
    clap::command!("debugrhgsparse")
        .arg(
            clap::Arg::new("files")
                .value_name("FILES")
                .required(true)
                .num_args(1..)
                .value_parser(clap::value_parser!(std::ffi::OsString))
                .help("Files to check against sparse profile"),
        )
        .about(HELP_TEXT)
}

pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
    let repo = invocation.repo?;

    let (matcher, _warnings) = hg::sparse::matcher(&repo).unwrap();
    let files = invocation.subcommand_args.get_many::<OsString>("files");
    if let Some(files) = files {
        let files: Vec<&OsStr> = files
            .filter(|s| !s.is_empty())
            .map(|s| s.as_os_str())
            .collect();
        for file in files {
            invocation.ui.write_stdout(b"matches: ")?;
            invocation.ui.write_stdout(
                if matcher.matches(HgPath::new(file.as_bytes())) {
                    b"yes"
                } else {
                    b"no"
                },
            )?;
            invocation.ui.write_stdout(b" | file: ")?;
            invocation.ui.write_stdout(file.as_bytes())?;
            invocation.ui.write_stdout(b"\n")?;
        }
    }
    Ok(())
}