rust/rhg/src/commands/cat.rs
author Simon Sapin <simon.sapin@octobus.net>
Mon, 15 Feb 2021 20:05:32 +0100
changeset 46631 80840b651721
parent 46555 d8730ff51d5a
child 46632 5ce2aa7c2ad5
permissions -rw-r--r--
rhg: Group values passed to every sub-command into a struct The set of which values this is is evidently not stable yet, so this will make changes easier. Also it is growing, and the function signatures are getting out hand. Differential Revision: https://phab.mercurial-scm.org/D10003
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46502
3e2d539d0d1a rust: remove `FooError` structs with only `kind: FooErrorKind` enum field
Simon Sapin <simon.sapin@octobus.net>
parents: 46167
diff changeset
     1
use crate::error::CommandError;
46553
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
     2
use clap::Arg;
46504
252d1bdba33d rhg: replace `map_*_error` functions with `From` impls
Simon Sapin <simon.sapin@octobus.net>
parents: 46502
diff changeset
     3
use hg::operations::cat;
46167
8a4914397d02 rust: introduce Repo and Vfs types for filesystem abstraction
Simon Sapin <simon.sapin@octobus.net>
parents: 46136
diff changeset
     4
use hg::repo::Repo;
45548
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     5
use hg::utils::hg_path::HgPathBuf;
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     6
use micro_timer::timed;
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     7
use std::convert::TryFrom;
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     8
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
     9
pub const HELP_TEXT: &str = "
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    10
Output the current or given revision of files
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    11
";
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    12
46553
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    13
pub fn args() -> clap::App<'static, 'static> {
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    14
    clap::SubCommand::with_name("cat")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    15
        .arg(
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    16
            Arg::with_name("rev")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    17
                .help("search the repository as it is in REV")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    18
                .short("-r")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    19
                .long("--revision")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    20
                .value_name("REV")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    21
                .takes_value(true),
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    22
        )
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    23
        .arg(
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    24
            clap::Arg::with_name("files")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    25
                .required(true)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    26
                .multiple(true)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    27
                .empty_values(false)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    28
                .value_name("FILE")
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    29
                .help("Activity to start: activity@category"),
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    30
        )
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    31
        .about(HELP_TEXT)
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    32
}
1ecaf09d9964 rhg: Move subcommand CLI arguments definitions to respective modules
Simon Sapin <simon.sapin@octobus.net>
parents: 46552
diff changeset
    33
46552
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    34
#[timed]
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
    35
pub fn run(invocation: &crate::CliInvocation) -> Result<(), CommandError> {
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
    36
    let rev = invocation.subcommand_args.value_of("rev");
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
    37
    let file_args = match invocation.subcommand_args.values_of("files") {
46552
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    38
        Some(files) => files.collect(),
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    39
        None => vec![],
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    40
    };
45548
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    41
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
    42
    let repo = Repo::find(invocation.non_repo_config, invocation.repo_path)?;
46552
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    43
    let cwd = hg::utils::current_dir()?;
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    44
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    45
    let mut files = vec![];
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    46
    for file in file_args.iter() {
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    47
        // TODO: actually normalize `..` path segments etc?
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    48
        let normalized = cwd.join(&file);
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    49
        let stripped = normalized
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    50
            .strip_prefix(&repo.working_directory_path())
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    51
            // TODO: error message for path arguments outside of the repo
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    52
            .map_err(|_| CommandError::abort(""))?;
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    53
        let hg_file = HgPathBuf::try_from(stripped.to_path_buf())
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    54
            .map_err(|e| CommandError::abort(e.to_string()))?;
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    55
        files.push(hg_file);
45548
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    56
    }
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    57
46552
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    58
    match rev {
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    59
        Some(rev) => {
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    60
            let data = cat(&repo, rev, &files).map_err(|e| (e, rev))?;
46631
80840b651721 rhg: Group values passed to every sub-command into a struct
Simon Sapin <simon.sapin@octobus.net>
parents: 46555
diff changeset
    61
            invocation.ui.write_stdout(&data)?;
46552
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    62
            Ok(())
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    63
        }
184e46550dc8 rhg: replace command structs with functions
Simon Sapin <simon.sapin@octobus.net>
parents: 46543
diff changeset
    64
        None => Err(CommandError::Unimplemented.into()),
45548
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    65
    }
33ded2d3f4fc rhg: add a limited `rhg cat -r` subcommand
Antoine Cezar <antoine.cezar@octobus.net>
parents:
diff changeset
    66
}